********************************************************************************
* 描述: *
* 按键扫描程序 *
* 上电时, 点亮P00口LED *
* 按下K1时, LED向右移一位 *
* 按下K2时, LED向左移一位 *
*******************************************************************************/
#include <reg51.h>
#include <intrins.h>
sbit K1 = P3^2;
sbit K2 = P3^3;
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++);
}
}
main()
{
unsigned char key_s,key_v;
key_v = 0x03;
P0 = 0xfe;
while(1)
{
key_s = scan_key();
if(key_s != key_v)
{
delayms(20);
key_s = scan_key();
if(key_s != key_v)
{
key_v = key_s;
proc_key(key_v);
delayms(150);
}
}
}
}