123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- #include "INA219.h"
- #include "i2c.h"
- static uint16_t vShuntMax;
- static uint16_t vBusMax;
- static uint16_t rShunt;
- static uint8_t flagCNVR;
- static uint8_t flagOVF;
- static t_i2c_status status = I2C_SUCCESS;
- void INA219_Config(void) {
- uint16_t config;
- vBusMax = 32000;
- vShuntMax = 80;
- rShunt = CURRENT_SHUNT_RESISTANCE;
-
- config = INA219_RESET_OFF;
- config |= INA219_RANGE_32V;
- config |= INA219_GAIN_80MV;
- config |= INA219_BUS_RES_12BIT_128S;
- config |= INA219_SHUNT_RES_12BIT_128S;
- config |= INA219_MODE_SHUNT_BUS_CONT;
- i2c_wr_reg(INA219_ADDRESS, INA219_REG_CONFIG, config);
- i2c_wr_reg(INA219_ADDRESS, INA219_REG_CALIBRATION, CALIBRATION_VALUE);
- }
- uint16_t getMaxPossibleCurrent(void) {
- return (((1000 * vShuntMax) + (rShunt>>1)) / rShunt);
- }
- uint16_t getMaxCurrent(void) {
- uint16_t maxCurrent = ((CURRENT_LSB * 32767) + 500) / 1000;
- uint16_t maxPossible = getMaxPossibleCurrent();
- if (maxCurrent > maxPossible) {
- return maxPossible;
- } else {
- return maxCurrent;
- }
- }
- uint16_t getMaxShuntVoltage(void) {
- uint16_t maxVoltage = ((getMaxCurrent() * rShunt) + 500) / 1000;
- if (maxVoltage >= vShuntMax) {
- return vShuntMax;
- } else {
- return maxVoltage;
- }
- }
- uint32_t getMaxPower(void) {
- return (((getMaxCurrent() * vBusMax) + 500000) / 1000000);
- }
- uint16_t readBusCurrent(void) {
- uint32_t current;;
- int16_t tmp;
- status = i2c_rd_reg(INA219_ADDRESS, INA219_REG_CURRENT, (uint16_t *)&tmp);
- if (status != I2C_SUCCESS) {
- return 9999;
- }
- if (tmp < 0) {
- tmp = - tmp;
- }
- current = tmp;
- current *= CURRENT_LSB;
- current += 500;
- current /= 1000;
- return (uint16_t)current;
- }
- uint32_t readBusPower(void) {
- uint32_t power;
- uint16_t tmp;
- i2c_rd_reg(INA219_ADDRESS, INA219_REG_POWER, &tmp);
- power = tmp;
- power *= POWER_LSB;
- power += 500;
- power /= 1000;
- return power;
- }
- int16_t readShuntVoltage(void) {
- uint16_t shvolt;
- status = i2c_rd_reg(INA219_ADDRESS, INA219_REG_SHUNTVOLTAGE, &shvolt);
- if (status != I2C_SUCCESS) {
- return 999;
- }
- return shvolt;
- }
- uint16_t readBusVoltage(void) {
- uint16_t volt;
- flagCNVR = 0;
- flagOVF = 0;
- status = i2c_rd_reg(INA219_ADDRESS, INA219_REG_BUSVOLTAGE, &volt);
- if (status != I2C_SUCCESS) {
- return 65535;
- }
- if ((volt & 0x0001) != 0) {
- flagOVF = 1;
- }
- if ((volt & 0x0002) != 0) {
- flagCNVR = 1;
- }
- return ((volt >> 3) * 4);
- }
- ina219_bvr_t getRange(void) {
- uint16_t value;
- i2c_rd_reg(INA219_ADDRESS, INA219_REG_CONFIG, &value);
- value &= 0x2000;
- value >>= 13;
- return (ina219_bvr_t)value;
- }
- ina219_pg_t getGain(void) {
- uint16_t value;
- i2c_rd_reg(INA219_ADDRESS, INA219_REG_CONFIG, &value);
- value &= 0x1800;
- value >>= 11;
- return (ina219_pg_t)value;
- }
- ina219_badc_t getBusRes(void) {
- uint16_t value;
- i2c_rd_reg(INA219_ADDRESS, INA219_REG_CONFIG, &value);
- value &= 0x0780;
- value >>= 7;
- return (ina219_badc_t)value;
- }
- ina219_sadc_t getShuntRes(void) {
- uint16_t value;
- i2c_rd_reg(INA219_ADDRESS, INA219_REG_CONFIG, &value);
- value &= 0x0078;
- value >>= 3;
- return (ina219_sadc_t)value;
- }
- ina219_mode_t getMode(void) {
- uint16_t value;
- i2c_rd_reg(INA219_ADDRESS, INA219_REG_CONFIG, &value);
- value &= 0x0007;
- return (ina219_mode_t)value;
- }
|