菜鸟PIC16按键LED不亮,求解
本帖最后由 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 uintunsigned 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不亮的原因是程序不对,还是配置位不对,
之前接触过些其它单片机,一直对PIC单片机很敬仰没有时间入手,我写过些简单的PIC18F4620程序,PIC16单片机光是编译器都画了好长时间从下载到安装再到编译通过,网上也搜了好多示例程序,要么编译出错,要么就是编译下载后不对 多看手册,设置OSCCON 你没有把I/O脚设置为数字口,PIC单片机引脚有模拟功能,商店默认是模拟口 本帖最后由 wang479871 于 2014-3-1 23:04 编辑
wavelee123 发表于 2014-3-1 22:39
你没有把I/O脚设置为数字口,PIC单片机引脚有模拟功能,商店默认是模拟口
真是太感谢你了,我加了个ANSEL &= ~0x02;就可以了。
我仔细看了下手册配置IO端口
之前接触其它单片机的时候,由于时间比较充裕,我都是先看芯片手册后,才开始写程序的,
现在倒过来,先在网上找些示例程序,从点亮LED开始,这样虽然进度会快些,但是一旦碰到问题,就会卡半天.... 顶一下 瞧一瞧看一看 路过路过 顺便看看 {:lol:} ANSEL 寄存器 专坑PIC刚入门新手 ANSEL 受教了。{:lol:}原来如此 很容易忽略的问题! 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; //DIMINT
TRISA3=1; //VPP
TRISA4=1; //DIMSAMP
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;
}
} // 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 INT8Uunsigned char
#define _XTAL_FREQ 1000000UL
#define uchar unsigned char
#define uintunsigned 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不亮的原因是程序不对,还是配置位不对, 關類比功能、設定輸入或輸出,做按鍵輸入的腳位要上拉,輸出電晶體的要串電阻,輸出LED的串電阻限流,我PIC大概IO口這樣設定就能工作了,給您參考。
页:
[1]