simple PWM – DSPIC30F2010

Photo of author

By Jackson Taylor

Home » Electronics Topics » dsPIC Microcontroller » simple PWM – DSPIC30F2010

As an Amazon Associate we earn from qualifying purchases.

The dsPIC30F2010 is a 16-bit microcontroller from Microchip that is popular for motor control and power electronics projects. One of the first things most people want to do with it is generate a clean PWM signal. This article shows a short, working example that produces two square waves at the same time: 100 kHz and 200 kHz, followed by a version with 100 kHz and 800 kHz. It also explains how the period and duty cycle values are calculated, so you can adapt the code to your own frequency.

The code is written in mikroBasic (the PWM_Init, PWM_Start and PWM_Set_Duty library calls come from its PWM library). The calculations, however, apply to any language you use on this chip.

What You Need

  • A dsPIC30F2010 on a development board or breadboard circuit
  • A 16 MHz crystal as the clock source (the calculations below assume this)
  • mikroBasic for dsPIC, or another compiler with an equivalent PWM library
  • A programmer for the chip
  • An oscilloscope to check frequency and duty cycle

100KHz AND 200KHz SQUARE WAVE USING DSPIC

This first example runs two PWM channels at different frequencies. Channel 1 runs at 100 kHz using timer 2, and channel 2 runs at 200 kHz using timer 3. Because each channel uses its own timer, the two frequencies are independent of each other. The oscilloscope capture below shows the result.

Hardware Hack

Stop Buying New Batteries for Your Tech

Electronics are expensive, but their power sources don't have to be. Discover the simple engineering trick to recondition dead batteries and bring your gadgets back to 100% capacity.

Watch the Free Video
DSPIC square wave diagram

The video below shows the same setup running.

PROGRAM

program BLINK
dim current_duty, current_duty1, pwm_period1 , pwm_period2 as word
main:
  PORTD = 0                ' set PORTD to 0
  TRISD = 0                ' designate PORTD pins as output
  current_duty   = 20      ' initial value for current_duty
  current_duty1 = 10       ' initial value for current_duty1
  pwm_period1 = PWM_Init(100000 , 1, 1, 2)   'frequency ,channel no.,timer prescale value(1, 8, 64, and 256 ) ,timer no (2 or 3)
  pwm_period2 = PWM_Init(200000, 2, 1, 3)    'frequency ,channel no.,timer prescale value(1, 8, 64, and 256 ) ,timer no (2 or 3)
  PWM_Start(1)
  PWM_Start(2)
  PWM_Set_Duty(current_duty,   1)   ' Set current duty for PWM1
  PWM_Set_Duty(current_duty1, 2)   ' Set current duty for PWM2   'OC2RS=50---duty cycle actuall writes to this register
  while (TRUE)                     ' endless loop
  wend
end.

How the code works

  1. Port setup: PORTD = 0 clears the port and TRISD = 0 makes the PORTD pins outputs.
  2. PWM_Init: takes four arguments: the PWM frequency in Hz, the channel number, the timer prescaler (1, 8, 64 or 256) and the timer to use (2 or 3). It returns the period value for that channel, which is stored in pwm_period1 and pwm_period2.
  3. PWM_Start: starts the output on each channel.
  4. PWM_Set_Duty: sets the duty value for a channel. The comment in the code notes that the value is written to a duty register (OC2RS for channel 2).
  5. Endless loop: once started, the PWM is generated by hardware, so the main loop can stay empty or do other work.
See also
ARDUINO BASED BOOST CONVERTER SIMULATION IN PROTEUS

The program name “BLINK” is only a leftover from the template project. It has nothing to do with blinking an LED, and you can rename it.

HOW TO CALCULATE PWM DUTY CYCLE

The duty value you pass to PWM_Set_Duty is a count, not a percentage. It only makes sense relative to the period value, which depends on the output frequency, the clock and the prescaler. These are the conditions used in this example:

  • 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

The factor of 4 reflects that the timer counts at one quarter of the oscillator frequency. The result is the highest count the duty value can reach, which is the period value that PWM_Init returns.

FOR 100KHz

Maximum duty ratio = 16MHz / (100KHz *4 *1) – 1 = 39

FOR 200KHz

Maximum duty ratio = 16MHz / (200KHz *4 *1) – 1 = 19

I HAVE USED 50% DUTY RATIO FOR BOTH

Sustainable Tech

Keep Toxic E-Waste Out of Landfills

Don't throw away your failing laptop, phone, or car batteries. Learn how to recondition them at home in just a few hours. It's better for the planet—and your wallet.

Learn How It Works

Choosing the duty value

