View previous topic :: View next topic |
Author |
Message |
kidswizz
Joined: 21 Feb 2013 Posts: 16
|
PWM on DC Motor using 18F4550? |
Posted: Fri Feb 22, 2013 2:38 pm |
|
|
Hi, I am a beginner and I was wondering if you can guide me in using PWM to control motor shaft rotation speed. I am using the internal osc which is 8MHz. I have got the following code although I am not sure if it is correct. Thanks!
Code: | void PWM_init()
{
output_low(PIN_C1); // Set CCP1 output low
output_low(PIN_C2); // Set CCP2 output low
setup_ccp1(CCP_PWM); // Configure CCP1 as a PWM
setup_ccp2(CCP_PWM); // Configure CCP2 as a PWM
setup_timer_2(T2_DIV_BY_4,124,1); // Is this correct?
delay_ms(3000); //Is this needed?
while(1);//Is this needed?
} |
|
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Fri Feb 22, 2013 5:57 pm |
|
|
You need to post a SHORT but complete and compilable code which we can test.
Give us some clues about your hardware.
EXACTLY what are you trying to do?
The more you tell us the better.
Read the forum guidelines on the best ways to get help.
Mike |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Sat Feb 23, 2013 2:32 am |
|
|
Code: |
//Some comments inline
void PWM_init()
{
output_low(PIN_C1); // Set CCP1 output low
output_low(PIN_C2); // Set CCP2 output low
setup_ccp1(CCP_PWM); // Configure CCP1 as a PWM
setup_ccp2(CCP_PWM); // Configure CCP2 as a PWM
setup_timer_2(T2_DIV_BY_4,124,1); // Is this correct?
//Possibly. It depends on the frequency you want the PWM to run
delay_ms(3000); //Is this needed?
//Probably not. Looks like you have copied this from some code where the
//poster needed a delay before the PWM started.
while(1);//Is this needed?
//Uurgh. A 'while' like this is needed at the end of the 'main' code. Not here.
//Means the function will never exit, and the PWM duty cycle is never
//set, so nothing will happen....
}
|
Best Wishes |
|
|
kidswizz
Joined: 21 Feb 2013 Posts: 16
|
|
Posted: Sat Feb 23, 2013 10:51 am |
|
|
Hi, thanks for the reply guys. I am wondering how to find out the best frequency to PWM the DC motors? What are the advantages and disadvantages of higher and lower frequencies? Thank you all for your time. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Sat Feb 23, 2013 10:57 am |
|
|
Higher frequencies - for the same circuit, greater losses, but smoother, and with less audio noise. Increased RF noise though. Needs more power in the drive circuit.
Generally look for around 5KHz for a DC motor. Higher is unlikely to be needed.
Best Wishes |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Sat Feb 23, 2013 3:36 pm |
|
|
For minimum RFI, EMI and general kindness to the power supplies involved,
I tend to ALWAYS use a simple buck modulator for motor control.
Since you will need some sort of switch anyway, perhaps an actual power modulator device would be better.
I suggest a robust, simple "low side" N channel mosfet, with appropriate buck diode, inductor and filter capacitor. The exact value of the inductor and capacitor will depend on pwm frequency and peak load current.
add a gate current drive resistor of a hundred ohms and you are in phat city.
For the range of PWM frequencies your pic can produce, the selection of the Mosfet and buck diode depend on the maximum motor current and the voltage input.
Then the potential for radiated and conducted interference
can be nearly eliminated from your design.
If this is to be an actual product, it is very bad form to direct switch
the DC to the motor, as severe radio interference can be generated and radiated with ease |
|
|
|