搜索
bottom↓
回复: 14

菜鸟PIC16按键LED不亮,求解

[复制链接]

出0入0汤圆

发表于 2014-3-1 19:40:51 | 显示全部楼层 |阅读模式
本帖最后由 wang479871 于 2014-3-1 19:42 编辑

//我想通过PIC16F883单片机的RA1管脚按键输入,RB6管脚接LED做指示灯
//我已经用万用表测了RA1管脚信号电压不管是0V还是5V,RB6LED都不亮,如果PORTB=0xFF,则LED亮
//编译环境为 MPLAB IDEv8.7,编译器用HI-TECH C,下面这是单片机配置位

#include <pic.h>
#define uchar unsigned char
#define uint  unsigned int

void Port_Init()
{
        TRISA |= 0x02;        //RA1管脚配置输入
        TRISB &= ~0x40;        //RB6管脚配置输出
        INTCON = 0x00;   //关闭所有中断
}


void main()
{
        Port_Init();
        while(1)
        {
                if(PORTA & 0x02)
                        PORTB |= 0x40;
                else
                        PORTB &= ~0x40;        
        }
}

//请教LED不亮的原因是程序不对,还是配置位不对,

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x

阿莫论坛20周年了!感谢大家的支持与爱护!!

一只鸟敢站在脆弱的枝条上歇脚,它依仗的不是枝条不会断,而是自己有翅膀,会飞。

出0入0汤圆

 楼主| 发表于 2014-3-1 19:48:06 | 显示全部楼层
之前接触过些其它单片机,一直对PIC单片机很敬仰没有时间入手,我写过些简单的PIC18F4620程序,PIC16单片机光是编译器都画了好长时间从下载到安装再到编译通过,网上也搜了好多示例程序,要么编译出错,要么就是编译下载后不对

出0入0汤圆

发表于 2014-3-1 20:01:08 | 显示全部楼层
多看手册,设置OSCCON

出0入0汤圆

发表于 2014-3-1 22:39:51 | 显示全部楼层
你没有把I/O脚设置为数字口,PIC单片机引脚有模拟功能,商店默认是模拟口

出0入0汤圆

 楼主| 发表于 2014-3-1 23:01:48 | 显示全部楼层
本帖最后由 wang479871 于 2014-3-1 23:04 编辑
wavelee123 发表于 2014-3-1 22:39
你没有把I/O脚设置为数字口,PIC单片机引脚有模拟功能,商店默认是模拟口


真是太感谢你了,我加了个ANSEL &= ~0x02;就可以了。
我仔细看了下手册配置IO端口


之前接触其它单片机的时候,由于时间比较充裕,我都是先看芯片手册后,才开始写程序的,
现在倒过来,先在网上找些示例程序,从点亮LED开始,这样虽然进度会快些,但是一旦碰到问题,就会卡半天....

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x

出0入0汤圆

发表于 2014-4-16 14:59:20 | 显示全部楼层
   顶一下

出0入0汤圆

发表于 2014-4-22 18:53:19 | 显示全部楼层
瞧一瞧看一看

出0入0汤圆

发表于 2014-4-28 11:23:33 | 显示全部楼层
路过路过 顺便看看

出0入0汤圆

发表于 2014-5-4 16:42:16 | 显示全部楼层
ANSEL 寄存器 专坑PIC刚入门新手

出0入0汤圆

发表于 2014-5-6 14:46:33 | 显示全部楼层
ANSEL         

出0入0汤圆

发表于 2014-12-27 16:42:41 | 显示全部楼层
受教了。原来如此

出0入0汤圆

发表于 2014-12-30 15:35:27 | 显示全部楼层
很容易忽略的问题!

出0入0汤圆

发表于 2017-2-10 08:40:15 | 显示全部楼层
wang479871 发表于 2014-3-1 23:01
真是太感谢你了,我加了个ANSEL &= ~0x02;就可以了。
我仔细看了下手册配置IO端口

楼主,以下我根据你的代码改的,根据RC2的电平来控制RC0的电平,可是实际上测试RC0一直都是低电平,我还有什么问题吗?查了2天资料都没有发现


#include <htc.h>
//__CONFIG(FOSC_ECL&WDTE_OFF);
__CONFIG(0x3fe4);
//内部晶振,禁止看门狗

void init_gpio()
{
        TRISA0=1;   //ICSPDAT        
        TRISA1=1;        //ICSPCLK
        TRISA2=1;        //DIM  INT
        TRISA3=1;         //VPP
        TRISA4=1;         //DIM  SAMP
        TRISA5=1;        //NC

        SDOSEL = 1;
        ANSELC |= 0X04;
        TRISC = 0X04;

        INTCON = 0x00;   //关闭所有中断
}

main()
{
        OSCCON=0B01101011;        //fosc=4Mhz; internal osc;
        WDTCON = 0;

        init_gpio();

        while(1)
        {
                if(PORTC & 0X04)
                        PORTC |= 0X01;
                else
                        PORTC &= ~0X01;       
        }
}

