Society of Robots - Robot Forum
Software => Software => Topic started by: mohit sharma on August 07, 2009, 01:11:55 AM
-
i have a line following robot programme.
#include<avr/io.h>
#define left_sensor PD0 // Left sensor
#define middle_sensor PD1 // Middle sensor
#define right_sensor PD2 // Right sensor
#define sensor_port PORTD
#define sensor_pin PIND
#define sensor_ddr DDRD
#define Motor_L1 PB2 //Left positive motor
#define Motor_L2 PB3 //Left negetive motor
#define Motor_R1 PB1 //Right positive motor
#define Motor_R2 PB0 //Right negetive motor
int forward()
{
PORTB&=~((Motor_L2)|(Motor_R2)); //turn Off PB3,PB0
PORTB|=(Motor_L1)|(Motor_R1); //turn On PB2 and PB1
return 0;
}
int reverse()
{
PORTB&=~((Motor_L1)|(Motor_R1)); //turn Off PB2,PB1
PORTB|=(Motor_L2)|(Motor_R2); //turn On PB3 ,PB0
return 0;
}
int for_left()
{
PORTB&=~((Motor_L2)|(Motor_R2)|(Motor_R1)); //turn Off PB3 , PB0 and PB1
PORTB|=(Motor_L1); //turn On PB2
return 0;
}
int for_right()
{
PORTB&=~((Motor_L1)|(Motor_R1)|(Motor_L2)); //turn Off PB2 , PB1,PB3
PORTB|=(Motor_R1); //turn On PB0
return 0;
}
int main()
{
// ***** Setup Ports and sensors
DDRB|=(PB0)|(PB1)|(PB2)|(PB3); //motor
PORTD|=(PD1)|(PD2)|(PD0); //front sensors
// ***** Insert code here to move forward
while(1)
{
// Where is the black line?
// if(sensor_pin&((left_sensor)|(middle_sensor)|(right_sensor)))
// if(sensor_pin&((PD0)|(PD1)|(PD2)))
// {
// forward();
// }
if(sensor_pin&((middle_sensor)))
{
forward();
}
else if(sensor_pin&((left_sensor)))
{
for_left();
}
else if(sensor_pin&((right_sensor)))
{
for_right();
}
else
{
forward();
}
}
}
can someone tell me what does [ PORTB&=~((Motor_L2)|(Motor_R2)|(Motor_R1)); //turn Off PB3 , PB0 and PB1
PORTB|=(Motor_L1); //turn On PB2] stands for and how it works , how operators work here & [ DDRB|=(PB0)|(PB1)|(PB2)|(PB3); //motor
PORTD|=(PD1)|(PD2)|(PD0); //front sensors ] how ports are initialize here. ???
-
can someone tell me what does [ PORTB&=~((Motor_L2)|(Motor_R2)|(Motor_R1));
PORTB is the register Motor_L2 and Motor_R2 etc. are defined as pins on PORTB. ~ means not. What this does is turn off the pins listed. This is whats called bitwise operators. Its similar if not the same as Boolean algebra. topics for you to research so you will better understand this kind of code 1) registers, 2)defines, and 3) bitwise operators. Just google those and everything will start to make sense. if you want details on registers you should look at the atmega8 datasheet.
-
thanks it is fruitfull, but can u tell me what this
[ if(sensor_pin&((middle_sensor)))
{
forward();
}
] works ???
-
sensor_pin represents the whole register PIND while middle_sensor represents PIND1 a sngle bit in the PIND register
so PIND is register like this containing 8 bits listed here [PIND7,PIND6,PIND5,PIND4,PIND3,PIND2,PIND1,PIND0]
As you may know any number other than 0 is interpreted as true. This is what this statement is based on. So if
PIND1 of true then the statement is true. The reason you need the PIND & PIND1 is because PIND1 is just
interpreted as a bit 7:0 to reference from the register PIND. PIND needs to be referenced to show the compiler
what register its dealing with.
-
That is awesome . thanks a lot ,here in my prog i have not initilize the port pin D using DDRD register , and i m directly using it
int main()
{
// ***** Setup Ports and sensors
DDRB|=(PB0)|(PB1)|(PB2)|(PB3); //motor
PORTD|=(PD1)|(PD2)|(PD0); //front sensors
}
do you think it will produce any error in working......
-
um... I you should write it like
int main()
{
// ***** Setup Ports and sensors
DDRB|=_BV(PB0)|_BV(PB1)|_BV(PB2)|_BV(PB3); //motor
PORTD|=_BV(PD1)|_BV(PD2)|_BV(PD0); //front sensors
}
The original code should have been written that way. I personally would use DDRxn and PORTxn where x is the port letter, and n is the bit number. The _BV() function sets the bit in question. Take PB3 this represents the number three 0b11 in binary what you want is 0b1000. The _BV() function is the same thing a 1<<PB3. This shifts a 1 over three places which is 0b1000. If you don't include the _BV() function in the first line of code you get 0b11 | 0b10 | 0b01 | 0b00, which is equivalent to 0b11. This is not what you intended. Sorry I didn't catch that earlier.
-
thanks budy i'll try that............. ;)
-
finally i did it thanks budy u helped me a lot i'll post a video soon thanks a lot.........