我是自動控制方面的新手,因為學校出了一樣作業叫專題製作,對這方面還處於生手的我還煩請專家們幫忙解答
最近我買了一個GP2Y0A02YK紅外線距離感測器,並搭配了ADC0831的IC來穩定輸出
電路及程式的來源都是利用飆機器人網站所提供的資料
電路是利用GP2D12 紅外線距離感測器的接線方式 如第一張圖http://www.playrobot.com/files/SharpGP2D12Snrs.pdf
此為GP2Y0A02YK紅外線距離感測器的資料http://sharp-world.com/products/device/lineup/data/pdf/datasheet/gp2y0a02_e.pdf
當我完成電路,並用Setup-Stamp-Editor-v2.5.2撰寫完程式,偵測時卻一直保持著10cm,拔掉感測器三條線(Vo、Vcc、GND)的其中一條,偵測值就會隨意跳動,若全部拔掉就只固定在150cm,是不是我的程式或電路出了什麼問題了呢??
程式撰寫的部份
' =========================================================================
' File...... GP2D12 Demo.bs2
' Purpose... Demonstrate GP2D12
' Author.... Parallax, Inc.
' {$STAMP BS2}
' {$PBASIC 2.5}
' =========================================================================
' -----[ Program Description ]---------------------------------------------
' This program demonstrates reading the distance in centimeters from the
' Sharp GP2D12 Analog Distance Sensor.
' -----[ I/O Definitions ]-------------------------------------------------
Adc0831 PIN 0 ' ADC0831 Chip Select (ADC0831.1)
AdcClock PIN 1 ' ADC0831 Clock (ADC0831.7)
AdcData PIN 2 ' ADC0831 Data (ADC0831.6)
' -----[ Constants ]-------------------------------------------------------
span CON 5 ' 5 cm Per Data Point
' -----[ Variables ]-------------------------------------------------------
result VAR Byte ' ADC8031 Result
volts VAR Word ' Volts (0.01 Increments)
cm VAR Byte ' centimeters
index VAR Nib
test1 VAR Byte ' Values For
test2 VAR Byte ' Interpolation
slope VAR Word ' mV/cm between test points
' -----[ EEPROM Data ]-----------------------------------------------------
Vout DATA 251, 179, 139, 114, 97
DATA 85, 76, 67, 62, 57
DATA 53, 50, 48, 46, 43
DATA 0
' -----[ Initialization ]--------------------------------------------------
HIGH Adc0831 ' Disable ADC0831
' -----[ Program Code ]----------------------------------------------------
DO
GOSUB Read_GP2Y0A02YK ' Read Sensor Value
GOSUB Calculate_Distance ' Convert Value To cm
DEBUG HOME, "Distance = ", DEC cm, " cm "
PAUSE 100
LOOP
END
' -----[ Subroutines ]-----------------------------------------------------
Read_GP2Y0A02YK:
volts = 0 ' Reset Sensor Value
FOR index = 0 TO 2 ' Read 3 Times
LOW Adc0831 ' Enable ADC0831
SHIFTIN AdcData, AdcClock, MSBPOST, [result\9] ' Read The Voltage
HIGH Adc0831 ' Disable ADC0831
volts = volts + result ' Add The Values
PAUSE 30
NEXT
volts = volts / 3 ' Average The Readings
RETURN
Calculate_Distance:
FOR index = 0 TO 15 ' Search DATA Table For Value
READ (Vout + index), test2 ' Get Value From DATA Table
IF (test2 <= volts) THEN EXIT ' Found Value
NEXT
SELECT index
CASE 0
cm = 10 ' Set To Minimum Distance
CASE 1 TO 14 ' Calculate Distance
cm = 10 + (5 * index)
IF (test2 < volts) THEN ' Estimate Using Interpolation
READ (Vout + index - 1), test1
slope = (test1 - test2) * 10 / span ' Calculate Slope
cm = cm - ((volts - test2) * 10 / slope)
ENDIF
CASE 15
cm = 150 ' Set To Maximum Distance
ENDSELECT
RETURN |