From 718cd80f290ac78238eaeda691193e3f837ee7a9 Mon Sep 17 00:00:00 2001 From: Nathan Cheek Date: Wed, 14 Aug 2024 11:35:56 -0700 Subject: [PATCH] Add support for ATmega 128 --- TimerOne.h | 12 +++++++++++- config/known_16bit_timers.h | 7 +++++++ library.properties | 2 +- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/TimerOne.h b/TimerOne.h index 99e4502..43ea737 100644 --- a/TimerOne.h +++ b/TimerOne.h @@ -6,6 +6,7 @@ * Modified April 2012 by Paul Stoffregen - portable to other AVR chips, use inline functions * Modified again, June 2014 by Paul Stoffregen - support Teensy 3.x & even more AVR chips * Modified July 2017 by Stoyko Dimitrov - added support for ATTiny85 except for the PWM functionality + * Modified August 2024 by Nathan Cheek - added support for ATmega128 * * * This is free software. You can redistribute it and/or modify it under @@ -171,7 +172,7 @@ class TimerOne #elif defined(__AVR__) -#if defined (__AVR_ATmega8__) +#if defined (__AVR_ATmega8__) || defined (__AVR_ATmega128__) //in some io definitions for older microcontrollers TIMSK is used instead of TIMSK1 #define TIMSK1 TIMSK #endif @@ -278,14 +279,23 @@ class TimerOne void attachInterrupt(void (*isr)()) __attribute__((always_inline)) { isrCallback = isr; + #if defined(__AVR_ATmega128__) + TIMSK1 |= _BV(TOIE1); + TIMSK1 &= ~(_BV(TICIE1) | _BV(OCIE1A) | _BV(OCIE1B)); + #else TIMSK1 = _BV(TOIE1); + #endif } void attachInterrupt(void (*isr)(), unsigned long microseconds) __attribute__((always_inline)) { if(microseconds > 0) setPeriod(microseconds); attachInterrupt(isr); } void detachInterrupt() __attribute__((always_inline)) { + #if defined(__AVR_ATmega128__) + TIMSK1 &= ~(_BV(TOIE1) | _BV(TICIE1) | _BV(OCIE1A) | _BV(OCIE1B)); + #else TIMSK1 = 0; + #endif } static void (*isrCallback)(); static void isrDefaultUnused(); diff --git a/config/known_16bit_timers.h b/config/known_16bit_timers.h index ec93a45..7aed224 100644 --- a/config/known_16bit_timers.h +++ b/config/known_16bit_timers.h @@ -143,6 +143,13 @@ //#define TIMER1_ICP_PIN 8 //#define TIMER1_CLK_PIN 5 +#elif defined (__AVR_ATmega128__) + #define TIMER1_A_PIN 13 // PB5 + #define TIMER1_B_PIN 14 // PB6 + #define TIMER1_C_PIN 15 // PB7 + #define TIMER1_ICP_PIN 22 // PD4 + #define TIMER1_CLK_PIN 24 // PD6 + // Sanguino // #elif defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644__) diff --git a/library.properties b/library.properties index cae8a9a..4822b6b 100644 --- a/library.properties +++ b/library.properties @@ -1,6 +1,6 @@ name=TimerOne version=1.1.1 -author=Stoyko Dimitrov, Jesse Tane, Jérôme Despatis, Michael Polli, Dan Clemens, Paul Stoffregen +author=Stoyko Dimitrov, Jesse Tane, Jérôme Despatis, Michael Polli, Dan Clemens, Nathan Cheek, Paul Stoffregen maintainer=Paul Stoffregen sentence=Use hardware Timer1 for finer PWM control and/or running an periodic interrupt function paragraph=