INA219例程,可校准电流值误差(基于stm32)

这篇具有很好参考价值的文章主要介绍了INA219例程,可校准电流值误差(基于stm32)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

ina219简介

INA219可用于测量电压、电流、功率,通过IIC输出结果。

引脚定义

IN+和IN-:分别是接检测分流电阻的两端。接线时与想要检测的负载串联,该芯片检测的电压为IN-和GND的电压差,因此接线时应该IN+接电源正极,从IN-流入负载,负载再接GND

GND:接电源负极,注意共地
Vs:传感器供电正极(电压范围:3-5.5V)

SCL:通讯时钟线
SDA:通讯数据线

A0和A1:地址选择引脚(IIC通讯时的从机地址)
ina219,stm32,单片机,arm

传感器定义

INA219共有6个寄存器
ina219,stm32,单片机,arm
00:配置寄存器,主要是用来配置INA219的工作方式和参数

01:分流电阻两端的电压

02:总线电压(IN-和GND的电压差)

03:功率

04:电流

05:基准值寄存器,用于设置基准值

寄存器具体内容可以参考其他博客和ina219参考手册,写得很具体了
INA219例程
INA219参考手册

使用前配置

INA219使用前需要对00配置寄存器以及05基准值寄存器进行配置

  1. 00寄存器bit13:设置检测最大检测电压 0=16V,1=32V
  2. 00寄存器bit11-12:设置总线分流电阻最大的电压
  3. 00寄存器bit0-2:设置工作模式
  4. 05寄存器:设置基准值

05寄存器的基准值可以通过以下公式得到:
ina219,stm32,单片机,arm

我使用的ina219是这个
ina219,stm32,单片机,arm

分流电阻两端最大电压设置为320mv,分流电阻阻值为0.1Ω
因此最大能检测电流大小为0.32V/0.1Ω=3.2A
Current_LSB_MIN=I_MAX/(2^ 15)=3.2/32767=0.00009766
Current_LSB_MAX=I_MAX/(2^ 12)=3.2/4096=0.00078125
因此这里选择了Current_LSB = 0.0001A
可以计算得到cal = 0.04096/(0.0001*0.1) = 4096 = 0x1000

但是这样配置完后发现测量到的电流值不对,很多博客也没有提到这个问题,最后翻看官方手册,发现还有Cal的校准公式
ina219,stm32,单片机,arm
用电流表测到的实际值为0.290A,传感器接收到的为0.342A,
校准后的cal=4096*0.290/0.3421 = 3472 = 0x0D90
设置后的测量值正确

代码例程

代码基于stm32,iic是通过模拟iic实现,PB13为SCL口,PB12为SDA口文章来源地址https://www.toymoban.com/news/detail-841035.html

#include "ina219.h"
#include "SysTick.h"

u8  ina219_busVolt_LSB_mV = 4;   // Bus Voltage LSB value = 4mV
u8  ina219_shuntVolt_LSB_uV = 10;  // Shunt Voltage LSB value = 10uV
unsigned short ina219_calValue = 0;
 
u32 ina219_current_LSB_uA;
u32 ina219_power_LSB_mW;
 
INA219_DATA ina219_data;
 
u8 ram_for_ina219[60];
u8 INA219process_flag;
#define Open 1
#define Close 0
 
void INA_SCL_OUT(void)
{
    GPIO_InitTypeDef  GPIO_InitStructure;
 
    RCC_APB2PeriphClockCmd(INA219_I2C_GPIO_CLOCK, ENABLE);
    
    /* Configure I2C1 pins: PB12->SCL->OUT */
    GPIO_InitStructure.GPIO_Pin = INA219_I2C_SCL_PIN;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_Init(INA219_I2C_PORT, &GPIO_InitStructure);
    GPIO_SetBits(INA219_I2C_PORT, INA219_I2C_SCL_PIN);
}
 
void INA_SDA_OUT(void)
{
    GPIO_InitTypeDef  GPIO_InitStructure;
 
    RCC_APB2PeriphClockCmd(INA219_I2C_GPIO_CLOCK, ENABLE);
    
    /* Configure I2C1 pins: PB14->SDA-OUT */
    GPIO_InitStructure.GPIO_Pin = INA219_I2C_SDA_PIN;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_Init(INA219_I2C_PORT, &GPIO_InitStructure);
    GPIO_SetBits(INA219_I2C_PORT, INA219_I2C_SDA_PIN);
}
 
