51单片机制作简易电子钟,求电路图和程序,要求6位显示,24小时制_百度知 ...
发布网友
发布时间:2024-10-19 05:30
我来回答
共1个回答
热心网友
时间:2024-11-30 06:54
#include "reg51.h"
#define uint unsigned int
#define uchar unsigned char
unsigned char code led[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};uchar sec,min,hour;
bit secup=00;
uchar n=0;
void delay(unsigned int ms)
{
unsigned int i=ms*91;
for(;i>0;i--);
}void disp(unsigned char sec,min,hour)
{
uchar i;
unsigned char num[6];
num[0]=sec%10;num[1]=sec/10;
num[2]=min%10;num[3]=min/10;
num[4]=hour%10;num[5]=hour/10; P2=1;
for(i=0;i<6;i++)
{
P0=led[num[i]];
delay(3);
P0=0;
P2<<=1;
}
} void main()
{
TMOD=0x11;
TH0=0x3c;
TL0=0xb0;
EA=1;
ET0=1;
EX0=1;EX1=1;
IT0=1;IT1=1;
TR0=1;
while(1)
{
if(secup==1)
{
secup=0;
sec++;
if(sec>=60)
{
sec=0;
min++;
if(min>=60)
{
min=0;
hour++;
if(hour>=24) hour=0;
}
}
}
disp(sec,min,hour);
}
}
void t0_int() interrupt 1 using 1
{
static unsigned char k;
TH0=0x3c;
TL0=0xb0;
k++;
if(k>=20)
{
k=0;
secup=1;
}
} void it0_int() interrupt 0
{ delay(1);
n++;
}
void it1_int() interrupt 2
{
delay(1);
if(n==1)
hour++;
else if(n==2)
min++;
}