Jump to main content

##

# 3.17.13 Motor Control Pulse-Width Modulation (MCPWM)

The PIC32MK GP/MC Family of devices support a dedicated Motor Control Pulse-Width Modulation (PWM) module with up to 12 outputs. The Motor Control PWM module consists of the following major features:

- Two master time base modules with special event triggers

- PWM module input clock prescaler

- Two synchronization inputs

- Two synchronization outputs

- Eight PWM generators with complimentary output pairs

- Four additional PWM generators with single ended outputs

- Period, duty cycle, phase shift and dead time minimum resolution of 1 / FSYSCLK in Edge-Aligned mode and 2 / FSYSCLK minimum resolution in Center-Aligned mode

- Cycle by cycle fault recovery and latched fault modes

- PWM time-base capture upon current limit

- Nine fault input pins are available for faults and current limits

- Programmable analog-to-digital trigger with interrupt for each PWM pair

- Complementary PWM outputs

- Push-Pull PWM outputs

- Redundant PWM outputs

- Edge-Aligned PWM mode

- Center-Aligned PWM mode

- Variable Phase PWM mode

- Multi-Phase PWM mode

- Fixed-Off Time PWM mode

- Current Limit PWM mode

- Current Reset PWM mode

- PWMxH and PWMxL output override control

- PWMxH and PWMxL output pin swapping

- Chopping mode (also known as Gated mode)

- Dead time insertion

- Dead time compensation

- Enhanced Leading-Edge Blanking (LEB)

- 15 mA PWM pin output drive

## Using The Library

This example shows how to configure the MCPWM to generate PWM signals whose duty and period can be configured independently.

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

    /* Save PWM period */
    uint16_t period;

    /* This function is called after PWM0 counter event */
    void PWM_PeriodHandler(uint32_t status, uintptr_t context)
    {
        /* duty cycle values */
        static uint16_t duty0 = 0U;
        static uint16_t duty1 = 1000U;
        static uint16_t duty2 = 2000U;

        MCPWM_ChannelPrimaryDutySet(MCPWM_CH_1, duty0);
        MCPWM_ChannelPrimaryDutySet(MCPWM_CH_2, duty1);
        MCPWM_ChannelPrimaryDutySet(MCPWM_CH_3, 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 )
    {
        /* Initialize all modules */
        SYS_Initialize ( NULL );

        /* Register callback function for Channel 0 counter event */
        MCPWM_CallbackRegister(MCPWM_CH_1, PWM_PeriodHandler, (uintptr_t)NULL);

        /* Read the period */
        period = MCPWM_PrimaryPeriodGet();

        /* Start all synchronous channels by starting channel 0*/
        MCPWM_Start();

        while ( true )
        {
            /* Maintain state machines of all polled MPLAB Harmony modules. */
            SYS_Tasks ( );
        }

        /* Execution should not come here during normal operation */

        return ( EXIT_FAILURE );
    }

## Library Interface

Peripheral library provides the following interfaces:

Functions

Name | Description
--- | ---
MCPWM_Initialize | Initializes given instance of MCPWM peripheral
MCPWM_Start | Starts the MCPWM module
MCPWM_Stop | Stops the given MCPWM module
MCPWM_PrimaryPeriodSet | Sets the period value of the primary time base
MCPWM_SecondaryPeriodSet | Sets the period value of the secondary time base
MCPWM_PrimaryPeriodGet | Reads the period value of primary time base generator
MCPWM_SecondaryPeriodGet | Reads the period value of secondary time base generator
MCPWM_ChannelPrimaryDutySet | Writes the primary duty cycle value of given MCPWM channel
MCPWM_ChannelSecondaryDutySet | Writes the secondary duty cycle value of given MCPWM channel
MCPWM_ChannelDeadTimeSet | Writes dead time values of given MCPWM channel
MCPWM_ChannelPrimaryTriggerSet | Writes the primary trigger compare value to trigger ADC conversion
MCPWM_ChannelSecondaryTriggerSet | Writes the secondary trigger compare value to trigger ADC conversion
MCPWM_PrimaryEventCallbackRegister | Registers the function to be called from interrupt
MCPWM_SecondaryEventCallbackRegister | Registers the function to be called from interrupt
MCPWM_CallbackRegister | Registers the function to be called from interrupt
MCPWM_ChannelPinsOverrideEnable | Enables PWM output pins override function
MCPWM_ChannelPinsOverrideDisable | Disables PWM output pins override function
MCPWM_ChannelPinsOwnershipEnable | Sets the PWM output pins ownership to PWM generator
MCPWM_ChannelPinsOwnershipDisable | Sets the PWM output pins ownership to GPIO

Data types and constants

Name | Type | Description
--- | --- | ---
MCPWM_CH_CALLBACK | Typedef | Defines the data type and function signature for the MCPWM peripheral callback function
MCPWM_CALLBACK | Typedef | Defines the data type and function signature for the MCPWM peripheral callback function for primary event and secondary event
MCPWM_CH_NUM | Enum | Identifies MCPWM channel number in a MCPWM peripheral
MCPWM_CH_STATUS | Enum | Identifies MCPWM channel event status