cubeMX版本:6.4.0
keil5版本:5.36.0.0
?
打开cube MX,创建工程文件,这里直接跳过。
配置IIC,选择I2C2,(I2C可选,由硬件电路决定)
I2C Speed Mode有两种模式:标准模式(最大100kHz)、快速模式(最大400kHz)
本次使用快速模式,时钟频率400kHz
?
?其他参数保持默认
点击GENERATE CODE,生成代码,工程文件路径不能包含中文
等待代码生成后打开,至此工程创建完成
?为了方便后续使用,为ina219功能创建 ina219.c和ina219.h文件
下载地址:(126条消息) STM32Fx+cubeMX+硬件IIC+INA219(同一个I2C挂载两个芯片)-C文档类资源-CSDN文库https://download.csdn.net/download/qq_28455253/84203175
翻阅ina219芯片手册
?Maximum Expected Current:最大电流,由最大分压电压值和分压电阻阻值决定,本次采用40mv和0.003Ω,因此
Maximum Expected Current = U/R = 40 * 0.001 / 0.003 = 40 / 3 = 13.333A
Current_LSB =?Maximum Expected Current / 2^15 = 4.069?* 10^-4
Cal = 0.04096 / (4.069?* 10^-a * 0.003) = 33555
ina219.c文件
I2C通信函数
HAL_StatusTypeDef ina219_write(uint8_t channel, uint16_t MemAddress, uint16_t value){ ?? ?uint8_t ina219_address = channel==1?INA219_ADDRESS_1 : INA219_ADDRESS_2; ?? ?uint8_t data[2] = {(uint8_t)(value>>8), (uint8_t)(value & 0xFF)}; ?? ?return HAL_I2C_Mem_Write(&hi2c2, ina219_address, MemAddress, I2C_MEMADD_SIZE_8BIT, data, 2, 0x1000); }
HAL_StatusTypeDef ina219_read(uint8_t channel, uint16_t MemAddress, uint8_t *data){ ?? ?uint8_t ina219_address = channel==1?INA219_ADDRESS_1 : INA219_ADDRESS_2; ?? ?return HAL_I2C_Mem_Read(&hi2c2, ina219_address, MemAddress, I2C_MEMADD_SIZE_8BIT, data, 2, 0x1000); }
芯片参数设置函数
void ina219_init(void) { ?? ?uint16_t INA219_CONFIG_value = ?? ?INA219_CONFIG_BVOLTAGERANGE_32V| ?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?INA219_CONFIG_GAIN_1_40MV| ?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?INA219_CONFIG_BADCRES_12BIT| ?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?INA219_CONFIG_SADCRES_12BIT_1S_532US| ?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS; ?? ?ina219_write(1, INA219_REG_CONFIG, INA219_CONFIG_value); ?? ?ina219_write(1, INA219_REG_CALIBRATION, INA_CAL); ?? ?ina219_write(2, INA219_REG_CONFIG, INA219_CONFIG_value); ?? ?ina219_write(2, INA219_REG_CALIBRATION, INA_CAL); }
获取电压、电流、功率函数
double ina219_get_V(uint8_t channel){ ?? ?unsigned char data[2]; ?? ?ina219_read(channel, INA219_REG_BUSVOLTAGE, data); ?? ?int val = (data[0]<<8) + data[1]; ?? ?val >>= 3; ?? ?val *= 4; ?? ?return (double)val / 1000; }
double ina219_get_v(uint8_t channel){ ?? ?unsigned char data[2]; ?? ?ina219_read(channel, INA219_REG_SHUNTVOLTAGE, data); ?? ?int val = (data[0]<<8) + data[1]; ?? ?return (double)val * 0.040 / 32000; }
double ina219_get_A(uint8_t channel){ ?? ?unsigned char data[2]; ?? ?ina219_read(channel, INA219_REG_CURRENT, data); ?? ?int val = (data[0]<<8) + data[1]; ?? ?return (double)val * 0.0004069; }
double ina219_get_W(uint8_t channel){ ?? ?unsigned char data[2]; ?? ?ina219_read(channel, INA219_REG_POWER, data); ?? ?int val = (data[0]<<8) + data[1]; ?? ?return (double)val * 20 * 0.0004069; }
ina219.h文件
#define INA219_ADDRESS_1 ?(0x41 << 1) ? ? // A0 = GND, A1 = GND #define INA219_ADDRESS_2 ?(0x40 << 1) ? ? // A0 = VCC, A1 = GND
// 寄存器地址 #define INA219_REG_CONFIG ? ? ? ? ? ? ? ? ? ? ? (uint8_t)(0x00) ? ? ?// 模式配置寄存器 (R/W) #define INA219_REG_SHUNTVOLTAGE ? ? ? ? ? ? ? ? (uint8_t)(0x01) ? ? ?// 分流电阻电压寄存器 (R) #define INA219_REG_BUSVOLTAGE ? ? ? ? ? ? ? ? ? (uint8_t)(0x02) ? ? ?// 总线电压寄存器 (R) #define INA219_REG_POWER ? ? ? ? ? ? ? ? ? ? ? ?(uint8_t)(0x03) ? ? ?// 功率寄存器 (R) #define INA219_REG_CURRENT ? ? ? ? ? ? ? ? ? ? ?(uint8_t)(0x04) ? ? ?// 电流寄存器 (R) #define INA219_REG_CALIBRATION ? ? ? ? ? ? ? ? ?(uint8_t)(0x05) ? ? ?// 基准值寄存器 (R/W)
//寄存器(0x01)参数定义 #define INA219_CONFIG_RESET ? ? ? ? ? ? ? ? ? ?(0x8000) ?// Reset Bit
#define INA219_CONFIG_BVOLTAGERANGE_MASK ? ? ? (0x2000) ?// Bus Voltage Range Mask #define INA219_CONFIG_BVOLTAGERANGE_16V ? ? ? ?(0x0000) ?// 0-16V Range #define INA219_CONFIG_BVOLTAGERANGE_32V ? ? ? ?(0x2000) ?// 0-32V Range
#define INA219_CONFIG_GAIN_MASK ? ? ? ? ? ? ? ?(0x1800) ?// Gain Mask #define INA219_CONFIG_GAIN_1_40MV ? ? ? ? ? ? ?(0x0000) ?// Gain 1, 40mV Range?? ??? ?//配置检测电阻上的最大检测电压 #define INA219_CONFIG_GAIN_2_80MV ? ? ? ? ? ? ?(0x0800) ?// Gain 2, 80mV Range #define INA219_CONFIG_GAIN_4_160MV ? ? ? ? ? ? (0x1000) ?// Gain 4, 160mV Range #define INA219_CONFIG_GAIN_8_320MV ? ? ? ? ? ? (0x1800) ?// Gain 8, 320mV Range
#define INA219_CONFIG_BADCRES_MASK ? ? ? ? ? ? (0x0780) ?// Bus ADC Resolution Mask #define INA219_CONFIG_BADCRES_9BIT ? ? ? ? ? ? (0x0080) ?// 9-bit bus res = 0..511 #define INA219_CONFIG_BADCRES_10BIT ? ? ? ? ? ?(0x0100) ?// 10-bit bus res = 0..1023 #define INA219_CONFIG_BADCRES_11BIT ? ? ? ? ? ?(0x0200) ?// 11-bit bus res = 0..2047 #define INA219_CONFIG_BADCRES_12BIT ? ? ? ? ? ?(0x0400) ?// 12-bit bus res = 0..4097
#define INA219_CONFIG_SADCRES_MASK ? ? ? ? ? ? (0x0078) ?// Shunt ADC Resolution and Averaging Mask #define INA219_CONFIG_SADCRES_9BIT_1S_84US ? ? (0x0000) ?// 1 x 9-bit shunt sample #define INA219_CONFIG_SADCRES_10BIT_1S_148US ? (0x0008) ?// 1 x 10-bit shunt sample #define INA219_CONFIG_SADCRES_11BIT_1S_276US ? (0x0010) ?// 1 x 11-bit shunt sample #define INA219_CONFIG_SADCRES_12BIT_1S_532US ? (0x0018) ?// 1 x 12-bit shunt sample #define INA219_CONFIG_SADCRES_12BIT_2S_1060US ?(0x0048)?? ? // 2 x 12-bit shunt samples averaged together #define INA219_CONFIG_SADCRES_12BIT_4S_2130US ?(0x0050) ?// 4 x 12-bit shunt samples averaged together #define INA219_CONFIG_SADCRES_12BIT_8S_4260US ?(0x0058) ?// 8 x 12-bit shunt samples averaged together #define INA219_CONFIG_SADCRES_12BIT_16S_8510US (0x0060) ?// 16 x 12-bit shunt samples averaged together #define INA219_CONFIG_SADCRES_12BIT_32S_17MS ? (0x0068) ?// 32 x 12-bit shunt samples averaged together #define INA219_CONFIG_SADCRES_12BIT_64S_34MS ? (0x0070) ?// 64 x 12-bit shunt samples averaged together #define INA219_CONFIG_SADCRES_12BIT_128S_69MS ?(0x0078) ?// 128 x 12-bit shunt samples averaged together
#define INA219_CONFIG_MODE_MASK ? ? ? ? ? ? ? ?(0x0007) ?// Operating Mode Mask #define INA219_CONFIG_MODE_POWERDOWN ? ? ? ? ? (0x0000) #define INA219_CONFIG_MODE_SVOLT_TRIGGERED ? ? (0x0001) #define INA219_CONFIG_MODE_BVOLT_TRIGGERED ? ? (0x0002) #define INA219_CONFIG_MODE_SANDBVOLT_TRIGGERED (0x0003) #define INA219_CONFIG_MODE_ADCOFF ? ? ? ? ? ? ?(0x0004) #define INA219_CONFIG_MODE_SVOLT_CONTINUOUS ? ?(0x0005) #define INA219_CONFIG_MODE_BVOLT_CONTINUOUS ? ?(0x0006) #define INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS (0x0007)?? ?
#define INA_CAL 33555?? ??? ??? ??? ??? ??? ??? ?//(0x05)寄存器基准值
直接调用获取函数即可
int main(void)
{
? ? ? ? ...
????????ina219_init();
? ? ? ? while(1)
? ? ? ? {
????????????????double? p1 =?ina219_get_W(1);
????????????????double? p2 =?ina219_get_W(2);
? ? ? ? ? ? ? ? ...
????????????????HAL_Delay(100);
? ? ? ? }
}
|