51单片机(24小时的计时器) 使用51单片机(STC89C52),晶振为11.0592MHz。利用定时器0中断,获得50ms的时间为基准来记录时间。再用60进制换算成时分秒最后拆分成数字用数码管动态显示出来。
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
#define n 1
sbit dula=P2^6;
sbit wela=P2^7;
sbit d1=P1^0;
sbit D=P0^7;
uchar num,h1,h2,m1,m2,s1,s2,t1,t2,t3,;
void Delay1ms()
{
unsigned char i, j;
i = 5;
j = 4;
do
{
while (--j);
} while (--i);
}
void delay(uint z)
{
uchar k;
for(k=z;k>0;k--)
Delay1ms();
}
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void look();
void main()
{
TMOD=0x01;
TH0=(65536-46080)/256;
TL0=(65536-46080)%256;
EA=1;
ET0=1;
TR0=1;
while(1)
{
look();
}
}
void exter0() interrupt 1
{
TH0=(65536-46080)/256;
TL0=(65536-46080)%256;
num++;
if(num==20)
{
num=0;
t1++;
s2=t1%10;
s1=t1/10;
if(t1==60)
{ t1=0;
t2++;
m2=t2%10;
m1=t2/10;
if(t2==60)
{
t2=0;
t3++;
h2=t3%10;
h1=t3/10;
if(t3==24)
t3=0;
}
}
d1=~d1;
}
}
void look()
{
dula=1;
P0=table[s2];
dula=0;
P0=0xff;
wela=1;
P0=0xdf;
wela=0;
delay(n);
dula=1;
P0=table[s1];
dula=0;
P0=0xff;
wela=1;
P0=0xef;
wela=0;
delay(n);
wela=1;
P0=0xf7;
wela=0;
dula=1;
P0=table[m2];
D=1;
dula=0;
P0=0xff;
delay(n);
wela=1;
P0=0xfb;
wela=0;
dula=1;
P0=table[m1];
dula=0;
P0=0xff;
delay(n);
wela=1;
P0=0xfd;
wela=0;
dula=1;
P0=table[h2];
D=1;
dula=0;
P0=0xff;
delay(n);
wela=1;
P0=0xfe;
wela=0;
dula=1;
P0=table[h1];
dula=0;
P0=0xff;
delay(n);
}
记录自己自学单片机的第一个程序,可能有错误还望指导、、、、、、
|