欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136
標題:
STM32單片機按鍵按下較長時間才有反應
[打印本頁]
作者:
查理九世
時間:
2019-7-20 19:21
標題:
STM32單片機按鍵按下較長時間才有反應
剛學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);
}
復制代碼
作者:
suncat0504
時間:
2019-7-20 23:31
第一次的時候,沒有執行呼吸燈的處理。按鍵的循環掃描很快。一旦執行了呼吸燈,呼吸燈處理中的for循環會占用MPU的執行時間,不執行完呼吸燈的處理,程序不會退出到main程序中檢查按鍵是否按下的。如果想快速響應按鍵,最好改成中斷方式響應按鍵。
作者:
1105730718
時間:
2019-7-20 23:50
你好哦,你的呼吸燈子函數是個大的for循環,這個寫法的話,程序只在每次跑完for后去掃描按鍵1次,就是說,每呼吸1次才能掃描到1次按鍵的。你第1次按按鍵時沒循環在跑,程序在不斷掃按鍵,開啟呼吸燈之后不管哪個程序都存在for循環,這就導致按鍵在大部分時間都是處在沒被掃描的狀態,不想動大體的結構的話,你在每個for里面放個按鍵檢測,按鍵按下就break.但是別清flag.OK,希望你能理解
作者:
zengs
時間:
2019-7-24 17:05
只看了問題,沒看你程序。你檢查一下按鍵延時,防抖延時看看
作者:
微軟如果
時間:
2019-7-24 19:57
建議加一個防抖動程序和延時配套使用
歡迎光臨 (http://m.raoushi.com/bbs/)
Powered by Discuz! X3.1