出0入0汤圆

发表于 2017-3-16 14:53:10 | 显示全部楼层
// PIC18F25K80 Configuration Bit Settings

// 'C' source line config statements

#include <xc.h>

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

// CONFIG1L
#pragma config RETEN = OFF      // VREG Sleep Enable bit (Ultra low-power regulator is Disabled (Controlled by REGSLP bit))
#pragma config INTOSCSEL = HIGH // LF-INTOSC Low-power Enable bit (LF-INTOSC in High-power mode during Sleep)
#pragma config SOSCSEL = DIG    // SOSC Power Selection and mode Configuration bits (Digital (SCLKI) mode)
#pragma config XINST = OFF      // Extended Instruction Set (Disabled)

// CONFIG1H
#pragma config FOSC = EC2     // Oscillator (Internal RC oscillator, CLKOUT function on OSC2)
#pragma config PLLCFG = OFF     // PLL x4 Enable bit (Disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor (Disabled)
#pragma config IESO = OFF       // Internal External Oscillator Switch Over Mode (Disabled)

// CONFIG2L
#pragma config PWRTEN = OFF     // Power Up Timer (Disabled)
#pragma config BOREN = OFF      // Brown Out Detect (Disabled in hardware, SBOREN disabled)
#pragma config BORV = 0         // Brown-out Reset Voltage bits (3.0V)
#pragma config BORPWR = ZPBORMV // BORMV Power level (ZPBORMV instead of BORMV is selected)

// CONFIG2H
#pragma config WDTEN = SWDTDIS  // Watchdog Timer (WDT enabled in hardware; SWDTEN bit disabled)
#pragma config WDTPS = 1048576  // Watchdog Postscaler (1:1048576)

// CONFIG3H
#pragma config CANMX = PORTB    // ECAN Mux bit (ECAN TX and RX pins are located on RB2 and RB3, respectively)
#pragma config MSSPMSK = MSK7   // MSSP address masking (7 Bit address masking mode)
#pragma config MCLRE = ON       // Master Clear Enable (MCLR Enabled, RE3 Disabled)

// CONFIG4L
#pragma config STVREN = OFF     // Stack Overflow Reset (Disabled)
#pragma config BBSIZ = BB1K     // Boot Block Size (1K word Boot Block size)

// CONFIG5L
#pragma config CP0 = OFF        // Code Protect 00800-01FFF (Disabled)
#pragma config CP1 = OFF        // Code Protect 02000-03FFF (Disabled)
#pragma config CP2 = OFF        // Code Protect 04000-05FFF (Disabled)
#pragma config CP3 = OFF        // Code Protect 06000-07FFF (Disabled)

// CONFIG5H
#pragma config CPB = OFF        // Code Protect Boot (Disabled)
#pragma config CPD = OFF        // Data EE Read Protect (Disabled)

// CONFIG6L
#pragma config WRT0 = OFF       // Table Write Protect 00800-01FFF (Disabled)
#pragma config WRT1 = OFF       // Table Write Protect 02000-03FFF (Disabled)
#pragma config WRT2 = OFF       // Table Write Protect 04000-05FFF (Disabled)
#pragma config WRT3 = OFF       // Table Write Protect 06000-07FFF (Disabled)

// CONFIG6H
#pragma config WRTC = OFF       // Config. Write Protect (Disabled)
#pragma config WRTB = OFF       // Table Write Protect Boot (Disabled)
#pragma config WRTD = OFF       // Data EE Write Protect (Disabled)

// CONFIG7L
#pragma config EBTR0 = OFF      // Table Read Protect 00800-01FFF (Disabled)
#pragma config EBTR1 = OFF      // Table Read Protect 02000-03FFF (Disabled)
#pragma config EBTR2 = OFF      // Table Read Protect 04000-05FFF (Disabled)
#pragma config EBTR3 = OFF      // Table Read Protect 06000-07FFF (Disabled)

// CONFIG7H
#pragma config EBTRB = OFF      // Table Read Protect Boot (Disabled)

#define INT8U  unsigned char
#define _XTAL_FREQ 1000000UL

#define uchar unsigned char
#define uint  unsigned int

void Port_Init()
{
        TRISA |= 0x02;        //RA1管脚配置输入
        TRISB &= ~0x40;        //RB6管脚配置输出
        INTCON = 0x00;   //关闭所有中断
}


void main()
{
        Port_Init();
        while(1)
        {
                if(PORTA & 0x02)
                        PORTB |= 0x40;
                else
                        PORTB &= ~0x40;         
        }
}

//请教LED不亮的原因是程序不对,还是配置位不对,

出0入0汤圆

发表于 2017-3-16 23:40:07 | 显示全部楼层
關類比功能、設定輸入或輸出,做按鍵輸入的腳位要上拉,輸出電晶體的要串電阻,輸出LED的串電阻限流,我PIC大概IO口這樣設定就能工作了,給您參考。
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-3-29 22:07

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表