/** * Parse and then execute the "S" command from the user, which sets the time. */ void command_set(char *command) { uint8_t rc; rtc_datetime_24h_t dt; rc = rtc_clock_stop(rtc); printf_P(PSTR("Halted clock, rc=%i\n"), rc); rtc_sqw_enable(rtc); rtc_sqw_rate(rtc, 1); rc = rtc_read(rtc, &dt); sscanf_P(command, PSTR("S %04d-%02hhd-%02hhd %02hhd:%02hhd:%02hhd\n"), &dt.year, &dt.month, &dt.date, &dt.hour, &dt.minute, &dt.second); dt.day_of_week = rtc_find_dow(dt.year, dt.month, dt.date); printf_P(PSTR("Setting time to %04i-%02i-%02i %02i:%02i:%02i (%s)\n"), dt.year, dt.month, dt.date, dt.hour, dt.minute, dt.second, rtc_dow_names[dt.day_of_week]); printf_P(PSTR("Trying to write RTC...\n")); rc = rtc_write(rtc, &dt); printf_P(PSTR("Wrote RTC, rc=%i\n"), rc); rc = rtc_clock_start(rtc); printf_P(PSTR("Started clock, rc=%i\n"), rc); }
/** * MCU: Atmega328 * Fuses: Oscilador interno a 8 Mhz (sin dividir por 8) * -U lfuse:w:0xe2:m -U hfuse:w:0xd1:m -U efuse:w:0x07:m */ int main(void) { adc_init(); timer0_init(timer0_callback); i2c_init(); rtc_init(rtc); rtc_sqw_rate(rtc, 1); rtc_sqw_enable(rtc); rtc_clock_start(rtc); eMBInit(MB_RTU, 0x03, 0, 9600, MB_PAR_NONE); eMBSetSlaveID(0x3, TRUE, (UCHAR*) "demeter", 8); eMBEnable(); blinkenlight(5, 100); parameters_init(); ports_init(); f_mount(&fs, "", 0); update_log_filename(); while (1) { eMBPoll(); update_state(); _delay_ms(100); } return (0); }
int main(void) { uart_t *u0; uint8_t rc; rtc_device_t *rtc = &rtc_ds1307; rtc_datetime_24h_t current_dt, offset_dt; _delay_ms(1000); u0 = uart_init("0", UART_BAUD_SELECT(38400, F_CPU)); uart_init_stdout(u0); i2c_init(); DDRC = _BV(PC0) | _BV(PC1); PORTC = _BV(PC0) | _BV(PC1); sei(); printf("\n\nBooted up!\n"); rc = rtc_init(rtc); printf("Inited RTC, rc=%i\n", rc); rc = rtc_sqw_rate(rtc, 1); printf("Set sqw rate, rc=%i\n", rc); rc = rtc_sqw_enable(rtc); printf("Set sqw enable, rc=%i\n", rc); rc = rtc_clock_start(rtc); printf("Started clock, rc=%i\n", rc); _delay_ms(1000); while(1) { rc = rtc_read(rtc, ¤t_dt); rtc_offset_time(¤t_dt, &offset_dt, -7); printf("rc = %i, %04i-%02i-%02i %02i:%02i:%02i %i offset: %04i-%02i-%02i %02i:%02i:%02i %i\n", rc, current_dt.year, current_dt.month, current_dt.date, current_dt.hour, current_dt.minute, current_dt.second, current_dt.day_of_week, offset_dt.year, offset_dt.month, offset_dt.date, offset_dt.hour, offset_dt.minute, offset_dt.second, offset_dt.day_of_week ); _delay_ms(1000); } return(0); }
void command_set_from_gps(void) { uint8_t rc; if(update_gps()) return; if(gps_data.gps_signal_strength < 3) { printf_P(PSTR("GPS signal strength (%d) is unreliable. Aborting sync!\n"), gps_data.gps_signal_strength); return; } if(gps_data.dt.second < (59 - gps_leap_second_offset)) { /* Adjust to the edge of the next second, don't deal with rollover */ printf_P(PSTR("Sleeping for %d ms to synchronize...\n"), 1000 - gps_data.dt.millisecond - 1); wait_ms(1000 - gps_data.dt.millisecond - 1); gps_data.dt.second += 1 + gps_leap_second_offset; gps_data.dt.millisecond = 0; } rc = rtc_clock_stop(rtc); printf_P(PSTR("Halted clock, rc=%i\n"), rc); rtc_sqw_enable(rtc); rtc_sqw_rate(rtc, 1); printf_P(PSTR("Setting time to %04i-%02i-%02i %02i:%02i:%02i (%s)\n"), gps_data.dt.year, gps_data.dt.month, gps_data.dt.date, gps_data.dt.hour, gps_data.dt.minute, gps_data.dt.second, rtc_dow_names[gps_data.dt.day_of_week]); printf_P(PSTR("Trying to write RTC...\n")); rc = rtc_write(rtc, &gps_data.dt); printf_P(PSTR("Wrote RTC, rc=%i\n"), rc); rc = rtc_clock_start(rtc); printf_P(PSTR("Started clock, rc=%i\n"), rc); }