add drift output

This commit is contained in:
King Kévin 2016-06-11 20:14:19 +02:00
parent 9f938c1303
commit 1167eb7dd8
1 changed files with 16 additions and 7 deletions

23
main.c
View File

@ -382,6 +382,7 @@ int main(void)
rtc_auto_awake(RCC_LSE, 32768/ticks_second-1); // ensure RTC is on, uses the 32.678 kHz LSE, and the prescale is set to our tick speed, else update backup registers accordingly (power off the micro-controller for the change to take effect)
rtc_ds1307_setup(); // setup external RTC module
rtc_dcf77_setup(); // setup DCF77 module
rtc_dcf77_on(); // switch on DCF77 module
printf("OK\n");
// setup WS2812b LEDs
@ -431,9 +432,8 @@ int main(void)
printf("/!\\ RTC oscillator is disabled: the battery may be empty\n");
}
// variables to measure the RTC drift
uint32_t rtc_internal_start = rtc_get_counter_val();
uint32_t rtc_external_start = rtc_ds1307_ticks;
// use DCF77 time to measure drift
uint32_t time_dcf77 = 0;
// get date and time
printf("current time: %02lu:%02lu:%02lu\n", rtc_get_counter_val()/ticks_hour, (rtc_get_counter_val()%ticks_hour)/ticks_minute, (rtc_get_counter_val()%ticks_minute)/ticks_second); // display time
@ -491,11 +491,11 @@ int main(void)
adc_start_conversion_regular(ADC1); // start measuring ambient luminosity
}
if ((rtc_ds1307_ticks%ticks_second)==0) { // one second passed
//led_toggle(); // don't use the LED, this confuses the 32.768 kHz oscillator
led_toggle(); // LED toggling confuses the 32.768 kHz oscillator on the blue pill
}
if ((rtc_ds1307_ticks%ticks_minute)==0) { // one minute passed
printf("%02lu:%02lu:%02lu\n", rtc_get_counter_val()/ticks_hour, (rtc_get_counter_val()%ticks_hour)/ticks_minute, (rtc_get_counter_val()%ticks_minute)/ticks_second); // display time
printf("internal RTC drift: %f%%\n", 1-((double)(rtc_ds1307_ticks-rtc_external_start))/(double)(rtc_get_counter_val()-rtc_internal_start));
//printf("%02lu:%02lu:%02lu\n", rtc_get_counter_val()/ticks_hour, (rtc_get_counter_val()%ticks_hour)/ticks_minute, (rtc_get_counter_val()%ticks_minute)/ticks_second); // display internal time
printf("%02lu:%02lu:%02lu\n", rtc_ds1307_ticks/ticks_hour, (rtc_ds1307_ticks%ticks_hour)/ticks_minute, (rtc_ds1307_ticks%ticks_minute)/ticks_second); // display external time
}
if ((rtc_ds1307_ticks%ticks_hour)==0) { // one hours passed
clock_hours(); // show hour markers
@ -510,7 +510,16 @@ int main(void)
action = true; // action has been performed
uint8_t* dcf77_time = rtc_dcf77_time(); // get time
if (dcf77_time) { // ensure it's valid
printf("DCF77 time: 20%02u-%02u-%02u %02u:%02u:00\n", dcf77_time[5], dcf77_time[4], dcf77_time[3], dcf77_time[1], dcf77_time[0]); // display time
printf("DCF77 time: 20%02u-%02u-%02u %02u:%02u:00\n", dcf77_time[5], dcf77_time[4], dcf77_time[2], dcf77_time[1], dcf77_time[0]); // display time
uint32_t time_dcf77_new = dcf77_time[1]*ticks_hour+dcf77_time[0]*ticks_minute; // calculate new time
if (time_dcf77!=0) { // we got a previous time to measure drift
printf("internal/external RTC count drifts: %lu %lld %lld\n", time_dcf77_new-time_dcf77, (int64_t)rtc_get_counter_val()-time_dcf77_new, (int64_t)rtc_ds1307_ticks-time_dcf77_new);
}
time_dcf77 = time_dcf77_new; // set new time
rtc_set_counter_val(time_dcf77); // set internal RTC time
rtc_ds1307_ticks = time_dcf77; // set external RTC time
} else {
printf("DCF77 time: error\n");
}
}
while (photoresistor_flag) { // new photo-resistor value has been measured