单片机里不想阻塞系统的延时

状态机

状态机有助于解决延时导致系统的阻塞问题

按键状态机

头文件

#ifndef STATE_MACHINE_H

#define STATE_MACHINE_H

#include "sys.h"

uint32_t GetSystemSTART_TO_NOW_RunTimer(void);

uint32_t GetSystemLast_TO_NOW_RunTimer(uint32_t LastTime,uint32_t NowTime);

bool GetKeyState(void);

#endif

C文件

/**********************获取系统运行时间***********************************************/

/*DEFIN*/

#define U32_DataMax 4294967295

uint32_t SystemRuningTime_ms = 0;//将该值放入1ms定时器中断中进行累加

/*

获取系统运行的时间

*/

uint32_t GetSystemSTART_TO_NOW_RunTimer(void)

{

return SystemRuningTime_ms;

}

/*

获取系统上一次到现在的时间

*/

uint32_t GetSystemLast_TO_NOW_RunTimer(uint32_t LastTime,uint32_t NowTime)

{

if(LastTime < NowTime)

{

return NowTime - LastTime;

}

else//判断是否溢出

{

return (U32_DataMax - LastTime)+NowTime;

}

}

/***********状态机例子******************************************************************************************/

/*

以按键检测为例

我们如果循环检测按键,消除抖动的时候如果使用delay延时,这对于系统其他在while执行的程序将会是致命的。

因此我们标记状态,根据当前的状态在执行下一步动作。

*/

/*

使用这个模块,有多少种状态?

按键:按键就绪,按键检测按下,按键消抖等待,按键消抖等待完成,按键按下和没有按下

过程:当检测到按下后我们将按键状态标记为按键检测按下,

*/

/***创建状态种类*****/

typedef enum KeyState

{

Ready = 0,//就绪

TestTakeDown,//检测到按下

TakeDown,

NotTakeDown,

}EnumKeyState;

/*DEFINE*/

#define Key_1 1

/*创建变量***************/

/*没有连续计数工能*/

bool GetKeyState1(void)

{

uint32_t NowTime = 0;

static uint32_t UseTimer = 0;

static uint32_t LastTimer = 0;

static EnumKeyState Key1_state = Ready;

switch(Key1_state)

{

case Ready:

if(gpio_input_bit_get(Key_EncoderC_Pro,Key_EncoderC_Pin) == RESET)

{

Key1_state = TestTakeDown;

LastTimer = GetSystemSTART_TO_NOW_RunTimer();//开始获取时间,便于GetSystemLast_TO_NOW_RunTimer函数调用

}

else

{

Key1_state = NotTakeDown;

}

break;

case TestTakeDown:

NowTime = GetSystemSTART_TO_NOW_RunTimer();

UseTimer = GetSystemLast_TO_NOW_RunTimer(LastTimer,NowTime) + UseTimer;

LastTimer = NowTime;

if(UseTimer >= 10)//等待了10ms

{

if(gpio_input_bit_get(Key_EncoderC_Pro,Key_EncoderC_Pin) == RESET)

{

Key1_state = TakeDown;

}

else

{

Key1_state = Ready;

}

UseTimer = 0;

}

break;

case TakeDown:

Key1_state = Ready;

return 1;

break;

case NotTakeDown:

Key1_state = Ready;

break;

return 0;

}

}

非阻塞延时

非阻塞延时,根据延时返回状态判断是否进行下一步动作。

缺陷:只能用一次,如果想用多个,就得写多个函数。

优点:简单方便,适用于延时较少的场合

/*

非阻塞延时

*/

bool Task_Delay(uint32_t time_ms)

{

uint32_t NowTime = 0;

static uint32_t UseTimer = 0;

static EnumKeyState Key1_state = Ready;

static uint32_t LastTimer =0;

static uint8_t flag = 1;

if(flag == 1)

{

LastTimer = GetSystemSTART_TO_NOW_RunTimer();

flag = 0;

}

NowTime = GetSystemSTART_TO_NOW_RunTimer();

UseTimer = GetSystemLast_TO_NOW_RunTimer(LastTimer,NowTime) + UseTimer;

LastTimer = NowTime;

if(time_ms >= UseTimer)

{

flag = 1;

return 1;

}

else

{

return 0;

}

}

软件定时器

以后再补充把,有兴趣的可以网上搜一下

相关推荐
吠品2 小时前
智谱开源 ZCode 之后,真正的考试才开始
单片机·嵌入式硬件·1024程序员节
Chery11404 小时前
高速ADC与精密DAC怎么选?DP9230D与DP8532D应用解析
嵌入式硬件·物联网·射频工程
小李不想当小白7 小时前
SPI通信外设与W25Q64 (硬件SPI)(STM32标准库学习笔记)
笔记·stm32·单片机·嵌入式硬件·学习·嵌入式
LCG米7 小时前
STM32驱动W5500以太网模块实测:TCP吞吐瓶颈定位、TX指针回绕陷阱与断线重连保活落地
stm32·单片机·嵌入式硬件·tcp/ip
尼喃8 小时前
662K/65Z5三脚稳压芯片规格书—PW6566B33/B30引脚功能
嵌入式硬件
创界工坊工作室9 小时前
63-基于STC89C52霍尔传感器的智能化电能表设计
stm32·单片机·嵌入式硬件·51单片机·proteus
智购科技自动售货机厂家10 小时前
让AI经营自动售货机:从Project Vend看工具权限与业务规则~YH
大数据·服务器·单片机·嵌入式硬件·深度学习
Zwawa11 小时前
从 printf 到曲线:把数据送上 VOFA+
c语言·单片机·嵌入式
意法半导体STM3211 小时前
【官方原创】LAT6048 如何扩大MP2的A35核和M33核共享内存
stm32·单片机·嵌入式硬件·内存映射·stm32mp2
en.en..11 小时前
STM32(F429)启动模式与两种烧录方式
stm32·单片机·嵌入式硬件