use internal RTC counter

This commit is contained in:
King Kévin 2016-04-03 21:50:22 +02:00
parent a07e287865
commit f89a9b8ea6
1 changed files with 12 additions and 26 deletions

38
main.c
View File

@ -83,8 +83,6 @@ const uint32_t ticks_midday = 12*60*60*TICKS_PER_SECOND;
/** RGB values for the WS2812b clock LEDs */
uint8_t clock_leds[WS2812B_LEDS*3] = {0};
/** current time in tick */
volatile uint32_t current_time = 0;
/** user input command */
char command[32] = {0};
/** user input command index */
@ -317,19 +315,8 @@ static void process_command(char* str)
} else if (strlen(word)!=8 || word[0]<'0' || word[0]>'2' || word[1]<'0' || word[1]>'9' || word[3]<'0' || word[3]>'5' || word[4]<'0' || word[4]>'9' || word[6]<'0' || word[6]>'5' || word[7]<'0' || word[7]>'9') {
goto error;
} else {
/*
if (!rtc_write_hours((word[0]-'0')*10+(word[1]-'0')*1)) {
printf("setting hours failed\n");
} else if (!rtc_write_minutes((word[3]-'0')*10+(word[4]-'0')*1)) {
printf("setting minutes failed\n");
} else if (!rtc_write_seconds((word[6]-'0')*10+(word[7]-'0')*1)) {
printf("setting seconds failed\n");
} else {
current_time = rtc_read_hours()*ticks_hour+rtc_read_minutes()*ticks_minute+rtc_read_seconds()*ticks_second; // set also internal time
rtc_oscillator_enable(); // be sure the oscillation is enabled
printf("time set\n");
}
*/
rtc_set_counter_val(((word[0]-'0')*10+(word[1]-'0')*1)*ticks_hour+((word[3]-'0')*10+(word[4]-'0')*1)*ticks_minute+((word[6]-'0')*10+(word[7]-'0')*1)*ticks_second); // set time in RTC counter
printf("time set\n");
}
} else {
goto error;
@ -404,7 +391,7 @@ int main(void)
printf("welcome to the CuVoodoo LED clock\n"); // print welcome message
led_on(); // switch on LED to indicate setup completed
// read internal reference 1.2V voltage
uint8_t channels[] = {ADC_CHANNEL17}; // voltages to convert
adc_set_regular_sequence(BATTERY_ADC, LENGTH(channels), channels); // set channels to convert
@ -424,8 +411,8 @@ int main(void)
printf("battery voltage: %.2fV\n", battery_voltage);
// get date and time
current_time = 6*ticks_hour+15*ticks_minute; // the current time
clock_animate_time(current_time); // set time with animation
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
clock_animate_time(rtc_get_counter_val()); // set time with animation
printf("input commands\n");
bool action = false; // if an action has been performed don't go to sleep
@ -470,19 +457,19 @@ int main(void)
while (time_flag) { // time passed
time_flag = false; // reset flag
action = true; // action has been performed
if ((current_time%ticks_second)==0) { // one second passed
if ((rtc_get_counter_val()%ticks_second)==0) { // one second passed
led_toggle(); // LED activity to show we are not stuck
}
if ((current_time%ticks_minute)==0) { // one minute passed
printf("it is now %02lu:%02lu:%02lu\n", current_time/ticks_hour, (current_time%ticks_hour)/ticks_minute, (current_time%ticks_minute)/ticks_second); // display time
if ((rtc_get_counter_val()%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
}
if ((current_time%ticks_hour)==0) { // one hours passed
if ((rtc_get_counter_val()%ticks_hour)==0) { // one hours passed
clock_hours(); // show hour markers
}
if ((current_time%ticks_midday*2)==0) { // one day passed
current_time = 0; // reset time counter
if (rtc_get_counter_val()>=ticks_midday*2) { // one day passed
rtc_set_counter_val(rtc_get_counter_val()%ticks_midday); // reset time counter
}
clock_set_time(current_time); // set time
clock_set_time(rtc_get_counter_val()); // set time
}
if (action) { // go to sleep if nothing had to be done, else recheck for activity
action = false;
@ -505,6 +492,5 @@ void BUTTON_ISR(void)
void rtc_isr(void)
{
rtc_clear_flag(RTC_SEC); // clear flag
current_time++; // increment time
time_flag = true; // notify to show new time
}