PWM control with ramp up/down using PIC


Thread Starter

Iain Jones

I am trying to design a 200Amp DC motor controller using a PIC Microcontroller. The PWM control is not the problem but I need to have
selectable ramp up and ramp down times to give smooth running of the motor. Any suggestions, examples, ideas etc. would be appreciated.
If I need help w/ a PIC program I usually go to the following forum:


You can get helpful and expert advice within a couple of hours.

My thoughts are that you will need additional circuitry, and you have 2 options: 1) use the PWM feature and add a ramp-up/ramp-down circuit
using a couple of op-amps, 2) use one of the 8-bit output ports and connect to a D/A. You may not be able to use the PWM feature because
you'll only get a 0V OR 5V output (no analog for ramping). If you use a d/a, the code would be:

for(x=0; x++; x=255) //ramp up in 255usec
delay_ms(100); //stay high for 100msec
for (x=255; x--; x=0) //ramp down in 255usec
delay_ms(100); //stay low for 100msec

That's my guess, but I'd definitely check out the forum. Good luck!


Bill Gausman

It takes a couple of additional registers in the PIC. The user would select the ramp time perhaps with a dip switch. Here's how you might ramp from an old value to a new value.

Start with old PWM value

Compare to the new desired value.If not equal, increment or decrement the PWM register.

Wait for an amount of time determined by the user selected ramp time, and do the routime again. It will ramp smoothly and linearly until the delivered PWM value is equal to the desired PWM value.

If the ramp time is long (ie. minutes), consider using AC line crossings as your ramp time counter time base, rather than oscillator cycles.

Bill Gausman, PE
If your using a PIC with a built in PWM module like the 16F873 or 16F628, this would be a easy task.
Supose your using a resolution of 8 bits, this would result in 255 steps.
You can design your firmware to increase the PWM register from time to time, the size of the delay between each step defines the size of
the ramp.
EX: 5S/255 = 20mS => 20ms betweem each step results in a 5S ramp.
To chose how long the ramp will be, you will need anoter register.