BLE 協議棧之RTC時鐘

BLE協議棧裏有已經封裝好的RTC時鐘,時鐘源可以是外部晶體振盪時鐘也可以是內部RC時鐘。

The32-kHzXOSC is designed to operate at 32.768kHz and provide a stable clock signal for systems requiring time accuracy.The 32-kHz RCOS runs at32.753kHz when  calibrated.

外部晶振更加精準一些!

主要的函數在OSAL_ClockBLE.c中;

[cpp] view plain copy
  1. void osalTimeUpdate( void )  
  2. {  
  3.   uint16 tmp;      //爲暫存變量,用於臨時存放時間值   
  4.   uint16 ticks625us;  //用於存放timer2的溢出次數,每次溢出爲625us,也就是說ticks625us代表了625us的個數  
  5.   uint16 elapsedMSec = 0;//也是用來存放時間值的,只是它存放的值是上一次操作所保留下來的值,它最終存放的是時間的ms值  
  6.   
  7.   
  8.   // Get the free-running count of 625us timer ticks  
  9.   tmp = ll_McuPrecisionCount();//這個函數就是用來讀取timer2溢出次數的,溢出次數存放在T2MOVF2([23:16]),T2MOVF2([15:8]), T2MOVF0([7:0]), 一共有24bit  
  10.   
  11.   if ( tmp != previousLLTimerTick )//判斷時間是否有變化(正常情況下,隨着程序的運行,tmp 這個值一直增大(16-bit))  
  12.   {  
  13.     // Calculate the elapsed ticks of the free-running timer.  
  14.     ticks625us = tmp - previousLLTimerTick;//當前的時間值減去上一次的值,也就是代碼再一次運行到這裏所消耗的時間(注意單位爲625us)  
  15.   
  16.     // Store the LL Timer tick count for the next time through this function.  
  17.     previousLLTimerTick = tmp;//將當前值存儲起來,爲程序下一次運行到這裏作準備  
  18.   /* 
  19.   下面紅色部分的代碼,都只是實現了一個功能,就是將ticks625us個625us 轉換成 elapsedMSec 個ms(ms的整數部分)和remUsTicks 個us(ms的小數部分),“ *5 / 8” 等價於 “ *625 / 1000”(分子分母同時擴大40倍),同理“ *5 % 8” 等價於 “*625 % 1000”  至於MAXCALCTICKS ,程序中有一下兩段註釋,可以幫助我們理解  
  20.   // (MAXCALCTICKS * 5) + (max remainder) must be <= (uint16 max), 
  21.   // so: (13105 * 5) + 7 <= 65535 
  22.   #define MAXCALCTICKS  ((uint16)(13105)) 
  23.    
  24.   */  
  25.   
  26.   
  27.     /* It is necessary to loop to convert the usecs to msecs in increments so as 
  28.      * not to overflow the 16-bit variables. 
  29.      */  
  30.     while ( ticks625us > MAXCALCTICKS )//這個while是對溢出的操作  
  31.     {  
  32.       ticks625us -= MAXCALCTICKS;  
  33.       elapsedMSec += MAXCALCTICKS * 5 / 8;  
  34.       remUsTicks += MAXCALCTICKS * 5 % 8;  
  35.     }  
  36.   
  37.     // update converted number with remaining ticks from loop and the  
  38.     // accumulated remainder from loop  
  39.     tmp = (ticks625us * 5) + remUsTicks;  
  40.   
  41.     // Convert the 625 us ticks into milliseconds and a remainder  
  42.     elapsedMSec += tmp / 8;  
  43.     remUsTicks = tmp % 8;  
  44.   
  45.     // Update OSAL Clock and Timers  
  46.     if ( elapsedMSec )//判斷時間是否到了1ms,如果等於或者超過1ms(elapsedMSec  >= 1),則需要輪詢任務列表  
  47.     {  
  48.       osalClockUpdate( elapsedMSec );//更新osal操作系統的系統時間  
  49.       osalTimerUpdate( elapsedMSec );//這個函數和任務的輪詢調度有關,即和timeout有關  
  50.     }  
  51.   }  
  52. }  


當然還有設置時間函數(UTCTime是從2000年1月1日00:00:00到至今的秒數)

[cpp] view plain copy
  1. /********************************************************************* 
  2.  * @fn      osal_setClock 
  3.  * 
  4.  * @brief   Set the new time.  This will only set the seconds portion 
  5.  *          of time and doesn't change the factional second counter. 
  6.  * 
  7.  * @param   newTime - number of seconds since 0 hrs, 0 minutes, 
  8.  *                    0 seconds, on the 1st of January 2000 UTC 
  9.  * 
  10.  * @return  none 
  11.  */  
  12. void osal_setClock( UTCTime newTime )  
  13. {  
  14.   OSAL_timeSeconds = newTime;  
  15. }  


獲得當前時間函數:

