標(biāo)題: 每次循環(huán)增加1 怎么我這個按鍵次數(shù)每次都加2啊?難道說我很2 [打印本頁]

作者: xzf586    時間: 2017-9-10 11:00
標(biāo)題: 每次循環(huán)增加1 怎么我這個按鍵次數(shù)每次都加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()    //取鍵值,并且記錄按鍵次數(shù)
{
     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;
     }
}


按鍵掃描程序調(diào)試過程當(dāng)中,發(fā)現(xiàn)Key_Times怎么每次加2,沒有找出問題!

作者: wulin    時間: 2017-9-10 12:29
需要設(shè)置按鍵抬起標(biāo)志,否則會重復(fù)計(jì)數(shù)。樓主這個按鍵程序如果用于組合鍵功能才有意義,否則是簡單問題復(fù)雜化。
作者: mrchen77    時間: 2017-9-10 15:44
你沒有按鍵抖動處理,按一次單片機(jī)會檢測到有多次的脈沖
作者: 135555    時間: 2017-9-10 16:03
加延時在判斷
作者: 西瓜切忍者    時間: 2017-9-10 16:18
你沒有,消抖的代碼???
作者: xzf586    時間: 2017-9-10 19:21
wulin 發(fā)表于 2017-9-10 12:29
需要設(shè)置按鍵抬起標(biāo)志,否則會重復(fù)計(jì)數(shù)。樓主這個按鍵程序如果用于組合鍵功能才有意義,否則是簡單問題復(fù)雜 ...

是在另外一個程序里面包含這個按鍵掃描程序,當(dāng)然在原來程序加上while(Button_Scan);可以解決問題,只是沒有看出來這個問題為什么會加2?
作者: xzf586    時間: 2017-9-10 19:22
西瓜切忍者 發(fā)表于 2017-9-10 16:18
你沒有,消抖的代碼???

有消抖代碼!
作者: xzf586    時間: 2017-9-10 19:23
mrchen77 發(fā)表于 2017-9-10 15:44
你沒有按鍵抖動處理,按一次單片機(jī)會檢測到有多次的脈沖

有消抖,  if(Button_Scan())
     {
          delay1ms(10);
          if(Button_Scan())
。。。。。。。。。。
作者: wulin    時間: 2017-9-10 20:40
xzf586 發(fā)表于 2017-9-10 19:21
是在另外一個程序里面包含這個按鍵掃描程序,當(dāng)然在原來程序加上while(Button_Scan);可以解決問題,只是 ...

你可以仔細(xì)觀察一下,你這程序按鍵按下計(jì)一次數(shù),抬起再計(jì)一次數(shù)。核心問題在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;//按鍵標(biāo)志位

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()    //取鍵值,并且記錄按鍵次數(shù)
{
        if(Button_Scan()!=Key_Value)
        {
                delay1ms(10);
                if(Button_Scan()!=Key_Value)
                {
                        if(x==0)
                        {
                                x=1;//防止重復(fù)計(jì)數(shù)
                                Key_Times++;
                                if(Key_Times==8)
                                        Key_Times=0;
//                                Key_Value=Key_Push;
//                                Button_Proc();            
                                switch(Button_Scan())//取鍵值,3個鍵共組合有8個狀態(tài),去除0x07可以產(chǎn)生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 發(fā)表于 2017-9-10 20:40
你可以仔細(xì)觀察一下,你這程序按鍵按下計(jì)一次數(shù),抬起再計(jì)一次數(shù)。核心問題在Key_Value=Key_Push;
給你 ...

謝謝老兄提醒,確實(shí)是按下加1,松開又加1,
經(jīng)過仔細(xì)分析 Key_Value=Key_Push;發(fā)現(xiàn)問題:
假如第一次按下K2,Key_Push=0x02,K2鍵沒有松開,
下一輪Button_Scan(),Key_Value=Key_Push,其返回值為0,不會再次進(jìn)入if條件,直至按鍵松開,
但是按鍵松開之后,問題來了,此時Key_Push=0x00;Key_Push!=Key_Value,滿足if(Button_Scan())
的條件,因此又執(zhí)行一次!
作者: 渴望壯大    時間: 2017-9-14 17:28
xzf586 發(fā)表于 2017-9-11 08:10
謝謝老兄提醒,確實(shí)是按下加1,松開又加1,
經(jīng)過仔細(xì)分析 Key_Value=Key_Push;發(fā)現(xiàn)問題:
假如第一次按 ...

Key_Value你這個值初始值直接給0x00就可以了,后面不要給復(fù)制就可以
作者: lth977    時間: 2017-9-14 18:13
加一個延時就行,你寫個延時函數(shù),在每個按鍵判斷的后面都延遲個5毫秒就不會加2了
作者: bb24242424    時間: 2017-9-14 20:34
按鍵檢測,消抖就可以了




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1