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.
LPS22HB.h
00001 /* 00002 * mbed library program 00003 * LPS22HB MEMS pressure sensor: 260-1260 hPa absolute digital output barometer 00004 * made by STMicroelectronics 00005 * http://www.st.com/ja/mems-and-sensors/lps22hb.html 00006 * 00007 * Modified for LPS22HB by feunoir 00008 * http://mbed.org/users/feunoir/ 00009 * 00010 * Copyright (c) 2015 Kenji Arai / JH1PJL 00011 * http://www.page.sannet.ne.jp/kenjia/index.html 00012 * http://mbed.org/users/kenjiArai/ 00013 * 00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 00015 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE 00016 * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00017 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00018 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00019 */ 00020 /* 00021 *---------------- REFERENCE ---------------------------------------------------------------------- 00022 * Original Information 00023 * http://www.st.com/ja/mems-and-sensors/lps22hb.html 00024 * AN4833 Application Note (as follows) 00025 * http://www.st.com/resource/en/application_note/dm00269729.pdf 00026 * Device kit 00027 * https://strawberry-linux.com/catalog/items?code=12122 00028 */ 00029 00030 #ifndef LPS22HB_H 00031 #define LPS22HB_H 00032 00033 #include "mbed.h" 00034 00035 // LPS22HB Address 00036 // 7bit address = 0b101110x(0x5c or 0x5d depends on SA0/SDO) 00037 #define LPS22HB_G_CHIP_ADDR (0x5c << 1) // SA0(=SDO pin) = Ground 00038 #define LPS22HB_V_CHIP_ADDR (0x5d << 1) // SA0(=SDO pin) = Vdd 00039 00040 // LPS22HB ID 00041 #define I_AM_LPS22HB 0xb1 00042 00043 // Register's definition 00044 #define LPS22HB_WHO_AM_I 0x0f 00045 #define LPS22HB_RES_CONF 0x1a 00046 00047 #define LPS22HB_CTRL_REG1 0x10 00048 #define LPS22HB_CTRL_REG2 0x11 00049 #define LPS22HB_CTRL_REG3 0x12 00050 00051 #define LPS22HB_STATUS_REG 0x27 00052 #define LPS22HB_PRESS_POUT_XL 0x28 00053 #define LPS22HB_PRESS_OUT_L 0x29 00054 #define LPS22HB_PRESS_OUT_H 0x2a 00055 #define LPS22HB_TEMP_OUT_L 0x2b 00056 #define LPS22HB_TEMP_OUT_H 0x2c 00057 00058 #define LPS22HB_FIFO_CTRL 0x14 00059 #define LPS22HB_FIFO_STATUS 0x26 00060 00061 00062 /** Interface for STMicronics MEMS pressure sensor 00063 * Chip: LPS22HB 00064 * 00065 * @code 00066 * #include "mbed.h" 00067 * #include "LPS22HB.h" 00068 * 00069 * // I2C Communication 00070 * LPS22HB baro(p_sda, p_scl, LPS22HB_G_CHIP_ADDR); 00071 * // If you connected I2C line not only this device but also other devices, 00072 * // you need to declare following method. 00073 * I2C i2c(dp5,dp27); // SDA, SCL 00074 * LPS22HB baro(i2c, LPS22HB_G_CHIP_ADDR); 00075 * 00076 * int main() { 00077 * while( trure){ 00078 * baro.get(); 00079 * printf("Presere: 0x%6.1f, Temperature: 0x%+4.1f\r\n", baro.pressue(), baro.temperature()); 00080 * wait(1.0); 00081 * } 00082 * } 00083 * @endcode 00084 */ 00085 00086 class LPS22HB 00087 { 00088 public: 00089 00090 enum lps22hb_odr { 00091 LPS22HB_PD = 0x00, 00092 LPS22HB_ODR_1HZ = 0x10, 00093 LPS22HB_ODR_10HZ = 0x20, 00094 LPS22HB_ODR_25HZ = 0x30, 00095 LPS22HB_ODR_50HZ = 0x40, 00096 LPS22HB_ODR_75HZ = 0x50 00097 }; 00098 00099 enum lps22hb_lpf { 00100 LPS22HB_LPF_DISABLE = 0x00, 00101 LPS22HB_LPF_BW9 = 0x08, 00102 LPS22HB_LPF_BW20 = 0x0c 00103 }; 00104 00105 enum lps22hb_drdy { 00106 LPS22HB_DRDY_DISABLE = 0x00, 00107 LPS22HB_DRDY_ENABLE = 0x04 00108 }; 00109 00110 /** Configure data pin 00111 * @param data SDA and SCL pins 00112 * @param device address LPS22HB(SA0=0 or 1), LPS22HB_G_CHIP_ADDR or LPS22HB_V_CHIP_ADDR 00113 * @param Operation mode FIFO_HW_FILTER(default) or FIFO_BYPASS (Option parameter) 00114 */ 00115 LPS22HB(PinName p_sda, PinName p_scl, uint8_t addr); 00116 00117 /** Configure data pin (with other devices on I2C line) 00118 * @param I2C previous definition 00119 * @param device address LPS22HB(SA0=0 or 1), LPS22HB_G_CHIP_ADDR or LPS22HB_V_CHIP_ADDR 00120 * @param Operation mode FIFO_HW_FILTER(default) or FIFO_BYPASS (Option parameter) 00121 */ 00122 LPS22HB(I2C& p_i2c, uint8_t addr); 00123 00124 ~LPS22HB(); 00125 00126 /** Start convertion & data save 00127 * @param none 00128 * @return none 00129 */ 00130 void get(void); 00131 00132 /** Read pressure data 00133 * @param none 00134 * @return pressure 00135 */ 00136 float pressure(void); 00137 00138 /** Read raw pressure data 00139 * @param none 00140 * @return raw pressure 00141 */ 00142 uint32_t pressure_raw(void); // add by user 00143 00144 /** Read temperature data 00145 * @param none 00146 * @return temperature 00147 */ 00148 float temperature(void); 00149 00150 /** Read raw temperature data 00151 * @param none 00152 * @return raw temperature 00153 */ 00154 int16_t temperature_raw(void); // add by user 00155 00156 /** Read a ID number 00157 * @param none 00158 * @return if STM MEMS LPS22HB, it should be I_AM_ LPS22HB 00159 */ 00160 uint8_t read_id(void); 00161 00162 /** Read Data Ready flag 00163 * @param none 00164 * @return 1 = Ready 00165 */ 00166 uint8_t data_ready(void); 00167 00168 /** Set I2C clock frequency 00169 * @param freq. 00170 * @return none 00171 */ 00172 void frequency(int hz); 00173 00174 /** Read register (general purpose) 00175 * @param register's address 00176 * @return register data 00177 */ 00178 uint8_t read_reg(uint8_t addr); 00179 00180 /** Write register (general purpose) 00181 * @param register's address 00182 * @param data 00183 * @return none 00184 */ 00185 void write_reg(uint8_t addr, uint8_t data); 00186 00187 /** Set output data rate 00188 * @param data rate config 00189 * @return none 00190 */ 00191 void set_odr(lps22hb_odr odrcfg = LPS22HB_ODR_75HZ); //add by user 00192 00193 /** LPF 00194 * @param LPF config 00195 * @return none 00196 */ 00197 void set_lpf(lps22hb_lpf lpfcfg = LPS22HB_LPF_DISABLE); //add by user 00198 00199 /** DRDY 00200 * @param DRDY config 00201 * @return none 00202 */ 00203 void drdy(lps22hb_drdy drdycfg = LPS22HB_DRDY_DISABLE); //add by user 00204 00205 protected: 00206 I2C* i2c_p; 00207 I2C& _i2c; 00208 00209 void init(void); 00210 00211 private: 00212 char dt[6]; // working buffer 00213 uint8_t LPS22HB_addr; // Sensor address 00214 uint8_t LPS22HB_id; // ID 00215 uint8_t LPS22HB_ready; // Device is on I2C line = 1, not = 0 00216 uint8_t LPS22HB_mode; // Operation mode 00217 uint32_t press; // pressure raw data 00218 int16_t temp; // temperature raw data 00219 }; 00220 00221 #endif // LPS22HB_H
Generated on Sat Jul 16 2022 04:09:25 by
