/* * ---------------------------------------------------------------------------- * "THE BEER-WARE LICENSE" (Revision 42): * wrote this file. As long as you retain this notice you * can do whatever you want with this stuff. If we meet some day, and you think * this stuff is worth it, you can buy me a beer in return. * (c) 2014 Stefan Rupp * ---------------------------------------------------------------------------- */ #include #include "rtc.h" #include #include static volatile uint32_t rtc_time; ISR(TIMER2_COMP_vect) { ++rtc_time; return; } void rtc_init(void) { // Stop all interrupts cli(); // Reset timer to zero rtc_time = 0; // ... // ... // ... sei(); } /** * Get the current time * \return the current time (in s) */ inline uint32_t rtc_get(void) { uint32_t t; uint8_t sreg; sreg = SREG; cli(); t = rtc_time; SREG = sreg; return t; } #if 0 /** * Decode a integer timestamp (in seconds) * into days, hours, minutes, seconds * \param time the time in seconds * \param days will be filled in to contain the days after return * \param hours will be filled in to contain the hours after return * \param minutes will be filled in to contain the minutes after return * \param seconds will be filled in to contain the seconds after return * \param ms be filled in to contain the microseconds after return */ void rtc_decode(uint32_t time, uint16_t days, uint8_t *hours, uint8_t *minutes, uint8_t *seconds) { if (seconds != NULL) { *seconds = time % 60; } time /= 60; if (minutes != NULL) { *minutes = time % 60; } time /= 60; if (hours != NULL) { *hours = time; } return; } /** * Encode a time given as hours, minutes, seconds and microsecods into a integer storing micorseconds * \param time the variable to store the time into * \param hour the hours to store * \param hour the minutes to store * \param hour the seconds to store * \param hour the microseconds to store */ void timer_encode(uint32_t *time, uint8_t hours, uint8_t minutes, uint8_t seconds, uint16_t ms) { *time = hours; *time *= 60; *time += minutes; *time *= 60; *time += seconds; *time *= 1000U; *time += ms; return; } /* void timer_validate(uint8_t *hours, uint8_t *minutes, uint8_t *seconds, uint16_t *ms) { while (*ms >= 1000) { *ms -= 1000; (*seconds)++; } while (*seconds >= 60) { *ms -= 60; (*minutes)++; } while (*minutes >= 60) { *minutes -= 60; (*hours)++; } return; } */ #endif