C51简单电子钟
扫描二维码
随时随地手机看文章
/**************************************************************************************************************
Seven Segment LED Clock
COPYRIGHT (c) 2012 DESIGNED BY GARRY
-- ALL RIGHTS RESERVED --
File Name: Seven Segment LED Clock.c
Author: GARRY MENG
Created: 2012-08-18 20:17:34
Modified:
Revision: 1.0
***************************************************************************************************************/
#include
#define uchar unsigned char
#define uint unsigned int
uint counter=0;
uchar second=58;
uchar minute=33;
uchar hour=22;
uchar bitselect=1;
uchar temp=0;
uchar getBCD(uchar i)
{
switch(i)
{
case 0:i=0xc0;break;
case 1:i=0xf9;break;
case 2:i=0xa4;break;
case 3:i=0xb0;break;
case 4:i=0x99;break;
case 5:i=0x92;break;
case 6:i=0x82;break;
case 7:i=0xf8;break;
case 8:i=0x80;break;
case 9:i=0x90;break;
default:i=0xff;
}
return i;
}
void timer2()interrupt 5 using 1
{
uchar t;
TF2=0;
counter++;
if(counter==400)
{
second++;
counter=0;
if(second==60)
{
second=0;
minute++;
if(minute==60)
{
minute=0;
hour++;
if(hour==24)
{
hour=0;
}
}
}
}
switch(bitselect)
{
case 1: t=second%10;break;
case 2: t=second/10;break;
case 4: t=minute%10;break;
case 8: t=minute/10;break;
case 16:t=hour%10; break;
case 32:t=hour/10; break;
}
P1=0xff; //这一句不加Proteus仿真时数码管不亮,可能是驱动能力不足吧。实际电路是亮的,
P0=~bitselect; //但是会有上一位显示的余辉,这一句看来必须要加了。
P1=getBCD(t);
bitselect<<=1;
if(bitselect==64)
bitselect=1;
}
void timer0()interrupt 1 using 2
{
uchar keyvalue;
TL0|=0X00;
TH0=0XDC;
keyvalue=P2;
temp++;
keyvalue&=0x0f;
if(temp==1||temp==17)
{
switch(keyvalue)
{
case 0x0E:
second++;
if(second==60)
{
second=0;
minute+=1;
}
break;
case 0x0D:
minute++;
if(minute==60)
{
minute=0;
hour++;
}
break;
case 0x0B:
hour++;
if(hour==24)
hour=0;
break;
}
}
if(temp==17)
temp=2;
if(keyvalue==0x0f)
{
TR0=0;
temp=0;
}
}
main()
{
uchar key;
TH2=0Xf7;
TL2=0X00;
RCAP2H=0Xf7;
RCAP2L=0X00;
RCLK=0;
TCLK=0;
CP_RL2=0;
TMOD=0X01;
TR2=1;
PT2=1; //T2为最高级中断
EA=1; //t0 t2中断打开
ET0=1;
ET2=1;
while(1)
{
key=P2;
if(key!=0xff)
if(!TR0)
{
TH0=0XDC;
TL0=0X00;
TR0=1;
}
}
}