summaryrefslogtreecommitdiff
path: root/include/arch/atmega2560
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2022-01-26 22:01:18 +0100
committerDaniel Friesel <derf@finalrewind.org>2022-01-26 22:01:18 +0100
commita0e6e8efb208d8b6b21199554ddf21f86c405857 (patch)
treed7a077026817d283e9ff148ccb6e099d4e770948 /include/arch/atmega2560
parentc6ba398335daea46ca33ee000534299dc5e4b6b9 (diff)
atmega2650 timer: Use 16-bit Timer 4, fix setup_hz / setup_khz
Diffstat (limited to 'include/arch/atmega2560')
-rw-r--r--include/arch/atmega2560/driver/timer.h26
1 files changed, 13 insertions, 13 deletions
diff --git a/include/arch/atmega2560/driver/timer.h b/include/arch/atmega2560/driver/timer.h
index 73a8125..c16fbe4 100644
--- a/include/arch/atmega2560/driver/timer.h
+++ b/include/arch/atmega2560/driver/timer.h
@@ -6,7 +6,7 @@
#include <avr/io.h>
#include <avr/interrupt.h>
-#define ON_TIMER_INTERRUPT_head ISR(TIMER0_COMPA_vect) {
+#define ON_TIMER_INTERRUPT_head ISR(TIMER4_COMPA_vect) {
#define ON_TIMER_INTERRUPT_tail }
class Timer {
@@ -17,24 +17,24 @@ class Timer {
public:
Timer() {}
- inline void setup_khz(uint16_t const frequency) {
- OCR0A = frequency ? 255 / frequency : 1;
- TCCR0A = _BV(WGM01);
- prescaler = _BV(CS01) | _BV(CS00);
+ inline void setup_khz(uint16_t const frequency) { // 16 MHz base
+ OCR4A = frequency ? 16000 / frequency : 65535;
+ TCCR4A = _BV(WGM42);
+ prescaler = _BV(CS40);
}
- inline void setup_hz(uint16_t const frequency) {
- OCR0A = frequency ? 255 / frequency : 1;
- TCCR0A = _BV(WGM01);
- prescaler = _BV(CS02) | _BV(CS00);
+ inline void setup_hz(uint16_t const frequency) { // 16 MHz / 256 == 62.5 kHz base
+ OCR4A = frequency ? 62500 / frequency : 65535;
+ TCCR4A = _BV(WGM42);
+ prescaler = _BV(CS42);
}
inline void start(unsigned char const interrupt) {
- TCNT0 = 0;
- TCCR0B = prescaler;
+ TCNT4 = 0;
+ TCCR4B = prescaler;
if (interrupt) {
- TIMSK0 = _BV(OCIE0A);
+ TIMSK4 = _BV(OCIE4A);
}
}
- inline void stop() { TCCR0B = 0; TIMSK0 = 0; }
+ inline void stop() { TCCR4B = 0; TIMSK4 = 0; }
};
extern Timer timer;