软件时钟
#include "global.h" //时基函数结构体 typedef struct { uint16_t DelayTime; uint16_t TimeBackup; uint16_t Init; }TIMER_T; static volatile uint16_t u16Day; //日 static volatile uint16_t u16Hour; //时 static volatile uint16_t u16Minute; //分 static volatile uint16_t u16S; //秒 static volatile uint16_t u16Ms; //毫秒 /******************************************************************************* #Function : DelayAdd #Description : 计算经过的时间 #Parameter : NowTime 当前时间 *Time 时基结构体 #Return : NULL #AuthorAndData : huangzhigang 20140716 *******************************************************************************/ static void DelayAdd(uint16_t NowTime,TIMER_T *Time) { if(NowTime > (Time->TimeBackup)) { uint16_t Temp = NowTime - Time->TimeBackup; if((0xffff - Time->DelayTime) >= Temp) { Time->DelayTime += Temp; } } Time->TimeBackup = NowTime; } void TimeH(TIMER_T *Time) //以小时为单位 添加计时任务 { if(!Time->Init)//起始时间初始化 { Time->Init = 1; Time->TimeBackup = u16Hour; } else { DelayAdd(u16Hour,Time); } } void TimeM(TIMER_T *Time) //以分钟为单位 添加计时任务 { if(!Time->Init)//起始时间初始化 { Time->Init = 1; Time->TimeBackup = u16Minute; } else { DelayAdd(u16Minute,Time); } } void TimeS(TIMER_T *Time) //以秒为单位 添加计时任务 { if(!Time->Init)//起始时间初始化 { Time->Init = 1; Time->TimeBackup = u16S; } else { DelayAdd(u16S,Time); } } void TimeMs(TIMER_T *Time) //以毫秒为单位 添加计时任务 { if(!Time->Init)//起始时间初始化 { Time->Init = 1; Time->TimeBackup = u16Ms; } else { DelayAdd(u16Ms,Time); } } /******************************************************************************* #Function : Interrup_Time #Description : 中断中调用的函数 1ms调用一次 记录时钟的运行 #Parameter : NULL #Return : NULL #AuthorAndData : huangzhigang 20140716 *******************************************************************************/ void Interrup_Time(void) { if(!(++u16Ms%1000)) { if(!(++u16S%60)) { u16Ms = 0;//60000ms后清毫秒累加器 if(!(++u16Minute%60)) { if(!(++u16Hour%24)) { ++u16Day; } if(u16S >= 61200){u16S = 0;}//17小时后清秒累加器 } } } }
原文地址:http://blog.csdn.net/a656343072/article/details/38845427