'{$STAMP BS2} '{$PBASIC 2.5} ' 2008 mlab.taik.fi/paja ' MEMSIC2125 Accelerometer ' ---[ I/O Definitions ]--- pXin PIN 8 pYin PIN 9 ' ---[ Variables ]--- wXPulse VAR WORD ' raw data from accelerometer wXGravity VAR WORD ' X axis gravity wYPulse VAR WORD ' raw data from accelerometer wYGravity VAR WORD ' Y axis gravity ' ---[ Constants ]--- cPulseState CON 1 cScale CON $200 ' 2.0 us per unit ' ---[ Initialization ]--- DEBUG CLS, "MEMSIC2125 Accelerometer", CR ' ---[ Main Code ]--- Main: DO GOSUB Read_Accelerometer DEBUG "x: ", SDEC wXGravity, TAB, "y: ", SDEC wYGravity, CR LOOP END ' ---[ Subroutines ]--- Read_Accelerometer: ' PULSIN Pin, State, Variable ' Pin: Pin number (0 - 15) ' State: Specifies whether the puse to be mesured is low(0) or high(1) ' Variable: the measured pulse duration will be stored. PULSIN pXin, cPulseState, wXPulse ' read pulse output wXPulse = wXPulse */ cScale ' convert to uSecs for BS2 wXGravity = ((wXPulse / 10) - 500) * 8 ' calculating the pulse to 1/1000 g PULSIN pYin, cPulseState, wYPulse wYPulse = wYPulse */ cScale wYGravity = ((wYPulse / 10) - 500) * 8 RETURN