INA226 Library
Dependents: HYBRYD2018_IZU_ROCKET Hybrid_IZU2019 SWAN_IZU2019_v1 Hybrid_OB2021_MAIN
INA226.h@7:5c1d19898020, 2018-11-14 (annotated)
- Committer:
- Sigma884
- Date:
- Wed Nov 14 09:11:38 2018 +0000
- Revision:
- 7:5c1d19898020
- Parent:
- 6:4aa455bab63f
????????????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Sigma884 | 7:5c1d19898020 | 1 | #ifndef INA226_H |
Sigma884 | 7:5c1d19898020 | 2 | #define INA226_H |
zebrin1422 | 0:ad712ebc38d6 | 3 | |
zebrin1422 | 2:eccb118370eb | 4 | #define check_reg 0xFE |
zebrin1422 | 0:ad712ebc38d6 | 5 | #define Ctrl_reg 0x00 |
zebrin1422 | 0:ad712ebc38d6 | 6 | #define V_shunt_reg 0x01 |
zebrin1422 | 0:ad712ebc38d6 | 7 | #define V_bus_reg 0x02 |
zebrin1422 | 0:ad712ebc38d6 | 8 | #define power_reg 0x03 |
zebrin1422 | 0:ad712ebc38d6 | 9 | #define current_reg 0x04 |
zebrin1422 | 0:ad712ebc38d6 | 10 | #define cal_reg 0x05 |
zebrin1422 | 0:ad712ebc38d6 | 11 | |
zebrin1422 | 0:ad712ebc38d6 | 12 | //I2C i2c(PB_7, PB_6); // sda, scl |
zebrin1422 | 0:ad712ebc38d6 | 13 | //Serial pc(USBTX, USBRX); //tx, rx |
zebrin1422 | 0:ad712ebc38d6 | 14 | |
zebrin1422 | 0:ad712ebc38d6 | 15 | /** |
zebrin1422 | 0:ad712ebc38d6 | 16 | * @bref INA226 Class |
zebrin1422 | 0:ad712ebc38d6 | 17 | * @author Seiya Maemura |
zebrin1422 | 0:ad712ebc38d6 | 18 | */ |
zebrin1422 | 0:ad712ebc38d6 | 19 | |
zebrin1422 | 0:ad712ebc38d6 | 20 | |
zebrin1422 | 0:ad712ebc38d6 | 21 | class myINA226 |
zebrin1422 | 0:ad712ebc38d6 | 22 | { |
zebrin1422 | 0:ad712ebc38d6 | 23 | public: |
zebrin1422 | 0:ad712ebc38d6 | 24 | |
zebrin1422 | 6:4aa455bab63f | 25 | typedef enum A1{ |
zebrin1422 | 6:4aa455bab63f | 26 | A1_GND = 0, |
zebrin1422 | 6:4aa455bab63f | 27 | A1_VDD = 1, |
zebrin1422 | 6:4aa455bab63f | 28 | A1_SDA = 2, |
zebrin1422 | 6:4aa455bab63f | 29 | A1_SCL = 3 |
zebrin1422 | 6:4aa455bab63f | 30 | } A1; |
zebrin1422 | 6:4aa455bab63f | 31 | |
zebrin1422 | 6:4aa455bab63f | 32 | typedef enum A0{ |
zebrin1422 | 6:4aa455bab63f | 33 | A0_GND = 0, |
zebrin1422 | 6:4aa455bab63f | 34 | A0_VDD = 1, |
zebrin1422 | 6:4aa455bab63f | 35 | A0_SDA = 2, |
zebrin1422 | 6:4aa455bab63f | 36 | A0_SCL = 3 |
zebrin1422 | 6:4aa455bab63f | 37 | } A0; |
zebrin1422 | 6:4aa455bab63f | 38 | |
zebrin1422 | 6:4aa455bab63f | 39 | /*********************************************************************** |
zebrin1422 | 6:4aa455bab63f | 40 | A1,A0はそれぞれはんだでジャンパして選択します |
zebrin1422 | 6:4aa455bab63f | 41 | このA1,A0の組み合わせでスレーブアドレスを決定します |
zebrin1422 | 6:4aa455bab63f | 42 | |
zebrin1422 | 6:4aa455bab63f | 43 | @example |
zebrin1422 | 6:4aa455bab63f | 44 | A1:GND,A0:GND → SLAVE_ADDR:0b10000000 |
zebrin1422 | 6:4aa455bab63f | 45 | ***********************************************************************/ |
zebrin1422 | 0:ad712ebc38d6 | 46 | |
zebrin1422 | 6:4aa455bab63f | 47 | myINA226(I2C &i2cBus,A1 celect_1 = A1_GND, A0 celect_0 = A0_GND); |
zebrin1422 | 6:4aa455bab63f | 48 | /*********************************************************************** |
zebrin1422 | 6:4aa455bab63f | 49 | 第一、第二引数がないとスレーブアドレスが0b10000000になるようになっている |
zebrin1422 | 6:4aa455bab63f | 50 | |
zebrin1422 | 6:4aa455bab63f | 51 | ***********************************************************************/ |
zebrin1422 | 0:ad712ebc38d6 | 52 | |
zebrin1422 | 0:ad712ebc38d6 | 53 | void set_callibretion(unsigned int val = 0x0AAA); |
zebrin1422 | 6:4aa455bab63f | 54 | /*********************************************************************** |
zebrin1422 | 6:4aa455bab63f | 55 | 電流を取得したいときに必要 |
zebrin1422 | 6:4aa455bab63f | 56 | これをセットしないと電流は取れない |
zebrin1422 | 6:4aa455bab63f | 57 | |
zebrin1422 | 6:4aa455bab63f | 58 | @example |
zebrin1422 | 6:4aa455bab63f | 59 | |
zebrin1422 | 6:4aa455bab63f | 60 | float current; |
zebrin1422 | 6:4aa455bab63f | 61 | |
zebrin1422 | 6:4aa455bab63f | 62 | myINA226.set_callibretion(); |
zebrin1422 | 6:4aa455bab63f | 63 | current = myINA226.get_current(); |
zebrin1422 | 6:4aa455bab63f | 64 | ***********************************************************************/ |
zebrin1422 | 0:ad712ebc38d6 | 65 | |
zebrin1422 | 0:ad712ebc38d6 | 66 | int Connection_check(); |
zebrin1422 | 6:4aa455bab63f | 67 | /*********************************************************************** |
zebrin1422 | 6:4aa455bab63f | 68 | INA226の接続確認 |
zebrin1422 | 6:4aa455bab63f | 69 | 0が返ってくればOK |
zebrin1422 | 6:4aa455bab63f | 70 | 1が返ってくればError |
zebrin1422 | 6:4aa455bab63f | 71 | ***********************************************************************/ |
zebrin1422 | 0:ad712ebc38d6 | 72 | |
zebrin1422 | 0:ad712ebc38d6 | 73 | void setup(int i); |
zebrin1422 | 6:4aa455bab63f | 74 | /*********************************************************************** |
zebrin1422 | 6:4aa455bab63f | 75 | シャント抵抗の電圧(よくわかんない)か,測りたい電圧の値、またはその両方の値を |
zebrin1422 | 6:4aa455bab63f | 76 | 取得するかどうかを決める |
zebrin1422 | 6:4aa455bab63f | 77 | i=0のとき、両方の値を取得 |
zebrin1422 | 6:4aa455bab63f | 78 | i=1のとき、測りたい方の電圧のみ取得 |
zebrin1422 | 6:4aa455bab63f | 79 | i=2のとき、シャント抵抗の電圧のみ取得 |
zebrin1422 | 6:4aa455bab63f | 80 | 印加電圧は測りたい方の電圧なので基本的にはi=1 |
zebrin1422 | 6:4aa455bab63f | 81 | デフォルトでは、両方の値を取得できるようになっている |
zebrin1422 | 6:4aa455bab63f | 82 | ***********************************************************************/ |
zebrin1422 | 0:ad712ebc38d6 | 83 | |
zebrin1422 | 0:ad712ebc38d6 | 84 | void get_Voltage_current(float *V_bus, float *current); |
zebrin1422 | 6:4aa455bab63f | 85 | /*********************************************************************** |
zebrin1422 | 6:4aa455bab63f | 86 | INA226で測定したいだろう電圧と電流の値を |
zebrin1422 | 6:4aa455bab63f | 87 | まとめて取得する |
zebrin1422 | 6:4aa455bab63f | 88 | もしsetupでi=2、つまりシャント抵抗の電圧の値だけを取得するようにしていたら |
zebrin1422 | 6:4aa455bab63f | 89 | 事前にi=0 or 1に設定しておかなければならない |
zebrin1422 | 6:4aa455bab63f | 90 | |
zebrin1422 | 6:4aa455bab63f | 91 | @example |
zebrin1422 | 6:4aa455bab63f | 92 | |
zebrin1422 | 6:4aa455bab63f | 93 | float V_bus,current; |
zebrin1422 | 6:4aa455bab63f | 94 | |
zebrin1422 | 6:4aa455bab63f | 95 | myINA226.setup(0) or myINA226.setup(1); |
zebrin1422 | 6:4aa455bab63f | 96 | myINA226.get_Voltage_current(&V_bus,¤t); |
zebrin1422 | 6:4aa455bab63f | 97 | ***********************************************************************/ |
zebrin1422 | 0:ad712ebc38d6 | 98 | |
zebrin1422 | 1:9c367d227582 | 99 | float get_Shunt_voltage(); |
zebrin1422 | 6:4aa455bab63f | 100 | /*********************************************************************** |
zebrin1422 | 6:4aa455bab63f | 101 | シャント抵抗の電圧の値を取得 |
zebrin1422 | 6:4aa455bab63f | 102 | もしsetupでi=1、つまり印加電圧の値だけを取得するようにしていたら |
zebrin1422 | 6:4aa455bab63f | 103 | 事前にi=0 or 2に設定しておかなければならない |
zebrin1422 | 6:4aa455bab63f | 104 | |
zebrin1422 | 6:4aa455bab63f | 105 | @example |
zebrin1422 | 6:4aa455bab63f | 106 | |
zebrin1422 | 6:4aa455bab63f | 107 | float V_shunt; |
zebrin1422 | 6:4aa455bab63f | 108 | |
zebrin1422 | 6:4aa455bab63f | 109 | myINA226.setup(0) or myINA226.setup(2); |
zebrin1422 | 6:4aa455bab63f | 110 | V_shunt = myINA226.get_Shunt_voltage(); |
zebrin1422 | 6:4aa455bab63f | 111 | ***********************************************************************/ |
zebrin1422 | 0:ad712ebc38d6 | 112 | |
zebrin1422 | 1:9c367d227582 | 113 | float get_Bus_voltage(); |
zebrin1422 | 6:4aa455bab63f | 114 | /*********************************************************************** |
zebrin1422 | 6:4aa455bab63f | 115 | 印加電圧の値を取得 |
zebrin1422 | 6:4aa455bab63f | 116 | もしsetupでi=2、つまりシャント抵抗の電圧の値だけを取得するようにしていたら |
zebrin1422 | 6:4aa455bab63f | 117 | 事前にi=0 or 1に設定しておかなければならない |
zebrin1422 | 6:4aa455bab63f | 118 | |
zebrin1422 | 6:4aa455bab63f | 119 | @example |
zebrin1422 | 6:4aa455bab63f | 120 | |
zebrin1422 | 6:4aa455bab63f | 121 | float V_bus; |
zebrin1422 | 6:4aa455bab63f | 122 | |
zebrin1422 | 6:4aa455bab63f | 123 | myINA226.setup(0) or myINA226.setup(1); |
zebrin1422 | 6:4aa455bab63f | 124 | V_bus = myINA226.get_Bus_voltage(); |
zebrin1422 | 6:4aa455bab63f | 125 | ***********************************************************************/ |
zebrin1422 | 0:ad712ebc38d6 | 126 | |
zebrin1422 | 1:9c367d227582 | 127 | float get_current(); |
zebrin1422 | 6:4aa455bab63f | 128 | /*********************************************************************** |
zebrin1422 | 6:4aa455bab63f | 129 | 電流の値を取得 |
zebrin1422 | 6:4aa455bab63f | 130 | 事前にset_callibretionをセットしておかないと |
zebrin1422 | 6:4aa455bab63f | 131 | 0しか返ってこない |
zebrin1422 | 6:4aa455bab63f | 132 | @example |
zebrin1422 | 6:4aa455bab63f | 133 | |
zebrin1422 | 6:4aa455bab63f | 134 | float current; |
zebrin1422 | 6:4aa455bab63f | 135 | |
zebrin1422 | 6:4aa455bab63f | 136 | myINA226.set_callibretion(); |
zebrin1422 | 6:4aa455bab63f | 137 | current = myINA226.get_current(); |
zebrin1422 | 6:4aa455bab63f | 138 | ***********************************************************************/ |
zebrin1422 | 0:ad712ebc38d6 | 139 | |
zebrin1422 | 1:9c367d227582 | 140 | float get_power(); |
zebrin1422 | 6:4aa455bab63f | 141 | /*********************************************************************** |
zebrin1422 | 6:4aa455bab63f | 142 | 電力の値を取得 |
zebrin1422 | 6:4aa455bab63f | 143 | 事前にset_callibretionをセットしておかないと |
zebrin1422 | 6:4aa455bab63f | 144 | 0しか返ってこない |
zebrin1422 | 6:4aa455bab63f | 145 | |
zebrin1422 | 6:4aa455bab63f | 146 | @example |
zebrin1422 | 6:4aa455bab63f | 147 | |
zebrin1422 | 6:4aa455bab63f | 148 | float power; |
zebrin1422 | 6:4aa455bab63f | 149 | |
zebrin1422 | 6:4aa455bab63f | 150 | myINA226.set_callibretion(); |
zebrin1422 | 6:4aa455bab63f | 151 | power = myINA226.get_power(); |
zebrin1422 | 6:4aa455bab63f | 152 | ***********************************************************************/ |
zebrin1422 | 0:ad712ebc38d6 | 153 | |
zebrin1422 | 0:ad712ebc38d6 | 154 | |
zebrin1422 | 0:ad712ebc38d6 | 155 | private: |
zebrin1422 | 0:ad712ebc38d6 | 156 | |
zebrin1422 | 0:ad712ebc38d6 | 157 | I2C *i2c; |
zebrin1422 | 6:4aa455bab63f | 158 | char _addr_1,_addr_0,_addr; |
zebrin1422 | 6:4aa455bab63f | 159 | char SLV_WRITE_INA; |
zebrin1422 | 6:4aa455bab63f | 160 | char SLV_READ_INA; |
zebrin1422 | 0:ad712ebc38d6 | 161 | float vlotage,current; |
zebrin1422 | 0:ad712ebc38d6 | 162 | unsigned int val[4]; |
zebrin1422 | 0:ad712ebc38d6 | 163 | char *cal,buff[4],reg,check[2]; |
zebrin1422 | 0:ad712ebc38d6 | 164 | }; |
zebrin1422 | 0:ad712ebc38d6 | 165 | |
zebrin1422 | 0:ad712ebc38d6 | 166 | #endif |