找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 5118|回復(fù): 13
打印 上一主題 下一主題
收起左側(cè)

每次循環(huán)增加1 怎么我這個按鍵次數(shù)每次都加2。侩y道說我很2

  [復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:135253 發(fā)表于 2017-9-10 11:00 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
#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,沒有找出問題!
分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報

沙發(fā)
ID:213173 發(fā)表于 2017-9-10 12:29 | 只看該作者
需要設(shè)置按鍵抬起標(biāo)志,否則會重復(fù)計數(shù)。樓主這個按鍵程序如果用于組合鍵功能才有意義,否則是簡單問題復(fù)雜化。
回復(fù)

使用道具 舉報

板凳
ID:232152 發(fā)表于 2017-9-10 15:44 | 只看該作者
你沒有按鍵抖動處理,按一次單片機會檢測到有多次的脈沖

評分

參與人數(shù) 1黑幣 +8 收起 理由
張小帥1126 + 8 很給力!

查看全部評分

回復(fù)

使用道具 舉報

地板
ID:232239 發(fā)表于 2017-9-10 16:03 | 只看該作者
加延時在判斷
回復(fù)

使用道具 舉報

5#
ID:232222 發(fā)表于 2017-9-10 16:18 來自觸屏版 | 只看該作者
你沒有,消抖的代碼???
回復(fù)

使用道具 舉報

6#
ID:135253 發(fā)表于 2017-9-10 19:21 | 只看該作者
wulin 發(fā)表于 2017-9-10 12:29
需要設(shè)置按鍵抬起標(biāo)志,否則會重復(fù)計數(shù)。樓主這個按鍵程序如果用于組合鍵功能才有意義,否則是簡單問題復(fù)雜 ...

是在另外一個程序里面包含這個按鍵掃描程序,當(dāng)然在原來程序加上while(Button_Scan);可以解決問題,只是沒有看出來這個問題為什么會加2?
回復(fù)

使用道具 舉報

7#
ID:135253 發(fā)表于 2017-9-10 19:22 | 只看該作者
西瓜切忍者 發(fā)表于 2017-9-10 16:18
你沒有,消抖的代碼???

有消抖代碼!
回復(fù)

使用道具 舉報

8#
ID:135253 發(fā)表于 2017-9-10 19:23 | 只看該作者
mrchen77 發(fā)表于 2017-9-10 15:44
你沒有按鍵抖動處理,按一次單片機會檢測到有多次的脈沖

有消抖,  if(Button_Scan())
     {
          delay1ms(10);
          if(Button_Scan())
。。。。。。。。。。
回復(fù)

使用道具 舉報

9#
ID:213173 發(fā)表于 2017-9-10 20:40 | 只看該作者
xzf586 發(fā)表于 2017-9-10 19:21
是在另外一個程序里面包含這個按鍵掃描程序,當(dāng)然在原來程序加上while(Button_Scan);可以解決問題,只是 ...

你可以仔細(xì)觀察一下,你這程序按鍵按下計一次數(shù),抬起再計一次數(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ù)計數(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;
     }
}
回復(fù)

使用道具 舉報

10#
ID:222006 發(fā)表于 2017-9-10 23:51 | 只看該作者
延時防抖動,看一哈例子。
回復(fù)

使用道具 舉報

11#
ID:135253 發(fā)表于 2017-9-11 08:10 | 只看該作者
wulin 發(fā)表于 2017-9-10 20:40
你可以仔細(xì)觀察一下,你這程序按鍵按下計一次數(shù),抬起再計一次數(shù)。核心問題在Key_Value=Key_Push;
給你 ...

謝謝老兄提醒,確實是按下加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í)行一次!!
回復(fù)

使用道具 舉報

12#
ID:188935 發(fā)表于 2017-9-14 17:28 | 只看該作者
xzf586 發(fā)表于 2017-9-11 08:10
謝謝老兄提醒,確實是按下加1,松開又加1,
經(jīng)過仔細(xì)分析 Key_Value=Key_Push;發(fā)現(xiàn)問題:
假如第一次按 ...

Key_Value你這個值初始值直接給0x00就可以了,后面不要給復(fù)制就可以
回復(fù)

使用道具 舉報

13#
ID:232530 發(fā)表于 2017-9-14 18:13 | 只看該作者
加一個延時就行,你寫個延時函數(shù),在每個按鍵判斷的后面都延遲個5毫秒就不會加2了
回復(fù)

使用道具 舉報

14#
ID:142450 發(fā)表于 2017-9-14 20:34 | 只看該作者
按鍵檢測,消抖就可以了
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復(fù) 返回頂部 返回列表