diff options
author | Daniel Friesel <derf@finalrewind.org> | 2022-01-26 22:35:00 +0100 |
---|---|---|
committer | Daniel Friesel <derf@finalrewind.org> | 2022-01-26 22:35:00 +0100 |
commit | 6be3d8699095f53f037e051ba889b1187609d19f (patch) | |
tree | fda588eca4e213bd9e06d188904e17aebc2575d9 /include/arch/arduino-nano/driver | |
parent | a324a83c5602fa7921282adc66a9f704af00ae2f (diff) |
arduino nano: fix timer; note that it's limited to ≥ 62 Hz
Diffstat (limited to 'include/arch/arduino-nano/driver')
-rw-r--r-- | include/arch/arduino-nano/driver/timer.h | 13 |
1 files changed, 9 insertions, 4 deletions
diff --git a/include/arch/arduino-nano/driver/timer.h b/include/arch/arduino-nano/driver/timer.h index e5375ba..a048c3d 100644 --- a/include/arch/arduino-nano/driver/timer.h +++ b/include/arch/arduino-nano/driver/timer.h @@ -17,13 +17,18 @@ class Timer { public: Timer() {} - inline void setup_khz(uint16_t const frequency) { - OCR0A = frequency ? 255 / frequency : 1; + inline void setup_khz(uint16_t const frequency) { // 16 MHz / 64 == 250 kHz base + OCR0A = frequency ? 250 / frequency : 1; TCCR0A = _BV(WGM01); prescaler = _BV(CS01) | _BV(CS00); } - inline void setup_hz(uint16_t const frequency) { - OCR0A = frequency ? 255 / frequency : 1; + // lowest supported frequency: 62 Hz + inline void setup_hz(uint16_t const frequency) { // 16 MHz / 1024 == 15.625 kHz base + if (15625 / frequency > 255) { + OCR0A = 255; + } else { + OCR0A = 15625 / frequency; + } TCCR0A = _BV(WGM01); prescaler = _BV(CS02) | _BV(CS00); } |