本人最近新接触PIC单片机,遇到一些麻烦.
采用的是MCU芯片是PIC16F1503,
开发工具MPLAB X IDE,XC8编译器.PICkit3仿真器.
暂时先先控制两个LED灯,以确认单片机工作正常,
但是把如下代码烧录后,LED没反应,经测量,单片机没输出.
请教各位前辈,这个代码是否有问题?先谢谢各位了.
// CONFIG1
#pragma config FOSC = INTOSC
#pragma config WDTE = OFF
#pragma config PWRTE = ON
#pragma config MCLRE = ON
#pragma config CP = OFF
#pragma config BOREN = OFF
#pragma config CLKOUTEN = OFF
// CONFIG2
#pragma config WRT = OFF
#pragma config STVREN = ON
#pragma config BORV = 25
#pragma config LPBOR = OFF
#pragma config LVP = OFF
#define K1P RC4
#define K1 LATC4
#define TX LATC5
#define RX RA4
#define LED1 LATC2
#define LED2 LATC3
typedef unsigned char U8;
typedef unsigned int U16;
U16 IsSendCmd,Timeout;
void System_Init()
{
OSCCON = 0b01111010;
OPTION_REG = 0b10000000;
IOCIE=0;
PEIE=0;
GIE=0;
ANSELA = 0b00000000;
ANSELC = 0b00000000;
TRISA = 0b00110100;
TRISC = 0b00000000;
PORTA = 0b00000000;
PORTC = 0b00000000;
/* Port Interrupt Config */
IOCAF2 = 0; //INT_Pin
IOCAP2=1; //Positive edge
IOCAN2=1; //Negative edge
IOCAF4 = 0; //RX_Pin
IOCAP4=1;
IOCAN4=1;
IOCAF5 = 0; //ZERO_Pin
IOCAP5=1;
IOCAN5=1;
/* TIME2 Config */
PR2=0x7D; //125
T2CON=0x02; //16M 1:16 Fosc/4, -> 0.25MHz -> 4us
TMR2=0;
TMR2ON=1;
TMR2IE=1;
IOCIE=1;
PEIE=1;
GIE=1;
}
void interrupt ISR(void)
{
if(TMR2IE&&TMR2IF) //Timer2 500us
{
TMR2IF=0;
Timeout ++;
if(Timeout >= 2000) // 1S
{
Timeout = 0;
IsSendCmd = 1;
}
}
}
void main()
{
System_Init();
while(1)
{
if(IsSendCmd)
{
IsSendCmd = 0;
if(LED1 == 1)
{
LED1 = 0;
LED2 = 1;
}
else
{
LED1 = 1;
LED2 = 0;
}
}
}
}
|