View previous topic :: View next topic |
Author |
Message |
rollover_bethoven
Joined: 22 Nov 2011 Posts: 2
|
Control a servo with hardware on PIC18f1330 |
Posted: Thu Mar 01, 2012 4:36 am |
|
|
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. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9229 Location: Greensville,Ontario
|
|
Posted: Thu Mar 01, 2012 6:32 am |
|
|
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
|
|
Posted: Thu Mar 01, 2012 1:57 pm |
|
|
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
|
|
Posted: Thu Mar 01, 2012 7:56 pm |
|
|
thank you so much, that's what I was asking for, gonna try it |
|
|
|