单片机实验代码——可调时间的时钟程序

今天单片机课上写的一个时钟程序,代码如下:

#include 
#define uint unsigned int
#define uchar unsigned char
sbit S1 = P1^3;
sbit S2 = P1^1;
sbit S3 = P1^2;
sbit S4 = P1^0;
sbit beep = P2^7;
sbit a = P0^0;
sbit b = P0^1;
sbit c = P0^2;
sbit d = P0^3;
sbit e = P0^4;
sbit f = P0^5;
sbit g = P0^6;
sbit p = P0^7;
sbit key1 = P1^4;
sbit key2 = P1^5;
sbit key3 = P3^6;
sbit key4 = P3^7;
uchar num, hour, min, sec, snum;
uchar qian, bai, shi, ge, counter;
uchar code N[10] = {0xc0, 0xf9 ,0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90}; //0, 1, 2, 3, ...8, 9
uchar code Z[10] = {0x40, 0x79 ,0x24, 0x30, 0x19, 0x12, 0x02, 0x78, 0x00, 0x10}; //0., 1., 2., ... 8., 9.

void delayms(uint xms)
{
	uint i,j;
	for(i = xms; i > 0; i--)
		for(j = 110; j > 0; j--);
}

void init()
{
	TMOD = 0x01;
	TH0 = 0x3c;
	TL0 = 0xb0;
	S1 = S2 = S3 = S4 = 0;
	P0 = N[0];
	delayms(5);

	EA = 1;
	TR0 = 0;
	ET0 = 1;	
	hour = min = sec = 0;
}

void display()
{
	if(min >= 0)
	{
		shi = min/10;
		ge = min%10;

		S4 = 0;
		S1 = S2 = S3 = 1;
		P0 = N[ge];
		delayms(5);

		S3 = 0;
		S1 = S2 = S4 = 1;
		P0 = N[shi];
	}
	delayms(5);
	if(hour >= 0)
	{
		qian = hour/10;
		bai = hour%10;

		S2 = 0;
		S1 = S3 = S4 = 1;
		P0 = Z[bai];
		delayms(5);

		S1 = 0;
		S2 = S3 = S4 = 1;
		P0 = N[qian];
	}
	delayms(5);
}

void keyscan()
{
	if(key1 == 0)
	{
		delayms(10);
		if(key1 == 0)
		{
			while(!key1);
			TR0 = ~TR0;
		}
	}
	if(key2 == 0)
	{
		delayms(10);
		if(key2 == 0)
		{
			hour = min = sec = num = 0;
			while(!key1);
		}
	}
	if(key3 == 0)//功能键,位选   
    {   
        delayms(10);   
        if(key3 == 0)   
        {   
            snum++; 
			while(!key3); //等待按键释放  
            switch(snum)
			{
				case 1:
					TR0 = 0;
					//display();
					S4 = 0;
					S1 = S2 = S3 = 1;
					P0 = N[ge];
					delayms(50);			
					S3 = 0;
					S1 = S2 = S4 = 1;
					P0 = N[shi];
					delayms(50);
					break;
				case 2:
					S2 = 0;
					S1 = S3 = S4 = 1;
					P0 = Z[bai];
					delayms(50);			
					S1 = 0;
					S2 = S3 = S4 = 1;
					P0 = N[qian];
					delayms(50);
					break;
				case 3:
					snum = 0;
					TR0 = 1;
					break;
			}
        }   
    }
	if(key4 == 0)
    {   
        delayms(10);   
        if(key4 == 0)   
        {   
            while(!key4); //等待按键释放  
            switch(snum)
			{
				case 1:
					min++;
					if(min >= 60)
						min = min - 60;
						shi = min/10;
						ge = min%10;
						S4 = 0;
						S1 = S2 = S3 = 1;
						P0 = N[ge];
						delayms(5);			
						S3 = 0;
						S1 = S2 = S4 = 1;
						P0 = N[shi];
						delayms(5);
					break;
				case 2:
					hour++;
					if(hour >= 24)
						hour = hour - 24;
					qian = hour/10;
					bai = hour%10;
					S2 = 0;
					S1 = S3 = S4 = 1;
					P0 = Z[bai];
					delayms(5);			
					S1 = 0;
					S2 = S3 = S4 = 1;
					P0 = N[qian];
					delayms(5);
					break;
				case 3:
					snum = 0;
					TR0 = 1;
					break;
			}      
        }  
    }
}

main()
{
	init();
	while(1)
	{
		keyscan();
		display();		
	}
}

void T0_time() interrupt 1
{
	TH0 = 0x3c;
	TL0 = 0xb0;
		num++;
	if(num == 20)
	{
		num = 0;
		sec = sec + 60;//这里是为了加快时钟速度
		if(sec >= 60)
		{
			sec = sec - 60;
			min = min + 1;
			if(min >= 60)
			{
				min = min - 60;
				hour = hour + 1;
				if(hour >= 24)
				{
					hour = 0;
				}
			}
		}
	}
} 

PS:准备再加点功能【1.闹钟功能(可以结合蜂鸣器);2.模式选择功能(然后把秒表模式加上)】

参考文档:郭天祥新概念51单片机源程序

                 单片机综合练习 - 多功能时钟

你可能感兴趣的:(51单片机,文档,c)