GithubHelp home page GithubHelp logo

Comments (3)

cancundiudiu avatar cancundiudiu commented on June 25, 2024

from multitimer.

askhua520 avatar askhua520 commented on June 25, 2024

我一般这样用
#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);
}
}

from multitimer.

Related Issues (12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.