Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

how to control rc servo motors using pic controller???

Status
Not open for further replies.

hedonist

New Member
hi everyone...
i am a mechanical engineer..need to control rc servos dont know how to go @ it....dont want to buy ready controller..like sv203...can anyone help...need to knowwhich controller i could use...and coding....
thanks
 
RC servoes are usualy controlled using the PWM module on the PIC or generating the PWM in software. The Pulse width maps to the steering angle. Try searching this forum as this is a very common question.
 
im doing this at the moment, i have found the easiest way is to use the "Pulsout" command using PIC Basic.
 
lompa said:
im doing this at the moment, i have found the easiest way is to use the "Pulsout" command using PIC Basic.

Servo's are probably the main reason for the "Pulseout" command in PIC Basic, it's simply a software delay loop - the PWM hardware isn't suited to driving servos.
 
RC servo's operate by varying a positive going pulse between
1ms and 2ms duration in (i believe) 20ms period
to determine the position of the servo
 
u can try this:

use tmr1 to time for 20ms. then when 20ms is up tmr1 interrupt flag goes up. this will cause the interrupt routine to kick in. In the interrupt routine, use tmr0 to time for the various pulses u want to send to your servos.

say u have to control 2 servos, so inside interrupt routine

void interrupt intRoutine () {

if (TMR1IF) {

RB5 = 1; //line a
TMR0 = 0;
while (TMR0<servoPulse1);
RB5 = 0;

TMR0 = 0;
RC4 = 1;
while (TMR0<servoPulse2);
RC4=0; //line b

TMR1H = -50;
TMR1L = 0;

TMR1IF = 0; // Clear interrupt flag

}

in this routine, rb5 and rc4 are used to output the pulse.
the line TMR1H = -50 and TMR1L = 0 is used to set the value of tmr1 so that it times roughly 20ms thereabouts.

the value of servoPulse1 and servoPulse2 is the pulse length you want to send to the servo. say 1.5ms or 1.8ms etc.

note that using this method, your micro-controller does nothing else but time for servo pulse between line a and line b. it is "stuck" there for the overall duration of servoPulse1 + servoPulse2. so not recommended if u have to control many many servos...
 
Umm... Oliver... Three year old thread!

Quite right, sorry for resurrecting this!

"Reference for anybody else that see's this post":p

PS; Futz, just had a look at your site, fantastic stuff you have their! I'll have to study the balancing robot it bit closer first before I can comment on that, but really interesting non the less!
 
Last edited:
Pic 16f876 Servo RC

Hi all, i'm not english, so i could make some mistakes :D
so... i had to interface Servo RC (s3003 Futaba) with my micro, but the servo RC turn always in the same direction both with 1 ms of DC and with 2ms of DC. The frequence thay I apply is 50 Hz.
I apply to servo RC 5 Volt and i connect only with a wire the "control pin" with PORTC,6. Maybe I should attack a resistance between??
This is my programme : it's easy so, please, check it!! Thanks :)

LIST P=16F876
INCLUDE "P16F876.INC"
CT1 EQU 023H
CT0 EQU 024H

ORG 0000H
GOTO START
ORG 0050H
START BSF STATUS,RP0
MOVLW B'00000000'
MOVWF TRISC
BCF STATUS,RP0

CALL GIRASX




GIRASX BSF PORTC,6
CALL DELAY1MS
BCF PORTC,6
CALL DELAY19MS
GOTO GIRASX
RETURN

DELAY1MS MOVLW 02H
MOVWF CT1
DELAY1 MOVLW 044H
MOVWF CT0
DELAY DECF CT0,F
BTFSC CT0,F
GOTO DELAY
DECF CT1,F
BTFSC CT1,F
GOTO DELAY1
RETURN
DELAY19MS MOVLW 09H
MOVWF CT1
DDELAY1 MOVLW 09EH
MOVWF CT0
DDELAY DECF CT0,F
BTFSC CT0,F
GOTO DDELAY
DECF CT1,F
BTFSC CT1,F
GOTO DDELAY1
RETURN

END
 
PIC pwm for servo

Hi guys,

I was wondering whether this could be the right place to ask.
I am starting with microcontrollers and I have developed a few things. I am currently trying to make a servo to move. I have a code... the servo does move, but it moves like in steps, not a continuous movement as I move the potentiometer. I am using a pic16f690 at 500kHz. the servo is a Spectrum digital ds821. I would like to know whether I am making something wrong in order to make the servo to move more smoothly. Any suggestion will be helpful.

---------------------------
void main()
{
TRISC = 0b00100000;
PORTC = 0b00000000;

OSCCON = 0b00110101; //500kHz fosc
CCP1CON = 0b00001100; //
//CCPR1L = 0b11111111;
PIR1 = 0x00;
TRISC = 0x00;
// ADC portion
//==================
T2CON = 0b00000111; // prescaler = 16 + turn on TMR2;

PR2 = 0b10011011;
CCPR1L = 0b00000000;
ANSEL = 0b00000001;
ANSELH = 0;
/* Init ADC */
ADCON0 = 0b00000000; // select left justify result. ADC port channel 0
ADCON1 = 0b00110000;
ADON = 1; // turn on the A2D conversion module
//CCPR1L = 0b00000000;

// <Initiate the ADC conversion>
double constant1 = 0.06;
double constant2 = 6.2;
while(1)
{
TMR2IF = 0;
for(;;) {
TMR2IF = 0;
GODONE = 1;

while(GODONE) continue; // Wait conversion done
//constant3 = floor(constant1*ADRESH + constant2); // this is to make wider the potentiometer // turns
CCPR1L = ADRESH;// constant3;

//adc end
while(TMR2IF == 0)
{
//just wait until it overflows
}
} //end for
}
}
 
Status
Not open for further replies.

Latest threads

Back
Top