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 at 32.753kHz when calibrated.
外部晶振更加精准一些!
主要的函数在OSAL_ClockBLE.c中;
void osalTimeUpdate( void )
{
uint16 tmp; //为暂存变量,用于临时存放时间值
uint16 ticks625us; //用于存放timer2的溢出次数,每次溢出为625us,也就是说ticks625us代表了625us的个数
uint16 elapsedMSec = 0;//也是用来存放时间值的,只是它存放的值是上一次操作所保留下来的值,它最终存放的是时间的ms值
// Get the free-running count of 625us timer ticks
tmp = ll_McuPrecisionCount();//这个函数就是用来读取timer2溢出次数的,溢出次数存放在T2MOVF2([23:16]),T2MOVF2([15:8]), T2MOVF0([7:0]), 一共有24bit
if ( tmp != previousLLTimerTick )//判断时间是否有变化(正常情况下,随着程序的运行,tmp 这个值一直增大(16-bit))
{
// Calculate the elapsed ticks of the free-running timer.
ticks625us = tmp - previousLLTimerTick;//当前的时间值减去上一次的值,也就是代码再一次运行到这里所消耗的时间(注意单位为625us)
// Store the LL Timer tick count for the next time through this function.
previousLLTimerTick = tmp;//将当前值存储起来,为程序下一次运行到这里作准备
/*
下面红色部分的代码,都只是实现了一个功能,就是将ticks625us个625us 转换成 elapsedMSec 个ms(ms的整数部分)和remUsTicks 个us(ms的小数部分