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

Control a servo with hardware on PIC18f1330

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



Joined: 22 Nov 2011
Posts: 2

View user's profile Send private message

Control a servo with hardware on PIC18f1330
PostPosted: Thu Mar 01, 2012 4:36 am     Reply with quote

Hi everyone:

I have mess arround a little with PWM modules on PIC18f1330 and read something on the datasheet that it's posible to configure them to drive a servo. I am not as familiar with PIC18f1330 modules as with ccp modules of other PICs and ended up having it work with timer0.

This PIC has 3 independent PWM's so I think it would be nice to drive servos by hardware and use timers for anything else.

I appreciate any idea or example.

Thanks in advance. Smile
temtronic



Joined: 01 Jul 2010
Posts: 9229
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Mar 01, 2012 6:32 am     Reply with quote

It's easy to use PWM for servo control however you need to tell us what kind of servo!
It seems most people here think 'servo' means those $5 servos used in model RC planes and cars. They are not what I normally refer to as a 'servo'. Mine typically have 1024/rev optical encoders for feedback feeding into a 16F877 using a tight PID control loop outputting to 1HP drivers. There's a few 'variations in the middle too !

If you mean the RC style units, search the forum, lots of Q&As about them. If the latter, there's a few here and if you have MATLAB,designing the controlling takes less than an hour.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Mar 01, 2012 1:57 pm     Reply with quote

Here is an example of how to use the Power PWM module on an 18F1330
to put a hobby servo in the neutral position. This program creates a 1.5ms
pulse at a 50 Hz rate. You can increase or decrease the duty cycle to
some other value than 1.5ms to experiment with changing the servo
position. The signal will be on pin B1 (PWM1) for the program below.
This is pin 9 of the DIP package.

If you don't understand the paragraph above, then you need to Google for
articles on servos. Example:
http://servocity.com/html/how_do_servos_work_.html
Code:

#include <18F1330.h>
#fuses INTRC_IO,NOWDT,PUT,BROWNOUT
#use delay(clock=8M)

#define POWER_PWM_PERIOD 2468  // 50 Hz pwm freq with 8 MHz osc.

//=======================================
void main()
{

// Setup the 4 Power PWM channels as ordinary pwm channels.
setup_power_pwm_pins(PWM_ODD_ON, PWM_ODD_ON, PWM_ODD_ON, PWM_ODD_ON);

// Mode = Free Run 
// Postscale = 1   (1-16) Timebase output postscaler
// TimeBase = 0   (0-65355) Initial value of PWM Timebase
// Period = 2000  (0-4095) Max value of PWM TimeBase
// Compare = 0     (Timebase value for special event trigger)
// Compare Postscale = 1 (Postscaler for Compare value)
// Dead Time

setup_power_pwm(PWM_FREE_RUN | PWM_CLOCK_DIV_64, 1, 0, POWER_PWM_PERIOD, 0, 1,0); 

// This setting gives a 1.5ms pulse at 50 Hz.
set_power_pwm0_duty((int16)((POWER_PWM_PERIOD *4) * .075));

//set_power_pwm2_duty((int16)((POWER_PWM_PERIOD *4) * .4)); // 40%
//set_power_pwm4_duty((int16)((POWER_PWM_PERIOD *4) * .6)); // 60%

while(1);
}
rollover_bethoven



Joined: 22 Nov 2011
Posts: 2

View user's profile Send private message

PostPosted: Thu Mar 01, 2012 7:56 pm     Reply with quote

thank you so much, that's what I was asking for, gonna try it 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