@@ -103,33 +103,11 @@ volatile uint8_t *timerb2_outtgl_reg;
103
103
volatile uint8_t timerb2_bit_mask;
104
104
#endif
105
105
106
- static int8_t toneBegin (uint8_t _pin)
107
- {
108
- int8_t _timer = -1 ;
109
-
110
- // If pin already being used for tone, return the associated timer
111
- for (int i = 0 ; i < AVAILABLE_TONE_PINS; i++) {
112
- if (tone_pins[i] == _pin) {
113
- return pgm_read_byte (tone_pin_to_timer_PGM + i);
114
- }
115
- }
116
-
117
- // If not, search for an unused timer
118
- for (int i = 0 ; i < AVAILABLE_TONE_PINS; i++) {
119
- if (tone_pins[i] == NOT_A_PIN) {
120
- tone_pins[i] = _pin;
121
- _timer = pgm_read_byte (tone_pin_to_timer_PGM + i);
122
- break ;
123
- }
124
- }
125
-
126
- return _timer;
127
- }
128
-
129
-
106
+ // helper functions
107
+ static uint8_t toneBegin (uint8_t _pin);
108
+ static void disableTimer (uint8_t _timer);
130
109
131
110
// frequency (in hertz) and duration (in milliseconds).
132
-
133
111
void tone (uint8_t _pin, unsigned int frequency, unsigned long duration)
134
112
{
135
113
long toggle_count = 0 ;
@@ -221,43 +199,35 @@ void tone(uint8_t _pin, unsigned int frequency, unsigned long duration)
221
199
}
222
200
}
223
201
224
- /* Works for all timers -- the timer being disabled will go back to the
225
- configuration it had to output PWM for analogWrite() */
226
- void disableTimer (uint8_t _timer)
202
+ // helper function for tone()
203
+ static uint8_t toneBegin (uint8_t _pin)
227
204
{
228
- // Reinit back to producing PWM -- timer will be type B
229
-
230
- // Get timer struct
231
- TCB_t *timer_B = ((TCB_t *)&TCB0 + (_timer - TIMERB0));
232
-
233
- // Disable interrupt
234
- timer_B->INTCTRL = 0 ;
235
-
236
- // Disable timer
237
- timer_B->CTRLA = 0 ;
238
-
239
- // RESTORE PWM FUNCTIONALITY:
240
-
241
- /* 8 bit PWM mode, but do not enable output yet, will do in analogWrite() */
242
- timer_B->CTRLB = (TCB_CNTMODE_PWM8_gc);
205
+ uint8_t _timer = NOT_ON_TIMER;
243
206
244
- /* Assign 8-bit period */
245
- timer_B->CCMPL = PWM_TIMER_PERIOD;
246
-
247
- /* default duty 50%, set when output enabled */
248
- timer_B->CCMPH = PWM_TIMER_COMPARE;
207
+ // If pin already being used for tone, return the associated timer
208
+ for (int i = 0 ; i < AVAILABLE_TONE_PINS; i++) {
209
+ if (tone_pins[i] == _pin) {
210
+ return pgm_read_byte (tone_pin_to_timer_PGM + i);
211
+ }
212
+ }
213
+
214
+ // If not, search for an unused timer
215
+ for (int i = 0 ; i < AVAILABLE_TONE_PINS; i++) {
216
+ if (tone_pins[i] == NOT_A_PIN) {
217
+ tone_pins[i] = _pin;
218
+ _timer = pgm_read_byte (tone_pin_to_timer_PGM + i);
219
+ break ;
220
+ }
221
+ }
249
222
250
- /* Use TCA clock (250kHz) and enable */
251
- /* (sync update commented out, might try to synchronize later */
252
- timer_B->CTRLA = (TCB_CLKSEL_CLKTCA_gc) | (TCB_ENABLE_bm);
253
-
223
+ return _timer;
254
224
}
255
225
256
226
// pin which currently is being used for a tone
257
227
void noTone (uint8_t _pin)
258
228
{
259
229
int8_t _timer = NOT_ON_TIMER;
260
-
230
+
261
231
// Find timer associated with pin
262
232
for (int i = 0 ; i < AVAILABLE_TONE_PINS; i++) {
263
233
@@ -277,14 +247,47 @@ void noTone(uint8_t _pin)
277
247
278
248
if (_timer > NOT_ON_TIMER){
279
249
disableTimer (_timer);
280
-
250
+
281
251
// Keep pin low after disabling of timer
282
252
digitalWrite (_pin, LOW);
283
253
}
284
254
285
255
}
286
256
287
257
// helper function for noTone()
258
+ /* Works for all timers -- the timer being disabled will go back to the
259
+ configuration it had to output PWM for analogWrite() */
260
+ static void disableTimer (uint8_t _timer)
261
+ {
262
+ // Reinit back to producing PWM -- timer will be type B
263
+
264
+ // Get timer struct
265
+ TCB_t *timer_B = ((TCB_t *)&TCB0 + (_timer - TIMERB0));
266
+
267
+ // Disable interrupt
268
+ timer_B->INTCTRL = 0 ;
269
+
270
+ // Disable timer
271
+ timer_B->CTRLA = 0 ;
272
+
273
+ // RESTORE PWM FUNCTIONALITY:
274
+
275
+ /* 8 bit PWM mode, but do not enable output yet, will do in analogWrite() */
276
+ timer_B->CTRLB = (TCB_CNTMODE_PWM8_gc);
277
+
278
+ /* Assign 8-bit period */
279
+ timer_B->CCMPL = PWM_TIMER_PERIOD;
280
+
281
+ /* default duty 50%, set when output enabled */
282
+ timer_B->CCMPH = PWM_TIMER_COMPARE;
283
+
284
+ /* Use TCA clock (250kHz) and enable */
285
+ /* (sync update commented out, might try to synchronize later */
286
+ timer_B->CTRLA = (TCB_CLKSEL_CLKTCA_gc) | (TCB_ENABLE_bm);
287
+
288
+ }
289
+
290
+
288
291
289
292
#ifdef USE_TIMERB0
290
293
ISR (TCB0_INT_vect)
0 commit comments