#i2c-driver #power #driver #i2c #embedded-hal-driver #embedded-hal-i2c

no-std ina3221

INA3221三通道电力监控器的嵌入式驱动程序

10个不稳定版本 (3个破坏性更新)

0.4.5 2023年2月20日
0.4.4 2023年2月3日
0.3.0 2023年2月1日
0.2.1 2023年2月1日
0.1.0 2023年2月1日

嵌入式开发 中排名第1525

每月下载量34

MIT许可证

29KB
314

ina3221

crates.io

Rust语言中INA3221三通道电力监控器的嵌入式驱动程序。

INA3221与经典INA219电力监控IC非常相似。

兼容性

任何支持embedded-hal blocking 1.0 I2c的板子都应与该库兼容。

注意:一些HAL需要特征标记以启用1.0功能,例如esp-hal需要eh1特征。

安装

您可以通过crates.io添加。

$ cargo add ina3221

注意:一些HAL需要特征标记以启用1.0功能,例如esp-hal需要eh1特征。

文档

您可以在此处找到文档。

示例

此示例假设使用0.1欧姆的分流电阻进行电流和功率计算。

const INA3221_I2C_ADDR: u8 = 0x40;
const SHUNT_RESISTANCE: f32 = 0.1f32;   // 0.1 Ohm

use ina3221::INA3221;

fn main() {
    let i2c = I2C::new(/* initialize your I2C here */);
    let ina = INA3221::new(i2c, INA3221_I2C_ADDR);

    let mut delay = Delay::new(/* initialize your delay/clocks */);

    loop {
        for channel in 0..3 {
            let shunt_voltage = ina.get_shunt_voltage(channel).unwrap();
            let bus_voltage = ina.get_bus_voltage(channel).unwrap();
            
            // Voltage can be added using the '+' operator on the unit type
            let load_voltage = bus_voltage + shunt_voltage;

            // Skip channel if no voltage present
            if shunt_voltage.is_zero() {
                continue;
            }
            
            // Use Ohm's Law to calculate current and power with known resistance
            let current_milliamps = shunt_voltage.milli_volts() / SHUNT_RESISTANCE;
            let power_milliwatts = current_milliamps * load_voltage.volts();
            
            println!(
                "Channel {}: load = {:.3} V, current = {:.3} mA, power = {:.3} mW",
                channel_index + 1,
                load_voltage.volts(),
                current_milliamps,
                power_milliwatts,
            );
        }
        
        delay.delay_ms(1000u32);
    }
}

输出

这是通过USB为Arduino Uno R3供电并运行blinky脚本的示例输出。

Channel 1: load = 5.212 V, current = 36.800 mA, power = 191.790 mW
Channel 1: load = 5.211 V, current = 33.600 mA, power = 175.102 mW
Channel 1: load = 5.212 V, current = 36.800 mA, power = 191.790 mW
Channel 1: load = 5.219 V, current = 34.000 mA, power = 177.460 mW
Channel 1: load = 5.212 V, current = 36.800 mA, power = 191.790 mW
Channel 1: load = 5.211 V, current = 34.000 mA, power = 177.188 mW
Channel 1: load = 5.211 V, current = 34.000 mA, power = 177.188 mW
Channel 1: load = 5.212 V, current = 36.400 mA, power = 189.704 mW
Channel 1: load = 5.211 V, current = 34.000 mA, power = 177.188 mW
Channel 1: load = 5.212 V, current = 36.800 mA, power = 191.790 mW
Channel 1: load = 5.211 V, current = 34.000 mA, power = 177.188 mW

依赖项

~220KB