void INA_SDA_IN(void)
{
    GPIO_InitTypeDef  GPIO_InitStructure;
 
    RCC_APB2PeriphClockCmd(INA219_I2C_GPIO_CLOCK, ENABLE);
    
    /* Configure I2C1 pins: PB14->SDA-IN */
    GPIO_InitStructure.GPIO_Pin = INA219_I2C_SDA_PIN;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_Init(INA219_I2C_PORT, &GPIO_InitStructure);
}
 
void INA_IIC_Start(void)
{
  INA_SDA_OUT();
  INA_SCL_OUT();
  
  INA_SDA_SET;
  INA_SCL_SET;
  INA_SDA_CLR;
  INA_SCL_CLR;
}
 
void INA_IIC_Stop(void)
{
    INA_SDA_OUT();
 
    INA_SDA_CLR;
    INA_SCL_SET;
    INA_SDA_SET;
}
 
void INA_IIC_Set_Ack(unsigned char ack)
{
    INA_SDA_OUT();
    
    if(ack)
    {
      INA_SDA_SET;
    }
    else
    {
      INA_SDA_CLR;
    }
    
    INA_SCL_SET;
    INA_SCL_CLR;
}
 
unsigned char INA_IIC_Get_Ack(void)
{
    unsigned char ack;
 
    INA_SDA_IN();
    INA_SDA_SET;
    INA_SCL_SET;
    if(INA_SDA_TST)
    {
      ack = 1;
    }
    else
    {
      ack = 0;
    }
    
    INA_SCL_CLR;
 
    return(ack);
}
 
void INA_IIC_Write_8bits(unsigned char dat)
{
  unsigned char i;
  
  INA_SDA_OUT();
  for(i = 8; i; i--)
  {
    if(dat & 0x80)
    {
      INA_SDA_SET;
    }
    else
    {
      INA_SDA_CLR;
    }
    
    INA_SCL_SET;
    dat <<= 1;
    INA_SCL_CLR;
  }
}
 
unsigned char INA_IIC_Read_8bits(void)
{
    unsigned char i, dat;
 
    INA_SDA_IN();
    INA_SDA_SET;
    dat = 0;
    for(i = 8; i; i--)
    {
        INA_SCL_SET;
        dat <<= 1;
        if(INA_SDA_TST)
          dat++;
        INA_SCL_CLR;
    }
    
    return(dat);
}
 
void INA_IIC_Write_Byte(unsigned char reg, unsigned char dat)
{
  unsigned char dev = INA219_I2C_ADDRESS;
  
  INA_IIC_Start();
  
  //  dev &= ~0x01;
  INA_IIC_Write_8bits(dev);
  INA_IIC_Get_Ack();
  
  INA_IIC_Write_8bits(reg);
  INA_IIC_Get_Ack();
  
  INA_IIC_Write_8bits(dat);
  INA_IIC_Get_Ack();
  
  INA_IIC_Stop();
}
 
unsigned char INA_IIC_Read_Byte(unsigned char reg)
{
  unsigned char dat;
  unsigned char dev = INA219_I2C_ADDRESS;
  
  INA_IIC_Start();
  
  //  dev &= ~0x01;
  INA_IIC_Write_8bits(dev);
  INA_IIC_Get_Ack();
  
  INA_IIC_Write_8bits(reg);
  INA_IIC_Get_Ack();
  
  INA_IIC_Start();
  
  dev |= 0x01;
  INA_IIC_Write_8bits(dev);
  INA_IIC_Get_Ack();
  
  dat = INA_IIC_Read_8bits();
  INA_IIC_Set_Ack(1);
  
  INA_IIC_Stop();
  
  return (dat);
}
 
void INA_IIC_Write_Bytes(unsigned char reg, unsigned char *dat, unsigned char num)
{
  unsigned char dev = INA219_I2C_ADDRESS;
  
  INA_IIC_Start();
  
  //  dev &= ~0x01;
  INA_IIC_Write_8bits(dev);
  INA_IIC_Get_Ack();
  
  INA_IIC_Write_8bits(reg);
  INA_IIC_Get_Ack();
  
  while(num--)
  {
    INA_IIC_Write_8bits(*dat);
    INA_IIC_Get_Ack();
    dat++;
  }
  
  INA_IIC_Stop();
}
 
