标题: 这是一段键盘扫描程序,我知道作用,但不知道每条为什么这样写,烦请哪位给逐句解.... [打印本页]

作者: hanxingyijian    时间: 2023-2-28 20:04
标题: 这是一段键盘扫描程序,我知道作用,但不知道每条为什么这样写,烦请哪位给逐句解....
unsigned char GetKey(void)
{
        unsigned char KeyTemp,CheckValue,Key = 0x00;
        CheckValue = P3&0x4c;
        if(CheckValue==0x4c)
                return 0x00;
        Delay1ms(10);
        KeyTemp = P3&0x4c;
        if(KeyTemp==CheckValue)
                return 0x00;

        if(!(CheckValue&0x04))
                Key|=0x01;
        if(!(CheckValue&0x08))
                Key|=0x02;
        if(!(CheckValue&0x40))
                Key|=0x04;
        return Key;
}


作者: wulin    时间: 2023-3-1 09:22
unsigned char GetKey(void)
{//3个按键连接在P3.6、P3.3、P3.2
        unsigned char KeyTemp,CheckValue,Key = 0x00;
        CheckValue = P3&0x4c;  //按键的常态值赋值变量CheckValue 01001100
        if(CheckValue==0x4c)   //两者相等,说明没有键按下
                return 0x00;        //返回键值0x00,不执行以下语句
                          //如果if(CheckValue==0x4c)为假,说明有键按下或可能在抖动中,也可能是干扰信号
        Delay1ms(10);          //延时10ms,等待按键稳定
        KeyTemp = P3&0x4c;     //稳定后再将按键的变态值赋值变量KeyTemp 01001100
        if(KeyTemp==CheckValue)//判断两个变量是否相等,如果相等,则不是按键按下
                return 0x00;        //返回键值0x00,不执行以下语句
                               //两个变量不相等,
        if(!(CheckValue&0x04)) //顺序判断是哪个键按下或哪几个键组合按下
                Key|=0x01;
        if(!(CheckValue&0x08))
                Key|=0x02;
        if(!(CheckValue&0x40))
                Key|=0x04;
        return Key;            //返回键值Key
}
作者: hanxingyijian    时间: 2023-3-1 16:57
wulin 发表于 2023-3-1 09:22
unsigned char GetKey(void)
{//3个按键连接在P3.6、P3.3、P3.2
        unsigned char KeyTemp,CheckValue,Key  ...

非常感谢!有老师指导才能使我不断进步!




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1