Stairs/module/src/lib/TinyMillis.c

47 lines
610 B
C

/*
* Stairs lighting
* Copyright 2017 (c) Mark van Renswoude
*
* https://git.x2software.net/pub/Stairs
*/
#include "TinyMillis.h"
#include <avr/io.h>
#include <util/atomic.h>
#include <avr/interrupt.h>
volatile uint16_t tickCount;
ISR(TIMER1_COMPA_vect)
{
tickCount++;
}
void millis_init(void)
{
uint32_t overflow;
overflow = ((F_CPU / 1000) / 8);
TCCR1B |= (1 << WGM12) | (1 << CS11);
OCR1AH = (overflow >> 8);
OCR1AL = overflow;
TIMSK |= (1 << OCIE1A);
}
uint16_t millis(void)
{
uint16_t value;
ATOMIC_BLOCK(ATOMIC_FORCEON)
{
value = tickCount;
}
return value;
}