CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

PWM on DC Motor using 18F4550?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
kidswizz



Joined: 21 Feb 2013
Posts: 16

View user's profile Send private message

PWM on DC Motor using 18F4550?
PostPosted: Fri Feb 22, 2013 2:38 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Feb 22, 2013 5:57 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Feb 23, 2013 1:43 am     Reply with quote

You didn't describe your motor driver circuit (external to the PIC).
Here's an example for an H-Bridge motor driver
http://www.ccsinfo.com/forum/viewtopic.php?t=29565&start=1
Ttelmah



Joined: 11 Mar 2010
Posts: 19520

View user's profile Send private message

PostPosted: Sat Feb 23, 2013 2:32 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Feb 23, 2013 10:51 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Feb 23, 2013 10:57 am     Reply with quote

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

View user's profile Send private message AIM Address

PostPosted: Sat Feb 23, 2013 3:36 pm     Reply with quote

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 Very Happy Very Happy Very Happy
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group