#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
#define temp P3;
sfr P4=0xc0;
sbit p42=P4^2;
sbit p44=P4^4;
uchar code duan[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xff};
uchar wei[]={0,0,0,0,0,0,0,0};
uchar discom=0;
uint sec=50,min=59,hour=23,ms=0,t=0;
sfr AUXR=0x8e;
uchar value;
void keyscan();
void cls();
void Timer0Init(void);
void display();
void clock();
void delay(uint x)
{
uint i;
while(x--)
for(i=0;i<12;i++);
}
void main()
{
cls();
Timer0Init();
ET0=1;
EA=1;
while(1)
{
display();
keyscan();
}
}
void Timer0Init(void) //100微秒@11.0592MHz
{
AUXR |= 0x80; //定時器時鐘1T模式
TMOD &= 0xF0; //設置定時器模式
TL0 = 0xAE; //設置定時初值
TH0 = 0xFB; //設置定時初值
TR0 = 1; //定時器0開始計時
}
void cls()
{
P0=0X00;
P2=(P2&0X1F|0XA0);
P2&=0X1F;
P0=0XFF;
P2=(P2&0X1F|0X80);
P2&=0X1F;
}
void Timer0() interrupt 1
{
ms++;
clock();
}
void display()
{
wei[0]=hour/10;
wei[1]=hour%10;
wei[2]=10;
wei[3]=min/10;
wei[4]=min%10;
wei[5]=10;
wei[6]=sec/10;
wei[7]=sec%10;
P0=0XFF;
P2=(P2&0X1F|0XC0);
P2&=0X1F;
P0=1<<discom;
P2=(P2&0X1F|0XC0);
P2&=0X1F;
P0=duan[wei[discom]];
P2=(P2&0X1F|0XE0);
if(++discom==8)
{
discom=0;
}
}
void clock()
{
if(ms==10)
{
ms=0;
t++;
if(t==1000)
{
t=0;
sec++;
if(sec==60)
{
sec=0;
min++;
if(min==60)
{
min=0;
hour++;
if(hour==24)
{
hour=0;
}
}
}
}
}
}
void keyscan()
{
temp=0x0f;p42=0;p44=0;
if(temp!=0x0f)
{
delay(2);
if(temp!=0x0f);
{
switch(temp)
{
case 0x0e:value=1;break;
case 0x0d:value=5;break;
case 0x0b:value=9;break;
case 0x07:value=13;break;
}
temp=0xf0;
p44=1;
p42=1;
if(p44==0){
value+=0;while(p44==0);}
if(p42==0){
value+=1;while(p42==0);}
if(temp==0xd0){
value+=2;while(temp==0xb0);}
if(temp==0xe0){
value+=3;while(temp==0xe0);}
wei[1]=11;
wei[2]=11;
wei[3]=11;
wei[4]=value/10;
wei[5]=value%10;
wei[6]=11;
wei[7]=11;
wei[8]=11;
}
}
}
捕獲.PNG (69.25 KB, 下載次數: 30)
下載附件
2019-3-21 22:28 上傳
|