Electronics forum

Tuesday, 16 August 2011

Line tracer code for PIC controller


Beginners Code for a three sensored line follower in CCS compiler(Mplab)..


#include<16f877a.h>
#use delay(crystal=8Mhz)
#fuses NOWDT

void initialise(void);
void ackn(void);
void pwm(void);
void stop(void);
void forward(void);
void reverse(void);
void right(void);
void left(void);
void sensorcheck(void);

char sensorleft,sensormiddle,sensorright;

void main()
{delay_ms(1000);
 ackn();/*acknowledgment by Led blinking*/
 initialise();
 pwm();

while(1)
    { sensorcheck();
	         

    if(((sensorleft==0)&&(sensormiddle==1)&&(sensorright==0)))
                                      { 
                                        forward();
				                    
                                      }
    if(((sensorleft==1)&&(sensormiddle==1)&&(sensorright==0))||
	   ((sensorleft==1)&&(sensormiddle==0)&&(sensorright==0)))
                                      { 
                                        right();
				                    
                                      }

  if(((sensorleft==0)&&(sensormiddle==1)&&(sensorright==1))||
	 ((sensorleft==0)&&(sensormiddle==0)&&(sensorright==1)))
                                      { 
                                        left();
				                    
                                      }


   
   

	} 



}

void initialise(void)
{set_tris_a(0xFF);
set_tris_d(0x0);
set_tris_c(0x0);
}

void ackn(void)
{output_high(PIN_C0);
delay_ms(4000);
output_low(PIN_C0);
delay_ms(1000);
}

void pwm(void)
{setup_ccp1(CCP_PWM);
setup_ccp2(CCP_PWM);
setup_timer_2(T2_DIV_BY_16, 124, 1);
set_pwm1_duty(105);
set_pwm2_duty(105);

 }


void sensorcheck(void)
{      sensorleft=input_state(PIN_A0);
       sensormiddle=input_state(PIN_A1);
       sensorright=input_state(PIN_A2);
       
}



void stop(void)

{output_low(PIN_D0);
output_low(PIN_D1);
output_low(PIN_D2);
output_low(PIN_D3);                       
}

void forward(void)
{
                      output_high(PIN_D0);
                      output_low(PIN_D1);
                      output_high(PIN_D2);
                      output_low(PIN_D3);

}


 
void reverse(void)
{                     output_low(PIN_D0);
                      output_high(PIN_D1);
                      output_low(PIN_D2);
                      output_high(PIN_D3);

}




 



void left(void)
{  output_low(PIN_D0);
   output_high(PIN_D1);
   output_low(PIN_D3);
   output_high(PIN_D2); 
}



 



void right(void)



{output_high(PIN_D0);
 output_low(PIN_D1);
 output_high(PIN_D3);
 output_low(PIN_D2);
}

Basic C code for line tracer

This is a basic code in 89c51

#include <AT89X52.h>
/*
                Sensors input port - P1
                  P1_0 --------> Left sensor
                  P1_4 --------> Right sensor

                 Motors output port - P0
                 
                  P0_0 --------> Enable pin of the left half of the H-bridge
                  P0_1 --------> will drive the left motor in forward direction
                  P0_2 --------> will drive the left motor in reverse direction
                  P0_3 --------> will drive the right motor in forward direction
                  P0_4 --------> Enable pin of the right half of the H-bridge
                  P0_5 --------> will drive the right motor in reverse direction
*/


/*Delay function runs an idle loop to create a time delay. If the crystal used is of 11.0592 MHz then the argument passed in delay is in 'milliseconds'.*/
void Delay(unsigned int itime)
{
                unsigned int i,j;
                for(i=0;i<itime;i++)
                                for(j=0;j<1275;j++);       //Idle loop
}
void Forward()
{
                P0_1=1;
                P0_2=0;
                P0_3=1;
                P0_5=0;
}

/*Generally for turning we use a pulsated wave so the bOt doesn’t get out of control i.e. we run the motor for sometime then again stop it and this is done very quickly to create an effective pulse. See the function below.*/

void TurnLeft()
{
                P0_1=0; /*Left motor is not running in any direction.*/
                P0_2=0;
                P0_3=1;   /*Right motor is running in forward direction. bOt will eventually turn left*/
                P0_5=0;
                Delay(50); /* Wait for 50 ms*/
                P0_1=0;  /*Motors are not running*/
                P0_2=0;
                P0_3=0;
                P0_5=0;
                Delay(50); /*Delay of another 50 ms*/
               
}

/*So in the above program we have effectively created a pulse of 100ms which is on for 50ms and off for another 50ms. You can change this value to suit your needs*/

/*Similarly we can write a function to turn right*/

void TurnRight()
{
                P0_1=1; /*Left motor running in forward direction.*/
                P0_2=0;
                P0_3=0; /*Right motor is not running.*/
                P0_5=0;
                Delay(50); /*50ms time delay*/
                P0_1=0; /*Motors not running in any direction*/
                P0_2=0;
                P0_3=0;
                P0_5=0;
                Delay(50); /*50ms time delay*/
               
}


void main()
{
/* The pins which are receiving inputs from the sensors should be initially set to logic 1.*/
                P1_0=1; /*Left sensor input*/
                P1_4=1; /*Right sensor input*/
                P0_0=1; /*Enable pin of the left half of the H-bridge*/
                P0_4=1;  /*Enable pin of the right half of the H-bridge*/

               
                //main loop of the program
                while(1)
                {
                                               
                                if((P1_0==0)&&(P1_4==1))
                                                TurnRight();
                                else if((P1_0==1)&&(P1_4==0))
                                                TurnLeft();
                                else
                                                Forward();
                               
                }
}