Carter John
/
LSM303DLHC_magnetometer_example
An example on how to read raw data
main.cpp@0:fb4503358bc7, 2017-07-20 (annotated)
- Committer:
- ElectronicsSanta
- Date:
- Thu Jul 20 08:41:07 2017 +0000
- Revision:
- 0:fb4503358bc7
first commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ElectronicsSanta | 0:fb4503358bc7 | 1 | #include "mbed.h" |
ElectronicsSanta | 0:fb4503358bc7 | 2 | #include <algorithm> // for min, max functions |
ElectronicsSanta | 0:fb4503358bc7 | 3 | |
ElectronicsSanta | 0:fb4503358bc7 | 4 | #define MAGN_I2C_ADDRESS (0x1E << 1) |
ElectronicsSanta | 0:fb4503358bc7 | 5 | |
ElectronicsSanta | 0:fb4503358bc7 | 6 | #define LSM303_REGISTER_MAG_OUT_X_H_M 0x03 |
ElectronicsSanta | 0:fb4503358bc7 | 7 | #define LSM303_REGISTER_MAG_OUT_X_L_M 0x04 |
ElectronicsSanta | 0:fb4503358bc7 | 8 | #define LSM303_REGISTER_MAG_OUT_Z_H_M 0x05 |
ElectronicsSanta | 0:fb4503358bc7 | 9 | #define LSM303_REGISTER_MAG_OUT_Z_L_M 0x06 |
ElectronicsSanta | 0:fb4503358bc7 | 10 | #define LSM303_REGISTER_MAG_OUT_Y_H_M 0x07 |
ElectronicsSanta | 0:fb4503358bc7 | 11 | #define LSM303_REGISTER_MAG_OUT_Y_L_M 0x08 |
ElectronicsSanta | 0:fb4503358bc7 | 12 | |
ElectronicsSanta | 0:fb4503358bc7 | 13 | #define LSM303_REGISTER_MAG_CRA_REG_M 0x00 |
ElectronicsSanta | 0:fb4503358bc7 | 14 | #define LSM303_REGISTER_MAG_CRB_REG_M 0x01 |
ElectronicsSanta | 0:fb4503358bc7 | 15 | #define LSM303_REGISTER_MAG_MR_REG_M 0x02 |
ElectronicsSanta | 0:fb4503358bc7 | 16 | #define LSM303_REGISTER_MAG_OUT_X_H_M 0x03 |
ElectronicsSanta | 0:fb4503358bc7 | 17 | #define LSM303_REGISTER_MAG_OUT_X_L_M 0x04 |
ElectronicsSanta | 0:fb4503358bc7 | 18 | #define LSM303_REGISTER_MAG_OUT_Z_H_M 0x05 |
ElectronicsSanta | 0:fb4503358bc7 | 19 | #define LSM303_REGISTER_MAG_OUT_Z_L_M 0x06 |
ElectronicsSanta | 0:fb4503358bc7 | 20 | #define LSM303_REGISTER_MAG_OUT_Y_H_M 0x07 |
ElectronicsSanta | 0:fb4503358bc7 | 21 | #define LSM303_REGISTER_MAG_OUT_Y_L_M 0x08 |
ElectronicsSanta | 0:fb4503358bc7 | 22 | #define LSM303_REGISTER_MAG_SR_REG_Mg 0x09 |
ElectronicsSanta | 0:fb4503358bc7 | 23 | #define LSM303_REGISTER_MAG_IRA_REG_M 0x0A |
ElectronicsSanta | 0:fb4503358bc7 | 24 | #define LSM303_REGISTER_MAG_IRB_REG_M 0x0B |
ElectronicsSanta | 0:fb4503358bc7 | 25 | #define LSM303_REGISTER_MAG_IRC_REG_M 0x0C |
ElectronicsSanta | 0:fb4503358bc7 | 26 | #define LSM303_REGISTER_MAG_TEMP_OUT_H_M 0x31 |
ElectronicsSanta | 0:fb4503358bc7 | 27 | #define LSM303_REGISTER_MAG_TEMP_OUT_L_M 0x32 |
ElectronicsSanta | 0:fb4503358bc7 | 28 | |
ElectronicsSanta | 0:fb4503358bc7 | 29 | // MD1 MD0 |
ElectronicsSanta | 0:fb4503358bc7 | 30 | #define LSM303_REGISTER_MAG_MR_REG_CCM 0x00 // 0 0 = continuous-conversion mode |
ElectronicsSanta | 0:fb4503358bc7 | 31 | #define LSM303_REGISTER_MAG_MR_REG_SCM 0x01 // 0 1 = single-conversion mode |
ElectronicsSanta | 0:fb4503358bc7 | 32 | #define LSM303_REGISTER_MAG_MR_REG_SM 0x02 // 0 1 = sleep-mode. device is placed in sleep-mode |
ElectronicsSanta | 0:fb4503358bc7 | 33 | |
ElectronicsSanta | 0:fb4503358bc7 | 34 | #define LSM303_REGISTER_MAG_CRA_REG_M_0_75HZ (0 << 2) // 0.75 Hz |
ElectronicsSanta | 0:fb4503358bc7 | 35 | #define LSM303_REGISTER_MAG_CRA_REG_M_1_5HZ (1 << 2) // 1.5 Hz |
ElectronicsSanta | 0:fb4503358bc7 | 36 | #define LSM303_REGISTER_MAG_CRA_REG_M_3_0HZ (2 << 2) // 3.0 Hz |
ElectronicsSanta | 0:fb4503358bc7 | 37 | #define LSM303_REGISTER_MAG_CRA_REG_M_7_5HZ (3 << 2) // 7.5 Hz |
ElectronicsSanta | 0:fb4503358bc7 | 38 | #define LSM303_REGISTER_MAG_CRA_REG_M_15HZ (4 << 2) // 15 Hz |
ElectronicsSanta | 0:fb4503358bc7 | 39 | #define LSM303_REGISTER_MAG_CRA_REG_M_30HZ (5 << 2) // 30 Hz |
ElectronicsSanta | 0:fb4503358bc7 | 40 | #define LSM303_REGISTER_MAG_CRA_REG_M_75HZ (6 << 2) // 75 Hz |
ElectronicsSanta | 0:fb4503358bc7 | 41 | #define LSM303_REGISTER_MAG_CRA_REG_M_220HZ (7 << 2) // 220 Hz |
ElectronicsSanta | 0:fb4503358bc7 | 42 | |
ElectronicsSanta | 0:fb4503358bc7 | 43 | #define LSM303_MULTIPLE_REGISTER_READS (1 << 7) // MSB set to 1 in the command is telling the LSM303 to increment the register pointer after each read/write (https://www.pololu.com/product/1268) |
ElectronicsSanta | 0:fb4503358bc7 | 44 | |
ElectronicsSanta | 0:fb4503358bc7 | 45 | Serial serial(SERIAL_TX, SERIAL_RX); |
ElectronicsSanta | 0:fb4503358bc7 | 46 | I2C i2c(I2C_SDA, I2C_SCL); // PB_9, PB_8 |
ElectronicsSanta | 0:fb4503358bc7 | 47 | Timer elapsed_time; |
ElectronicsSanta | 0:fb4503358bc7 | 48 | |
ElectronicsSanta | 0:fb4503358bc7 | 49 | int16_t x; |
ElectronicsSanta | 0:fb4503358bc7 | 50 | int16_t y; |
ElectronicsSanta | 0:fb4503358bc7 | 51 | int16_t z; |
ElectronicsSanta | 0:fb4503358bc7 | 52 | bool magn_found = false; |
ElectronicsSanta | 0:fb4503358bc7 | 53 | |
ElectronicsSanta | 0:fb4503358bc7 | 54 | uint8_t i2c_read8_reg(uint8_t address, int8_t reg) { |
ElectronicsSanta | 0:fb4503358bc7 | 55 | char i2cBuffer[1]; |
ElectronicsSanta | 0:fb4503358bc7 | 56 | i2cBuffer[0] = reg; |
ElectronicsSanta | 0:fb4503358bc7 | 57 | i2c.write(address, i2cBuffer, 1); |
ElectronicsSanta | 0:fb4503358bc7 | 58 | |
ElectronicsSanta | 0:fb4503358bc7 | 59 | i2c.read(address, i2cBuffer, 1); |
ElectronicsSanta | 0:fb4503358bc7 | 60 | |
ElectronicsSanta | 0:fb4503358bc7 | 61 | return (uint8_t)i2cBuffer[0]; |
ElectronicsSanta | 0:fb4503358bc7 | 62 | } |
ElectronicsSanta | 0:fb4503358bc7 | 63 | |
ElectronicsSanta | 0:fb4503358bc7 | 64 | void i2c_write8_reg(uint8_t address, int8_t reg, int8_t value) { |
ElectronicsSanta | 0:fb4503358bc7 | 65 | char i2cBuffer[2]; |
ElectronicsSanta | 0:fb4503358bc7 | 66 | i2cBuffer[0] = reg; |
ElectronicsSanta | 0:fb4503358bc7 | 67 | i2cBuffer[1] = value; |
ElectronicsSanta | 0:fb4503358bc7 | 68 | i2c.write(address, i2cBuffer, 2); |
ElectronicsSanta | 0:fb4503358bc7 | 69 | } |
ElectronicsSanta | 0:fb4503358bc7 | 70 | |
ElectronicsSanta | 0:fb4503358bc7 | 71 | void read_magn() { |
ElectronicsSanta | 0:fb4503358bc7 | 72 | char i2cBuffer[1]; |
ElectronicsSanta | 0:fb4503358bc7 | 73 | |
ElectronicsSanta | 0:fb4503358bc7 | 74 | i2cBuffer[0] = LSM303_REGISTER_MAG_OUT_X_H_M; |
ElectronicsSanta | 0:fb4503358bc7 | 75 | i2c.write(MAGN_I2C_ADDRESS, i2cBuffer, 1); |
ElectronicsSanta | 0:fb4503358bc7 | 76 | i2c.read(MAGN_I2C_ADDRESS, i2cBuffer, 1); |
ElectronicsSanta | 0:fb4503358bc7 | 77 | x = (i2cBuffer[0] << 8); |
ElectronicsSanta | 0:fb4503358bc7 | 78 | |
ElectronicsSanta | 0:fb4503358bc7 | 79 | i2cBuffer[0] = LSM303_REGISTER_MAG_OUT_X_L_M; |
ElectronicsSanta | 0:fb4503358bc7 | 80 | i2c.write(MAGN_I2C_ADDRESS, i2cBuffer, 1); |
ElectronicsSanta | 0:fb4503358bc7 | 81 | i2c.read(MAGN_I2C_ADDRESS, i2cBuffer, 1); |
ElectronicsSanta | 0:fb4503358bc7 | 82 | x |= i2cBuffer[0]; |
ElectronicsSanta | 0:fb4503358bc7 | 83 | |
ElectronicsSanta | 0:fb4503358bc7 | 84 | i2cBuffer[0] = LSM303_REGISTER_MAG_OUT_Y_H_M; |
ElectronicsSanta | 0:fb4503358bc7 | 85 | i2c.write(MAGN_I2C_ADDRESS, i2cBuffer, 1); |
ElectronicsSanta | 0:fb4503358bc7 | 86 | i2c.read(MAGN_I2C_ADDRESS, i2cBuffer, 1); |
ElectronicsSanta | 0:fb4503358bc7 | 87 | y = (i2cBuffer[0] << 8); |
ElectronicsSanta | 0:fb4503358bc7 | 88 | |
ElectronicsSanta | 0:fb4503358bc7 | 89 | i2cBuffer[0] = LSM303_REGISTER_MAG_OUT_Y_L_M; |
ElectronicsSanta | 0:fb4503358bc7 | 90 | i2c.write(MAGN_I2C_ADDRESS, i2cBuffer, 1); |
ElectronicsSanta | 0:fb4503358bc7 | 91 | i2c.read(MAGN_I2C_ADDRESS, i2cBuffer, 1); |
ElectronicsSanta | 0:fb4503358bc7 | 92 | y |= i2cBuffer[0]; |
ElectronicsSanta | 0:fb4503358bc7 | 93 | |
ElectronicsSanta | 0:fb4503358bc7 | 94 | i2cBuffer[0] = LSM303_REGISTER_MAG_OUT_Z_H_M; |
ElectronicsSanta | 0:fb4503358bc7 | 95 | i2c.write(MAGN_I2C_ADDRESS, i2cBuffer, 1); |
ElectronicsSanta | 0:fb4503358bc7 | 96 | i2c.read(MAGN_I2C_ADDRESS, i2cBuffer, 1); |
ElectronicsSanta | 0:fb4503358bc7 | 97 | z = (i2cBuffer[0] << 8); |
ElectronicsSanta | 0:fb4503358bc7 | 98 | |
ElectronicsSanta | 0:fb4503358bc7 | 99 | i2cBuffer[0] = LSM303_REGISTER_MAG_OUT_Z_L_M; |
ElectronicsSanta | 0:fb4503358bc7 | 100 | i2c.write(MAGN_I2C_ADDRESS, i2cBuffer, 1); |
ElectronicsSanta | 0:fb4503358bc7 | 101 | i2c.read(MAGN_I2C_ADDRESS, i2cBuffer, 1); |
ElectronicsSanta | 0:fb4503358bc7 | 102 | z |= i2cBuffer[0]; |
ElectronicsSanta | 0:fb4503358bc7 | 103 | } |
ElectronicsSanta | 0:fb4503358bc7 | 104 | |
ElectronicsSanta | 0:fb4503358bc7 | 105 | int main() { |
ElectronicsSanta | 0:fb4503358bc7 | 106 | serial.baud(115200); |
ElectronicsSanta | 0:fb4503358bc7 | 107 | i2c.frequency((uint32_t)100e3); |
ElectronicsSanta | 0:fb4503358bc7 | 108 | |
ElectronicsSanta | 0:fb4503358bc7 | 109 | serial.printf("LSM303DLHC magnetometer example...\n"); |
ElectronicsSanta | 0:fb4503358bc7 | 110 | |
ElectronicsSanta | 0:fb4503358bc7 | 111 | // set the magnetometer data output rate (Hz) |
ElectronicsSanta | 0:fb4503358bc7 | 112 | i2c_write8_reg(MAGN_I2C_ADDRESS, LSM303_REGISTER_MAG_CRA_REG_M, LSM303_REGISTER_MAG_CRA_REG_M_3_0HZ); |
ElectronicsSanta | 0:fb4503358bc7 | 113 | |
ElectronicsSanta | 0:fb4503358bc7 | 114 | // read the magnetometer register for the value we set above |
ElectronicsSanta | 0:fb4503358bc7 | 115 | uint8_t default_val = i2c_read8_reg(MAGN_I2C_ADDRESS, LSM303_REGISTER_MAG_CRA_REG_M); |
ElectronicsSanta | 0:fb4503358bc7 | 116 | |
ElectronicsSanta | 0:fb4503358bc7 | 117 | if (default_val == LSM303_REGISTER_MAG_CRA_REG_M_3_0HZ) { |
ElectronicsSanta | 0:fb4503358bc7 | 118 | magn_found = true; |
ElectronicsSanta | 0:fb4503358bc7 | 119 | serial.printf("magn found\n"); |
ElectronicsSanta | 0:fb4503358bc7 | 120 | } else { |
ElectronicsSanta | 0:fb4503358bc7 | 121 | serial.printf("magn NOT found\n"); |
ElectronicsSanta | 0:fb4503358bc7 | 122 | } |
ElectronicsSanta | 0:fb4503358bc7 | 123 | |
ElectronicsSanta | 0:fb4503358bc7 | 124 | if (magn_found) { |
ElectronicsSanta | 0:fb4503358bc7 | 125 | // enable the magnetometer |
ElectronicsSanta | 0:fb4503358bc7 | 126 | i2c_write8_reg(MAGN_I2C_ADDRESS, LSM303_REGISTER_MAG_MR_REG_M, LSM303_REGISTER_MAG_MR_REG_CCM); |
ElectronicsSanta | 0:fb4503358bc7 | 127 | |
ElectronicsSanta | 0:fb4503358bc7 | 128 | elapsed_time.start(); |
ElectronicsSanta | 0:fb4503358bc7 | 129 | |
ElectronicsSanta | 0:fb4503358bc7 | 130 | while (true) { |
ElectronicsSanta | 0:fb4503358bc7 | 131 | if (elapsed_time.read_ms() > 200) { // print every x ms |
ElectronicsSanta | 0:fb4503358bc7 | 132 | read_magn(); |
ElectronicsSanta | 0:fb4503358bc7 | 133 | elapsed_time.reset(); |
ElectronicsSanta | 0:fb4503358bc7 | 134 | serial.printf("x: %d; y: %d; z: %d\n", x, y, z); |
ElectronicsSanta | 0:fb4503358bc7 | 135 | serial.printf("**************************\n"); |
ElectronicsSanta | 0:fb4503358bc7 | 136 | } |
ElectronicsSanta | 0:fb4503358bc7 | 137 | } |
ElectronicsSanta | 0:fb4503358bc7 | 138 | } |
ElectronicsSanta | 0:fb4503358bc7 | 139 | } |