time is working

This commit is contained in:
Daniel Knüttel 2019-01-17 19:39:21 +01:00
parent ac2d26bd65
commit fa096c7ce0
2 changed files with 30 additions and 15 deletions

29
time.c
View File

@ -4,25 +4,34 @@
#include <avr/interrupt.h> #include <avr/interrupt.h>
#include <avr/io.h> #include <avr/io.h>
volatile unsigned int _eigentime_ms; volatile struct eigentime_s _eigentime_ms;
void void
time_init(void) time_init(void)
{ {
_eigentime_ms = 0; _eigentime_ms.low = 0;
TCCR1B = _BV(CS10); _eigentime_ms.high = 0;
OCR1A = TIMER1_COMPA_VALUE; // prescaler is 64.
TIMSK |= _BV(OCIE1A); TCCR0B = _BV(CS00) | _BV(CS01);
TIMSK0 |= _BV(TOIE0);
sei();
} }
unsigned int void
get_eigentime(void) get_eigentime(struct eigentime_s * time)
{ {
return _eigentime_ms; time->low = _eigentime_ms.low;
time->high = _eigentime_ms.high;
} }
ISR(TIMER1_COMPA_vect) ISR(TIMER0_OVF_vect)
{ {
_eigentime_ms++; 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++;
}
} }

16
time.h
View File

@ -2,13 +2,19 @@
#define _TIME_H_ #define _TIME_H_
#include "conf.h" #include "conf.h"
#define TIMER1_MAX_VALUE 0xffff // This is 2.04 = 2ms
#define TIMER1_COMPA_VALUE (TIMER1_MAX_VALUE / F_CPU) #define TIME_MILLIS_PER_OVERFLOW (0xff * 1000UL * 64) / F_CPU
extern volatile unsigned int _eigentime_ms; // for 32bit eigentime
struct eigentime_s
{
unsigned int low, high;
};
unsigned int extern volatile struct eigentime_s _eigentime_ms;
get_eigentime(void);
void
get_eigentime(struct eigentime_s * time);
void void
time_init(void); time_init(void);