[cpp] view plain copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. /********************************************************************* 
  2.  * @fn      osal_getClock 
  3.  * 
  4.  * @brief   Gets the current time.  This will only return the seconds 
  5.  *          portion of time and doesn't include the factional second 
  6.  *          counter. 
  7.  * 
  8.  * @param   none 
  9.  * 
  10.  * @return  number of seconds since 0 hrs, 0 minutes, 0 seconds, 
  11.  *          on the 1st of January 2000 UTC 
  12.  */  
  13. UTCTime osal_getClock( void )  
  14. {  
  15.   return ( OSAL_timeSeconds );  
  16. }  

 

 

將秒數轉換爲年月日時分秒格式儲存在結構體UTCTimeStruct(定義在OSAL_Clock.h)中

[cpp] view plain copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. /********************************************************************* 
  2.  * @fn      osal_ConvertUTCTime 
  3.  * 
  4.  * @brief   Converts UTCTime to UTCTimeStruct 
  5.  * 
  6.  * @param   tm - pointer to breakdown struct 
  7.  * 
  8.  * @param   secTime - number of seconds since 0 hrs, 0 minutes, 
  9.  *          0 seconds, on the 1st of January 2000 UTC 
  10.  * 
  11.  * @return  none 
  12.  */  
  13. void osal_ConvertUTCTime( UTCTimeStruct *tm, UTCTime secTime )  
  14. {  
  15.   // calculate the time less than a day - hours, minutes, seconds  
  16.   {  
  17.     uint32 day = secTime % DAY;  
  18.     tm->seconds = day % 60UL;  
  19.     tm->minutes = (day % 3600UL) / 60UL;  
  20.     tm->hour = day / 3600UL;  
  21.   }  
  22.   
  23.   // Fill in the calendar - day, month, year  
  24.   {  
  25.     uint16 numDays = secTime / DAY;  
  26.     tm->year = BEGYEAR;  
  27.     while ( numDays >= YearLength( tm->year ) )  
  28.     {  
  29.       numDays -= YearLength( tm->year );  
  30.       tm->year++;  
  31.     }  
  32.   
  33.     tm->month = 0;  
  34.     while ( numDays >= monthLength( IsLeapYear( tm->year ), tm->month ) )  
  35.     {  
  36.       numDays -= monthLength( IsLeapYear( tm->year ), tm->month );  
  37.       tm->month++;  
  38.     }  
  39.   
  40.     tm->day = numDays;  
  41.   }  
  42. }  

 

 

校準時間時將年月日時分秒格式轉換爲秒的函數;

[cpp] view plain copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. /********************************************************************* 
  2.  * @fn      osal_ConvertUTCSecs 
  3.  * 
  4.  * @brief   Converts a UTCTimeStruct to UTCTime 
  5.  * 
  6.  * @param   tm - pointer to provided struct 
  7.  * 
  8.  * @return  number of seconds since 00:00:00 on 01/01/2000 (UTC) 
  9.  */  
  10. UTCTime osal_ConvertUTCSecs( UTCTimeStruct *tm )  
  11. {  
  12.   uint32 seconds;  
  13.   
  14.   /* Seconds for the partial day */  
  15.   seconds = (((tm->hour * 60UL) + tm->minutes) * 60UL) + tm->seconds;  
  16.   
  17.   /* Account for previous complete days */  
  18.   {  
  19.     /* Start with complete days in current month */  
  20.     uint16 days = tm->day;  
  21.   
  22.     /* Next, complete months in current year */  
  23.     {  
  24.       int8 month = tm->month;  
  25.       while ( --month >= 0 )  
  26.       {  
  27.         days += monthLength( IsLeapYear( tm->year ), month );  
  28.       }  
  29.     }  
  30.   
  31.     /* Next, complete years before current year */  
  32.     {  
  33.       uint16 year = tm->year;  
  34.       while ( --year >= BEGYEAR )  
  35.       {  
  36.         days += YearLength( year );  
  37.       }  
  38.     }  
  39.   
  40.     /* Add total seconds before partial day */  
  41.     seconds += (days * DAY);  
  42.   }  
  43.   
  44.   return ( seconds );  
  45. }  

example:

[cpp] view plain copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. har tempbuf[10]={0};  
  2.  UTCTimeStruct *Ti;  
  3.    
  4.  osalTimeUpdate();    
  5.  osal_ConvertUTCTime(Ti,osal_getClock());  
  6.   tempbuf[0]=(Ti->hour)/10+'0';  
  7.  tempbuf[1]=(Ti->hour)%10+'0';  
  8.  tempbuf[2]=(Ti->minutes)/10+'0';  
  9.  tempbuf[3]=(Ti->minutes)%10+'0';  
  10.  tempbuf[4]=(Ti->seconds)/10+'0';  
  11.  tempbuf[5]=(Ti->seconds)%10+'0';  

  1.  Uart0Send_String(tempbuf,10);// 將時分秒在串口打印   

http://blog.csdn.net/xiaoleiacmer/article/details/42458351
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章