From 4827bdd9fea511457497f18155eb42a7fb0e1e28 Mon Sep 17 00:00:00 2001 From: Tom Barthe Date: Wed, 30 Dec 2020 01:33:48 +0100 Subject: [PATCH] WIP: Add a TIMER1 configuration routine in avr.h The function hasn't been tested on real hardware. --- lib/c/avr.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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);