数码管定时器

#include <REGX52.H>
#include <INTRINS.H>
sbit BEEP=P2^5;	//½«P2.5¹Ü½Å¶¨ÒåΪBEEP
void Delay100ms()		//@11.0592MHz
{
	unsigned char i, j;

	i = 180;
	j = 73;
	do
	{
		while (--j);
	} while (--i);
}
void num(int weixuan,int duanxuan)
{
	switch (weixuan)
	{		
			case 8:
			{P2_2=1;P2_3=1;P2_4=1;
				switch (duanxuan)
				{
					case 0:P0=0x3f;break;
					case 1:P0=0x06;break;
					case 2:P0=0x5b;break;
					case 3:P0=0x4f;break;
					case 4:P0=0x66;break;
					case 5:P0=0x6d;break;
					case 6:P0=0x7d;break;
					case 7:P0=0x07;break;
					case 8:P0=0x7f;break;
					case 9:P0=0x6f;break;
				}break;
			}	
			case 7:
			{P2_2=0;P2_3=1;P2_4=1;
				switch (duanxuan)
				{
					case 0:P0=0xbf;break;
					case 1:P0=0x86;break;
					case 2:P0=0xdb;break;
					case 3:P0=0xcf;break;
					case 4:P0=0xe6;break;
					case 5:P0=0xed;break;
					case 6:P0=0xfd;break;
					case 7:P0=0x87;break;
					case 8:P0=0xff;break;
					case 9:P0=0xef;break;
				}break;
			}	
			case 6:
			{P2_2=1;P2_3=0;P2_4=1;
				switch (duanxuan)
				{
					case 0:P0=0x3f;break;
					case 1:P0=0x06;break;
					case 2:P0=0x5b;break;
					case 3:P0=0x4f;break;
					case 4:P0=0x66;break;
					case 5:P0=0x6d;break;
					case 6:P0=0x7d;break;
					case 7:P0=0x07;break;
					case 8:P0=0x7f;break;
					case 9:P0=0x6f;break;
				}break;
			}	
			case 5:
			{P2_2=0;P2_3=0;P2_4=1;
				switch (duanxuan)
				{
					case 0:P0=0x3f;break;
					case 1:P0=0x06;break;
					case 2:P0=0x5b;break;
					case 3:P0=0x4f;break;
					case 4:P0=0x66;break;
					case 5:P0=0x6d;break;
					case 6:P0=0x7d;break;
					case 7:P0=0x07;break;
					case 8:P0=0x7f;break;
					case 9:P0=0x6f;break;
				}break;
			}
	}
}
void Delay1ms()		//@11.0592MHz
{
	unsigned char i, j;

	_nop_();
	i = 2;
	j = 199;
	do
	{
		while (--j);
	} while (--i);
}
void time0_init(void)
{
	TMOD &= 0xF0;		//???????
	TMOD |= 0x01;		//???????
	TL0 = 0x66;		//??????
	TH0 = 0xFC;		//??????
	TF0 = 0;		//??TF0??
	TR0 = 1;		//???0????
	ET0=1;
	EA=1;
}
int time[4]=0;
int m,n=1;
void settime()
{
			if(P3_0==0)
			{	
				time0_init();
				time[3]++;num(8,time[0]);P0=0;num(7,time[1]);P0=0;num(6,time[2]);P0=0;num(5,time[3]);P0=0;
				if(time[3]==10){time[3]=0;time[2]++;}num(8,time[0]);P0=0;num(7,time[1]);P0=0;num(6,time[2]);P0=0;num(5,time[3]);P0=0;
				if(time[2]==10){time[2]=0;time[1]++;}num(8,time[0]);P0=0;num(7,time[1]);P0=0;num(6,time[2]);P0=0;num(5,time[3]);P0=0;
				if(time[1]==10){time[1]=0;time[0]++;}num(8,time[0]);P0=0;num(7,time[1]);P0=0;num(6,time[2]);P0=0;num(5,time[3]);P0=0;
				if(time[0]==10){time[0]=0;}num(8,time[0]);P0=0;num(7,time[1]);P0=0;num(6,time[2]);P0=0;num(5,time[3]);P0=0;
				Delay1ms();num(8,time[0]);P0=0;num(7,time[1]);P0=0;num(6,time[2]);P0=0;num(5,time[3]);P0=0;
			}
			if(P3_2==0)
			{
				if(time[0]!=0||time[1]!=0||time[2]!=0||time[3]!=0)
				{
					time[3]--;num(8,time[0]);P0=0;num(7,time[1]);P0=0;num(6,time[2]);P0=0;num(5,time[3]);P0=0;
					if(time[3]==-1){time[3]=9;time[2]--;}num(8,time[0]);P0=0;num(7,time[1]);P0=0;num(6,time[2]);P0=0;num(5,time[3]);P0=0;
					if(time[2]==-1){time[2]=9;time[1]--;}num(8,time[0]);P0=0;num(7,time[1]);P0=0;num(6,time[2]);P0=0;num(5,time[3]);P0=0;
					if(time[1]==-1){time[1]=9;time[0]--;}num(8,time[0]);P0=0;num(7,time[1]);P0=0;num(6,time[2]);P0=0;num(5,time[3]);P0=0;
					if(time[0]==-1){time[0]=9;}num(8,time[0]);P0=0;num(7,time[1]);P0=0;num(6,time[2]);P0=0;num(5,time[3]);P0=0;
					Delay1ms();num(8,time[0]);P0=0;num(7,time[1]);P0=0;num(6,time[2]);P0=0;num(5,time[3]);P0=0;
				}
			}
}
void timerun()
{
	if(time[0]!=0||time[1]!=0||time[2]!=0||time[3]!=0)
		{
			time[3]--;
			if(time[3]==-1){time[3]=9;time[2]--;}
			if(time[2]==-1){time[2]=9;time[1]--;}
			if(time[1]==-1){time[1]=9;time[0]--;}
			if(time[0]==-1){time[0]=9;}
			if(time[0]==0&&time[1]==0&&time[2]==0&&time[3]==0){n=0;}
		}
}
int b=0,c=0,EAa=0;
void main()
{
	while(n)
	{
		num(8,time[0]);
		P0=0;
		num(7,time[1]);
		P0=0;
		num(6,time[2]);
		P0=0;
		num(5,time[3]); 
		P0=0;
		settime();
	}
	while(1)
	{
		BEEP=!BEEP;num(8,time[0]);
		P0=0;
		num(7,time[1]);
		P0=0;
		num(6,time[2]);
		P0=0;
		num(5,time[3]);
		P0=0;
	}
}
void time0() interrupt 1 //¶¨Ê±Æ÷0ÖжϺ¯Êý
{
	TL0 = 0x66;
	TH0 = 0xFC;
	c++;
	b++;
	if(b==500)
	{
		if(P3_1==0){if(EAa==1)EAa=0;}
		b=0;
	}
	if(c==1000)
	{
		if(P3_1==0){if(EAa==0)EAa=1;}
		c=0;
	}
		if(EAa==1)
	{	
		m++;
		if(m==10)
		{
			timerun();
			m=0;
		}
	}	
}

数码管定时报警作业演示

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值