The author completed his final year engineering project with the dsPic micro-controllers, gaining extensive insight in these devices.
What Are dspic30f and dspic33f?
dspic30f and dspic33f series micro-controllers are advanced 16-bit processors from microchip which can be used for a variety of PWM applications. This walkthrough guide will teach you how to configure the PWM modules in the dspic to get the desired PWM output.
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
Important Registers Used for PWM Initialization in dspic
PWM time period register holds the time base value.
PWM duty cycle register holds the duty cycle value.
PWM configuration registers have configuration bits.
PWM configuration registers have configuration bits.
Dead time configuration register.
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.
Continuous Up/Down mode with interrupts for double PWM update
Continuous Up/Down counting mode
Single event mode
2: Calculate the PWM Time Base Period
PWM time base period can be calculated with the help of the formula given below:
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 than this, then 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. After solving for PTPER, the answer is:
- Multiplied by 2 and
- 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:
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 duty cycle values in PDC registers.
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.
- 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.
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 than 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.
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.
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.
The following is the standard code of the PWM interrupt function which will be called when the interrupt is triggered.
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 may 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
StormsHalted (author) from Pullman, Washington, United States 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 Pullman, Washington, United States 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:
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 Pullman, Washington, United States 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 Pullman, Washington, United States 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:
Thank you for posting this code, I want to learn dspic. can you tell me the ebook name.
StormsHalted (author) from Pullman, Washington, United States 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:
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.