@@ -97,7 +97,7 @@ static int8_t toneBegin(uint8_t _pin)
9797
9898 // If not, search for an unused timer
9999 for (int i = 0 ; i < AVAILABLE_TONE_PINS; i++) {
100- if (tone_pins[i] == 255 ) {
100+ if (tone_pins[i] == NOT_A_PIN ) {
101101 tone_pins[i] = _pin;
102102 _timer = pgm_read_byte (tone_pin_to_timer_PGM + i);
103103 break ;
@@ -158,7 +158,9 @@ void tone(uint8_t _pin, unsigned int frequency, unsigned long duration)
158158 TCB_t *timer_B = ((TCB_t *)&TCB0 + (_timer - TIMERB0));
159159
160160 // 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() )
162164 if (prescaler_needed){
163165 timer_B->CTRLA = TCB_CLKSEL_CLKTCA_gc;
164166 } else {
@@ -189,7 +191,7 @@ void tone(uint8_t _pin, unsigned int frequency, unsigned long duration)
189191 } else { // _timer == TIMERB0
190192 timerb0_outtgl_reg = port_outtgl;
191193 timerb0_bit_mask = bit_mask;
192- timerb2_toggle_count = toggle_count;
194+ timerb0_toggle_count = toggle_count;
193195 }
194196
195197 // Enable timer
@@ -200,6 +202,8 @@ void tone(uint8_t _pin, unsigned int frequency, unsigned long duration)
200202 }
201203 }
202204
205+ /* Keep in mind this is NOT RECOMMENDED since other timers
206+ rely on the clock of TCA0 */
203207 #if defined(USE_TIMERA0)
204208 case TIMERA0:{
205209
@@ -243,6 +247,8 @@ void tone(uint8_t _pin, unsigned int frequency, unsigned long duration)
243247 }
244248}
245249
250+ /* Works for all timers -- the timer being disabled will go back to the
251+ configuration it had to output PWM for analogWrite() */
246252void disableTimer (uint8_t _timer)
247253{
248254 switch (_timer){
@@ -291,7 +297,7 @@ void disableTimer(uint8_t _timer)
291297 // RESTORE PWM FUNCTIONALITY:
292298
293299 /* 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;
295301
296302 /* Period setting, 16 bit register but val resolution is 8 bit */
297303 TCA0.SINGLE .PER = PWM_TIMER_PERIOD;
0 commit comments