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);
}

No comments:

Post a Comment