Interface library for ST LSM303DLM 3-axis magnetometer/accelerometer
Dependents: AVC_2012 m3pi_Kompass Fish_2014Fall Fish_2014Fall ... more
LSM303DLM.cpp@0:faef9e4c8bea, 2012-01-20 (annotated)
- Committer:
- shimniok
- Date:
- Fri Jan 20 23:47:25 2012 +0000
- Revision:
- 0:faef9e4c8bea
- Child:
- 1:fc5c9258ec45
Initial version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shimniok | 0:faef9e4c8bea | 1 | #include "mbed.h" |
shimniok | 0:faef9e4c8bea | 2 | #include "LSM303DLM.h" |
shimniok | 0:faef9e4c8bea | 3 | #include "stdio.h" |
shimniok | 0:faef9e4c8bea | 4 | |
shimniok | 0:faef9e4c8bea | 5 | #define MAG_ADDRESS 0x3C |
shimniok | 0:faef9e4c8bea | 6 | #define ACC_ADDRESS 0x30 |
shimniok | 0:faef9e4c8bea | 7 | |
shimniok | 0:faef9e4c8bea | 8 | LSM303DLM::LSM303DLM(PinName sda, PinName scl): _device(sda, scl) |
shimniok | 0:faef9e4c8bea | 9 | { |
shimniok | 0:faef9e4c8bea | 10 | _device.frequency(400000); |
shimniok | 0:faef9e4c8bea | 11 | init(); |
shimniok | 0:faef9e4c8bea | 12 | } |
shimniok | 0:faef9e4c8bea | 13 | |
shimniok | 0:faef9e4c8bea | 14 | void LSM303DLM::init() |
shimniok | 0:faef9e4c8bea | 15 | { |
shimniok | 0:faef9e4c8bea | 16 | // init mag |
shimniok | 0:faef9e4c8bea | 17 | // continuous conversion mode |
shimniok | 0:faef9e4c8bea | 18 | _data[0] = MR_REG_M; |
shimniok | 0:faef9e4c8bea | 19 | _data[1] = 0x00; |
shimniok | 0:faef9e4c8bea | 20 | if (!_device.write(MAG_ADDRESS, _data, 2)) |
shimniok | 0:faef9e4c8bea | 21 | fprintf(stdout, "MR_REG_M: no ack\n"); |
shimniok | 0:faef9e4c8bea | 22 | // data rate 75hz |
shimniok | 0:faef9e4c8bea | 23 | _data[0] = CRA_REG_M; |
shimniok | 0:faef9e4c8bea | 24 | _data[1] = 0x18; // 0b00011000 |
shimniok | 0:faef9e4c8bea | 25 | if (_device.write(MAG_ADDRESS, _data, 2)) |
shimniok | 0:faef9e4c8bea | 26 | fprintf(stdout, "CRA_REG_M: no ack\n"); |
shimniok | 0:faef9e4c8bea | 27 | |
shimniok | 0:faef9e4c8bea | 28 | // init acc |
shimniok | 0:faef9e4c8bea | 29 | // data rate 100hz |
shimniok | 0:faef9e4c8bea | 30 | _data[0] = CTRL_REG1_A; |
shimniok | 0:faef9e4c8bea | 31 | _data[1] = 0x2F; // 0b00101111 |
shimniok | 0:faef9e4c8bea | 32 | if (!_device.write(ACC_ADDRESS, _data, 2)) |
shimniok | 0:faef9e4c8bea | 33 | fprintf(stdout, "CTRL_REG1_A: no ack\n"); |
shimniok | 0:faef9e4c8bea | 34 | } |
shimniok | 0:faef9e4c8bea | 35 | |
shimniok | 0:faef9e4c8bea | 36 | void LSM303DLM::read(int a[3], int m[3]) |
shimniok | 0:faef9e4c8bea | 37 | { |
shimniok | 0:faef9e4c8bea | 38 | readAcc(a); |
shimniok | 0:faef9e4c8bea | 39 | readMag(m); |
shimniok | 0:faef9e4c8bea | 40 | } |
shimniok | 0:faef9e4c8bea | 41 | |
shimniok | 0:faef9e4c8bea | 42 | void LSM303DLM::readAcc(int a[3]) |
shimniok | 0:faef9e4c8bea | 43 | { |
shimniok | 0:faef9e4c8bea | 44 | _data[0] = OUT_X_L_A | (1<<7); |
shimniok | 0:faef9e4c8bea | 45 | _device.write(ACC_ADDRESS, _data, 1); |
shimniok | 0:faef9e4c8bea | 46 | _device.read(ACC_ADDRESS, _data, 6); |
shimniok | 0:faef9e4c8bea | 47 | |
shimniok | 0:faef9e4c8bea | 48 | // 12-bit values |
shimniok | 0:faef9e4c8bea | 49 | a[0] = (short) (_data[0] | _data[1]<<8)>>4; |
shimniok | 0:faef9e4c8bea | 50 | a[1] = (short) (_data[2] | _data[3]<<8)>>4; |
shimniok | 0:faef9e4c8bea | 51 | a[2] = (short) (_data[4] | _data[5]<<8)>>4; |
shimniok | 0:faef9e4c8bea | 52 | } |
shimniok | 0:faef9e4c8bea | 53 | |
shimniok | 0:faef9e4c8bea | 54 | void LSM303DLM::readMag(int m[3]) |
shimniok | 0:faef9e4c8bea | 55 | { |
shimniok | 0:faef9e4c8bea | 56 | _data[0] = OUT_X_H_M; |
shimniok | 0:faef9e4c8bea | 57 | _device.write(MAG_ADDRESS, _data, 1); |
shimniok | 0:faef9e4c8bea | 58 | _device.read(MAG_ADDRESS, _data, 6); |
shimniok | 0:faef9e4c8bea | 59 | |
shimniok | 0:faef9e4c8bea | 60 | m[0] = (short) _data[0]<<8 | _data[1]; // X |
shimniok | 0:faef9e4c8bea | 61 | m[1] = (short) _data[4]<<8 | _data[5]; // Y |
shimniok | 0:faef9e4c8bea | 62 | m[2] = (short) _data[2]<<8 | _data[3]; // Z |
shimniok | 0:faef9e4c8bea | 63 | } |