100行代码简单小巧的软件定时器
直接用,除了变量名冲突,不需要移植。
#include “softTimer.h”
//链表首地址
static softTimer_t *softTmerHead = 0;
//函数功能:创建一个定时器
//参数:1 - 定时器结构体地址,2 - 定时中断回调函数地址(定时器计数溢出会调用该函数)
void softTimerCreat(softTimer_t *softTimer, void (*callback)(void))
{
softTimer_t *point = softTmerHead;
if(softTimer == 0 || callback == 0) return;
//空链表,给头指针赋值
if(softTmerHead == 0)
{
softTmerHead = softTimer;
softTmerHead->callback = callback;
softTmerHead->next = 0;
}
else
{
while(point != 0)
{
//已存在该定时器
if(point == softTimer) break;
//在链表末尾添加新的定时器
else if(point->next == 0)
{
point->next = softTimer;
point = point->next;
point->callback = callback;
point->next = 0;
break;
}
point = point->next;
}
}
}
//函数功能:定时器启动
//参数:1 - 需要启动的定时器,2 - 定时时间(>=1单位ms,输入0关闭定时器)
void softTimerStart(softTimer_t *softTimer,unsigned int setTime)
{
softTimer_t *point = softTmerHead;
if(softTimer == 0) return;
while(point != 0)
{
//找到目标
if(point == softTimer)
{
point->setTime = setTime;
point->count = 0;
break;
}
point = point->next;
}
}
//函数功能:定时器停止
//参数:需要关闭的定时器
void softTimerStop(softTimer_t *softTimer)
{
softTimerStart(softTimer,0);
}
//函数功能:内部实现函数,放在1ms的定时中断中
void timerControlIntv1ms(void)
{
softTimer_t *point = softTmerHead;
//遍历所有定时器
while(point != 0)
{
//该定时器在运行
if(point->setTime != 0)
{
point->count ++;
//定时器触发,调用回调函数
if(point->count >= point->setTime)
{
point->count = 0;
if(point->callback != 0)
point->callback();
}
}
point = point->next;
}
}
//链表首地址
static softTimer_t *softTmerHead = 0;
//函数功能:创建一个定时器
//参数:1 - 定时器结构体地址,2 - 定时中断回调函数地址(定时器计数溢出会调用该函数)
void softTimerCreat(softTimer_t *softTimer, void (*callback)(void))
{
softTimer_t *point = softTmerHead;
if(softTimer == 0 || callback == 0) return;
//空链表,给头指针赋值
if(softTmerHead == 0)
{
softTmerHead = softTimer;
softTmerHead->callback = callback;
softTmerHead->next = 0;
}
else
{
while(point != 0)
{
//已存在该定时器
if(point == softTimer) break;
//在链表末尾添加新的定时器
else if(point->next == 0)
{
point->next = softTimer;
point = point->next;
point->callback = callback;
point->next = 0;
break;
}
point = point->next;
}
}
}
//函数功能:定时器启动
//参数:1 - 需要启动的定时器,2 - 定时时间(>=1单位ms,输入0关闭定时器)
void softTimerStart(softTimer_t *softTimer,unsigned int setTime)
{
softTimer_t *point = softTmerHead;
if(softTimer == 0) return;
while(point != 0)
{
//找到目标
if(point == softTimer)
{
point->setTime = setTime;
point->count = 0;
break;
}
point = point->next;
}
}
//函数功能:定时器停止
//参数:需要关闭的定时器
void softTimerStop(softTimer_t *softTimer)
{
softTimerStart(softTimer,0);
}
//函数功能:内部实现函数,放在1ms的定时中断中
void timerControlIntv1ms(void)
{
softTimer_t *point = softTmerHead;
//遍历所有定时器
while(point != 0)
{
//该定时器在运行
if(point->setTime != 0)
{
point->count ++;
//定时器触发,调用回调函数
if(point->count >= point->setTime)
{
point->count = 0;
if(point->callback != 0)
point->callback();
}
}
point = point->next;
}
}`
//softTimer.c文件代码
/*************************************************************************/
/*********************************************************************/
//softTimer.h文件代码
/*********************************************************************/
#ifndef _SOFT_TIMER_H
#define _SOFT_TIMER_H
struct softTimer
{
unsigned int setTime;
unsigned int count;
void (*callback)(void);
struct softTimer *next;
};
typedef struct softTimer softTimer_t;
void softTimerCreat(softTimer_t *softTimer, void (*callback)(void));
void softTimerStart(softTimer_t *softTimer,unsigned int setTime);
void softTimerStop(softTimer_t *softTimer);
void softTimerControlIntv1ms(void);
#endif
/*****************************************************************/
//使用示例main.c
/*****************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include “softTimer.h”
softTimer_t timer1,timer2;
//timer1回调函数
void timer1CallBack(void)
{
printf(“timer1 CallBack \r\n”);
}
//timer2回调函数
void timer2CallBack(void)
{
printf(“timer2 CallBack\r\n”);
}
int main(int argc, char *argv[])
{
static int times = 0;
//创建定时器
softTimerCreat(&timer1,timer1CallBack);
softTimerCreat(&timer2,timer2CallBack);
//启动定时器 ,分别定时1s,2s
softTimerStart(&timer1,1000);
softTimerStart(&timer2,2000);
while(1)
{
//内部实现函数放在1ms定时中断中,这里用循环代替
timerControlIntv1ms();
//延时1ms,代替1ms中断
Sleep(1);
times ++;
//5s后关闭timer1
if(times == 5 * 1000)
{
softTimerStop(&timer1);
}
//8s后关闭timer2,并结束程序
else if(times == 8 * 1000)
{
softTimerStop(&timer2);
break;
}
}
system(“PAUSE”);
return 0;
}
/****************************************************************************************/
运行结果