diff --git a/lib/c/avr.h b/lib/c/avr.h index acd5931..e3eadce 100644 --- a/lib/c/avr.h +++ b/lib/c/avr.h @@ -34,6 +34,11 @@ #ifdef __AVR__ #include #include +#include + +#ifndef F_CPU +#error "F_CPU must be defined" +#endif void run_timers(); @@ -41,6 +46,19 @@ ISR(TIMER0_OVF_vect) { run_timers(); } +static inline void set_ocr1a(uint16_t value) { + OCR1AH = value >> 8; + OCR1AL = value; +} + +/* Source: https://adnbr.co.uk/articles/counting-milliseconds */ +void init_timer1() { + TCCR1B |= _BV(WGM12) | _BV(CS11); + set_ocr1a((F_CPU / 1000) / 8); + TIMSK1 |= _BV(OCIE1A); + sei(); +} + static inline void atomic_memcpy(void *dest, const void *src, size_t size) { ATOMIC_BLOCK(ATOMIC_FORCEON) { memcpy(dest, src, size);