|
11 | 11 | #include "Arduino.h" |
12 | 12 | #include <HardwareTimer.h> |
13 | 13 |
|
14 | | -// define default timer and channel |
15 | | -#ifndef TONE_TIMER |
16 | | -#define TONE_TIMER 4 |
17 | | -#endif |
18 | | -#ifndef TONE_CHANNEL |
19 | | -#define TONE_CHANNEL 4 |
20 | | -#endif |
21 | 14 |
|
22 | 15 | #define PinTimer(pin) (PIN_MAP[pin].timer_device->clk_id-RCC_TIMER1+1) |
23 | 16 | #define PinChannel(pin) (PIN_MAP[pin].timer_channel) |
|
30 | 23 | #define USE_BSRR |
31 | 24 |
|
32 | 25 | // construct static timer array ( |
33 | | -HardwareTimer TTimer1(1), TTimer2(2), TTimer3(3), TTimer4(4); |
34 | | -#ifdef STM32_HIGH_DENSITY |
35 | | -HardwareTimer TTimer5(5), TTimer6(6), TTimer7(7), TTimer8(8); |
36 | | -#endif |
| 26 | + |
37 | 27 |
|
38 | 28 | #ifdef STM32_HIGH_DENSITY |
39 | | - HardwareTimer *TTimer[8] { &TTimer1,&TTimer2,&TTimer3,&TTimer4,&TTimer5,&TTimer6,&TTimer7,&TTimer8 }; |
| 29 | +// define default timer and channel |
| 30 | + #ifndef TONE_TIMER |
| 31 | + #define TONE_TIMER 8 |
| 32 | + #endif |
| 33 | + #ifndef TONE_CHANNEL |
| 34 | + #define TONE_CHANNEL 8 |
| 35 | + #endif |
| 36 | + |
| 37 | + HardwareTimer TTimer1(1), TTimer2(2), TTimer3(3), TTimer4(4),TTimer5(5), TTimer6(6), TTimer7(7), TTimer8(8); |
| 38 | + HardwareTimer *TTimer[8] = { &TTimer1,&TTimer2,&TTimer3,&TTimer4,&TTimer5,&TTimer6,&TTimer7,&TTimer8 }; |
40 | 39 | #else |
41 | | - HardwareTimer *TTimer[4] { &TTimer1,&TTimer2,&TTimer3,&TTimer4 }; |
| 40 | + // define default timer and channel |
| 41 | + #ifndef TONE_TIMER |
| 42 | + #define TONE_TIMER 4 |
| 43 | + #endif |
| 44 | + #ifndef TONE_CHANNEL |
| 45 | + #define TONE_CHANNEL 4 |
| 46 | + #endif |
| 47 | + |
| 48 | + HardwareTimer TTimer1(1), TTimer2(2), TTimer3(3), TTimer4(4); |
| 49 | + HardwareTimer *TTimer[4] = { &TTimer1,&TTimer2,&TTimer3,&TTimer4 }; |
42 | 50 | #endif |
43 | 51 |
|
44 | 52 |
|
45 | 53 | uint8_t tone_force_channel = 0; // forced timer channel |
46 | 54 | uint8_t tone_force_ntimer = 0; // forced timer |
47 | 55 |
|
48 | | -HardwareTimer *tone_timer = TTimer[TONE_TIMER]; // timer used to generate frequency |
| 56 | +HardwareTimer *tone_timer;// = TTimer[TONE_TIMER-1]; // timer used to generate frequency |
49 | 57 | uint8_t tone_channel = TONE_CHANNEL; // timer channel used to generate frequency |
50 | 58 | uint8_t tone_ntimer = TONE_TIMER; // timer used to generate frequency |
51 | 59 |
|
@@ -138,7 +146,7 @@ void tone(uint32_t pin, uint32_t freq, uint32_t duration) { |
138 | 146 |
|
139 | 147 | tone_timer->pause(); |
140 | 148 |
|
141 | | - if(freq > 0 || duration >0 ){ |
| 149 | + if(freq > 0 && duration >0 ){ |
142 | 150 | uint32_t count = 18000000/freq; // timer counts per half wave |
143 | 151 | tone_ncount = tone_n = (count>>16)+1; // number of 16-bit count chunk |
144 | 152 | tone_tcount = count/tone_ncount; // size of count chunk |
@@ -183,7 +191,7 @@ void tone(uint32_t pin, uint32_t freq, uint32_t duration) { |
183 | 191 | //////////////////////////////////////////////////////////////////////////////// |
184 | 192 | // disable tone on specified pin, if any |
185 | 193 | void noTone(uint32_t pin){ |
186 | | - tone(pin,-1,0); // it's all handled in tone() |
| 194 | + tone(pin,0,0); // it's all handled in tone() |
187 | 195 | } |
188 | 196 |
|
189 | 197 | //////////////////////////////////////////////////////////////////////////////// |
|
0 commit comments