Consumer ElectronicsComputersCell PhonesHome Theater & AudioGraphic Design & Video EditingInternetIndustrial Technology

How to Generate PWM on dspic30f and dspic33f

Updated on March 4, 2017
alikhan3 profile image

The author is currently doing his final year engineering project with the dspic micro-controllers.

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

Register
Function
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.

PTMOD
Mode
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:

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.

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.

PMOD
Mode
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.

Mode
Point 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;
}
    

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.

© 2017 StormsHalted

Comments

    0 of 8192 characters used
    Post Comment

    • profile image

      VENKATESWARAN 3 months ago

      Hello StormsHalted,

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

    • alikhan3 profile image
      Author

      StormsHalted 3 months ago from Karachi, Pakistan

      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.

    • profile image

      Hafiz Idris 4 months ago

      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.