Tone.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. /* Tone.cpp
  2. A Tone Generator Library
  3. Written by Brett Hagman
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. Version Modified By Date Comments
  16. ------- ----------- -------- --------
  17. 0001 B Hagman 09/08/02 Initial coding
  18. 0002 B Hagman 09/08/18 Multiple pins
  19. 0003 B Hagman 09/08/18 Moved initialization from constructor to begin()
  20. 0004 B Hagman 09/09/26 Fixed problems with ATmega8
  21. 0005 B Hagman 09/11/23 Scanned prescalars for best fit on 8 bit timers
  22. 09/11/25 Changed pin toggle method to XOR
  23. 09/11/25 Fixed timer0 from being excluded
  24. 0006 D Mellis 09/12/29 Replaced objects with functions
  25. 0007 M Sproul 10/08/29 Changed #ifdefs from cpu to register
  26. 0008 S Kanemoto 12/06/22 Fixed for Leonardo by @maris_HY
  27. *************************************************/
  28. #include <avr/interrupt.h>
  29. #include <avr/pgmspace.h>
  30. #include "Arduino.h"
  31. #include "pins_arduino.h"
  32. #if defined(__AVR_ATmega8__) || defined(__AVR_ATmega128__)
  33. #define TCCR2A TCCR2
  34. #define TCCR2B TCCR2
  35. #define COM2A1 COM21
  36. #define COM2A0 COM20
  37. #define OCR2A OCR2
  38. #define TIMSK2 TIMSK
  39. #define OCIE2A OCIE2
  40. #define TIMER2_COMPA_vect TIMER2_COMP_vect
  41. #define TIMSK1 TIMSK
  42. #endif
  43. // timerx_toggle_count:
  44. // > 0 - duration specified
  45. // = 0 - stopped
  46. // < 0 - infinitely (until stop() method called, or new play() called)
  47. #if !defined(__AVR_ATmega8__)
  48. volatile long timer0_toggle_count;
  49. volatile uint8_t *timer0_pin_port;
  50. volatile uint8_t timer0_pin_mask;
  51. #endif
  52. volatile long timer1_toggle_count;
  53. volatile uint8_t *timer1_pin_port;
  54. volatile uint8_t timer1_pin_mask;
  55. volatile long timer2_toggle_count;
  56. volatile uint8_t *timer2_pin_port;
  57. volatile uint8_t timer2_pin_mask;
  58. #if defined(TIMSK3)
  59. volatile long timer3_toggle_count;
  60. volatile uint8_t *timer3_pin_port;
  61. volatile uint8_t timer3_pin_mask;
  62. #endif
  63. #if defined(TIMSK4)
  64. volatile long timer4_toggle_count;
  65. volatile uint8_t *timer4_pin_port;
  66. volatile uint8_t timer4_pin_mask;
  67. #endif
  68. #if defined(TIMSK5)
  69. volatile long timer5_toggle_count;
  70. volatile uint8_t *timer5_pin_port;
  71. volatile uint8_t timer5_pin_mask;
  72. #endif
  73. #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  74. #define AVAILABLE_TONE_PINS 1
  75. #define USE_TIMER2
  76. const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 2 /*, 3, 4, 5, 1, 0 */ };
  77. static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255, 255, 255, 255, 255 */ };
  78. #elif defined(__AVR_ATmega8__)
  79. #define AVAILABLE_TONE_PINS 1
  80. #define USE_TIMER2
  81. const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 2 /*, 1 */ };
  82. static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255 */ };
  83. #elif defined(__AVR_ATmega32U4__)
  84. #define AVAILABLE_TONE_PINS 1
  85. #define USE_TIMER3
  86. const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 3 /*, 1 */ };
  87. static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255 */ };
  88. #else
  89. #define AVAILABLE_TONE_PINS 1
  90. #define USE_TIMER2
  91. // Leave timer 0 to last.
  92. const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 2 /*, 1, 0 */ };
  93. static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255, 255 */ };
  94. #endif
  95. static int8_t toneBegin(uint8_t _pin)
  96. {
  97. int8_t _timer = -1;
  98. // if we're already using the pin, the timer should be configured.
  99. for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
  100. if (tone_pins[i] == _pin) {
  101. return pgm_read_byte(tone_pin_to_timer_PGM + i);
  102. }
  103. }
  104. // search for an unused timer.
  105. for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
  106. if (tone_pins[i] == 255) {
  107. tone_pins[i] = _pin;
  108. _timer = pgm_read_byte(tone_pin_to_timer_PGM + i);
  109. break;
  110. }
  111. }
  112. if (_timer != -1)
  113. {
  114. // Set timer specific stuff
  115. // All timers in CTC mode
  116. // 8 bit timers will require changing prescalar values,
  117. // whereas 16 bit timers are set to either ck/1 or ck/64 prescalar
  118. switch (_timer)
  119. {
  120. #if defined(TCCR0A) && defined(TCCR0B)
  121. case 0:
  122. // 8 bit timer
  123. TCCR0A = 0;
  124. TCCR0B = 0;
  125. bitWrite(TCCR0A, WGM01, 1);
  126. bitWrite(TCCR0B, CS00, 1);
  127. timer0_pin_port = portOutputRegister(digitalPinToPort(_pin));
  128. timer0_pin_mask = digitalPinToBitMask(_pin);
  129. break;
  130. #endif
  131. #if defined(TCCR1A) && defined(TCCR1B) && defined(WGM12)
  132. case 1:
  133. // 16 bit timer
  134. TCCR1A = 0;
  135. TCCR1B = 0;
  136. bitWrite(TCCR1B, WGM12, 1);
  137. bitWrite(TCCR1B, CS10, 1);
  138. timer1_pin_port = portOutputRegister(digitalPinToPort(_pin));
  139. timer1_pin_mask = digitalPinToBitMask(_pin);
  140. break;
  141. #endif
  142. #if defined(TCCR2A) && defined(TCCR2B)
  143. case 2:
  144. // 8 bit timer
  145. TCCR2A = 0;
  146. TCCR2B = 0;
  147. bitWrite(TCCR2A, WGM21, 1);
  148. bitWrite(TCCR2B, CS20, 1);
  149. timer2_pin_port = portOutputRegister(digitalPinToPort(_pin));
  150. timer2_pin_mask = digitalPinToBitMask(_pin);
  151. break;
  152. #endif
  153. #if defined(TCCR3A) && defined(TCCR3B) && defined(TIMSK3)
  154. case 3:
  155. // 16 bit timer
  156. TCCR3A = 0;
  157. TCCR3B = 0;
  158. bitWrite(TCCR3B, WGM32, 1);
  159. bitWrite(TCCR3B, CS30, 1);
  160. timer3_pin_port = portOutputRegister(digitalPinToPort(_pin));
  161. timer3_pin_mask = digitalPinToBitMask(_pin);
  162. break;
  163. #endif
  164. #if defined(TCCR4A) && defined(TCCR4B) && defined(TIMSK4)
  165. case 4:
  166. // 16 bit timer
  167. TCCR4A = 0;
  168. TCCR4B = 0;
  169. #if defined(WGM42)
  170. bitWrite(TCCR4B, WGM42, 1);
  171. #elif defined(CS43)
  172. #warning this may not be correct
  173. // atmega32u4
  174. bitWrite(TCCR4B, CS43, 1);
  175. #endif
  176. bitWrite(TCCR4B, CS40, 1);
  177. timer4_pin_port = portOutputRegister(digitalPinToPort(_pin));
  178. timer4_pin_mask = digitalPinToBitMask(_pin);
  179. break;
  180. #endif
  181. #if defined(TCCR5A) && defined(TCCR5B) && defined(TIMSK5)
  182. case 5:
  183. // 16 bit timer
  184. TCCR5A = 0;
  185. TCCR5B = 0;
  186. bitWrite(TCCR5B, WGM52, 1);
  187. bitWrite(TCCR5B, CS50, 1);
  188. timer5_pin_port = portOutputRegister(digitalPinToPort(_pin));
  189. timer5_pin_mask = digitalPinToBitMask(_pin);
  190. break;
  191. #endif
  192. }
  193. }
  194. return _timer;
  195. }
  196. // frequency (in hertz) and duration (in milliseconds).
  197. void tone(uint8_t _pin, unsigned int frequency, unsigned long duration)
  198. {
  199. uint8_t prescalarbits = 0b001;
  200. long toggle_count = 0;
  201. uint32_t ocr = 0;
  202. int8_t _timer;
  203. _timer = toneBegin(_pin);
  204. if (_timer >= 0)
  205. {
  206. // Set the pinMode as OUTPUT
  207. pinMode(_pin, OUTPUT);
  208. // if we are using an 8 bit timer, scan through prescalars to find the best fit
  209. if (_timer == 0 || _timer == 2)
  210. {
  211. ocr = F_CPU / frequency / 2 - 1;
  212. prescalarbits = 0b001; // ck/1: same for both timers
  213. if (ocr > 255)
  214. {
  215. ocr = F_CPU / frequency / 2 / 8 - 1;
  216. prescalarbits = 0b010; // ck/8: same for both timers
  217. if (_timer == 2 && ocr > 255)
  218. {
  219. ocr = F_CPU / frequency / 2 / 32 - 1;
  220. prescalarbits = 0b011;
  221. }
  222. if (ocr > 255)
  223. {
  224. ocr = F_CPU / frequency / 2 / 64 - 1;
  225. prescalarbits = _timer == 0 ? 0b011 : 0b100;
  226. if (_timer == 2 && ocr > 255)
  227. {
  228. ocr = F_CPU / frequency / 2 / 128 - 1;
  229. prescalarbits = 0b101;
  230. }
  231. if (ocr > 255)
  232. {
  233. ocr = F_CPU / frequency / 2 / 256 - 1;
  234. prescalarbits = _timer == 0 ? 0b100 : 0b110;
  235. if (ocr > 255)
  236. {
  237. // can't do any better than /1024
  238. ocr = F_CPU / frequency / 2 / 1024 - 1;
  239. prescalarbits = _timer == 0 ? 0b101 : 0b111;
  240. }
  241. }
  242. }
  243. }
  244. #if defined(TCCR0B)
  245. if (_timer == 0)
  246. {
  247. TCCR0B = prescalarbits;
  248. }
  249. else
  250. #endif
  251. #if defined(TCCR2B)
  252. {
  253. TCCR2B = prescalarbits;
  254. }
  255. #else
  256. {
  257. // dummy place holder to make the above ifdefs work
  258. }
  259. #endif
  260. }
  261. else
  262. {
  263. // two choices for the 16 bit timers: ck/1 or ck/64
  264. ocr = F_CPU / frequency / 2 - 1;
  265. prescalarbits = 0b001;
  266. if (ocr > 0xffff)
  267. {
  268. ocr = F_CPU / frequency / 2 / 64 - 1;
  269. prescalarbits = 0b011;
  270. }
  271. if (_timer == 1)
  272. {
  273. #if defined(TCCR1B)
  274. TCCR1B = (TCCR1B & 0b11111000) | prescalarbits;
  275. #endif
  276. }
  277. #if defined(TCCR3B)
  278. else if (_timer == 3)
  279. TCCR3B = (TCCR3B & 0b11111000) | prescalarbits;
  280. #endif
  281. #if defined(TCCR4B)
  282. else if (_timer == 4)
  283. TCCR4B = (TCCR4B & 0b11111000) | prescalarbits;
  284. #endif
  285. #if defined(TCCR5B)
  286. else if (_timer == 5)
  287. TCCR5B = (TCCR5B & 0b11111000) | prescalarbits;
  288. #endif
  289. }
  290. // Calculate the toggle count
  291. if (duration > 0)
  292. {
  293. toggle_count = 2 * frequency * duration / 1000;
  294. }
  295. else
  296. {
  297. toggle_count = -1;
  298. }
  299. // Set the OCR for the given timer,
  300. // set the toggle count,
  301. // then turn on the interrupts
  302. switch (_timer)
  303. {
  304. #if defined(OCR0A) && defined(TIMSK0) && defined(OCIE0A)
  305. case 0:
  306. OCR0A = ocr;
  307. timer0_toggle_count = toggle_count;
  308. bitWrite(TIMSK0, OCIE0A, 1);
  309. break;
  310. #endif
  311. case 1:
  312. #if defined(OCR1A) && defined(TIMSK1) && defined(OCIE1A)
  313. OCR1A = ocr;
  314. timer1_toggle_count = toggle_count;
  315. bitWrite(TIMSK1, OCIE1A, 1);
  316. #elif defined(OCR1A) && defined(TIMSK) && defined(OCIE1A)
  317. // this combination is for at least the ATmega32
  318. OCR1A = ocr;
  319. timer1_toggle_count = toggle_count;
  320. bitWrite(TIMSK, OCIE1A, 1);
  321. #endif
  322. break;
  323. #if defined(OCR2A) && defined(TIMSK2) && defined(OCIE2A)
  324. case 2:
  325. OCR2A = ocr;
  326. timer2_toggle_count = toggle_count;
  327. bitWrite(TIMSK2, OCIE2A, 1);
  328. break;
  329. #endif
  330. #if defined(TIMSK3)
  331. case 3:
  332. OCR3A = ocr;
  333. timer3_toggle_count = toggle_count;
  334. bitWrite(TIMSK3, OCIE3A, 1);
  335. break;
  336. #endif
  337. #if defined(TIMSK4)
  338. case 4:
  339. OCR4A = ocr;
  340. timer4_toggle_count = toggle_count;
  341. bitWrite(TIMSK4, OCIE4A, 1);
  342. break;
  343. #endif
  344. #if defined(OCR5A) && defined(TIMSK5) && defined(OCIE5A)
  345. case 5:
  346. OCR5A = ocr;
  347. timer5_toggle_count = toggle_count;
  348. bitWrite(TIMSK5, OCIE5A, 1);
  349. break;
  350. #endif
  351. }
  352. }
  353. }
  354. // XXX: this function only works properly for timer 2 (the only one we use
  355. // currently). for the others, it should end the tone, but won't restore
  356. // proper PWM functionality for the timer.
  357. void disableTimer(uint8_t _timer)
  358. {
  359. switch (_timer)
  360. {
  361. case 0:
  362. #if defined(TIMSK0)
  363. TIMSK0 = 0;
  364. #elif defined(TIMSK)
  365. TIMSK = 0; // atmega32
  366. #endif
  367. break;
  368. #if defined(TIMSK1) && defined(OCIE1A)
  369. case 1:
  370. bitWrite(TIMSK1, OCIE1A, 0);
  371. break;
  372. #endif
  373. case 2:
  374. #if defined(TIMSK2) && defined(OCIE2A)
  375. bitWrite(TIMSK2, OCIE2A, 0); // disable interrupt
  376. #endif
  377. #if defined(TCCR2A) && defined(WGM20)
  378. TCCR2A = (1 << WGM20);
  379. #endif
  380. #if defined(TCCR2B) && defined(CS22)
  381. TCCR2B = (TCCR2B & 0b11111000) | (1 << CS22);
  382. #endif
  383. #if defined(OCR2A)
  384. OCR2A = 0;
  385. #endif
  386. break;
  387. #if defined(TIMSK3)
  388. case 3:
  389. TIMSK3 = 0;
  390. break;
  391. #endif
  392. #if defined(TIMSK4)
  393. case 4:
  394. TIMSK4 = 0;
  395. break;
  396. #endif
  397. #if defined(TIMSK5)
  398. case 5:
  399. TIMSK5 = 0;
  400. break;
  401. #endif
  402. }
  403. }
  404. void noTone(uint8_t _pin)
  405. {
  406. int8_t _timer = -1;
  407. for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
  408. if (tone_pins[i] == _pin) {
  409. _timer = pgm_read_byte(tone_pin_to_timer_PGM + i);
  410. tone_pins[i] = 255;
  411. }
  412. }
  413. disableTimer(_timer);
  414. digitalWrite(_pin, 0);
  415. }
  416. #ifdef USE_TIMER0
  417. ISR(TIMER0_COMPA_vect)
  418. {
  419. if (timer0_toggle_count != 0)
  420. {
  421. // toggle the pin
  422. *timer0_pin_port ^= timer0_pin_mask;
  423. if (timer0_toggle_count > 0)
  424. timer0_toggle_count--;
  425. }
  426. else
  427. {
  428. disableTimer(0);
  429. *timer0_pin_port &= ~(timer0_pin_mask); // keep pin low after stop
  430. }
  431. }
  432. #endif
  433. #ifdef USE_TIMER1
  434. ISR(TIMER1_COMPA_vect)
  435. {
  436. if (timer1_toggle_count != 0)
  437. {
  438. // toggle the pin
  439. *timer1_pin_port ^= timer1_pin_mask;
  440. if (timer1_toggle_count > 0)
  441. timer1_toggle_count--;
  442. }
  443. else
  444. {
  445. disableTimer(1);
  446. *timer1_pin_port &= ~(timer1_pin_mask); // keep pin low after stop
  447. }
  448. }
  449. #endif
  450. #ifdef USE_TIMER2
  451. ISR(TIMER2_COMPA_vect)
  452. {
  453. if (timer2_toggle_count != 0)
  454. {
  455. // toggle the pin
  456. *timer2_pin_port ^= timer2_pin_mask;
  457. if (timer2_toggle_count > 0)
  458. timer2_toggle_count--;
  459. }
  460. else
  461. {
  462. // need to call noTone() so that the tone_pins[] entry is reset, so the
  463. // timer gets initialized next time we call tone().
  464. // XXX: this assumes timer 2 is always the first one used.
  465. noTone(tone_pins[0]);
  466. // disableTimer(2);
  467. // *timer2_pin_port &= ~(timer2_pin_mask); // keep pin low after stop
  468. }
  469. }
  470. #endif
  471. #ifdef USE_TIMER3
  472. ISR(TIMER3_COMPA_vect)
  473. {
  474. if (timer3_toggle_count != 0)
  475. {
  476. // toggle the pin
  477. *timer3_pin_port ^= timer3_pin_mask;
  478. if (timer3_toggle_count > 0)
  479. timer3_toggle_count--;
  480. }
  481. else
  482. {
  483. disableTimer(3);
  484. *timer3_pin_port &= ~(timer3_pin_mask); // keep pin low after stop
  485. }
  486. }
  487. #endif
  488. #ifdef USE_TIMER4
  489. ISR(TIMER4_COMPA_vect)
  490. {
  491. if (timer4_toggle_count != 0)
  492. {
  493. // toggle the pin
  494. *timer4_pin_port ^= timer4_pin_mask;
  495. if (timer4_toggle_count > 0)
  496. timer4_toggle_count--;
  497. }
  498. else
  499. {
  500. disableTimer(4);
  501. *timer4_pin_port &= ~(timer4_pin_mask); // keep pin low after stop
  502. }
  503. }
  504. #endif
  505. #ifdef USE_TIMER5
  506. ISR(TIMER5_COMPA_vect)
  507. {
  508. if (timer5_toggle_count != 0)
  509. {
  510. // toggle the pin
  511. *timer5_pin_port ^= timer5_pin_mask;
  512. if (timer5_toggle_count > 0)
  513. timer5_toggle_count--;
  514. }
  515. else
  516. {
  517. disableTimer(5);
  518. *timer5_pin_port &= ~(timer5_pin_mask); // keep pin low after stop
  519. }
  520. }
  521. #endif