Posts: 1
Registered: ‎03-16-2017
Accepted Solution

Delays in example code RTC_Init

I am using the C8051F931 and have been setting up the SmaRTClock with the submitted example code in SmartClock.c, RTC_Init: This is a snippet of the RTC_Init():


RTC_Write (RTC0CN, 0xC0); // Enable missing smaRTClock detector
// and leave smaRTClock oscillator
// enabled.

// Wait > 2 ms
for (i=0x340; i!=0; i--);

RTC_Write (RTC0CN, 0xC0); // Clear the clock fail flag (precautionary).

The comment says "Wait > 2 ms", and indeed it does, but if I measured it correctly on the scope, it is way longer, more like 50ms. I figured that the loop takes ~8-11 machine cycles and the example code is using the 156 kHz LP oscillator, which gives a similar result of roughly 40-60ms


So, if I am correct, why is it longer than needed, and could I trust the "> 2ms" info and reduce the delay?


Posts: 344
Registered: ‎09-22-2009

Re: Delays in example code RTC_Init

That code was likely written for the worst case scenario of running with the internal precision oscillator at full speed. You may reduce the delay down to 2 ms if you know what clock speed you will always be at when you reach that line.

Posts: 3,153
Registered: ‎02-07-2002

Re: Delays in example code RTC_Init

The delay also highly depends on how much optimization the compiler can achieve.