From d93e50243778e8d6abb20bc7fff4efa97600e961 Mon Sep 17 00:00:00 2001 From: jxh Date: Wed, 19 Mar 2025 17:25:17 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=BB=E5=8F=96=E7=94=B5=E6=B5=81=E7=94=B5?= =?UTF-8?q?=E5=8E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Lib/ina226.c | 7 ++++--- Lib/ina226.h | 2 +- Src/main.c | 48 +++++++++++++++++++++--------------------------- 3 files changed, 26 insertions(+), 31 deletions(-) diff --git a/Lib/ina226.c b/Lib/ina226.c index 70bc6ff..f7f64ae 100644 --- a/Lib/ina226.c +++ b/Lib/ina226.c @@ -3,7 +3,7 @@ static float CurrentLSB; // 电流分辨率 void -INA226_Init (uint16_t r_shunt, float max_current) +INA226_Init (float r_shunt, float max_current) { uint16_t cal = 0; @@ -25,14 +25,14 @@ INA226_WriteReg (uint8_t reg, uint16_t value) uint8_t data_t[2]; data_t[0] = (uint8_t)(value >> 8); data_t[1] = (uint8_t)(value & 0xFF); - IIC_WriteBytes (INA226_ADDR, reg, data_t, 2); // 需适配您的I2C驱动 + IIC_WriteBytes (INA226_ADDR, reg, data_t, 2); } // 寄存器读取函数 uint16_t INA226_ReadReg (uint8_t reg) { uint8_t data_t[2]; - IIC_ReadBytes (INA226_ADDR, reg, data_t, 2); // 需适配您的I2C驱动 + IIC_ReadBytes (INA226_ADDR, reg, data_t, 2); return (data_t[0] << 8) | data_t[1]; } @@ -58,4 +58,5 @@ INA226_ReadPower (void) { int16_t raw = (int16_t)INA226_ReadReg (POWER_REG); return raw * 25 * CurrentLSB; // P=25×CurrentLSB×raw +// return raw * CurrentLSB; // 实测结果偏大 } \ No newline at end of file diff --git a/Lib/ina226.h b/Lib/ina226.h index 102af48..afe72b0 100644 --- a/Lib/ina226.h +++ b/Lib/ina226.h @@ -19,7 +19,7 @@ #define INA226_ADDR 0x80 // 函数声明 -void INA226_Init (uint16_t r_shunt, float max_current); +void INA226_Init (float r_shunt, float max_current); void INA226_WriteReg (uint8_t reg, uint16_t value); uint16_t INA226_ReadReg (uint8_t reg); diff --git a/Src/main.c b/Src/main.c index d5d7d21..bfc3736 100644 --- a/Src/main.c +++ b/Src/main.c @@ -22,9 +22,11 @@ SystemClock_Init (void) void main () { - u8 addr; - u16 read_i = 0; - u16 adder = 0; + u16 ina226_voltage = 0; + u16 ina226_current = 0; + u16 ina226_power = 0; + + u16 loop_counter = 0; SystemClock_Init (); // 时钟配置 @@ -40,7 +42,7 @@ main () OLED_Init (); printf ("INA226_Init()\r\n"); - INA226_Init (0x0A, 0.05f); + INA226_Init (0.005f, 2.0f); DelayMs (100); // 初始化延时 @@ -63,34 +65,26 @@ main () while (1) { - adder++; + loop_counter++; - OLED_ShowNum (8, 2, adder, 10, OLED_FONT_SIXTEEN, OLED_LEFT_ROLL, + OLED_ShowNum (8, 0, loop_counter, 10, OLED_FONT_SIXTEEN, OLED_LEFT_ROLL, + OLED_SHOW); + + ina226_voltage = (int16_t)(INA226_ReadBusVoltage () * 1000); + + OLED_ShowNum (4, 2, ina226_voltage, 10, OLED_FONT_SIXTEEN, OLED_LEFT_ROLL, + OLED_SHOW); + + ina226_current = (int16_t)(INA226_ReadCurrent () * 1000); + OLED_ShowNum (4, 4, ina226_current, 10, OLED_FONT_SIXTEEN, + OLED_LEFT_ROLL, OLED_SHOW); + + ina226_power = (int16_t)(INA226_ReadPower () * 1000); + OLED_ShowNum (4, 6, ina226_power, 10, OLED_FONT_SIXTEEN, OLED_LEFT_ROLL, OLED_SHOW); DelayMs (1000); - read_i = (int16_t)(INA226_ReadBusVoltage () * 1000); - - OLED_ShowNum (4, 6, read_i, 10, OLED_FONT_SIXTEEN, OLED_LEFT_ROLL, - OLED_SHOW); - - printf ("Scanning I2C bus...\r\n"); - - for (addr = 0x08; addr <= 0x78; addr++) - { // 有效地址范围 - IIC_Start (); - IIC_SendByte (addr << 1); // 发送写命令 - if (IIC_WaitAck ()) - { - printf ("Device found at: 0x%02X\n", addr); - } - IIC_Stop (); - DelayMs (1); - } - - printf ("Scan complete.\n"); - DelayMs (1000); } }