2.62 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

Motor Control Pulse-Width Modulation peripheral library provides the following interfaces:

Functions

NameDescription
MCPWM_InitializeInitializes given instance of MCPWM peripheral
MCPWM_StartStarts the MCPWM module
MCPWM_StopStops the given MCPWM module
MCPWM_PrimaryPeriodSetSets the period value of the primary time base
MCPWM_SecondaryPeriodSetSets the period value of the secondary time base
MCPWM_PrimaryPeriodGetReads the period value of primary time base generator
MCPWM_SecondaryPeriodGetReads the period value of secondary time base generator
MCPWM_ChannelPrimaryDutySetWrites the primary duty cycle value of given MCPWM channel
MCPWM_ChannelSecondaryDutySetWrites the secondary duty cycle value of given MCPWM channel
MCPWM_ChannelDeadTimeSetWrites dead time values of given MCPWM channel
MCPWM_ChannelPrimaryTriggerSetWrites the primary trigger compare value to trigger ADC conversion
MCPWM_ChannelSecondaryTriggerSetWrites the secondary trigger compare value to trigger ADC conversion
MCPWM_PrimaryEventCallbackRegisterRegisters the function to be called from interrupt
MCPWM_SecondaryEventCallbackRegisterRegisters the function to be called from interrupt
MCPWM_CallbackRegisterRegisters the function to be called from interrupt
MCPWM_ChannelPinsOverrideEnableEnables PWM output pins override function
MCPWM_ChannelPinsOverrideDisableDisables PWM output pins override function
MCPWM_ChannelPinsOwnershipEnableSets the PWM output pins ownership to PWM generator
MCPWM_ChannelPinsOwnershipDisableSets the PWM output pins ownership to GPIO

Data types and constants

NameTypeDescription
MCPWM_CH_CALLBACKTypedefDefines the data type and function signature for the MCPWM peripheral callback function
MCPWM_CALLBACKTypedefDefines the data type and function signature for the MCPWM peripheral callback function for primary event and secondary event
MCPWM_CH_NUMEnumIdentifies MCPWM channel number in a MCPWM peripheral
MCPWM_CH_STATUSEnumIdentifies MCPWM channel event status
Note: Not all APIs maybe implemented. See the specific device family section for available APIs.