void INA_IIC_Read_Bytes(unsigned char reg, unsigned char *dat, unsigned char num)
{
  unsigned char *tmp = dat;
  unsigned char dev = INA219_I2C_ADDRESS;
  
  INA_IIC_Start();
  
  //  dev &= ~0x01;
  INA_IIC_Write_8bits(dev);
  INA_IIC_Get_Ack();
  
  INA_IIC_Write_8bits(reg);
  INA_IIC_Get_Ack();
  
  INA_IIC_Start();
  
  dev |= 0x01;
  INA_IIC_Write_8bits(dev);
  INA_IIC_Get_Ack();
  
  while(num--)
  {
    *tmp = INA_IIC_Read_8bits();
    if(num == 0)
      INA_IIC_Set_Ack(1);
    else
      INA_IIC_Set_Ack(0);
    tmp++;
  }
  
  INA_IIC_Stop();
}
 
void ina219_Write_Register(unsigned char reg, unsigned int dat)
{
    unsigned char val[2];
    
    val[0] = (unsigned char)(dat >> 8);
    val[1] = (unsigned char)(dat & 0xFF);
    INA_IIC_Write_Bytes(reg, val, 2);
}
 
void ina219_Read_Register(unsigned char reg, signed short *dat)
{
    //printf("read reg == %d\r\n",reg);
  unsigned char val[2];
  
  INA_IIC_Read_Bytes(reg, val, 2);
  *dat = ((unsigned int)(val[0]) << 8) + val[1];
  
    //printf("data1 == %x\r\n",val[0]);
    //printf("data2 == %x\r\n",val[1]);
    
}
 
// INA219 Set Calibration 16V/16A(Max) 0.02|?
void ina219_SetCalibration_16V_16A(void)
{
  u16 configValue;
  
  // By default we use a pretty huge range for the input voltage,
  // which probably isn't the most appropriate choice for system
  // that don't use a lot of power.  But all of the calculations
  // are shown below if you want to change the settings.  You will
  // also need to change any relevant register settings, such as
  // setting the VBUS_MAX to 16V instead of 32V, etc.
  
  // VBUS_MAX     = 16V   (Assumes 16V, can also be set to 32V)
  // VSHUNT_MAX   = 0.32  (Assumes Gain 8, 320mV, can also be 0.16, 0.08, 0.04)
  // RSHUNT       = 0.02   (Resistor value in ohms)
  
  // 1. Determine max possible current
  // MaxPossible_I = VSHUNT_MAX / RSHUNT
  // MaxPossible_I = 16A
  
  // 2. Determine max expected current
  // MaxExpected_I = 16A
  
  // 3. Calculate possible range of LSBs (Min = 15-bit, Max = 12-bit)
  // MinimumLSB = MaxExpected_I/32767
  // MinimumLSB = 0.00048            (0.48mA per bit)
  // MaximumLSB = MaxExpected_I/4096
  // MaximumLSB = 0,00390            (3.9mA per bit)
  
  // 4. Choose an LSB between the min and max values
  //    (Preferrably a roundish number close to MinLSB)
  // CurrentLSB = 0.00050            (500uA per bit)
  
  // 5. Compute the calibration register
  // Cal = trunc (0.04096 / (Current_LSB * RSHUNT))
  // Cal = 4096 (0x1000)
  
  ina219_calValue = 0x0D90;  //0x1000;
  
  // 6. Calculate the power LSB
  // PowerLSB = 20 * CurrentLSB
  // PowerLSB = 0.01 (10mW per bit)
  
  // 7. Compute the maximum current and shunt voltage values before overflow
  //
  // Max_Current = Current_LSB * 32767
  // Max_Current = 16.3835A before overflow
  //
  // If Max_Current > Max_Possible_I then
  //    Max_Current_Before_Overflow = MaxPossible_I
  // Else
  //    Max_Current_Before_Overflow = Max_Current
  // End If
  //
  // Max_ShuntVoltage = Max_Current_Before_Overflow * RSHUNT
  // Max_ShuntVoltage = 0.32V
  //
  // If Max_ShuntVoltage >= VSHUNT_MAX
  //    Max_ShuntVoltage_Before_Overflow = VSHUNT_MAX
  // Else
  //    Max_ShuntVoltage_Before_Overflow = Max_ShuntVoltage
  // End If
  
  // 8. Compute the Maximum Power
  // MaximumPower = Max_Current_Before_Overflow * VBUS_MAX
  // MaximumPower = 1.6 * 16V
  // MaximumPower = 256W
  
  // Set multipliers to convert raw current/power values
  ina219_current_LSB_uA = 100;     // Current LSB = 500uA per bit
  ina219_power_LSB_mW = 2;        // Power LSB = 10mW per bit = 20 * Current LSB
  
  // Set Calibration register to 'Cal' calculated above
  ina219_Write_Register(INA219_REG_CALIBRATION, ina219_calValue);
  
  // Set Config register to take into account the settings above
  configValue = ( INA219_CFG_BVOLT_RANGE_16V | INA219_CFG_SVOLT_RANGE_320MV | INA219_CFG_BADCRES_12BIT_16S_8MS | INA219_CFG_SADCRES_12BIT_16S_8MS | INA219_CFG_MODE_SANDBVOLT_CONTINUOUS );
  
  ina219_Write_Register(INA219_REG_CONFIG, configValue);
}
 
