一个简单的按键扫描两种写法,有一个总是出错

[复制链接]
 楼主| 冷冻链 发表于 2019-11-14 11:46 | 显示全部楼层 |阅读模式
两种写法,应该是一样的,但有一种不起作用,求大神解答,谢谢!
第一种:
unsigned char scanKey(void)
{
    unsigned char ret=0x00;
    if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)==1)
    {
        while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0));
        ret= 0x01;    //这个返回值意思是亮第一个灯
    }
    if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_13)==1)
    {
        while(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_13));
        ret= 0x10;    //这个返回值意思是亮第二个灯
    }
    return ret;
}

第二种:  
unsigned char scanKey(void)
{
    unsigned char ret=0x00;
    if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)==1)
    {
        ret= 0x01;    //这个返回值意思是亮第一个灯
    }
    if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_13)==1)
    {
        ret= 0x10;    //这个返回值意思是亮第二个灯
    }
//下面两句是等待按键释放的意思
    while(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_13));
    while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0));
    return ret;
}

main函数里面死循环:检测此函数返回值是0x01还是0x10,点亮不同的灯,并延时0.5s,进入下一次循环。

事实情况是第一种写法完全没问题;第二种写法,有时候按下去按键没反应,该亮的灯不亮。

求大神解答,这个问题一直想不通,很困惑。谢谢!!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

397

主题

397

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部