找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 5974|回复: 4
收起左侧

STM32单片机按键按下较长时间才有反应

[复制链接]
ID:483399 发表于 2019-7-20 19:21 | 显示全部楼层 |阅读模式
刚学STM32的小白,写了stm32一个按键按几下就亮几个呼吸灯的程序,呼吸灯运行正常,但就是在按第二下按键时需要按下按键较长时间才会亮第二个呼吸灯,以后几个也都是这样,是在哪里有问题呢?望大佬指教
单片机源程序如下:
  1. #include "sys.h"
  2. #include "delay.h"
  3. #include "usart.h"
  4. #include "led.h"
  5. #include "stm32f10x.h"

  6. void delay(uint32_t counter)
  7. {
  8.         while(counter--);
  9.         }
  10. int main(void)
  11. {        
  12.           int key_number;    //按键次数变量
  13.     int flag1=0;       //按键标志位
  14.           int a,b;
  15.                 int c,d,e,f;
  16.                 LED_Init();                          //初始化与LED连接的硬件接口
  17.           delay_init();            //延时函数初始化
  18.                 a=4000;
  19.           b=3800;
  20.           LED_D2=1;          //四个LED灯刚开始处于熄灭状态
  21.           LED_D3=1;
  22.           LED_D4=1;
  23.           LED_D5=1;
  24.                          while(1)
  25.                          {
  26.                         if(Key4==0) flag1=1;
  27.                         if(Key4==1&&flag1==1)  //判断按键是否按下
  28.                         {
  29.                                 flag1=0;
  30.                                 key_number++;        //按键按下则计数加1,按四次,则有四个呼吸灯亮
  31.                         }
  32.          
  33.                         switch(key_number)     //通过按键按下次数来决定有多少个呼吸灯亮
  34.                         {
  35.                                 case 1:huxi1(c);break;
  36.                                 case 2:huxi2(d);break;
  37.                                 case 3:huxi3(e);break;
  38.                                 case 4:huxi4(f);break;
  39.                   }
  40.                 }
  41. }
  42. int huxi1(int c)       //第一个呼吸灯运行函数
  43. {
  44.           int i,a,b;
  45.           a=4000;
  46.           b=3800;
  47.       for(i=0;i<a;i++)
  48.       {
  49.       GPIO_ResetBits(GPIOB, GPIO_Pin_12);                                
  50.       delay(a - i);
  51.       GPIO_SetBits(GPIOB, GPIO_Pin_12);
  52.       delay(i);                                 
  53.                   }
  54.                         for(i=0;i<b;i++)
  55.       {
  56.       GPIO_ResetBits(GPIOB, GPIO_Pin_12);                                
  57.       delay(i);
  58.       GPIO_SetBits(GPIOB, GPIO_Pin_12);
  59.       delay(b-i);                                 
  60.                   }
  61.                         delay(100);
  62.                 }
  63. int huxi2(int d)  //第一个和第2个呼吸灯运行函数
  64. {
  65.           int i,a,b;
  66.           a=4000;
  67.           b=3800;
  68.       for(i=0;i<a;i++)
  69.       {
  70.       GPIO_ResetBits(GPIOB, GPIO_Pin_12);
  71.       GPIO_ResetBits(GPIOB, GPIO_Pin_13);                                 
  72.       delay(a - i);
  73.       GPIO_SetBits(GPIOB, GPIO_Pin_12);
  74.                         GPIO_SetBits(GPIOB, GPIO_Pin_13);
  75.       delay(i);                                 
  76.                   }
  77.                         for(i=0;i<b;i++)
  78.       {
  79.       GPIO_ResetBits(GPIOB, GPIO_Pin_12);
  80.       GPIO_ResetBits(GPIOB, GPIO_Pin_13);                        
  81.       delay(i);
  82.       GPIO_SetBits(GPIOB, GPIO_Pin_12);
  83.                         GPIO_SetBits(GPIOB, GPIO_Pin_13);
  84.       delay(b-i);                                 
  85.                   }
  86.                         delay(100);
  87.                 }
  88. int huxi3(int e)        //第三个呼吸灯运行函数
  89. {
  90.           int i,a,b;
  91.           a=4000;
  92.           b=3800;
  93.       for(i=0;i<a;i++)
  94.       {
  95.       GPIO_ResetBits(GPIOB, GPIO_Pin_12);
  96.       GPIO_ResetBits(GPIOB, GPIO_Pin_13);
  97.       GPIO_ResetBits(GPIOB, GPIO_Pin_14);                                 
  98.       delay(a - i);
  99.       GPIO_SetBits(GPIOB, GPIO_Pin_12);
  100.                         GPIO_SetBits(GPIOB, GPIO_Pin_13);
  101.                         GPIO_SetBits(GPIOB, GPIO_Pin_14);
  102.       delay(i);                                 
  103.                   }
  104.                         for(i=0;i<b;i++)
  105.       {
  106.       GPIO_ResetBits(GPIOB, GPIO_Pin_12);
  107.       GPIO_ResetBits(GPIOB, GPIO_Pin_13);
  108.       GPIO_ResetBits(GPIOB, GPIO_Pin_14);                        
  109.       delay(i);
  110.       GPIO_SetBits(GPIOB, GPIO_Pin_12);
  111.                         GPIO_SetBits(GPIOB, GPIO_Pin_13);
  112.                         GPIO_SetBits(GPIOB, GPIO_Pin_14);
  113.       delay(b-i);                                 
  114.                   }
  115.                         delay(100);
  116.                 }
  117. int huxi4(int f)        //第四个呼吸灯运行函数
  118. {
  119.           int i,a,b;
  120.           a=4000;
  121.           b=3800;
  122.       for(i=0;i<a;i++)
  123.       {
  124.       GPIO_ResetBits(GPIOB, GPIO_Pin_12);
  125.       GPIO_ResetBits(GPIOB, GPIO_Pin_13);
  126.       GPIO_ResetBits(GPIOB, GPIO_Pin_14);
  127.       GPIO_ResetBits(GPIOB, GPIO_Pin_15);                                 
  128.       delay(a - i);
  129.       GPIO_SetBits(GPIOB, GPIO_Pin_12);
  130.                         GPIO_SetBits(GPIOB, GPIO_Pin_13);
  131.                         GPIO_SetBits(GPIOB, GPIO_Pin_14);
  132.                         GPIO_SetBits(GPIOB, GPIO_Pin_15);
  133.       delay(i);                                 
  134.                   }
  135.                         for(i=0;i<b;i++)
  136.       {
  137.       GPIO_ResetBits(GPIOB, GPIO_Pin_12);
  138.       GPIO_ResetBits(GPIOB, GPIO_Pin_13);
  139.       GPIO_ResetBits(GPIOB, GPIO_Pin_14);
  140.       GPIO_ResetBits(GPIOB, GPIO_Pin_15);                                 
  141.       delay(i);
  142.       GPIO_SetBits(GPIOB, GPIO_Pin_12);
  143.                         GPIO_SetBits(GPIOB, GPIO_Pin_13);
  144.                         GPIO_SetBits(GPIOB, GPIO_Pin_14);
  145.                         GPIO_SetBits(GPIOB, GPIO_Pin_15);
  146.       delay(b-i);                                 
  147.                   }
  148.                         delay(100);
  149.                 }
