avrinput/time.c

50 lines
883 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;
2019-04-07 18:35:05 +00:00
_eigentime_ms.tenth = 0;
2019-01-17 18:39:21 +00:00
// 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;
2019-04-07 18:35:05 +00:00
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;
}
2019-01-17 18:39:21 +00:00
// Handle the overflow.
if(_eigentime_ms.low < last_eigentime_low)
{
_eigentime_ms.high++;
}
2019-01-01 15:48:54 +00:00
}