Yahoo Groups archive

AVR-Chat

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

Thread

GP2D02 + ATmega32

GP2D02 + ATmega32

2006-08-31 by katraven1

Hey guys, me again =P

I've been spending the last couple of days trying to get one ex-Sharp 
infrared sensor to communicate wif my ATmega32. It's jst connected to 
the OC0 pin on PORTB generating a pwm to read a signal and the output 
of the sensor to one of the other IO pins, similar as used on the 
Dilberts (thx Larry!)

However, as the output of the sensor is in serial, I can't quite get 
the timing right to read it properly...

I've read the previous post which converts code for the BSII to C 
(http://groups.yahoo.com/group/AVR-Chat/message/7190) but I'm not 
quite sure how to build on top of that...

*boggled yet again....

RE: [AVR-Chat] GP2D02 + ATmega32

2006-08-31 by Larry Barello

Don't use PWM output to drive the unit.  Have you seen
http://www.barello.net/Papers/GP2D02?  

The assembly code is complex since it multiplexes three sensors but at it's
core it is simple, it does the clocking and shifting of data in s/w which is
more than fast enough and easy to control.  I paraphrase below, for one
sensor (I used 9 for the loop to discard the first bit, you could re-arrange
and make it 8 loops):

Set control port pin low
wait for input pin to go high (or a max of 30ms IIRC)
for (i = 0; i< 9; i++)
{
	if input high
		output |= 1;	// Set low bit
	output << 1;		// Left shift
	set control pin low	// Clock in next bit
	wait 1 us
	set control pin high
	wait 16 us
}
set control pin high.

At the end of the routine your data should be in "output" and the sensor
should be in a low-power state ready for another reading.  Please make sure
the code you write follows the clock diagram in the data sheet. I just
paraphrased assembly code I wrote over 7 years ago and might have made a
mistake or two...

-----------
Larry Barello
www.barello.net


| -----Original Message-----
| From: AVR-Chat@yahoogroups.com [mailto:AVR-Chat@yahoogroups.com] On Behalf
| Of katraven1
| Sent: Thursday, August 31, 2006 8:54 AM
| To: AVR-Chat@yahoogroups.com
| Subject: [QUARANTINE] [AVR-Chat] GP2D02 + ATmega32
| Importance: Low
| 
| Hey guys, me again =P
| 
| I've been spending the last couple of days trying to get one ex-Sharp
| infrared sensor to communicate wif my ATmega32. It's jst connected to
| the OC0 pin on PORTB generating a pwm to read a signal and the output
| of the sensor to one of the other IO pins, similar as used on the
| Dilberts (thx Larry!)
| 
| However, as the output of the sensor is in serial, I can't quite get
| the timing right to read it properly...
| 
| I've read the previous post which converts code for the BSII to C
| (http://groups.yahoo.com/group/AVR-Chat/message/7190) but I'm not
| quite sure how to build on top of that...
| 
| *boggled yet again....
| 
| 
| 
| 
| 
| 
| 
| Yahoo! Groups Links
| 
| 
| 
| 
|

Re: GP2D02 + ATmega32

2006-09-10 by 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? =(

--- In AVR-Chat@yahoogroups.com, "Larry Barello" <yahoo@...> wrote:
>
> Don't use PWM output to drive the unit.  Have you seen
> http://www.barello.net/Papers/GP2D02?  
> 
> The assembly code is complex since it multiplexes three sensors 
but at it's
> core it is simple, it does the clocking and shifting of data in 
s/w which is
> more than fast enough and easy to control.  I paraphrase below, 
for one
> sensor (I used 9 for the loop to discard the first bit, you could 
re-arrange
> and make it 8 loops):
> 
> Set control port pin low
> wait for input pin to go high (or a max of 30ms IIRC)
> for (i = 0; i< 9; i++)
> {
> 	if input high
> 		output |= 1;	// Set low bit
> 	output << 1;		// Left shift
> 	set control pin low	// Clock in next bit
> 	wait 1 us
> 	set control pin high
> 	wait 16 us
> }
> set control pin high.
> 
> At the end of the routine your data should be in "output" and the 
sensor
> should be in a low-power state ready for another reading.  Please 
make sure
> the code you write follows the clock diagram in the data sheet. I 
just
> paraphrased assembly code I wrote over 7 years ago and might have 
made a
> mistake or two...
> 
> -----------
> Larry Barello
> www.barello.net
> 
> 
> | -----Original Message-----
> | From: AVR-Chat@yahoogroups.com [mailto:AVR-Chat@yahoogroups.com] 
On Behalf
> | Of katraven1
> | Sent: Thursday, August 31, 2006 8:54 AM
> | To: AVR-Chat@yahoogroups.com
> | Subject: [QUARANTINE] [AVR-Chat] GP2D02 + ATmega32
> | Importance: Low
> | 
> | Hey guys, me again =P
> | 
> | I've been spending the last couple of days trying to get one ex-
Sharp
> | infrared sensor to communicate wif my ATmega32. It's jst 
connected to
> | the OC0 pin on PORTB generating a pwm to read a signal and the 
output
> | of the sensor to one of the other IO pins, similar as used on the
> | Dilberts (thx Larry!)
> | 
> | However, as the output of the sensor is in serial, I can't quite 
get
Show quoted textHide quoted text
> | the timing right to read it properly...
> | 
> | I've read the previous post which converts code for the BSII to C
> | (http://groups.yahoo.com/group/AVR-Chat/message/7190) but I'm not
> | quite sure how to build on top of that...
> | 
> | *boggled yet again....
> | 
> | 
> | 
> | 
> | 
> | 
> | 
> | Yahoo! Groups Links
> | 
> | 
> | 
> | 
> |
>

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

2006-09-10 by James Ronald

Here's another link.  The sample code is in basic and easy to follow.  You should be able to get a good idea of how GP2D02 works.  

http://www.acroname.com/robotics/info/ideas/avrexamp/avrexamp.html#s1
Show quoted textHide quoted text
  ----- Original Message ----- 
  From: katraven1 
  To: AVR-Chat@yahoogroups.com 
  Sent: Sunday, September 10, 2006 12:40 PM
  Subject: [AVR-Chat] Re: GP2D02 + ATmega32


  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? =(

  --- In AVR-Chat@yahoogroups.com, "Larry Barello" <yahoo@...> wrote:
  >
  > Don't use PWM output to drive the unit.  Have you seen
  > http://www.barello.net/Papers/GP2D02?  
  > 
  > The assembly code is complex since it multiplexes three sensors 
  but at it's
  > core it is simple, it does the clocking and shifting of data in 
  s/w which is
  > more than fast enough and easy to control.  I paraphrase below, 
  for one
  > sensor (I used 9 for the loop to discard the first bit, you could 
  re-arrange
  > and make it 8 loops):
  > 
  > Set control port pin low
  > wait for input pin to go high (or a max of 30ms IIRC)
  > for (i = 0; i< 9; i++)
  > {
  > if input high
  > output |= 1; // Set low bit
  > output << 1; // Left shift
  > set control pin low // Clock in next bit
  > wait 1 us
  > set control pin high
  > wait 16 us
  > }
  > set control pin high.
  > 
  > At the end of the routine your data should be in "output" and the 
  sensor
  > should be in a low-power state ready for another reading.  Please 
  make sure
  > the code you write follows the clock diagram in the data sheet. I 
  just
  > paraphrased assembly code I wrote over 7 years ago and might have 
  made a
  > mistake or two...
  > 
  > -----------
  > Larry Barello
  > www.barello.net
  > 
  > 
  > | -----Original Message-----
  > | From: AVR-Chat@yahoogroups.com [mailto:AVR-Chat@yahoogroups.com] 
  On Behalf
  > | Of katraven1
  > | Sent: Thursday, August 31, 2006 8:54 AM
  > | To: AVR-Chat@yahoogroups.com
  > | Subject: [QUARANTINE] [AVR-Chat] GP2D02 + ATmega32
  > | Importance: Low
  > | 
  > | Hey guys, me again =P
  > | 
  > | I've been spending the last couple of days trying to get one ex-
  Sharp
  > | infrared sensor to communicate wif my ATmega32. It's jst 
  connected to
  > | the OC0 pin on PORTB generating a pwm to read a signal and the 
  output
  > | of the sensor to one of the other IO pins, similar as used on the
  > | Dilberts (thx Larry!)
  > | 
  > | However, as the output of the sensor is in serial, I can't quite 
  get
  > | the timing right to read it properly...
  > | 
  > | I've read the previous post which converts code for the BSII to C
  > | (http://groups.yahoo.com/group/AVR-Chat/message/7190) but I'm not
  > | quite sure how to build on top of that...
  > | 
  > | *boggled yet again....


[Non-text portions of this message have been removed]

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

2006-09-10 by Larry Barello

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? =(
|

[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
Show quoted textHide quoted text
> | is to a surface..
> | 
> | am I still missing the mark here? =(
> |
>

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

2006-09-11 by Larry Barello

I don't think your shifting of data in does what you want it to do...  I
would guess it always returns 0xFF00 or maybe 0x7F80

change 
 	output |= (IR_PIN & 0x80);
to
	if (IR_PIN & _BV(7))
		output |= 0x01;

Cheers!

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

2006-09-12 by katraven1

hmmz... jst tried using that line instead..
now it says the ir_reading = 0... =(


--- In AVR-Chat@yahoogroups.com, "Larry Barello" <yahoo@...> wrote:
>
> I don't think your shifting of data in does what you want it to 
do...  I
Show quoted textHide quoted text
> would guess it always returns 0xFF00 or maybe 0x7F80
> 
> change 
>  	output |= (IR_PIN & 0x80);
> to
> 	if (IR_PIN & _BV(7))
> 		output |= 0x01;
> 
> Cheers!
>

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

2006-09-12 by katraven1

well, now i feel jst silly...

after all this stressing and request for aid it turns out there was 
nothing wrong with my code - the error was assuming delay_ms(70) 
actually means a delay of 70ms...

i dun believe it... calling library functions without research is a 
bad idea... assumptions are bad things (i need to remember this...)

after borrowing a logic probe from my supervisor, we found the 
problem of the incorrect delays, thus not producing the correct 
signal to drive the sensor.

Once these delays were set right (go voodoo programming) the ir 
responds as expected and now working perfectly fine.

thx for all the help guys.. and sorry for not finding this out 
sooner...

--- In AVR-Chat@yahoogroups.com, "Larry Barello" <yahoo@...> wrote:
>
> I don't think your shifting of data in does what you want it to 
do...  I
Show quoted textHide quoted text
> would guess it always returns 0xFF00 or maybe 0x7F80
> 
> change 
>  	output |= (IR_PIN & 0x80);
> to
> 	if (IR_PIN & _BV(7))
> 		output |= 0x01;
> 
> Cheers!
>

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

2006-09-12 by katraven1

how do i check/set that?

i assumed (again) that as I didn't alter it, the clock defaults to the 
internal rc which is set to 1Mhz...

F_CPU has been set to equal 1000000UL <- admittedly i dun really noe 
what the UL at the end is but that's the way the delay.h defined it to 
mean 1 MHz...

am i missing something here?

--- In AVR-Chat@yahoogroups.com, "Dave Hylands" <dhylands@...> wrote:
Show quoted textHide quoted text
>
> Hi,
> 
> > after all this stressing and request for aid it turns out there was
> > nothing wrong with my code - the error was assuming delay_ms(70)
> > actually means a delay of 70ms...
> 
> As far as I know it does. Provided of course that you set the F_CPU
> define to match the frequency that you're actually running your AVR
> at.
> 
> -- 
> Dave Hylands
> Vancouver, BC, Canada
> http://www.DaveHylands.com/
>

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

2006-09-12 by Dave Hylands

Hi,

> after all this stressing and request for aid it turns out there was
> nothing wrong with my code - the error was assuming delay_ms(70)
> actually means a delay of 70ms...

As far as I know it does. Provided of course that you set the F_CPU
define to match the frequency that you're actually running your AVR
at.

-- 
Dave Hylands
Vancouver, BC, Canada
http://www.DaveHylands.com/

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.