LINE FOLLOWER ROBOT PROGRAMMING

pic programming

  • Programming  is the next step that you should do after building your line follower. This is a sample of the programming for line follower robot that are using PIC16877A microcontroller. This program can be use for the circuit on my previous post Line Follower Robot. If you haven’t already read it, I suggest you read it before going any further in this post.



#include <pic.h>
__CONFIG(0x3732);

void main ()

{          int x;                                                    //declare variable x as integer type
            TRISD = 0b00000000;                       //select port D as output for motor direction and LED
            TRISC = 0b00000000;                       //select port C as output for motor enable
            PORTC = 0b00000000;                     

            PR2 = 249;                                          //set period = 200µs @ frequency = 5 kHz    
            T2CON = 0b00000111;                      //timer 2 ON, prescaler is 16
            CCP1CON = 0b00001100;                //set CCP1 as PWM mode
            CCP2CON = 0b00001100;                //set CCP2 as PWM mode
            CCPR1L = 249;                                 //set duty cycle = 100%
            CCPR2L = 249;                                 // set duty cycle = 100%
           
            CMCON = 0b00000110;                    //set for internal voltage reference
            CVRCON = 0b11101001;                  //set Vref = 3.125 volts

            while(1)
                        {          if(C1OUT==1 && C2OUT==1){                  //if both sensor detect white area
                                                CCPR1L = 113;                                  //set duty cycle = 45%
                                                CCPR2L = 118;                                  //set duty cycle = 47%
                                                PORTD = 0b00000110;}                   //robot is straight on the line, robot
            move straight forward

else if(C1OUT==1 && C2OUT==0){           //sensor 1 detect white area while 
            sensor 2 detect black area
                                                CCPR1L = 165;                                  //set duty cycle = 66%
                                                CCPR2L = 165;                                  //set duty cycle = 66%
                                                PORTD=0b10001010;}                      //robot steers to the left

                                    else if(C1OUT==0 && C2OUT==1){           //sensor 1 detect black area while
            sensor 2 detect white area
                                                CCPR2L = 165;                                  //set duty cycle = 66%
                                                CCPR1L = 165;                                  //set duty cycle = 66%
                                                PORTD=0b10000101;}                      //robot steers to the right

                                    else if(C1OUT==0 && C2OUT==0){           //both sensor detect black area
                                                PORTD=0b00000000;}                      //robot will stop moving
                       
                   }

}