PDA

View Full Version : Ir distance sensor



dantaylor129
02-22-2010, 07:40 PM
Can someone give me a simple example code for the Sharp IR distance sensor to work with the BASIC Stamp?

MikeG
02-22-2010, 11:19 PM
Which Sharp IR sensor? Anyway, here's code for the Max1112/1113 ADC and GP2D12.


'
'---- [max1113_gp2d12.BS2] -------------------------------------------------------
'{$STAMP BS2}
'{$PBASIC 2.5}
'
' Hardware
' (1) BOE\Basic Stamp
' (1) Max1113 4-channel 8-bit A/D
' (1) SHARP GP2D12 Infrared Distance Detector
' (2) 1.0 mF capacitors
' (1) 0.1 mF capacitor
' (1) 5-10k potentiometer
'
'================================================= ========================
' Updates
'================================================= ========================
' Add MAX1112 Schematics
'
'================================================= ========================
' About max1113_gp2d12.BS2
'================================================= ========================
' Use this program to test your MAX1112/1113 connections. This program
' writes the following to the DEBUG terminal screen.
' 1. Distance in cm to an object.
' 2. Raw 8 bit A/D data form the MAX1113/1112 A/D.
' 3. MAX1113 Control Byte
'
' Below are several resources for your convenience.
'
'================================================= ========================
' MAX 1113 (Pin Out) (Fig 1)
'================================================= ========================
' ----------
' | |
'Ch0 -|1 M 16|- Vdd
' | A |
'Ch1 -|2 X 15|- SCLK
' | | __
'Ch2 -|3 1 14|- CS
' | 1 |
'Ch3 -|4 1 13|- DIN
' | 3 |
'COM -|5 12|- SSTRB
'____ | |
'SHDN -|6 11|- Dout
' | |
'RFin -|7 10|- DGND
' | |
'FRout -|8 9|- AGND
' | |
' ----------

' ----------
' | |
'Ch0 -|1 M 20|- Vdd
' | A |
'Ch1 -|2 X 19|- SCLK
' | | __
'Ch2 -|3 1 18|- CS
' | 1 |
'Ch3 -|4 1 17|- DIN
' | 2 |
'Ch4 -|5 16|- SSTRB
' | |
'Ch5 -|6 15|- Dout
' | |
'Ch6 -|7 14|- DGND
' | |
'Ch7 -|8 13|- AGND
' | |
'COM -|9 12|- REFOUT
'____ | |
'SHDN -|10 11|- REFIN
' | |
' ----------
'
'================================================= ========================
' Schematic (Fig 2)
'================================================= ========================
' The schematic below is based on a typical operating circuit. This
' circuit can be found on page 8 of the MAX1113\1112 datasheet.
'
' The differences:
' 1. Added a voltage divider to Pin 7 (reference in) and set it to
' +2.55V. The MAX1113/1112 is an 8-bit A/D (256 possible values)
' therefore a reference voltage of 2.55V makes provides 256 output
' values for this application.
' ____
' 2. SHDN set to high (5V).
'
' Ch0 - Analog input (program default)
' Ch1 - Analog input
' Ch2 - Analog input
' Ch3 - Analog input
'
' |-----|-------------------|--------|
' | | | | +5V
' | --- --- 1mF --- 0.1mF |
' | - ---------- --- --- |
' | | | | | |
' | Ch0----|1 M 16|---|--------|------------|
' | | A |
' | Ch1----|2 X 15|---PIN 8 (STAMP)
' | | |
' | Ch2----|3 1 14|---PIN 9 (STAMP)
' | | 1 |
' | Ch3----|4 1 13|---PIN 10 (STAMP)
' | | 3 |
' +5V |----------|5 12|---NC
' | | |
' |-------------|6 11|---PIN 11 (STAMP)
' \ | |
' /<--[+2.55V]--|7 10|---|
' \ | | |
' / |------|8 9|---|
' \ | | | |
' | --- ---------- |
' | --- 1mF |
' |------|---------------------|
' |
' ---
' - +5V (Vdd)
' |
' ---------- |
' | | |
' Ch0----|1 M 20|---Vdd-----------------------|----------|
' | A | | |
' Ch1----|2 X 19|---SCLK----PIN 8 (STAMP) --- 1mF --- 0.1mF
' | | __ --- ---
' Ch2----|3 1 18|---CS------PIN 9 (STAMP) | |
' | 1 | | |
' Ch3----|4 1 17|---DIN-----PIN 10 (STAMP) |----------|
' | 2 | |
' Ch4----|5 16|---SSTRB---NC |
' | | |
' Ch5----|6 15|---Dout----PIN 11 (STAMP) |
' | | |
' Ch6----|7 14|---DGND----|----------------------------|
' | | | +5V (Vdd) |
' Ch7----|8 13|---AGND----| | |
' | | | |
' |----------COM----|9 12|---REFOUT------| / |
' | +5V ____ | | | \ |
' | (Vdd)--- SHDN -|10 11|---REFIN-------|---->/<---[+2.55V] |
' | | | | \ |
' | ---------- | / |
' | | | |
' | --- | |
' | --- | |
' ---------------------------------------------|-----|------------------|
' |
' --- STAMP (Vss)
' -
'
'
'================================================= ========================
' Simple 5-10k potentiometer voltage divider circuit (Fig 3)
'================================================= ========================
' Use a simple voltage divider to reference 2.55V on MAX1113 pin7 (RFin).
' The internal reference is disabled in the circuit above. The datasheet
' has more information about using the MAX1113 and 1112.
'
' +5V STAMP(Vdd)
' |
' |
' \
' P / (5-10k pot)
' O \
' T /<---| (+2.55V)
' \ |
' / MAX1113 Pin 7
' \ MAX1112 Pin 11
' |
' |
' --- STAMP(Vss)
' -
'
'================================================= ========================
' SHARP GP2D12 (Pin Out)
'================================================= ========================
'
' |----------------|
' | --- --- |
' | --- --- |
' | --- --- |
' |----------------|
' | | |
' | | |
' | --- |---Red--+5V (STAMP Vdd)
' White -
' | (STAMP Vss Black)
' |
' MAX 1113
' Pin 1, 2, 3, or 4
' MAX 1112
' Pin 1, 2, 3, 4, 5, 6, or 7
'
'================================================= ========================
' Resources
'================================================= ========================
' Sharp IR Range Finder
' Voltage-To-Range Conversion
' http://www.acroname.com/robotics/info/articles/irlinear/irlinear.html
'
' The October 2004 issue of Nuts and Volts "Stamp Applications"
' "Measuring Up - Up to 80 cm, That is"
' by Jon Williams
'
'
'-------------------------------------------------------------------------

