【一个定时中断产生N个软件定时器】

一个定时中断产生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--;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值