Naresh Krish
/
itracker-mbed-os-example-lis2mdl
Sample program on how to use the LIS2MDL sensor on the RAKWirelss iTracker module
main.cpp@0:10c0e81df4ba, 2018-02-12 (annotated)
- Committer:
- knaresh89
- Date:
- Mon Feb 12 04:58:22 2018 +0000
- Revision:
- 0:10c0e81df4ba
Sample app for the LIS2MDL sensor on the RAKWireless iTracker module
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
knaresh89 | 0:10c0e81df4ba | 1 | #include "mbed.h" |
knaresh89 | 0:10c0e81df4ba | 2 | #include "LIS2MDL.h" |
knaresh89 | 0:10c0e81df4ba | 3 | |
knaresh89 | 0:10c0e81df4ba | 4 | #define LIS2MDL_intPin p16 // interrupt for magnetometer data ready |
knaresh89 | 0:10c0e81df4ba | 5 | |
knaresh89 | 0:10c0e81df4ba | 6 | uint8_t MODR = MODR_100Hz; |
knaresh89 | 0:10c0e81df4ba | 7 | int16_t temp[3] = {0, 0, 0}; |
knaresh89 | 0:10c0e81df4ba | 8 | float magBias[3] = {0,0,0}, magScale[3] = {0,0,0}; |
knaresh89 | 0:10c0e81df4ba | 9 | |
knaresh89 | 0:10c0e81df4ba | 10 | //Attach interrupt to this pin if you want to use magnetometer sensor interrupt |
knaresh89 | 0:10c0e81df4ba | 11 | // Before that make sure to set the correct interrupt value in the INT_CTRL_REG, INT_THS_L_REG and the INT_THS_H_REG (threshold value) |
knaresh89 | 0:10c0e81df4ba | 12 | DigitalIn interruptPin(LIS2MDL_intPin); |
knaresh89 | 0:10c0e81df4ba | 13 | |
knaresh89 | 0:10c0e81df4ba | 14 | // The itracker has the LIS2MDL sensor on the 13 and 11 I2C pins |
knaresh89 | 0:10c0e81df4ba | 15 | I2C i2c(p13,p11); |
knaresh89 | 0:10c0e81df4ba | 16 | |
knaresh89 | 0:10c0e81df4ba | 17 | // main() method. Runs in its own thread in the OS |
knaresh89 | 0:10c0e81df4ba | 18 | int main() |
knaresh89 | 0:10c0e81df4ba | 19 | { |
knaresh89 | 0:10c0e81df4ba | 20 | //Create LIS2MDL object |
knaresh89 | 0:10c0e81df4ba | 21 | LIS2MDL sensor(i2c, LIS2MDL_ADDRESS); |
knaresh89 | 0:10c0e81df4ba | 22 | |
knaresh89 | 0:10c0e81df4ba | 23 | //Reset the sensor to ensure correct starting config register values |
knaresh89 | 0:10c0e81df4ba | 24 | sensor.reset(); |
knaresh89 | 0:10c0e81df4ba | 25 | wait(3); |
knaresh89 | 0:10c0e81df4ba | 26 | |
knaresh89 | 0:10c0e81df4ba | 27 | //Intialise the CHIP with the MODR value chosen |
knaresh89 | 0:10c0e81df4ba | 28 | sensor.init(MODR); |
knaresh89 | 0:10c0e81df4ba | 29 | |
knaresh89 | 0:10c0e81df4ba | 30 | //Test the Chip ID. Should return 64 (0x40) |
knaresh89 | 0:10c0e81df4ba | 31 | sensor.getChipID(); |
knaresh89 | 0:10c0e81df4ba | 32 | |
knaresh89 | 0:10c0e81df4ba | 33 | // Calcaulte the offset bias to be used in future reading. See self check for example usage |
knaresh89 | 0:10c0e81df4ba | 34 | sensor.offsetBias(magBias, magScale); |
knaresh89 | 0:10c0e81df4ba | 35 | |
knaresh89 | 0:10c0e81df4ba | 36 | // Read the internal temp sensor |
knaresh89 | 0:10c0e81df4ba | 37 | sensor.readTemperature(); |
knaresh89 | 0:10c0e81df4ba | 38 | |
knaresh89 | 0:10c0e81df4ba | 39 | //Get readings from the sensor; |
knaresh89 | 0:10c0e81df4ba | 40 | for(int i=0; i<60; i++) { |
knaresh89 | 0:10c0e81df4ba | 41 | int16_t temp[3] = {0, 0, 0}; |
knaresh89 | 0:10c0e81df4ba | 42 | sensor.readData(temp); |
knaresh89 | 0:10c0e81df4ba | 43 | wait(0.1); |
knaresh89 | 0:10c0e81df4ba | 44 | } |
knaresh89 | 0:10c0e81df4ba | 45 | } |
knaresh89 | 0:10c0e81df4ba | 46 |