Microchip® Advanced Software Framework

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;
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);
}
}
}
pwm_channel_disable(PWM, PWM_CHANNEL_0);
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);
pwm_channel_enable_interrupt(PWM, PWM_CHANNEL_0, 0);

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:
    • if ((ul_status & PWM_CHANNEL_0) == PWM_CHANNEL_0) {
      }
  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:
  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:
    • pwm_channel_enable_interrupt(PWM, PWM_CHANNEL_0, 0);
    • 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

pwm_channel_enable(PWM, PWM_CHANNEL_0);

Workflow

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