查看: 4817|回复: 2

[原创] LPC1768与INA219电流功率监测计

[复制链接]
  • TA的每日心情
    开心
    7 天前
  • 签到天数: 1503 天

    连续签到: 1 天

    [LV.Master]伴坛终老

    97

    主题

    4691

    帖子

    12

    版主

    Rank: 7Rank: 7Rank: 7

    积分
    10088
    最后登录
    2025-7-29
    发表于 2019-11-10 12:39:40 | 显示全部楼层 |阅读模式
    INA219 是一款具备I2C 或SMBUS 兼容接口的分流器和功率监测计。该器件监测分流器电压降和总线电源电压,转换次数和滤波选项可通过编程设定。可编程校准值与内部乘法器相结合,支持直接读取电流值(单位:安培)。通过附加乘法寄存器可计算功率(单位:瓦)。I2C 或SMBUS 兼容接口具有16 个可编程地址。
    INA219 可在0V 至26V 范围内感测总线中的分压。该器件由3V 至5.5V 单电源供电,电源的最大流耗为1mA。
    芯片同是支持差分分压电流检测跟总线电压检测,芯片外部引脚A0、A1引脚,支持支持16路地址设置,同时可挂载16路设备。
    INA219内部只有6个寄存器,需要配置的寄存器仅有两个。
    configuration寄存器,其配置芯片的工作状态与工作参数。
    calibration寄存器,其校正芯片的采样数据。
    其余的寄存器为只读寄存器,shunt电压,bus电压,功率,电流。
    对其编程就显得非常简单了,仅需要通过I2C总线,首先配置芯片,然后再读取对应的寄存器值即可。
    该芯片电路简洁,工作稳定,价格实惠。
    源代码如下:

    1. #include <stdint.h>
    2. #include "bspI2C.h"
    3. #include "devINA219.h"

    4. struct tINA219Class charli219 = {
    5.         .bus = 0,
    6.         .address = INA219_ADDRESS,
    7. };

    8. int8 ina219_init(tINA219Class *dev)
    9. {
    10.         int8_t ret = 0;

    11.         tI2CMasterClass i2c_dev;
    12.         i2c_dev.busNum = dev->bus;
    13.         i2c_dev.salveAddr = dev->address;

    14.         ret = openI2C(&i2c_dev, busWrite);
    15.         if(ret != 0)
    16.         {
    17. //                printf("i2c bus busy...\n");
    18.                 return (1);
    19.         }
    20.         writeI2c(&i2c_dev, INA219_REG_CONFIG, INA219_CONFIG_value);
    21. }

    22. int8_t ina219_read_voltage(tINA219Class *dev, int16_t *value_ptr)
    23. {
    24.         int8_t ret = 0;
    25.         uint8_t mbuf[2];
    26.         tI2CMasterClass i2c_dev;
    27.         i2c_dev.busNum = dev->bus;
    28.         i2c_dev.salveAddr = dev->address;

    29.         ret = wrReadI2c(&i2c_dev, INA219_REG_BUSVOLTAGE, mbuf, 2);
    30.         *value_ptr = (((mbuf[0] << 5) + mbuf[1]) >> 3) * 4;

    31.         return (ret);
    32. }
    复制代码
    头文件的源代码:

    1. #ifndef MIDWARE_DEVINA219_H_
    2. #define MIDWARE_DEVINA219_H_

    3. #include <stdint.h>

    4. #define INA219_ADDRESS  (0x40 << 1)     // A0 = GND, A1 = GND

    5. #define INA219_REG_CONFIG                       (0x00)
    6. #define INA219_REG_SHUNTVOLTAGE                 (0x01)
    7. #define INA219_REG_BUSVOLTAGE                   (0x02)
    8. #define INA219_REG_POWER                        (0x03)
    9. #define INA219_REG_CURRENT                      (0x04)
    10. #define INA219_REG_CALIBRATION                  (0x05)

    11. #define INA219_CONFIG_RESET                    (0x8000)  // Reset Bit

    12. #define INA219_CONFIG_BVOLTAGERANGE_MASK       (0x2000)  // Bus Voltage Range Mask
    13. #define INA219_CONFIG_BVOLTAGERANGE_16V        (0x0000)  // 0-16V Range
    14. #define INA219_CONFIG_BVOLTAGERANGE_32V        (0x2000)  // 0-32V Range

    15. #define INA219_CONFIG_GAIN_MASK                (0x1800)  // Gain Mask
    16. #define INA219_CONFIG_GAIN_1_40MV              (0x0000)  // Gain 1, 40mV Range
    17. #define INA219_CONFIG_GAIN_2_80MV              (0x0800)  // Gain 2, 80mV Range
    18. #define INA219_CONFIG_GAIN_4_160MV             (0x1000)  // Gain 4, 160mV Range
    19. #define INA219_CONFIG_GAIN_8_320MV             (0x1800)  // Gain 8, 320mV Range

    20. #define INA219_CONFIG_BADCRES_MASK             (0x0780)  // Bus ADC Resolution Mask
    21. #define INA219_CONFIG_BADCRES_9BIT             (0x0080)  // 9-bit bus res = 0..511
    22. #define INA219_CONFIG_BADCRES_10BIT            (0x0100)  // 10-bit bus res = 0..1023
    23. #define INA219_CONFIG_BADCRES_11BIT            (0x0200)  // 11-bit bus res = 0..2047
    24. #define INA219_CONFIG_BADCRES_12BIT            (0x0400)  // 12-bit bus res = 0..4097

    25. #define INA219_CONFIG_SADCRES_MASK             (0x0078)  // Shunt ADC Resolution and Averaging Mask
    26. #define INA219_CONFIG_SADCRES_9BIT_1S_84US     (0x0000)  // 1 x 9-bit shunt sample
    27. #define INA219_CONFIG_SADCRES_10BIT_1S_148US   (0x0008)  // 1 x 10-bit shunt sample
    28. #define INA219_CONFIG_SADCRES_11BIT_1S_276US   (0x0010)  // 1 x 11-bit shunt sample
    29. #define INA219_CONFIG_SADCRES_12BIT_1S_532US   (0x0018)  // 1 x 12-bit shunt sample
    30. #define INA219_CONFIG_SADCRES_12BIT_2S_1060US  (0x0048)         // 2 x 12-bit shunt samples averaged together
    31. #define INA219_CONFIG_SADCRES_12BIT_4S_2130US  (0x0050)  // 4 x 12-bit shunt samples averaged together
    32. #define INA219_CONFIG_SADCRES_12BIT_8S_4260US  (0x0058)  // 8 x 12-bit shunt samples averaged together
    33. #define INA219_CONFIG_SADCRES_12BIT_16S_8510US (0x0060)  // 16 x 12-bit shunt samples averaged together
    34. #define INA219_CONFIG_SADCRES_12BIT_32S_17MS   (0x0068)  // 32 x 12-bit shunt samples averaged together
    35. #define INA219_CONFIG_SADCRES_12BIT_64S_34MS   (0x0070)  // 64 x 12-bit shunt samples averaged together
    36. #define INA219_CONFIG_SADCRES_12BIT_128S_69MS  (0x0078)  // 128 x 12-bit shunt samples averaged together

    37. #define INA219_CONFIG_MODE_MASK                (0x0007)  // Operating Mode Mask
    38. #define INA219_CONFIG_MODE_POWERDOWN           (0x0000)
    39. #define INA219_CONFIG_MODE_SVOLT_TRIGGERED     (0x0001)
    40. #define INA219_CONFIG_MODE_BVOLT_TRIGGERED     (0x0002)
    41. #define INA219_CONFIG_MODE_SANDBVOLT_TRIGGERED (0x0003)
    42. #define INA219_CONFIG_MODE_ADCOFF              (0x0004)
    43. #define INA219_CONFIG_MODE_SVOLT_CONTINUOUS    (0x0005)
    44. #define INA219_CONFIG_MODE_BVOLT_CONTINUOUS    (0x0006)
    45. #define INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS (0x0007)

    46. #define INA_R                                         (0.1)
    47. #define INA_I_MAX                                (3)
    48. #define IAN_I_LSB                                (0.1)
    49. #define INA_Power_LSB         (2)
    50. #define INA_CAL                                 (4096)

    51. #define INA219_CONFIG_value        INA219_CONFIG_BVOLTAGERANGE_32V|INA219_CONFIG_GAIN_8_320MV|INA219_CONFIG_BADCRES_12BIT|INA219_CONFIG_SADCRES_12BIT_1S_532US|INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS

    52. struct tINA219Class
    53. {
    54.         uint8_t bus;
    55.         uint8_t address;
    56. };

    57. #endif /* MIDWARE_DEVINA219_H_ */
    复制代码



    其余采样量只是替换寄存器而已,在这里不再赘述。
    提醒一下,对于其余值的数据,符号位的整理也是非常重要,尤其要测试,
    该会员没有填写今日想说内容.
    回复

    使用道具 举报

    该用户从未签到

    714

    主题

    6373

    帖子

    0

    超级版主

    Rank: 8Rank: 8

    积分
    25053
    最后登录
    2025-8-5
    发表于 2019-11-11 09:34:09 | 显示全部楼层
    感谢分享!!
    回复

    使用道具 举报

  • TA的每日心情
    开心
    2020-12-18 12:56
  • 签到天数: 55 天

    连续签到: 1 天

    [LV.5]常住居民I

    114

    主题

    288

    帖子

    0

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    2031
    最后登录
    2025-8-4
    发表于 2019-11-18 17:01:42 | 显示全部楼层
    学习学习
    签到
    回复

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /3 下一条

    Archiver|手机版|小黑屋|恩智浦技术社区

    GMT+8, 2025-8-5 15:13 , Processed in 0.081124 second(s), 20 queries , MemCache On.

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.

    快速回复 返回顶部 返回列表