The timer counts from 0 up to the period value, so a full cycle is the period value plus one counts. For 100 kHz that is 40 counts, and for 200 kHz it is 20 counts. A 50% duty cycle is therefore half of each:

  • 100 kHz: 20 out of 40 counts, so the duty value is 20
  • 200 kHz: 10 out of 20 counts, so the duty value is 10

These are the values used in the program above (current_duty = 20 and current_duty1 = 10). For another duty cycle, multiply the number of counts per cycle by the fraction you want. For example, 25% at 100 kHz would be 10.

800KHz SQUARE WAVE USING DSPIC

In the second example, channel 1 stays at 100 kHz and channel 2 is raised to 800 kHz. Only the frequency passed to PWM_Init and the duty value for channel 2 change.

See also
KEYPAD INTERFACING WITH PIC16F877A SIMULATION IN PROTEUS AND MIKRO C
800KHz DSPIC square wave diagram

PROGRAM

program BLINK
dim current_duty, current_duty1, pwm_period1 , pwm_period2 as word
main:
  PORTD = 0                ' set PORTD to 0
  TRISD = 0                ' designate PORTD pins as output
  current_duty   = 20      ' initial value for current_duty
  current_duty1 = 2        ' initial value for current_duty1
  pwm_period1 = PWM_Init(100000 , 1, 1, 2)   'frequency ,channel no.,timer prescale value(1, 8, 64, and 256 ) ,timer no (2 or 3)
  pwm_period2 = PWM_Init(800000, 2, 1, 3)    'frequency ,channel no.,timer prescale value(1, 8, 64, and 256 ) ,timer no (2 or 3)
  PWM_Start(1)
  PWM_Start(2)
  PWM_Set_Duty(current_duty,   1)   ' Set current duty for PWM1
  PWM_Set_Duty(current_duty1, 2)   ' Set current duty for PWM2   'OC2RS=50---duty cycle actuall writes to this register
  while (TRUE)                     ' endless loop
  wend
end.

Resolution at 800 kHz

Using the same formula, 16MHz / (800KHz *4 *1) – 1 = 4. A full cycle is only 5 counts, so the duty value can only move in steps of 20%. A duty value of 2 gives about 40%, and 3 gives about 60%. An exact 50% is not possible at this frequency with these clock settings.

This is the main trade-off with a fixed clock: the higher the PWM frequency, the fewer duty steps you have. If you need finer duty control at a high frequency, you need a faster instruction clock, or you have to accept a lower PWM frequency.

Tips for Getting a Clean Signal

  • Set the clock in your project. The compiler must know the oscillator frequency. If the project setting does not match your 16 MHz crystal, every output frequency will be off by the same ratio.
  • Check the pins in the datasheet. Confirm which pins carry the PWM outputs for your package before wiring the scope probe.
  • Use short probe grounds. At 800 kHz, a long ground lead adds ringing that looks like a bad signal but is a measurement problem.
  • Keep the prescaler at 1 for high frequencies. A larger prescaler lowers the timer resolution and limits how fast you can go.
  • Recalculate after any change. Changing the crystal, frequency or prescaler changes the period value, so the duty value must be recalculated.
See also
ANALOG TO DIGITAL CONVERTER ADS7816 INTERFACING WITH ARDUINO

Where to Go Next

Fixed-duty square waves are the starting point for most power electronics work. If you want to see how varying the duty cycle over time creates a sine output, read our guide on PWM and sine wave basics. For a lower-cost board with a similar goal, there is a project on building a sine wave inverter using Arduino.

If you plan to drive power switches, it also helps to simulate first. See the H-bridge square wave inverter simulation for how the switching works, and the three-phase inverter simulation for a larger example.

FAQ

How do I change the PWM frequency on the dsPIC30F2010?

Change the first argument of PWM_Init. Then recalculate the period with the formula above and update the duty value so it stays at the duty cycle you want.

Why is my duty value not a percentage?

The library takes a count that is compared against the timer period. To get a percentage, divide the duty value by the number of counts in a cycle (the period value plus one).

Can I run two PWM channels at different frequencies?

Yes, as long as the channels use different timers. In this example, channel 1 uses timer 2 and channel 2 uses timer 3, so they can run at 100 kHz and 200 kHz, or 100 kHz and 800 kHz.

Do I need mikroBasic to use this chip for PWM?

No. The same result can be achieved in C or assembly by setting up the timer period and output compare registers directly. The period formula stays the same.

Revive Your Dead Electronics

Got a drawer full of dead gadgets or a failing car battery? Instead of paying a premium for replacements, use this simple trick to easily recondition them right from home.

See How It Works