DCF77_Clock_Control/main.cpp

78 lines
2.0 KiB
C++

/**
* Copyright (C) 2022 Julian Metzler
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include "config.h"
#include "clock.h"
#include "dcf77.h"
dcf77::decoder dcf77_dec;
static volatile uint32_t t = 0;
uint32_t dcf77_lastUpdate = 0;
uint32_t minute_lastUpdate = 0;
uint8_t dcf77_initial = 1;
ISR(TIMER1_COMPA_vect) { t++; }
int main(void) {
clock_init();
// Set up pin directions
DCF77_DDR &= ~(1 << DCF77_PIN);
DCF77_PORT |= (1 << DCF77_PIN);
// Configure a millisecond timer
TCCR1B = (1 << WGM12) | (1 << CS10);
OCR1A = F_CPU / 1000;
TIMSK1 = (1 << OCIE1A);
sei();
//clock_setTime(1, 23);
//while(1) clock_loop(t);
while (1) {
if (dcf77_initial || (t - dcf77_lastUpdate) >= DCF77_UPDATE_INTERVAL) {
const bool dcf77_signal = !(DCF77_PINR & (1 << DCF77_PIN));
if (dcf77_dec.sample(!dcf77_signal, t) >=
dcf77::decoder::state::has_time_and_date) {
auto &d = dcf77_dec.get_data();
// Valid data has been received, do something with it
clock_setTime(d.hour(), d.minute());
dcf77_lastUpdate = t;
minute_lastUpdate = t;
dcf77_initial = 0;
}
}
if ((t - minute_lastUpdate) >= 60000) {
clock_advanceSet();
minute_lastUpdate = t;
}
clock_loop(t);
// Wait for the next millisecond
set_sleep_mode(SLEEP_MODE_IDLE);
sleep_mode();
}
}