Sie sind auf Seite 1von 4

Interfacing Servomotor with PIC 16F877A - MikroC

Servos are used in cars, boats, helicopters, robots etc. They are geared DC motors with
positional feedback control that allows for the rotor to be accurately positioned. The gear
drives the output shaft and also control an internal potentiometer. The potentiometer feeds
back the output shaft position to the internal control devices that control the DC motor.
Servos can be rotated through 180 degrees (+ or - 90 degrees) though its internals can be
modified to give full 360 degree rotation.

It has 3 wires: power (5v), ground, and the control wire. By sending pulse width signals
to the control wire, the shaft can be rotated. The pulse can be varied from 1 - 2
miliseconds.
1 ms will rotatae the shaft counter clockwise (-90 degrees).
1.5 ms will rotate the shaft to its neutral axis (0 degrees)
2 ms will rotate the shaft in clockwise direction (+90 degrees).
Any pulse width in between moves the shaft between these positions (+ or - 90 degrees)
MikroC Code:

/**********************************
Project Name :
Interfacing Servomotor with PIC 16F877A
Author : Promise
Description :
This project demonstrate how to interface a servomotor with PIC 16F877A. It rotates
back and forth (CW and CCW) continuously

Test configuration:
MCU: PIC 16F877A
Oscillator: XT 8.0000 MHz, 8.0000 MHz Crystal
***********************************/

unsigned i ;

void main() {

TRISB.B0 = 0; // set PORTB bit zero to output

while(1){

for(i=0;i<100;i++){ // rotate to neutral axis (0 degree)


PORTB.B0 = 1;
delay_us(1500); // generate 1.5ms pulse
PORTB.B0 = 0;
delay_ms(20); // 50Hz frequency for servo to react
}

for(i=100;i<200;i++){ // rotate CounterClock wise CCW (-90 degree)


PORTB.B0 = 1;
delay_ms(1); // generate 1ms pulse
PORTB.B0 = 0;
delay_ms(20); // 50Hz frequency for servo to react
}

for(;i>100;i--){ // rotate ClockWise CW (+90 degree)


PORTB.B0 = 1;
delay_ms(2); // generate 2ms pulse
PORTB.B0 = 0;
delay_ms(20); // 50Hz frequency for servo to react
}
}
}

Proteus Schematic:

Das könnte Ihnen auch gefallen