我使用PIC16F1946芯片,使用定时器1的定时方式,发现一个错误,程序不能按照的我要求去执行!下面是代码! 10
不知道是我的程序写错了,还是什么问题啊,请高手帮我指点下,谢谢!#include<htc.h>#defineLEDRA0unsignedinttime=0;bitflag...
不知道是我的程序写错了,还是什么问题啊,请高手帮我指点下,谢谢!
#include <htc.h>
#define LED RA0
unsigned int time = 0;
bit flag = 0;
void port_init(void)
{
__CONFIG(0x0f7c);
__CONFIG(0xcaff);
OSCCON = 0x73; //晶振设置 使用8M晶振 不使用4倍频
LCDREF = 0x00; //液晶设置
LCDEN = 0; //液晶显示设置
CM1CON0 = 0X00; //比较器设置
CM2CON0 = 0X00;
WDTCON = 0x12; //看门狗时间设置
PSA = 0; //预分频器分配给TIMER0
SWDTEN = 1; //打开看门狗
TRISA = 0x00;
PORTA = 0x00;
}
void timer1_init(void)
{
INTCON = 0x0; //中断
TMR1H = 0xD8; //定时20ms
TMR1L = 0xEF;
// TMR1H = 0xF6; //定时10ms
// TMR1L = 0x3B;
T1CON = 0x10;
TMR1IF = 0; // 中断1标志
TMR1IE = 1;
TMR1ON = 1;
CCP1IE = 0; //关闭捕捉功能
PEIE = 1;
GIE = 1;
}
void interrupt timer1(void)
{
if( TMR1IF)
{
TMR1IF = 0;
TMR1H = 0xD8;
TMR1L = 0xEF;
T1CON = 0x11;
time ++;
if(time == 51) //改变time的数值,但是LED还是1s改变一次状态
{
time = 0;
flag = ~flag;
}
}
}
void main(void)
{
port_init();
timer1_init();
while(1)
{
if(flag)
{
LED = ~LED;
}
}
} 展开
#include <htc.h>
#define LED RA0
unsigned int time = 0;
bit flag = 0;
void port_init(void)
{
__CONFIG(0x0f7c);
__CONFIG(0xcaff);
OSCCON = 0x73; //晶振设置 使用8M晶振 不使用4倍频
LCDREF = 0x00; //液晶设置
LCDEN = 0; //液晶显示设置
CM1CON0 = 0X00; //比较器设置
CM2CON0 = 0X00;
WDTCON = 0x12; //看门狗时间设置
PSA = 0; //预分频器分配给TIMER0
SWDTEN = 1; //打开看门狗
TRISA = 0x00;
PORTA = 0x00;
}
void timer1_init(void)
{
INTCON = 0x0; //中断
TMR1H = 0xD8; //定时20ms
TMR1L = 0xEF;
// TMR1H = 0xF6; //定时10ms
// TMR1L = 0x3B;
T1CON = 0x10;
TMR1IF = 0; // 中断1标志
TMR1IE = 1;
TMR1ON = 1;
CCP1IE = 0; //关闭捕捉功能
PEIE = 1;
GIE = 1;
}
void interrupt timer1(void)
{
if( TMR1IF)
{
TMR1IF = 0;
TMR1H = 0xD8;
TMR1L = 0xEF;
T1CON = 0x11;
time ++;
if(time == 51) //改变time的数值,但是LED还是1s改变一次状态
{
time = 0;
flag = ~flag;
}
}
}
void main(void)
{
port_init();
timer1_init();
while(1)
{
if(flag)
{
LED = ~LED;
}
}
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询