How to interface more than one servo with PIC 16F887

Home Forums Microcontrollers PIC Microcontroller How to interface more than one servo with PIC 16F887

Tagged: , ,

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #9805
    Eliobardo González
    Participant

    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();
        }
      }
    }
    
    #9809
    Ligo George
    Keymaster

    Your code seems to be correct. It might be the problem of simulator. Try in real hardware.

    #9816
    Eliobardo González
    Participant

    right now it already works on the simulator but it doesn’t on real hardware

    #9817
    Ligo George
    Keymaster

    Can 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.

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.
>