This is the quickstart guide for the XMEGA Pulse Width Modulation (PWM) service, with step-by-step instructions on how to configure and use the service in a selection of use cases.
The use cases contain several code fragments. The code fragments in the steps for setup can be copied into a custom initialization function, while the steps for usage can be copied into, e.g., the main application function.
Basic use case
In the most basic use case, we configure one PWM channel in non-interrupt mode.
Setup steps
Example code
Add to application C-file:
Workflow
- Ensure that conf_clock.h is present for the driver.
- Note
- This file is only for the driver and should not be included by the user.
- Define config struct for PWM module:
- Initialize sysclock module:
- Initialize config struct and set up PWM with frequency of 500 Hz.
- Note
- Since the timer/counter PWM_TCE0 and channel PWM_CH_A is used, the PWM will be output on port E, pin 0. See pwm_tc_t and pwm_channel_t for more information on what port/pin is used for different timer/counters.
- Attention
- This step must not be skipped or the initial content of the structs will be unpredictable, possibly causing misconfiguration.
Usage steps
Example code
Add to, e.g., main loop in application C-file:
Workflow
- Start PWM with 50% duty cycle:
Advanced use cases
For more advanced use of the PWM service, see the following use cases: