读取电流电压
This commit is contained in:
@ -3,7 +3,7 @@
|
|||||||
static float CurrentLSB; // 电流分辨率
|
static float CurrentLSB; // 电流分辨率
|
||||||
|
|
||||||
void
|
void
|
||||||
INA226_Init (uint16_t r_shunt, float max_current)
|
INA226_Init (float r_shunt, float max_current)
|
||||||
{
|
{
|
||||||
uint16_t cal = 0;
|
uint16_t cal = 0;
|
||||||
|
|
||||||
@ -25,14 +25,14 @@ INA226_WriteReg (uint8_t reg, uint16_t value)
|
|||||||
uint8_t data_t[2];
|
uint8_t data_t[2];
|
||||||
data_t[0] = (uint8_t)(value >> 8);
|
data_t[0] = (uint8_t)(value >> 8);
|
||||||
data_t[1] = (uint8_t)(value & 0xFF);
|
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
|
uint16_t
|
||||||
INA226_ReadReg (uint8_t reg)
|
INA226_ReadReg (uint8_t reg)
|
||||||
{
|
{
|
||||||
uint8_t data_t[2];
|
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];
|
return (data_t[0] << 8) | data_t[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -58,4 +58,5 @@ INA226_ReadPower (void)
|
|||||||
{
|
{
|
||||||
int16_t raw = (int16_t)INA226_ReadReg (POWER_REG);
|
int16_t raw = (int16_t)INA226_ReadReg (POWER_REG);
|
||||||
return raw * 25 * CurrentLSB; // P=25×CurrentLSB×raw
|
return raw * 25 * CurrentLSB; // P=25×CurrentLSB×raw
|
||||||
|
// return raw * CurrentLSB; // 实测结果偏大
|
||||||
}
|
}
|
@ -19,7 +19,7 @@
|
|||||||
#define INA226_ADDR 0x80
|
#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);
|
void INA226_WriteReg (uint8_t reg, uint16_t value);
|
||||||
uint16_t INA226_ReadReg (uint8_t reg);
|
uint16_t INA226_ReadReg (uint8_t reg);
|
||||||
|
|
||||||
|
48
Src/main.c
48
Src/main.c
@ -22,9 +22,11 @@ SystemClock_Init (void)
|
|||||||
void
|
void
|
||||||
main ()
|
main ()
|
||||||
{
|
{
|
||||||
u8 addr;
|
u16 ina226_voltage = 0;
|
||||||
u16 read_i = 0;
|
u16 ina226_current = 0;
|
||||||
u16 adder = 0;
|
u16 ina226_power = 0;
|
||||||
|
|
||||||
|
u16 loop_counter = 0;
|
||||||
|
|
||||||
SystemClock_Init (); // 时钟配置
|
SystemClock_Init (); // 时钟配置
|
||||||
|
|
||||||
@ -40,7 +42,7 @@ main ()
|
|||||||
OLED_Init ();
|
OLED_Init ();
|
||||||
|
|
||||||
printf ("INA226_Init()\r\n");
|
printf ("INA226_Init()\r\n");
|
||||||
INA226_Init (0x0A, 0.05f);
|
INA226_Init (0.005f, 2.0f);
|
||||||
|
|
||||||
DelayMs (100); // 初始化延时
|
DelayMs (100); // 初始化延时
|
||||||
|
|
||||||
@ -63,34 +65,26 @@ main ()
|
|||||||
|
|
||||||
while (1)
|
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);
|
OLED_SHOW);
|
||||||
|
|
||||||
DelayMs (1000);
|
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user