你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

【经验分享】INA219驱动,基于STM32(STM8移植可用)

[复制链接]
STMCU小助手 发布时间:2022-2-8 21:06
之前要做一个电量计,采用INA219电流检测芯片,参考了网上大神的代码后发现在STM32上可用,移植到STM8后不可用,后来找到了官方的示例demo,综合网上大神代码调试后成功驱动,测电流、电压、功率,精确度很高。
现在分享出来供大家参考,直接贴代码:
main.c
  1. int main(void)
  2. {
  3.     ina219_init();
  4.    
  5.     while(1)
  6.    {  
  7.         //根据具体需求调用检测电流电压的函数
  8.    }  
  9. }
复制代码

ina219.c
  1. #include "ina219.h"

  2. u8  ina219_busVolt_LSB_mV = 4;   // Bus Voltage LSB value = 4mV
  3. u8  ina219_shuntVolt_LSB_uV = 10;  // Shunt Voltage LSB value = 10uV
  4. unsigned short ina219_calValue = 0;

  5. u32 ina219_current_LSB_uA;
  6. u32 ina219_power_LSB_mW;

  7. INA219_DATA ina219_data;

  8. void INA_SCL_OUT(void)
  9. {
  10.     GPIO_InitTypeDef  GPIO_InitStructure;

  11.     RCC_APB2PeriphClockCmd(INA219_I2C_GPIO_CLOCK, ENABLE);
  12.    
  13.     /* Configure I2C1 pins: PB12->SCL->OUT */
  14.     GPIO_InitStructure.GPIO_Pin = INA219_I2C_SCL_PIN;
  15.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  16.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  17.     GPIO_Init(INA219_I2C_PORT, &GPIO_InitStructure);
  18.     GPIO_SetBits(INA219_I2C_PORT, INA219_I2C_SCL_PIN);
  19. }

  20. void INA_SDA_OUT(void)
  21. {
  22.     GPIO_InitTypeDef  GPIO_InitStructure;

  23.     RCC_APB2PeriphClockCmd(INA219_I2C_GPIO_CLOCK, ENABLE);
  24.    
  25.     /* Configure I2C1 pins: PB14->SDA-OUT */
  26.     GPIO_InitStructure.GPIO_Pin = INA219_I2C_SDA_PIN;
  27.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  28.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  29.     GPIO_Init(INA219_I2C_PORT, &GPIO_InitStructure);
  30.     GPIO_SetBits(INA219_I2C_PORT, INA219_I2C_SDA_PIN);
  31. }

  32. void INA_SDA_IN(void)
  33. {
  34.     GPIO_InitTypeDef  GPIO_InitStructure;

  35.     RCC_APB2PeriphClockCmd(INA219_I2C_GPIO_CLOCK, ENABLE);
  36.    
  37.     /* Configure I2C1 pins: PB14->SDA-IN */
  38.     GPIO_InitStructure.GPIO_Pin = INA219_I2C_SDA_PIN;
  39.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  40.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  41.     GPIO_Init(INA219_I2C_PORT, &GPIO_InitStructure);
  42. }

  43. void INA_IIC_Start(void)
  44. {
  45.   INA_SDA_OUT();
  46.   INA_SCL_OUT();
  47.   
  48.   INA_SDA_SET;
  49.   INA_SCL_SET;
  50.   INA_SDA_CLR;
  51.   INA_SCL_CLR;
  52. }

  53. void INA_IIC_Stop(void)
  54. {
  55.     INA_SDA_OUT();

  56.     INA_SDA_CLR;
  57.     INA_SCL_SET;
  58.     INA_SDA_SET;
  59. }

  60. void INA_IIC_Set_Ack(unsigned char ack)
  61. {
  62.     INA_SDA_OUT();
  63.    
  64.     if(ack)
  65.     {
  66.       INA_SDA_SET;
  67.     }
  68.     else
  69.     {
  70.       INA_SDA_CLR;
  71.     }
  72.    
  73.     INA_SCL_SET;
  74.     INA_SCL_CLR;
  75. }

  76. unsigned char INA_IIC_Get_Ack(void)
  77. {
  78.     unsigned char ack;

  79.     INA_SDA_IN();
  80.     INA_SDA_SET;
  81.     INA_SCL_SET;
  82.     if(INA_SDA_TST)
  83.     {
  84.       ack = 1;
  85.     }
  86.     else
  87.     {
  88.       ack = 0;
  89.     }
  90.    
  91.     INA_SCL_CLR;

  92.     return(ack);
  93. }

  94. void INA_IIC_Write_8bits(unsigned char dat)
  95. {
  96.   unsigned char i;
  97.   
  98.   INA_SDA_OUT();
  99.   for(i = 8; i; i--)
  100.   {
  101.     if(dat & 0x80)
  102.     {
  103.       INA_SDA_SET;
  104.     }
  105.     else
  106.     {
  107.       INA_SDA_CLR;
  108.     }
  109.    
  110.     INA_SCL_SET;
  111.     dat <<= 1;
  112.     INA_SCL_CLR;
  113.   }
  114. }

  115. unsigned char INA_IIC_Read_8bits(void)
  116. {
  117.     unsigned char i, dat;

  118.     INA_SDA_IN();
  119.     INA_SDA_SET;
  120.     dat = 0;
  121.     for(i = 8; i; i--)
  122.     {
  123.         INA_SCL_SET;
  124.         dat <<= 1;
  125.         if(INA_SDA_TST)
  126.           dat++;
  127.         INA_SCL_CLR;
  128.     }
  129.    
  130.     return(dat);
  131. }

  132. void INA_IIC_Write_Byte(unsigned char reg, unsigned char dat)
  133. {
  134.   unsigned char dev = INA219_I2C_ADDRESS;
  135.   
  136.   INA_IIC_Start();
  137.   
  138.   //  dev &= ~0x01;
  139.   INA_IIC_Write_8bits(dev);
  140.   INA_IIC_Get_Ack();
  141.   
  142.   INA_IIC_Write_8bits(reg);
  143.   INA_IIC_Get_Ack();
  144.   
  145.   INA_IIC_Write_8bits(dat);
  146.   INA_IIC_Get_Ack();
  147.   
  148.   INA_IIC_Stop();
  149. }

  150. unsigned char INA_IIC_Read_Byte(unsigned char reg)
  151. {
  152.   unsigned char dat;
  153.   unsigned char dev = INA219_I2C_ADDRESS;
  154.   
  155.   INA_IIC_Start();
  156.   
  157.   //  dev &= ~0x01;
  158.   INA_IIC_Write_8bits(dev);
  159.   INA_IIC_Get_Ack();
  160.   
  161.   INA_IIC_Write_8bits(reg);
  162.   INA_IIC_Get_Ack();
  163.   
  164.   INA_IIC_Start();
  165.   
  166.   dev |= 0x01;
  167.   INA_IIC_Write_8bits(dev);
  168.   INA_IIC_Get_Ack();
  169.   
  170.   dat = INA_IIC_Read_8bits();
  171.   INA_IIC_Set_Ack(1);
  172.   
  173.   INA_IIC_Stop();
  174.   
  175.   return (dat);
  176. }

  177. void INA_IIC_Write_Bytes(unsigned char reg, unsigned char *dat, unsigned char num)
  178. {
  179.   unsigned char dev = INA219_I2C_ADDRESS;
  180.   
  181.   INA_IIC_Start();
  182.   
  183.   //  dev &= ~0x01;
  184.   INA_IIC_Write_8bits(dev);
  185.   INA_IIC_Get_Ack();
  186.   
  187.   INA_IIC_Write_8bits(reg);
  188.   INA_IIC_Get_Ack();
  189.   
  190.   while(num--)
  191.   {
  192.     INA_IIC_Write_8bits(*dat);
  193.     INA_IIC_Get_Ack();
  194.     dat++;
  195.   }
  196.   
  197.   INA_IIC_Stop();
  198. }

  199. void INA_IIC_Read_Bytes(unsigned char reg, unsigned char *dat, unsigned char num)
  200. {
  201.   unsigned char *tmp = dat;
  202.   unsigned char dev = INA219_I2C_ADDRESS;
  203.   
  204.   INA_IIC_Start();
  205.   
  206.   //  dev &= ~0x01;
  207.   INA_IIC_Write_8bits(dev);
  208.   INA_IIC_Get_Ack();
  209.   
  210.   INA_IIC_Write_8bits(reg);
  211.   INA_IIC_Get_Ack();
  212.   
  213.   INA_IIC_Start();
  214.   
  215.   dev |= 0x01;
  216.   INA_IIC_Write_8bits(dev);
  217.   INA_IIC_Get_Ack();
  218.   
  219.   while(num--)
  220.   {
  221.     *tmp = INA_IIC_Read_8bits();
  222.     if(num == 0)
  223.       INA_IIC_Set_Ack(1);
  224.     else
  225.       INA_IIC_Set_Ack(0);
  226.     tmp++;
  227.   }
  228.   
  229.   INA_IIC_Stop();
  230. }

  231. void ina219_Write_Register(unsigned char reg, unsigned int dat)
  232. {
  233.     unsigned char val[2];
  234.    
  235.     val[0] = (unsigned char)(dat >> 8);
  236.     val[1] = (unsigned char)(dat & 0xFF);
  237.     INA_IIC_Write_Bytes(reg, val, 2);
  238. }

  239. void ina219_Read_Register(unsigned char reg, signed short *dat)
  240. {
  241.     //printf("read reg == %d\r\n",reg);
  242.   unsigned char val[2];
  243.   
  244.   INA_IIC_Read_Bytes(reg, val, 2);
  245.   *dat = ((unsigned int)(val[0]) << 8) + val[1];
  246.   
  247.     //printf("data1 == %x\r\n",val[0]);
  248.     //printf("data2 == %x\r\n",val[1]);
  249.    
  250. }

  251. // INA219 Set Calibration 16V/16A(Max) 0.02¦¸
  252. void ina219_SetCalibration_16V_16A(void)
  253. {
  254.   u16 configValue;
  255.   
  256.   // By default we use a pretty huge range for the input voltage,
  257.   // which probably isn't the most appropriate choice for system
  258.   // that don't use a lot of power.  But all of the calculations
  259.   // are shown below if you want to change the settings.  You will
  260.   // also need to change any relevant register settings, such as
  261.   // setting the VBUS_MAX to 16V instead of 32V, etc.
  262.   
  263.   // VBUS_MAX     = 16V   (Assumes 16V, can also be set to 32V)
  264.   // VSHUNT_MAX   = 0.32  (Assumes Gain 8, 320mV, can also be 0.16, 0.08, 0.04)
  265.   // RSHUNT       = 0.02   (Resistor value in ohms)
  266.   
  267.   // 1. Determine max possible current
  268.   // MaxPossible_I = VSHUNT_MAX / RSHUNT
  269.   // MaxPossible_I = 16A
  270.   
  271.   // 2. Determine max expected current
  272.   // MaxExpected_I = 16A
  273.   
  274.   // 3. Calculate possible range of LSBs (Min = 15-bit, Max = 12-bit)
  275.   // MinimumLSB = MaxExpected_I/32767
  276.   // MinimumLSB = 0.00048            (0.48mA per bit)
  277.   // MaximumLSB = MaxExpected_I/4096
  278.   // MaximumLSB = 0,00390            (3.9mA per bit)
  279.   
  280.   // 4. Choose an LSB between the min and max values
  281.   //    (Preferrably a roundish number close to MinLSB)
  282.   // CurrentLSB = 0.00050            (500uA per bit)
  283.   
  284.   // 5. Compute the calibration register
  285.   // Cal = trunc (0.04096 / (Current_LSB * RSHUNT))
  286.   // Cal = 4096 (0x1000)
  287.   
  288.   ina219_calValue = 0x1000;
  289.   
  290.   // 6. Calculate the power LSB
  291.   // PowerLSB = 20 * CurrentLSB
  292.   // PowerLSB = 0.01 (10mW per bit)
  293.   
  294.   // 7. Compute the maximum current and shunt voltage values before overflow
  295.   //
  296.   // Max_Current = Current_LSB * 32767
  297.   // Max_Current = 16.3835A before overflow
  298.   //
  299.   // If Max_Current > Max_Possible_I then
  300.   //    Max_Current_Before_Overflow = MaxPossible_I
  301.   // Else
  302.   //    Max_Current_Before_Overflow = Max_Current
  303.   // End If
  304.   //
  305.   // Max_ShuntVoltage = Max_Current_Before_Overflow * RSHUNT
  306.   // Max_ShuntVoltage = 0.32V
  307.   //
  308.   // If Max_ShuntVoltage >= VSHUNT_MAX
  309.   //    Max_ShuntVoltage_Before_Overflow = VSHUNT_MAX
  310.   // Else
  311.   //    Max_ShuntVoltage_Before_Overflow = Max_ShuntVoltage
  312.   // End If
  313.   
  314.   // 8. Compute the Maximum Power
  315.   // MaximumPower = Max_Current_Before_Overflow * VBUS_MAX
  316.   // MaximumPower = 1.6 * 16V
  317.   // MaximumPower = 256W
  318.   
  319.   // Set multipliers to convert raw current/power values
  320.   ina219_current_LSB_uA = 500;     // Current LSB = 500uA per bit
  321.   ina219_power_LSB_mW = 10;        // Power LSB = 10mW per bit = 20 * Current LSB
  322.   
  323.   // Set Calibration register to 'Cal' calculated above
  324.   ina219_Write_Register(INA219_REG_CALIBRATION, ina219_calValue);
  325.   
  326.   // Set Config register to take into account the settings above
  327.   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 );
  328.   
  329.   ina219_Write_Register(INA219_REG_CONFIG, configValue);
  330. }

  331. void ina219_configureRegisters(void)
  332. {
  333.   DelayMs(15);
  334.   
  335.   ina219_SetCalibration_16V_16A();
  336. }

  337. void ina219_gpio_init(void)
  338. {
  339.     INA_SCL_OUT();
  340.     INA_SDA_OUT();
  341. }

  342. void ina219_init(void)
  343. {
  344.   ina219_gpio_init();
  345.   
  346.   ina219_configureRegisters();
  347. }


  348. /* ÕâÀïÒÔÉÏÊdzõʼ»¯º¯Êý */
  349. /* ´ÓÕâÀïÍùÏÂÊǹ¦Äܺ¯Êý */


  350. signed short ina219_GetBusVoltage_raw(void)
  351. {
  352.   signed short val;
  353.   
  354.   ina219_Read_Register(INA219_REG_BUSVOLTAGE, &val);
  355.   val >>= 3;                      // Shift to the right 3 to drop CNVR and OVF
  356.   
  357.   return (val);
  358. }

  359. signed short ina219_GetCurrent_raw(void)
  360. {
  361.   signed short val;
  362.   
  363.   // Sometimes a sharp load will reset the INA219, which will
  364.   // reset the cal register, meaning CURRENT and POWER will
  365.   // not be available ... avoid this by always setting a cal
  366.   // value even if it's an unfortunate extra step
  367.   ina219_Write_Register(INA219_REG_CALIBRATION, ina219_calValue);
  368.   
  369.   // Now we can safely read the CURRENT register!
  370.   ina219_Read_Register(INA219_REG_CURRENT, &val);
  371.   
  372.   return (val);
  373. }


  374. signed short ina219_GetBusVoltage_mV(void)
  375. {
  376.   signed short val;
  377.   
  378.   ina219_Read_Register(INA219_REG_BUSVOLTAGE, &val);
  379.   val >>= 3;                      // Shift to the right 3 to drop CNVR and OVF
  380.   val *= ina219_busVolt_LSB_mV;   // multiply by LSB(4mV)
  381.   
  382.   return (val);
  383. }

  384. s32 ina219_GetShuntVoltage_uV(void)
  385. {
  386.   s32 val;
  387.   s16 reg;
  388.   
  389.   ina219_Read_Register(INA219_REG_SHUNTVOLTAGE, ®);
  390.   val = (s32)reg * ina219_shuntVolt_LSB_uV;   // multiply by LSB(10uV)
  391.   
  392.   return (val);
  393. }

  394. s32 ina219_GetCurrent_uA(void)
  395. {
  396.   s32 val;
  397.   s16 reg;
  398.   
  399.   // Sometimes a sharp load will reset the INA219, which will
  400.   // reset the cal register, meaning CURRENT and POWER will
  401.   // not be available ... avoid this by always setting a cal
  402.   // value even if it's an unfortunate extra step
  403.   ina219_Write_Register(INA219_REG_CALIBRATION, ina219_calValue);
  404.   
  405.   // Now we can safely read the CURRENT register!
  406.   ina219_Read_Register(INA219_REG_CURRENT, ®);
  407.   
  408.   val = (s32)reg * ina219_current_LSB_uA;
  409.   
  410.   return (val);
  411. }

  412. s32 ina219_GetPower_mW(void)
  413. {
  414.   s32 val;
  415.   s16 reg;
  416.   
  417.   // Sometimes a sharp load will reset the INA219, which will
  418.   // reset the cal register, meaning CURRENT and POWER will
  419.   // not be available ... avoid this by always setting a cal
  420.   // value even if it's an unfortunate extra step
  421.   ina219_Write_Register(INA219_REG_CALIBRATION, ina219_calValue);
  422.   
  423.   // Now we can safely read the POWER register!
  424.   ina219_Read_Register(INA219_REG_POWER, ®);
  425.   
  426.   val = (s32)reg * ina219_power_LSB_mW;
  427.   
  428.   return (val);
  429. }

  430. void INA_Process(void)
  431. {
  432.     if(INA219process_flag == Open)
  433.     {
  434.         INA219process_flag = Close;
  435.         
  436.         ina219_data.voltage_ina219 = ina219_GetBusVoltage_mV();
  437.         printf("voltage_ina219 is %d\r\n",ina219_data.voltage_ina219);
  438.         
  439.         ina219_data.shunt_ina219 = ina219_GetShuntVoltage_uV();
  440.         printf("shunt_ina219 is %ld\r\n",ina219_data.shunt_ina219);
  441.         
  442.             
  443.         ina219_data.current_ina219 = ina219_GetCurrent_uA();
  444.         printf("current_ina219 is %ld\r\n",ina219_data.current_ina219);
  445.         
  446.         ina219_data.power_ina219 = ina219_GetPower_mW();
  447.         printf("power_ina219 is %ld\r\n",ina219_data.power_ina219);
  448.     }
  449. }
