Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

为什么重复执行的定时器不在MultiTimerYield执行重新启动,在回调函数容易漏掉 #10

Open
askhua520 opened this issue Jan 13, 2022 · 3 comments

Comments

@askhua520
Copy link

为什么重复执行的定时器不在MultiTimerYield执行重新启动,在回调函数容易漏掉
增加排序功能很好,但是去掉了重复执行功能啊,单片机又很多都是要重复执行的定时器
增加可重复执行功能
struct MultiTimerHandle {
MultiTimer* next;
uint64_t deadline;
MultiTimerCallback_t callback;
void* userData;
uint64_t interval; //记录间隔
uint8_t repeat; //重复执行标记
};
//根据repeat标志重新启动定时器
if (entry->repeat) {
MultiTimerStart(entry, entry->interval, entry->callback, entry->userData, entry->repeat)
}

@cancundiudiu
Copy link

cancundiudiu commented Jan 13, 2022 via email

@askhua520
Copy link
Author

我一般这样用
#ifndef TIMER_H
#define TIMER_H

#include "NewType.h"

#define tType uint16
enum TimerOut { TIMERRST = 0, TIMEROUT = 1 };
typedef struct TIMER {
uint8 TimeOut;
tType Value;
} Timer, *pTimer;

extern void TimerStart(pTimer t, tType time);
extern void TimerStop(pTimer t);
extern void TimerRun(pTimer t);
extern uint8 TimerOut(pTimer t);

#endif

#include "Timer.h"

//启动已经停止的定时器,如果定时器已经启动或时间到则忽略.
void TimerStart(pTimer t, tType time) {
if ((t->Value == 0) && (t->TimeOut == TIMERRST)) {
t->Value = time;
}
}
//重新设置定时时间并启动定时器,不管时间是否到或定时器是否启动
void TimerSet(pTimer t, tType time) {
t->Value = time;
t->TimeOut = TIMERRST;
}
//停止定时器,清除时间到标志
void TimerStop(pTimer t) {
t->Value = 0;
t->TimeOut = TIMERRST;
}
//在1ms定时器内递减,时间到时设置标志为1
void TimerRun(pTimer t) {
if (t->Value > 0) {
t->Value--;
if (t->Value == 0) {
t->TimeOut = TIMEROUT;
}
}
}
//定时器时间到,并清除时间到标志为0
uint8 TimerOut(pTimer t) {
uint8 result = t->TimeOut;
if (t->TimeOut == TIMEROUT) {
t->TimeOut = TIMERRST;
}
return result;
}

//使用
void Tick1ms(void){
TimerRun(&tm1);
TimerRun(&tm2);
TimerRun(&tm3);
}

while(1){
if ( setp == 1){
if (...){
TimerStart(&tm1, 1000);
} else {
TimerStop(&tm1);
}
}
......
if (TimerOut(&tm1)) {
......
TimerStart(&tm2, 1000);
}
if (TimerOut(&tm2)) {
......
TimerStart(&tm3, 1000);
}
}

@kaidegit
Copy link

kaidegit commented Jun 4, 2024

这个必须在回调里重启的设计对回调用匿名函数很不友好。。。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants