概述
mcu定时器操作方式很多先列举两个,后边补充。
一:比较灵活开启时间比较自由
1 /*TIMER LIST intferce*/ 2 typedef struct timer_event 3 { 4 uint32_t de_counter; 5 void ( *callback )( void ); 6 struct timer_event *next; 7 }timer_event_t; 8 9 void TimerInit( timer_event_t *obj,void( *callback)(void)); 10 void TimerStart( timer_event_t *obj,uint32_t timer_val); 11 void TimerStop( timer_event_t *obj); 12 void App1MsTimerTickCallBack(void);
static timer_event_t timer_list_head = {0,NULL,NULL}; //表头 /* 定时器list */ void App1MsTimerTickCallBack(void) { timer_event_t* cur = &timer_list_head; while(cur->next != NULL) { cur = cur->next; if(cur->de_counter > 0) { cur->de_counter -= 1; if(cur->de_counter == 0) { cur->callback(); } } } } void TimerInit( timer_event_t *obj,void( *callback)(void)) { timer_event_t* cur = &timer_list_head; while(cur->next != NULL) { cur = cur->next; } cur->next = obj; obj->de_counter = 0; obj->callback = callback; obj->next = NULL; } void TimerStart( timer_event_t *obj,uint32_t timer_val) { obj->de_counter = timer_val; } void TimerStop( timer_event_t *obj) { obj->de_counter = 0; }
二.第二种适合裸核的任务执行
/* timer */ typedef struct tagTIMEMANAGE { uint16_t Cnt_1ms; uint16_t Cnt_10ms; uint16_t Cnt_100ms; uint16_t Cnt_500ms; uint16_t Cnt_1s; uint16_t Cnt_Test; union TIMEOUT_REG{ uint16_t all; struct TIMEOUT_BIT { uint16_t F_1ms :1; uint16_t F_10ms :1; uint16_t F_100ms :1; uint16_t F_500ms :1; uint16_t F_1s :1; uint16_t rsvd :11; } bit; } TimeOut; } tTIMEMANAGE; extern tTIMEMANAGE TimeManage; #define TIMEMANAGE_DEFAULT { 0, 0, 0, 0, 0, 0, 0, }
/* 全局变量 */ tTIMEMANAGE TimeManage = TIMEMANAGE_DEFAULT; while (1) { if(TimeManage.TimeOut.bit.F_1s == 1) //1s时钟 { TimeManage.TimeOut.bit.F_1s = 0; /* user */ } if(TimeManage.TimeOut.bit.F_500ms == 1) //500ms时钟 { TimeManage.TimeOut.bit.F_500ms = 0; /* user */ } if(TimeManage.TimeOut.bit.F_100ms == 1) //100ms时钟 { TimeManage.TimeOut.bit.F_100ms = 0; /* user */ } } void HAL_SYSTICK_Callback(void) { /* NOTE : This function Should not be modified, when the callback is needed, the HAL_SYSTICK_Callback could be implemented in the user file */ TimeManage.Cnt_10ms++; if(TimeManage.Cnt_10ms >= 10) //10ms周期时钟 { TimeManage.Cnt_10ms = 0; TimeManage.TimeOut.bit.F_10ms = 1; } TimeManage.Cnt_100ms++; if(TimeManage.Cnt_100ms >= 100) //100ms周期时钟 { TimeManage.Cnt_100ms = 0; TimeManage.TimeOut.bit.F_100ms = 1; } TimeManage.Cnt_500ms++; if(TimeManage.Cnt_500ms >= 500) //500ms周期时钟 { TimeManage.Cnt_500ms = 0; TimeManage.TimeOut.bit.F_500ms = 1; } TimeManage.Cnt_1s++; if(TimeManage.Cnt_1s >= 1000) //1s周期时钟 { TimeManage.Cnt_1s = 0; TimeManage.TimeOut.bit.F_1s = 1; } }
转载于:https://www.cnblogs.com/firstparke/p/8351248.html
最后
以上就是悦耳小蝴蝶为你收集整理的MCU定时器分频的N种用法的全部内容,希望文章能够帮你解决MCU定时器分频的N种用法所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复