added incomplete RTC module
This commit is contained in:
		
							
								
								
									
										5
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										5
									
								
								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 
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										132
									
								
								rtc.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										132
									
								
								rtc.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,132 @@
 | 
			
		||||
/*
 | 
			
		||||
 * ----------------------------------------------------------------------------
 | 
			
		||||
 * "THE BEER-WARE LICENSE" (Revision 42):
 | 
			
		||||
 * <struppi@struppi.name> 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 <avr/io.h>
 | 
			
		||||
#include "rtc.h"
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#include <avr/interrupt.h>
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
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
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user