PQ_Hybrid_Electrical_Equipment_Team / PQ_LPS22HB

Dependents:   Hybrid_IZU2021_MAIN_OS5 Hybrid_IZU2021_MISSION_v2 Hybrid_IZU2021_MAIN Hybrid_IZU2021_MISSION

Files at this revision

API Documentation at this revision

Comitter:
tanahashi
Date:
Tue Dec 15 15:37:50 2020 +0000
Child:
1:fdde28032fb5
Commit message:
first commit

Changed in this revision

PQ_LPS22HB.cpp Show annotated file Show diff for this revision Revisions of this file
PQ_LPS22HB.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PQ_LPS22HB.cpp	Tue Dec 15 15:37:50 2020 +0000
@@ -0,0 +1,50 @@
+#include "mbed.h"
+#include "PQ_LPS22HB.h"
+
+LPS22HB :: LPS22HB(I2C &i2c, SA0_t SA0)
+{
+    _addr = SA0;
+    _i2c = &i2c;
+    _i2c -> frequency(400000);
+}
+
+void LPS22HB :: begin()
+{
+    cmd[0] = LPS22HB_CTRL_REG1;
+    cmd[1] = 0x40;
+    _i2c -> write(_addr, cmd, 2);
+}
+
+bool LPS22HB :: test()
+{
+    cmd[0] = LPS22HB_WHO_AM_I;
+    _i2c -> write(_addr, cmd, 1);
+    _i2c -> read(_addr, buff, 1);
+    if(buff[0] == 0xB1) {
+        return true;
+    } else {
+        return false;
+    }
+}
+
+void LPS22HB :: read(float *press, float *temp)
+{
+    read_press(press);
+    read_temp(temp);
+}
+
+void LPS22HB :: read_press(float *press)
+{
+    cmd[0] = LPS22HB_PRESS_XL;
+    _i2c -> write(_addr, cmd, 1);
+    _i2c -> read(_addr, buff, 3);
+    *press = (int)(buff[0] | buff[1] << 8 | buff[2] << 16) / 4096.0f;
+}
+
+void LPS22HB :: read_temp(float *temp)
+{
+    cmd[0] = LPS22HB_TEMP_L;
+    _i2c -> write(_addr, cmd, 1);
+    _i2c -> read(_addr, buff, 2);
+    *temp = (short)(buff[0] | buff[1] << 8) * 0.01;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PQ_LPS22HB.h	Tue Dec 15 15:37:50 2020 +0000
@@ -0,0 +1,91 @@
+#ifndef PQ_LPS22HB_H
+#define PQ_LPS22HB_H
+
+#define LPS22HB_ADDR_HIGH 0b1011101<<1
+#define LPS22HB_ADDR_LOW 0b1011100<<1
+#define LPS22HB_WHO_AM_I 0x0F
+#define LPS22HB_CTRL_REG1 0x10
+#define LPS22HB_PRESS_XL 0x28
+#define LPS22HB_TEMP_L 0x2B
+
+/** LPS22HBのライブラリ
+ * @code
+#include "mbed.h"
+#include "PQ_LPS22HB.h"
+
+Serial pc(USBTX, USBRX, 115200);
+
+I2C i2c(p9, p10);
+
+LPS22HB lps(i2c, LPS22HB::SA0_HIGH);
+
+float press, temp;
+
+int main() {
+    lps.begin();
+    while(1) {
+        if(lps.test()){
+            lps.read(&press, &temp);
+            pc.printf("%f\t%f\r\n", press, temp);
+        }
+        else {
+            pc.printf("ERROR!!\r\n");
+        }
+    }
+}
+ * @endcode
+ */
+class LPS22HB
+{
+public:
+    typedef enum {
+        SA0_HIGH = LPS22HB_ADDR_HIGH,
+        SA0_LOW = LPS22HB_ADDR_LOW
+    } SA0_t;
+
+private:
+    I2C *_i2c;
+    int _addr;
+    char cmd[2];
+    char buff[6];
+
+public:
+    /**
+     * @param i2c I2Cのインスタンスへの参照
+     * @param SA0 SA0ピンのH/Lレベル
+     */
+    LPS22HB(I2C &i2c, SA0_t SA0);
+
+    /**
+     * センサ動作開始
+     */
+    void begin();
+
+    /**
+     * センサ通信テスト
+     * @retval true 通信成功
+     * @retval false 通信失敗
+     */
+    bool test();
+
+    /**
+     * 測定値の読み取り
+     * @param press 気圧を格納する変数
+     * @param temp 温度を格納する変数
+     */
+    void read(float *press, float *temp);
+
+    /**
+     * 気圧測定値の読み取り
+     * @param press 気圧を格納する変数
+     */
+    void read_press(float *press);
+
+    /**
+     * 温度測定値の読み取り
+     * @param temp 温度を格納する変数
+     */
+    void read_temp(float *temp);
+};
+
+#endif
\ No newline at end of file