想用定时器1设置两个定时时间,分别是1s和10s,并且这两个时间分别对应两个时间,但是为什么在主程序的死循环里处理这两个时间就出错
程序如下:
#include <pic.h> //调用PIC16F87XA单片机的头文件
__CONFIG(HS&WDTDIS&LVPDIS);
#define uchar unsigned char
#define uint unsigned int
#define LED PORTD
char counter1=0;
char counter2=0;
int 1s_flag=0;
int 10s_flag=0;
void timer1_int();
void interrupt ISR();
void timer1_init(void)
{
OPTION=0x31;
TMR1H=0xFB; //设置10ms
TMR1L=0x1E;
TMR1IF =0;
GIE=1;
PEIE=1;
TMR1IE=1;
}
void main(void) //主函数,单片机开机后就是从这个函数开始运行
{
TRISD=0X00;//初始化RD7-RD0为输出方向
LED=0X00;
timer1_init();
while(1) //死循环,单片机初始化后,将一直运行这个死循环
{
if(1s_flag==1)
{
事件1;
}
else if(10s_flag==1)
{
事件2;
}
}
void interrupt ISR(void)
{
if(TMR1IF==1)
{
counter++;
TMR1H=0xFB;
TMR1L=0x1E;
TMR1IF=0;
if(counter==100)
{
1s_flag=1;
counter1=0;
if(counter2==10)
{
10s_flag=1;
counter2=0;
}
}
求指教啊?
|