SAM4SD32 (SAM4S-EK2)
Loading...
Searching...
No Matches
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;
pmc_enable_periph_clk(ID_PWM);
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);
#define PWM_CMR_CPRE_CLKA
(PWM_CMR) Clock A
uint32_t pwm_init(Pwm *p_pwm, pwm_clock_t *clock_config)
Initialize the PWM source clock (clock A and clock B).
Definition pwm.c:119
void pwm_channel_disable(Pwm *p_pwm, uint32_t ul_channel)
Disable the PWM channel.
Definition pwm.c:410
uint32_t pwm_channel_init(Pwm *p_pwm, pwm_channel_t *p_channel)
Initialize one PWM channel.
Definition pwm.c:160
@ PWM_CHANNEL_0
Definition pwm.h:54
#define PWM
(PWM ) Base Address
Definition sam4sd32c.h:423
#define ID_PWM
Pulse Width Modulation (PWM).
Definition sam4sd32c.h:349
Input parameters when configuring a PWM channel mode.
Definition pwm.h:284
uint32_t ul_period
Period Cycle Value.
Definition pwm.h:296
uint32_t ul_prescaler
Channel prescaler.
Definition pwm.h:288
uint32_t channel
Channel number.
Definition pwm.h:286
uint32_t ul_duty
Duty Cycle Value.
Definition pwm.h:294
Input parameters when initializing PWM.
Definition pwm.h:82

Workflow

  1. Define the PWM channel instance in order to configure channel 0:
  2. Enable the module clock for the PWM peripheral:
    • pmc_enable_periph_clk(ID_PWM);
  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:

void pwm_channel_enable(Pwm *p_pwm, uint32_t ul_channel)
Enable the PWM channel.
Definition pwm.c:397

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.