From 4d4145690615833d6d5f580bdc5336c491fc905b Mon Sep 17 00:00:00 2001 From: Stefan Rupp Date: Sat, 1 Mar 2014 04:11:40 +0100 Subject: [PATCH] added incomplete RTC module --- Makefile | 5 ++- rtc.c | 132 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ rtc.h | 10 +++++ 3 files changed, 145 insertions(+), 2 deletions(-) create mode 100644 rtc.c create mode 100644 rtc.h diff --git a/Makefile b/Makefile index b53b79f..bd679c6 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ PRG = rgbyteclock -OBJ = rgbyteclock.o timer.o lcd.o main.o +OBJ = rgbyteclock.o timer.o lcd.o main.o rtc.o MCU_TARGET = atmega16 OPTIMIZE = -Os @@ -25,10 +25,11 @@ $(PRG).elf: $(OBJ) @echo # dependency: -rgbyteclock.o: timer.o lcd.o rgbyteclock.h +rgbyteclock.o: timer.o lcd.o rgbyteclock.h rtc.o main.o: rgbyteclock.o timer.o:timer.h lcd.o: lcd.h +rtc.o: rtc.h clean: rm -rf *.o $(PRG).elf *.eps *.png *.pdf *.bak diff --git a/rtc.c b/rtc.c new file mode 100644 index 0000000..835b3fd --- /dev/null +++ b/rtc.c @@ -0,0 +1,132 @@ +/* + * ---------------------------------------------------------------------------- + * "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 + diff --git a/rtc.h b/rtc.h new file mode 100644 index 0000000..3889fcd --- /dev/null +++ b/rtc.h @@ -0,0 +1,10 @@ +#if !defined RTC_H +#define RTC_H + +#include + + +uint32_t rtc_get(void); +void rtc_init(void); + +#endif // RTC_H