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:
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;
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;
}
}
}
.ul_clkb = 0,
.ul_mck = 48000000
};
Workflow
- Define the PWM channel instance in order to configure channel 0:
- Define the PWM interrupt handler in the application:
- In PWM_Handler(), get PWM interrupt status:
- In PWM_Handler(), check whether the PWM channel 0 interrupt has occurred:
if ((ul_status & PWM_CHANNEL_0) == PWM_CHANNEL_0) {
}
- 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;
}
}
}
- In PWM_Handler(), if the ul_duty value has been updated, change the square wave duty:
- Enable the PWM clock:
- Disable PWM channel 0:
- Setup clock for PWM module:
.ul_clkb = 0,
.ul_mck = 48000000
};
- Note
- 1. Only Clock A is configured (clock B is not used).
- The expected frequency is 1KHz, system main clock is assumed to be 48Mhz.
- Initialize channel instance and configure PWM channel 0, selecting clock A as its source clock and setting the initial ducy as 0%:
- Note
- 1. Period is left-aligned and output waveform starts at a low level.
- The pwm_channel_instance can be re-used to configure other PWM channels after setting the required parameters.
- 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
Workflow
- Enable PWM channel 0 and output square wave on this channel: