@@ -29,6 +29,7 @@ Version Modified By Date Comments
2929 09/11/25 Fixed timer0 from being excluded
30300006 D Mellis 09/12/29 Replaced objects with functions
31310007 M Sproul 10/08/29 Changed #ifdefs from cpu to register
32+ 0008 S Kanemoto 12/06/22 Fixed for Leonardo by @maris_HY
3233*************************************************/
3334
3435#include < avr/interrupt.h>
@@ -85,24 +86,34 @@ volatile uint8_t timer5_pin_mask;
8586#endif
8687
8788
88- // MLS: This does not make sense, the 3 options are the same
8989#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
9090
9191#define AVAILABLE_TONE_PINS 1
92+ #define USE_TIMER2
9293
9394const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 2 /* , 3, 4, 5, 1, 0 */ };
9495static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /* , 255, 255, 255, 255, 255 */ };
9596
9697#elif defined(__AVR_ATmega8__)
9798
9899#define AVAILABLE_TONE_PINS 1
100+ #define USE_TIMER2
99101
100102const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 2 /* , 1 */ };
101103static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /* , 255 */ };
102104
105+ #elif defined(__AVR_ATmega32U4__)
106+
107+ #define AVAILABLE_TONE_PINS 1
108+ #define USE_TIMER3
109+
110+ const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 3 /* , 1 */ };
111+ static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /* , 255 */ };
112+
103113#else
104114
105115#define AVAILABLE_TONE_PINS 1
116+ #define USE_TIMER2
106117
107118// Leave timer 0 to last.
108119const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 2 /* , 1, 0 */ };
@@ -480,8 +491,7 @@ void noTone(uint8_t _pin)
480491 digitalWrite (_pin, 0 );
481492}
482493
483- #if 0
484- #if !defined(__AVR_ATmega8__)
494+ #ifdef USE_TIMER0
485495ISR (TIMER0_COMPA_vect)
486496{
487497 if (timer0_toggle_count != 0 )
@@ -501,6 +511,7 @@ ISR(TIMER0_COMPA_vect)
501511#endif
502512
503513
514+ #ifdef USE_TIMER1
504515ISR (TIMER1_COMPA_vect)
505516{
506517 if (timer1_toggle_count != 0 )
@@ -520,6 +531,7 @@ ISR(TIMER1_COMPA_vect)
520531#endif
521532
522533
534+ #ifdef USE_TIMER2
523535ISR (TIMER2_COMPA_vect)
524536{
525537
@@ -541,12 +553,10 @@ ISR(TIMER2_COMPA_vect)
541553// *timer2_pin_port &= ~(timer2_pin_mask); // keep pin low after stop
542554 }
543555}
556+ #endif
544557
545558
546-
547- // #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
548- #if 0
549-
559+ #ifdef USE_TIMER3
550560ISR (TIMER3_COMPA_vect)
551561{
552562 if (timer3_toggle_count != 0 )
@@ -563,7 +573,10 @@ ISR(TIMER3_COMPA_vect)
563573 *timer3_pin_port &= ~(timer3_pin_mask); // keep pin low after stop
564574 }
565575}
576+ #endif
577+
566578
579+ #ifdef USE_TIMER4
567580ISR (TIMER4_COMPA_vect)
568581{
569582 if (timer4_toggle_count != 0 )
@@ -580,7 +593,10 @@ ISR(TIMER4_COMPA_vect)
580593 *timer4_pin_port &= ~(timer4_pin_mask); // keep pin low after stop
581594 }
582595}
596+ #endif
583597
598+
599+ #ifdef USE_TIMER5
584600ISR (TIMER5_COMPA_vect)
585601{
586602 if (timer5_toggle_count != 0 )
@@ -597,5 +613,4 @@ ISR(TIMER5_COMPA_vect)
597613 *timer5_pin_port &= ~(timer5_pin_mask); // keep pin low after stop
598614 }
599615}
600-
601616#endif
0 commit comments