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 *************************************************/
34 #include <avr/interrupt.h>
35 #include <avr/pgmspace.h>
37 #include "pins_arduino.h"
39 #if defined(__AVR_ATmega8__) || defined(__AVR_ATmega128__)
47 #define TIMER2_COMPA_vect TIMER2_COMP_vect
51 // timerx_toggle_count:
52 // > 0 - duration specified
54 // < 0 - infinitely (until stop() method called, or new play() called)
56 #if !defined(__AVR_ATmega8__)
57 volatile long timer0_toggle_count;
58 volatile uint8_t *timer0_pin_port;
59 volatile uint8_t timer0_pin_mask;
62 volatile long timer1_toggle_count;
63 volatile uint8_t *timer1_pin_port;
64 volatile uint8_t timer1_pin_mask;
65 volatile long timer2_toggle_count;
66 volatile uint8_t *timer2_pin_port;
67 volatile uint8_t timer2_pin_mask;
70 volatile long timer3_toggle_count;
71 volatile uint8_t *timer3_pin_port;
72 volatile uint8_t timer3_pin_mask;
76 volatile long timer4_toggle_count;
77 volatile uint8_t *timer4_pin_port;
78 volatile uint8_t timer4_pin_mask;
82 volatile long timer5_toggle_count;
83 volatile uint8_t *timer5_pin_port;
84 volatile uint8_t timer5_pin_mask;
88 // MLS: This does not make sense, the 3 options are the same
89 #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
91 #define AVAILABLE_TONE_PINS 1
93 const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 2 /*, 3, 4, 5, 1, 0 */ };
94 static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255, 255, 255, 255, 255 */ };
96 #elif defined(__AVR_ATmega8__)
98 #define AVAILABLE_TONE_PINS 1
100 const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 2 /*, 1 */ };
101 static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255 */ };
105 #define AVAILABLE_TONE_PINS 1
107 // Leave timer 0 to last.
108 const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 2 /*, 1, 0 */ };
109 static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255, 255 */ };
115 static int8_t toneBegin(uint8_t _pin)
119 // if we're already using the pin, the timer should be configured.
120 for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
121 if (tone_pins[i] == _pin) {
122 return pgm_read_byte(tone_pin_to_timer_PGM + i);
126 // search for an unused timer.
127 for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
128 if (tone_pins[i] == 255) {
130 _timer = pgm_read_byte(tone_pin_to_timer_PGM + i);
137 // Set timer specific stuff
138 // All timers in CTC mode
139 // 8 bit timers will require changing prescalar values,
140 // whereas 16 bit timers are set to either ck/1 or ck/64 prescalar
143 #if defined(TCCR0A) && defined(TCCR0B)
148 bitWrite(TCCR0A, WGM01, 1);
149 bitWrite(TCCR0B, CS00, 1);
150 timer0_pin_port = portOutputRegister(digitalPinToPort(_pin));
151 timer0_pin_mask = digitalPinToBitMask(_pin);
155 #if defined(TCCR1A) && defined(TCCR1B) && defined(WGM12)
160 bitWrite(TCCR1B, WGM12, 1);
161 bitWrite(TCCR1B, CS10, 1);
162 timer1_pin_port = portOutputRegister(digitalPinToPort(_pin));
163 timer1_pin_mask = digitalPinToBitMask(_pin);
167 #if defined(TCCR2A) && defined(TCCR2B)
172 bitWrite(TCCR2A, WGM21, 1);
173 bitWrite(TCCR2B, CS20, 1);
174 timer2_pin_port = portOutputRegister(digitalPinToPort(_pin));
175 timer2_pin_mask = digitalPinToBitMask(_pin);
179 #if defined(TCCR3A) && defined(TCCR3B) && defined(TIMSK3)
184 bitWrite(TCCR3B, WGM32, 1);
185 bitWrite(TCCR3B, CS30, 1);
186 timer3_pin_port = portOutputRegister(digitalPinToPort(_pin));
187 timer3_pin_mask = digitalPinToBitMask(_pin);
191 #if defined(TCCR4A) && defined(TCCR4B) && defined(TIMSK4)
197 bitWrite(TCCR4B, WGM42, 1);
199 #warning this may not be correct
201 bitWrite(TCCR4B, CS43, 1);
203 bitWrite(TCCR4B, CS40, 1);
204 timer4_pin_port = portOutputRegister(digitalPinToPort(_pin));
205 timer4_pin_mask = digitalPinToBitMask(_pin);
209 #if defined(TCCR5A) && defined(TCCR5B) && defined(TIMSK5)
214 bitWrite(TCCR5B, WGM52, 1);
215 bitWrite(TCCR5B, CS50, 1);
216 timer5_pin_port = portOutputRegister(digitalPinToPort(_pin));
217 timer5_pin_mask = digitalPinToBitMask(_pin);
228 // frequency (in hertz) and duration (in milliseconds).
230 void tone(uint8_t _pin, unsigned int frequency, unsigned long duration)
232 uint8_t prescalarbits = 0b001;
233 long toggle_count = 0;
237 _timer = toneBegin(_pin);
241 // Set the pinMode as OUTPUT
242 pinMode(_pin, OUTPUT);
244 // if we are using an 8 bit timer, scan through prescalars to find the best fit
245 if (_timer == 0 || _timer == 2)
247 ocr = F_CPU / frequency / 2 - 1;
248 prescalarbits = 0b001; // ck/1: same for both timers
251 ocr = F_CPU / frequency / 2 / 8 - 1;
252 prescalarbits = 0b010; // ck/8: same for both timers
254 if (_timer == 2 && ocr > 255)
256 ocr = F_CPU / frequency / 2 / 32 - 1;
257 prescalarbits = 0b011;
262 ocr = F_CPU / frequency / 2 / 64 - 1;
263 prescalarbits = _timer == 0 ? 0b011 : 0b100;
265 if (_timer == 2 && ocr > 255)
267 ocr = F_CPU / frequency / 2 / 128 - 1;
268 prescalarbits = 0b101;
273 ocr = F_CPU / frequency / 2 / 256 - 1;
274 prescalarbits = _timer == 0 ? 0b100 : 0b110;
277 // can't do any better than /1024
278 ocr = F_CPU / frequency / 2 / 1024 - 1;
279 prescalarbits = _timer == 0 ? 0b101 : 0b111;
288 TCCR0B = prescalarbits;
294 TCCR2B = prescalarbits;
298 // dummy place holder to make the above ifdefs work
304 // two choices for the 16 bit timers: ck/1 or ck/64
305 ocr = F_CPU / frequency / 2 - 1;
307 prescalarbits = 0b001;
310 ocr = F_CPU / frequency / 2 / 64 - 1;
311 prescalarbits = 0b011;
317 TCCR1B = (TCCR1B & 0b11111000) | prescalarbits;
321 else if (_timer == 3)
322 TCCR3B = (TCCR3B & 0b11111000) | prescalarbits;
325 else if (_timer == 4)
326 TCCR4B = (TCCR4B & 0b11111000) | prescalarbits;
329 else if (_timer == 5)
330 TCCR5B = (TCCR5B & 0b11111000) | prescalarbits;
336 // Calculate the toggle count
339 toggle_count = 2 * frequency * duration / 1000;
346 // Set the OCR for the given timer,
347 // set the toggle count,
348 // then turn on the interrupts
352 #if defined(OCR0A) && defined(TIMSK0) && defined(OCIE0A)
355 timer0_toggle_count = toggle_count;
356 bitWrite(TIMSK0, OCIE0A, 1);
361 #if defined(OCR1A) && defined(TIMSK1) && defined(OCIE1A)
363 timer1_toggle_count = toggle_count;
364 bitWrite(TIMSK1, OCIE1A, 1);
365 #elif defined(OCR1A) && defined(TIMSK) && defined(OCIE1A)
366 // this combination is for at least the ATmega32
368 timer1_toggle_count = toggle_count;
369 bitWrite(TIMSK, OCIE1A, 1);
373 #if defined(OCR2A) && defined(TIMSK2) && defined(OCIE2A)
376 timer2_toggle_count = toggle_count;
377 bitWrite(TIMSK2, OCIE2A, 1);
384 timer3_toggle_count = toggle_count;
385 bitWrite(TIMSK3, OCIE3A, 1);
392 timer4_toggle_count = toggle_count;
393 bitWrite(TIMSK4, OCIE4A, 1);
397 #if defined(OCR5A) && defined(TIMSK5) && defined(OCIE5A)
400 timer5_toggle_count = toggle_count;
401 bitWrite(TIMSK5, OCIE5A, 1);
410 // XXX: this function only works properly for timer 2 (the only one we use
411 // currently). for the others, it should end the tone, but won't restore
412 // proper PWM functionality for the timer.
413 void disableTimer(uint8_t _timer)
421 TIMSK = 0; // atmega32
425 #if defined(TIMSK1) && defined(OCIE1A)
427 bitWrite(TIMSK1, OCIE1A, 0);
432 #if defined(TIMSK2) && defined(OCIE2A)
433 bitWrite(TIMSK2, OCIE2A, 0); // disable interrupt
435 #if defined(TCCR2A) && defined(WGM20)
436 TCCR2A = (1 << WGM20);
438 #if defined(TCCR2B) && defined(CS22)
439 TCCR2B = (TCCR2B & 0b11111000) | (1 << CS22);
467 void noTone(uint8_t _pin)
471 for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
472 if (tone_pins[i] == _pin) {
473 _timer = pgm_read_byte(tone_pin_to_timer_PGM + i);
478 disableTimer(_timer);
480 digitalWrite(_pin, 0);
484 #if !defined(__AVR_ATmega8__)
485 ISR(TIMER0_COMPA_vect)
487 if (timer0_toggle_count != 0)
490 *timer0_pin_port ^= timer0_pin_mask;
492 if (timer0_toggle_count > 0)
493 timer0_toggle_count--;
498 *timer0_pin_port &= ~(timer0_pin_mask); // keep pin low after stop
504 ISR(TIMER1_COMPA_vect)
506 if (timer1_toggle_count != 0)
509 *timer1_pin_port ^= timer1_pin_mask;
511 if (timer1_toggle_count > 0)
512 timer1_toggle_count--;
517 *timer1_pin_port &= ~(timer1_pin_mask); // keep pin low after stop
523 ISR(TIMER2_COMPA_vect)
526 if (timer2_toggle_count != 0)
529 *timer2_pin_port ^= timer2_pin_mask;
531 if (timer2_toggle_count > 0)
532 timer2_toggle_count--;
536 // need to call noTone() so that the tone_pins[] entry is reset, so the
537 // timer gets initialized next time we call tone().
538 // XXX: this assumes timer 2 is always the first one used.
539 noTone(tone_pins[0]);
541 // *timer2_pin_port &= ~(timer2_pin_mask); // keep pin low after stop
547 //#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
550 ISR(TIMER3_COMPA_vect)
552 if (timer3_toggle_count != 0)
555 *timer3_pin_port ^= timer3_pin_mask;
557 if (timer3_toggle_count > 0)
558 timer3_toggle_count--;
563 *timer3_pin_port &= ~(timer3_pin_mask); // keep pin low after stop
567 ISR(TIMER4_COMPA_vect)
569 if (timer4_toggle_count != 0)
572 *timer4_pin_port ^= timer4_pin_mask;
574 if (timer4_toggle_count > 0)
575 timer4_toggle_count--;
580 *timer4_pin_port &= ~(timer4_pin_mask); // keep pin low after stop
584 ISR(TIMER5_COMPA_vect)
586 if (timer5_toggle_count != 0)
589 *timer5_pin_port ^= timer5_pin_mask;
591 if (timer5_toggle_count > 0)
592 timer5_toggle_count--;
597 *timer5_pin_port &= ~(timer5_pin_mask); // keep pin low after stop