Tone.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  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(TCCR2) // ATmega8, ATmega128 or similar
  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(TIMSK0)
  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. // Don't distinguish by chip type, but by register presence
  74. #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  75. #define AVAILABLE_TONE_PINS 1
  76. #define USE_TIMER2
  77. const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 2 /*, 3, 4, 5, 1, 0 */ };
  78. static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255, 255, 255, 255, 255 */ };
  79. #elif defined(__AVR_ATmega8__)
  80. #define AVAILABLE_TONE_PINS 1
  81. #define USE_TIMER2
  82. const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 2 /*, 1 */ };
  83. static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255 */ };
  84. #elif defined(__AVR_ATmega32U4__)
  85. #define AVAILABLE_TONE_PINS 1
  86. #define USE_TIMER3
  87. const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 3 /*, 1 */ };
  88. static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255 */ };
  89. #else
  90. #define AVAILABLE_TONE_PINS 1
  91. #define USE_TIMER2
  92. // Leave timer 0 to last.
  93. const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 2 /*, 1, 0 */ };
  94. static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255, 255 */ };
  95. #endif
  96. static int8_t toneBegin(uint8_t _pin)
  97. {
  98. int8_t _timer = -1;
  99. // if we're already using the pin, the timer should be configured.
  100. for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
  101. if (tone_pins[i] == _pin) {
  102. return pgm_read_byte(tone_pin_to_timer_PGM + i);
  103. }
  104. }
  105. // search for an unused timer.
  106. for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
  107. if (tone_pins[i] == 255) {
  108. tone_pins[i] = _pin;
  109. _timer = pgm_read_byte(tone_pin_to_timer_PGM + i);
  110. break;
  111. }
  112. }
  113. if (_timer != -1)
  114. {
  115. // Set timer specific stuff
  116. // All timers in CTC mode
  117. // 8 bit timers will require changing prescalar values,
  118. // whereas 16 bit timers are set to either ck/1 or ck/64 prescalar
  119. switch (_timer)
  120. {
  121. #if defined(TCCR0A) && defined(TCCR0B)
  122. case 0:
  123. // 8 bit timer
  124. TCCR0A = 0;
  125. TCCR0B = 0;
  126. bitWrite(TCCR0A, WGM01, 1);
  127. bitWrite(TCCR0B, CS00, 1);
  128. timer0_pin_port = portOutputRegister(digitalPinToPort(_pin));
  129. timer0_pin_mask = digitalPinToBitMask(_pin);
  130. break;
  131. #endif
  132. #if defined(TCCR1A) && defined(TCCR1B) && defined(WGM12)
  133. case 1:
  134. // 16 bit timer
  135. TCCR1A = 0;
  136. TCCR1B = 0;
  137. bitWrite(TCCR1B, WGM12, 1);
  138. bitWrite(TCCR1B, CS10, 1);
  139. timer1_pin_port = portOutputRegister(digitalPinToPort(_pin));
  140. timer1_pin_mask = digitalPinToBitMask(_pin);
  141. break;
  142. #endif
  143. #if defined(TCCR2A) && defined(TCCR2B)
  144. case 2:
  145. // 8 bit timer
  146. TCCR2A = 0;
  147. TCCR2B = 0;
  148. bitWrite(TCCR2A, WGM21, 1);
  149. bitWrite(TCCR2B, CS20, 1);
  150. timer2_pin_port = portOutputRegister(digitalPinToPort(_pin));
  151. timer2_pin_mask = digitalPinToBitMask(_pin);
  152. break;
  153. #endif
  154. #if defined(TCCR3A) && defined(TCCR3B) && defined(TIMSK3)
  155. case 3:
  156. // 16 bit timer
  157. TCCR3A = 0;
  158. TCCR3B = 0;
  159. bitWrite(TCCR3B, WGM32, 1);
  160. bitWrite(TCCR3B, CS30, 1);
  161. timer3_pin_port = portOutputRegister(digitalPinToPort(_pin));
  162. timer3_pin_mask = digitalPinToBitMask(_pin);
  163. break;
  164. #endif
  165. #if defined(TCCR4A) && defined(TCCR4B) && defined(TIMSK4)
  166. case 4:
  167. // 16 bit timer
  168. TCCR4A = 0;
  169. TCCR4B = 0;
  170. #if defined(WGM42)
  171. bitWrite(TCCR4B, WGM42, 1);
  172. #elif defined(CS43)
  173. #warning this may not be correct
  174. // atmega32u4
  175. bitWrite(TCCR4B, CS43, 1);
  176. #endif
  177. bitWrite(TCCR4B, CS40, 1);
  178. timer4_pin_port = portOutputRegister(digitalPinToPort(_pin));
  179. timer4_pin_mask = digitalPinToBitMask(_pin);
  180. break;
  181. #endif
  182. #if defined(TCCR5A) && defined(TCCR5B) && defined(TIMSK5)
  183. case 5:
  184. // 16 bit timer
  185. TCCR5A = 0;
  186. TCCR5B = 0;
  187. bitWrite(TCCR5B, WGM52, 1);
  188. bitWrite(TCCR5B, CS50, 1);
  189. timer5_pin_port = portOutputRegister(digitalPinToPort(_pin));
  190. timer5_pin_mask = digitalPinToBitMask(_pin);
  191. break;
  192. #endif
  193. }
  194. }
  195. return _timer;
  196. }
  197. // frequency (in hertz) and duration (in milliseconds).
  198. void tone(uint8_t _pin, unsigned int frequency, unsigned long duration)
  199. {
  200. uint8_t prescalarbits = 0b001;
  201. long toggle_count = 0;
  202. uint32_t ocr = 0;
  203. int8_t _timer;
  204. _timer = toneBegin(_pin);
  205. if (_timer >= 0)
  206. {
  207. // Set the pinMode as OUTPUT
  208. pinMode(_pin, OUTPUT);
  209. // if we are using an 8 bit timer, scan through prescalars to find the best fit
  210. if (_timer == 0 || _timer == 2)
  211. {
  212. ocr = F_CPU / frequency / 2 - 1;
  213. prescalarbits = 0b001; // ck/1: same for both timers
  214. if (ocr > 255)
  215. {
  216. ocr = F_CPU / frequency / 2 / 8 - 1;
  217. prescalarbits = 0b010; // ck/8: same for both timers
  218. if (_timer == 2 && ocr > 255)
  219. {
  220. ocr = F_CPU / frequency / 2 / 32 - 1;
  221. prescalarbits = 0b011;
  222. }
  223. if (ocr > 255)
  224. {
  225. ocr = F_CPU / frequency / 2 / 64 - 1;
  226. prescalarbits = _timer == 0 ? 0b011 : 0b100;
  227. if (_timer == 2 && ocr > 255)
  228. {
  229. ocr = F_CPU / frequency / 2 / 128 - 1;
  230. prescalarbits = 0b101;
  231. }
  232. if (ocr > 255)
  233. {
  234. ocr = F_CPU / frequency / 2 / 256 - 1;
  235. prescalarbits = _timer == 0 ? 0b100 : 0b110;
  236. if (ocr > 255)
  237. {
  238. // can't do any better than /1024
  239. ocr = F_CPU / frequency / 2 / 1024 - 1;
  240. prescalarbits = _timer == 0 ? 0b101 : 0b111;
  241. }
  242. }
  243. }
  244. }
  245. #if defined(TCCR0B)
  246. if (_timer == 0)
  247. {
  248. TCCR0B = prescalarbits;
  249. }
  250. else
  251. #endif
  252. #if defined(TCCR2B)
  253. {
  254. TCCR2B = prescalarbits;
  255. }
  256. #else
  257. {
  258. // dummy place holder to make the above ifdefs work
  259. }
  260. #endif
  261. }
  262. else
  263. {
  264. // two choices for the 16 bit timers: ck/1 or ck/64
  265. ocr = F_CPU / frequency / 2 - 1;
  266. prescalarbits = 0b001;
  267. if (ocr > 0xffff)
  268. {
  269. ocr = F_CPU / frequency / 2 / 64 - 1;
  270. prescalarbits = 0b011;
  271. }
  272. if (_timer == 1)
  273. {
  274. #if defined(TCCR1B)
  275. TCCR1B = (TCCR1B & 0b11111000) | prescalarbits;
  276. #endif
  277. }
  278. #if defined(TCCR3B)
  279. else if (_timer == 3)
  280. TCCR3B = (TCCR3B & 0b11111000) | prescalarbits;
  281. #endif
  282. #if defined(TCCR4B)
  283. else if (_timer == 4)
  284. TCCR4B = (TCCR4B & 0b11111000) | prescalarbits;
  285. #endif
  286. #if defined(TCCR5B)
  287. else if (_timer == 5)
  288. TCCR5B = (TCCR5B & 0b11111000) | prescalarbits;
  289. #endif
  290. }
  291. // Calculate the toggle count
  292. if (duration > 0)
  293. {
  294. toggle_count = 2 * frequency * duration / 1000;
  295. }
  296. else
  297. {
  298. toggle_count = -1;
  299. }
  300. // Set the OCR for the given timer,
  301. // set the toggle count,
  302. // then turn on the interrupts
  303. switch (_timer)
  304. {
  305. #if defined(OCR0A) && defined(TIMSK0) && defined(OCIE0A)
  306. case 0:
  307. OCR0A = ocr;
  308. timer0_toggle_count = toggle_count;
  309. bitWrite(TIMSK0, OCIE0A, 1);
  310. break;
  311. #endif
  312. case 1:
  313. #if defined(OCR1A) && defined(TIMSK1) && defined(OCIE1A)
  314. OCR1A = ocr;
  315. timer1_toggle_count = toggle_count;
  316. bitWrite(TIMSK1, OCIE1A, 1);
  317. #elif defined(OCR1A) && defined(TIMSK) && defined(OCIE1A)
  318. // this combination is for at least the ATmega32
  319. OCR1A = ocr;
  320. timer1_toggle_count = toggle_count;
  321. bitWrite(TIMSK, OCIE1A, 1);
  322. #endif
  323. break;
  324. #if defined(OCR2A) && defined(TIMSK2) && defined(OCIE2A)
  325. case 2:
  326. OCR2A = ocr;
  327. timer2_toggle_count = toggle_count;
  328. bitWrite(TIMSK2, OCIE2A, 1);
  329. break;
  330. #endif
  331. #if defined(TIMSK3)
  332. case 3:
  333. OCR3A = ocr;
  334. timer3_toggle_count = toggle_count;
  335. bitWrite(TIMSK3, OCIE3A, 1);
  336. break;
  337. #endif
  338. #if defined(TIMSK4)
  339. case 4:
  340. OCR4A = ocr;
  341. timer4_toggle_count = toggle_count;
  342. bitWrite(TIMSK4, OCIE4A, 1);
  343. break;
  344. #endif
  345. #if defined(OCR5A) && defined(TIMSK5) && defined(OCIE5A)
  346. case 5:
  347. OCR5A = ocr;
  348. timer5_toggle_count = toggle_count;
  349. bitWrite(TIMSK5, OCIE5A, 1);
  350. break;
  351. #endif
  352. }
  353. }
  354. }
  355. // XXX: this function only works properly for timer 2 (the only one we use
  356. // currently). for the others, it should end the tone, but won't restore
  357. // proper PWM functionality for the timer.
  358. void disableTimer(uint8_t _timer)
  359. {
  360. switch (_timer)
  361. {
  362. case 0:
  363. #if defined(TIMSK0)
  364. TIMSK0 = 0;
  365. #elif defined(TIMSK)
  366. TIMSK = 0; // atmega32
  367. #endif
  368. break;
  369. #if defined(TIMSK1) && defined(OCIE1A)
  370. case 1:
  371. bitWrite(TIMSK1, OCIE1A, 0);
  372. break;
  373. #endif
  374. case 2:
  375. #if defined(TIMSK2) && defined(OCIE2A)
  376. bitWrite(TIMSK2, OCIE2A, 0); // disable interrupt
  377. #endif
  378. #if defined(TCCR2A) && defined(WGM20)
  379. TCCR2A = (1 << WGM20);
  380. #endif
  381. #if defined(TCCR2B) && defined(CS22)
  382. TCCR2B = (TCCR2B & 0b11111000) | (1 << CS22);
  383. #endif
  384. #if defined(OCR2A)
  385. OCR2A = 0;
  386. #endif
  387. break;
  388. #if defined(TIMSK3)
  389. case 3:
  390. TIMSK3 = 0;
  391. break;
  392. #endif
  393. #if defined(TIMSK4)
  394. case 4:
  395. TIMSK4 = 0;
  396. break;
  397. #endif
  398. #if defined(TIMSK5)
  399. case 5:
  400. TIMSK5 = 0;
  401. break;
  402. #endif
  403. }
  404. }
  405. void noTone(uint8_t _pin)
  406. {
  407. int8_t _timer = -1;
  408. for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
  409. if (tone_pins[i] == _pin) {
  410. _timer = pgm_read_byte(tone_pin_to_timer_PGM + i);
  411. tone_pins[i] = 255;
  412. }
  413. }
  414. disableTimer(_timer);
  415. digitalWrite(_pin, 0);
  416. }
  417. #ifdef USE_TIMER0
  418. ISR(TIMER0_COMPA_vect)
  419. {
  420. if (timer0_toggle_count != 0)
  421. {
  422. // toggle the pin
  423. *timer0_pin_port ^= timer0_pin_mask;
  424. if (timer0_toggle_count > 0)
  425. timer0_toggle_count--;
  426. }
  427. else
  428. {
  429. disableTimer(0);
  430. *timer0_pin_port &= ~(timer0_pin_mask); // keep pin low after stop
  431. }
  432. }
  433. #endif
  434. #ifdef USE_TIMER1
  435. ISR(TIMER1_COMPA_vect)
  436. {
  437. if (timer1_toggle_count != 0)
  438. {
  439. // toggle the pin
  440. *timer1_pin_port ^= timer1_pin_mask;
  441. if (timer1_toggle_count > 0)
  442. timer1_toggle_count--;
  443. }
  444. else
  445. {
  446. disableTimer(1);
  447. *timer1_pin_port &= ~(timer1_pin_mask); // keep pin low after stop
  448. }
  449. }
  450. #endif
  451. #ifdef USE_TIMER2
  452. ISR(TIMER2_COMPA_vect)
  453. {
  454. if (timer2_toggle_count != 0)
  455. {
  456. // toggle the pin
  457. *timer2_pin_port ^= timer2_pin_mask;
  458. if (timer2_toggle_count > 0)
  459. timer2_toggle_count--;
  460. }
  461. else
  462. {
  463. // need to call noTone() so that the tone_pins[] entry is reset, so the
  464. // timer gets initialized next time we call tone().
  465. // XXX: this assumes timer 2 is always the first one used.
  466. noTone(tone_pins[0]);
  467. // disableTimer(2);
  468. // *timer2_pin_port &= ~(timer2_pin_mask); // keep pin low after stop
  469. }
  470. }
  471. #endif
  472. #ifdef USE_TIMER3
  473. ISR(TIMER3_COMPA_vect)
  474. {
  475. if (timer3_toggle_count != 0)
  476. {
  477. // toggle the pin
  478. *timer3_pin_port ^= timer3_pin_mask;
  479. if (timer3_toggle_count > 0)
  480. timer3_toggle_count--;
  481. }
  482. else
  483. {
  484. disableTimer(3);
  485. *timer3_pin_port &= ~(timer3_pin_mask); // keep pin low after stop
  486. }
  487. }
  488. #endif
  489. #ifdef USE_TIMER4
  490. ISR(TIMER4_COMPA_vect)
  491. {
  492. if (timer4_toggle_count != 0)
  493. {
  494. // toggle the pin
  495. *timer4_pin_port ^= timer4_pin_mask;
  496. if (timer4_toggle_count > 0)
  497. timer4_toggle_count--;
  498. }
  499. else
  500. {
  501. disableTimer(4);
  502. *timer4_pin_port &= ~(timer4_pin_mask); // keep pin low after stop
  503. }
  504. }
  505. #endif
  506. #ifdef USE_TIMER5
  507. ISR(TIMER5_COMPA_vect)
  508. {
  509. if (timer5_toggle_count != 0)
  510. {
  511. // toggle the pin
  512. *timer5_pin_port ^= timer5_pin_mask;
  513. if (timer5_toggle_count > 0)
  514. timer5_toggle_count--;
  515. }
  516. else
  517. {
  518. disableTimer(5);
  519. *timer5_pin_port &= ~(timer5_pin_mask); // keep pin low after stop
  520. }
  521. }
  522. #endif