Updated date:

How to Generate PWM on dspic30f and dspic33f

The author completed his final year engineering project with the dsPic micro-controllers, gaining extensive insight in these devices.

dspic30f and dspic33f series micro-controllers are advanced 16 bit processors from microchip which can b used for a variety of PWM applications. This walk-through guide will teach you how to configure the PWM modules in the dspic to get the desired PWM output.

The dspic30f4011 being programmed with PicKit 2.

The dspic30f4011 being programmed with PicKit 2.

This sample code can be used to initialize all the registers and configuration bits required to get the desired PWM output.

PWM Code Example for dspic30f and dspic33f

void PWM_Init( void )
{
    TRISE = 0x00; // make sure PWM pins are set to be outputs
    PORTE = 0x00; // clear the outputs 

    PTCONbits.PTOPS = 1; // PWM timer post-scale
    PTCONbits.PTCKPS = 0; // PWM timer pre-scale
    PTCONbits.PTMOD = 2; // PWM operates in Up-down Mode continuously

    PTMR = 0; // PWM counter value, start at 0

    PTPER = 19999; // PWM Timebase period

    PWMCON1bits.PMOD3 = 0; // PWM in complimentary mode
    PWMCON1bits.PMOD2 = 0; // PWM in complimentary mode
    PWMCON1bits.PMOD1 = 0; // PWM in complimentary mode
    PWMCON1bits.PEN3H = 1; // PWM High pin is enabled
    PWMCON1bits.PEN2H = 1; // PWM High pin is enabled
    PWMCON1bits.PEN1H = 1; // PWM High pin is enabled
    PWMCON1bits.PEN3L = 1; // PWM Low pin enabled (direction control later?)
    PWMCON1bits.PEN2L = 1; // PWM Low pin enabled (direction control later?)
    PWMCON1bits.PEN1L = 1; // PWM Low pin enabled (direction control later?)

    //PWMCON2 = 0x0000; // PWM update info

    DTCON1bits.DTAPS = 0;  //DeadTime pre-scaler
    DTCON1bits.DTA = 59;   //DeadTime value for 4 us. 

    //FLTACON = 0x0000; // Fault A Control

    //OVDCON = 0x0000; // Override control info

    // Duty Cycle has a max value of 2xPeriod since output  
    // can change on rising or falling edge of Tcy
    PDC1 = 19999; // PWM#1 Duty Cycle register (11-bit)
    PDC2 = 19999; // PWM#2 Duty Cycle register (11-bit)
    PDC3 = 19999; // PWM#3 Duty Cycle register (11-bit)

    PTCONbits.PTEN = 1; // Enable PWM Timerbase!
}

Important Registers used for PWM Initialization in dspic

RegisterFunction

PTPER

PWM time period register holds the time base value.

PDC

PWM duty cycle register holds the duty cycle value.

PTCON

PWM configuration registers has configuration bits.

PTCON1

PWM configuration registers has configuration bits.

DTCON1

Dead time configuration register.

PTMR

This is usually set to zero.

1: Select the PWM Mode of Operation

This is done with the help of PTMOD configuration bits in the PTCON register, as shown in line 8 of our code.

PTMODMode

11

Continuous Up/Down mode with interrupts for double PWM update

10

Continuous Up/Down counting mode

01

Single event mode

00

Free-running mode

2: Calculate the PWM Time Base Period

PWM time base period can be calculated with the help of the formula given below:

configure-pwm-on-dspic30f-and-dspic33f

Where:

1 / Tcy = 4 / ( Oscillator frequency * PLLx )

Let's calculate this for a time base period of 1 ms. Oscillator frequency is 10 MHz and PLL is set at x8.

configure-pwm-on-dspic30f-and-dspic33f

Solving for PTPER gives a value of 19,999 which is then loaded into the PTPER register in line 12.

  • PTPER can be loaded with a maximum value of 32,768.
  • If the calculated value comes greater then this, than the prescaler should be adjusted.

3: Calculate the Duty Cycle Value

