avrinput/time.c
2019-01-17 19:39:21 +01:00

38 lines
633 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;
// 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;
_eigentime_ms.low += TIME_MILLIS_PER_OVERFLOW;
// Handle the overflow.
if(_eigentime_ms.low < last_eigentime_low)
{
_eigentime_ms.high++;
}
}