1.26.12 Pulse Width Modulation Controller (PWM)

The Pulse Width Modulation Controller (PWM) controls several channels independently. Each channel controls one square output waveform. Characteristics of the output waveform such as period, duty-cycle and polarity are configurable through the user interface. Each channel selects and uses one of the clocks provided by the clock generator. The clock generator provides several clocks resulting from the division of the PWM macrocell master clock. Channels can be synchronized, to generate non overlapped waveforms. All channels integrate a double buffering system in order to prevent an unexpected output waveform while modifying the period or the duty-cycle.

Using The Library

This example shows how to configure the PWM to generate three PWM waveform signals. The duty cycle of the PWM is updated in the interrupt handler.

/* Duty cycle increment value */
#define DUTY_INCREMENT (10U)

/* Save PWM period */
uint32_t period;

/* This function is called after PWM0 counter event */
void PWM_CounterEventHandler(uintptr_t context)
{
    static uint32_t duty0 = 0U;
    static uint32_t duty1 = 2500U;
    static uint32_t duty2 = 5000U;

    PWM0_ChannelDutySet(PWM_CHANNEL_0, duty0);
    PWM0_ChannelDutySet(PWM_CHANNEL_1, duty1);
    PWM0_ChannelDutySet(PWM_CHANNEL_2, duty2);

    /* Increment duty cycle values */
    duty0 += DUTY_INCREMENT;
    duty1 += DUTY_INCREMENT;
    duty2 += DUTY_INCREMENT;

    if (duty0 > period)
        duty0 = 0U;
    if (duty1 > period)
        duty1 = 0U;
    if (duty2 > period)
        duty2 = 0U;
}

int main ( void )
{
    /* Register callback function for Channel 0 counter event */
    PWM0_CallbackRegister(PWM_CounterEventHandler, (uintptr_t)NULL);

    /* Read the period */
    period = PWM0_ChannelPeriodGet(PWM_CHANNEL_0);

    /* Start all channels synchronously */
    PWM0_ChannelsStart(PWM_CHANNEL_0_MASK | PWM_CHANNEL_1_MASK | PWM_CHANNEL_2_MASK);
}

Library Interface

Pulse Width Modulation Controller peripheral library provides the following interfaces:

Functions

Name Description
PWMx_Initialize Initializes given instance of PWM peripheral
PWMx_ChannelsStart Starts the given PWM channels
PWMx_ChannelsStop Stops the given PWM channels
PWMx_ChannelPeriodSet Sets the period value of given PWM channel
PWMx_ChannelPeriodGet Reads the period value of given PWM channel
PWMx_ChannelDutySet Writes the duty cycle value of given PWM channel
PWMx_ChannelCounterEventEnable Enables counter event of given channels
PWMx_ChannelCounterEventDisable Disables counter event of given channels
PWMx_ChannelCounterEventStatusGet Disables counter event of given channels
PWMx_CallbackRegister Registers the function to be called from interrupt

Data types and constants

Name Type Description
PWM_CALLBACK Typedef Defines the data type and function signature for the PWM peripheral callback function
PWM_CHANNEL_NUM Enum Identifies PWM channel number in a PWM peripheral
PWM_CHANNEL_MASK Enum Identifies PWM channel mask