复制代码


回复

使用道具 举报

ID:517466 发表于 2019-7-20 23:31 | 显示全部楼层
第一次的时候,没有执行呼吸灯的处理。按键的循环扫描很快。一旦执行了呼吸灯,呼吸灯处理中的for循环会占用MPU的执行时间,不执行完呼吸灯的处理,程序不会退出到main程序中检查按键是否按下的。如果想快速响应按键,最好改成中断方式响应按键。
回复

使用道具 举报

ID:377268 发表于 2019-7-20 23:50 | 显示全部楼层
你好哦,你的呼吸灯子函数是个大的for循环,这个写法的话,程序只在每次跑完for后去扫描按键1次,就是说,每呼吸1次才能扫描到1次按键的。你第1次按按键时没循环在跑,程序在不断扫按键,开启呼吸灯之后不管哪个程序都存在for循环,这就导致按键在大部分时间都是处在没被扫描的状态,不想动大体的结构的话,你在每个for里面放个按键检测,按键按下就break.但是别清flag.OK,希望你能理解
回复

使用道具 举报

ID:587834 发表于 2019-7-24 17:05 | 显示全部楼层
只看了问题,没看你程序。你检查一下按键延时,防抖延时看看
回复

使用道具 举报

ID:588192 发表于 2019-7-24 19:57 | 显示全部楼层
建议加一个防抖动程序和延时配套使用
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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