剛學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);
- }
復制代碼
|