標籤:軟體時間 單片機時基 定時器時鐘
軟體時鐘
#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小時後清秒累加器 } } }}
軟體時鐘--使用定時器定時類比時鐘