复制代码

ina219.h
  1. #ifndef __INA219_H
  2. #define __INA219_H
  3. #include "main.h"

  4. #define INA219_I2C_PORT                        GPIOB
  5. #define INA219_I2C_GPIO_CLOCK            RCC_APB2Periph_GPIOB
  6. #define INA219_I2C_SCL_PIN                GPIO_Pin_12
  7. #define INA219_I2C_SDA_PIN                GPIO_Pin_14

  8. #define INA_SCL_SET     GPIO_SetBits(INA219_I2C_PORT,INA219_I2C_SCL_PIN)
  9. #define INA_SDA_SET     GPIO_SetBits(INA219_I2C_PORT, INA219_I2C_SDA_PIN)

  10. #define INA_SCL_CLR     GPIO_ResetBits(INA219_I2C_PORT,INA219_I2C_SCL_PIN)
  11. #define INA_SDA_CLR     GPIO_ResetBits(INA219_I2C_PORT,INA219_I2C_SDA_PIN)

  12. #define INA_SDA_TST     GPIO_ReadInputDataBit(INA219_I2C_PORT,INA219_I2C_SDA_PIN)

  13. /*----------------------------------------------------------------------------*/
  14. // I2C Address Options
  15. #define INA219_I2C_ADDRESS_CONF_0               (u8)(0x40 << 1)     // A0 = GND, A1 = GND
  16. #define INA219_I2C_ADDRESS_CONF_1               (u8)(0x41 << 1)     // A0 = VS+, A1 = GND
  17. #define INA219_I2C_ADDRESS_CONF_2               (u8)(0x42 << 1)     // A0 = SDA, A1 = GND
  18. #define INA219_I2C_ADDRESS_CONF_3               (u8)(0x43 << 1)     // A0 = SCL, A1 = GND
  19. #define INA219_I2C_ADDRESS_CONF_4               (u8)(0x44 << 1)     // A0 = GND, A1 = VS+
  20. #define INA219_I2C_ADDRESS_CONF_5               (u8)(0x45 << 1)     // A0 = VS+, A1 = VS+
  21. #define INA219_I2C_ADDRESS_CONF_6               (u8)(0x46 << 1)     // A0 = SDA, A1 = VS+
  22. #define INA219_I2C_ADDRESS_CONF_7               (u8)(0x47 << 1)     // A0 = SCL, A1 = VS+
  23. #define INA219_I2C_ADDRESS_CONF_8               (u8)(0x48 << 1)     // A0 = GND, A1 = SDA
  24. #define INA219_I2C_ADDRESS_CONF_9               (u8)(0x49 << 1)     // A0 = VS+, A1 = SDA
  25. #define INA219_I2C_ADDRESS_CONF_A               (u8)(0x4A << 1)     // A0 = SDA, A1 = SDA
  26. #define INA219_I2C_ADDRESS_CONF_B               (u8)(0x4B << 1)     // A0 = SCL, A1 = SDA
  27. #define INA219_I2C_ADDRESS_CONF_C               (u8)(0x4C << 1)     // A0 = GND, A1 = SCL
  28. #define INA219_I2C_ADDRESS_CONF_D               (u8)(0x4D << 1)     // A0 = VS+, A1 = SCL
  29. #define INA219_I2C_ADDRESS_CONF_E               (u8)(0x4E << 1)     // A0 = SDA, A1 = SCL
  30. #define INA219_I2C_ADDRESS_CONF_F               (u8)(0x4F << 1)     // A0 = SCL, A1 = SCL
  31. #define INA219_I2C_ADDRESS                      INA219_I2C_ADDRESS_CONF_0


  32. /*----------------------------------------------------------------------------*/
  33. // Register Addresses
  34. #define INA219_REG_CONFIG                       (u8)(0x00)      // CONFIG REGISTER (R/W)
  35. #define INA219_REG_SHUNTVOLTAGE                 (u8)(0x01)      // SHUNT VOLTAGE REGISTER (R)
  36. #define INA219_REG_BUSVOLTAGE                   (u8)(0x02)      // BUS VOLTAGE REGISTER (R)
  37. #define INA219_REG_POWER                        (u8)(0x03)      // POWER REGISTER (R)
  38. #define INA219_REG_CURRENT                      (u8)(0x04)      // CURRENT REGISTER (R)
  39. #define INA219_REG_CALIBRATION                  (u8)(0x05)      // CALIBRATION REGISTER (R/W)


  40. /*----------------------------------------------------------------------------*/
  41. // Macros for assigning config bits
  42. #define INA219_CFGB_RESET(x)                    (u16)((x & 0x01) << 15)     // Reset Bit
  43. #define INA219_CFGB_BUSV_RANGE(x)               (u16)((x & 0x01) << 13)     // Bus Voltage Range
  44. #define INA219_CFGB_PGA_RANGE(x)                (u16)((x & 0x03) << 11)     // Shunt Voltage Range
  45. #define INA219_CFGB_BADC_RES_AVG(x)             (u16)((x & 0x0F) << 7)      // Bus ADC Resolution/Averaging
  46. #define INA219_CFGB_SADC_RES_AVG(x)             (u16)((x & 0x0F) << 3)      // Shunt ADC Resolution/Averaging
  47. #define INA219_CFGB_MODE(x)                     (u16) (x & 0x07)            // Operating Mode


  48. /*----------------------------------------------------------------------------*/
  49. // Configuration Register
  50. #define INA219_CFG_RESET                        INA219_CFGB_RESET(1)            // Reset Bit

  51. #define INA219_CFG_BVOLT_RANGE_MASK             INA219_CFGB_BUSV_RANGE(1)       // Bus Voltage Range Mask
  52. #define INA219_CFG_BVOLT_RANGE_16V              INA219_CFGB_BUSV_RANGE(0)       // 0-16V Range
  53. #define INA219_CFG_BVOLT_RANGE_32V              INA219_CFGB_BUSV_RANGE(1)       // 0-32V Range (default)

  54. #define INA219_CFG_SVOLT_RANGE_MASK             INA219_CFGB_PGA_RANGE(3)        // Shunt Voltage Range Mask
  55. #define INA219_CFG_SVOLT_RANGE_40MV             INA219_CFGB_PGA_RANGE(0)        // Gain 1, 40mV Range
  56. #define INA219_CFG_SVOLT_RANGE_80MV             INA219_CFGB_PGA_RANGE(1)        // Gain 2, 80mV Range
  57. #define INA219_CFG_SVOLT_RANGE_160MV            INA219_CFGB_PGA_RANGE(2)        // Gain 4, 160mV Range
  58. #define INA219_CFG_SVOLT_RANGE_320MV            INA219_CFGB_PGA_RANGE(3)        // Gain 8, 320mV Range (default)

  59. #define INA219_CFG_BADCRES_MASK                 INA219_CFGB_BADC_RES_AVG(15)    // Bus ADC Resolution and Averaging Mask
  60. #define INA219_CFG_BADCRES_9BIT_1S_84US         INA219_CFGB_BADC_RES_AVG(0)     // 1 x 9-bit Bus sample
  61. #define INA219_CFG_BADCRES_10BIT_1S_148US       INA219_CFGB_BADC_RES_AVG(1)     // 1 x 10-bit Bus sample
  62. #define INA219_CFG_BADCRES_11BIT_1S_276US       INA219_CFGB_BADC_RES_AVG(2)     // 1 x 11-bit Bus sample
  63. #define INA219_CFG_BADCRES_12BIT_1S_532US       INA219_CFGB_BADC_RES_AVG(3)     // 1 x 12-bit Bus sample (default)
  64. #define INA219_CFG_BADCRES_12BIT_2S_1MS         INA219_CFGB_BADC_RES_AVG(9)     // 2 x 12-bit Bus samples averaged together
  65. #define INA219_CFG_BADCRES_12BIT_4S_2MS         INA219_CFGB_BADC_RES_AVG(10)    // 4 x 12-bit Bus samples averaged together
  66. #define INA219_CFG_BADCRES_12BIT_8S_4MS         INA219_CFGB_BADC_RES_AVG(11)    // 8 x 12-bit Bus samples averaged together
  67. #define INA219_CFG_BADCRES_12BIT_16S_8MS        INA219_CFGB_BADC_RES_AVG(12)    // 16 x 12-bit Bus samples averaged together
  68. #define INA219_CFG_BADCRES_12BIT_32S_17MS       INA219_CFGB_BADC_RES_AVG(13)    // 32 x 12-bit Bus samples averaged together
  69. #define INA219_CFG_BADCRES_12BIT_64S_34MS       INA219_CFGB_BADC_RES_AVG(14)    // 64 x 12-bit Bus samples averaged together
  70. #define INA219_CFG_BADCRES_12BIT_128S_68MS      INA219_CFGB_BADC_RES_AVG(15)    // 128 x 12-bit Bus samples averaged together

  71. #define INA219_CFG_SADCRES_MASK                 INA219_CFGB_SADC_RES_AVG(15)    // Shunt ADC Resolution and Averaging Mask
  72. #define INA219_CFG_SADCRES_9BIT_1S_84US         INA219_CFGB_SADC_RES_AVG(0)     // 1 x 9-bit Shunt sample
  73. #define INA219_CFG_SADCRES_10BIT_1S_148US       INA219_CFGB_SADC_RES_AVG(1)     // 1 x 10-bit Shunt sample
  74. #define INA219_CFG_SADCRES_11BIT_1S_276US       INA219_CFGB_SADC_RES_AVG(2)     // 1 x 11-bit Shunt sample
  75. #define INA219_CFG_SADCRES_12BIT_1S_532US       INA219_CFGB_SADC_RES_AVG(3)     // 1 x 12-bit Shunt sample (default)
  76. #define INA219_CFG_SADCRES_12BIT_2S_1MS         INA219_CFGB_SADC_RES_AVG(9)     // 2 x 12-bit Shunt samples averaged together
  77. #define INA219_CFG_SADCRES_12BIT_4S_2MS         INA219_CFGB_SADC_RES_AVG(10)    // 4 x 12-bit Shunt samples averaged together
  78. #define INA219_CFG_SADCRES_12BIT_8S_4MS         INA219_CFGB_SADC_RES_AVG(11)    // 8 x 12-bit Shunt samples averaged together
  79. #define INA219_CFG_SADCRES_12BIT_16S_8MS        INA219_CFGB_SADC_RES_AVG(12)    // 16 x 12-bit Shunt samples averaged together
  80. #define INA219_CFG_SADCRES_12BIT_32S_17MS       INA219_CFGB_SADC_RES_AVG(13)    // 32 x 12-bit Shunt samples averaged together
  81. #define INA219_CFG_SADCRES_12BIT_64S_34MS       INA219_CFGB_SADC_RES_AVG(14)    // 64 x 12-bit Shunt samples averaged together
  82. #define INA219_CFG_SADCRES_12BIT_128S_68MS      INA219_CFGB_SADC_RES_AVG(15)    // 128 x 12-bit Shunt samples averaged together

  83. #define INA219_CFG_MODE_MASK                    INA219_CFGB_MODE(7)             // Operating Mode Mask
  84. #define INA219_CFG_MODE_POWERDOWN               INA219_CFGB_MODE(0)             // Power-Down
  85. #define INA219_CFG_MODE_SVOLT_TRIGGERED         INA219_CFGB_MODE(1)             // Shunt Voltage, Triggered
  86. #define INA219_CFG_MODE_BVOLT_TRIGGERED         INA219_CFGB_MODE(2)             // Bus Voltage, Triggered
  87. #define INA219_CFG_MODE_SANDBVOLT_TRIGGERED     INA219_CFGB_MODE(3)             // Shunt and Bus, Triggered
  88. #define INA219_CFG_MODE_ADCOFF                  INA219_CFGB_MODE(4)             // ADC Off (disabled)
  89. #define INA219_CFG_MODE_SVOLT_CONTINUOUS        INA219_CFGB_MODE(5)             // Shunt Voltage, Continuous
  90. #define INA219_CFG_MODE_BVOLT_CONTINUOUS        INA219_CFGB_MODE(6)             // Bus Voltage, Continuous
  91. #define INA219_CFG_MODE_SANDBVOLT_CONTINUOUS    INA219_CFGB_MODE(7)             // Shunt and Bus, Continuous (default)


  92. /*----------------------------------------------------------------------------*/
  93. // Bus Voltage Register
  94. #define INA219_BVOLT_CNVR                       (u16)(0x0002)       // Conversion Ready
  95. #define INA219_BVOLT_OVF                        (u16)(0x0001)       // Math Overflow Flag

  96. typedef struct
  97. {
  98.   signed short voltage_ina219;
  99.   signed long shunt_ina219;
  100.   signed long current_ina219;
  101.   signed long power_ina219;
  102. }INA219_DATA;


  103. extern u8  ina219_busVolt_LSB_mV;
  104. extern u8  ina219_shuntVolt_LSB_uV;
  105. extern unsigned short ina219_calValue;

  106. extern u32 ina219_current_LSB_uA;
  107. extern u32 ina219_power_LSB_mW;

  108. extern void ina219_init(void);
  109. extern void INA_Process(void);
  110. extern signed short ina219_GetBusVoltage_raw(void);
  111. extern signed short ina219_GetCurrent_raw(void);
  112. extern signed short ina219_GetBusVoltage_mV(void);
  113. extern s32 ina219_GetShuntVoltage_uV(void);
  114. extern s32 ina219_GetCurrent_uA(void);
  115. extern s32 ina219_GetPower_mW(void);

  116. #endif
复制代码


驱动写好有一段时间,记不太清了,这里就不对代码进行解释了,以免误导别人。官方驱动的代码中有着很详细的注释,此处依然保留。
不同的采样电阻有不同的设定值,校准寄存器具体的计算方法手册中写的很清楚。

收藏 评论0 发布时间:2022-2-8 21:06

举报

0个回答

所属标签

相似分享

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版