|
DeterministicESPAsyncWebServer v6.27.1
Zero-allocation, bounded-execution async HTTP server for ESP32
|
TI INA219 high-side current / power monitor codec (DETWS_ENABLE_INA219). More...
#include <stdint.h>Go to the source code of this file.
Macros | |
| #define | INA219_REG_CONFIG 0x00 |
| configuration | |
| #define | INA219_REG_SHUNT 0x01 |
| shunt voltage | |
| #define | INA219_REG_BUS 0x02 |
| bus voltage | |
| #define | INA219_REG_POWER 0x03 |
| power | |
| #define | INA219_REG_CURRENT 0x04 |
| current | |
| #define | INA219_REG_CALIBRATION 0x05 |
| calibration | |
Functions | |
| int32_t | ina219_bus_mv (uint16_t raw) |
| Decode the bus-voltage register to millivolts (value is bits [15:3], LSB 4 mV). | |
| int32_t | ina219_shunt_uv (int16_t raw) |
| Decode the shunt-voltage register to microvolts (signed, LSB 10 uV). | |
| uint16_t | ina219_calibration (uint32_t current_lsb_ua, uint32_t shunt_mohm) |
Compute the calibration register from the current LSB (microamps per bit) and the shunt resistance (milliohms): trunc(0.04096 / (current_LSB[A] * R[ohm])) = 40960000 / (lsb_ua * shunt_mohm), clamped to 16 bits. (100 uA + 100 mohm -> 4096.) 0 on a zero denominator. | |
| int32_t | ina219_current_ua (int16_t raw, uint32_t current_lsb_ua) |
| Scale the raw current register to microamps (raw * current_lsb_ua). | |
| int32_t | ina219_power_uw (int16_t raw, uint32_t current_lsb_ua) |
| Scale the raw power register to microwatts (power LSB is 20 * current LSB). | |
| bool | ina219_begin (uint8_t addr, uint32_t current_lsb_ua, uint32_t shunt_mohm) |
Program the INA219 at addr: write the calibration for current_lsb_ua (uA/bit) and shunt_mohm (milliohms), then the default 32 V / 320 mV continuous config. | |
| bool | ina219_read_bus_mv (int32_t *millivolts) |
Read the bus voltage into millivolts. | |
| bool | ina219_read_shunt_uv (int32_t *microvolts) |
Read the shunt voltage into microvolts. | |
| bool | ina219_read_current_ua (int32_t *microamps) |
Read the current into microamps (needs the calibration set by ina219_begin). | |
| bool | ina219_read_power_uw (int32_t *microwatts) |
Read the power into microwatts (needs the calibration set by ina219_begin). | |
TI INA219 high-side current / power monitor codec (DETWS_ENABLE_INA219).
The INA219 measures the voltage across a shunt resistor (LSB 10 uV) and the bus voltage (LSB 4 mV, in the upper 13 bits of its register), and - once a calibration value derived from the shunt resistance and a chosen current LSB is programmed - reports current and power directly. From those you get how much current and power a circuit draws.
This codec is pure and host-tested: ina219_bus_mv / ina219_shunt_uv decode the voltage registers, ina219_calibration computes the calibration register, and ina219_current_ua / ina219_power_uw scale the raw current / power registers by the current LSB. On an ESP32 the binding programs the calibration + config and reads the registers over I2C (Wire); only that touches hardware.
A cheap solder-and-bench-test breakout: put it in series with a load and watch the current.
Definition in file ina219.h.
| int32_t ina219_bus_mv | ( | uint16_t | raw | ) |
Decode the bus-voltage register to millivolts (value is bits [15:3], LSB 4 mV).
| int32_t ina219_shunt_uv | ( | int16_t | raw | ) |
Decode the shunt-voltage register to microvolts (signed, LSB 10 uV).
| uint16_t ina219_calibration | ( | uint32_t | current_lsb_ua, |
| uint32_t | shunt_mohm | ||
| ) |
Compute the calibration register from the current LSB (microamps per bit) and the shunt resistance (milliohms): trunc(0.04096 / (current_LSB[A] * R[ohm])) = 40960000 / (lsb_ua * shunt_mohm), clamped to 16 bits. (100 uA + 100 mohm -> 4096.) 0 on a zero denominator.
| int32_t ina219_current_ua | ( | int16_t | raw, |
| uint32_t | current_lsb_ua | ||
| ) |
Scale the raw current register to microamps (raw * current_lsb_ua).
| int32_t ina219_power_uw | ( | int16_t | raw, |
| uint32_t | current_lsb_ua | ||
| ) |
Scale the raw power register to microwatts (power LSB is 20 * current LSB).
| bool ina219_begin | ( | uint8_t | addr, |
| uint32_t | current_lsb_ua, | ||
| uint32_t | shunt_mohm | ||
| ) |
Program the INA219 at addr: write the calibration for current_lsb_ua (uA/bit) and shunt_mohm (milliohms), then the default 32 V / 320 mV continuous config.
| bool ina219_read_bus_mv | ( | int32_t * | millivolts | ) |
Read the bus voltage into millivolts.
| bool ina219_read_shunt_uv | ( | int32_t * | microvolts | ) |
Read the shunt voltage into microvolts.
| bool ina219_read_current_ua | ( | int32_t * | microamps | ) |
Read the current into microamps (needs the calibration set by ina219_begin).
| bool ina219_read_power_uw | ( | int32_t * | microwatts | ) |
Read the power into microwatts (needs the calibration set by ina219_begin).