Duty cycle value is calculated using the same formula used for time base value (PTPER) calculation, except that in place of time base period, the required high time of the pulse is kept and after solving for PTPER the answer is:

  1. Multiplied by 2 and
  2. Loaded into the PDC register, as shown in line 35.

In this example, for 50% duty cycle, 0.5 ms is kept in place of 1 ms in the above given formula. Solving for PTPER yields 9,999. The value loaded into the PDC register is 19,999.

The PWM output obtained with this configuration is shown below:

Edge-aligned PWM in free running mode. Period = 1 ms, duty cycle = 50%.

Edge-aligned PWM in free running mode. Period = 1 ms, duty cycle = 50%.

PWM in Up-Down Counting Mode

Up-down counting mode gives a PWM which is aligned to the center. This is particularly useful in vector-control applications.

  • In this mode PWM time base is twice of that calculated for the free-running mode. For example PTPER = 19,999 will give a period of 2 ms rather then 1 ms.
  • Same goes with the cuty cycle values in PDC registers.
Center-aligned PWM with period = 2 ms and duty cycle = 50%.

Center-aligned PWM with period = 2 ms and duty cycle = 50%.

PWM in Complimentary Mode.

Complimentary mode is a unique feature of dspic30f and dspic33f series. It allows us to configure two PWM channels in complimentary mode such that output of one channel is exactly opposite of the other channel.

To configure this,

  • The PMOD bit in the PTCON1 register needs to be written with a zero, as mentioned in line 14 - 16.
PMODMode

0

Complimentary mode

1

Independent mode

  • PWM high enable bit or PENxH and the corresponding PWM low enable bit PENxL must be written with a one, as mentioned in lines 17 - 22.
Center-aligned PWM in complimentary mode.

Center-aligned PWM in complimentary mode.

Dead Time Insertion in dspic

When the PWM is configured in complimentary mode, a dead time can be easily inserted between the two complimentary channels by configuring the DTCON1 register.

  • DTAPS bit is used to select the dead time prescaler.
  • DTA is loaded with a 6-bit integer value to determine the time duration of the dead time to be inserted. It can be calculated from the same formula of PTPER value calculation, given above.
  • Since this is only a 6-bit value it cannot be greater then 64. If a larger value comes after calculation, the prescaler needs to be adjusted.
  • Line 27 shows a value of 59 calculated for 4 microseconds keeping the prescaler at zero.
4 us dead time inserted between two complimentary channels. (Probe at blue channel is showing a bit slow response because it was cheap!)

4 us dead time inserted between two complimentary channels. (Probe at blue channel is showing a bit slow response because it was cheap!)

The PWM interrupt in dspic30f and dspic33f

A PWM interrupt is available in the dspic30f and dspic33f which can be triggered at various places during the PWM cycle.

ModePoint where interrupt is triggered

Continuous Up-down counting mode with interrupts for double update

At the start and end of each PWM cycle. Postscaler has no effect in this mode.

Continuous Up-down counting mode

At the start of each PWM cycle.

Single event mode

At the end of each PWM cycle. (When a compare match occurs between PTPER and PTMR.) Post scaler has no effect in this mode.

Free-running mode

At the end of each PWM cycle. (When a compare match occurs between PTPER and PTMR)

To enable a PWM interrupt the PWMIE bit in the IEC2 register must be set to 1.

void Interrupt_Init( void )
{
    IEC2bits.PWMIE = 1;  
}

The following is the standard code of the PWM interrupt function which will be called when the interrupt is triggered.

void __attribute__((interrupt, auto_psv)) _PWMInterrupt( void )               
{
    //your code here
    IFS2bits.PWMIF = 0;
}
    

More information on interrupts can be found in this detailed tutorial – How to use Interrupts in Pic MicroControllers

PWM Interrupt Postscaler

A postscaler may be used to reduce the frequency of interrupt triggering. For example it me be configured to trigger after every 1, 2, 4 or 8 PWM cycles, depending on the PTOPS bits in the PTCON register, as shown in line 6 of our reference code.

This article is accurate and true to the best of the author’s knowledge. Content is for informational or entertainment purposes only and does not substitute for personal counsel or professional advice in business, financial, legal, or technical matters.

Questions & Answers

Question: I am trying to obtain a complimentary mode for one PWM output using the reference program, dspic30f4011 and Pickit 3. The only thing I included is the configuration bits for internal frequency selection and the c-compiler, but can not get any output at the PWM1 pins?

Answer: Sometimes, when you change the configuration bits, the micro-controller stops working, and by that I mean, when the oscillator is not working correctly your code will not start executing.

So, in order to confirm that your controller has picked the correct oscillator configuration you should include an LED blink code in all your codes so as to confirm that the controller is powered on and working.

© 2017 StormsHalted

Comments

Indranil Mandal on August 17, 2020:

how to genetate PWM dspic33EPxxxMC20x please help me

Anirban Biswas on August 16, 2020:

Important Registers and timer used for PWM Initialization in dspic33ep128mc202 for inverter pwm and motor control

StormsHalted (author) from Karachi, Pakistan on August 02, 2019:

When ever you try to download a new program after changing the configuration bits, keep an LED flashing code in the loop so that if the LED flashes you will know that the code is executing correctly and the problem is with the pwm part of the code. So try that first, may be your code didn't execute at all.

Nicholas Maina on August 02, 2019:

I tried generating a complimentary mode pwm1 output using dspic30f4011 and Pickit3(c compiler) with the reference program.I only added the confirguration bits .The program downloaded into the pickit but could not get any output.Is there any other line of code that should be added to the reference program?

Gaston on October 22, 2018:

Thanks a lot !

StormsHalted (author) from Karachi, Pakistan on September 29, 2018:

For this you will need to use the PWM interrupt. Enough detail is given in the article.

Rakesh on September 26, 2018:

Hello

I am trying to generate pwm (50%) duty cycle but frequency should vary according to input frequency.(square wave is input). How to update ptper register, based on input frequency.

StormsHalted (author) from Karachi, Pakistan on July 06, 2018:

Two ways to do that:

1) Change the value of pdc register after a set time interval.

2) Use the pwm interrupt to write new value to pdc register after each cycle or two cycles or maybe more.

Hossein on April 24, 2018:

I tried to understand it from datasheet but i could not. thanks for this useful post.

Electronics on November 05, 2017:

Hi! I have exactly the same problem. Could someone help us???

someone on October 25, 2017:

Hello, im trying to compile this program for my dspic30f2020. I already declared de fuses and everything but once i try to compile it it shows me an error telling that PTCONbits and PWMCONbits are not defined. How can i solve it? thank you for your time

StormsHalted (author) from Karachi, Pakistan on September 10, 2017:

Complimentry PWM can be generated on two pins acting as complimentary pairs. More information is given in the article.

online79 on September 10, 2017:

Thank you StormsHalted, your post is very usefull. Sir, please help me to generate 2 pwm at any pin in dsPIC33 with 180 phase angle with adjustable duty cycle.

VENKATESWARAN on April 24, 2017:

Hello StormsHalted,

Thank you for posting this code, I want to learn dspic. can you tell me the ebook name.

StormsHalted (author) from Karachi, Pakistan on April 19, 2017:

Oscillator frequency is the frequency of clock input to the controller. It may be derived from an external or internal oscillator. PLL is the multiplier of clock frequency. If for example it is x4 oscillator frequency is multiplied by 4 to give the actual clock frequency that is being used to drive the controller.

Hafiz Idris on April 18, 2017:

Hello StormsHalted,

Thank you for posting this, it's actually help me a lot. can you please explain to me , what is actually oscillator freq and PLL and how should I know how much my PLL and oscillator freq is ? and I would be appreciate if you can upload picture and explanation about hardware(pickit and dspic) and how should be connected . Sorry for poor question , Im kindly a beginner to this part.

Looking forward to your reply sir and thank you.

Related Articles