|
本帖最後由 vegewell 於 2011-5-3 21:41 編輯
這是BStamp code .
我們可使用BASIC Stamp 的SHIFTOUT 和SHIFTIN 指令來
讀出H48C 的加速度值(從串列傳輸接收訊號)。
請參考與研究:
程式碼
'================================================================
' File....... H48C_3-Axis.BS2
' Purpose.... Hitachi H48C 3-Axis Accelerometer Demonstration
' Author..... Copyright (c) 2005-2006 Parallax, Inc.
' E-mail..... support@parallax.com
' Started....
' Updated.... 02 FEB 2006
' {$STAMP BS2}
' {$PBASIC 2.5}
'================================================================
' -----[ I/O Definitions ]-------------------------------------------------
Dio PIN 15 ' data to/from module
Clk PIN 14 ' clock output
CS PIN 13 ' active-low chip select
' -----[ Constants ]-------------------------------------------------------
XAxis CON 0 ' adc channels
YAxis CON 1
ZAxis CON 2
Copyright© Parallax Inc. ‧ Hitachi H48C 3-Axis Accelerometer Module (#28026) ‧
7/27/2007 Rev 1.2 Page 5 of 6
VRef CON 3
Cnt2Mv CON $CE4C ' counts to millivolts
' 0.80586 with **
GfCnv CON $3852 ' g-force conversion
' 0.22 with **
' -----[ Variables ]-------------------------------------------------------
axis VAR Nib ' axis selection
rvCount VAR Word ' ref voltage adc counts
axCount VAR Word ' axis voltage adc counts
mVolts VAR Word ' millivolts
gForce VAR Word ' axis g-force
dValue VAR Word ' display value
dPad VAR Nib ' display pad
' -----[ Initialization ]--------------------------------------------------
Reset:
HIGH CS ' deselect module
DEBUG CLS, ' paint display
"=========================", CR,
"H48C 3-Axis Accelerometer", CR,
"=========================", CR,
CR,
" Count Volts G ", CR,
" ----- ----- -----", CR,
"VRef ", CR,
" X ", CR,
" Y ", CR,
" Z "
' -----[ Program Code ]----------------------------------------------------
Main:
FOR axis = XAxis TO ZAxis ' loop through each axis
GOSUB Get_H48C ' read vRef & axis counts
dValue = rvCount ' display vRef count
DEBUG CRSRXY, 6, 6
GOSUB RJ_Print
dValue = axCount ' display axis count
DEBUG CRSRXY, 6, (7 + axis)
GOSUB RJ_Print
mVolts = rvCount ** Cnt2Mv ' convert vref to mv
DEBUG CRSRXY, 13, 6, ' display
DEC (mVolts / 1000), ".",
DEC3 mVolts
mVolts = axCount ** Cnt2Mv ' convert axis to mv
DEBUG CRSRXY, 13, (7 + axis),
Copyright© Parallax Inc. ‧ Hitachi H48C 3-Axis Accelerometer Module (#28026) ‧
7/27/2007 Rev 1.2 Page 6 of 6
DEC (mVolts / 1000), ".",
DEC3 mVolts
' calculate g-force
' -- "gForce" is signed word
IF (axCount >= rvCount) THEN
gForce = (axCount - rvCount) ** GfCnv ' positive g-force
ELSE
gForce = -((rvCount - axCount) ** GfCnv) ' negative g-force
ENDIF
DEBUG CRSRXY, 20, (7 + axis), ' display g-force
" " + (gForce.BIT15 * 13),
DEC1 (ABS(gForce) / 100), ".",
DEC2 ABS(gForce)
NEXT
PAUSE 200
GOTO Main
' -----[ Subroutines ]-----------------------------------------------------
' Reads VRef and selected H48C axis through an MCP3204 ADC
' -- pass axis (0 - 2) in "axis"
' -- returns reference voltage counts in "rvCount"
' -- returns axis voltage counts in "axCounts"
Get_H48C:
LOW CS
SHIFTOUT Dio, Clk, MSBFIRST, [%11\2, VRef\3] ' select vref register
SHIFTIN Dio, Clk, MSBPOST, [rvCount\13] ' read ref voltage counts
HIGH CS
PAUSE 1
LOW CS
SHIFTOUT Dio, Clk, MSBFIRST, [%11\2, axis\3] ' select axis
SHIFTIN Dio, Clk, MSBPOST, [axCount\13] ' read axis voltage counts
HIGH CS
RETURN
' -------------------------------------------------------------------------
' Right-justify value in 5-digit field
' -- move cursor first, then call with value in "dValue"
RJ_Print:
LOOKDOWN dValue, >=[10000, 1000, 100, 10, 0], dPad
DEBUG REP " "\dPad, DEC dValue
RETURN |
|