SAM4SD32 (SAM4S-EK2)
Loading...
Searching...
No Matches
Use case #1

In this use case, the PWM module is configured to:

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

Setup steps

Prerequisites

Example code

Add to application C-file:

pwm_channel_t pwm_channel_instance;
Input parameters when configuring a PWM channel mode.
Definition pwm.h:284
void PWM_Handler(void)
{
static uint32_t ul_duty = 0;
uint32_t ul_status;
static uint8_t uc_countn = 0;
static uint8_t uc_flag = 1;
if ((ul_status & PWM_CHANNEL_0) == PWM_CHANNEL_0) {
uc_count++;
if (uc_count == 10) {
if (uc_flag) {
ul_duty++;
if (ul_duty == 100) {
uc_flag = 0;
}
} else {
ul_duty--;
if (ul_duty == 0) {
uc_flag = 1;
}
}
uc_count = 0;
pwm_channel_instance.channel = PWM_CHANNEL_0;
pwm_channel_update_duty(PWM, &pwm_channel_instance, ul_duty);
}
}
}
uint32_t pwm_channel_update_duty(Pwm *p_pwm, pwm_channel_t *p_channel, uint32_t ul_duty)
Change the duty cycle of the PWM channel.
Definition pwm.c:348
uint32_t pwm_channel_get_interrupt_status(Pwm *p_pwm)
Get channel counter event and fault protection trigger interrupt status.
Definition pwm.c:434
@ PWM_CHANNEL_0
Definition pwm.h:54
void PWM_Handler(void)
#define PWM
(PWM ) Base Address
Definition sam4sd32c.h:423
uint32_t channel
Channel number.
Definition pwm.h:286
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 = 0;
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_enable_interrupt(Pwm *p_pwm, uint32_t ul_event, uint32_t ul_fault)
Enable the interrupt of a channel counter event and fault protection.
Definition pwm.c:467
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
#define ID_PWM
Pulse Width Modulation (PWM).
Definition sam4sd32c.h:349
uint32_t ul_period
Period Cycle Value.
Definition pwm.h:296
uint32_t ul_prescaler
Channel prescaler.
Definition pwm.h:288
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. Define the PWM interrupt handler in the application:
  3. In PWM_Handler(), get PWM interrupt status:
  4. In PWM_Handler(), check whether the PWM channel 0 interrupt has occurred:
  5. In PWM_Handler(), if the PWM channel 0 interrupt has occurred, update the ul_duty value:
    • uc_count++;
      if (uc_count == 10) {
      if (uc_flag) {
      ul_duty++;
      if (ul_duty >= 100) {
      uc_flag = 0;
      }
      } else {
      ul_duty--;
      if (ul_duty == 0) {
      uc_flag = 1;
      }
      }
      }
  6. In PWM_Handler(), if the ul_duty value has been updated, change the square wave duty:
  7. Enable the PWM clock:
    • pmc_enable_periph_clk(ID_PWM);
  8. Disable PWM channel 0:
  9. 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 initial ducy as 0%:
  1. The pwm_channel_instance can be re-used to configure other PWM channels after setting the required parameters.
  2. Enable channel 0 interrupt:
    • Note
      1.In order to enable the PWM interrupt, the NVIC must be configured to enable the PWM interrupt. 2. When the channel 0 counter reaches the channel period, the interrupt (counter event) will occur.

Usage steps

Example code

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: