标题: 每次循环增加1 怎么我这个按键次数每次都加2啊?难道说我很2 [打印本页]

作者: xzf586    时间: 2017-9-10 11:00
标题: 每次循环增加1 怎么我这个按键次数每次都加2啊?难道说我很2
#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,没有找出问题!

作者: wulin    时间: 2017-9-10 12:29
需要设置按键抬起标志,否则会重复计数。楼主这个按键程序如果用于组合键功能才有意义,否则是简单问题复杂化。
作者: mrchen77    时间: 2017-9-10 15:44
你没有按键抖动处理,按一次单片机会检测到有多次的脉冲
作者: 135555    时间: 2017-9-10 16:03
加延时在判断
作者: 西瓜切忍者    时间: 2017-9-10 16:18
你没有,消抖的代码???
作者: xzf586    时间: 2017-9-10 19:21
wulin 发表于 2017-9-10 12:29
需要设置按键抬起标志,否则会重复计数。楼主这个按键程序如果用于组合键功能才有意义,否则是简单问题复杂 ...

是在另外一个程序里面包含这个按键扫描程序,当然在原来程序加上while(Button_Scan);可以解决问题,只是没有看出来这个问题为什么会加2?
作者: xzf586    时间: 2017-9-10 19:22
西瓜切忍者 发表于 2017-9-10 16:18
你没有,消抖的代码???

有消抖代码!
作者: xzf586    时间: 2017-9-10 19:23
mrchen77 发表于 2017-9-10 15:44
你没有按键抖动处理,按一次单片机会检测到有多次的脉冲

有消抖,  if(Button_Scan())
     {
          delay1ms(10);
          if(Button_Scan())
。。。。。。。。。。
作者: wulin    时间: 2017-9-10 20:40
xzf586 发表于 2017-9-10 19:21
是在另外一个程序里面包含这个按键扫描程序,当然在原来程序加上while(Button_Scan);可以解决问题,只是 ...

你可以仔细观察一下,你这程序按键按下计一次数,抬起再计一次数。核心问题在Key_Value=Key_Push;
给你改了一下,你试试:
#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;
bit x=0;//按键标志位

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);
     return Key_Push;
}
/*
void Button_Proc()              //取按键值
{
     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;
     }         
}
*/

void Button()    //取键值,并且记录按键次数
{
        if(Button_Scan()!=Key_Value)
        {
                delay1ms(10);
                if(Button_Scan()!=Key_Value)
                {
                        if(x==0)
                        {
                                x=1;//防止重复计数
                                Key_Times++;
                                if(Key_Times==8)
                                        Key_Times=0;
//                                Key_Value=Key_Push;
//                                Button_Proc();            
                                switch(Button_Scan())//取键值,3个键共组合有8个状态,去除0x07可以产生7个键值
                                {
                                        case 0x00: Key=0; break;
                                        case 0x01: Key=1; break;
                                        case 0x02: Key=2; break;
                                        case 0x03: Key=3; break;
                                        case 0x04: Key=4; break;
                                        case 0x05: Key=5; break;
                                        case 0x06: Key=6; break;
//                                        case 0x07: Key=7; break;
                                }
                        }               
                }
        }
        else x=0;
}

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

作者: 阔爱的钊钊    时间: 2017-9-10 23:51
延时防抖动,看一哈例子。
作者: xzf586    时间: 2017-9-11 08:10
wulin 发表于 2017-9-10 20:40
你可以仔细观察一下,你这程序按键按下计一次数,抬起再计一次数。核心问题在Key_Value=Key_Push;
给你 ...

谢谢老兄提醒,确实是按下加1,松开又加1,
经过仔细分析 Key_Value=Key_Push;发现问题:
假如第一次按下K2,Key_Push=0x02,K2键没有松开,
下一轮Button_Scan(),Key_Value=Key_Push,其返回值为0,不会再次进入if条件,直至按键松开,
但是按键松开之后,问题来了,此时Key_Push=0x00;Key_Push!=Key_Value,满足if(Button_Scan())
的条件,因此又执行一次!!
作者: 渴望壮大    时间: 2017-9-14 17:28
xzf586 发表于 2017-9-11 08:10
谢谢老兄提醒,确实是按下加1,松开又加1,
经过仔细分析 Key_Value=Key_Push;发现问题:
假如第一次按 ...

Key_Value你这个值初始值直接给0x00就可以了,后面不要给复制就可以
作者: lth977    时间: 2017-9-14 18:13
加一个延时就行,你写个延时函数,在每个按键判断的后面都延迟个5毫秒就不会加2了
作者: bb24242424    时间: 2017-9-14 20:34
按键检测,消抖就可以了




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