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
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值