Mcpwm Block Icon
MCPWM (Motor Control PWM) offers enhanced features beyond standard PWM blocks, including programmable fault sources with digital comparator inputs, leading-edge blanking, dead-time compensation modes, and extensive trigger output capabilities for ADC synchronization.

Block Inputs/Outputs

Block Inputs

InputTypeConditionDescription
Perioduint16/uint32Variable period modePWM period value. Scalar for common time base, vector for independent time bases
DutyCycle [1..N]uint16/uint32Per enabled PWMDuty cycle value. Can be [single] or [val1 val2] for asymmetric mode
PhaseShift [1..N]uint16/uint32If enabledPhase shift value for interleaved operation
TRIG1..3 [1..N]uint16/uint32Dynamic triggerRuntime trigger point modification
DeadTimeH/L [1..N]uint16Dynamic dead-timeRuntime dead-time adjustment
Fault_SWbooleanIf software fault enabledSoftware-triggered fault input
Fault_ClearbooleanLatched fault modeClear latched fault condition

Block Outputs

OutputTypeDescription
Fault_Statusuint16Current fault status register (bitfield of active faults)
Trigger 1..NbooleanTrigger output signals for external synchronization
PWM_Statusuint16Overall PWM status (enable state, mode, etc.)

Scaling

The block supports 4 input datatype representations:

Input TypeRangeScalingDescription
Uint raw0 to PWMxmaxUser handles scalingDirect register value
Float [0 1]0.0 to 1.0Block scales internallyNormalized unipolar
Float [-1 1]-1.0 to 1.0Block scales internallyNormalized bipolar
Float (s)Time in secondsBlock scales internallyPhysical time value

⚠️ Important: The block performs no range checking on inputs. Users must clamp values before the block input (e.g., using a Saturation block) to prevent overflow, regardless of datatype.

Float Inputs (3 options)

Selecting a float datatype accepts either single or double precision floating-point values. PIC32MK has hardware FPU—float inputs are processed efficiently. dsPIC33C uses software emulation—consider uint raw inputs for time-critical loops.

  • Float [0 1] (Unipolar): Normalized control where 0.0 represents 0% duty cycle and 1.0 represents 100% duty cycle. The block handles all scaling internally.
  • Float [-1 1] (Bipolar): Normalized control where -1.0 and +1.0 represent the duty cycle extremes. Suitable for H-bridge and bidirectional control where the sign indicates direction.
  • Float (s) (Physical): Specify duty cycle or period directly in seconds. The block converts to register values automatically.

Uint Raw Input

  • Uint raw: Integer values transfer directly to peripheral registers with no runtime computation—maximum efficiency.

The block creates workspace variables for user scaling:

VariableDescription
PWM1maxPWMNmaxMaximum timer count per channel (based on Max Period in GUI)

Fixed Period (period input disabled):

$$\text{DutyCycle}_\text{raw} = \text{DutyCycle}_\text{normalized} \times \text{PWMxmax}$$

DutyCycle_raw = DutyCycle_normalized × PWMxmax

Where DutyCycle_normalized is in the range [0, 1] relative to the fixed Max Period.

Variable Period (period input enabled):

When the period is dynamically controlled via block input, both period and duty cycle use the same scaling:

$$\text{Period}_\text{raw} = \text{Period}_\text{normalized} \times \text{PWMxmax}$$

Period_raw = Period_normalized × PWMxmax

$$\text{DutyCycle}_\text{raw} = \text{DutyCycle}_\text{normalized} \times \text{Period}_\text{raw}$$

DutyCycle_raw = DutyCycle_normalized × Period_raw

⚠️ Constraints:

  • Period input must always be < PWMxmax (Max Period in GUI)
  • Duty cycle normalizes to the actual period input, not PWMxmax
  • If ADC triggers from PWM events and drives the scheduler, changing period alters the control loop sample time

Center-Aligned vs Edge-Aligned

Center-Aligned Mode: The PWMxmax workspace variable is halved at configuration time because the timer counts both up and down. This applies to all input types (Uint raw, Float).

No runtime division: Since PWMxmax is already halved, the block passes integer inputs directly to registers without any runtime shift operation. Users simply scale their values to the halved PWMxmax.

Edge-Aligned Mode: The timer counts up only. PWMxmax represents the full period count. Duty cycle updates take effect at the next period boundary.

Asymmetric Center-Aligned Modes

