|
对RTC进行矫正,使用1PPS信号做时间基准, 测试 32.768K时钟的频率误差, 根据误差值对RTC进行调整。
基本代码如下 :
void RtcTempAdjust(int16_t temp)
{
第一步 根据温度计算RTC查表调整值
// according to the temperature to adjust the offset , inorder to get precise time .
// Get the abs differ between current temp and CRYSTAL_NOMINAL_TEMP.
QuotRemain qr;
if(temp >250)
qr.quotient = temp - CRYSTAL_CENTER_TEMP*10;
else
qr.quotient = CRYSTAL_CENTER_TEMP*10 -temp;
qr.remainder = 10 ;
HardDiv(&qr);
temp = (int16_t)qr.quotient ;
if (qr.remainder >5)
temp++ ;
// Checking value . should not bigger than 50
if (temp > CRYSTAL_COMPENSATION_TABLE_LENGTH - 1)
{
temp = CRYSTAL_COMPENSATION_TABLE_LENGTH - 1;
}
// Get additional accumulator value from lookup table using temperature value
// It is assumed that the temperature was the same from now to the next time of doing adjust .
temp = (crystalCompensationTable[temp] );
获取查表校准值结束, 校准值经过确认 没有问题。
第二步 根据校准值 设置 调整方向以及 设计校准值
if(temp >= CRYSTAL_CENTER_OFFSET)
{
RTC->ADJUST = temp - CRYSTAL_CENTER_OFFSET ;
RTC->ADSIGN = 0;
}
else
{ // sub the current count .
RTC->ADJUST = CRYSTAL_CENTER_OFFSET - temp ;
RTC->ADSIGN = 1;
}
}
在调用这个函数之前RTC已经处于运行中状态, 上述代码将每3小时 在RTC产生小时中断后3ms之后调用 ,
上述代码中 第一步测试确认正常 , 第二步确认也正常 , 以 25度 时 为例 :最终设置结果为 RTC->ADJUST = 0x22 , RTC ->ADSIGN = 1 ;
但实际测试发现 经过上述设置后 RTC的时间反而比没有调整 (RTC->ADJUST = 0x0 , RTC ->ADSIGN = 0 ) 更快了 , 似乎调整方向反了 ?
但查规格书 RTC ->ADSIGN 为0 为增加RTC计数值, 为1 为减少RTC计数值, 是否规格书中写错了 ?
|
|