MultiTimer 软件定时器扩展模块的研究与优化【原创】

news2024/11/25 6:28:20

文章目录

  • 前言
  • 收获
  • 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

我将任务结构体参数改为了枚举类型,不用多说了

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/701702.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

MathType公式批量转换为Word自带公式的VBA实现及error ‘4198‘ 处理

VBA 纯新手&#xff0c;程序可能比较傻&#xff0c;大家将就看。有改进建议&#xff0c;欢迎留言 参考链接&#xff1a; 1、2种办法把MathType公式转换为Word公式 2、一键批量将mathtype公式转换成word自带公式-vba/vbnet_mathtype批量转word自带_一路向前的小Q的博客-CSDN博…

如何保证缓存与数据库的双写一致性?

分析&#xff1f; 你只要用缓存&#xff0c;就可能会涉及到缓存与数据库双存储双写&#xff0c;你只要是双写&#xff0c;就一定会有数据一 致性的问题&#xff0c;那么你如何解决一致性问题&#xff1f; 一般来说&#xff0c;如果允许缓存可以稍微的跟数据库偶尔有不一致的情…

合同数智化如何助力地产企业实现变革“突围”?

从稳步发展到求新求变&#xff0c; 数智化成破局关键 近年来&#xff0c;随着宏观经济政策调整&#xff0c;在中央房住不炒的大基调下&#xff0c;房地产逐步回归居住属性。在这样的大背景下&#xff0c;针对不同类型的房地产企业&#xff0c;国家出台了不同的数字化转型指导文…

【Bio】头骨组成,及其剖切面

在总结这篇文章之前&#xff0c;我看过了一本书《认知觉醒》&#xff0c;里边有提到一个观点&#xff1a;我们不仅要去获取新知识&#xff0c;也要注重对新知识的“缝接”&#xff0c;这个过程也就是关联。这样&#xff0c;如沙粒般的新知识才能关联到一起&#xff0c;达到聚沙…

go get google.golang.org/grpc报错

win10环境&#xff0c;报错完整内容如下 go get google.golang.org/grpc: module google.golang.org/grpc: Get https://proxy.golang.org/google.golang.org/grpc/v/list: dial tcp [2404:6800:4012:3::2011]:443: connectex: A connection attempt failed because the conne…

Spring 的依赖注入(DI)

前言 欢迎来到本篇文章&#xff0c;书接上回&#xff0c;本篇说说 Spring 中的依赖注入&#xff0c;包括注入的方式&#xff0c;写法&#xff0c;该选择哪个注入方式以及可能出现的循环依赖问题等内容。 如果正在阅读的朋友还不清楚什么是「依赖」&#xff0c;建议先看看我第一…

34岁上岸,我终于圆了自己的考研梦

​ 大家好&#xff0c;我是独孤风&#xff0c;一位曾经的港口煤炭工人&#xff0c;目前在某国企任大数据负责人&#xff0c;公众号大数据流动的作者。 ​ 虽然告诉自己要平静&#xff0c;但是当接到EMS录取通知书的那一刻&#xff0c;眼眶还是忍不住有些湿润。今年正好是是东北…

SpringBoot源码分析(1)--@SpringBootApplication注解使用和原理/SpringBoot的自动配置原理详解

文章目录 前言主启动类的配置1、SpringBootApplication注解1.1、SpringBootConfiguration注解验证启动类是否被注入到spring容器中 1.2、ComponentScan 注解ComponentScan 注解解析与路径扫描 1.3、EnableAutoConfiguration注解1.3.1、AutoConfigurationPackage注解1.3.2、Impo…

【MySQL】事务及其隔离性/隔离级别

目录 一、事务的概念 1、事务的四种特性 2、事务的作用 3、存储引擎对事务的支持 4、事务的提交方式 二、事务的启动、回滚与提交 1、准备工作&#xff1a;调整MySQL的默认隔离级别为最低/创建测试表 2、事务的启动、回滚与提交 3、启动事务后未commit&#xff0c;但是…

HTB-Pilgrimage

