Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Message

[QUARANTINE] [AVR-Chat] Re: GP2D02 + ATmega32

2006-09-11 by katraven1

Ok - attached below is my code. It's written in C to be compiled 
thru AVR Studio 4.

Essentially, this is jst some testing code to get the sensor to read 
once and display the values it reads onto the LCD screen. Thing is, 
upon more testing today, I found that when I run this code, PINx 
always returns me the 2's complement of DDRx. For example:

DDRx = 0x0F; on the lcd PINx = 240 = 0xF0 -> no matter what value I 
set PORTx to be...

I think that's the reason y my Vout is always read as HI since I set 
the DDR = 0x40 -> PIN = 191(0xBF).... which means it doesn't 
actually detect what value is present at pin7(Vout).. =(

the lcd code has previously been tested and is working correctly, so 
the binary number stored in the int is being displayed as the 
correct decimal value...

if anyone has any suggestions plz put them forward.. i truly am out 
of ideas now =(

-----------------------------------------------------------
#include <inttypes.h>
#include <string.h>
#include <avr/sfr_defs.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include "lcd.h"

#define IR_PORT	PORTA
#define IR_DDR	DDRA
#define IR_PIN	PINA

int read_ir(void);

int main(void)
{
	unsigned int reading;

	IR_PORT = 0xC0;		// PIN6 = Vin
	IR_DDR = 0x40;		// PIN7 = Vout
	
	lcd();			// initialises lcd screen
	
	reading = read_ir();

	display_clear();	//displays value read by IR on lcd
	lcd_goto(0,0);
	putLs("ir_reading:");
	lcd_goto(0,1);
	putVal(reading);

	while(1);	
	
	return 0;
}

int read_ir(void)
{
	int count = 0;
	unsigned int output = 0;

	IR_PORT &= ~_BV(6);		// Set Vin low to read
	_delay_ms(70);

	while(1)			// wait until Vout is Hi
	{
		if(bit_is_set(IR_PIN, 7))
			break;
	}
	
	IR_PORT |= _BV(6);			// Pulse in MSB
		
	for(count = 0; count < 8; count++)	// read byte
	{
		_delay_ms(10);
		IR_PORT &= ~_BV(6);
		
		_delay_ms(10);
				
		output <<= 1;
		output |= (IR_PIN&0x80);

		IR_PORT |= _BV(6);
	}

	_delay_ms(2);				// switch IR off
	IR_PORT &= ~_BV(6);
	_delay_us(2);
	IR_PORT |= _BV(6);

	return output;
}

--- In AVR-Chat@yahoogroups.com, "Larry Barello" <yahoo@...> wrote:
>
> How about showing us your code...
> 
> | -----Original Message-----
> | From: AVR-Chat@yahoogroups.com [mailto:AVR-Chat@yahoogroups.com] 
On Behalf
> | Of katraven1
> | 
> | Ok, since the reply to this post showed that obviously some
> | knowledge was lacking in this area I have gone away and redone my
> | coding and interfacing to the GP2D02.
> | 
> | I've gotten the input correct now (I mistook the pulsing of the
> | input line to be a PWM.. silly me..) and have witnessed the IR 
flash
> | (via camera).
> | 
> | It is the output from the sensor which is of concern now... for 
some
> | reason the output is forever high.. for example, when i want to 
take
> | a reading i make my input line low, to which the sensor responds 
by
> | making the output low during the reading, and then high, to 
which my
> | code response by starting the pulses to read each bit in.
> | 
> | Yet for some reason the output bit is always high... my 'output'
> | variable is always "255" DEC regardless of the distance the 
sensor
> | is to a surface..
> | 
> | am I still missing the mark here? =(
> |
>

Attachments

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.