stm32g030f6p6读取ina3221

uint16_t INA3221_GetShuntVoltage(uint8_t channel)
{
    uint32_t temp = 0;
    switch (channel)
    {
    case 1:
        temp = INA3221_ReadData(INA3221_ADDR, INA3221_CH1SHUNT_REG);
        break;
    case 2:
        temp = INA3221_ReadData(INA3221_ADDR, INA3221_CH2SHUNT_REG);
        break;
    case 3:
        temp = INA3221_ReadData(INA3221_ADDR, INA3221_CH3SHUNT_REG);
        break;
    default:
        break;
    }
		
    if (temp & 0x8000)
        temp = ~(temp - 1);
		//   // 1 LSB = 40uV
    return (uint16_t)(temp>>3)*40;
}

参考:https://www.ti.com/lit/ds/symlink/ina3221.pdf

注意 1lsb是40uv,需要乘以40uv,得到的单位是是uV,除以取样电阻就是电流

((float)INA3221_GetShuntVoltage(i+1))/1000/1000/0.005; //current_A

uint16_t INA3221_GetVoltage(uint8_t channel)
{
    uint32_t temp = 0;
    switch (channel)
    {
    case 1:
        temp = INA3221_ReadData(INA3221_ADDR, INA3221_CH1BUS_REG);
        break;
    case 2:
        temp = INA3221_ReadData(INA3221_ADDR, INA3221_CH2BUS_REG);
        break;
    case 3:
        temp = INA3221_ReadData(INA3221_ADDR, INA3221_CH3BUS_REG);
        break;
    default:
        break;
    }
    if (temp & 0x8000)
        temp = ~(temp - 1);
    return (uint16_t)(temp>>3)*8;
}

电压同理 1lsb 8mV,右移三位再乘8 其实就是寄存器值

(float)INA3221_GetVoltage(i+1)/1000; //voltage_V

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
INA3221是一种带有I2C接口的三路电流和电压监测器,可以用来测量多个电源的电流和电压。以下是使用Arduino读取INA3221数据的简单示例代码: ```arduino #include <Wire.h> #define INA3221_ADDRESS 0x40 // INA3221 I2C地址 #define INA3221_REG_CONFIG 0x00 // 配置寄存器地址 #define INA3221_REG_SHUNTVOLTAGE_1 0x01 // 通道1电压寄存器地址 #define INA3221_REG_BUSVOLTAGE_1 0x02 // 通道1电流寄存器地址 #define INA3221_REG_SHUNTVOLTAGE_2 0x03 // 通道2电压寄存器地址 #define INA3221_REG_BUSVOLTAGE_2 0x04 // 通道2电流寄存器地址 #define INA3221_REG_SHUNTVOLTAGE_3 0x05 // 通道3电压寄存器地址 #define INA3221_REG_BUSVOLTAGE_3 0x06 // 通道3电流寄存器地址 void setup() { Serial.begin(9600); Wire.begin(); Wire.beginTransmission(INA3221_ADDRESS); Wire.write(INA3221_REG_CONFIG); Wire.write(0x7E); // 配置INA3221,使其采样时间为1.1ms,分辨率为12位,连续模式 Wire.endTransmission(); } void loop() { float shuntVoltage1, busVoltage1, current1, shuntVoltage2, busVoltage2, current2, shuntVoltage3, busVoltage3, current3; Wire.beginTransmission(INA3221_ADDRESS); Wire.write(INA3221_REG_SHUNTVOLTAGE_1); Wire.endTransmission(); Wire.requestFrom(INA3221_ADDRESS, 2); shuntVoltage1 = Wire.read() << 8 | Wire.read(); shuntVoltage1 *= 0.000025; // 将原始数据转换为电压值 Wire.beginTransmission(INA3221_ADDRESS); Wire.write(INA3221_REG_BUSVOLTAGE_1); Wire.endTransmission(); Wire.requestFrom(INA3221_ADDRESS, 2); busVoltage1 = Wire.read() << 8 | Wire.read(); busVoltage1 *= 0.001; // 将原始数据转换为电压值 current1 = shuntVoltage1 / 0.1; // 电流计算公式 Serial.print("Channel 1: "); Serial.print(busVoltage1); Serial.print("V "); Serial.print(current1); Serial.println("A"); // 读取通道2的数据 // ... // 读取通道3的数据 // ... delay(1000); } ``` 在这个例子中,我们首先配置了INA3221,以便它采样的时间为1.1ms,分辨率为12位,然后在循环中读取每个通道的电流和电压数据,并计算出实际的电流值。注意,这里的电压和电流值都需要根据INA3221的数据手册进行转换。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值