標(biāo)題: 一種基于系統(tǒng)節(jié)拍時(shí)間的單片機(jī)程序設(shè)計(jì)模式(節(jié)拍時(shí)間的產(chǎn)生-02) [打印本頁]

作者: 水漫金山2017    時(shí)間: 2019-12-3 08:59
標(biāo)題: 一種基于系統(tǒng)節(jié)拍時(shí)間的單片機(jī)程序設(shè)計(jì)模式(節(jié)拍時(shí)間的產(chǎn)生-02)
系統(tǒng)節(jié)拍時(shí)間,由單片機(jī)中的定時(shí)中斷產(chǎn)生。我們先構(gòu)造如下數(shù)據(jù)結(jié)構(gòu):

          typedef struct {
         volatile uchar Tick;          //這個(gè)就是系統(tǒng)節(jié)拍時(shí)間
         uchar Cnt20ms;              //20ms掃描一次的任務(wù),比如按鍵,等等
         uchar Cnt100ms;            //100ms掃描一次的任務(wù),比如顯示數(shù)據(jù)刷新,  等等      
         uchar Cnt500ms;           //RTC時(shí)間數(shù)據(jù),等等
         uchar Cnt2000ms;         //其它等等         //當(dāng)然了,這里還可以添加其它周期
    }CLOCK_t;
    extern CLOCK_t Clock;
在定時(shí)中斷服務(wù)程序中,我們這樣寫:
  
void TimeX_ISR(void)
{
    uchar Tick = Clock.Tick;
    //定時(shí)器重裝初值
    TMR1H = (uint08)(PRE_VALUE >> 8);         //這里假定系統(tǒng)節(jié)拍時(shí)間為4ms   
    TMR1L = (uint08)(PRE_VALUE);       
   
        if (Tick < 0xFF)
        {
                Clock.Tick = Tick+1;        
        }
}

然后,在主程序中,我們就可以使用這個(gè)系統(tǒng)節(jié)拍了
void main(void)
{
       //關(guān)全局中斷
        //系統(tǒng)節(jié)拍定時(shí)器初始化
       TimeX_Init();
       other_Inint();
       //開全局中斷


      for(;;)
     {
             if(Clock.Tick )
            {
                  //關(guān)全局中斷
                  Clock.Tick--;
                  //開全局中斷


                 //按節(jié)拍分時(shí)處理各種任務(wù)
                  
            }
     }
}
因?yàn)?/font>系統(tǒng)節(jié)拍時(shí)間的產(chǎn)生確實(shí)一件沒有任何爭議的必須實(shí)現(xiàn),所以就直接上了代碼展示,讓人更清晰。






歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1