I noticed couple of misstakes in the code.
One in the two's complement calculation formula. Previously posted formula gave result as: 255 = 0, 254 = (-)1, etc ... Correct formula should give 255 = (-)1, 254 = (-)2, etc ...
Error was minor, but here is updated code (only change is 255 -> 256).
Other in Acceleration_Menu -loop. Accelerometer_Init required only once you start.
Code:
Acceleratio var word
Acceleratio_max var word
X_Data var Byte
Y_Data var Byte
COUNTER VAR BYTE
TEMP VAR BYTE
'-------------------------------------------------------------------------------
Accelerometer_Init:
Acceleratio = 0
Acceleratio_max = 0
COUNTER = 0
'Axes
I2CAddress_3D = $20 'CTRL_REG1
TEMP = %01000011 'DR=0, PD=1, FS=0, STP=00, Zen=0, Yen=1, Xen=1 (X,Y enable, Output 100Hz)
gosub I2C_Write_3D '
'Interrupts
I2CAddress_3D = $22 'CTRL_REG3
TEMP = %00000100 'IHL = 0, PP_OD = 0, I2CFG2-0 = 000, I1CFG2-0 = 100 (DataReady = INT1)
gosub I2C_Write_3D
Return
'-------------------------------------------------------------------------------
I2C_Write_3D:
I2CWRITE SDA_3D, SCL_3D, I2CDevice_3D_write, I2CAddress_3D,[ TEMP ]
Return
I2C_Read_3D:
I2cread SDA_3D, SCL_3D, I2CDevice_3D_read, I2CAddress_3D,[ TEMP ]
Return
'-------------------------------------------------------------------------------
Acceleration_Menu:
gosub Accelerometer_Init 'Start from here
Acceleration_Loop:
IF Int1_3D = 1 THEN 'Int1_3D is IO pin for Interrup coming from 3D sensor, DataReady in device
COUNTER = COUNTER + 1
'---------------------------
I2CAddress_3D = $29 'Read OUTX value
gosub I2C_Read_3D
if TEMP > 127 then 'Two's complementary changed to amplitude information
X_Data = 256 - TEMP 'Acceleration data presented in two's complement
else
X_Data = TEMP
ENDIF
'---------------------------
I2CAddress_3D = $2B 'Read OUTY value
gosub I2C_Read_3D
if TEMP > 127 then 'Two's complementary changed to amplitude information
Y_Data = 256 - TEMP 'Acceleration data presented in two's complement
else
Y_Data = TEMP
ENDIF
'---------------------------
Acceleratio = Acceleratio + (18 * SQR((X_Data * X_Data) + (Y_Data * Y_Data))) 'Average factor 5x
'--------------------------- 'Calculate resultant force
IF COUNTER = 5 then
COUNTER = 0
Acceleratio = Acceleratio/5
IF Acceleratio > Acceleratio_max then
Acceleratio_max = Acceleratio
endif
'ADD YOUR CODE HERE TO SHOW ACCELERATIO AND ACCELERATIO_MAX ON YOUR DISPLAY
Acceleratio = 0
endif
'---------------------------
Gosub Accelerometer_Clear
endif
goto Acceleration_Loop
'-------------------------------------------------------------------------------
Accelerometer_Clear: 'Clear interrupt
I2CAddress_3D = $31
gosub I2C_Read_3D
Return

Originally Posted by
Glenn
Looks really good, if I knowed this before I started myself I probably would have ordered a I2C version instead
..But I might borrow some of your code anyway
I was thinking of using a timer and calculate speed from it, but I dont know how accurate it would be.
Thanks! Feel free to borrow, therefore I posted it here 
I would guess that digi output device would give overall better accuracy, but I might be wrong.
BR,
-Gusse-
Bookmarks