PIC18F4580 – 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.
PIC18F4580 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. Sample code to check servo motor is given below,upon code execution servo turns each angle.
SchematicNote: Use adapter for powering since USB doesn’t provide required current.
Sample Code
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 62 63 |
/************************************************************************* HEADER FILE **************************************************************************/ #include<pic18.h> #define servo RE1 /************************************************************************* VARIABLE DECLARATIONS **************************************************************************/ int count=0; char x,value; /************************************************************************* MAIN FUNCTION **************************************************************************/ void main() { ADCON1 = 0X0F; /* A/D ports configured as digital */ RE0 = 0; TRISC = 0x80; /* RC6(TX)-o/p, RC7(RX)-(i/p) */ TRISE = 0x00; /* PORTE configured as o/p */ T0CON = 0XC3; /* TMR0ON=1, T08BIT=1, Prescalar-1:16 */ TXSTA = 0x24; /* 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(TMR0IF==1) /* TMR0IF set when TMR0 overflows */ { TMR0IF = 0; /* Clear TMR0IF */ count++; /* Increment count for each overflow of timer0 */ TMR0 = 0x64; /* Load calculated value for .5ms to timer0 register */ if(count==40) /* Condition for obtain 20ms */ { count = 0; servo = 1; /* Make servo pin high */ } if(count==value) /* Make servo pin low when the pulse is of sufficient duration */ { servo = 0; } } if(RCIF==1) /* RCIF set when character is received */ { RCIF = 0; /* Clear RCIF */ x=RCREG; if(x=='a') /* Load count for 90° when 'a' is received */ value = 3; if(x=='b') /* Load count for 180° when 'b' is received */ value = 5; if(x=='c') /* Load count for 0° when 'c' is received */ value = 0; } } } /*************************** END OF PROGRAM ****************************/ |
Topics related to PIC18F4580 Mini Development Board
- PIC18F4580 Mini Development Board – Overview
- PIC18F4580 Mini Development Board – Interfacing LED
- PIC18F4580 Mini Development Board – Interfacing LCD
- PIC18F4580 Mini Development Board – Serial communication(USART)
- PIC18F4580 Mini Development Board – Interfacing Switch
- PIC18F4580 Mini Development Board – Interfacing Buzzer
- PIC 18F4580 Mini Development Board – Interfacing POT(ADC)
- PIC18F4580 Mini Development Board – Interfacing Temperature sensor
- PIC18F4580 Mini Development Board – Interfacing Servo Motor
- PIC18F4580 Mini Development Board – Interfacing μRFID
- PIC18F4580 Mini Development Board – Interfacing CAN
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.