HTB-Pilgrimage 信息收集80端口立足emily -> root 信息收集 80端口 扫描目录发现存在.git。 通过scrabble获取网站的git文件。 有如下这些文件。 在index.php中使用了magick来处理图像。 正好我们靠git弄了一个&#xff0c;查看一下版本。 这个版本似乎有些不得了的东西…

Quiz 9: Dictionaries | Python for Everybody 配套练习_解题记录

文章目录 课程简介Quiz 9: Dictionaries 单选题&#xff08;1-11&#xff09;编程题Exercise 9.4 课程简介 Python for Everybody 零基础程序设计&#xff08;Python 入门&#xff09; This course aims to teach everyone the basics of programming computers using Python.…

conda的多线程下载工具mamba(解决Anaconda3 solving environment 巨慢的方法)

solving environment为什么会越来越慢&#xff1f; 根据原博的解释以及我查阅的相关资料&#xff0c;这是由于conda在新安装一个包或者更新包时需要搜索当前环境中所有的包的依赖空间&#xff0c;以找到满足所有依赖项的版本&#xff0c;随着用户安装的包越来越多&#xff0c;…

C#核心知识回顾——1.结构体、构造函数、GC、成员属性、索引器

1.结构体&#xff1a; 在 C# 中&#xff0c;结构体是值类型数据结构。它使得一个单一变量可以存储各种数据类型的相关数据。例如我定义了一个结构体&#xff0c;它有两个变量&#xff0c;创建一个这个类型的结构体&#xff0c;通过一个变量名调用多个变量&#xff0c;这些变量可…

Layui时间范围选择器,添加【本周、本月、本季度、本年等常用时间快捷键】

文章目录 1. 界面实现2. JS具体实现2.1 第一种实现2.2 第二种实现 1. 界面实现 <input id"Date_select" type"text" class"form-control" placeholder"请选择时间范围" style"border-radius: 4px;" /><input id&qu…

RuoYi-Vue Swagger 上传文件接口

前言 RuoYi-Vue&#xff1a; v3.8.5swagger 1.6.2 &#xff08;https://github.com/swagger-api/swagger-core, https://gitee.com/mirrors/swagger-core&#xff09; Swagger 上传接口定义 ApiOperation(value "图片上传") PostMapping(value "/upload&qu…

SpringBoo集成MongoDB

一、集成简介 spring-data-mongodb提供了MongoTemplate与MongoRepository两种方式访问mongodb&#xff0c;MongoRepository操作简单&#xff0c;MongoTemplate操作灵活&#xff0c;我们在项目中可以灵活适用这两种方式操作mongodb&#xff0c;MongoRepository的缺点是不够灵活…

OpenMMLab-AI实战营第二期——相关3. RGB语义分割标注图像转为Gray格式的mask

文章目录 1. 转换代码1.1 查看原始图像1.2 转换1.3 cv::IMREAD_GRAYSCALE与CV_BGR2GRAY结果不一致1.3.1 现象描述1.3.2 原因1.3.3 推荐做法 1.4 CV_BGR2GRAY和CV_RGB2GRAY不一致 2. macOS上查看mask&#xff08;使用默认的预览&#xff09; 1. 转换代码 找到了一个语义分割的数…

rc表格卡方检验

一、案例介绍 某医院用三种穴位针刺治疗急性腰扭伤&#xff0c;现在想比较三种穴位针刺效果有无差别&#xff0c;结果汇总如下表&#xff1a; 二、问题分析 本案例想比较三种穴位针刺效果有无差别&#xff0c;可以使用RxC卡方检验进行分析。 通常情况下&#xff0c;共有三种…

uniapp项目 封装一个饼图组件 并且修改显示项的排列方式

需求如下: 真实数据渲染后的完成效果如下: 记录一下代码: <template><view><view style"height: 600rpx;"><l-echart ref"chart" finished"init"></l-echart></view></view> </template><…

【面试】一文知晓---拦截器和过滤器的区别

目录 背景关系图 拦截器和过滤器的区别实操1.过滤器1.1HttpServletRequestWrapper1.2 OncePerRequestFilter1.3 配置 2.拦截器2.1登录拦截2.2配置 3.监听器 三、注意1.静态资源问题2.登录拦截ajax重定向 总结 背景 关系图 然后具体执行流程如下&#xff1a; 拦截器和过滤器的区…