Flag the Arduino M0 board how to change the PWM output frequency

Source: Internet
Author: User
Tags function prototype
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);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.