3 A Tone Generator Library
5 Written by Brett Hagman
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 Version Modified By Date Comments
22 ------- ----------- -------- --------
23 0001 B Hagman 09/08/02 Initial coding
24 0002 B Hagman 09/08/18 Multiple pins
25 0003 B Hagman 09/08/18 Moved initialization from constructor to begin()
26 0004 B Hagman 09/09/26 Fixed problems with ATmega8
27 0005 B Hagman 09/11/23 Scanned prescalars for best fit on 8 bit timers
28 09/11/25 Changed pin toggle method to XOR
29 09/11/25 Fixed timer0 from being excluded
30 0006 D Mellis 09/12/29 Replaced objects with functions
31 0007 M Sproul 10/08/29 Changed #ifdefs from cpu to register
32 0008 S Kanemoto 12/06/22 Fixed for Leonardo by @maris_HY
33 0009 J Reucker 15/04/10 Issue #292 Fixed problems with ATmega8 (thanks to Pete62)
34 0010 jipp 15/04/13 added additional define check #2923
35 *************************************************/
37 #include <avr/interrupt.h>
38 #include <avr/pgmspace.h>
40 #include "pins_arduino.h"
42 #if defined(__AVR_ATmega8__) || defined(__AVR_ATmega128__)
50 #define TIMER2_COMPA_vect TIMER2_COMP_vect
54 // timerx_toggle_count:
55 // > 0 - duration specified
57 // < 0 - infinitely (until stop() method called, or new play() called)
59 #if !defined(__AVR_ATmega8__)
60 volatile long timer0_toggle_count;
61 volatile uint8_t *timer0_pin_port;
62 volatile uint8_t timer0_pin_mask;
65 volatile long timer1_toggle_count;
66 volatile uint8_t *timer1_pin_port;
67 volatile uint8_t timer1_pin_mask;
68 volatile long timer2_toggle_count;
69 volatile uint8_t *timer2_pin_port;
70 volatile uint8_t timer2_pin_mask;
73 volatile long timer3_toggle_count;
74 volatile uint8_t *timer3_pin_port;
75 volatile uint8_t timer3_pin_mask;
79 volatile long timer4_toggle_count;
80 volatile uint8_t *timer4_pin_port;
81 volatile uint8_t timer4_pin_mask;
85 volatile long timer5_toggle_count;
86 volatile uint8_t *timer5_pin_port;
87 volatile uint8_t timer5_pin_mask;
91 #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
93 #define AVAILABLE_TONE_PINS 1
96 const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 2 /*, 3, 4, 5, 1, 0 */ };
97 static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255, 255, 255, 255, 255 */ };
99 #elif defined(__AVR_ATmega8__)
101 #define AVAILABLE_TONE_PINS 1
104 const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 2 /*, 1 */ };
105 static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255 */ };
107 #elif defined(__AVR_ATmega32U4__)
109 #define AVAILABLE_TONE_PINS 1
112 const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 3 /*, 1 */ };
113 static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255 */ };
117 #define AVAILABLE_TONE_PINS 1
120 // Leave timer 0 to last.
121 const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 2 /*, 1, 0 */ };
122 static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255, 255 */ };
128 static int8_t toneBegin(uint8_t _pin)
132 // if we're already using the pin, the timer should be configured.
133 for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
134 if (tone_pins[i] == _pin) {
135 return pgm_read_byte(tone_pin_to_timer_PGM + i);
139 // search for an unused timer.
140 for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
141 if (tone_pins[i] == 255) {
143 _timer = pgm_read_byte(tone_pin_to_timer_PGM + i);
150 // Set timer specific stuff
151 // All timers in CTC mode
152 // 8 bit timers will require changing prescalar values,
153 // whereas 16 bit timers are set to either ck/1 or ck/64 prescalar
156 #if defined(TCCR0A) && defined(TCCR0B) && defined(WGM01)
161 bitWrite(TCCR0A, WGM01, 1);
162 bitWrite(TCCR0B, CS00, 1);
163 timer0_pin_port = portOutputRegister(digitalPinToPort(_pin));
164 timer0_pin_mask = digitalPinToBitMask(_pin);
168 #if defined(TCCR1A) && defined(TCCR1B) && defined(WGM12)
173 bitWrite(TCCR1B, WGM12, 1);
174 bitWrite(TCCR1B, CS10, 1);
175 timer1_pin_port = portOutputRegister(digitalPinToPort(_pin));
176 timer1_pin_mask = digitalPinToBitMask(_pin);
180 #if defined(TCCR2A) && defined(TCCR2B)
185 bitWrite(TCCR2A, WGM21, 1);
186 bitWrite(TCCR2B, CS20, 1);
187 timer2_pin_port = portOutputRegister(digitalPinToPort(_pin));
188 timer2_pin_mask = digitalPinToBitMask(_pin);
192 #if defined(TCCR3A) && defined(TCCR3B) && defined(TIMSK3)
197 bitWrite(TCCR3B, WGM32, 1);
198 bitWrite(TCCR3B, CS30, 1);
199 timer3_pin_port = portOutputRegister(digitalPinToPort(_pin));
200 timer3_pin_mask = digitalPinToBitMask(_pin);
204 #if defined(TCCR4A) && defined(TCCR4B) && defined(TIMSK4)
210 bitWrite(TCCR4B, WGM42, 1);
212 // TODO this may not be correct
214 bitWrite(TCCR4B, CS43, 1);
216 bitWrite(TCCR4B, CS40, 1);
217 timer4_pin_port = portOutputRegister(digitalPinToPort(_pin));
218 timer4_pin_mask = digitalPinToBitMask(_pin);
222 #if defined(TCCR5A) && defined(TCCR5B) && defined(TIMSK5)
227 bitWrite(TCCR5B, WGM52, 1);
228 bitWrite(TCCR5B, CS50, 1);
229 timer5_pin_port = portOutputRegister(digitalPinToPort(_pin));
230 timer5_pin_mask = digitalPinToBitMask(_pin);
241 // frequency (in hertz) and duration (in milliseconds).
243 void tone(uint8_t _pin, unsigned int frequency, unsigned long duration)
245 uint8_t prescalarbits = 0b001;
246 long toggle_count = 0;
250 _timer = toneBegin(_pin);
254 // Set the pinMode as OUTPUT
255 pinMode(_pin, OUTPUT);
257 // if we are using an 8 bit timer, scan through prescalars to find the best fit
258 if (_timer == 0 || _timer == 2)
260 ocr = F_CPU / frequency / 2 - 1;
261 prescalarbits = 0b001; // ck/1: same for both timers
264 ocr = F_CPU / frequency / 2 / 8 - 1;
265 prescalarbits = 0b010; // ck/8: same for both timers
267 if (_timer == 2 && ocr > 255)
269 ocr = F_CPU / frequency / 2 / 32 - 1;
270 prescalarbits = 0b011;
275 ocr = F_CPU / frequency / 2 / 64 - 1;
276 prescalarbits = _timer == 0 ? 0b011 : 0b100;
278 if (_timer == 2 && ocr > 255)
280 ocr = F_CPU / frequency / 2 / 128 - 1;
281 prescalarbits = 0b101;
286 ocr = F_CPU / frequency / 2 / 256 - 1;
287 prescalarbits = _timer == 0 ? 0b100 : 0b110;
290 // can't do any better than /1024
291 ocr = F_CPU / frequency / 2 / 1024 - 1;
292 prescalarbits = _timer == 0 ? 0b101 : 0b111;
301 TCCR0B = (TCCR0B & 0b11111000) | prescalarbits;
307 TCCR2B = (TCCR2B & 0b11111000) | prescalarbits;
311 // dummy place holder to make the above ifdefs work
317 // two choices for the 16 bit timers: ck/1 or ck/64
318 ocr = F_CPU / frequency / 2 - 1;
320 prescalarbits = 0b001;
323 ocr = F_CPU / frequency / 2 / 64 - 1;
324 prescalarbits = 0b011;
330 TCCR1B = (TCCR1B & 0b11111000) | prescalarbits;
334 else if (_timer == 3)
335 TCCR3B = (TCCR3B & 0b11111000) | prescalarbits;
338 else if (_timer == 4)
339 TCCR4B = (TCCR4B & 0b11111000) | prescalarbits;
342 else if (_timer == 5)
343 TCCR5B = (TCCR5B & 0b11111000) | prescalarbits;
349 // Calculate the toggle count
352 toggle_count = 2 * frequency * duration / 1000;
359 // Set the OCR for the given timer,
360 // set the toggle count,
361 // then turn on the interrupts
365 #if defined(OCR0A) && defined(TIMSK0) && defined(OCIE0A)
368 timer0_toggle_count = toggle_count;
369 bitWrite(TIMSK0, OCIE0A, 1);
374 #if defined(OCR1A) && defined(TIMSK1) && defined(OCIE1A)
376 timer1_toggle_count = toggle_count;
377 bitWrite(TIMSK1, OCIE1A, 1);
378 #elif defined(OCR1A) && defined(TIMSK) && defined(OCIE1A)
379 // this combination is for at least the ATmega32
381 timer1_toggle_count = toggle_count;
382 bitWrite(TIMSK, OCIE1A, 1);
386 #if defined(OCR2A) && defined(TIMSK2) && defined(OCIE2A)
389 timer2_toggle_count = toggle_count;
390 bitWrite(TIMSK2, OCIE2A, 1);
394 #if defined(OCR3A) && defined(TIMSK3) && defined(OCIE3A)
397 timer3_toggle_count = toggle_count;
398 bitWrite(TIMSK3, OCIE3A, 1);
402 #if defined(OCR4A) && defined(TIMSK4) && defined(OCIE4A)
405 timer4_toggle_count = toggle_count;
406 bitWrite(TIMSK4, OCIE4A, 1);
410 #if defined(OCR5A) && defined(TIMSK5) && defined(OCIE5A)
413 timer5_toggle_count = toggle_count;
414 bitWrite(TIMSK5, OCIE5A, 1);
423 // XXX: this function only works properly for timer 2 (the only one we use
424 // currently). for the others, it should end the tone, but won't restore
425 // proper PWM functionality for the timer.
426 void disableTimer(uint8_t _timer)
434 TIMSK = 0; // atmega32
438 #if defined(TIMSK1) && defined(OCIE1A)
440 bitWrite(TIMSK1, OCIE1A, 0);
445 #if defined(TIMSK2) && defined(OCIE2A)
446 bitWrite(TIMSK2, OCIE2A, 0); // disable interrupt
448 #if defined(TCCR2A) && defined(WGM20)
449 TCCR2A = (1 << WGM20);
451 #if defined(TCCR2B) && defined(CS22)
452 TCCR2B = (TCCR2B & 0b11111000) | (1 << CS22);
459 #if defined(TIMSK3) && defined(OCIE3A)
461 bitWrite(TIMSK3, OCIE3A, 0);
465 #if defined(TIMSK4) && defined(OCIE4A)
467 bitWrite(TIMSK4, OCIE4A, 0);
471 #if defined(TIMSK5) && defined(OCIE5A)
473 bitWrite(TIMSK5, OCIE5A, 0);
480 void noTone(uint8_t _pin)
484 for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
485 if (tone_pins[i] == _pin) {
486 _timer = pgm_read_byte(tone_pin_to_timer_PGM + i);
492 disableTimer(_timer);
494 digitalWrite(_pin, 0);
498 ISR(TIMER0_COMPA_vect)
500 if (timer0_toggle_count != 0)
503 *timer0_pin_port ^= timer0_pin_mask;
505 if (timer0_toggle_count > 0)
506 timer0_toggle_count--;
511 *timer0_pin_port &= ~(timer0_pin_mask); // keep pin low after stop
518 ISR(TIMER1_COMPA_vect)
520 if (timer1_toggle_count != 0)
523 *timer1_pin_port ^= timer1_pin_mask;
525 if (timer1_toggle_count > 0)
526 timer1_toggle_count--;
531 *timer1_pin_port &= ~(timer1_pin_mask); // keep pin low after stop
538 ISR(TIMER2_COMPA_vect)
541 if (timer2_toggle_count != 0)
544 *timer2_pin_port ^= timer2_pin_mask;
546 if (timer2_toggle_count > 0)
547 timer2_toggle_count--;
551 // need to call noTone() so that the tone_pins[] entry is reset, so the
552 // timer gets initialized next time we call tone().
553 // XXX: this assumes timer 2 is always the first one used.
554 noTone(tone_pins[0]);
556 // *timer2_pin_port &= ~(timer2_pin_mask); // keep pin low after stop
563 ISR(TIMER3_COMPA_vect)
565 if (timer3_toggle_count != 0)
568 *timer3_pin_port ^= timer3_pin_mask;
570 if (timer3_toggle_count > 0)
571 timer3_toggle_count--;
576 *timer3_pin_port &= ~(timer3_pin_mask); // keep pin low after stop
583 ISR(TIMER4_COMPA_vect)
585 if (timer4_toggle_count != 0)
588 *timer4_pin_port ^= timer4_pin_mask;
590 if (timer4_toggle_count > 0)
591 timer4_toggle_count--;
596 *timer4_pin_port &= ~(timer4_pin_mask); // keep pin low after stop
603 ISR(TIMER5_COMPA_vect)
605 if (timer5_toggle_count != 0)
608 *timer5_pin_port ^= timer5_pin_mask;
610 if (timer5_toggle_count > 0)
611 timer5_toggle_count--;
616 *timer5_pin_port &= ~(timer5_pin_mask); // keep pin low after stop