気圧センサLPS22HBのライブラリ
Dependents: Hybrid_IZU2021_MAIN_OS5 Hybrid_IZU2021_MISSION_v2 Hybrid_IZU2021_MAIN Hybrid_IZU2021_MISSION
PQ_LPS22HB.cpp@1:fdde28032fb5, 2021-03-08 (annotated)
- Committer:
- tanahashi
- Date:
- Mon Mar 08 16:48:54 2021 +0000
- Revision:
- 1:fdde28032fb5
- Parent:
- 0:9a7d5d7e63be
fixed LSB
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tanahashi | 0:9a7d5d7e63be | 1 | #include "mbed.h" |
tanahashi | 0:9a7d5d7e63be | 2 | #include "PQ_LPS22HB.h" |
tanahashi | 0:9a7d5d7e63be | 3 | |
tanahashi | 0:9a7d5d7e63be | 4 | LPS22HB :: LPS22HB(I2C &i2c, SA0_t SA0) |
tanahashi | 0:9a7d5d7e63be | 5 | { |
tanahashi | 0:9a7d5d7e63be | 6 | _addr = SA0; |
tanahashi | 0:9a7d5d7e63be | 7 | _i2c = &i2c; |
tanahashi | 0:9a7d5d7e63be | 8 | _i2c -> frequency(400000); |
tanahashi | 0:9a7d5d7e63be | 9 | } |
tanahashi | 0:9a7d5d7e63be | 10 | |
tanahashi | 0:9a7d5d7e63be | 11 | void LPS22HB :: begin() |
tanahashi | 0:9a7d5d7e63be | 12 | { |
tanahashi | 0:9a7d5d7e63be | 13 | cmd[0] = LPS22HB_CTRL_REG1; |
tanahashi | 0:9a7d5d7e63be | 14 | cmd[1] = 0x40; |
tanahashi | 0:9a7d5d7e63be | 15 | _i2c -> write(_addr, cmd, 2); |
tanahashi | 0:9a7d5d7e63be | 16 | } |
tanahashi | 0:9a7d5d7e63be | 17 | |
tanahashi | 0:9a7d5d7e63be | 18 | bool LPS22HB :: test() |
tanahashi | 0:9a7d5d7e63be | 19 | { |
tanahashi | 0:9a7d5d7e63be | 20 | cmd[0] = LPS22HB_WHO_AM_I; |
tanahashi | 0:9a7d5d7e63be | 21 | _i2c -> write(_addr, cmd, 1); |
tanahashi | 0:9a7d5d7e63be | 22 | _i2c -> read(_addr, buff, 1); |
tanahashi | 0:9a7d5d7e63be | 23 | if(buff[0] == 0xB1) { |
tanahashi | 0:9a7d5d7e63be | 24 | return true; |
tanahashi | 0:9a7d5d7e63be | 25 | } else { |
tanahashi | 0:9a7d5d7e63be | 26 | return false; |
tanahashi | 0:9a7d5d7e63be | 27 | } |
tanahashi | 0:9a7d5d7e63be | 28 | } |
tanahashi | 0:9a7d5d7e63be | 29 | |
tanahashi | 0:9a7d5d7e63be | 30 | void LPS22HB :: read(float *press, float *temp) |
tanahashi | 0:9a7d5d7e63be | 31 | { |
tanahashi | 0:9a7d5d7e63be | 32 | read_press(press); |
tanahashi | 0:9a7d5d7e63be | 33 | read_temp(temp); |
tanahashi | 0:9a7d5d7e63be | 34 | } |
tanahashi | 0:9a7d5d7e63be | 35 | |
tanahashi | 0:9a7d5d7e63be | 36 | void LPS22HB :: read_press(float *press) |
tanahashi | 0:9a7d5d7e63be | 37 | { |
tanahashi | 0:9a7d5d7e63be | 38 | cmd[0] = LPS22HB_PRESS_XL; |
tanahashi | 0:9a7d5d7e63be | 39 | _i2c -> write(_addr, cmd, 1); |
tanahashi | 0:9a7d5d7e63be | 40 | _i2c -> read(_addr, buff, 3); |
tanahashi | 1:fdde28032fb5 | 41 | *press = (int)(buff[0] | buff[1] << 8 | buff[2] << 16) / LPS22HB_PRESS_LSB; |
tanahashi | 0:9a7d5d7e63be | 42 | } |
tanahashi | 0:9a7d5d7e63be | 43 | |
tanahashi | 0:9a7d5d7e63be | 44 | void LPS22HB :: read_temp(float *temp) |
tanahashi | 0:9a7d5d7e63be | 45 | { |
tanahashi | 0:9a7d5d7e63be | 46 | cmd[0] = LPS22HB_TEMP_L; |
tanahashi | 0:9a7d5d7e63be | 47 | _i2c -> write(_addr, cmd, 1); |
tanahashi | 0:9a7d5d7e63be | 48 | _i2c -> read(_addr, buff, 2); |
tanahashi | 1:fdde28032fb5 | 49 | *temp = (short)(buff[0] | buff[1] << 8) * LPS22HB_TEMP_LSB; |
tanahashi | 0:9a7d5d7e63be | 50 | } |