void ina219_configureRegisters(void)
{
  delay_ms(15);
  
  ina219_SetCalibration_16V_16A();
}
 
void ina219_gpio_init(void)
{
    INA_SCL_OUT();
    INA_SDA_OUT();
}
 
void ina219_init(void)
{
  ina219_gpio_init();
  
  ina219_configureRegisters();
}


signed short ina219_GetBusVoltage_raw(void)
{
  signed short val;
  
  ina219_Read_Register(INA219_REG_BUSVOLTAGE, &val);
  val >>= 3;                      // Shift to the right 3 to drop CNVR and OVF
  
  return (val);
}
 
signed short ina219_GetCurrent_raw(void)
{
  signed short val;
  
  // Sometimes a sharp load will reset the INA219, which will
  // reset the cal register, meaning CURRENT and POWER will
  // not be available ... avoid this by always setting a cal
  // value even if it's an unfortunate extra step
  ina219_Write_Register(INA219_REG_CALIBRATION, ina219_calValue);
  
  // Now we can safely read the CURRENT register!
  ina219_Read_Register(INA219_REG_CURRENT, &val);
  
  return (val);
}
 
 
signed short ina219_GetBusVoltage_mV(void)
{
  signed short val;
  
  ina219_Read_Register(INA219_REG_BUSVOLTAGE, &val);
  val >>= 3;                      // Shift to the right 3 to drop CNVR and OVF
  val *= ina219_busVolt_LSB_mV;   // multiply by LSB(4mV)
  
  return (val);
}
 
s32 ina219_GetShuntVoltage_uV(void)
{
  s32 val;
  s16 reg;
  
  ina219_Read_Register(INA219_REG_SHUNTVOLTAGE, &reg);
  val = (s32)reg * ina219_shuntVolt_LSB_uV;   // multiply by LSB(10uV)
  
  return (val);
}
 
s32 ina219_GetCurrent_uA(void)
{
  s32 val;
  s16 reg;
  
  // Sometimes a sharp load will reset the INA219, which will
  // reset the cal register, meaning CURRENT and POWER will
  // not be available ... avoid this by always setting a cal
  // value even if it's an unfortunate extra step
  ina219_Write_Register(INA219_REG_CALIBRATION, ina219_calValue);
  
  // Now we can safely read the CURRENT register!
  ina219_Read_Register(INA219_REG_CURRENT, &reg);
  
  val = (s32)reg * ina219_current_LSB_uA;
  
  return (val);
}
 
s32 ina219_GetPower_mW(void)
{
  s32 val;
  s16 reg;
  
  // Sometimes a sharp load will reset the INA219, which will
  // reset the cal register, meaning CURRENT and POWER will
  // not be available ... avoid this by always setting a cal
  // value even if it's an unfortunate extra step
  ina219_Write_Register(INA219_REG_CALIBRATION, ina219_calValue);
  
  // Now we can safely read the POWER register!
  ina219_Read_Register(INA219_REG_POWER, &reg);
  
  val = (s32)reg * ina219_power_LSB_mW;
  
  return (val);
}
 
void INA_Process(void)
{
    if(1)
    {
        //INA219process_flag = Close;
        
        ina219_data.voltage_ina219 = ina219_GetBusVoltage_mV();
      //  printf("voltage_ina219 is %d\r\n",ina219_data.voltage_ina219);
        
        ina219_data.shunt_ina219 = ina219_GetShuntVoltage_uV();
       // printf("shunt_ina219 is %ld\r\n",ina219_data.shunt_ina219);
            
        ina219_data.current_ina219 = ina219_GetCurrent_uA();
       // printf("current_ina219 is %ld\r\n",ina219_data.current_ina219);
        
        ina219_data.power_ina219 = ina219_GetPower_mW();
      //  printf("power_ina219 is %ld\r\n",ina219_data.power_ina219);
				
    }
}
#ifndef __INA219_H
#define __INA219_H
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"

