COMPLEMENTARY PWM

Photo of author

By Jackson Taylor

COMPLEMENTARY PWM  AT 5KHz

COMPLEMENTARY PWM AT 5KHz

PROGRAM (for 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 (for 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 60KHz  

COMPLEMENTARY PWM AT 60KHz

PROGRAM (for C language)

//i have used crystal of 16 MHz
//output at 60KHz
void main()
{
    unsigned int pwm_period, current_duty ;
    current_duty=100  ;                   //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 (for 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 PWM DUTY CYCLE

  • PWM duty ratio depend on output frequency
  • 16MHz  crystal as clock source
  • PRESCALE VALUE ARE  1, 8, 64, and 256

Maximum duty ratio =( (FOSC  /(FOUTPUT  * 4  * PRESCALE VALUE)  -1)*2

FOR 5KHz
Maximum duty ratio = (16MHz / (5KHz *4 *1)  – 1)*2=1598

FOR 60KHz ,Maximum duty ratio = (16MHz / (60KHz *4 *1)  – 1)*2 = 130
I HAVE USED 50% DUTY RATIO FOR BOTH

COMPLEMENTARY PWM  AT 240KHz

COMPLEMENTARY PWM AT 240KHz

PROGRAM (for C language)

//i have used crystal of 16 MHz
//output at 60KHz
void main()
{
    unsigned int pwm_period, current_duty ;
    current_duty=100  ;                   //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 (for 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(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.

COMPLEMENTARY PWM  AT 60KHz WITH DEAD TIME CONTROL

COMPLEMENTARY PWM AT 60KHz WITH DEAD TIME CONTROL
'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.
See also
MATLAB - IMAGE PROCESSING