rgbyteclock-code/timer.c

167 lines
3.6 KiB
C

/*
* "THE BEER-WARE LICENSE" (Revision 42):
* Martin Wenger <martin.wenger@arcormail.de> and Stefan Rupp <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/us a beer in return.
* (c) 2005-2010 Martin Wenger, Stefan Rupp
* (c) 2013,2014 Stefan Rupp
*/
#include <avr/io.h>
#include "timer.h"
#include <stdlib.h>
#include <avr/interrupt.h>
/* timer_time gets increased on every interrupt
* those interrupts happen every 1ms
*/
static volatile uint32_t timer_time;
ISR(TIMER0_COMPA_vect) {
++timer_time;
return;
}
/**
* Initialize the timer
* This function has to be called first, before calling timer_wait and/or timer_get,
* else timer_get will always return 0, and timer_wait will wait forever!
*/
void timer_init(void)
{
uint8_t sreg = SREG;
// Stop all interrupts
cli();
// Reset timer to zero
timer_time = 0;
// - Time accuracy: 1 millisecond (corresponding frequency: 1kHz)
// ==> F_CPU = 20Mhz
// ==> 20Mhz / 256 = 78.125 kHz
// ==> let timer count to 78 to get (almost) 1kHz frequency
// therefore:
// - Set Timer/Counter0 prescaler to 256 ==> (1<<CS02)
// - Set OCR2 to 78
// - CTC ( i.e. clear counter, when COUNTER == OCR0A) ==> (1<<WGM01)
// unfortunately, due to the 20MHz and the coarse prescaler dividers
// provided, we can't get any closer to the desired frequency of
// 1kHz :(
OCR0A = 78;
TCCR0B = (1<<WGM02);
TCCR0A = (1<<CS02);
// Interrupts setzen
TIMSK0 |= (1<<OCIE0A);
SREG = sreg;
}
/**
* Get the current time
* \return the current time (in ms)
*/
inline uint32_t timer_get(void)
{
uint32_t t;
uint8_t sreg;
sreg = SREG;
cli();
t = timer_time;
SREG = sreg;
return t;
}
/**
* Wait for (at least) the specified amount of time
* \param delay The time to wait (in ms)
*/
void timer_wait(uint32_t delay)
{
uint32_t end = timer_get() + delay +1;
while ( end > timer_get() );
return;
}
/**
* Decode a integer timestamp (in microseconds)
* into hours, minutes, seconds, microseconds
* \param time the time in microseconds
* \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 timer_decode(uint32_t time, uint8_t *hours, uint8_t *minutes, uint8_t *seconds, uint16_t *ms)
{
if (ms != NULL) {
*ms = time % 1000;
}
time /= 1000;
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;
}
*/