找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 786|回复: 2
收起左侧

这是一段键盘扫描程序,我知道作用,但不知道每条为什么这样写,烦请哪位给逐句解....

[复制链接]
ID:879080 发表于 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;
}

回复

使用道具 举报

ID:213173 发表于 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
}
回复

使用道具 举报

ID:879080 发表于 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  ...

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

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表