实现时钟按键显示
作者:互联网
#include<AT89X51.h>
char display[]={0X3F,0X06,0X5B,0X4F,0X66,0X6D,0X7D,0X07,0X7F,0X6F,0XF7,0X7C,0X39,0X5E,0X79,0X71};
//char cache[6]={2,2,5,9,5,8}; //时钟的 十 分 秒
char hour=20,minute=20,second=20;
char year=21,month=12,day=20;
void DelayXms(unsigned int xms) //延时函数
{
unsigned int i,j;
for(i=xms; i>0; i--)
{
for(j=240; j>0; j--)
{ }
}
}
void DisplayCode(char value)
{
P0=display[value];
}
void screen1(void)
{
int x;
P2=0xfe;
DisplayCode(hour/10);
DelayXms(1);
P2=0xfd;
x=display[hour%10];
x|=0x80;
P0=x;
DelayXms(1);
P2=0xfb;
DisplayCode(minute/10);
DelayXms(1);
P2=0xf7;
x=display[minute%10];
x|=0x80;
P0=x;
DelayXms(1);
P2=0xef;
DisplayCode(second/10);
DelayXms(1);
P2=0xdf;
x=display[second%10];
P0=x;
DelayXms(1);
}
void screen2()
{
int x;
P2=0xfe;
DisplayCode(year/10);
DelayXms(1);
P2=0xfd;
x=display[year%10];
x|=0x80;
P0=x;
DelayXms(1);
P2=0xfb;
DisplayCode(month/10);
DelayXms(1);
P2=0xf7;
x=display[month%10];
x|=0x80;
P0=x;
DelayXms(1);
P2=0xef;
DisplayCode(day/10);
DelayXms(1);
P2=0xdf;
x=display[day%10];
P0=x;
DelayXms(1);
}
unsigned char KeyScan(void)
{
char KeyValue;
P1 = 0x0f; //P1 == 00001111
if(P1!=0x0f) //P1 != 00001111
{
switch(P1)
{
case 0x0e: //00001110 P1.0=0
P1 = 0xf0;
switch(P1)
{
case 0x70: //01110000 P1.7=0
KeyValue = 12;break;
case 0xb0: //10110000 P1.6=0
KeyValue = 8;break;
case 0xd0: //11010000 P1.5=0
KeyValue = 4;break;
case 0xe0: //11100000 P1.4=0
KeyValue = 0;break;
}
break;
case 0x0d: //00001101 P1.1=0
P1 = 0xf0;
switch(P1)
{
case 0x70: //01110000 P1.7=0
KeyValue = 13;break;
case 0xb0: //10110000 P1.6=0
KeyValue = 9;break;
case 0xd0: //11010000 P1.5=0
KeyValue = 5;break;
case 0xe0: //11100000 P1.4=0
KeyValue = 1;break;
}
break;
case 0x0b: //00001011 P1.2=0
P1 = 0xf0;
switch(P1)
{
case 0x70: //01110000 P1.7=0
KeyValue = 14;break;
case 0xb0: //10110000 P1.6=0
KeyValue = 10;break;
case 0xd0: //11010000 P1.5=0
KeyValue = 6;break;
case 0xe0: //11100000 P1.4=0
KeyValue = 2;break;
}
break;
case 0x07: //00000111 P1.3=0
P1 = 0xf0;
switch(P1)
{
case 0x70: //01110000 P1.7=0
KeyValue = 15;break;
case 0xb0: //10110000 P1.6=0
KeyValue = 11;break;
case 0xd0: //11010000 P1.5=0
KeyValue = 7;break;
case 0xe0: //11100000 P1.4=0
KeyValue = 3;break;
}
break;
}
}
return KeyValue;
}
char rn(int y)
{
if( (y%400==0) || ( (y%4==0)&&(y%100!=0) ) )
return 29;
else
return 28;
}
char judge(int year, char mon)
{
switch(mon)
{
case 4:
case 6:
case 9:
case 11:
return 30;
case 2:
return rn(year);
default:
return 31;
}
}
char i=1;
void timer0(void) interrupt 1
{
if(++i == 20)
{
i = 0;
if(++second == 60)
{
second=0;
if(++minute == 60)
{
minute=0;
if(++hour==24)
{
hour=0;
if( ++day == (judge(2000+year,month)+1) )
{
day = 1;
if(++month==13)
{
month=1;
year++;
}
}
}
}
}
}
}
void main()
{
char key=0x55;
TMOD = 0x01;//00000001,T0 mode 1,16bit
TH0 = -50000/256;
TL0 = -50000%256;
ET0 = 1; //T0中断允许标志位
EA = 1; //中断允许总标志位
TR0 = 1; //T0启动允许标志位
while(1)
{
key=KeyScan();
if(key==1)
screen1();
else if(key==2)
screen2();
}
}
标签:case,10,P1,实现,break,DelayXms,KeyValue,按键,时钟 来源: https://blog.csdn.net/qq_57918758/article/details/120563520