第1期 定时器实现非阻塞式程序 按键控制LED闪烁模式
参考江协科技
KEY1和KEY2两者独立控制互不影响
阻塞:如果按下按键不松手,程序就会卡死在while循环里,主程序的其他程序无法执行,直到松手,函数才能结束。CPU花很长时间等大地。
非阻塞:程序执行很快且很快结束。
任务:按下K1慢闪,再按下K1熄灭
常规方法:
为什么开灯灵敏,关灯就不灵敏呢?因为开灯之后,程序会执行delay等待以及while等待,阻塞按键扫描程序,只有长按按键才能熄灭LED。
#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "OLED.h"
#include "Key.h"
#include "LED.h"
uint8_t KeyNum = 0;
uint8_t FlashFlag = 0;
int main(void)
{
OLED_Init();
Key_Init();
LED_Init();
while (1)
{
KeyNum = Key_GetNum();
if(KeyNum == 1)
{
FlashFlag = !FlashFlag;
}
if(FlashFlag)
{
LED1_ON();
Delay_ms(500);
LED1_OFF();
Delay_ms(500);
}
else
{
LED1_OFF