打印

每次循环增加1 怎么我这个按键次数每次都加2啊?

[复制链接]
2580|20
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
wangpe|  楼主 | 2021-8-14 14:01 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include <reg52.h>

#define uchar unsigned char
#define uint unsigned int

uchar Key_Push,Key_Value=7,Key_Times=0,Key;


sbit K1 = P1^0;
sbit K2 = P1^1;
sbit K3 = P1^2;

void delay1ms(uint x)
{
     uchar i;
     while(x--)
     for(i=0;i<125;i++)
     ;
}

uchar Button_Scan()
{
     Key_Push=0x00;
     Key_Push|=K3;
     Key_Push <<= 1;
     Key_Push|=K2;
     Key_Push <<= 1;
     Key_Push|= K1;
     return(Key_Push^Key_Value);
}

void Button_Proc()              //取按键值
{
     EA = 0;
     if((Key_Value&0x01)==0)  // K1按下,取键值1
     {
         Key=1;
     }
     else if((Key_Value&0x02)==0) // K2按下,取键值2
     {
          Key=2;
     }
    else if((Key_Value&0x04)==0) // K3按下,取键值3
     {
          Key=3;
     }         
     EA = 1;
}


void Button()    //取键值,并且记录按键次数
{
     if(Button_Scan())
     {
          delay1ms(10);
          if(Button_Scan())
          {
               Key_Times++;
               if(Key_Times==8)
               Key_Times=0;
               Key_Value=Key_Push;
               Button_Proc();
               
              
          }
     }
}

void main()
{
     while(1)
     {
          Button();
          P3=Key_Times;
     }
}


按键扫描程序调试过程当中,发现Key_Times怎么每次加2,没有找出问题!

使用特权

评论回复
沙发
zwll| | 2021-8-14 14:05 | 只看该作者
需要设置按键抬起标志,否则会重复计数。

使用特权

评论回复
板凳
supernan| | 2021-8-14 14:07 | 只看该作者
楼主这个按键程序如果用于组合键功能才有意义,否则是简单问题复杂化。

使用特权

评论回复
地板
wuhany| | 2021-8-14 14:09 | 只看该作者
没有进行按键抖动处理,按一次单片机会检测到有多次的脉冲

使用特权

评论回复
5
jlyuan| | 2021-8-14 14:12 | 只看该作者
或许可以加延时在判断。

使用特权

评论回复
6
zhenykun| | 2021-8-14 14:14 | 只看该作者
你没有,消抖的代码?

使用特权

评论回复
7
chuxh| | 2021-8-14 14:17 | 只看该作者
在原来程序加上while(Button_Scan);可以解决问题的。

使用特权

评论回复
8
pengf| | 2021-8-14 14:19 | 只看该作者

if(Button_Scan())
     {
          delay1ms(10);
          if(Button_Scan())

这个就是消抖吧。

使用特权

评论回复
9
zhanghqi| | 2021-8-14 14:21 | 只看该作者
延时防抖动,看一哈例子。

使用特权

评论回复
10
houcs| | 2021-8-14 14:24 | 只看该作者
这个是按键的控制识别问题的

使用特权

评论回复
11
chenjunt| | 2021-8-14 14:27 | 只看该作者
在识别按键的时候要加上防误触的措施的,如加时间延迟的

使用特权

评论回复
12
chuxh| | 2021-8-14 15:03 | 只看该作者
需要加入防抖动的措施的

使用特权

评论回复
13
dingy| | 2021-8-14 15:05 | 只看该作者
一般用延时就可以解决的,,或者记住当前的按键的按下状态的,跟前后几次的作比较的

使用特权

评论回复
14
liuzaiy| | 2021-8-14 15:08 | 只看该作者
这个需要对按键进行抗干扰的设计的

使用特权

评论回复
15
shimx| | 2021-8-14 15:10 | 只看该作者
让其在一定时间段内只处理响应到的其中一个主要的中断的

使用特权

评论回复
16
zhaoxqi| | 2021-8-14 15:11 | 只看该作者
按键的抗干扰处理的,需要注意下

使用特权

评论回复
17
wyjie| | 2021-8-14 15:13 | 只看该作者
是不是没有去抖啊

使用特权

评论回复
18
juventus9554| | 2021-8-14 15:15 | 只看该作者

上升沿和下降沿都触发了?

使用特权

评论回复
19
pengf| | 2021-8-14 15:17 | 只看该作者
去读的io口那边加一个电容试试

使用特权

评论回复
20
morrisk| | 2021-8-14 15:19 | 只看该作者
我也有这个问题

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

938

主题

9837

帖子

2

粉丝