Microchip® Advanced Software Framework

Quickstart guide for SAM PWM module

This is the quickstart guide for the PWM module, with step-by-step instructions on how to configure and use the drivers 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 this basic use case, the PWM module is configured to:

  • Output a square wave on PWM channel 0
  • The frequency of the square wave is 1KHz, 50% duty cycle
  • Clock A as the source clock
  • The output wave can be checked on the selected output pin

Setup steps

Prerequisites

Example code

Add this PWM initialization code at the beginning of the main function:

pwm_channel_t pwm_channel_instance;
pwm_clock_t clock_setting = {
.ul_clka = 1000 * 100,
.ul_clkb = 0,
.ul_mck = 48000000
};
pwm_init(PWM, &clock_setting);
pwm_channel_instance.ul_prescaler = PWM_CMR_CPRE_CLKA;
pwm_channel_instance.ul_period = 100;
pwm_channel_instance.ul_duty = 50;
pwm_channel_instance.channel = PWM_CHANNEL_0;
pwm_channel_init(PWM, &pwm_channel_instance);

Workflow

  1. Define the PWM channel instance in order to configure channel 0:
  2. Enable the module clock for the PWM peripheral:
  3. Disable PWM channel 0:
  4. Setup clock for PWM module:
    • pwm_clock_t clock_setting = {
      .ul_clka = 1000 * 100,
      .ul_clkb = 0,
      .ul_mck = 48000000
      };
      pwm_init(PWM, &clock_setting);
    • Note
      1. Only Clock A is configured (clock B is not used).
  1. The expected frequency is 1KHz, system main clock is assumed to be 48MHz.
  2. Initialize channel instance and configure PWM channel 0, selecting clock A as its source clock and setting the duty cycle at 50%:
  1. The pwm_channel_instance can be re-used to configure other PWM channels after setting the required parameters.

Usage steps

Example code

Add to, e.g., main loop in application C-file:

Workflow

  1. Enable PWM channel 0 and output square wave on this channel:

Advanced use cases

For more advanced use of the pwm driver, see the following use cases:

  • Use case #1 : PWM channel 0 outputs square wave and duty cycle is updated in the PWM ISR.