'----[sharp_ir_demo.bs2]----------------- '{$STAMP BS2} '{$PBASIC 2.5} ' ' File....... sharp_ir_demo.BS2 ' Purpose.... Distance readings using a Sharp GP2D12 ' IR sensor, ADC0831 8-bit Analog ' to Digital Converter, and S2. ' Author..... CustCrawler Inc. (Mike Gebhard) ' E-mail..... support@crustcrawler.com ' Started.... 4 November 2004 ' Updated.... 4 November 2004 ' ' This program allows you to: ' 1. Display raw 8 bit A/D data vs. Voltage in mv. ' 2. Find the distance in cm to an object ' ' Resources: ' Please take the time to look through the ADC0831 datasheet ' http://www.crustcrawler.com/downloads/datasheet/dsadc0831.pdf ' ' SHARP GP2D12 datasheet ' http://www.crustcrawler.com/downloads/datasheet/gp2d12.pdf ' ' Voltage to distance calculation using the ' ADC0831 8 bit A/D and Sharp IR. ' http://www.acroname.com/robotics/info/articles/irlinear/irlinear.html ' This resource contains everythnig you need to understand ' the distance calculation in this program. ' ' October's issue of Nuts and Volts "Stamp Applications" ' "Measuring Up - Up to 80 cm, That is" ' by Jon Williams is another great resource. ' '========================================================================= ' ADC0831 to BOE and GP2D12 connections ' http://www.crustcrawler.com/images/schematics/Two_GP2D12_ADC0831.gif '========================================================================= ' Vdd(+5V) ' ____________ | ' |1 | | 3.3-10 micro F ' pin 9/8-|CS A Vcc |-|------||-------| ' | D | Vdd(+5V) | ' GP2D12 -|Vin+ C CLK |- pin 11 | | ' | 0 | / Vss ' |---|Vin= 8 Dout|- pin 10 \ ' | | 3 | / 1k resistor ' |---|GND 1 Vref|-----------| ' | |____________| / ' | \ 2k resistor (ref) ' Vss / 2.74 V ' | ' Vss '========================================================================= ' GP2D12 to BOE and ADC0831 '========================================================================= ' ___________________ ' | GP2D12 | ' | Front | ' |_Vo_____GND____Vcc_| ' | | | ' | 33mf | | ' |--||--| | ' Vin+ Vss Vdd ' ADC0831 BOE BOE ' ' How does it work? ' Connect the circuit above or visit ' http://www.crustcrawler.com/images/schematics/ADC0831_GP2D12.gif '------------------------------------------------------------------------- ' Pin Assignments CS CON 9 ' Chip Select DIO_10 CON 10 ' Data I/O pin 10 CLK CON 11 ' Clock ' ADC0831 A/D reading to Range ' transform constants. ' These constants are used to calculate ' distance are were derived separately from ' this program. More information can be ' found in the comments above m CON 3200 b CON 3 k CON 4 ADResult VAR Byte ' Variable to hold 8-bit AD result. AvgReading VAR Byte ' Average ADC0831 Reading distance VAR Byte ' IR distance calculation counter VAR Nib ' Loop to average ADC0831 readings ' Deactivate ADC0831 and initialize debug display HIGH CS DEBUG CLS DEBUG "-------------------------------------------",CR, " ADC0831 8-bit A/D, Sharp GP2D12 IR Sensor,",CR, " distance calculations.",CR, "-------------------------------------------",CR,CR, "GP2D12 Vout (estimate)........",CR, "ADC0831 8-Bit output..........",CR, "GP2D12 distance reading.......",CR Main: AvgReading = 0 FOR counter = 1 TO 3 ' Average reading GOSUB ADC0831_Reading ' Get 8 bit data from ADC. AvgReading = AvgReading + (ADResult/3) PAUSE 50 NEXT GOSUB ADC0831_Distance_Calc ' Get ADC0831 distance GOSUB Display_Results ' Display distance and raw data GOTO Main ' Endless loop. '---- [Sub routines] ------------------------- ' Get ADC0831 8 bit data ADC0831_Reading: LOW CS ' Activate the ADC. SHIFTIN DIO_10,CLK, ' Get data bits from. MSBPOST,[ADResult\9] ' ADC0831 HIGH CS ' Deactivate the ADC. RETURN ' Linearizing function ADC0831_Distance_Calc: distance = (m /(AvgReading + b)) - k RETURN ' Update the Debug screen Display_Results: DEBUG CRSRXY, 30, 5, DEC (AvgReading-7)*10, "mV", CLREOL DEBUG CRSRXY, 30, 6, DEC AvgReading, CLREOL DEBUG CRSRXY, 30, 7, DEC distance, CLREOL PAUSE 100 RETURN