一个定时中断产生N个软件定时器
#include <reg52.h>//三个LED灯并行处理,分别闪烁0.5、1、2S
#define BLINK_TIME1 25//定时时间0.5S,每20MS中断一次
#define BLINK_TIME2 50//1S
#define BLINK_TIME3 100//2S
sbit led1=P1^0;
sbit led2=P1^2;
sbit led3=P1^4;
volatile unsigned int count1=0,count2=0,count3=0;//volatile是一个前缀的修饰关键词,用来保护主函数和中断函数公用的全局变量
volatile unsigned char timeflag1=0,timeflag2=0,timeflag3=0;/*互斥量,保护数据的安全。因为主函数和中断函数本质上是
两个独立的进程并行运行,如果主函数在对count1变量(两个字节)进行写操作时需要2个指令,在执行了第一个指令后还没有来得及
执行第2条指令时来了一个中断对count1进行写操作,这样就带来了隐患*/
unsigned char Gu8Step1=0,Gu8Step2=0,Gu8Step3=0;
void main()
{
TMOD=0x01;
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
EA=1;
ET0=1;
TR0=1;
timeflag1=0;
count1=BLINK_TIME1;
timeflag1=1;
timeflag2=0;
count2=BLINK_TIME2;
timeflag2=1;
timeflag3=0;
count3=BLINK_TIME3;
timeflag3=1;
while(1)
{
switch(Gu8Step1)//非阻塞程序框架
{
case 0:
if(0==count1)
{
led1=0;
timeflag1=0;//互斥量加锁
count1=BLINK_TIME1;
timeflag1=1;//互斥量解锁
Gu8Step1=1;
}
break;
case 1:
if(0==count1)
{
led1=1;
timeflag1=0;
count1=BLINK_TIME1;
timeflag1=1;
Gu8Step1=0;
}
break;
}
switch(Gu8Step2)
{
case 0:
if(0==count2)
{
led2=0;
timeflag2=0;
count2=BLINK_TIME2;
timeflag2=1;
Gu8Step2=1;
}
break;
case 1:
if(0==count2)
{
led2=1;
timeflag2=0;
count2=BLINK_TIME2;
timeflag2=1;
Gu8Step2=0;
}
break;
}
switch(Gu8Step3)
{
case 0:
if(0==count3)
{
led3=0;
timeflag3=0;
count3=BLINK_TIME3;
timeflag3=1;
Gu8Step3=1;
}
break;
case 1:
if(0==count3)
{
led3=1;
timeflag3=0;
count3=BLINK_TIME3;
timeflag3=1;
Gu8Step3=0;
}
break;
}
}
}/*定时器0属于硬件定时器,硬件定时器是一个母体,可以孕育出N个软件定时器按照此套路写,可以
衍生出N个"软件定时器",只要不超过单片机的RAM和ROM*/
void T0timer() interrupt 1
{
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
if(1==timeflag1 && count1>0)
{
count1--;
}
if(1==timeflag2 && count2>0)
{
count2--;
}
if(1==timeflag3 && count3>0)
{
count3--;
}
}