@@ -97,7 +97,7 @@ static int8_t toneBegin(uint8_t _pin)
97
97
98
98
// If not, search for an unused timer
99
99
for (int i = 0 ; i < AVAILABLE_TONE_PINS; i++) {
100
- if (tone_pins[i] == 255 ) {
100
+ if (tone_pins[i] == NOT_A_PIN ) {
101
101
tone_pins[i] = _pin;
102
102
_timer = pgm_read_byte (tone_pin_to_timer_PGM + i);
103
103
break ;
@@ -158,7 +158,9 @@ void tone(uint8_t _pin, unsigned int frequency, unsigned long duration)
158
158
TCB_t *timer_B = ((TCB_t *)&TCB0 + (_timer - TIMERB0));
159
159
160
160
// Disable for now, set clk according to 'prescaler_needed'
161
- // (Prescaled clock will come from TCA)
161
+ // (Prescaled clock will come from TCA --
162
+ // by default it should have a prescaler of 64 (250kHz clock)
163
+ // TCA default initialization is in wiring.c -- init() )
162
164
if (prescaler_needed){
163
165
timer_B->CTRLA = TCB_CLKSEL_CLKTCA_gc;
164
166
} else {
@@ -189,7 +191,7 @@ void tone(uint8_t _pin, unsigned int frequency, unsigned long duration)
189
191
} else { // _timer == TIMERB0
190
192
timerb0_outtgl_reg = port_outtgl;
191
193
timerb0_bit_mask = bit_mask;
192
- timerb2_toggle_count = toggle_count;
194
+ timerb0_toggle_count = toggle_count;
193
195
}
194
196
195
197
// Enable timer
@@ -200,6 +202,8 @@ void tone(uint8_t _pin, unsigned int frequency, unsigned long duration)
200
202
}
201
203
}
202
204
205
+ /* Keep in mind this is NOT RECOMMENDED since other timers
206
+ rely on the clock of TCA0 */
203
207
#if defined(USE_TIMERA0)
204
208
case TIMERA0:{
205
209
@@ -243,6 +247,8 @@ void tone(uint8_t _pin, unsigned int frequency, unsigned long duration)
243
247
}
244
248
}
245
249
250
+ /* Works for all timers -- the timer being disabled will go back to the
251
+ configuration it had to output PWM for analogWrite() */
246
252
void disableTimer (uint8_t _timer)
247
253
{
248
254
switch (_timer){
@@ -291,7 +297,7 @@ void disableTimer(uint8_t _timer)
291
297
// RESTORE PWM FUNCTIONALITY:
292
298
293
299
/* Setup timers for single slope PWM, but do not enable, will do in analogWrite() */
294
- TCA0.SINGLE .CTRLB |= ( TCA_SINGLE_WGMODE_SINGLESLOPE_gc) ;
300
+ TCA0.SINGLE .CTRLB = TCA_SINGLE_WGMODE_SINGLESLOPE_gc;
295
301
296
302
/* Period setting, 16 bit register but val resolution is 8 bit */
297
303
TCA0.SINGLE .PER = PWM_TIMER_PERIOD;
0 commit comments