Thanks and here is the current temporary code. The plan is to take Read_Tiltx and Read_tilty and make it one subroutine for both the X and Y to conserve on the amount of code. I put a indicator (0 and 1) to show + and - angle for X and port and starboard for the Y. Best, Ed
'************************************************* ***************
'* Name : X-Y Tilt.BAS *
'* Author : Ed Cannady *
'* Notice : Copyright (c) 2012 Copyright (c) 2012 *
'* : All Rights Reserved *
'* Date : 6/17/2012 *
'* Version : 1.0 *
'* Notes : 16F628A and Memsic 2125 *
'* : *
'************************************************* ***************
include "Modedefs.bas"
' ** Set Xtal Value in mHz **
define OSC 20 ' Set Xtal Frequency
#CONFIG
__config _HS_OSC & _WDT_ON & _MCLRE_ON & _LVP_OFF & _CP_OFF
#endconfig
' ** Declare Pins Used **
CMCON= 7
input PortA.0 ' X Data is clocked on rising edge of this pin
input PortA.2 ' Y Data is clocked on rising edge of this pin
' ** Variables **
xRaw var word 'High Pulse width from Memsic 2125
xGForce var word ' x axis g force (1000ths)
xTilt var word ' x axis tilt (100ths)
xidx var byte 'Table index
xmult var word 'Multiplier - whole part
xfrac var word 'Multiplier - fractional part
xPosNeg var byte 'Positive angle or Negative angle flag
yRaw var word 'High Pulse width from Memsic 2125
yGforce var word ' x axis g force (1000ths)
yTilt var word ' x axis tilt (100ths)
yidx var byte 'Table index
ymult var word 'Multiplier - whole part
yfrac var word 'Multiplier - fractional part
yPosNeg var byte 'Positive angle or Negative angle flag
Read_Force:
pulsin PORTA.0, 1, xRaw
pulsin PORTA.2, 1, yRaw
xRaw = xRaw * 20
xGForce = (xRaw / 99 - 500) * 8 'xGForce = ((xRaw / 99) - 502) * 8 ' g = ((t1 / 0.01) - 0.5) / 12.5%
yRaw = yRaw * 20
yGforce = (yRaw / 99 - 487) * 8 'yGforce = ((xRaw / 99) - 502) * 8 ' g = ((t1 / 0.01) - 0.5) / 12.5%
Read_Tiltx: 'tilt = g x k Select tilt conversion factor based on static G force. Table data derived from Memsic specs.
lookdown2 abs xGForce, <=[174, 344, 508, 661, 2000], xidx ' Table is in thousandths g results for 10,20,30,40 and max value degrees
lookup xidx, [57, 58, 59, 60, 62], xmult 'integer lookup
lookup2 xidx, [32768, 10486, 2621, 30802, 22938], xfrac 'gets fractional result
xTilt = xmult * (abs xGForce / 10) + (xfrac ** (abs xGForce / 10))
Read_Tilty: 'tilt = g x k Select tilt conversion factor based on static G force. Table data derived from Memsic specs.
lookdown2 abs yGforce, <=[174, 344, 508, 661, 2000], yidx ' Table is in thousandths g results for 10,20,30,40 and max value degrees
lookup yidx, [57, 58, 59, 60, 62], ymult 'integer lookup
lookup2 yidx, [32768, 10486, 2621, 30802, 22938], yfrac 'gets fractional result
yTilt = ymult * (abs yGforce / 10) + (yfrac ** (abs yGforce / 10))
Check_SignX: 'Check the sign of X angle
if (xGForce.bit15 = 0) then XT_Exit ' if positive, skip correct for g force sign
xPosNeg = 0 ' If xPosNeg = 0 then the angle is negative (-)
gosub Check_SignY
XT_Exit:
xPosNeg = 1 ' If xPosNeg = 1 then the angle is positive (+)
Check_SignY: 'Check the sign of Y angle
if (yGforce.bit15 = 0) then YT_Exit ' if positive, skip correct for g force sign
yPosNeg = 0 ' If yPosNeg = 0 then the angle is negative (-)
gosub Read_Force
YT_Exit:
yPosNeg = 1 ' If yPosNeg = 1 then the angle is positive (+)
gosub Read_Force




Bookmarks