avrinput/time.c

38 lines
633 B
C
Raw Normal View History

2019-01-01 15:48:54 +00:00
#include "time.h"
#include "conf.h"
#include <avr/interrupt.h>
#include <avr/io.h>
2019-01-17 18:39:21 +00:00
volatile struct eigentime_s _eigentime_ms;
2019-01-01 15:48:54 +00:00
void
time_init(void)
{
2019-01-17 18:39:21 +00:00
_eigentime_ms.low = 0;
_eigentime_ms.high = 0;
// prescaler is 64.
TCCR0B = _BV(CS00) | _BV(CS01);
TIMSK0 |= _BV(TOIE0);
sei();
2019-01-01 15:48:54 +00:00
}
2019-01-17 18:39:21 +00:00
void
get_eigentime(struct eigentime_s * time)
2019-01-01 15:48:54 +00:00
{
2019-01-17 18:39:21 +00:00
time->low = _eigentime_ms.low;
time->high = _eigentime_ms.high;
2019-01-01 15:48:54 +00:00
}
2019-01-17 18:39:21 +00:00
ISR(TIMER0_OVF_vect)
2019-01-01 15:48:54 +00:00
{
2019-01-17 18:39:21 +00:00
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++;
}
2019-01-01 15:48:54 +00:00
}