找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3824|回复: 13
打印 上一主题 下一主题
收起左侧

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

  [复制链接]
跳转到指定楼层
楼主
ID:135253 发表于 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()    //取键值,并且记录按键次数
{
     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,没有找出问题!
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:213173 发表于 2017-9-10 12:29 | 只看该作者
需要设置按键抬起标志,否则会重复计数。楼主这个按键程序如果用于组合键功能才有意义,否则是简单问题复杂化。
回复

使用道具 举报

板凳
ID:232152 发表于 2017-9-10 15:44 | 只看该作者
你没有按键抖动处理,按一次单片机会检测到有多次的脉冲

评分

参与人数 1黑币 +8 收起 理由
张小帅1126 + 8 很给力!

查看全部评分

回复

使用道具 举报

地板
ID:232239 发表于 2017-9-10 16:03 | 只看该作者
加延时在判断
回复

使用道具 举报

5#
ID:232222 发表于 2017-9-10 16:18 来自手机 | 只看该作者
你没有,消抖的代码???
回复

使用道具 举报

6#
ID:135253 发表于 2017-9-10 19:21 | 只看该作者
wulin 发表于 2017-9-10 12:29
需要设置按键抬起标志,否则会重复计数。楼主这个按键程序如果用于组合键功能才有意义,否则是简单问题复杂 ...

是在另外一个程序里面包含这个按键扫描程序,当然在原来程序加上while(Button_Scan);可以解决问题,只是没有看出来这个问题为什么会加2?
回复

使用道具 举报

7#
ID:135253 发表于 2017-9-10 19:22 | 只看该作者
西瓜切忍者 发表于 2017-9-10 16:18
你没有,消抖的代码???

有消抖代码!
回复

使用道具 举报

8#
ID:135253 发表于 2017-9-10 19:23 | 只看该作者
mrchen77 发表于 2017-9-10 15:44
你没有按键抖动处理,按一次单片机会检测到有多次的脉冲

有消抖,  if(Button_Scan())
     {
          delay1ms(10);
          if(Button_Scan())
。。。。。。。。。。
回复

使用道具 举报

9#
ID:213173 发表于 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;
     }
}
回复

使用道具 举报

10#
ID:222006 发表于 2017-9-10 23:51 | 只看该作者
延时防抖动,看一哈例子。
回复

使用道具 举报

11#
ID:135253 发表于 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())
的条件,因此又执行一次!!
回复

使用道具 举报

12#
ID:188935 发表于 2017-9-14 17:28 | 只看该作者
xzf586 发表于 2017-9-11 08:10
谢谢老兄提醒,确实是按下加1,松开又加1,
经过仔细分析 Key_Value=Key_Push;发现问题:
假如第一次按 ...

Key_Value你这个值初始值直接给0x00就可以了,后面不要给复制就可以
回复

使用道具 举报

13#
ID:232530 发表于 2017-9-14 18:13 | 只看该作者
加一个延时就行,你写个延时函数,在每个按键判断的后面都延迟个5毫秒就不会加2了
回复

使用道具 举报

14#
ID:142450 发表于 2017-9-14 20:34 | 只看该作者
按键检测,消抖就可以了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表