51单片机-利用定时器设计的简单时钟

此时钟利用的是定时器的简单时钟,可以利用独立键盘进行分,时的调整,并利用数码管进行显示

#include<reg52.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit wela=P2^7;
sbit dula=P2^6;
sbit s2=P3^0;
sbit s3=P3^1;
sbit s4=P3^2;
sbit s5=P3^3;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,0x40,0x40};
uint num,num1,num2,num3,flag=0;
void delay(uint z)
{
 	uint i,j;
	for(i=z;i>0;i--)
		for(j=110;j>0;j--);
}
/*void led()
{
	delay(5);
	P1=_crol_(P1,1);	
}*/
void display(uint shi,fen,miao)
{
	uint shi_s,shi_g,fen_s,fen_g,miao_s,miao_g;
	shi_s=shi/10;
	shi_g=shi%10;

	fen_s=fen/10;
	fen_g=fen%10;

	miao_s=miao/10;
	miao_g=miao%10;
	dula=1;
	P0=table[shi_s];
	dula=0;
	P0=0xff;
	wela=1;
	P0=0xfe;
	wela=0;
	delay(2);
	
	dula=1;
	P0=table[shi_g];
	dula=0;
	P0=0xff;
	wela=1;
	P0=0xfd;
	wela=0;
	delay(2);
	
	dula=1;
	P0=table[16];
	dula=0;
	P0=0xff;
	wela=1;
	P0=0xdf;
	wela=0;
	delay(2);//横杠

	dula=1;
	P0=table[fen_s];
	dula=0;
	P0=0xff;
	wela=1;
	P0=0xf7;
	wela=0;
	delay(2);
	
	dula=1;
	P0=table[fen_g];
	dula=0;
	P0=0xff;
	wela=1;
	P0=0xef;
	wela=0;
	delay(2);

	dula=1;
	P0=table[16];
	dula=0;
	P0=0xff;
	wela=1;
	P0=0xfb;
	wela=0;
	delay(2);//横杠

	dula=1;
	P0=table[miao_s];
	dula=0;
	P0=0xff;
	wela=1;
	P0=0xbf;
	wela=0;
	delay(2);
	
	dula=1;
	P0=table[miao_g];
	dula=0;
	P0=0xff;
	wela=1;
	P0=0x7f;
	wela=0;
	delay(2);
		
}
void keyscan()
{
	if(s2==0)
	{
		delay(10);
		if(s2==0)
		{
			while(!s2);
			TR0=~TR0;
		}
	}
	if(s3==0)
	{
		delay(10);
		if(s3==0)
		{
			num2++;
			if(num2==60)
				num2=0;
				while(!s3);	
		}			   
	}
	if(s4==0)
	{
		delay(10);
		if(s4==0)
		{
		num3++;
		if(num3==24)
			num3=0;
			while(!s4);
		}
	}
	/*if(s5==0)
	{
		delay(10);
		if(s5==0)
		{
			while(!s5);
			flag=~flag;
		}	
	}*/	
}
void init()
{
	TMOD=0x01;
	TH0=(65536-45872)/256;
	TL0=(65536-45872)%256;
	EA=1;
	ET0=1;
	TR0=0;	
}
void main()
{
	P1=0xfe;
	init();
	while(1)
	{
	keyscan();
	display(num3,num2,num1);
	}	
	}	
void time0() interrupt 1
{
	TH0=(65536-45872)/256;
	TL0=(65536-45872)%256;
	num++;
	if(num==20)
	{
		num=0;
		num1++;
	}
	if(num1==60)
		{
			num1=0;
			num2++;
			if(num2==60)
			{
				num2=0;
				num3++;
				if(num3==24)
				{
					num3=0;
				}
			}
		}
}


©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页