|
发表于 2023-11-16 10:46:32
|
显示全部楼层
电子时钟
#include<Reg52.h>
char a[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
char b[]={0xFF,0xFB,0xF7,0xF3,0xEF,0xEB,0xE7,0xE3};
long unsigned int n=0,cc=0;
int hour=0,minite=0,second=0;
int T1MS = 65536-50000;
sbit key1=P3^1;
sbit key2=P3^0;
sbit key3=P3^2;
sbit key4=P3^3;
void dis_play(int hour,minite,second);
void transform(int num);
void init_interrupt();
void system();
void delay(int i)
{
int x,y;
for(x=i;x>=0;x--)
for(y=0;y<40;y++);
}
void system()
{
if(key1==0)
{
delay(2);
if(key1==0)
{
n+=3600;
}
while(key1==0);
}
if(key2==0)
{
delay(2);
if(key2==0)
{
n+=60;
}
while(key2==0);
}
if(key4==0)
{
delay(2);
if(key4==0)
{
n+=1;
}
while(key4==0);
}
dis_play(hour,minite,second);
}
void transform(long unsigned int num)
{
hour=num/3600;
num-=(long unsigned int)hour*3600;
minite=num/60;
num-=(long unsigned int)minite*60;
second=num;
if(hour>=24)
{
n=0;
}
dis_play(hour,minite,second);
}
void dis_play(int hour,int minite,int second)
{
P0=0x00;
P2=0xFF;
P0=a[hour/10];
delay(2);
P0=0x00;
P2=0xFB;
P0=a[hour%10];
delay(2);
P0=0x00;
P2=0xF7;
P0=0x40;
delay(2);
P0=0x00;
P2=0xF3;
P0=a[minite/10];
delay(2);
P0=0x00;
P2=0xEF;
P0=a[minite%10];
delay(2);
P0=0x00;
P2=0xEB;
P0=0x40;
delay(2);
P0=0x00;
P2=0xE7;
P0=a[second/10];
delay(2);
P0=0x00;
P2=0xE3;
P0=a[second%10];
delay(2);
}
void init_interrupt();
void main()
{
init_interrupt();
while(1)
{
transform(n);
system();
}
}
void init_interrupt()
{
TMOD = 0x00;
TL0 = T1MS%256;
TH0 = T1MS/256;
TR0= 1;
ET0 = 1;
EA = 1;
}
void T0_interrupt() interrupt 1
{
TL0 = T1MS%256;
TH0 = T1MS/256;
cc++;
if (cc>=20)
{
cc = 0;
n++;
}
} |
|