Microchip® Advanced Software Framework

Quickstart guide for AVR XMEGA PWM service

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:

struct pwm_config pwm_cfg;
pwm_init(&pwm_cfg, PWM_TCE0, PWM_CH_A, 500);

Workflow

  1. 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.
  2. Define config struct for PWM module:
    struct pwm_config pwm_cfg;
  3. Initialize sysclock module:
  4. Initialize config struct and set up PWM with frequency of 500 Hz.
    pwm_init(&pwm_cfg, PWM_TCE0, PWM_CH_A, 500);
    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

  1. Start PWM with 50% duty cycle:

Advanced use cases

For more advanced use of the PWM service, see the following use cases: