|
1 | 1 | /* Tone.cpp
|
2 | 2 |
|
3 |
| - A Tone Generator Library for ATmega4809 |
| 3 | + A Tone Generator Library |
| 4 | + |
| 5 | + Written by Brett Hagman |
4 | 6 |
|
5 | 7 | This library is free software; you can redistribute it and/or
|
6 | 8 | modify it under the terms of the GNU Lesser General Public
|
|
15 | 17 | You should have received a copy of the GNU Lesser General Public
|
16 | 18 | License along with this library; if not, write to the Free Software
|
17 | 19 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
18 |
| -
|
19 |
| -*************************************************/ |
| 20 | + |
| 21 | + -------- ----------- -------- -------- |
| 22 | + 0001 B Hagman 09/08/02 Initial coding |
| 23 | + 0002 B Hagman 09/08/18 Multiple pins |
| 24 | + 0003 B Hagman 09/08/18 Moved initialization from constructor to begin() |
| 25 | + 0004 B Hagman 09/09/26 Fixed problems with ATmega8 |
| 26 | + 0005 B Hagman 09/11/23 Scanned prescalars for best fit on 8 bit timers |
| 27 | + 09/11/25 Changed pin toggle method to XOR |
| 28 | + 09/11/25 Fixed timer0 from being excluded |
| 29 | + 0006 D Mellis 09/12/29 Replaced objects with functions |
| 30 | + 0007 M Sproul 10/08/29 Changed #ifdefs from cpu to register |
| 31 | + 0008 S Kanemoto 12/06/22 Fixed for Leonardo by @maris_HY |
| 32 | + 0009 J Reucker 15/04/10 Issue #292 Fixed problems with ATmega8 (thanks to Pete62) |
| 33 | + 0010 jipp 15/04/13 added additional define check #2923 |
| 34 | + 0011 E Roy 13/02/18 ported to ATmega4809 |
| 35 | + *************************************************/ |
20 | 36 |
|
21 | 37 | #include <avr/interrupt.h>
|
22 | 38 | #include <avr/pgmspace.h>
|
23 | 39 | #include "Arduino.h"
|
24 | 40 | #include "pins_arduino.h"
|
25 | 41 |
|
26 |
| -#if defined(__AVR_ATmega8__) || defined(__AVR_ATmega128__) |
27 |
| -#define TCCR2A TCCR2 |
28 |
| -#define TCCR2B TCCR2 |
29 |
| -#define COM2A1 COM21 |
30 |
| -#define COM2A0 COM20 |
31 |
| -#define OCR2A OCR2 |
32 |
| -#define TIMSK2 TIMSK |
33 |
| -#define OCIE2A OCIE2 |
34 |
| -#define TIMER2_COMPA_vect TIMER2_COMP_vect |
35 |
| -#define TIMSK1 TIMSK |
36 |
| -#endif |
| 42 | +#define AVAILABLE_TONE_PINS 1 |
37 | 43 |
|
| 44 | +#define USE_TIMERB1 // interferes with PWM on pin 3 |
| 45 | +/* |
| 46 | +#define USE_TIMERB2 // interferes with PWM on pin 11 |
| 47 | +#define USE_TIMERB0 // interferes with PWM on pin 6 |
| 48 | +#define USE_TIMERA0 // interferes with PWM on pins 5,9,10 NOT RECOMMENDED |
| 49 | +*/ |
| 50 | + |
| 51 | +// Can't use TIMERB3 -- used for application time tracking |
| 52 | +// Using TIMERA0 NOT RECOMMENDED -- leave to last -- all other timers use its clock! |
| 53 | +const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { TIMERB1 /*, TIMERB2, TIMERB0, TIMERA0 */ }; |
| 54 | +static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { NOT_A_PIN /*, NOT_A_PIN, NOT_A_PIN, NOT_A_PIN */ }; |
| 55 | + |
| 56 | + |
38 | 57 | // timerx_toggle_count:
|
39 | 58 | // > 0 - duration specified
|
40 | 59 | // = 0 - stopped
|
41 | 60 | // < 0 - infinitely (until stop() method called, or new play() called)
|
42 | 61 |
|
43 |
| -#if defined(TCA0) |
| 62 | +#if defined(USE_TIMERA0) |
44 | 63 | volatile long timera0_toggle_count;
|
45 | 64 | volatile uint8_t *timera0_outtgl_reg;
|
46 | 65 | volatile uint8_t timera0_bit_mask;
|
47 | 66 | #endif
|
48 | 67 |
|
49 |
| -#if defined(TCB0) |
| 68 | +#if defined(USE_TIMERB0) |
50 | 69 | volatile long timerb0_toggle_count;
|
51 | 70 | volatile uint8_t *timerb0_outtgl_reg;
|
52 | 71 | volatile uint8_t timerb0_bit_mask;
|
53 | 72 | #endif
|
54 | 73 |
|
55 |
| -#if defined(TCB1) |
| 74 | +#if defined(USE_TIMERB1) |
| 75 | + |
56 | 76 | volatile long timerb1_toggle_count;
|
57 | 77 | volatile uint8_t *timerb1_outtgl_reg;
|
58 | 78 | volatile uint8_t timerb1_bit_mask;
|
59 | 79 | #endif
|
60 | 80 |
|
61 |
| -#if defined(TCB2) |
| 81 | +#if defined(USE_TIMERB2) |
62 | 82 | volatile long timerb2_toggle_count;
|
63 | 83 | volatile uint8_t *timerb2_outtgl_reg;
|
64 | 84 | volatile uint8_t timerb2_bit_mask;
|
65 | 85 | #endif
|
66 | 86 |
|
67 |
| -#define AVAILABLE_TONE_PINS 1 |
68 |
| -#define USE_TIMERB1 // interferes with PWM on pin 3 |
69 |
| -/* |
70 |
| -#define USE_TIMERB2 // interferes with PWM on pin 11 |
71 |
| -#define USE_TIMERB0 // interferes with PWM on pin 6 |
72 |
| -#define USE_TIMERA0 // interferes with PWM on pins 5,9,10 |
73 |
| -*/ |
74 |
| - |
75 |
| -// Can't use TIMERB3 -- used for application time tracking |
76 |
| -// Leave TIMERA0 to last -- all other timers use its clock |
77 |
| -const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { TIMERB1 /*, TIMERB2, TIMERB0, TIMERA0 */ }; |
78 |
| -static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { NOT_A_PIN /*, NOT_A_PIN, NOT_A_PIN, NOT_A_PIN */ }; |
79 |
| - |
80 |
| - |
81 | 87 | static int8_t toneBegin(uint8_t _pin)
|
82 | 88 | {
|
83 | 89 | int8_t _timer = -1;
|
|
0 commit comments