【039】两位数码管对直控按键计数2007-05-05 16:25:00
【评论】
【打印】
【字体:大 中 小】
本文链接:http://blog.pfan.cn/wentao/25593.html
dis_0 equ 30h
dis_1 equ 31h
dis_b equ 32h
dis_r equ 33h
count equ 34h
key_v equ 35h
key_t equ 36h
org 0000h
ajmp start
org 000bh
ajmp time0
org 0030h
start: mov count,#0
mov p2,#0xff
mov dis_b,#0x7f
mov dis_r,#0
mov tmod,#00000001b
mov th0,#0xfc
mov tl0,#0x18
setb ea
setb et0
setb tr0
lop: lcall key_scan
lcall key_to
ajmp lop
key_scan:
mov a,p1
mov key_t,a
xrl a,key_v
jz key_scan
lcall d_10ms
mov a,p1
mov key_t,a
xrl a,key_v
jz key_scan
mov key_v,key_t
ret
key_to:
mov a,key_v
cjne a,#0xef,next
ajmp key_1
next: cjne a,#0xdf,back
ajmp key_2
back: ret
key_1:
inc count
mov a,count
cjne a,#100,over_1
mov count,#0
over_1: ret
key_2:
mov a,count
cjne a,#0,over_2
mov count,#99
ret
over_2: dec count
ret
time0:
mov dptr,#table
mov a,count
mov b,#10
div ab
mov dis_0,b
mov dis_1,a
mov a,#dis_0
add a,dis_r
mov r0,a
mov a,@r0
movc a,@a+dptr
mov p0,a
mov p2,dis_b
xrl dis_r,#0x01
xrl dis_b,#0xc0
mov th0,#0xfc
mov tl0,#0x18
reti
d_10ms: mov r5,#20
temp: mov r6,#255
djnz r6,$
djnz r5,temp
ret
table: db 0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90
end
#include <reg51.h>
#define uchar unsigned char
void delay_ms(uchar ms);
void key_scan();
void key_to(uchar key_v);
uchar code dis_code[10] = {0xc0,0xf9,0xa4,0xb0,0x99,
0x92,0x82,0xf8,0x80,0x90};
uchar data dis[2];
uchar data dis_b;
uchar data dis_r;
uchar data count;
uchar data key_v;
uchar data key_t;
void main()
{
count = 0;
P2 = 0xff;
dis_b = 0x7f;
dis_r = 0;
TMOD = 0x01;
TH0 = 0xfc;
TL0 = 0x18;
EA = 1;
ET0 = 1;
TR0 = 1;
while(1)
{
key_scan();
key_to(key_v);
}
}
void key_scan()
{
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;
break;
}
}
}
}
void key_to(uchar key_v)
{
if(key_v == 0xef)
{
count++;
if(count == 100)
count = 0;
}
else
{
if(key_v == 0xdf)
{
if(count == 0)
count = 99;
else
count--;
}
}
}
void time0(void) interrupt 1
{
dis[1] = count / 10;
dis[0] = count % 10;
P0 = dis_code[dis[dis_r]];
P2 = dis_b;
dis_r ^= 0x01;
dis_b ^= 0xc0;
TH0 = 0xfc;
TL0 = 0x18;
}
void delay_ms(uchar ms)
{
uchar i;
while(ms--)
for(i = 0; i < 124; i++);
}
阅读(6860) | 评论(6)
版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!
评论