PWM and DC motor

Discuss PIC and electronic related things

Moderators: David Barker, Jerry Messina

Post Reply
User avatar
Coccoliso
Posts: 152
Joined: Mon Feb 17, 2014 10:34 am

PWM and DC motor

Post by Coccoliso » Sat Apr 26, 2014 9:51 pm

Hello,
I want to create a control for DC motor using PWM via buttons and adjust the duty cycle. I read the post http://sfcompiler.co.uk/phpBB3/viewtopi ... 55&start=0 and I noticed that the settings are based on 2 frequencies, 40 and 48 MHz.
I want to use an internal oscillator to 32 MHz or 20MHz quartz which settings to use ?

Code: Select all

#if (_clock = 48)
const TIMER_RELOAD_VALUE as word = 65380 + TIMER_RELOAD_OVERHEAD
#elseif (_clock = 40)
const TIMER_RELOAD_VALUE as word = 65406 + TIMER_RELOAD_OVERHEAD
#else
#error "compute timer value for clock freq"
#endif
I have to use a mini drill to 20,000 rpm at 12V, a MOSFET SSP2N60A what PWM frequency do you recommend?

Jerry Messina
Swordfish Developer
Posts: 1469
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: PWM and DC motor

Post by Jerry Messina » Sat Apr 26, 2014 10:46 pm

For that code you want to set the TIMER_RELOAD_VALUE to produce a 13.020us clock (76.8KHz).
TMR0 counts at Fosc/4 (four clocks per instruction on a PIC18), so for a 32MHz clock you have a 32MHz/4 = 8MHz (or 125ns) count rate.

So, the reload value should set TMR0 to rollover ever 13.020us/125ns = 104.16 clocks.
Since the timer counts up, subtract that from 65536 and that gives a reload value of 65536-104=65432 @ 32MHz
Based on that, I'm sure you can figure out the 20MHz value.

Do you really need the multiple outputs? If you're only doing a single channel or two using the PWM peripheral is a lot simpler.

I can't really help as far as what frequency to recommend for a drill motor.
Last edited by Jerry Messina on Sun Apr 27, 2014 12:06 pm, edited 1 time in total.
Reason: correct math

User avatar
Coccoliso
Posts: 152
Joined: Mon Feb 17, 2014 10:34 am

Re: PWM and DC motor

Post by Coccoliso » Sun Apr 27, 2014 9:35 am

Hello,
thank you for the quick response.
I thank you for telling me in 6 lines how to calculate the parameters in the example and you're absolutely right about the waste of lines of code to have a single PWM channel. In fact I started the wiki and I came across this article without any further research.
Then later I found this link http://digital-diy.com/swordfish-exampl ... a-pwm.html that, apart from the need of use a schottky diode protection, turns out to be exactly what hardware and intend to use and the example is in SF .. magnificent :D !

PS: in any case is an extension to an original routine SF :wink: ..

Jerry Messina
Swordfish Developer
Posts: 1469
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: PWM and DC motor

Post by Jerry Messina » Sun Apr 27, 2014 12:11 pm

The code in the PWM2.bas module should be a lot better for what you want to do.

There was an error in the calculations that I corrected. Timers set the IF when they overflow from FFFF->0000, so you need to subtract the count value from 65536, not 65535.

For example, to count 1 you need to set the value to 65536-1=65535.

Post Reply