PIC16F877A – Interfacing Servo Motor
Servos have simple electrical interface – they have 3 wires, one for power, one for ground and the other for the pulse train. Once the servo is powered, the signal wire is ready to receive signal in the form of pulse width modulation (PWM) from an external source.
The signal expects to be updated every 20 ms with a pulse between 0.5ms and 2.5ms. With a 1.5 ms pulse, the servo motor will be at the natural 90° position. With a 0.5 ms pulse, the servo will be at the 0° position, and with a 2.5 ms pulse, the servo will be at 180°. You can obtain the full range of motion by updating the servo with a value in between.
PIC 16F877A mini development board has a dedicated connector K16 to interface Servo motor. Regulated 5V is available on power supply pin and the signal pin is connected at the port pin RE1.
SchematicNote: Use adapter for powering since USB doesn’t provide required current.
Sample Code
Sample code to check servo motor is given below.
When ‘a’ is received, servo turns to 90°
When ‘b’ is received, servo turns to 180° and
When ‘c’ is received, servo turns to 0°
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
/************************************************************************* HEADER FILES **************************************************************************/ /************************************************************************* Required Time Delay(0.5ms) = Tosc(200ns) * Prescalar value(16) * count where TMR0, x = 256 - count **************************************************************************/ #include<pic.h> #define servo RE1 /************************************************************************* VARIABLE DECLARATIONS **************************************************************************/ int count = 0; char x,value; /************************************************************************* MAIN FUNCTION **************************************************************************/ void main() { TRISC = 0x80; /* RC6(TX)-o/p, RC7(RX)-i/p */ TRISE = 0x00; /* PORTE configured as O/P */ OPTION = 0X03; /* Prescalar selected as 1:16 */ TXSTA = 0x24; /* Transmission Enable(TXEN=1,SYNC=0,BRGH=1) */ RCSTA = 0X90; /* Reception Enable(SPEN=1,CREN=1) */ SPBRG = 0x81; /* Serial Port Baud rate Generator (9600) */ TMR0 = 0X64; /* Load calculated value for .5ms to timer0 register*/ while(1) { if(T0IF==1) /* TOIF set when TMR0 overflows */ { T0IF = 0; /* Clear the flag */ count++; /* Increment count for each overflow of timer0 */ TMR0 = 0x64; /* Reload the value for .5ms to timer0 */ if(count==40) /* Count to obtain 20ms is 40 (0.5ms * 40 = 20ms) */ { count = 0; servo = 1; /* Servo pin made high every 20ms */ } if(count==value) { servo = 0; } } if(RCIF==1) /* RCIF set when a character is received */ { RCIF = 0; /* Clear the flag */ x = RCREG; if(x == 'a') /* When 'a' is received, rotate the servo to 90° */ value = 3; /* 0.5ms*3 = 1.5ms, pulse width for 90° */ if(x == 'b') /* When 'b' is received, rotate the servo to 180° */ value = 5; /* 0.5ms*5 = 2.5ms, pulse width for 180° */ if(x == 'c') /* When 'c' is received, rotate the servo to 0° */ value = 0; } } } /*************************** END OF PROGRAM *****************************/ |
Topics related to PIC16F877A Development Board-Mini
- PIC16F877A Mini Development Board – Overview
- PIC16F877A Mini Development Board – Interfacing LED
- PIC16F877A Mini Development Board – Interfacing LCD
- PIC16F877A Mini Development Board – Serial communication(USART)
- PIC16F877A Mini Development Board – Interfacing Switch
- PIC16F877A Mini Development Board – Interfacing Buzzer
- PIC16F877A Mini Development Board – Interfacing POT(ADC)
- PIC16F877A Mini Development Board – Interfacing Temperature sensor
- PIC 16F877A Mini Development Board – Interfacing Servo Motor
- PIC 16F877A Mini Development Board – Interfacing μRFID Reader
Resources
- Datasheets
How to buy?
- Click here to buy rhydoLABZ PIC 16F877A Mini Development Board
- Click here to buy rhydoLABZ PIC 18F4520 Mini Development Board
- Click here to buy rhydoLABZ PIC 18F4550 Mini Development Board
- Click here to buy rhydoLABZ PIC 18F4580 Mini Development Board
- Click here to buy rhydoLABZ Servo motor
SupportPlease share your ideas with us, visit our forum for discussion
Leave a Reply
You must be logged in to post a comment.