关于单片机按键移位的问题,程序如下
#include<reg51.h>#include<intrins.h>unsignedcharscan_key();voidproc_key(unsignedchark...
#include <reg51.h>
#include <intrins.h>
unsigned char scan_key();
void proc_key(unsigned char key_v);
void delayms(unsigned char ms);
sbit K1 = P0^0; //对应按钮K1
sbit K2 = P0^1; //对应按钮K2
main()
{
unsigned char key_s,key_v;
key_v = 0x03;
P2 = 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)
{
P2 = _cror_(P2,1);
}
else if((key_v & 0x02) == 0)
{
P2 = _crol_(P2, 1);
}
}
void delayms(unsigned char ms) // 延时子程序
{
unsigned char i;
while(ms--)
{
for(i = 0; i < 120; i++);
}
}
在while循环中,key_v被赋值等于key_s的值,要么是0x01,要么是0x02,在下一次的按键时,如果和上一次的按键相同,怎么能达到if语句的条件? 展开
#include <intrins.h>
unsigned char scan_key();
void proc_key(unsigned char key_v);
void delayms(unsigned char ms);
sbit K1 = P0^0; //对应按钮K1
sbit K2 = P0^1; //对应按钮K2
main()
{
unsigned char key_s,key_v;
key_v = 0x03;
P2 = 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)
{
P2 = _cror_(P2,1);
}
else if((key_v & 0x02) == 0)
{
P2 = _crol_(P2, 1);
}
}
void delayms(unsigned char ms) // 延时子程序
{
unsigned char i;
while(ms--)
{
for(i = 0; i < 120; i++);
}
}
在while循环中,key_v被赋值等于key_s的值,要么是0x01,要么是0x02,在下一次的按键时,如果和上一次的按键相同,怎么能达到if语句的条件? 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询