avrinput/time.c

50 lines
883 B
C

#include "time.h"
#include "conf.h"
#include <avr/interrupt.h>
#include <avr/io.h>
volatile struct eigentime_s _eigentime_ms;
void
time_init(void)
{
_eigentime_ms.low = 0;
_eigentime_ms.high = 0;
_eigentime_ms.tenth = 0;
// prescaler is 64.
TCCR0B = _BV(CS00) | _BV(CS01);
TIMSK0 |= _BV(TOIE0);
sei();
}
void
get_eigentime(struct eigentime_s * time)
{
time->low = _eigentime_ms.low;
time->high = _eigentime_ms.high;
}
ISR(TIMER0_OVF_vect)
{
unsigned int last_eigentime_low = _eigentime_ms.low;
unsigned int ms;
// well basically at 20Mhz milliseconds are not precise enough.
_eigentime_ms.tenth += TIME_TENTH_MILLIS_PER_OVERFLOW;
if(_eigentime_ms.tenth > 10)
{
ms = _eigentime_ms.tenth / 10;
_eigentime_ms.low += ms;
_eigentime_ms.tenth -= 10 * ms;
}
// Handle the overflow.
if(_eigentime_ms.low < last_eigentime_low)
{
_eigentime_ms.high++;
}
}