Home › Forums › Microcontrollers › PIC Microcontroller › How to interface more than one servo with PIC 16F887
Tagged: PIC, Servo, Sevo Motor
- This topic has 3 replies, 2 voices, and was last updated 10 years ago by Ligo George.
-
AuthorPosts
-
November 11, 2014 at 11:02 am #9805Eliobardo GonzálezParticipant
I know how to do this with one of it but in the simulator when i try to do with 2 ones it does not work. I really need know hot to do it. i put the code right here.
int espacios = 0; void servoRotate90() //90 Degree { unsigned int i; for(i=0;i<50;i++) { PORTB.F0 = 1; Delay_us(1500); PORTB.F0 = 0; Delay_us(18500); } } void servoRotate180() //180 Degree { unsigned int i; for(i=0;i<50;i++) { PORTB.F0 = 1; Delay_us(2200); PORTB.F0 = 0; Delay_us(17800); } } void servo1Rotate90() //90 Degree { unsigned int i; for(i=0;i<50;i++) { PORTB.F1 = 1; Delay_us(1500); PORTB.F1 = 0; Delay_us(18500); } } void servo1Rotate180() //180 Degree { unsigned int i; for(i=0;i<50;i++) { PORTB.F1 = 1; Delay_us(2200); PORTB.F1 = 0; Delay_us(17800); } } void main() { TRISB = 0; // PORTB as Ouput Port TRISA = 1; ANSEL = 0; // Configure AN pins as digital ANSELH = 0; C1ON_bit = 0; // Disable comparators C2ON_bit = 0; while(1){ if(PORTA.RA0){ Delay_ms(20); espacios++; servoRotate90(); //90 Degree Delay_ms(3500); } else if(!PORTA.RA0){ servoRotate180(); //180 Degree } else if(PORTA.RA1){ Delay_ms(20); espacios--; servo1Rotate90(); //90 Degree Delay_ms(3500); } else if(!PORTA.RA1){ servo1Rotate180(); //180 Degree } else if(PORTA.RA1&&PORTA.RA0){ espacios = espacios; servoRotate90(); //180 Degree servo1Rotate90(); Delay_ms(3500); } else{ servoRotate180(); //180 Degree servo1Rotate180(); } } }
November 11, 2014 at 11:43 am #9809Ligo GeorgeKeymasterYour code seems to be correct. It might be the problem of simulator. Try in real hardware.
November 12, 2014 at 10:19 am #9816Eliobardo GonzálezParticipantright now it already works on the simulator but it doesn’t on real hardware
November 12, 2014 at 10:49 am #9817Ligo GeorgeKeymasterCan you explain the problem in detail ? What happens when you operate 2 servo motors simultaneously ?
It might be the problem of your power supply. Make sure that your power supply can provide enough current to operate 2 servo motors simultaneously.
It is better to use separate power supplies for PIC and Servo Motors OR you should use proper filtering capacitors, otherwise ripples produced by the servo motor will reset the PIC Microcontroller. -
AuthorPosts
- You must be logged in to reply to this topic.