About ARDUINOPWM pin Frequency control output
Board: Arduino M0 Pro Zero
Use Tone (pin_led_13, 1000); /* Change Current PIN frequency */
Use Analogwrite (pin_led_13, 200);/* Output PWM waveform */
The principle is that the tone function changes the Prescaler value of the clock domain, causing the PWM output waveform frequency to change
Minor adjustments can change the count upper value in the Analogwrite function prototype
Under the X:\XXX\ARDUINO\HARDWARE\ARDUINO\SAMD directory
Open in Wiring_analog.c
Change the code snippet in Per.reg = 0xFF the ff here, for example change to 0xf0 causes the PWM frequency to accelerate, the specific change how much needs to adjust carefully
Set PORT
if (IsTC)
{
--Configure TC
DISABLE TCx
Tcx->count8. Ctrla.reg &=~ (tc_ctrla_enable);
Set Timer counter Mode to 8 bits
Tcx->count8. Ctrla.reg |= tc_ctrla_mode_count8;
Set TCx as normal PWM
Tcx->count8. Ctrla.reg |= TC_CTRLA_WAVEGEN_NPWM;
Set TCx in waveform mode Normal PWM
Tcx->count8. Cc[channelx].reg = (uint8_t) ulvalue;
Set PER to Maximum counter value (RESOLUTION:0XFF)
Tcx->count8. Per.reg = 0xFF;
Enable TCx
Tcx->count8. Ctrla.reg |= tc_ctrla_enable;
}
Else
{
--Configure TCC
DISABLE TCCX
Tccx->ctrla.reg &=~ (tcc_ctrla_enable);
Set TCx as normal PWM
Tccx->wave.reg |= TCC_WAVE_WAVEGEN_NPWM;
Set TCx in waveform mode Normal PWM
Tccx->cc[channelx].reg = (uint32_t) ulvalue;
Set PER to Maximum counter value (RESOLUTION:0XFF)
Tccx->per.reg = 0xf0;
ENABLE TCCX
Tccx->ctrla.reg |= tcc_ctrla_enable;
}
return;
}
Note Because the TCC is a 24-bit counter, so the TCC here Per.reg = 0xf0, which can be changed to a maximum of 0xffffff, at this time Analogwrite (pin_led_13, 200); 200 can be a very large value. such as Analogwrite (pin_led_13, 20000);