'-----[ I/O Definitions ]-------------------------------------------------
SCLK CON 8 'Serial Clock
CS CON 9 'Chip Select
Din CON 10 'Data In
Dout CON 11 'Data Out

'-----[ Constants ]-------------------------------------------------------

'-----[ Transform Function Constants ]------------------------------------
Slope CON 3200
y_Intercept CON 3
k CON 4

'-----[ Variables ]------------------------------------------------------
ctrlByte VAR Byte 'Control byte
'Control byte bits
startBit VAR ctrlByte.BIT7 'Start bit
sel2 VAR ctrlByte.BIT6 '
sel1 VAR ctrlByte.BIT5 '
sel0 VAR ctrlByte.BIT4 'Used with MAX1112 else N/A
unipolar VAR ctrlByte.BIT3 '
singleEnded VAR ctrlByte.BIT2 '
fullyOperer VAR ctrlByte.BIT1 '
extClock VAR ctrlByte.BIT0 '
'Control byte high/low Nib
lowNibCtrl VAR ctrlByte.LOWNIB 'Low Nib
highNibCtrl VAR ctrlByte.HIGHNIB 'High Nib

ADResults VAR Byte 'Raw A/D reading
AvgReading VAR Byte 'Average Raw A/D reading
distance VAR Byte 'Distance holder
avgCounter VAR Nib 'Counter used in Average A/D
' reading
#DEFINE debugMode = 1 'Debug Mode

'-------------------------------------------------------------------------
'Unipolar | Single ended | Fully operational | External clock
'lowNibCtrl = %1111
'highNibCtrl = %1111
'-------------------------------------------------------------------------

'Control byte settings
startBit = 1

'================================================= ========================
' MAX 1113
'================================================= ========================
' sel 2 | sel 1 | sel 0 | Channel
' 0 0 N/A 0
' 1 0 N/A 1
' 0 1 N/A 2
' 1 1 N/A 3
'================================================= ========================

sel2 = 0
sel1 = 0
sel0 = 0
unipolar = 1
singleEnded = 1
fullyOperer = 1
extClock = 1

'Initialize
ADResults = 0
AvgReading = 0
HIGH CS
LOW Din

#IF debugMode #THEN
GOSUB display
#ENDIF

'-----[ Main Routine ]----------------------------------------------------
Main:
AvgReading = 0
FOR avgCounter = 1 TO 3 ' Average IR raw distance reading
GOSUB RawData_Max1113 ' Get 8 bit data from A/D.
AvgReading = AvgReading + (ADResults/3)
PAUSE 10 ' Give it a sec
NEXT
GOSUB CalcDistance
#IF debugMode #THEN
GOSUB Write_Values
#ENDIF
PAUSE 1000
GOTO Main

'-----[ Sub Routines ]----------------------------------------------------
RawData_Max1113:
LOW CS
SHIFTOUT Din, SCLK, MSBFIRST, [ctrlByte\8]
SHIFTIN Dout, SCLK, MSBPRE, [ADResults\10]
HIGH CS
RETURN

CalcDistance:
distance = (Slope /(AvgReading + y_Intercept)) - k
RETURN

'-----[ Display ]---------------------------------------------------------
#IF debugMode #THEN
Display:
DEBUG CLS
DEBUG "MAX A/D GP2D12 IR",CR,
"======================",CR,
"Distance......", CR,
"A/D Reading...", CR,
"Control Byte..", BIN ctrlByte
'"mV: "
RETURN

Write_Values:
DEBUG CRSRXY, 15, 2, CLREOL, DEC distance,
CRSRXY, 15, 3, CLREOL, DEC AvgReading
'CRSRXY 15, 5, CLREOL, DEC (AvgReading*10)
RETURN
#ENDIF