NOT GEETIN D DESIRED OUTPUT

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

followin r d codes f simple obstacle detector
USING ATMEGA 16
USING RELAY CIRCUIT
USING AVR STUDIO 4

#define F_CPU 14000000UL
#include 
#include 

int main (void)
{
DDRD=0x00; 				//input pprt

DDRA=0xFF; 				//output port

while(1)
{  
   if(PORTD=0b00000000)  			//sensor at pin 6 
 { PORTA=0b00101011;   				//move straight
 }
else if(PORTD=0b01000000)
{  PORTA=0b00000000;     			//stop
 _delay_ms(200);
  PORTA=0b00101101;				//turn left 
}

  }}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Read PIND, not PORTD.

In any real application, your reading and comparing of an entire port is bound to fail. Unrelated pins and floating pins will have other values.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I was trying similar thing yesterday.

hope this helps you get started with whatever you are trying to achieve.

#define F_CPU 14000000UL         /* 14 MHz crystal clock */

#include    
#include 

/* defines for left motor control */
#define MOTOR_L_PORT PORTA
#define MOTOR_L_DDR DDRA
#define MOTOR_L_PULSE PA5
#define MOTOR_L_DIR PA4
#define MOTOR_L_ENABLE PA3

/* defines for right motor control */
#define MOTOR_R_PORT PORTA
#define MOTOR_R_DDR DDRA
#define MOTOR_R_PULSE PA2
#define MOTOR_R_DIR PA1
#define MOTOR_R_ENABLE PA0

/* defines for sensors */
#define SENSOR_DDR DDRD          /* DDRx - register for SENSOR output */
#define SENSOR_PORT PORTD        /* PORTx -register for SENSOR output */
#define SENSOR_PIN PIND          /* PINx - register for SENSOR input  */
#define SENSOR_BIT_LEFT PD6      /* bit for SENSOR input/output */

/* definations of delay */
#define DELAY 500

void init_motor()
{
	/* setup data direction registers for motor outputs */
	MOTOR_L_DDR |= _BV(MOTOR_L_PULSE) | _BV(MOTOR_L_DIR) | _BV(MOTOR_L_ENABLE);
	MOTOR_R_DDR |= _BV(MOTOR_L_PULSE) | _BV(MOTOR_L_DIR) | _BV(MOTOR_L_ENABLE);
}
void init_sensor()
{
	SENSOR_DDR &= ~_BV(SENSOR_BIT_LEFT);		/* Pin as input */
	//SENSOR_PORT |= _BV(SENSOR_BIT_LEFT);		/* Enable internal pull-up if required */
}
void enable_motors()
{
	MOTOR_L_PORT |= _BV(MOTOR_L_ENABLE);		/* set ENABLE pin */
	MOTOR_R_PORT |= _BV(MOTOR_R_ENABLE);		/* set ENABLE pin */
}
void move_forward()
{
	MOTOR_L_PORT &= ~_BV(MOTOR_L_DIR);			/* clear DIR pin */ 
	MOTOR_L_PORT |=  _BV(MOTOR_L_PULSE);		/* set PULSE pin */

	MOTOR_R_PORT &= ~_BV(MOTOR_R_DIR);			/* clear DIR pin */ 
	MOTOR_R_PORT |=  _BV(MOTOR_R_PULSE);		/* set PULSE pin */	
}

void move_backward()
{
	MOTOR_L_PORT |=  _BV(MOTOR_L_DIR);			/* set DIR pin */
	MOTOR_L_PORT &= ~_BV(MOTOR_L_PULSE);		/* clear PULSE pin */ 

	MOTOR_R_PORT |=  _BV(MOTOR_R_DIR);			/* set DIR pin */	
	MOTOR_R_PORT &= ~_BV(MOTOR_R_PULSE);		/* clear PULSE pin */ 
}

void turn_left()
{
	MOTOR_L_PORT &= ~_BV(MOTOR_L_DIR);			/* clear DIR pin */ 
	MOTOR_L_PORT |=  _BV(MOTOR_L_PULSE);		/* set PULSE pin */
	
	MOTOR_R_PORT &= ~_BV(MOTOR_R_DIR);			/* clear DIR pin */ 
	MOTOR_R_PORT &= ~_BV(MOTOR_R_PULSE);		/* clear PULSE pin */	
}

void turn_right()
{
	MOTOR_L_PORT &= ~_BV(MOTOR_L_DIR);			/* clear DIR pin */ 
	MOTOR_L_PORT &= ~_BV(MOTOR_L_PULSE);		/* clear PULSE pin */
	
	MOTOR_R_PORT &= ~_BV(MOTOR_R_DIR);			/* clear DIR pin */ 
	MOTOR_R_PORT |=  _BV(MOTOR_R_PULSE);		/* set PULSE pin */	
}

void stop()
{
	MOTOR_L_PORT &= ~_BV(MOTOR_L_DIR);			/* clear DIR pin */ 
	MOTOR_L_PORT &= ~_BV(MOTOR_L_PULSE);		/* clear PULSE pin */
	
	MOTOR_R_PORT &= ~_BV(MOTOR_R_DIR);			/* clear DIR pin */ 
	MOTOR_R_PORT &= ~_BV(MOTOR_R_PULSE);		/* clear PULSE pin */	
}
int main(void)
{
	init_motor();
	init_sensor();
	enable_motors();
    while(1)
    {
        if (bit_is_set(SENSOR_PIN, SENSOR_BIT_LEFT))
		{
			stop();
			_delay_ms(DELAY);
			turn_left();
			_delay_ms(DELAY);
		}
		else
		{
			move_forward();
		}
    }
}

Thanks
K

Last Edited: Sat. Feb 25, 2012 - 07:51 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

theusch wrote:
In any real application, your reading and comparing of an entire port is bound to fail.
Especially if the comparing is actually an assigning. ;-)

Stefan Ernst

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Comparing a whole port isn't bad if you need to read multiple pins simultaneously, just mask the read byte with an AND operation...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

damn lengthy 4 a simple obstacle
let me check it out
btw thnx!!! :roll:

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What the previous posters are indicating is that your if tests with a single = sign are incorrect. You need ==

..and look how nice it is when whole English words are used when posting:)