電流電圧センサINA226のライブラリ
Dependents: Hybrid_IZU2021_MAIN_OS5 Hybrid_IZU2021_MAIN
PQ_INA226.h@1:415a7cc3ec67, 2021-03-08 (annotated)
- Committer:
- tanahashi
- Date:
- Mon Mar 08 16:48:44 2021 +0000
- Revision:
- 1:415a7cc3ec67
- Parent:
- 0:4c315fe513d0
fixed LSB
Who changed what in which revision?
User | Revision | Line number | New 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 | 1:415a7cc3ec67 | 10 | #define INA226_CURRENT_LSB 1.25 |
tanahashi | 0:4c315fe513d0 | 11 | #define INA226_POWER_LSB 25 |
tanahashi | 0:4c315fe513d0 | 12 | |
tanahashi | 1:415a7cc3ec67 | 13 | /** 電圧電流センサINA226のライブラリ(シャント抵抗0.002Ω) |
tanahashi | 0:4c315fe513d0 | 14 | * @code |
tanahashi | 0:4c315fe513d0 | 15 | #include "mbed.h" |
tanahashi | 0:4c315fe513d0 | 16 | #include "PQ_INA226.h" |
tanahashi | 0:4c315fe513d0 | 17 | |
tanahashi | 0:4c315fe513d0 | 18 | Serial pc(USBTX, USBRX, 115200); |
tanahashi | 0:4c315fe513d0 | 19 | I2C i2c(p9, p10); |
tanahashi | 0:4c315fe513d0 | 20 | |
tanahashi | 0:4c315fe513d0 | 21 | INA226 ina(i2c, INA226::A0_GND, INA226::A1_GND); |
tanahashi | 0:4c315fe513d0 | 22 | |
tanahashi | 0:4c315fe513d0 | 23 | float voltage, current, power; |
tanahashi | 0:4c315fe513d0 | 24 | |
tanahashi | 0:4c315fe513d0 | 25 | int main() |
tanahashi | 0:4c315fe513d0 | 26 | { |
tanahashi | 0:4c315fe513d0 | 27 | ina.begin(); |
tanahashi | 0:4c315fe513d0 | 28 | while(true) { |
tanahashi | 0:4c315fe513d0 | 29 | if(ina.test()) { |
tanahashi | 0:4c315fe513d0 | 30 | ina.read(&voltage, ¤t, &power); |
tanahashi | 0:4c315fe513d0 | 31 | pc.printf("%f\t%f\t%f\r\n", voltage, current, power); |
tanahashi | 0:4c315fe513d0 | 32 | } else { |
tanahashi | 0:4c315fe513d0 | 33 | pc.printf("ERROR!!\r\n"); |
tanahashi | 0:4c315fe513d0 | 34 | } |
tanahashi | 0:4c315fe513d0 | 35 | } |
tanahashi | 0:4c315fe513d0 | 36 | } |
tanahashi | 0:4c315fe513d0 | 37 | * @endcode |
tanahashi | 0:4c315fe513d0 | 38 | */ |
tanahashi | 0:4c315fe513d0 | 39 | class INA226 |
tanahashi | 0:4c315fe513d0 | 40 | { |
tanahashi | 0:4c315fe513d0 | 41 | public: |
tanahashi | 0:4c315fe513d0 | 42 | typedef enum A0 { |
tanahashi | 0:4c315fe513d0 | 43 | A0_GND = 0b00, |
tanahashi | 0:4c315fe513d0 | 44 | A0_VS = 0b01, |
tanahashi | 0:4c315fe513d0 | 45 | A0_SDA = 0b10, |
tanahashi | 0:4c315fe513d0 | 46 | A0_SCL = 0b11 |
tanahashi | 0:4c315fe513d0 | 47 | } A0_t; |
tanahashi | 0:4c315fe513d0 | 48 | |
tanahashi | 0:4c315fe513d0 | 49 | typedef enum A1 { |
tanahashi | 0:4c315fe513d0 | 50 | A1_GND = 0b00, |
tanahashi | 0:4c315fe513d0 | 51 | A1_VS = 0b01, |
tanahashi | 0:4c315fe513d0 | 52 | A1_SDA = 0b10, |
tanahashi | 0:4c315fe513d0 | 53 | A1_SCL = 0b11 |
tanahashi | 0:4c315fe513d0 | 54 | } A1_t; |
tanahashi | 0:4c315fe513d0 | 55 | |
tanahashi | 0:4c315fe513d0 | 56 | private: |
tanahashi | 0:4c315fe513d0 | 57 | I2C *_i2c; |
tanahashi | 0:4c315fe513d0 | 58 | int _addr; |
tanahashi | 0:4c315fe513d0 | 59 | char cmd[3]; |
tanahashi | 0:4c315fe513d0 | 60 | char buff[2]; |
tanahashi | 0:4c315fe513d0 | 61 | |
tanahashi | 0:4c315fe513d0 | 62 | public: |
tanahashi | 0:4c315fe513d0 | 63 | /** |
tanahashi | 0:4c315fe513d0 | 64 | * @param i2c I2Cのインスタンスへの参照 |
tanahashi | 0:4c315fe513d0 | 65 | * @param A0 A0のジャンパ |
tanahashi | 0:4c315fe513d0 | 66 | * @param A1 A1のジャンパ |
tanahashi | 0:4c315fe513d0 | 67 | */ |
tanahashi | 0:4c315fe513d0 | 68 | INA226(I2C &i2c, A0_t A0, A1_t A1); |
tanahashi | 0:4c315fe513d0 | 69 | |
tanahashi | 0:4c315fe513d0 | 70 | /** |
tanahashi | 0:4c315fe513d0 | 71 | * センサ動作開始 |
tanahashi | 0:4c315fe513d0 | 72 | */ |
tanahashi | 0:4c315fe513d0 | 73 | void begin(); |
tanahashi | 0:4c315fe513d0 | 74 | |
tanahashi | 0:4c315fe513d0 | 75 | /** |
tanahashi | 0:4c315fe513d0 | 76 | * センサ通信テスト |
tanahashi | 0:4c315fe513d0 | 77 | * @retval true 通信成功 |
tanahashi | 0:4c315fe513d0 | 78 | * @retval false 通信失敗 |
tanahashi | 0:4c315fe513d0 | 79 | */ |
tanahashi | 0:4c315fe513d0 | 80 | bool test(); |
tanahashi | 0:4c315fe513d0 | 81 | |
tanahashi | 0:4c315fe513d0 | 82 | /** |
tanahashi | 0:4c315fe513d0 | 83 | * 測定値読み取り |
tanahashi | 0:4c315fe513d0 | 84 | * @param voltage 電圧を格納する変数へのポインタ |
tanahashi | 0:4c315fe513d0 | 85 | * @param current 電流を格納する変数へのポインタ |
tanahashi | 0:4c315fe513d0 | 86 | * @param power 電力を格納する変数へのポインタ |
tanahashi | 0:4c315fe513d0 | 87 | */ |
tanahashi | 0:4c315fe513d0 | 88 | void read(float *voltage, float *current, float *power); |
tanahashi | 0:4c315fe513d0 | 89 | |
tanahashi | 0:4c315fe513d0 | 90 | /** |
tanahashi | 0:4c315fe513d0 | 91 | * 電圧測定値読み取り |
tanahashi | 0:4c315fe513d0 | 92 | * @param voltage 電圧を格納する変数へのポインタ |
tanahashi | 0:4c315fe513d0 | 93 | */ |
tanahashi | 0:4c315fe513d0 | 94 | void read_voltage(float *voltage); |
tanahashi | 0:4c315fe513d0 | 95 | |
tanahashi | 0:4c315fe513d0 | 96 | /** |
tanahashi | 0:4c315fe513d0 | 97 | * 電流測定値読み取り |
tanahashi | 0:4c315fe513d0 | 98 | * @param current 電流を格納する変数へのポインタ |
tanahashi | 0:4c315fe513d0 | 99 | */ |
tanahashi | 0:4c315fe513d0 | 100 | void read_current(float *current); |
tanahashi | 0:4c315fe513d0 | 101 | |
tanahashi | 0:4c315fe513d0 | 102 | /** |
tanahashi | 0:4c315fe513d0 | 103 | * 電力測定値読み取り |
tanahashi | 0:4c315fe513d0 | 104 | * @param power 電力を格納する変数へのポインタ |
tanahashi | 0:4c315fe513d0 | 105 | */ |
tanahashi | 0:4c315fe513d0 | 106 | void read_power(float *power); |
tanahashi | 0:4c315fe513d0 | 107 | }; |
tanahashi | 0:4c315fe513d0 | 108 | |
tanahashi | 0:4c315fe513d0 | 109 | #endif |