#define INA219_I2C_PORT                        GPIOB
#define INA219_I2C_GPIO_CLOCK            RCC_APB2Periph_GPIOB
#define INA219_I2C_SCL_PIN                GPIO_Pin_13
#define INA219_I2C_SDA_PIN                GPIO_Pin_12
 
#define INA_SCL_SET     GPIO_SetBits(INA219_I2C_PORT,INA219_I2C_SCL_PIN)
#define INA_SDA_SET     GPIO_SetBits(INA219_I2C_PORT, INA219_I2C_SDA_PIN)
 
#define INA_SCL_CLR     GPIO_ResetBits(INA219_I2C_PORT,INA219_I2C_SCL_PIN)
#define INA_SDA_CLR     GPIO_ResetBits(INA219_I2C_PORT,INA219_I2C_SDA_PIN)
 
#define INA_SDA_TST     GPIO_ReadInputDataBit(INA219_I2C_PORT,INA219_I2C_SDA_PIN)
 
/*----------------------------------------------------------------------------*/
// I2C Address Options
#define INA219_I2C_ADDRESS_CONF_0               (u8)(0x40 << 1)     // A0 = GND, A1 = GND
#define INA219_I2C_ADDRESS_CONF_1               (u8)(0x41 << 1)     // A0 = VS+, A1 = GND
#define INA219_I2C_ADDRESS_CONF_2               (u8)(0x42 << 1)     // A0 = SDA, A1 = GND
#define INA219_I2C_ADDRESS_CONF_3               (u8)(0x43 << 1)     // A0 = SCL, A1 = GND
#define INA219_I2C_ADDRESS_CONF_4               (u8)(0x44 << 1)     // A0 = GND, A1 = VS+
#define INA219_I2C_ADDRESS_CONF_5               (u8)(0x45 << 1)     // A0 = VS+, A1 = VS+
#define INA219_I2C_ADDRESS_CONF_6               (u8)(0x46 << 1)     // A0 = SDA, A1 = VS+
#define INA219_I2C_ADDRESS_CONF_7               (u8)(0x47 << 1)     // A0 = SCL, A1 = VS+
#define INA219_I2C_ADDRESS_CONF_8               (u8)(0x48 << 1)     // A0 = GND, A1 = SDA
#define INA219_I2C_ADDRESS_CONF_9               (u8)(0x49 << 1)     // A0 = VS+, A1 = SDA
#define INA219_I2C_ADDRESS_CONF_A               (u8)(0x4A << 1)     // A0 = SDA, A1 = SDA
#define INA219_I2C_ADDRESS_CONF_B               (u8)(0x4B << 1)     // A0 = SCL, A1 = SDA
#define INA219_I2C_ADDRESS_CONF_C               (u8)(0x4C << 1)     // A0 = GND, A1 = SCL
#define INA219_I2C_ADDRESS_CONF_D               (u8)(0x4D << 1)     // A0 = VS+, A1 = SCL
#define INA219_I2C_ADDRESS_CONF_E               (u8)(0x4E << 1)     // A0 = SDA, A1 = SCL
#define INA219_I2C_ADDRESS_CONF_F               (u8)(0x4F << 1)     // A0 = SCL, A1 = SCL
#define INA219_I2C_ADDRESS                      INA219_I2C_ADDRESS_CONF_0
 
 
/*----------------------------------------------------------------------------*/
// Register Addresses
#define INA219_REG_CONFIG                       (u8)(0x00)      // CONFIG REGISTER (R/W)
#define INA219_REG_SHUNTVOLTAGE                 (u8)(0x01)      // SHUNT VOLTAGE REGISTER (R)
#define INA219_REG_BUSVOLTAGE                   (u8)(0x02)      // BUS VOLTAGE REGISTER (R)
#define INA219_REG_POWER                        (u8)(0x03)      // POWER REGISTER (R)
#define INA219_REG_CURRENT                      (u8)(0x04)      // CURRENT REGISTER (R)
#define INA219_REG_CALIBRATION                  (u8)(0x05)      // CALIBRATION REGISTER (R/W)
 
 
/*----------------------------------------------------------------------------*/
// Macros for assigning config bits
#define INA219_CFGB_RESET(x)                    (u16)((x & 0x01) << 15)     // Reset Bit
#define INA219_CFGB_BUSV_RANGE(x)               (u16)((x & 0x01) << 13)     // Bus Voltage Range
#define INA219_CFGB_PGA_RANGE(x)                (u16)((x & 0x03) << 11)     // Shunt Voltage Range
#define INA219_CFGB_BADC_RES_AVG(x)             (u16)((x & 0x0F) << 7)      // Bus ADC Resolution/Averaging
#define INA219_CFGB_SADC_RES_AVG(x)             (u16)((x & 0x0F) << 3)      // Shunt ADC Resolution/Averaging
#define INA219_CFGB_MODE(x)                     (u16) (x & 0x07)            // Operating Mode
 
 
/*----------------------------------------------------------------------------*/
// Configuration Register
#define INA219_CFG_RESET                        INA219_CFGB_RESET(1)            // Reset Bit
 
