Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Drones-Controlador controladoatitude_cteste Drone_Controlador_Atitude optical_test
LPS25H/LPS25H.cpp@4:d55264b2cad5, 2018-04-19 (annotated)
- Committer:
- fbob
- Date:
- Thu Apr 19 19:44:07 2018 +0000
- Revision:
- 4:d55264b2cad5
- Parent:
- 3:2f2b8e863991
Updated
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fbob | 3:2f2b8e863991 | 1 | #include "LPS25H.h" |
fbob | 3:2f2b8e863991 | 2 | |
fbob | 3:2f2b8e863991 | 3 | /** Class constructor */ |
fbob | 3:2f2b8e863991 | 4 | LPS25H::LPS25H(PinName sda, PinName scl) : i2c(sda, scl) |
fbob | 3:2f2b8e863991 | 5 | { |
fbob | 3:2f2b8e863991 | 6 | } |
fbob | 3:2f2b8e863991 | 7 | |
fbob | 4:d55264b2cad5 | 8 | /** Init barometer */ |
fbob | 4:d55264b2cad5 | 9 | void LPS25H::init() |
fbob | 4:d55264b2cad5 | 10 | { |
fbob | 4:d55264b2cad5 | 11 | init_baro(); |
fbob | 4:d55264b2cad5 | 12 | } |
fbob | 4:d55264b2cad5 | 13 | /** Read barometer */ |
fbob | 4:d55264b2cad5 | 14 | void LPS25H::read() |
fbob | 4:d55264b2cad5 | 15 | { |
fbob | 4:d55264b2cad5 | 16 | read_baro(); |
fbob | 4:d55264b2cad5 | 17 | } |
fbob | 4:d55264b2cad5 | 18 | |
fbob | 3:2f2b8e863991 | 19 | /** Initialize barometer */ |
fbob | 4:d55264b2cad5 | 20 | void LPS25H::init_baro() |
fbob | 3:2f2b8e863991 | 21 | { |
fbob | 3:2f2b8e863991 | 22 | // LPS25H I2C address |
fbob | 3:2f2b8e863991 | 23 | char address = LPS25H_ADDRESS; |
fbob | 3:2f2b8e863991 | 24 | // Register address and data that we're going to write |
fbob | 3:2f2b8e863991 | 25 | char reg_data[2] = {CTRL_REG1, 0b10110000}; |
fbob | 3:2f2b8e863991 | 26 | |
fbob | 3:2f2b8e863991 | 27 | // Point to register address and write data into this address |
fbob | 3:2f2b8e863991 | 28 | i2c.write(address, reg_data, 2); |
fbob | 3:2f2b8e863991 | 29 | } |
fbob | 3:2f2b8e863991 | 30 | |
fbob | 3:2f2b8e863991 | 31 | /** Read barometer */ |
fbob | 3:2f2b8e863991 | 32 | void LPS25H::read_baro() |
fbob | 3:2f2b8e863991 | 33 | { |
fbob | 3:2f2b8e863991 | 34 | // LPS25H I2C address |
fbob | 3:2f2b8e863991 | 35 | char address = LPS25H_ADDRESS; |
fbob | 3:2f2b8e863991 | 36 | // Register address |
fbob | 3:2f2b8e863991 | 37 | char reg[1] = {PRESS_OUT_XL | (1 << 7)}; |
fbob | 3:2f2b8e863991 | 38 | // Data that we're going to read |
fbob | 3:2f2b8e863991 | 39 | char data[3]; |
fbob | 3:2f2b8e863991 | 40 | |
fbob | 3:2f2b8e863991 | 41 | // Point to register address |
fbob | 3:2f2b8e863991 | 42 | i2c.write(address, reg, 1); |
fbob | 3:2f2b8e863991 | 43 | // Read data from this address (register address will auto-increment and all three axis information (two 8 bit data each) will be read) |
fbob | 3:2f2b8e863991 | 44 | i2c.read(address, data, 3); |
fbob | 3:2f2b8e863991 | 45 | |
fbob | 3:2f2b8e863991 | 46 | // Reassemble the data (three 8 bit data into one 24 bit data) |
fbob | 3:2f2b8e863991 | 47 | int32_t p_raw = (data[2] << 16 ) | (data[1] << 8 ) | data[0]; |
fbob | 3:2f2b8e863991 | 48 | // Convert to SI units |
fbob | 3:2f2b8e863991 | 49 | p = p_raw / 4096.0; |
fbob | 3:2f2b8e863991 | 50 | // |
fbob | 3:2f2b8e863991 | 51 | z = 44330.8f*(1.0f-pow((p/1025.0f),0.190263f)); |
fbob | 3:2f2b8e863991 | 52 | } |