PDA

View Full Version : line follower



shiladityachattrrjee54
01-19-2011, 05:43 PM
Hi, I am starting my first project on a line follower and has written a code for atmega16, can you plz check the code and notify me for possible errors............................................
Project : FINAL LINE FOLLOWER
Version :
Date : 8/18/2010
Author : SHILADITYA CHATTERJEE
PA.0= Input from left sensor
PA.1=Input from middle sensor
PA.2=Input from right sensor
PD.5= Enable 1
PD.4=Enable 2
PC.0=Drive d left motor forward
PC.1=Drive the left motot reverse
PC.2=Drive the right motor in forward
PC.3=Drive the right motor in reverse


Chip type : ATmega16
Program type : Application
AVR Core Clock frequency: 8.000000 MHz
Memory model : Small
External RAM size : 0
Data Stack size : 256
************************************************** ***/

/* PORTA.0=Input from Left Sensor
PORTA.1=Input from Middle sensor
PORTA.2=Input from Right Sensor

PORTD.5=Enable pin 1
PORTD.4=Enable pin 2

PORTC.0=Drive the left motot forward
PORTC.1=Drive the left motor reverse
PORTC.2=Drive the right motor forward
PORTC.3=Drive the right motot reverse
*/

#include <mega16.h>
#include<delay.h>

void main(void)
{

// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTA=0x00;
DDRA=0x00;

// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0x00;

// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=Out Func2=Out Func1=Out Func0=Out
// State7=T State6=T State5=T State4=T State3=0 State2=0 State1=0 State0=0
PORTC=0x00;
DDRC=0x0F;

// Port D initialization
// Func7=In Func6=In Func5=Out Func4=Out Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=0 State4=0 State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0xff;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

/* The pins which are receiving inputs from the sensors should be initially set to logic 1.*/
PORTA.0=1; /*Left sensor input*/
PORTA.1=1; /*Middle sensor input*/
PORTA.2=1; /*Right sensor input */
PORTD.5=1; /*Enable pin of the left half of the H-bridge*/
PORTD.4=1; /*Enable pin of the right half of the H-bridge*/




//main loop of the program
while(1)
{

if((PORTA.1==0)&&(PORTA.0==1)&&(PORTA.2==0)) /*left turning*/
{
PORTC.0=0; /*Left motor is not running in any direction.*/
PORTC.1=0;
PORTC.2=1; /*Right motor is running in forward direction. bOt will eventually turn left*/
PORTC.3=0;
delay_ms(50); /* Wait for 50 ms*/
PORTC.0=0; /*Motors are not running*/
PORTC.1=0;
PORTC.2=0;
PORTC.3=0;
delay_ms(50); /*Delay of another 50 ms*/
}

/* effectively we are creating a pulse which is on for 5o ms nd off for 50 ms*/
else if((PORTA.1==1)&&(PORTA.0==1)&&(PORTA.2==0)) /*left turning*/
{
PORTC.0=0; /*Left motor is not running in any direction.*/
PORTC.1=0;
PORTC.2=1; /*Right motor is running in forward direction. bOt will eventually turn left*/
PORTC.3=0;
delay_ms(50); /* Wait for 50 ms*/
PORTC.0=0; /*Motors are not running*/
PORTC.1=0;
PORTC.2=0;
PORTC.3=0;
delay_ms(50); /*Delay of another 50 ms*/
}

else if((PORTA.1==0)&&(PORTA.0==0)&&(PORTA.2==1))/*right turning*/
{
PORTC.0=1; /*Left motor running in forward direction.*/
PORTC.1=0;
PORTC.2=0; /*Right motor is not running.*/
PORTC.3=0;
delay_ms(50); /*50ms time delay*/
PORTC.0=0; /*Motors not running in any direction*/
PORTC.1=0;
PORTC.2=0;
PORTC.3=0;
delay_ms(50); /*50ms time delay*/
}
else if((PORTA.1==0)&&(PORTA.0==1)&&(PORTA.2==1))/*right turning*/
{
PORTC.0=1; /*Left motor running in forward direction.*/
PORTC.1=0;
PORTC.2=0; /*Right motor is not running.*/
PORTC.3=0;
delay_ms(50); /*50ms time delay*/
PORTC.0=0; /*Motors not running in any direction*/
PORTC.1=0;
PORTC.2=0;
PORTC.3=0;
delay_ms(50); /*50ms time delay*/
}


else if((PORTA.1==0)&&(PORTA.0==1)&&(PORTA.2==0))/*forward*/

{ PORTC.0=1;
PORTC.1=0;
PORTC.2=1;
PORTC.3=0;
delay_ms(25);
PORTC.0=1;
PORTC.1=0;
PORTC.2=1;
PORTC.3=0;
delay_ms(25);

}
else if((PORTA.1==1)&&(PORTA.0==1)&&(PORTA.2==1))/*forward*/

{ PORTC.0=1;
PORTC.1=0;
PORTC.2=1;
PORTC.3=0;
delay_ms(25);
PORTC.0=1;
PORTC.1=0;
PORTC.2=1;
PORTC.3=0;
delay_ms(25);

}
else /*stop*/

{
PORTC.0=0;
PORTC.1=0;
PORTC.2=0;
PORTC.3=0;
}

}
}

vikas
01-19-2011, 06:45 PM
can you plz check the code and notify me for possible errors.

Why would anyone do that ?

shiladityachattrrjee54
01-19-2011, 08:14 PM
Y dre is a problem understanding it? pardon...... i asked for a help since i am new to this.........

pratheek
01-19-2011, 08:37 PM
First of all, most people do not have the time to read, check and verify codes for someone else.

Secondly, you have provided no details for your hardware, circuit, sensors, or even the compiler you are using. Without it I do not know how you expect people to help you find problems your the code. I see syntax errors in your pin configurations itself. But without knowing which compiler you are using it on, I can't tell for sure.

Post your algorithm and you will get a lot of suggestions.

If you have problems with writing the code, atleast let us know which part you are doubtful of, very few here will have the time to read and understand your whole code.

And finally, if you are knew to programming microcontrollers, please do not attempt such a complex project. First get your basics. Go one step at a time.

shiladityachattrrjee54
01-19-2011, 09:00 PM
its mentioned above the code......but yep........here is d details................
microcontroller- atmega16
3 sensors in Inline configuration
I am using Codevision Avr for programming and PonyProg for Burning.

rao
01-19-2011, 09:28 PM
http://www.exrobo.com/myfiles/line-farshad-yazdi.txt

why dont you compare your code to the source?

shiladityachattrrjee54
01-19-2011, 11:19 PM
Thanx a lot............i will certainly do that