#define INA219_CFG_BVOLT_RANGE_MASK             INA219_CFGB_BUSV_RANGE(1)       // Bus Voltage Range Mask
#define INA219_CFG_BVOLT_RANGE_16V              INA219_CFGB_BUSV_RANGE(0)       // 0-16V Range
#define INA219_CFG_BVOLT_RANGE_32V              INA219_CFGB_BUSV_RANGE(1)       // 0-32V Range (default)
 
#define INA219_CFG_SVOLT_RANGE_MASK             INA219_CFGB_PGA_RANGE(3)        // Shunt Voltage Range Mask
#define INA219_CFG_SVOLT_RANGE_40MV             INA219_CFGB_PGA_RANGE(0)        // Gain 1, 40mV Range
#define INA219_CFG_SVOLT_RANGE_80MV             INA219_CFGB_PGA_RANGE(1)        // Gain 2, 80mV Range
#define INA219_CFG_SVOLT_RANGE_160MV            INA219_CFGB_PGA_RANGE(2)        // Gain 4, 160mV Range
#define INA219_CFG_SVOLT_RANGE_320MV            INA219_CFGB_PGA_RANGE(3)        // Gain 8, 320mV Range (default)
 
#define INA219_CFG_BADCRES_MASK                 INA219_CFGB_BADC_RES_AVG(15)    // Bus ADC Resolution and Averaging Mask
#define INA219_CFG_BADCRES_9BIT_1S_84US         INA219_CFGB_BADC_RES_AVG(0)     // 1 x 9-bit Bus sample
#define INA219_CFG_BADCRES_10BIT_1S_148US       INA219_CFGB_BADC_RES_AVG(1)     // 1 x 10-bit Bus sample
#define INA219_CFG_BADCRES_11BIT_1S_276US       INA219_CFGB_BADC_RES_AVG(2)     // 1 x 11-bit Bus sample
#define INA219_CFG_BADCRES_12BIT_1S_532US       INA219_CFGB_BADC_RES_AVG(3)     // 1 x 12-bit Bus sample (default)
#define INA219_CFG_BADCRES_12BIT_2S_1MS         INA219_CFGB_BADC_RES_AVG(9)     // 2 x 12-bit Bus samples averaged together
#define INA219_CFG_BADCRES_12BIT_4S_2MS         INA219_CFGB_BADC_RES_AVG(10)    // 4 x 12-bit Bus samples averaged together
#define INA219_CFG_BADCRES_12BIT_8S_4MS         INA219_CFGB_BADC_RES_AVG(11)    // 8 x 12-bit Bus samples averaged together
#define INA219_CFG_BADCRES_12BIT_16S_8MS        INA219_CFGB_BADC_RES_AVG(12)    // 16 x 12-bit Bus samples averaged together
#define INA219_CFG_BADCRES_12BIT_32S_17MS       INA219_CFGB_BADC_RES_AVG(13)    // 32 x 12-bit Bus samples averaged together
#define INA219_CFG_BADCRES_12BIT_64S_34MS       INA219_CFGB_BADC_RES_AVG(14)    // 64 x 12-bit Bus samples averaged together
#define INA219_CFG_BADCRES_12BIT_128S_68MS      INA219_CFGB_BADC_RES_AVG(15)    // 128 x 12-bit Bus samples averaged together
 