MCPWM supports two asymmetric center-aligned modes:

  • Double Update: Duty cycle input is a 2-element vector [val_up val_down]. First value applied during up-count, second during down-count.
  • Simultaneous Update: Both up and down values updated at the same time, but can be different.

These modes enable asymmetric PWM waveforms for harmonic reduction and THD optimization.

Dead Time

  • GUI configuration: Specify in seconds (e.g., 1e-6 for 1 µs)

  • Integer block input: Dead time uses a different scaling base (full period, not halved).

    ⚠️ In center-aligned mode, multiply PWMxmax by 2 for dead time scaling:

$$\text{DeadTime}_\text{raw} = \frac{\text{DeadTime}_s \times 2 \times \text{PWMxmax}}{\text{MaxPeriod}_s}$$

DeadTime_raw = (DeadTime_s × 2 × PWMxmax) / MaxPeriod_s

  • Note: Dead time scaling is identical in center-aligned and edge-aligned modes—the factor of 2 compensates for the halved PWMxmax

Dead Time Compensation

When Positive dead time with compensation mode is enabled:

  • The block uses comparator feedback to adjust dead time based on current direction
  • Compensates for effective duty cycle changes caused by dead time insertion
  • Requires proper comparator configuration for zero-crossing detection

Block Parameters

This block uses a multi-tab GUI dialog with many parameters. Multiple instances can be used within a model to configure PWM channels with different characteristics or update at different rates.

Main Configuration Tab
ParameterDescription
PeriodPWM mode selection: Common - Edge aligned, Common - Center aligned - Symetric, Common - Center aligned - Symetric - Enhanced resolution, Common - Center aligned - Asymetric with double update, Common - Center aligned - Asymetric with simultaneous update, Independant - Edge aligned, Independant - Center aligned - Symetric, Independant - Center aligned - Asymetric with double update, Independant - Center aligned - Asymetric with simultaneous update
Clock sourcePrimary master timer or Secondary master timer
Clock source resolutionClock prescaler selection. Prompt shows effective resolution (e.g., “PWM res 8.333(ns) => periode up to 546(us)”)
Max Period (s)Maximum period in seconds. Defines PWMxmax workspace variables. Prompt shows calculated values
Initial Period (s)Starting period value
Initial Duty Cycle (s)Starting duty cycle in seconds
Initial second half Duty Cycle (s)For asymmetric center-aligned modes
Initial Phase (s)Starting phase shift (-1 for disabled)
Block output sample timeSample time for block outputs
Channel Configuration Tab
ParameterDescription
High/Low side ModeComplementary, Redundant, or Push Pull
PWM 1 ChannelPWMxH & PWMxL output enabled, PWMxH output enabled, PWMxL output enabled, not used, or Enabled with NO output
Swap PWM1_H and PWM1_LSwap high and low outputs
invert PWM1_H output polarityActive low output. Prompt shows pin (e.g., “B14 [4] active low”)
invert PWM1_L output polarityActive low output. Prompt shows pin
PWM 2 ChannelChannel 2 enable options
Swap PWM2_H and PWM2_LSwap high and low outputs
invert PWM2_H/L output polarityActive low outputs
Parameters repeat for PWM 3-12 depending on device
Dead Time Tab
ParameterDescription
Dead Time ControlDisabled, Positive dead time, Negative dead time (edge aligned only), or Positive dead time with compensation mode
Dead Time High-side (s)Dead-time duration for high-side transitions
Dead Time Low-side (s)Dead-time duration for low-side transitions
Dead Time CompensationComparator source for compensation mode
Fault Configuration Tab
ParameterDescription
Fault SourceFault input selection per PWM: Fault pin inputs, Digital comparator outputs, Software fault, or Combination (OR/AND logic)
Fault PinPhysical fault input pin selection
Fault Pin PolarityActive high or active low
Fault ModeLatched (requires manual clear) or Cycle-by-cycle (auto-restart when fault clears)
Fault PWMxH stateOutput state during fault: Low, High, or High-Z
Fault PWMxL stateOutput state during fault: Low, High, or High-Z
Fault State is a block outputEnable Fault_Status output port
Leading-Edge Blanking Tab
ParameterDescription
Leading Edge BlankingEnable blanking after PWM transitions to ignore switching noise on fault inputs
LEB DurationBlanking time period (typically 200-1000 ns)
LEB Trigger SourceRising edge PWMxH, Falling edge PWMxH, or PWMxL edges
Triggers Tab
ParameterDescription
Primary Event Trigger (s)Special event compare for primary ADC trigger (SEVTCMP)
Secondary Event Trigger (s)Secondary special event trigger
Trigger 1 (s)ADC/DAC trigger point 1 per PWM
Trigger 2 (s)ADC/DAC trigger point 2 per PWM
Trigger 3 (s)ADC/DAC trigger point 3 per PWM
Trigger is a block inputEnable dynamic trigger adjustment per channel
Block Input Tab
ParameterDescription
Input FormatScalar (same value for all channels) or Vector (individual values)
Period is a block InputEnable period block input
Duty Cycle is a block InputEnable duty cycle block input per channel
Phase Shift is a block InputEnable phase shift block input per channel
Dead Time is a block InputEnable dynamic dead-time adjustment

