当前位置:网站首页>[single chip microcomputer] single timer in front and back platform program framework to realize multi delay tasks
[single chip microcomputer] single timer in front and back platform program framework to realize multi delay tasks
2022-06-13 01:58:00 【lovemengx】
Preface explains
In the many things I have come into contact with MCU Source code ( Don't run RTOS), Whether it is the source code of our colleagues or other solutions , In order to achieve some work that does not require too precise fixed cycle , A common practice is to start a timer , Interrupts are generated at relatively small intervals , Such as 0.1 One millisecond , Count in timer interrupt function , Set multiple global variables according to the count value 1, Export these global variables in the required source file . If required, every 500ms Report it once ADC Detected value , By judgment 500ms Whether the global variable of is set 1, If set, it will ADC The value is sent through the serial port , And set the global variable to 0, Implementation interval 500ms Report it once .
In fact, this idea is very good , But we often see these global variables flying all over the world , I really can't stand it , Even if some source code centralizes all the places where you need to judge these global variables in one source file ,N Much of the IF sentence , It will also dazzle people , Especially when the variable name is similar to the context , Especially prone to mistakes , It's really hard to maintain . Encounter this source code , As long as the project time permits , I would probably choose to rewrite , Otherwise, it would be too painful .( Usually, this kind of global variables fly all over the source code , The code for other functions is no better )
Because this method is very common , Also useful , So I encapsulate it as a basic interface , The measured results are good , The core idea has not changed .
Encapsulating source code
time_base.h
/**
******************************************************************************
* @ file time_base.h
* @ edition V1.0.0
* @ date
* @ Summary Timing function interface
* @ author lmx
* @ mailbox [email protected]
******************************************************************************
* @ Be careful
*
******************************************************************************
*/
#ifndef TIME_BASE_H
#define TIME_BASE_H
// Optional time points
typedef enum
{
TIME_BASE_FLAGS_01MS = 1 << 0,
TIME_BASE_FLAGS_1MS = 1 << 1,
TIME_BASE_FLAGS_10MS = 1 << 2,
TIME_BASE_FLAGS_100MS = 1 << 3,
TIME_BASE_FLAGS_500MS = 1 << 4,
TIME_BASE_FLAGS_1000MS = 1 << 5,
TIME_BASE_FLAGS_2000MS = 1 << 6,
TIME_BASE_FLAGS_3000MS = 1 << 7,
TIME_BASE_FLAGS_ALL = 0xFF,
}timer_base_flags_e;
// Initialization and release
void time_base_init();
void time_base_release();
// Every time a loop is completed in the main loop, it needs to call
void time_base_update_flags();
// Each function module needs to be called when it is executed at a certain time point , Pass in the corresponding flag to judge whether the time point has reached
unsigned char time_base_is_flags(timer_base_flags_e flags);
#endif time_base.c
/**
******************************************************************************
* @ file time_base.h
* @ edition V1.0.0
* @ date
* @ Summary Timing function interface
* @ author lmx
* @ mailbox [email protected]
******************************************************************************
* @ Be careful
*
******************************************************************************
*/
#include "gd32e23x.h"
#include "configuration.h"
#include "time_base.h"
// Define timer reference structure
typedef struct
{
unsigned int bitmap; // Use bits to record whether each time point is in place
unsigned int time[8]; // The count value required for each data position bit
}timer_base_flags_t;
// The value here is the same as timer_base_flags_e One-to-one correspondence , And it is related to timer interrupt time
// Timers every 0.1ms There will be an interrupt , The value here must be from small to large
static volatile timer_base_flags_t timer_base_flags = {
.bitmap = 0x00,
.time[0] = 1, // 0.1ms
.time[1] = 10, // 1ms
.time[2] = 100, // 10ms
.time[3] = 1000, // 100ms
.time[4] = 5000, // 500ms
.time[5] = 10000, // 1000ms
.time[6] = 20000, // 2000ms
.time[7] = 30000, // 3000ms
};
#define TIMER_BASE_TIME_ARRAR_MAX (sizeof(timer_base_flags.time) / sizeof(timer_base_flags.time[0]))
// It is used by the external interface to determine whether the specified time point has arrived
static volatile unsigned int timer_bitmap = 0x00;
// Timer interrupt service program
void CFG_TIME_BASE_IRQ_FUNCTION(void)
{
static unsigned int time_01ms_count = 0;
static unsigned int i = 0;
if(RESET != timer_interrupt_flag_get (CFG_TIME_BASE_TIMER, TIMER_INT_FLAG_UP))
{
timer_interrupt_flag_clear(CFG_TIME_BASE_TIMER, TIMER_INT_UP);
// The counter counts , When the maximum value is reached, it will automatically reset to 1
time_01ms_count = time_01ms_count % timer_base_flags.time[TIMER_BASE_TIME_ARRAR_MAX - 1] + 1;
timer_base_flags.bitmap |= 1 << 0;
// Whether to arrive at each time point in turn
for(i = 1; i < TIMER_BASE_TIME_ARRAR_MAX; i++){
if(0 == time_01ms_count % timer_base_flags.time[i]){
timer_base_flags.bitmap |= 1 << i;
}
}
}
}
// Update flag bit
void time_base_update_flags()
{
static unsigned int i = 0;
// Copy the current time flag bit , It is convenient for other modules to judge whether the specified time point has arrived
timer_interrupt_disable(CFG_TIME_BASE_TIMER, TIMER_INT_UP);
timer_bitmap = timer_base_flags.bitmap;
timer_base_flags.bitmap &= ~timer_base_flags.bitmap;
timer_interrupt_enable(CFG_TIME_BASE_TIMER, TIMER_INT_UP);
return ;
}
// Judgment flags
unsigned char time_base_is_flags(timer_base_flags_e flags)
{
return timer_bitmap & flags ? 1 : 0;
}
// initialization
void time_base_init()
{
timer_parameter_struct para;
rcu_periph_clock_enable(CFG_TIME_BASE_RCU_CLOCK);
timer_deinit(CFG_TIME_BASE_TIMER);
para.prescaler = 720 - 1; // 72M720 frequency division 100khz
para.alignedmode = TIMER_COUNTER_EDGE;
para.counterdirection = TIMER_COUNTER_UP; // Count up
para.period = 10 - 1; // 0.1 ms
para.clockdivision = 0; // Regardless of the frequency
para.repetitioncounter = 0; // Count duplicates 0
timer_init(CFG_TIME_BASE_TIMER, ¶);
nvic_irq_enable(CFG_TIME_BASE_IRQ_NUMBER, 0U);
timer_interrupt_enable(CFG_TIME_BASE_TIMER, TIMER_INT_UP);
timer_auto_reload_shadow_enable(CFG_TIME_BASE_TIMER);
timer_enable(CFG_TIME_BASE_TIMER);
return ;
}
// Release
void time_base_release()
{
timer_interrupt_disable(CFG_TIME_BASE_TIMER, TIMER_INT_UP);
nvic_irq_disable(CFG_TIME_BASE_IRQ_NUMBER);
timer_disable(CFG_TIME_BASE_TIMER);
rcu_periph_clock_disable(CFG_TIME_BASE_RCU_CLOCK);
return ;
}
configuration.h
/**
******************************************************************************
* @ file configuration.h
* @ edition V1.0.0
* @ date
* @ Summary Configuration information file
* @ author lmx
* @ mailbox [email protected]
******************************************************************************
* @ Be careful
*
******************************************************************************
*/
#ifndef CONFIGURATION_H
#define CONFIGURATION_H
#include "gd32e23x.h"
#include "gd32e230c_eval.h"
#include "systick.h"
// Time base timer
#define CFG_TIME_BASE_TIMER TIMER2
#define CFG_TIME_BASE_RCU_CLOCK RCU_TIMER2
#define CFG_TIME_BASE_IRQ_NUMBER TIMER2_IRQn
#define CFG_TIME_BASE_IRQ_FUNCTION TIMER2_IRQHandler
#endif
Usage method
/**
******************************************************************************
* @ file main.c
* @ edition V1.0.0
* @ date
* @ Summary Main program file
* @ author lmx
******************************************************************************
* @ Be careful
*
* The copyright belongs to Shenzhen zhihuichuang Technology Co., Ltd , Do not use or modify without permission
*
******************************************************************************
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "time_base.h"
#include "xxx.h"
#include "ccc.h"
#include "aaa.h"
int main(void)
{
time_base_init();
// Main circulation
while(1)
{
time_base_update_flags(); // Update time stamp
xxx_process();
ccc_process();
aaa_process();
}
time_base_release();
return 0;
}
// xxx.c The realization inside
void xxx_process()
{
if(time_base_is_flags(TIME_BASE_FLAGS_100MS) == 0){
return ;
}
// 100 ms Do the things
}
// ccc.c The realization inside
void ccc_process()
{
if(time_base_is_flags(TIME_BASE_FLAGS_3000MS) == 0){
return ;
}
// 3000 ms Do the things
}
// aaa.c The realization inside
void aaa_process()
{
if(time_base_is_flags(TIME_BASE_FLAGS_500MS) == 0){
return ;
}
// 500 ms Do the things
}
边栏推荐
- Alertwindowmanager pop up prompt window help (Part 1)
- Compiling minicom-2.7.1 under msys2
- SWD debugging mode of stm32
- 6、 Implementation of warehouse out management function
- Looking at Qianxin's "wild prospect" of network security from the 2021 annual performance report
- Delphi 10.4.2 release instructions and installation methods of three patches
- Introduction to ROS runtime
- TensorFlow 2. X multi graphics card distributed training
- 水管工遊戲
- pytorch : srcIndex < srcSelectDimSize
猜你喜欢

Magics 23.0如何激活和使用视图工具页的切片预览功能

Stm32 3*3 matrix key (register version)

What did Hello travel do right for 500million users in five years?

传感器:MQ-5燃气模块测量燃气值(底部附代码)

微服务开发环境搭建

华为设备配置虚拟专用网FRR

Vscode configuration header file -- Take opencv and its own header file as an example
![[the second day of the actual combat of the smart lock project based on stm32f401ret6 in 10 days] light up with the key ----- input and output of GPIO](/img/98/77191c51c1bab28448fe197ea13a33.jpg)
[the second day of the actual combat of the smart lock project based on stm32f401ret6 in 10 days] light up with the key ----- input and output of GPIO

Learning notes 51 single chip microcomputer keyboard (non coding keyboard and coding keyboard, scanning mode of non coding keyboard, independent keyboard, matrix keyboard)

Day 1 of the 10 day smart lock project (understand the SCM stm32f401ret6 and C language foundation)
随机推荐
Répertoire d'exclusion du transport rsync
Leetcode question 20
三、上传织物图片至SQL Server并提供name进行展示织物照片
Detailed explanation of C language conditional compilation
numpy多维数组转置transpose
In addition to the full screen without holes under the screen, the Red Devils 7 series also has these black technologies
How does Google's audience work?
Ruixing coffee moves towards "national consumption"
Padavan mounts SMB sharing and compiles ffmpeg
Luzhengyao, who has entered the prefabricated vegetable track, still needs to stop being impatient
Application and routine of C language typedef struct
反爬虫策略(ip代理、设置随机休眠时间、哔哩哔哩视频信息爬取、真实URL的获取、特殊字符的处理、时间戳的处理、多线程处理)
[the second day of the actual combat of the smart lock project based on stm32f401ret6 in 10 days] light up with the key ----- input and output of GPIO
The commercial value of Kwai is being seen by more and more brands and businesses
Opencv camera calibration (2): fish eye camera calibration
Alertwindowmanager pop up prompt window help (Part 1)
dfs与bfs解决宝岛探险
Detailed explanation of maxpooling corresponding to conv1d, conv2d and conv3d machines of tensorflow2
Devaxpress Chinese description --tcximagelist (enhanced image list control)
华为设备配置虚拟专用网FRR