電流電圧センサINA226のライブラリ

Dependents:   Hybrid_IZU2021_MAIN_OS5 Hybrid_IZU2021_MAIN

Committer:
tanahashi
Date:
Tue Dec 15 15:37:10 2020 +0000
Revision:
0:4c315fe513d0
Child:
1:415a7cc3ec67
first commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tanahashi 0:4c315fe513d0 1 #ifndef PQ_INA226_H
tanahashi 0:4c315fe513d0 2 #define PQ_INA226_H
tanahashi 0:4c315fe513d0 3
tanahashi 0:4c315fe513d0 4 #define INA226_BUS_VOLTAGE 0x02
tanahashi 0:4c315fe513d0 5 #define INA226_POWER 0x03
tanahashi 0:4c315fe513d0 6 #define INA226_CURRENT 0x04
tanahashi 0:4c315fe513d0 7 #define INA226_CALIBRATION 0x05
tanahashi 0:4c315fe513d0 8 #define INA226_WHO_AM_I 0xFF
tanahashi 0:4c315fe513d0 9 #define INA226_VOLTAGE_LSB 1.25
tanahashi 0:4c315fe513d0 10 #define INA226_POWER_LSB 25
tanahashi 0:4c315fe513d0 11
tanahashi 0:4c315fe513d0 12 /** 電圧電流センサINA226のライブラリ
tanahashi 0:4c315fe513d0 13 * @code
tanahashi 0:4c315fe513d0 14 #include "mbed.h"
tanahashi 0:4c315fe513d0 15 #include "PQ_INA226.h"
tanahashi 0:4c315fe513d0 16
tanahashi 0:4c315fe513d0 17 Serial pc(USBTX, USBRX, 115200);
tanahashi 0:4c315fe513d0 18 I2C i2c(p9, p10);
tanahashi 0:4c315fe513d0 19
tanahashi 0:4c315fe513d0 20 INA226 ina(i2c, INA226::A0_GND, INA226::A1_GND);
tanahashi 0:4c315fe513d0 21
tanahashi 0:4c315fe513d0 22 float voltage, current, power;
tanahashi 0:4c315fe513d0 23
tanahashi 0:4c315fe513d0 24 int main()
tanahashi 0:4c315fe513d0 25 {
tanahashi 0:4c315fe513d0 26 ina.begin();
tanahashi 0:4c315fe513d0 27 while(true) {
tanahashi 0:4c315fe513d0 28 if(ina.test()) {
tanahashi 0:4c315fe513d0 29 ina.read(&voltage, &current, &power);
tanahashi 0:4c315fe513d0 30 pc.printf("%f\t%f\t%f\r\n", voltage, current, power);
tanahashi 0:4c315fe513d0 31 } else {
tanahashi 0:4c315fe513d0 32 pc.printf("ERROR!!\r\n");
tanahashi 0:4c315fe513d0 33 }
tanahashi 0:4c315fe513d0 34 }
tanahashi 0:4c315fe513d0 35 }
tanahashi 0:4c315fe513d0 36 * @endcode
tanahashi 0:4c315fe513d0 37 */
tanahashi 0:4c315fe513d0 38 class INA226
tanahashi 0:4c315fe513d0 39 {
tanahashi 0:4c315fe513d0 40 public:
tanahashi 0:4c315fe513d0 41 typedef enum A0 {
tanahashi 0:4c315fe513d0 42 A0_GND = 0b00,
tanahashi 0:4c315fe513d0 43 A0_VS = 0b01,
tanahashi 0:4c315fe513d0 44 A0_SDA = 0b10,
tanahashi 0:4c315fe513d0 45 A0_SCL = 0b11
tanahashi 0:4c315fe513d0 46 } A0_t;
tanahashi 0:4c315fe513d0 47
tanahashi 0:4c315fe513d0 48 typedef enum A1 {
tanahashi 0:4c315fe513d0 49 A1_GND = 0b00,
tanahashi 0:4c315fe513d0 50 A1_VS = 0b01,
tanahashi 0:4c315fe513d0 51 A1_SDA = 0b10,
tanahashi 0:4c315fe513d0 52 A1_SCL = 0b11
tanahashi 0:4c315fe513d0 53 } A1_t;
tanahashi 0:4c315fe513d0 54
tanahashi 0:4c315fe513d0 55 private:
tanahashi 0:4c315fe513d0 56 I2C *_i2c;
tanahashi 0:4c315fe513d0 57 int _addr;
tanahashi 0:4c315fe513d0 58 char cmd[3];
tanahashi 0:4c315fe513d0 59 char buff[2];
tanahashi 0:4c315fe513d0 60
tanahashi 0:4c315fe513d0 61 public:
tanahashi 0:4c315fe513d0 62 /**
tanahashi 0:4c315fe513d0 63 * @param i2c I2Cのインスタンスへの参照
tanahashi 0:4c315fe513d0 64 * @param A0 A0のジャンパ
tanahashi 0:4c315fe513d0 65 * @param A1 A1のジャンパ
tanahashi 0:4c315fe513d0 66 */
tanahashi 0:4c315fe513d0 67 INA226(I2C &i2c, A0_t A0, A1_t A1);
tanahashi 0:4c315fe513d0 68
tanahashi 0:4c315fe513d0 69 /**
tanahashi 0:4c315fe513d0 70 * センサ動作開始
tanahashi 0:4c315fe513d0 71 */
tanahashi 0:4c315fe513d0 72 void begin();
tanahashi 0:4c315fe513d0 73
tanahashi 0:4c315fe513d0 74 /**
tanahashi 0:4c315fe513d0 75 * センサ通信テスト
tanahashi 0:4c315fe513d0 76 * @retval true 通信成功
tanahashi 0:4c315fe513d0 77 * @retval false 通信失敗
tanahashi 0:4c315fe513d0 78 */
tanahashi 0:4c315fe513d0 79 bool test();
tanahashi 0:4c315fe513d0 80
tanahashi 0:4c315fe513d0 81 /**
tanahashi 0:4c315fe513d0 82 * 測定値読み取り
tanahashi 0:4c315fe513d0 83 * @param voltage 電圧を格納する変数へのポインタ
tanahashi 0:4c315fe513d0 84 * @param current 電流を格納する変数へのポインタ
tanahashi 0:4c315fe513d0 85 * @param power 電力を格納する変数へのポインタ
tanahashi 0:4c315fe513d0 86 */
tanahashi 0:4c315fe513d0 87 void read(float *voltage, float *current, float *power);
tanahashi 0:4c315fe513d0 88
tanahashi 0:4c315fe513d0 89 /**
tanahashi 0:4c315fe513d0 90 * 電圧測定値読み取り
tanahashi 0:4c315fe513d0 91 * @param voltage 電圧を格納する変数へのポインタ
tanahashi 0:4c315fe513d0 92 */
tanahashi 0:4c315fe513d0 93 void read_voltage(float *voltage);
tanahashi 0:4c315fe513d0 94
tanahashi 0:4c315fe513d0 95 /**
tanahashi 0:4c315fe513d0 96 * 電流測定値読み取り
tanahashi 0:4c315fe513d0 97 * @param current 電流を格納する変数へのポインタ
tanahashi 0:4c315fe513d0 98 */
tanahashi 0:4c315fe513d0 99 void read_current(float *current);
tanahashi 0:4c315fe513d0 100
tanahashi 0:4c315fe513d0 101 /**
tanahashi 0:4c315fe513d0 102 * 電力測定値読み取り
tanahashi 0:4c315fe513d0 103 * @param power 電力を格納する変数へのポインタ
tanahashi 0:4c315fe513d0 104 */
tanahashi 0:4c315fe513d0 105 void read_power(float *power);
tanahashi 0:4c315fe513d0 106 };
tanahashi 0:4c315fe513d0 107
tanahashi 0:4c315fe513d0 108 #endif