文章目录
- 前言
- 收获
- main.c
- MultiTimer.c
- MultiTimer.h
前言
博主花费几天时间研究了此开源软件定时器扩展模块代码,并参考别的博主讲解解决了定时器计数值会溢出的问题(很多博主写的文章并没有谈到这个计数溢出问题,我就想问一下看代码自己都不思考吗),且把代码风格改成了我的代码风格(修改了变量命名、函数命名、增加了枚举参数)
源代码:GitCode / GitHub 地址
1、解决计数值溢出,这篇文章代码有问题,且我不认同,仅作为参考:参考博客一地址
2、我是靠这篇博文,修改解决了计数值溢出的问题:参考博客二地址
收获
在研究改代码中,我温习了学校学的数据结构的链表知识,嗯,看来我得抽空好好补补数据结构了,还是有很多要学习的。
main.c
#include "Multi_Timer.h"
//定义结构体
Struct_Timer Timer1;
Struct_Timer Timer2;
void Timer1Callback(void *arg)
{
//printf("Timer1 timeout! arg: %p\r\n", arg);
}
void Timer2Callback(void *arg)
{
//printf("Timer2 timeout! arg: %p\r\n", arg);
}
int main(void)
{
TimerInit(&Timer1, Timer1Callback, TIMER1_TIME, TIMER_ALWAYS_LOOP, NULL); //1s loop
TimerStart(&Timer1);
TimerInit(&Timer2, Timer2Callback, TIMER2_TIME, TIMER_ONE_LOOP, NULL); //50ms delay
TimerStart(&Timer2);
while(1)
{
TimerLoop();
}
return 0;
}
void HAL_SYSTICK_Callback(void)
{
TimerTicksCount(); //1ms ticks
}
HAL_SYSTICK_Callback()这个函数放在你自己的定时器中断里面,没啥好说的
MultiTimer.c
/*
* Copyright (c) 2016 Zibin Zheng <znbin@qq.com>
* All rights reserved
*/
#include <stdio.h>
#include "Multi_Timer.h"
//timer handle list head.
static struct Timer* head_handle = NULL;//初始值给NULL
//Timer ticks
//static uint32_t _timer_ticks = (1 << 32)- 1000; // only for test tick clock overflow
static uint32_t s_u32_timer_ticks = 0;
/**
* @brief Initializes the timer struct handle.
* @param handle: the timer handle strcut.
* @param timeout_cb: timeout callback.
* @param timeout: delay to start the timer.
* @param repeat: repeat interval time.
* @param arg: the input argument for timeout_cb fucntion.
* @retval None
*/
void TimerInit(struct Timer* handle, void (*timeout_cb)(void *arg),
Enum_Timer timeout, Enum_Timer repeat, void *arg)
{
handle->timeout_cb = timeout_cb;
handle->timeout = (uint32_t)timeout;
handle->repeat = (uint32_t)repeat;
handle->cur_ticks = s_u32_timer_ticks;
//handle->cur_expired_time = handle->timeout;
handle->arg = arg;
//printf("cur_ticks: %u, cur_expired_time: %u, _timer_ticks: %u, timeout: %u\r\n",
//handle->cur_ticks, handle->cur_expired_time, _timer_ticks, timeout);
}
/**
* @brief Start the timer work, add the handle into work list.
* @param btn: target handle strcut.
* @retval 0: succeed. -1: already exist.
*/
int TimerStart(struct Timer* handle)
{
struct Timer* target = head_handle;
while(target)
{
if(target == handle)
{
return -1;//already exist.
}
target = target->next;
}
handle->next = head_handle;
head_handle = handle;
return 0;
}
/**
* @brief Stop the timer work, remove the handle off work list.
* @param handle: target handle strcut.
* @retval 0: succeed. -1: timer not exist.
*/
int TimerStop(struct Timer* handle)
{
struct Timer** curr;
for(curr = &head_handle; *curr;)
{
struct Timer* entry = *curr;
if(entry == handle)
{
*curr = entry->next;
//free(entry);
return 0;//found specified timer
}
else//如果是timer1(尾节点)
{
curr = &entry->next;
}
}
return 0;
}
/**
* @brief main loop.
* @param None.
* @retval None
*/
void TimerLoop(void)
{
struct Timer* target;
/*if(s_u32_timer_ticks == 0)
{
for(target = head_handle; target; target = target->next)
{
target->cur_ticks = (uint32_t)(0xFFFFFFFF) - target->cur_ticks;
}
}*/
for(target = head_handle; target; target = target->next)
{
/*
More detail on tick-clock overflow, please see https://blog.csdn.net/szullc/article/details/115332326
*/
//if(s_u32_timer_ticks - (target->cur_ticks + target->timeout) >= 0)
if(COMPARE(s_u32_timer_ticks,(target->cur_ticks + target->timeout)) >= 0)
{
//printf("cur_ticks: %u, cur_expired_time: %u, _timer_ticks: %u\r\n",
//target->cur_ticks, target->cur_expired_time, _timer_ticks);
if(target->repeat == 0)
{
TimerStop(target);
}
else
{
target->cur_ticks = s_u32_timer_ticks;
//target->cur_expired_time = target->timeout;
}
target->timeout_cb(target->arg);
}
}
}
/**
* @brief background ticks, timer repeat invoking interval nms.
* @param None.
* @retval None.
*/
void TimerTicksCount(void)
{
s_u32_timer_ticks += DEFINE_TIMER_BASE_TICK;
}
//handle->cur_expired_time = handle->timeout;这个是我注释掉的,因为用不到
TimerStart就是插入任务链表
TimerStop就是删除任务链表
TimerLoop这个函数被我根据别人博客讲解改写的,完美解决计数溢出的问题,为此我还专门写了C程序来验证
如果说不解决这个计数溢出也行,因为理论上来说1ms记一次,记满一次32位的变量也得个一两年吧,反正有的博主以这个理由说明不用管,我就想说,一两年就不管了吗?程序还是要尽善尽美的
#include "stdio.h"
int main()
{
unsigned char b = 254;
unsigned char a = 0;
for (a=0; a<=254; a++)
{
printf("a的unsigned char值是:%d,b的unsigned char值是:%d",a ,b);
printf(" (a-b)转换成char值是:%d\r\n", ((char)a - (char)b));
}
return 0;
}
a的unsigned char值是:0、b的unsigned char值是:254、((char)a - (char)b))值是:2
a的unsigned char值是:1、b的unsigned char值是:254、((char)a - (char)b))值是:3
a的unsigned char值是:2、b的unsigned char值是:254、((char)a - (char)b))值是:4
a的unsigned char值是:3、b的unsigned char值是:254、((char)a - (char)b))值是:5
a的unsigned char值是:4、b的unsigned char值是:254、((char)a - (char)b))值是:6
a的unsigned char值是:5、b的unsigned char值是:254、((char)a - (char)b))值是:7
a的unsigned char值是:6、b的unsigned char值是:254、((char)a - (char)b))值是:8
a的unsigned char值是:7、b的unsigned char值是:254、((char)a - (char)b))值是:9
a的unsigned char值是:8、b的unsigned char值是:254、((char)a - (char)b))值是:10
a的unsigned char值是:9、b的unsigned char值是:254、((char)a - (char)b))值是:11
a的unsigned char值是:10、b的unsigned char值是:254、((char)a - (char)b))值是:12
a的unsigned char值是:11、b的unsigned char值是:254、((char)a - (char)b))值是:13
a的unsigned char值是:12、b的unsigned char值是:254、((char)a - (char)b))值是:14
a的unsigned char值是:13、b的unsigned char值是:254、((char)a - (char)b))值是:15
a的unsigned char值是:14、b的unsigned char值是:254、((char)a - (char)b))值是:16
a的unsigned char值是:15、b的unsigned char值是:254、((char)a - (char)b))值是:17
a的unsigned char值是:16、b的unsigned char值是:254、((char)a - (char)b))值是:18
a的unsigned char值是:17、b的unsigned char值是:254、((char)a - (char)b))值是:19
a的unsigned char值是:18、b的unsigned char值是:254、((char)a - (char)b))值是:20
a的unsigned char值是:19、b的unsigned char值是:254、((char)a - (char)b))值是:21
a的unsigned char值是:20、b的unsigned char值是:254、((char)a - (char)b))值是:22
a的unsigned char值是:21、b的unsigned char值是:254、((char)a - (char)b))值是:23
a的unsigned char值是:22、b的unsigned char值是:254、((char)a - (char)b))值是:24
a的unsigned char值是:23、b的unsigned char值是:254、((char)a - (char)b))值是:25
a的unsigned char值是:24、b的unsigned char值是:254、((char)a - (char)b))值是:26
a的unsigned char值是:25、b的unsigned char值是:254、((char)a - (char)b))值是:27
a的unsigned char值是:26、b的unsigned char值是:254、((char)a - (char)b))值是:28
a的unsigned char值是:27、b的unsigned char值是:254、((char)a - (char)b))值是:29
a的unsigned char值是:28、b的unsigned char值是:254、((char)a - (char)b))值是:30
a的unsigned char值是:29、b的unsigned char值是:254、((char)a - (char)b))值是:31
a的unsigned char值是:30、b的unsigned char值是:254、((char)a - (char)b))值是:32
a的unsigned char值是:31、b的unsigned char值是:254、((char)a - (char)b))值是:33
a的unsigned char值是:32、b的unsigned char值是:254、((char)a - (char)b))值是:34
a的unsigned char值是:33、b的unsigned char值是:254、((char)a - (char)b))值是:35
a的unsigned char值是:34、b的unsigned char值是:254、((char)a - (char)b))值是:36
a的unsigned char值是:35、b的unsigned char值是:254、((char)a - (char)b))值是:37
a的unsigned char值是:36、b的unsigned char值是:254、((char)a - (char)b))值是:38
a的unsigned char值是:37、b的unsigned char值是:254、((char)a - (char)b))值是:39
a的unsigned char值是:38、b的unsigned char值是:254、((char)a - (char)b))值是:40
a的unsigned char值是:39、b的unsigned char值是:254、((char)a - (char)b))值是:41
a的unsigned char值是:40、b的unsigned char值是:254、((char)a - (char)b))值是:42
a的unsigned char值是:41、b的unsigned char值是:254、((char)a - (char)b))值是:43
a的unsigned char值是:42、b的unsigned char值是:254、((char)a - (char)b))值是:44
a的unsigned char值是:43、b的unsigned char值是:254、((char)a - (char)b))值是:45
a的unsigned char值是:44、b的unsigned char值是:254、((char)a - (char)b))值是:46
a的unsigned char值是:45、b的unsigned char值是:254、((char)a - (char)b))值是:47
a的unsigned char值是:46、b的unsigned char值是:254、((char)a - (char)b))值是:48
a的unsigned char值是:47、b的unsigned char值是:254、((char)a - (char)b))值是:49
a的unsigned char值是:48、b的unsigned char值是:254、((char)a - (char)b))值是:50
a的unsigned char值是:49、b的unsigned char值是:254、((char)a - (char)b))值是:51
a的unsigned char值是:50、b的unsigned char值是:254、((char)a - (char)b))值是:52
a的unsigned char值是:51、b的unsigned char值是:254、((char)a - (char)b))值是:53
a的unsigned char值是:52、b的unsigned char值是:254、((char)a - (char)b))值是:54
a的unsigned char值是:53、b的unsigned char值是:254、((char)a - (char)b))值是:55
a的unsigned char值是:54、b的unsigned char值是:254、((char)a - (char)b))值是:56
a的unsigned char值是:55、b的unsigned char值是:254、((char)a - (char)b))值是:57
a的unsigned char值是:56、b的unsigned char值是:254、((char)a - (char)b))值是:58
a的unsigned char值是:57、b的unsigned char值是:254、((char)a - (char)b))值是:59
a的unsigned char值是:58、b的unsigned char值是:254、((char)a - (char)b))值是:60
a的unsigned char值是:59、b的unsigned char值是:254、((char)a - (char)b))值是:61
a的unsigned char值是:60、b的unsigned char值是:254、((char)a - (char)b))值是:62
a的unsigned char值是:61、b的unsigned char值是:254、((char)a - (char)b))值是:63
a的unsigned char值是:62、b的unsigned char值是:254、((char)a - (char)b))值是:64
a的unsigned char值是:63、b的unsigned char值是:254、((char)a - (char)b))值是:65
a的unsigned char值是:64、b的unsigned char值是:254、((char)a - (char)b))值是:66
a的unsigned char值是:65、b的unsigned char值是:254、((char)a - (char)b))值是:67
a的unsigned char值是:66、b的unsigned char值是:254、((char)a - (char)b))值是:68
a的unsigned char值是:67、b的unsigned char值是:254、((char)a - (char)b))值是:69
a的unsigned char值是:68、b的unsigned char值是:254、((char)a - (char)b))值是:70
a的unsigned char值是:69、b的unsigned char值是:254、((char)a - (char)b))值是:71
a的unsigned char值是:70、b的unsigned char值是:254、((char)a - (char)b))值是:72
a的unsigned char值是:71、b的unsigned char值是:254、((char)a - (char)b))值是:73
a的unsigned char值是:72、b的unsigned char值是:254、((char)a - (char)b))值是:74
a的unsigned char值是:73、b的unsigned char值是:254、((char)a - (char)b))值是:75
a的unsigned char值是:74、b的unsigned char值是:254、((char)a - (char)b))值是:76
a的unsigned char值是:75、b的unsigned char值是:254、((char)a - (char)b))值是:77
a的unsigned char值是:76、b的unsigned char值是:254、((char)a - (char)b))值是:78
a的unsigned char值是:77、b的unsigned char值是:254、((char)a - (char)b))值是:79
a的unsigned char值是:78、b的unsigned char值是:254、((char)a - (char)b))值是:80
a的unsigned char值是:79、b的unsigned char值是:254、((char)a - (char)b))值是:81
a的unsigned char值是:80、b的unsigned char值是:254、((char)a - (char)b))值是:82
a的unsigned char值是:81、b的unsigned char值是:254、((char)a - (char)b))值是:83
a的unsigned char值是:82、b的unsigned char值是:254、((char)a - (char)b))值是:84
a的unsigned char值是:83、b的unsigned char值是:254、((char)a - (char)b))值是:85
a的unsigned char值是:84、b的unsigned char值是:254、((char)a - (char)b))值是:86
a的unsigned char值是:85、b的unsigned char值是:254、((char)a - (char)b))值是:87
a的unsigned char值是:86、b的unsigned char值是:254、((char)a - (char)b))值是:88
a的unsigned char值是:87、b的unsigned char值是:254、((char)a - (char)b))值是:89
a的unsigned char值是:88、b的unsigned char值是:254、((char)a - (char)b))值是:90
a的unsigned char值是:89、b的unsigned char值是:254、((char)a - (char)b))值是:91
a的unsigned char值是:90、b的unsigned char值是:254、((char)a - (char)b))值是:92
a的unsigned char值是:91、b的unsigned char值是:254、((char)a - (char)b))值是:93
a的unsigned char值是:92、b的unsigned char值是:254、((char)a - (char)b))值是:94
a的unsigned char值是:93、b的unsigned char值是:254、((char)a - (char)b))值是:95
a的unsigned char值是:94、b的unsigned char值是:254、((char)a - (char)b))值是:96
a的unsigned char值是:95、b的unsigned char值是:254、((char)a - (char)b))值是:97
a的unsigned char值是:96、b的unsigned char值是:254、((char)a - (char)b))值是:98
a的unsigned char值是:97、b的unsigned char值是:254、((char)a - (char)b))值是:99
a的unsigned char值是:98、b的unsigned char值是:254、((char)a - (char)b))值是:100
a的unsigned char值是:99、b的unsigned char值是:254、((char)a - (char)b))值是:101
a的unsigned char值是:100、b的unsigned char值是:254、((char)a - (char)b))值是:102
a的unsigned char值是:101、b的unsigned char值是:254、((char)a - (char)b))值是:103
a的unsigned char值是:102、b的unsigned char值是:254、((char)a - (char)b))值是:104
a的unsigned char值是:103、b的unsigned char值是:254、((char)a - (char)b))值是:105
a的unsigned char值是:104、b的unsigned char值是:254、((char)a - (char)b))值是:106
a的unsigned char值是:105、b的unsigned char值是:254、((char)a - (char)b))值是:107
a的unsigned char值是:106、b的unsigned char值是:254、((char)a - (char)b))值是:108
a的unsigned char值是:107、b的unsigned char值是:254、((char)a - (char)b))值是:109
a的unsigned char值是:108、b的unsigned char值是:254、((char)a - (char)b))值是:110
a的unsigned char值是:109、b的unsigned char值是:254、((char)a - (char)b))值是:111
a的unsigned char值是:110、b的unsigned char值是:254、((char)a - (char)b))值是:112
a的unsigned char值是:111、b的unsigned char值是:254、((char)a - (char)b))值是:113
a的unsigned char值是:112、b的unsigned char值是:254、((char)a - (char)b))值是:114
a的unsigned char值是:113、b的unsigned char值是:254、((char)a - (char)b))值是:115
a的unsigned char值是:114、b的unsigned char值是:254、((char)a - (char)b))值是:116
a的unsigned char值是:115、b的unsigned char值是:254、((char)a - (char)b))值是:117
a的unsigned char值是:116、b的unsigned char值是:254、((char)a - (char)b))值是:118
a的unsigned char值是:117、b的unsigned char值是:254、((char)a - (char)b))值是:119
a的unsigned char值是:118、b的unsigned char值是:254、((char)a - (char)b))值是:120
a的unsigned char值是:119、b的unsigned char值是:254、((char)a - (char)b))值是:121
a的unsigned char值是:120、b的unsigned char值是:254、((char)a - (char)b))值是:122
a的unsigned char值是:121、b的unsigned char值是:254、((char)a - (char)b))值是:123
a的unsigned char值是:122、b的unsigned char值是:254、((char)a - (char)b))值是:124
a的unsigned char值是:123、b的unsigned char值是:254、((char)a - (char)b))值是:125
a的unsigned char值是:124、b的unsigned char值是:254、((char)a - (char)b))值是:126
a的unsigned char值是:125、b的unsigned char值是:254、((char)a - (char)b))值是:127
a的unsigned char值是:126、b的unsigned char值是:254、((char)a - (char)b))值是:128
a的unsigned char值是:127、b的unsigned char值是:254、((char)a - (char)b))值是:129
a的unsigned char值是:128、b的unsigned char值是:254、((char)a - (char)b))值是:-126
a的unsigned char值是:129、b的unsigned char值是:254、((char)a - (char)b))值是:-125
a的unsigned char值是:130、b的unsigned char值是:254、((char)a - (char)b))值是:-124
a的unsigned char值是:131、b的unsigned char值是:254、((char)a - (char)b))值是:-123
a的unsigned char值是:132、b的unsigned char值是:254、((char)a - (char)b))值是:-122
a的unsigned char值是:133、b的unsigned char值是:254、((char)a - (char)b))值是:-121
a的unsigned char值是:134、b的unsigned char值是:254、((char)a - (char)b))值是:-120
a的unsigned char值是:135、b的unsigned char值是:254、((char)a - (char)b))值是:-119
a的unsigned char值是:136、b的unsigned char值是:254、((char)a - (char)b))值是:-118
a的unsigned char值是:137、b的unsigned char值是:254、((char)a - (char)b))值是:-117
a的unsigned char值是:138、b的unsigned char值是:254、((char)a - (char)b))值是:-116
a的unsigned char值是:139、b的unsigned char值是:254、((char)a - (char)b))值是:-115
a的unsigned char值是:140、b的unsigned char值是:254、((char)a - (char)b))值是:-114
a的unsigned char值是:141、b的unsigned char值是:254、((char)a - (char)b))值是:-113
a的unsigned char值是:142、b的unsigned char值是:254、((char)a - (char)b))值是:-112
a的unsigned char值是:143、b的unsigned char值是:254、((char)a - (char)b))值是:-111
a的unsigned char值是:144、b的unsigned char值是:254、((char)a - (char)b))值是:-110
a的unsigned char值是:145、b的unsigned char值是:254、((char)a - (char)b))值是:-109
a的unsigned char值是:146、b的unsigned char值是:254、((char)a - (char)b))值是:-108
a的unsigned char值是:147、b的unsigned char值是:254、((char)a - (char)b))值是:-107
a的unsigned char值是:148、b的unsigned char值是:254、((char)a - (char)b))值是:-106
a的unsigned char值是:149、b的unsigned char值是:254、((char)a - (char)b))值是:-105
a的unsigned char值是:150、b的unsigned char值是:254、((char)a - (char)b))值是:-104
a的unsigned char值是:151、b的unsigned char值是:254、((char)a - (char)b))值是:-103
a的unsigned char值是:152、b的unsigned char值是:254、((char)a - (char)b))值是:-102
a的unsigned char值是:153、b的unsigned char值是:254、((char)a - (char)b))值是:-101
a的unsigned char值是:154、b的unsigned char值是:254、((char)a - (char)b))值是:-100
a的unsigned char值是:155、b的unsigned char值是:254、((char)a - (char)b))值是:-99
a的unsigned char值是:156、b的unsigned char值是:254、((char)a - (char)b))值是:-98
a的unsigned char值是:157、b的unsigned char值是:254、((char)a - (char)b))值是:-97
a的unsigned char值是:158、b的unsigned char值是:254、((char)a - (char)b))值是:-96
a的unsigned char值是:159、b的unsigned char值是:254、((char)a - (char)b))值是:-95
a的unsigned char值是:160、b的unsigned char值是:254、((char)a - (char)b))值是:-94
a的unsigned char值是:161、b的unsigned char值是:254、((char)a - (char)b))值是:-93
a的unsigned char值是:162、b的unsigned char值是:254、((char)a - (char)b))值是:-92
a的unsigned char值是:163、b的unsigned char值是:254、((char)a - (char)b))值是:-91
a的unsigned char值是:164、b的unsigned char值是:254、((char)a - (char)b))值是:-90
a的unsigned char值是:165、b的unsigned char值是:254、((char)a - (char)b))值是:-89
a的unsigned char值是:166、b的unsigned char值是:254、((char)a - (char)b))值是:-88
a的unsigned char值是:167、b的unsigned char值是:254、((char)a - (char)b))值是:-87
a的unsigned char值是:168、b的unsigned char值是:254、((char)a - (char)b))值是:-86
a的unsigned char值是:169、b的unsigned char值是:254、((char)a - (char)b))值是:-85
a的unsigned char值是:170、b的unsigned char值是:254、((char)a - (char)b))值是:-84
a的unsigned char值是:171、b的unsigned char值是:254、((char)a - (char)b))值是:-83
a的unsigned char值是:172、b的unsigned char值是:254、((char)a - (char)b))值是:-82
a的unsigned char值是:173、b的unsigned char值是:254、((char)a - (char)b))值是:-81
a的unsigned char值是:174、b的unsigned char值是:254、((char)a - (char)b))值是:-80
a的unsigned char值是:175、b的unsigned char值是:254、((char)a - (char)b))值是:-79
a的unsigned char值是:176、b的unsigned char值是:254、((char)a - (char)b))值是:-78
a的unsigned char值是:177、b的unsigned char值是:254、((char)a - (char)b))值是:-77
a的unsigned char值是:178、b的unsigned char值是:254、((char)a - (char)b))值是:-76
a的unsigned char值是:179、b的unsigned char值是:254、((char)a - (char)b))值是:-75
a的unsigned char值是:180、b的unsigned char值是:254、((char)a - (char)b))值是:-74
a的unsigned char值是:181、b的unsigned char值是:254、((char)a - (char)b))值是:-73
a的unsigned char值是:182、b的unsigned char值是:254、((char)a - (char)b))值是:-72
a的unsigned char值是:183、b的unsigned char值是:254、((char)a - (char)b))值是:-71
a的unsigned char值是:184、b的unsigned char值是:254、((char)a - (char)b))值是:-70
a的unsigned char值是:185、b的unsigned char值是:254、((char)a - (char)b))值是:-69
a的unsigned char值是:186、b的unsigned char值是:254、((char)a - (char)b))值是:-68
a的unsigned char值是:187、b的unsigned char值是:254、((char)a - (char)b))值是:-67
a的unsigned char值是:188、b的unsigned char值是:254、((char)a - (char)b))值是:-66
a的unsigned char值是:189、b的unsigned char值是:254、((char)a - (char)b))值是:-65
a的unsigned char值是:190、b的unsigned char值是:254、((char)a - (char)b))值是:-64
a的unsigned char值是:191、b的unsigned char值是:254、((char)a - (char)b))值是:-63
a的unsigned char值是:192、b的unsigned char值是:254、((char)a - (char)b))值是:-62
a的unsigned char值是:193、b的unsigned char值是:254、((char)a - (char)b))值是:-61
a的unsigned char值是:194、b的unsigned char值是:254、((char)a - (char)b))值是:-60
a的unsigned char值是:195、b的unsigned char值是:254、((char)a - (char)b))值是:-59
a的unsigned char值是:196、b的unsigned char值是:254、((char)a - (char)b))值是:-58
a的unsigned char值是:197、b的unsigned char值是:254、((char)a - (char)b))值是:-57
a的unsigned char值是:198、b的unsigned char值是:254、((char)a - (char)b))值是:-56
a的unsigned char值是:199、b的unsigned char值是:254、((char)a - (char)b))值是:-55
a的unsigned char值是:200、b的unsigned char值是:254、((char)a - (char)b))值是:-54
a的unsigned char值是:201、b的unsigned char值是:254、((char)a - (char)b))值是:-53
a的unsigned char值是:202、b的unsigned char值是:254、((char)a - (char)b))值是:-52
a的unsigned char值是:203、b的unsigned char值是:254、((char)a - (char)b))值是:-51
a的unsigned char值是:204、b的unsigned char值是:254、((char)a - (char)b))值是:-50
a的unsigned char值是:205、b的unsigned char值是:254、((char)a - (char)b))值是:-49
a的unsigned char值是:206、b的unsigned char值是:254、((char)a - (char)b))值是:-48
a的unsigned char值是:207、b的unsigned char值是:254、((char)a - (char)b))值是:-47
a的unsigned char值是:208、b的unsigned char值是:254、((char)a - (char)b))值是:-46
a的unsigned char值是:209、b的unsigned char值是:254、((char)a - (char)b))值是:-45
a的unsigned char值是:210、b的unsigned char值是:254、((char)a - (char)b))值是:-44
a的unsigned char值是:211、b的unsigned char值是:254、((char)a - (char)b))值是:-43
a的unsigned char值是:212、b的unsigned char值是:254、((char)a - (char)b))值是:-42
a的unsigned char值是:213、b的unsigned char值是:254、((char)a - (char)b))值是:-41
a的unsigned char值是:214、b的unsigned char值是:254、((char)a - (char)b))值是:-40
a的unsigned char值是:215、b的unsigned char值是:254、((char)a - (char)b))值是:-39
a的unsigned char值是:216、b的unsigned char值是:254、((char)a - (char)b))值是:-38
a的unsigned char值是:217、b的unsigned char值是:254、((char)a - (char)b))值是:-37
a的unsigned char值是:218、b的unsigned char值是:254、((char)a - (char)b))值是:-36
a的unsigned char值是:219、b的unsigned char值是:254、((char)a - (char)b))值是:-35
a的unsigned char值是:220、b的unsigned char值是:254、((char)a - (char)b))值是:-34
a的unsigned char值是:221、b的unsigned char值是:254、((char)a - (char)b))值是:-33
a的unsigned char值是:222、b的unsigned char值是:254、((char)a - (char)b))值是:-32
a的unsigned char值是:223、b的unsigned char值是:254、((char)a - (char)b))值是:-31
a的unsigned char值是:224、b的unsigned char值是:254、((char)a - (char)b))值是:-30
a的unsigned char值是:225、b的unsigned char值是:254、((char)a - (char)b))值是:-29
a的unsigned char值是:226、b的unsigned char值是:254、((char)a - (char)b))值是:-28
a的unsigned char值是:227、b的unsigned char值是:254、((char)a - (char)b))值是:-27
a的unsigned char值是:228、b的unsigned char值是:254、((char)a - (char)b))值是:-26
a的unsigned char值是:229、b的unsigned char值是:254、((char)a - (char)b))值是:-25
a的unsigned char值是:230、b的unsigned char值是:254、((char)a - (char)b))值是:-24
a的unsigned char值是:231、b的unsigned char值是:254、((char)a - (char)b))值是:-23
a的unsigned char值是:232、b的unsigned char值是:254、((char)a - (char)b))值是:-22
a的unsigned char值是:233、b的unsigned char值是:254、((char)a - (char)b))值是:-21
a的unsigned char值是:234、b的unsigned char值是:254、((char)a - (char)b))值是:-20
a的unsigned char值是:235、b的unsigned char值是:254、((char)a - (char)b))值是:-19
a的unsigned char值是:236、b的unsigned char值是:254、((char)a - (char)b))值是:-18
a的unsigned char值是:237、b的unsigned char值是:254、((char)a - (char)b))值是:-17
a的unsigned char值是:238、b的unsigned char值是:254、((char)a - (char)b))值是:-16
a的unsigned char值是:239、b的unsigned char值是:254、((char)a - (char)b))值是:-15
a的unsigned char值是:240、b的unsigned char值是:254、((char)a - (char)b))值是:-14
a的unsigned char值是:241、b的unsigned char值是:254、((char)a - (char)b))值是:-13
a的unsigned char值是:242、b的unsigned char值是:254、((char)a - (char)b))值是:-12
a的unsigned char值是:243、b的unsigned char值是:254、((char)a - (char)b))值是:-11
a的unsigned char值是:244、b的unsigned char值是:254、((char)a - (char)b))值是:-10
a的unsigned char值是:245、b的unsigned char值是:254、((char)a - (char)b))值是:-9
a的unsigned char值是:246、b的unsigned char值是:254、((char)a - (char)b))值是:-8
a的unsigned char值是:247、b的unsigned char值是:254、((char)a - (char)b))值是:-7
a的unsigned char值是:248、b的unsigned char值是:254、((char)a - (char)b))值是:-6
a的unsigned char值是:249、b的unsigned char值是:254、((char)a - (char)b))值是:-5
a的unsigned char值是:250、b的unsigned char值是:254、((char)a - (char)b))值是:-4
a的unsigned char值是:251、b的unsigned char值是:254、((char)a - (char)b))值是:-3
a的unsigned char值是:252、b的unsigned char值是:254、((char)a - (char)b))值是:-2
a的unsigned char值是:253、b的unsigned char值是:254、((char)a - (char)b))值是:-1
a的unsigned char值是:254、b的unsigned char值是:254、((char)a - (char)b))值是:0
请按任意键继续. . .
可以看出这样是可以解决计数溢出的问题
MultiTimer.h
/*
* Copyright (c) 2016 Zibin Zheng <znbin@qq.com>
* All rights reserved
*/
#ifndef _MULTI_TIMER_H_
#define _MULTI_TIMER_H_
//#include <stdint.h>
#include <stddef.h>
#define DEFINE_TIMER_BASE_TICK 1
#define COMPARE(a,b) ((int)(a)-(int)(b))
typedef enum
{
TIMER1_TIME=1000,
TIMER2_TIME=50,
TIMER_ALWAYS_LOOP=1,
TIMER_ONE_LOOP=0
}Enum_Timer;
typedef unsigned int uint32_t;
typedef struct Timer {
uint32_t tick_overflow; /* 定义备用 */
uint32_t cur_ticks; /* Record current timer start tick */
uint32_t cur_expired_time; /* Record current timer expired time */
uint32_t timeout; /* Delay (xx ms) time to start tiemr */
uint32_t repeat; /* Timer interval expired time (xx ms) */
void * arg; /* Input argument for timeout_cb function */
void (*timeout_cb)(void *arg); /* Timer expired callback function */
struct Timer* next; /* Pointer to next timer */
} Struct_Timer;
#ifdef __cplusplus
extern "C" {
#endif
void TimerInit(struct Timer* handle, void (*timeout_cb)(void *arg), Enum_Timer timeout, Enum_Timer repeat, void *arg);
int TimerStart(struct Timer* handle);
int TimerStop(struct Timer* handle);
void TimerTicksCount(void);
void TimerLoop(void);
#ifdef __cplusplus
}
#endif
#endif
我将任务结构体参数改为了枚举类型,不用多说了