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 support@ccsinfo.com

50Hz PWM

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



Joined: 24 Sep 2008
Posts: 9

View user's profile Send private message

50Hz PWM
PostPosted: Tue Oct 21, 2008 12:21 am     Reply with quote

I am trying to control my DC motor using an electronic speed controller which controls the DC motor with a 50Hz PWM frequency. But i believe that with 8Mhz clock (Internal clock) the lowest frequency i can produce is about 480Hz.

The DC motor moves backwards when the duty cycle is 7.5% and forward at 9%.

I am using the PIC18F4550.

Exactly how do i produce a 50Hz signal? could someone help me on how to produce a 50Hz signal either using timer 1 or timer 2.

I will also be using one of the timers to control a servo for the steering.

Thank you.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Oct 21, 2008 12:45 am     Reply with quote

CCS has a driver for two servos. It's in this directory:
Quote:
c:\Program Files\PICC\Drivers\Servos.c


Here's an example program that shows how to use it:
http://www.ccsinfo.com/forum/viewtopic.php?t=34560&start=22
RF_Geek



Joined: 24 Sep 2008
Posts: 9

View user's profile Send private message

PostPosted: Tue Oct 21, 2008 1:15 am     Reply with quote

Thank you PCM for your help.

I will only be using one Servo, but the code helps, its a good example. I am trying to control the servo using a digital compass ($HCHDG string) the heading from the digital compass to control the servo.

Any thoughts on how to produce 50Hz PWM signal, and exactly how do we calculate the value for PR2.

I am trying to generate 50Hz signal, unfortunately i believe with a 8Mhz clock it is not possible to produce a signal smaller than 480Hz.

Any Suggestions.


Last edited by RF_Geek on Tue Oct 21, 2008 1:58 am; edited 1 time in total
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Oct 21, 2008 1:24 am     Reply with quote

Look at the end of this thread for links to software PWM code:
http://www.ccsinfo.com/forum/viewtopic.php?t=35160
RLScott



Joined: 10 Jul 2007
Posts: 465

View user's profile Send private message

Re: 50Hz PWM
PostPosted: Tue Oct 21, 2008 5:17 am     Reply with quote

RF_Geek wrote:
...The DC motor moves backwards when the duty cycle is 7.5% and forward at 9%....


As you look through the various software PWM routines that have been mentioned, keep in mind that the RC servo is not really responding to the duty cycle percentage as such. It is responding to the actual on-time (around 1 to 2 mSec). So if your overall period changed from 20 mSec to 15 mSec, but the on-time remained fixed at 1.2 mSec, there would be no change in the servo's response, even though the duty cycle percentage changed from 6% to 8%.
_________________
Robert Scott
Real-Time Specialties
Embedded Systems Consulting
John P



Joined: 17 Sep 2003
Posts: 331

View user's profile Send private message

PostPosted: Wed Oct 22, 2008 1:50 am     Reply with quote

The regular PWM is indeed too fast for the 50Hz rate you need for a hobby servo. But you can easily set up a timer to give you the "on" time and then set it again to give you the "off" time. Just think in terms of time intervals rather then a PWM duty cycle. And as Robert Scott said, the "on" time is what really counts, so you might choose to keep a fixed "off" time, and save some calculating.
Guest








PostPosted: Thu Oct 23, 2008 9:25 pm     Reply with quote

Thank you All,

Could you guys help me on how to setup the on and off time. I know how to setup the PWM pulse and how to vary the duty cycle.

That was for the Servo, i still need the 50Hz to run my DC motor.

Currently i am playing with the Servos.c file in the PIC C library, but my servo is still now working.

Thank you guys, i really appreciate all the help.
RF_Geek



Joined: 24 Sep 2008
Posts: 9

View user's profile Send private message

PostPosted: Fri Oct 24, 2008 5:43 pm     Reply with quote

PCM and RLScott,

Thank you very much.

Yes you are right i need to vary the on and off time just changing the duty cycle is not enough.

I am currently trying to to use the Servos.c file in PIC C library.

Few questions?

Servo Left and Servo Right does it refer to two different servos?

Second when i am trying to compile the compiler is finding an error in the header file.

I am just trying to output PWM pulse, but its not compiling i would really appreciate some feedback.

Thank you.

The Error i am getting is mentioned below

Quote:

*** Error 128 "SERVOS.c" Line 102(1,39): A #DEVICE required before this line
1 Errors, 0 Warnings.


Code:

#include "main.h"
#include <servos.c>
#include <18F4550.h>

 

void main()
{

   setup_oscillator(OSC_8MHZ|OSC_NORMAL|OSC_PLL_OFF);

    init_servos()   
   
      void set_servo(left, forward, 1)

}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Oct 24, 2008 6:01 pm     Reply with quote

You have the first few lines in the wrong order.

Look at the link that I gave you above:
http://www.ccsinfo.com/forum/viewtopic.php?t=34560&start=22
It shows how to make a test program for the servos.c driver file.
Study that program.
RF_Geek



Joined: 24 Sep 2008
Posts: 9

View user's profile Send private message

PostPosted: Fri Oct 24, 2008 6:11 pm     Reply with quote

Thank you PCM.

But i have a question here, i cannot use a trimpot in the project that this servo is intended for.

I am basically trying to take in a digital compass string and based on that bearing turn the servos.

I will get back to you.

Thank you.
RF_Geek



Joined: 24 Sep 2008
Posts: 9

View user's profile Send private message

Program doesn't work
PostPosted: Sun Oct 26, 2008 3:13 am     Reply with quote

PCM,

Thank you for all your help, i am having some problems trying to compile the sample program you directed me to earlier, even when i copy the same program using the microcontroller it gives me the same error.

I am using CCS 4.057

The error
*** Error 128 "SERVOS.c" Line 102(1,39): A #DEVICE required before this line
1 Errors, 0 Warnings.

Please help i copied the code "as is" posted online.

Thank you
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Oct 26, 2008 12:35 pm     Reply with quote

Are you using MPLAB ? If so, see these links:

How to use multiple source files with MPLAB:
http://www.ccsinfo.com/forum/viewtopic.php?t=34046

More detailed information on using multiple files with MPLAB.
Read all the links in this thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=32221
RF_Geek



Joined: 24 Sep 2008
Posts: 9

View user's profile Send private message

PostPosted: Sun Oct 26, 2008 2:24 pm     Reply with quote

No i am using CCS PIC C, but i do have the mplab 8 plugin installed. Could that cause the problems.

I am just copying the same program posted online and just compiling and it is finding an error in the Servos.c file " the file requires some #device".
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Oct 26, 2008 2:37 pm     Reply with quote

It's all CCS PIC C.

Do you have MPLAB or the CCS IDE ? The "IDE" is sold under these
names: PCW, PCWH, or PCWHD. Do you have one of those ?
If you do, then someone else will have to help you with the "Device"
error message. I don't have the CCS IDE. I only have MPLAB and use
it with the CCS command line compilers.

Also, if you are using the CCS demo, you should tell us.
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