# How to calculate tilted information in LIS331DLH

Question asked by g.keerthi.001 on May 13, 2015
Hi,
I wish to calculate the tilted information using LIS331DLH accelerometer.
I get the code to calculate tilted information when HP filter is not enabled.(I have added in this post) .

when filter not enabled , they multiply z axis with 100000 and divide the result by 1024 and calculate the angle.

How can I calculate the tilted information when HP filter enabled?

`          ``/* `
`        ``**If condition is true, then acceleration is along negative x-axis.`
`        ``**Else it is along positive x-axis.`
`        ``*/`
`      ``if(x_axis & 0x0800)`
`         ``{`
`              ``x_axis = ~x_axis;`
`             ``x_axis =  x_axis + 1;`
`              ``x_axis =  x_axis & 0x0fff;`
`              ``x_direction = -1;`
`         ``}`
`         `
`         ``/* `
`         ``**If condition is true, then acceleration is along negative y-axis.`
`         ``**Else it is along positive y-axis.`
`         ``*/`
`      ``if(y_axis & 0x0800)`
`         ``{`
`              ``y_axis = ~y_axis;`
`              ``y_axis =  y_axis + 1;`
`              ``y_axis =  y_axis & 0x0fff;`
`              ``y_direction = -1;`
`         ``}`
`     `

`        ``/* `
`        ``**If condition is true, then acceleration is along negative z-axis.`
`         ``**Else it is along positive z-axis.`
`         ``*/`
`     ``if(z_axis & 0x0800)`
`         ``{`
`             ``z_axis = ~z_axis;`
`              ``z_axis = z_axis + 1;`
`              ``z_axis = z_axis & 0x0fff;`
`             ``z_direction = -1;`
`        ``} `
`       `
`               ``/* `
`        ``**cosine-inverse(z_axis/g) gives the degree by which`
`         ``**the device is tilted. 1g is eqvivalent to 1024 digital`
`         ``**value.`
`         ``*/`
`          ``z_axis = (z_axis * 100000) / 1024; `

`         ``/*`
`         ``**(z_axis/g) results in a fraction. The function 'BinarySearch()'`
`         ``**searches for the fraction in the cosine lookup table. The return`
`         ``**value is the index where fraction is found.This is nothing`
`         ``**but the degree by which device is tilted i.e (cosine-inverse(z_axis/g).`
`         ``*/`
`      `
`       ``Index = BinarySearch(z_axis, 90);`

`         ``if( Index != 0)`
`         ``{`
`   `
`              ``if( y_axis > x_axis)`
`             ``{`
`                  ``if(y_direction == -1)`
`                  ``{`
`                       ``y_direction = 1;`
`                       ``SB_HalUART_USCIA1_WriteString("  Tilted Left @ ");`
`                          `
`                  ``}`
`                  ``else`
`                  ``{`
`                         ``SB_HalUART_USCIA1_WriteString("Tilted Right @ ");`
`                            `
`                   ``}`
`             ``}`
`              ``else`
`              ``{`
`                   ``if(x_direction == -1)`
`                   ``{`
`                         ``SB_HalUART_USCIA1_WriteString("Tilted Backward @ ");`
`                            `
`                   ``}`
`                    ``else`
`                    ``{`
`                          ``SB_HalUART_USCIA1_WriteString("Tilted forward @ ");`
`                             `
`                   ``}`
`             ``}`
`       ``}`
`         `
`        ``if(((y_direction == 1) && (z_direction == -1)) || `
`          ``((y_direction == -1) && (z_direction == -1)))`
`        ``{`
`              ``Index = 180 - Index;`
`        ``}`
`        `
`        ``x_direction = 1;`
`        ``y_direction = 1;`
`       ``z_direction = 1; `

`        ``SB_HalUART_USCIA1_SendInteger(Index);`
`        ``SB_HalUART_USCIA1_WriteString("degree");`

`Binary Search function::::`

`  ``/*`
`**The function 'BinarySearch()' searches for the fraction in the cosine lookup table.`
`**The return value is the index where fraction is found.This is nothing`
`** but the degree by which device is tilted i.e (cosine-inverse(z_axis/g).`
`*/`
`static int32 BinarySearch(uint32 z, uint32 n)`
`{`
`    ``int32 low, high, mid = 0;`

`    ``low = 0;`
`    ``high = n - 1;`

`    ``while(low <= high)`
`    ``{`
`         ``mid = (low + high) / 2;`

`         ``if(z < ``cosine``[mid])`
`         ``{`
`              ``low` `= ``mid` `+ 1;`
`         ``}`
`         ``else if(z > cosine[mid])`
`         ``{`
`              ``high = mid - 1;`
`         ``}`
`         ``else`
`         ``{`
`              ``return mid;`
`         ``}`
`    ``}`

`    ``return mid;`
`}`

`Cosine lookup table:`

` ``/* This array is used as cosine lookup table.`
` ``** Each index of this array represnts the degree(0 to 90). The value at `
` ``** the corresponding index represents the cosine value of the degree.`
` ``*/`
`uint32 cosine [] = {10000, 99985, 99939, 99863, 99756, 99619, 99452, 99255,`
`                          ``99027, 98769, 98481, 98163, 97815, 97437, 97437,`
`                          ``96593, 96126, 95630, 95106, 94552, 93969, 93358,`
`                          ``92718, 92050, 91355, 90631, 89879, 89101, 88295,`
`                          ``87462, 85717, 84805, 83867, 82904, 81915, 80902,`
`                          ``79864, 78801, 77715, 76604, 75471, 74314, 73135,`
`                          ``71934, 69466, 68200, 66913, 65606, 64279, 62932,`
`                          ``61566, 60182, 58779, 57358, 55919, 54464, 52992,`
`                          ``51504, 48481, 46947, 45399, 43837, 42262, 40674,`
`                          ``39073, 37461, 35837, 34202, 32557, 30902, 29237,`
`                          ``27564, 25882, 24192, 22495, 20791, 19081, 17365,`
`                          ``15643, 13917, 12187, 10453, 8716,  6976,  5234,`
`                          ``3490,  1745, 0};`