#define INA219_CFG_SADCRES_MASK                 INA219_CFGB_SADC_RES_AVG(15)    // Shunt ADC Resolution and Averaging Mask
#define INA219_CFG_SADCRES_9BIT_1S_84US         INA219_CFGB_SADC_RES_AVG(0)     // 1 x 9-bit Shunt sample
#define INA219_CFG_SADCRES_10BIT_1S_148US       INA219_CFGB_SADC_RES_AVG(1)     // 1 x 10-bit Shunt sample
#define INA219_CFG_SADCRES_11BIT_1S_276US       INA219_CFGB_SADC_RES_AVG(2)     // 1 x 11-bit Shunt sample
#define INA219_CFG_SADCRES_12BIT_1S_532US       INA219_CFGB_SADC_RES_AVG(3)     // 1 x 12-bit Shunt sample (default)
#define INA219_CFG_SADCRES_12BIT_2S_1MS         INA219_CFGB_SADC_RES_AVG(9)     // 2 x 12-bit Shunt samples averaged together
#define INA219_CFG_SADCRES_12BIT_4S_2MS         INA219_CFGB_SADC_RES_AVG(10)    // 4 x 12-bit Shunt samples averaged together
#define INA219_CFG_SADCRES_12BIT_8S_4MS         INA219_CFGB_SADC_RES_AVG(11)    // 8 x 12-bit Shunt samples averaged together
#define INA219_CFG_SADCRES_12BIT_16S_8MS        INA219_CFGB_SADC_RES_AVG(12)    // 16 x 12-bit Shunt samples averaged together
#define INA219_CFG_SADCRES_12BIT_32S_17MS       INA219_CFGB_SADC_RES_AVG(13)    // 32 x 12-bit Shunt samples averaged together
#define INA219_CFG_SADCRES_12BIT_64S_34MS       INA219_CFGB_SADC_RES_AVG(14)    // 64 x 12-bit Shunt samples averaged together
#define INA219_CFG_SADCRES_12BIT_128S_68MS      INA219_CFGB_SADC_RES_AVG(15)    // 128 x 12-bit Shunt samples averaged together
 
#define INA219_CFG_MODE_MASK                    INA219_CFGB_MODE(7)             // Operating Mode Mask
#define INA219_CFG_MODE_POWERDOWN               INA219_CFGB_MODE(0)             // Power-Down
#define INA219_CFG_MODE_SVOLT_TRIGGERED         INA219_CFGB_MODE(1)             // Shunt Voltage, Triggered
#define INA219_CFG_MODE_BVOLT_TRIGGERED         INA219_CFGB_MODE(2)             // Bus Voltage, Triggered
#define INA219_CFG_MODE_SANDBVOLT_TRIGGERED     INA219_CFGB_MODE(3)             // Shunt and Bus, Triggered
#define INA219_CFG_MODE_ADCOFF                  INA219_CFGB_MODE(4)             // ADC Off (disabled)
#define INA219_CFG_MODE_SVOLT_CONTINUOUS        INA219_CFGB_MODE(5)             // Shunt Voltage, Continuous
#define INA219_CFG_MODE_BVOLT_CONTINUOUS        INA219_CFGB_MODE(6)             // Bus Voltage, Continuous
#define INA219_CFG_MODE_SANDBVOLT_CONTINUOUS    INA219_CFGB_MODE(7)             // Shunt and Bus, Continuous (default)
 
 
/*----------------------------------------------------------------------------*/
// Bus Voltage Register
#define INA219_BVOLT_CNVR                       (u16)(0x0002)       // Conversion Ready
#define INA219_BVOLT_OVF                        (u16)(0x0001)       // Math Overflow Flag
 
typedef struct
{
  signed short voltage_ina219;
  signed long shunt_ina219;
  signed long current_ina219;
  signed long power_ina219;
}INA219_DATA;
 
 
extern u8  ina219_busVolt_LSB_mV;
extern u8  ina219_shuntVolt_LSB_uV;
extern unsigned short ina219_calValue;
 
extern u32 ina219_current_LSB_uA;
extern u32 ina219_power_LSB_mW;
 
extern void ina219_init(void);
extern void INA_Process(void);
extern signed short ina219_GetBusVoltage_raw(void);
extern signed short ina219_GetCurrent_raw(void);
extern signed short ina219_GetBusVoltage_mV(void);
extern s32 ina219_GetShuntVoltage_uV(void);
extern s32 ina219_GetCurrent_uA(void);
extern s32 ina219_GetPower_mW(void);
 
#endif

