刚学STM32的小白,写了stm32一个按键按几下就亮几个呼吸灯的程序,呼吸灯运行正常,但就是在按第二下按键时需要按下按键较长时间才会亮第二个呼吸灯,以后几个也都是这样,是在哪里有问题呢?望大佬指教
单片机源程序如下:
- #include "sys.h"
- #include "delay.h"
- #include "usart.h"
- #include "led.h"
- #include "stm32f10x.h"
- void delay(uint32_t counter)
- {
- while(counter--);
- }
- int main(void)
- {
- int key_number; //按键次数变量
- int flag1=0; //按键标志位
- int a,b;
- int c,d,e,f;
- LED_Init(); //初始化与LED连接的硬件接口
- delay_init(); //延时函数初始化
- a=4000;
- b=3800;
- LED_D2=1; //四个LED灯刚开始处于熄灭状态
- LED_D3=1;
- LED_D4=1;
- LED_D5=1;
- while(1)
- {
- if(Key4==0) flag1=1;
- if(Key4==1&&flag1==1) //判断按键是否按下
- {
- flag1=0;
- key_number++; //按键按下则计数加1,按四次,则有四个呼吸灯亮
- }
-
- switch(key_number) //通过按键按下次数来决定有多少个呼吸灯亮
- {
- case 1:huxi1(c);break;
- case 2:huxi2(d);break;
- case 3:huxi3(e);break;
- case 4:huxi4(f);break;
- }
- }
- }
- int huxi1(int c) //第一个呼吸灯运行函数
- {
- int i,a,b;
- a=4000;
- b=3800;
- for(i=0;i<a;i++)
- {
- GPIO_ResetBits(GPIOB, GPIO_Pin_12);
- delay(a - i);
- GPIO_SetBits(GPIOB, GPIO_Pin_12);
- delay(i);
- }
- for(i=0;i<b;i++)
- {
- GPIO_ResetBits(GPIOB, GPIO_Pin_12);
- delay(i);
- GPIO_SetBits(GPIOB, GPIO_Pin_12);
- delay(b-i);
- }
- delay(100);
- }
- int huxi2(int d) //第一个和第2个呼吸灯运行函数
- {
- int i,a,b;
- a=4000;
- b=3800;
- for(i=0;i<a;i++)
- {
- GPIO_ResetBits(GPIOB, GPIO_Pin_12);
- GPIO_ResetBits(GPIOB, GPIO_Pin_13);
- delay(a - i);
- GPIO_SetBits(GPIOB, GPIO_Pin_12);
- GPIO_SetBits(GPIOB, GPIO_Pin_13);
- delay(i);
- }
- for(i=0;i<b;i++)
- {
- GPIO_ResetBits(GPIOB, GPIO_Pin_12);
- GPIO_ResetBits(GPIOB, GPIO_Pin_13);
- delay(i);
- GPIO_SetBits(GPIOB, GPIO_Pin_12);
- GPIO_SetBits(GPIOB, GPIO_Pin_13);
- delay(b-i);
- }
- delay(100);
- }
- int huxi3(int e) //第三个呼吸灯运行函数
- {
- int i,a,b;
- a=4000;
- b=3800;
- for(i=0;i<a;i++)
- {
- GPIO_ResetBits(GPIOB, GPIO_Pin_12);
- GPIO_ResetBits(GPIOB, GPIO_Pin_13);
- GPIO_ResetBits(GPIOB, GPIO_Pin_14);
- delay(a - i);
- GPIO_SetBits(GPIOB, GPIO_Pin_12);
- GPIO_SetBits(GPIOB, GPIO_Pin_13);
- GPIO_SetBits(GPIOB, GPIO_Pin_14);
- delay(i);
- }
- for(i=0;i<b;i++)
- {
- GPIO_ResetBits(GPIOB, GPIO_Pin_12);
- GPIO_ResetBits(GPIOB, GPIO_Pin_13);
- GPIO_ResetBits(GPIOB, GPIO_Pin_14);
- delay(i);
- GPIO_SetBits(GPIOB, GPIO_Pin_12);
- GPIO_SetBits(GPIOB, GPIO_Pin_13);
- GPIO_SetBits(GPIOB, GPIO_Pin_14);
- delay(b-i);
- }
- delay(100);
- }
- int huxi4(int f) //第四个呼吸灯运行函数
- {
- int i,a,b;
- a=4000;
- b=3800;
- for(i=0;i<a;i++)
- {
- GPIO_ResetBits(GPIOB, GPIO_Pin_12);
- GPIO_ResetBits(GPIOB, GPIO_Pin_13);
- GPIO_ResetBits(GPIOB, GPIO_Pin_14);
- GPIO_ResetBits(GPIOB, GPIO_Pin_15);
- delay(a - i);
- GPIO_SetBits(GPIOB, GPIO_Pin_12);
- GPIO_SetBits(GPIOB, GPIO_Pin_13);
- GPIO_SetBits(GPIOB, GPIO_Pin_14);
- GPIO_SetBits(GPIOB, GPIO_Pin_15);
- delay(i);
- }
- for(i=0;i<b;i++)
- {
- GPIO_ResetBits(GPIOB, GPIO_Pin_12);
- GPIO_ResetBits(GPIOB, GPIO_Pin_13);
- GPIO_ResetBits(GPIOB, GPIO_Pin_14);
- GPIO_ResetBits(GPIOB, GPIO_Pin_15);
- delay(i);
- GPIO_SetBits(GPIOB, GPIO_Pin_12);
- GPIO_SetBits(GPIOB, GPIO_Pin_13);
- GPIO_SetBits(GPIOB, GPIO_Pin_14);
- GPIO_SetBits(GPIOB, GPIO_Pin_15);
- delay(b-i);
- }
- delay(100);
- }
复制代码
|