As an Amazon Associate we earn from qualifying purchases.
Complementary PWM gives you two outputs that switch in opposite states: when one is high, the other is low. It is the signal pair you need to drive a half-bridge, where a high-side and a low-side switch must never conduct at the same time. This article shows working examples at 5 kHz, 60 kHz and 240 kHz, explains how to calculate the duty value, and finishes with dead time control.
The examples use the mikroC motor control PWM library (PWM1_MC_Init, PWM1_MC_Set_Duty, PWM1_MC_Start) and the DTCON1 dead time register. These belong to the dedicated motor control PWM module found on dsPIC30F-class controllers. The PIC16F877A‘s CCP modules only give single-ended PWM, with no complementary pair and no dead time. If you are working with that chip, see our PIC16F877A PWM tutorial instead. For a smaller dsPIC example, see this simple dsPIC30F2010 PWM example.
How complementary PWM works
A normal PWM output is one pin with a duty cycle. Complementary PWM uses a pair of pins, here called 1H and 1L. 1L is the inverse of 1H, so a 31% duty on 1H means 1L is high for the remaining 69%.
- Frequency is set by the first argument of PWM1_MC_Init, in Hz.
- Channel enable is set by the third argument. The value 0x11 enables the 1L and 1H pins.
- Duty is set with PWM1_MC_Set_Duty. It is a raw timer count, not a percentage.
- Return value of PWM1_MC_Init is the calculated timer period.
If you are new to the topic, our article on the basics of PWM covers duty cycle and frequency in more detail.
Complementary PWM at 5 kHz
This example uses a 16 MHz crystal with no PLL. The duty value of 500 gives about 31%, based on the maximum duty count of 1598 calculated further below.
Program (C language)
//i have used crystal of 16 MHz and no PLL
//output at 5KHz
void main()
{
unsigned int pwm_period, current_duty ;
current_duty=500 ; //duty ratio 31%
pwm_period = PWM1_MC_Init(5000, 0, 0x11, 0); //enable 1L AND 1H pwm pins
PWM1_MC_Set_Duty(current_duty, 1) ;
PWM1_MC_Start();
//DTCON1=0 //DEAD TIME CONTROL
while (1);
}
Program (Pascal language)
'i have used crystal of 16 MHz and no PLL
program BLINK
dim pwm_period, current_duty as word
main:
current_duty=500 'duty cycle of 31%
pwm_period = PWM1_MC_Init(5000, 0, 0x11, 0) ' Pwm_Mc_Init returns calculated timer period.
PWM1_MC_Set_Duty(current_duty, 1)
PWM1_MC_Start()
while (TRUE)
wend
end.
Complementary PWM at 60 kHz
At a higher frequency the timer period is much shorter, so the duty values are smaller. Here the maximum duty count is about 130, and a value of 65 gives a 50% duty ratio. (The earlier C listing used 100 with a 50% comment, which does not match the formula. It is corrected below.)
Program (C language)
//i have used crystal of 16 MHz and no PLL
//output at 60KHz
void main()
{
unsigned int pwm_period, current_duty ;
current_duty=65 ; //duty ratio 50%
pwm_period = PWM1_MC_Init(60000, 0, 0x11, 0); //enable 1L AND 1H pwm pins
PWM1_MC_Set_Duty(current_duty, 1) ;
PWM1_MC_Start();
//DTCON1=0 //DEAD TIME CONTROL
while (1);
}
Program (Pascal language)
'i have used crystal of 16 MHz and no PLL
program BLINK
dim pwm_period, current_duty as word
main:
current_duty=65 'duty ratio 50%
pwm_period = PWM1_MC_Init(60000, 0, 0x11, 0) ' Pwm_Mc_Init returns calculated timer period.
PWM1_MC_Set_Duty(current_duty, 1)
PWM1_MC_Start()
while (TRUE)
wend
end.
How to calculate the PWM duty value
The usable duty range depends on the output frequency, the clock and the prescaler. With a 16 MHz crystal as the clock source, the prescale values available are 1, 8, 64 and 256.
Maximum duty count = ((FOSC / (FOUT × 4 × prescale)) − 1) × 2
- 5 kHz: (16 MHz / (5 kHz × 4 × 1) − 1) × 2 = 1598
- 60 kHz: (16 MHz / (60 kHz × 4 × 1) − 1) × 2 ≈ 130
To get a duty percentage, divide your chosen value by the maximum. For 5 kHz, 500 / 1598 is about 31%. For 60 kHz, 65 / 130 is 50%. A 50% duty ratio is used in the 60 kHz and 240 kHz examples.
Complementary PWM at 240 kHz
For 240 kHz the 16 MHz crystal is used with a 4x PLL, which quadruples the clock. The ratio of clock to output frequency is then the same as in the 60 kHz case without PLL, so the maximum duty count is again about 130 and 65 gives 50%.
The original listings for this section passed 60000 to PWM1_MC_Init, which would give a 60 kHz output. The code below uses 240000 so the frequency matches the heading.
Program (C language)
//i have used crystal of 16 MHz and PLL of 4
//output at 240KHz
void main()
{
unsigned int pwm_period, current_duty ;
current_duty=65 ; //duty ratio 50%
pwm_period = PWM1_MC_Init(240000, 0, 0x11, 0); //enable 1L AND 1H pwm pins
PWM1_MC_Set_Duty(current_duty, 1) ;
PWM1_MC_Start();
//DTCON1=0 //DEAD TIME CONTROL
while (1);
}
Program (Pascal language)
'i have used crystal of 16 MHz and PLL of 4
program BLINK
dim pwm_period, current_duty as word
main:
current_duty=65 'duty ratio 50%
pwm_period = PWM1_MC_Init(240000, 0, 0x11, 0) ' Pwm_Mc_Init returns calculated timer period.
PWM1_MC_Set_Duty(current_duty, 1)
PWM1_MC_Start()
while (TRUE)
wend
end.
Complementary PWM at 60 kHz with dead time control
Real switches turn off more slowly than they turn on. If 1H and 1L swap states at exactly the same instant, both devices can conduct briefly and cause shoot-through. Dead time adds a short gap in which both outputs are off at every transition.
The program below is the 60 kHz example with DTCON1 set to 15. A larger value gives a longer gap, so choose it from the switching times of your own transistors or MOSFETs and check the result on an oscilloscope. In C, the same setting is written as DTCON1 = 15; after PWM1_MC_Start().
'i have used crystal of 16 MHz and no PLL
program BLINK
dim pwm_period, current_duty as word
main:
current_duty=65 'duty ratio 50%
pwm_period = PWM1_MC_Init(60000, 0, 0x11, 0) ' Pwm_Mc_Init returns calculated timer period.
PWM1_MC_Set_Duty(current_duty, 1)
PWM1_MC_Start()
DTCON1=15
while (TRUE)
wend
end.
Dead time takes a small part of each period, so the effective duty is slightly lower than the set value. This matters more at high frequencies such as 240 kHz, where each period is short. For another take on generating fast PWM on a different board, see 62.5 kHz PWM on Arduino.
Frequently asked questions
What is complementary PWM?
It is a pair of PWM outputs where one is always the inverse of the other. It is mainly used to drive half-bridge and full-bridge circuits, such as motor drivers and inverters.
Why do I need dead time?
Dead time prevents both switches in a half-bridge from being on together while one is still turning off. Without it, current can flow straight from supply to ground through both devices.
Why does the maximum duty value change with frequency?
The duty value is a timer count. A higher output frequency means a shorter period, so fewer counts fit in one cycle. That is why 5 kHz allows about 1598 while 60 kHz allows about 130.
Can the PIC16F877A do complementary PWM with dead time?
Not in hardware. Its CCP modules produce single-ended PWM. For a complementary pair with dead time, use a controller with a motor control PWM module, such as a dsPIC30F, or build the dead time into your gate driver.