軟體時鐘--使用定時器定時類比時鐘

來源:互聯網
上載者:User

標籤:軟體時間   單片機時基   定時器時鐘   

軟體時鐘

 

#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小時後清秒累加器       }    }  }}



軟體時鐘--使用定時器定時類比時鐘

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.