【045】数码管显示简单电子钟2007-05-05 16:33:00
【评论】
【打印】
【字体:大 中 小】
本文链接:http://blog.pfan.cn/wentao/25599.html
sec_l equ 30h
sec_h equ 31h
bar_2 equ 32h
min_l equ 33h
min_h equ 34h
bar_5 equ 35h
hou_l equ 36h
hou_h equ 37h
sec equ 38h
min equ 39h
hou equ 40h
cou equ 41h
dis_b equ 42h
dis_r equ 43h
key_v equ 44h
key_t equ 45h
org 0000h
ajmp start
org 000bh
ajmp time0
org 001bh
ajmp time1
org 0030h
start:
mov p2,#0xff
mov p1,#0xff
mov key_v,#0xff
mov bar_2,#10
mov bar_5,#10
mov dis_b,#0x7f
mov dis_r,#0
mov sec,#0
mov min,#0
mov hou,#0
mov cou,#0
mov tmod,#00010001b
mov th0,#0xd8
mov tl0,#0xf0
mov th1,#0xfc
mov tl1,#0x18
setb ea
setb et0
setb et1
setb tr0
setb tr1
key:
mov a,p1
mov key_t,a
xrl a,key_v
jz key
lcall d_10ms
mov a,p1
mov key_t,a
xrl a,key_v
jz key
mov key_v,key_t
lcall key_to
ajmp key
key_to:
mov a,key_v
cjne a,#0xef,next
ajmp k1
next: cjne a,#0xdf,back
ajmp k2
k1: mov a,min
cjne a,#59,k1_add
mov min,#0
ajmp back
k1_add: inc min
ajmp back
k2: mov a,hou
cjne a,#23,k2_add
mov hou,#0
ajmp back
k2_add: inc hou
back: ret
time0:
push psw
push acc
inc cou
mov a,cou
cjne a,#100,over
mov cou,#0
inc sec
mov a,sec
cjne a,#60,over
mov sec,#0
inc min
mov a,min
cjne a,#60,over
mov min,#0
inc hou
mov a,hou
cjne a,#24,over
mov hou,#0
over: mov th0,#0xd8
mov tl0,#0xf0
pop acc
pop psw
reti
time1:
push psw
push acc
push b
mov a,sec
mov b,#10
div ab
mov sec_l,b
mov sec_h,a
mov a,min
mov b,#10
div ab
mov min_l,b
mov min_h,a
mov a,hou
mov b,#10
div ab
mov hou_l,b
mov hou_h,a
mov dptr,#table
mov a,#sec_l
add a,dis_r
mov r0,a
mov a,@r0
movc a,@a+dptr
mov p0,a
mov p2,dis_b
inc dis_r
anl dis_r,#0x07
mov a,dis_b
rr a
mov dis_b,a
mov th1,#0xfc
mov tl1,#0x18
pop b
pop acc
pop psw
reti
d_10ms: mov r5,#20
temp1: mov r6,#255
djnz r6,$
djnz r5,temp1
ret
table: db 0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf
end
#include <reg51.h>
#include <intrins.h>
#define uchar unsigned char
void delay_ms(uchar ms);
void key_scan();
void key_to();
uchar code dis_code[11] = {0xc0,0xf9,0xa4,0xb0,0x99,
0x92,0x82,0xf8,0x80,0x90,0xbf};
uchar data dis[8];
uchar data sec = 0;
uchar data min = 0;
uchar data hou = 0;
uchar data cou = 0;
uchar data dis_b;
uchar data dis_r;
uchar data key_v = 0;
uchar data key_t = 0;
void main()
{
P2 = 0xff;
P1 = 0xff;
dis[2] = 10;
dis[5] = 10;
dis_b = 0x7f;
dis_r = 0;
TMOD = 0x11;
TH0 = 0xd8;
TL0 = 0xf0;
TH1 = 0xfc;
TH1 = 0x18;
EA = 1;
ET0 = 1;
ET1 = 1;
TR0 = 1;
TR1 = 1;
while(1)
{
key_t = P1;
if(key_t != key_v)
{
delay_ms(10);
key_t = P1;
if(key_t != key_v)
{
key_v = key_t;
key_to();
}
}
}
}
void key_to()
{
if(key_v == 0xef)
{
if(min == 59)
min = 0;
else
min++;
}
else
{
if(key_v == 0xdf)
{
if(hou == 23)
hou = 0;
else
hou++;
}
}
}
void tiem0(void) interrupt 1
{
cou++;
if(cou == 100)
{
cou = 0;
sec++;
if(sec == 60)
{
sec = 0;
min++;
if(min == 60)
{
min = 0;
hou++;
if(hou == 23)
hou = 0;
}
}
}
TH0 = 0xd8;
TL0 = 0xf0;
}
void time1(void) interrupt 3
{
dis[0] = sec % 10;
dis[1] = sec / 10;
dis[3] = min % 10;
dis[4] = min / 10;
dis[6] = hou % 10;
dis[7] = hou / 10;
P0 = dis_code[dis[dis_r]];
P2 = dis_b;
dis_r++;
dis_r &= 0x07;
dis_b = _cror_(dis_b,1);
TH1 = 0xfc;
TL1 = 0x18;
}
void delay_ms(uchar ms)
{
uchar i;
while(ms--)
for(i = 0; i < 124; i++);
}
阅读(8902) | 评论(6)
版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!
评论