【033】直控按键控制led循环方向2007-05-02 19:42:00
【评论】
【打印】
【字体:大 中 小】
本文链接:http://blog.pfan.cn/wentao/25479.html
k1 bit p1.4
k2 bit p1.5
org 0000h
ajmp start
org 0030h
start: mov p0,#0xfe
mov p1,#0xff
mov 30h,#0x03
mov 31h,#0x00
wait_key: lcall scan_key
mov 31h,a
xrl a,30h
jz wait_key
lcall delay10ms
lcall scan_key
mov 31h,a
xrl a,30h
jz wait_key
mov 30h,31h
lcall key
ajmp wait_key
scan_key:
clr a
mov c,k1
mov acc.0,c
mov c,k2
mov acc.1,c
ret
key:
mov a,30h
cjne a,#0x02,goon
ajmp key_k1
goon: cjne a,#0x01,back
ajmp key_k2
back: ret
key_k1:
mov a,p0
rl a
mov p0,a
ret
key_k2:
mov a,p0
rr a
mov p0,a
ret
delay10ms: mov r5,#20
temp: mov r6,#255
djnz r6,$
djnz r5,temp
ret
end
#include <reg51.h>
#include <intrins.h>
#define uchar unsigned char
sbit K1 = P1^4;
sbit K2 = P1^5;
uchar scan_key();
void key(uchar key_s);
void delay_ms(uchar ms);
void main()
{
uchar data key_s = 0x03;
uchar data key_t = 0x00;
P0 = 0xfe;
P1 = 0xff;
while(1)
{
key_t = scan_key();
if(key_t != key_s)
{
delay_ms(10);
key_t = scan_key();
if(key_t != key_s);
{
key_s = key_t;
key(key_s);
}
}
}
}
uchar scan_key()
{
uchar data key_m = 0x00;
key_m |= K2;
key_m <<= 1;
key_m |= K1;
return key_m;
}
void key(uchar key_s)
{
if(key_s == 0x02)
P0 = _crol_(P0,1);
else
{
if(key_s == 0x01)
P0 = _cror_(P0,1);
}
}
void delay_ms(uchar ms)
{
uchar i;
while(ms--)
for(i = 0; i < 124; i++);
}
阅读(4507) | 评论(1)
版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!
评论