到了这里,关于INA219例程,可校准电流值误差(基于stm32)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【GD32F427开发板试用】INA226完成电流电压采集

    本篇文章来自极术社区与兆易创新组织的GD32F427开发板评测活动,更多开发板试用活动请关注极术社区网站。作者: จุ๊บ冰语 本次有幸参与并通过了极术社区组织的【GD32F427开发板试用】活动,让我对国产兆易创新的GD32处理器有了更深刻的认识。 开发板到手后,先从邮

    2024年02月02日
    浏览(36)
  • 用浅薄的模电知识对INA199系列差放关于电流采样的说明

    (6条消息) 通过差分电路和采样电阻对电流进行采集----基于INA199_懒人在行动的博客-CSDN博客  他这玩意儿什么意思呢?首先R1就是那图里的1MΩ(datasheet里可知),R3即输入电阻,INA199X1/X2/X3几个型号的输入电阻是不一样的,所以R1/R3值也不一样,这个比值实际上就是这个型号的

    2024年02月16日
    浏览(38)
  • 基于长短期神经网络LSTM的测量误差预测

    目录 背影 摘要 代码和数据下载:基于长短期神经网络LSTM的测量误差预测(代码完整,数据齐全)资源-CSDN文库 https://download.csdn.net/download/abc991835105/88714812 LSTM的基本定义 LSTM实现的步骤 基于长短期神经网络LSTM的测量误差预测 结果分析 展望 参考论文 测量误差有一定的时间或

    2024年01月23日
    浏览(38)
  • 基于STM32电压检测和电流检测

    CPU: STM32F103C8 屏幕: 0.96寸OLED屏幕(SPI接口) 电压测量模块: INA226(IIC接口) 点流测量模块: ACS712(ADC采集) 1.可测量直流电压0~36V,适用于低电压电子电路中。 2.可测量直流电0~5A范围内,目前采用的ACS712测量量程为5A,该模块有多个量程,可测量到20A 3.实时功率监测 4.电池电量监测

    2024年02月02日
    浏览(45)
  • 干货 | 成本低误差小,携程基于 Kafka 的 Serverless 延迟队列的实践

    作者简介 Pin,关注 RPC、Service Mesh、Serverless 等云原生技术。 一、背景 随着上云项目的不断推进,大量的应用需要部署到 aws 上,其中有很多应用都依赖延迟队列的功能。而在 aws 上,我们选择以 Kafka 作为消息队列,但是 Kafka 本身不支持延迟队列,这就需要思考如何基于

    2024年02月13日
    浏览(45)
  • 【opencv】示例-stereo_calib.cpp 基于OpenCV的立体视觉相机校准的完整示例

    这段代码是一个用于执行立体视觉系统校准的应用程序的主函数main。它按以下步骤执行: 初始化用于指定棋盘尺寸、图像列表文件名、是否展示校正结果等参数的变量。 解析命令行输入的参数,其中包括棋盘的宽度、高度、类型、格子大小、Aruco标记大小、Aruco字典名称、额

    2024年04月15日
    浏览(43)
  • 基于51单片机和proteus的电流采集系统

    此系统是基于51单片机和proteus的仿真设计,功能如下: 1. LCD1602实时显示获取到电流值及设定值。 2. 按键可调整电流设定值。 3. 电流值过高则蜂鸣器报警。 4. 指示灯指示电流及系统状态。 5. 系统信息可通过串口实时更新。 功能框图如下: Proteus仿真界面如下: 下面就各个模

    2024年02月16日
    浏览(39)
  • 基于插值算法和Gardner定时误差检测的OOK信号定时同步的FPGA实现

      本文介绍如何用FPGA实现基于插值算法的OOK信号定时同步,Verilog代码参考杜勇《数字调制解调技术的MATLAB与FPGA实现》。我们的目标是用外部提供50MHz时钟的zynq7100芯片实现400MHz采样频率和100Mbps的OOK数字基带信号的定时同步。   采用传统的锁相环技术实现定时同步时,本

    2024年02月16日
    浏览(38)
  • 基于FPGA的SRIOIP例程及仿真实现

    一、IP 创建及相关介绍         首先创建一个工程,选择相应的FPGA器件,在左边选择IP Catalog来创建SRIO IP核,现在使用的是V4.1版本的IP核,双击进入SRIO 进行设置;         设置IP时有两种模式可以选择,一种是Advance模式,一种是Basic模式,在Advance模式下可以对几个相

    2024年02月09日
    浏览(37)
  • SPI通信协议及基于Arduino的SPI通信例程

    一、SPI通信协议介绍 SPI通信协议(Serial Peripheral Interface)是一种同步串行通信协议,由Motorola公司在1980年代初开发。SPI协议常用于单片机、嵌入式系统和外围设备之间的通信。 SPI协议使用四根线进行通信:时钟线(SCLK)、数据输入线(MOSI)、数据输出线(MISO)和从机选择

    2024年02月07日
    浏览(39)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包