|
剛學(xué)STM32的小白,寫了stm32一個(gè)按鍵按幾下就亮幾個(gè)呼吸燈的程序,呼吸燈運(yùn)行正常,但就是在按第二下按鍵時(shí)需要按下按鍵較長時(shí)間才會(huì)亮第二個(gè)呼吸燈,以后幾個(gè)也都是這樣,是在哪里有問題呢?望大佬指教
單片機(jī)源程序如下:
- #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; //按鍵次數(shù)變量
- int flag1=0; //按鍵標(biāo)志位
- int a,b;
- int c,d,e,f;
- LED_Init(); //初始化與LED連接的硬件接口
- delay_init(); //延時(shí)函數(shù)初始化
- a=4000;
- b=3800;
- LED_D2=1; //四個(gè)LED燈剛開始處于熄滅狀態(tài)
- 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++; //按鍵按下則計(jì)數(shù)加1,按四次,則有四個(gè)呼吸燈亮
- }
-
- switch(key_number) //通過按鍵按下次數(shù)來決定有多少個(gè)呼吸燈亮
- {
- case 1:huxi1(c);break;
- case 2:huxi2(d);break;
- case 3:huxi3(e);break;
- case 4:huxi4(f);break;
- }
- }
- }
- int huxi1(int c) //第一個(gè)呼吸燈運(yùn)行函數(shù)
- {
- 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) //第一個(gè)和第2個(gè)呼吸燈運(yùn)行函數(shù)
- {
- 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) //第三個(gè)呼吸燈運(yùn)行函數(shù)
- {
- 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) //第四個(gè)呼吸燈運(yùn)行函數(shù)
- {
- 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);
- }
復(fù)制代碼
|
|