Your smart phone almost certainly uses an accelerometer to detect if you hold it horizontally of vertically.

They are used in step counters, e.g. in fitness watches.

They may be employed in hard disk, where they can detect free fall and block the mechanism, trying to avoid damage when the disk hits the ground.

a) The mma8452 ADC uses 12 bits. This means a resolution of 1/4096 full scale.

b) Let’s say our 12 bit number is 0x0b42. If we left align this number then the highest significant bit in the 12 bit number becomes bit no. 15. The original number is therefore shifted left by 4 bit and becomes: 0xb420

c) This one is simple: to encode 0 or 1 we need 1 bit, to encode the sign, we need another one. So… we need 2 bits for the integer part of the acceleration value: bit 15 (sign) and bit 14 (integer part).

If the dynamic range is set to 4g, then we need 3 bits for the integer part (sign + 2 bit) and bit 12 becomes the first real part bit. For 8g, the integer part needs 4 bits and the first real part bit is bit 11.

In 2g mode bit 13 will be the first bit of the real part which corresponds to 0.5 or 2^{-1},bit 12 corresponds to 2^{-2 }= 0.25 and so on. Since we used 2 bits for the integer part in 2g mode, there are 10 bits left and the least significant bit becomes 2^{-}^{10 }or 1/1024. The resolution in units of g is therefore 1/1024 g or approximately 1 ‰ of one g. In 4g mode 3 bits are used for the integer part and the resolution drops by a factor of 2 to 1/512 and it drops to 1/256 in 8g mode.

This is used in the conversion from raw values to values in units of g.

Of course it it possible to write the whole application in a single file which will also contain the definitions needed to access the mma8452 registers, bit definitions and so on. This is done in the solution to exercise 3.

First we open the connection to the pigpiod daemon, then we open the i2c bus. It would be better to check the WHO_AM_I register to verify that a mma8452 is actually connected to the i2c bus (which is not done in the solution).

Once we have access to i2c we switch the accelerometer to active mode. Then we check I new data have arrived in the STATUS register and we finally read the acceleration data and print them.

At the end we close the i2c bus and disconnect from pigpiod.

Please have a look at the code:

As I said in the above section on exercise 3 it is much cleaner to separate out all functions that access the mma8452 into a separate file (mma8452.c in the solution) and use the main program only to call these access functions. Also all definitions better go into an include file (mma8352.h).

The new functions are very simple but use basic bit handling functions

Here is the code:

Same as exercise 4. Simple functions using bit handling

Concerning the change in the raw value when the range changes see the explanations in exercise 6.

The code:

This one is a bit more tricky. In the solution I present a function that does the conversion for any dynamic range. The dynamic range currently set is passed as a parameter into the function. You can read it from the configuration register.

In all modes bit 15 is the sign bit. Then follow the bits defining the integer part which can be 1 bit (for 2g) 2 bits (for 4g) or 3 bits (for 8g). I get the sign and then calculate the absolute value of the acceleration measurement. After that I get the integer part. Finally in a loop I add the real part to it. If the bit following the integer part is set then 2^{-1} is added if the next bit is set I add 2^{-2 }and so on. I negate the final result again if the sign bit was set.

The other (and simpler) way to look at things is to say that in 2g mode the resolution (or the value of the lowest significant bit) is 1 mg (1/1000 g). Therefore the raw, integer value, shifted right be 4 bits to right-adjust the value, is given in mg and all you need to do is to divide the number by 1000. In 4g mode the least significant bit is 2 mg (4mg in 4g mode) and the final value must be multiplied by 2 (shifted left by 1 bit) or multiplied by 4 (shifted left by 2 bits) in 4g mode.

You can run the code on the RPi or on the PC: https://afnog.iotworkshop.africa/pub/UCC_Course_2018/SolutionsToTheExaminationExercises/convTest.tar.gz

In this archive you also find a little test program showing that right shifts are "*arithmetic shifts*" with sign extension which means that they work for positive** and** negative numbers.

I | Attachment | History | Action | Size | Date | Who | Comment |
---|---|---|---|---|---|---|---|

gz | convTest.tar.gz | r1 | manage | 1.6 K | 2018-12-19 - 09:42 | UliRaich | |

gz | exercise_3.tar.gz | r1 | manage | 1.7 K | 2018-12-19 - 09:42 | UliRaich | |

gz | exercise_4.tar.gz | r1 | manage | 2.0 K | 2018-12-19 - 09:42 | UliRaich | |

gz | exercise_5.tar.gz | r1 | manage | 2.3 K | 2018-12-19 - 09:42 | UliRaich |

Edit | Attach | ~~Watch~~ | Print version | History: r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions

Copyright © 2008-2019 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.

Ideas, requests, problems regarding TWiki? Send feedback

Ideas, requests, problems regarding TWiki? Send feedback