按键扫描程序上电时,点亮P00口LED按下K1时,LED向右移一位按下K2时,LED向左移一位includereg51h>includeintrinsh>
按键扫描程序
上电时, 点亮P00口LED
按下K1时, LED向右移一位
按下K2时,LED向左移一位
#include
#include
unsigned char scan_key();
void proc_key(unsigned char key_v);
void delayms(unsigned char ms);
sbit K1 = P1^4;
sbit K2 = P1^5;
main()
{
unsigned char key_s,key_v;
key_v = 0x03;
P0 = 0xfe;
while(1)
{
key_s = scan_key();
if(key_s != key_v)
{
delayms(10);
key_s = scan_key();
if(key_s != key_v)
{
key_v = key_s;
proc_key(key_v);
}
}
}
}
unsigned char scan_key()
{
unsigned char key_s;
key_s = 0x00;
key_s |= K2;
key_s <= 1;
key_s |= K1;
return key_s;
}
void proc_key(unsigned char key_v)
{
if((key_v & 0x01) == 0)
{
P0 = _cror_(P0,1);
}
else if((key_v & 0x02) == 0)
{
P0 = _crol_(P0, 1);
}
}
void delayms(unsigned char ms)
// 延时子程序
{
unsigned char i;
while(ms–)
{
for(i = 0; i 120; i++);
}
}
声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/fangan/dianlu/255867.html