| GP2D02 to OOPic Example Last Modified: 2006-11-06 | | |
| Acroname Robotics | PDF webpage version | ||
| ![]() Introduction In this example, an OOpic microcontroller uses a Sharp GP2D02 detector to measure distances to obstacles. The program clocks in bits from the GP2D02 detector and assembles them into an 8-bit distance measurement. The program turns on two LEDs when this value is within a desirable range. It turns on only one of LEDs when the distance is either too near or too far. Circuit Schematic The following schematic assumes the OOPic has power. When the GP2D02 is taking a measurement, it has an average current draw of 22ma so it can be powered by the OOPic's 100ma onboard regulator. The pin selections match those in the source code listed below. This circuit uses an interface diode to prevent the HIGH logic level of the OOPic's output from exceeding the 3.3 volts allowed at the VIN input of the GP2D02 detector. This diode is included with and described in the instructions of the GP2D02 package. The VOUT output connects directly to an OOpic pin. All that remains is hooking up two LEDs. ![]() Source Code This code uses BASIC syntax. It runs with version 3.0.1 of the OOPic compiler and version A.1.7 of the OOPic chip. The Sharp GP2D02 has strict timing requirements for clocking out the data bits. The code uses an oOneShot object to generate short clock pulses on the VIN input line. Pulses that are too long will reset the GP2D02. The routine to read the GP2D02 has two consecutive commands to set VIN high in order to reset the sensor. Repeating the instruction twice adds enough delay to satisfy the 1.5ms time required to reset the sensor properly. The GP2D02 is ideal for microcontrollers without analog-to-digital conversion capability. However, since the OOPic has ADC inputs, it is usually simpler to use GP2D12 sensors on an OOPic. ' filename: GP2D02-5.osc
' This program reads the distance to an obstacle from
' the Sharp GP2D02 sensor and controls two LEDs
' in the following manner:
'
' #9 #8
' too close 1 0
' just right 1 1
' too far 0 1
'
' Such a program could be the starting point for a
' simple wall-hugging robot.
dim bit1 As New oDio1
dim bit2 As New oDio1
dim bX as new oByte
sub main()
bit1.Ioline = 9
bit1.Direction = cvOutput
bit2.Ioline = 8
bit2.Direction = cvOutput
call setup02
do
call read02
if bX.value<100 then
bit1.value=0
else
bit1.value=1
end if
if bX.value>120 then
bit2.value=0
else
bit2.value=1
end if
loop
end sub
'-----------------------------------------------------------
dim bitVIN as new oDio1 ' VIN green
dim bitVOUT as new oDio1 ' VOUT yellow
dim ii as new oByte ' counts bits
dim qq as new oBit ' input to one-shot
dim bitCLK as new oOneShot ' sends short clock pulses to GP2D02
sub setup02()
bitVIN.Ioline = 12
bitVIN.Direction = cvOutput
bitVOUT.Ioline = 13
bitVOUT.Direction = cvInput
bitCLK.Input.Link(qq.value)
bitCLK.Output.Link(bitVIN)
bitCLK.Operate=cvTrue
end sub
sub read02()
bitVIN.value=1 ' VIN high for 1.5ms
bitVIN.value=1
bitVIN.value=0
while bitVOUT.value=0 ' wait for measurement to finish
wend
bX.value=0
for ii.value=1 to 8
bX.LShift
qq.set ' trigger one-shot to clock the GP2D02
qq.clear
bX.value=bX.value or bitVOUT.value
next ii.value
end sub
Revision History:
| |||
| voice: 720-564-0373, email: sales@acroname.com, address: 4822 Sterling Dr., Boulder CO, 80301-2350, privacy © Copyright 1994-2010 Acroname, Inc., Boulder, Colorado. All rights reserved. |