Additional Features

PWM-ADC Synchronization

MCPWM provides multiple trigger outputs (TRIG1..3, SEVTCMP) to precisely coordinate ADC conversions with PWM waveform timing.

Option 3: PWM → ADC → Task (Motor Control Standard)

Configuration Example (Center-Aligned PWM):

% In MCPWM block:
Period = 'Center aligned - Symmetric';
Max_Period = 1/20e3;  % 20 kHz PWM frequency

% Trigger ADC at PWM valley (center point - all low-side FETs ON):
Primary_Event_Trigger = 0;  % Valley trigger

% In ADC block:
Trigger_Source = 'PWM1 Special Event';
Sample_Time = 'Inherited';  % ADC rate follows PWM (20 kHz)

Option 4: Multiple Triggers per PWM Period (Advanced Control)

MCPWM supports up to 3 trigger outputs per PWM generator for high-performance control requiring multiple measurements per PWM cycle:

% Center-aligned PWM with 3 trigger points:
Period = 'Center aligned - Symmetric';

% Trigger 1: Before valley (rising edge phase currents)
Trigger_1 = Period * 0.4;

% Trigger 2: At valley (all low-side ON)
Primary_Event_Trigger = 0;

% Trigger 3: After valley (falling edge phase currents)
Trigger_2 = Period * 0.6;

Best Practices

ApplicationRecommended StrategyKey Settings
3-Phase Motor FOCPWM → ADC → TaskCenter-aligned, SEVTCMP = 0 (valley)
Single Shunt FOCMultiple TriggersTRIG1, TRIG2 at sector transitions
PFC Boost ConverterPWM → ADC → TaskEdge-aligned, SEVTCMP at 90% period
Interleaved ConvertersMultiple TriggersIndependent time bases, phase-shifted triggers

Usage Examples

Example 1: 3-Phase Motor with Dead-Time Compensation

% Configuration:
Period = 'Center aligned - Symmetric';
Dead_Time_Control = 'Positive dead time with compensation mode';

% Dead-time settings:
Dead_Time_High = 1.2e-6;  % 1.2 µs high-side
Dead_Time_Low = 1.0e-6;   % 1.0 µs low-side

% Compensation comparator (current sensing):
% Dead-time automatically adjusted based on current direction

Example 2: Overcurrent Protection with LEB

% Fast overcurrent shutdown with noise immunity:
Fault_Mode = 'Cycle-by-cycle';       % Auto-restart when fault clears
Fault_State = 'All Low';             % Safe state

% Leading-Edge Blanking:
Leading_Edge_Blanking = 'on';
LEB_Duration = 500e-9;               % 500 ns blanking
LEB_Trigger = 'Rising edge PWMxH';   % Blank after high-side turn-on

Troubleshooting

Fault Lockout

Cause: Latched fault mode requires manual clear.

Solution: Send pulse to Fault_Clear input, or change to cycle-by-cycle mode for auto-restart.

False Fault Triggers

Solutions:

  • Enable Leading-Edge Blanking with appropriate duration (typically 200-1000 ns)
  • Adjust LEB trigger source to match noise characteristic
  • Filter fault comparator inputs in hardware (RC filter)

Dead-Time Compensation Not Working

Checklist:

  • ✅ Dead Time Control set to “Positive dead time with compensation mode”
  • ✅ Comparator configured and connected to current sensor
  • ✅ Comparator threshold properly set for zero-crossing detection
  • ✅ Comparator polarity matches current direction

Supported Device Families

FamilyRepresentative DevicesKey Features
PIC32MKPIC32MK MCJ/MCM/MCA familiesFull MCPWM with digital comparators, advanced fault handling
dsPIC33C (selected)33CH, 33CK families with MCPWMHigh-speed MCPWM, multiple fault sources
dsPIC33ALatest 33A familiesEnhanced MCPWM with fine edge placement

See Also

Datasheets