Author Topic: problem with atmega 8 programming  (Read 4239 times)

0 Members and 1 Guest are viewing this topic.

Offline mohit sharmaTopic starter

  • Jr. Member
  • **
  • Posts: 28
  • Helpful? 0
problem with atmega 8 programming
« 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. ???
 


Offline sonictj

  • Supreme Robot
  • *****
  • Posts: 416
  • Helpful? 11
    • Tim's Workshop
Re: problem with atmega 8 programming
« Reply #1 on: August 07, 2009, 01:32:11 AM »
Quote
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.

Offline mohit sharmaTopic starter

  • Jr. Member
  • **
  • Posts: 28
  • Helpful? 0
Re: problem with atmega 8 programming
« Reply #2 on: August 13, 2009, 03:37:40 AM »
thanks it is fruitfull, but can u tell me what this
  [ if(sensor_pin&((middle_sensor)))
         {
         forward();
         }
] works  ???

Offline sonictj

  • Supreme Robot
  • *****
  • Posts: 416
  • Helpful? 11
    • Tim's Workshop
Re: problem with atmega 8 programming
« Reply #3 on: August 13, 2009, 01:48:47 PM »
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.
 

Offline mohit sharmaTopic starter

  • Jr. Member
  • **
  • Posts: 28
  • Helpful? 0
Re: problem with atmega 8 programming
« Reply #4 on: August 19, 2009, 01:52:36 AM »
 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......

Offline sonictj

  • Supreme Robot
  • *****
  • Posts: 416
  • Helpful? 11
    • Tim's Workshop
Re: problem with atmega 8 programming
« Reply #5 on: August 19, 2009, 03:17:54 AM »
um... I you should write it like

Code: [Select]
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.

 

Offline mohit sharmaTopic starter

  • Jr. Member
  • **
  • Posts: 28
  • Helpful? 0
Re: problem with atmega 8 programming
« Reply #6 on: August 19, 2009, 03:30:06 AM »
thanks budy i'll try that............. ;)

Offline mohit sharmaTopic starter

  • Jr. Member
  • **
  • Posts: 28
  • Helpful? 0
Re: problem with atmega 8 programming
« Reply #7 on: August 27, 2009, 03:05:58 AM »
finally i did it thanks budy u helped me a lot i'll post a video soon thanks a lot.........

 

SMF spam blocked by CleanTalk