line following robot code for atmega32

 line following robot code for atmega32

 

#ifndef F_CPU #define F_CPU 16000000UL // defines CPU frequency for delay frequency

  #endif

 #include <avr/io.h> // includes input/output header file

  #include <util/delay.h> // includes delay header file /*CODE FOR LINE FOLLOWER BY ROBOVITICS USING ATMEGA8 MICROCONTROLLER*/

  //******************************************************************** /* Connection of L293D IC with aTmega8. Pin2 (A1) of L293D is connected with PB2 (Pin 16 of aTmega8). Pin 7(B1) of L293D is connected with PB3 (MOSI) (PIN 17 of aTmega8) **A1 and B1 are the input pins of left side H-Bridge of L293D which is driving the left motor. Pin15 (A2) of L293D is connected with PB0 (Pin 14 of aTmega). Pin10 (B2) of L293D is connected with PB1(Pin15 of aTmega). Pin1 (EN1) and pin9 (EN2) is connected via motor enable switch. Pin3 and pin6 of L293D is connected with Left Motor. Pin14 and Pin11 of L293D is connected with right motor. */ // connect the left sensors on PC4 and right ones on PC5 

 int main(void)

 { DDRB=0b11111111;   //PORTB as output Port connected to motors

  DDRC=0b0000000;   //PORTC Input port connected to Sensors 

 int left_sensor=0, right_sensor=0; while(1)    // infinite loop

  {     

 left_sensor=PINC&0b0010000;      // mask PC4 bit of Port C 

 right_sensor=PINC&0b0100000;  // mask PC5 bit of Port C    

   if((left_sensor==0b0000000) & (right_sensor==0b0000000))  

    {               

   PORTB=0b00000000;   // stop       

  }  

 if((left_sensor==0b0010000) & (right_sensor==0b0100000))  

 {               

   PORTB=0b00001001;   // move straight      

         }

 if((left_sensor==0b0000000) & (right_sensor==0b0100000))          

     {              

    PORTB=0b00000001;   // turn left      

 

 if((left_sensor==0b0010000) & (right_sensor==0b0000000))         

      {                  PORTB=0b00001000;  // turn right        }   }   } 

*THE TEXT IN BLUE ARE THE COMMAND LINES


 



 over_turn(){PORTD&=0xF0;PORTD|=0x06; _delay_ms(900);right(); // over turn and changes direction _delay_ms(100);while(S4==0);stop();if(bot_dir==N){ bot_dir=S; return;}if(bot_dir==E){ bot_dir=W; return;}if(bot_dir==S){ bot_dir=N; return;}if(bot_dir==W){ bot_dir=E; return;}}reach_home() // function to take robot to initial cooridnate{ // many more condition exits according to robots direction of approching block, writethem if you want!step();

bot_y--;if(bot_x!=0){ left_ninety();while(bot_x)step();}if(bot_x==0){ if( bot_dir==N){ over_turn();while(bot_y)step();return;}if ( bot_dir==W){ left_ninety();while(bot_y)step();return;}}}void main (void){DDRB=0x00; // making PORTB as input

DDRA=0x00; // making PORTA as inputDDRD=0xFF; // making PORTD as outputPORTD=0x30; // setting Logic high on Enable pin of L293Dgrid_x=3; // setting maximun X coordinate of gridgrid_y=3; // setting maximum Y coordinate of gridbot_dir=1; // Initial robot direction , 1= North _delay_ms(1000); // a small delay before code startssearch(); // function to search for robotover_turn(); // make over turnreach_home(); // return to initial coordinate// line_follow();while(1); // stuck in a infinite loop after task is completed}

No comments:

Post a Comment

its cool