鸿蒙硬件HI3861-INA226-电压测量(外挂方案)
ina226.c
#include "ina226.h"
#include <stdio.h>
#include "cmsis_os2.h"
#include "wifiiot_gpio.h"
#include "wifiiot_gpio_ex.h"
#include <hi_stdlib.h>
#include "wifiiot_i2c.h"
#include "ina226util.h"
//定义配置数据
#define INA226_VAL_LSB 2.5f //分流电压 LSB 2.5uV
#define Voltage_LSB 1.25f //总线电压 LSB 1.25mV
#define CURRENT_LSB 1.0f //电流LSB 1mA
#define POWER_LSB 25*CURRENT_LSB
#define CAL 456 //0.00512/(Current_LSB*R_SHUNT) = 470 //电流偏大改小
void INA226_INIT(void){
INA226_SendData(INA226_I2CWRITE_ADDRESS,CFG_REG,0x484f);
INA226_SendData(INA226_I2CWRITE_ADDRESS,CAL_REG,CAL);
}
void INA226_SendData(hi_u8 addr,hi_u8 reg,hi_u16 data){
hi_u8 write_data[3];
WifiIotI2cIdx id = INA226_I2C_IDX;
WifiIotI2cData i2cData = {0};
write_data[0] = reg;
write_data[1] = (hi_u8)(data>>8);
write_data[2] = (hi_u8)(data&0x00FF);
i2cData.sendBuf = write_data;
i2cData.sendLen = 3;
I2cWrite(id, addr, &i2cData);
}
hi_u16 INA226_ReadData(hi_u8 addr){
hi_u16 temp = 0;
hi_u8 writeBuf[1] = {0};
WifiIotI2cIdx id = INA226_I2C_IDX;
WifiIotI2cData i2cData = {0};
writeBuf[0] = addr;
i2cData.sendBuf = writeBuf;
i2cData.sendLen = 1;
I2cWrite(id, INA226_I2CWRITE_ADDRESS, &i2cData);
hi_u8 readBuf[5] = {0};
WifiIotI2cData i2cRData = {0};
i2cRData.receiveBuf = readBuf;
i2cRData.receiveLen = 2;
I2cRead(id, INA226_I2CWRITE_ADDRESS,&i2cRData);
temp = readBuf[0];
temp<<=8;
temp |= readBuf[1];
return temp;
}
//1.25mV/bit
hi_u16 INA226_GetVoltage()
{
hi_u16 temp = 0;
temp = INA226_ReadData(0x02);
return (hi_u16)temp;
}
//2.5uV/bit
hi_u16 INA226_GetShuntVoltage()
{
int16_t temp = 0;
temp = INA226_ReadData(0x01);
if(temp&0x8000) temp = ~(temp - 1);
return (hi_u16)temp;
}
//获取电压
float GetVoltage(){ //mV
return (INA226_GetVoltage()*Voltage_LSB);
}
//获取分流电压
float Get_Shunt_voltage(){ //uV
return ((INA226_GetShuntVoltage()*INA226_VAL_LSB));
}
ina226.h
#ifndef INA226UTIL_H
#define INA226UTIL_H
#include <hi_types_base.h>
#define CFG_REG 0x00 //
#define SV_REG 0x01 //分流电压, 此处分流电阻为 0.1欧
#define BV_REG 0x02 //总线电压
#define PWR_REG 0x03 //电源功率
#define CUR_REG 0x04 //电流
#define CAL_REG 0x05 //校准,设定满量程范围以及电流和功率测数的
#define ONFF_REG 0x06 //屏蔽 使能 警报配置和转换准备就绪
#define AL_REG 0x07 //包含与所选警报功能相比较的限定值
#define INA226_GET_ADDR 0xFF //包含唯一的芯片标识号
#define INA226_I2C_IDX WIFI_IOT_I2C_IDX_0
#define INA226_I2CWRITE_ADDRESS 0x80
#define INA226_I2CREAD_ADDRESS 0x81
void INA226_INIT(void);
void INA226_SendData(hi_u8 addr,hi_u8 reg,hi_u16 data);
hi_u16 INA226_ReadData(hi_u8 addr);
float Get_Shunt_voltage(void);
float GetVoltage(void);
hi_u16 INA226_GetShuntVoltage(void);
hi_u16 INA226_GetVoltage(void);
#endif
测试代码
#include "ina226.h"
#include <stdio.h>
#include "cmsis_os2.h"
#include "wifiiot_gpio.h"
#include "wifiiot_gpio_ex.h"
#include <hi_stdlib.h>
#include "wifiiot_i2c.h"
#include "ina226util.h"
static void *Ina226Task(const char *arg){
(void)arg;
printf("Ina226Task start \r\n");
//INA226_ReadData(0x00);
INA226_INIT();
osDelay(10);
while(1){
printf("电压:%f \r\n",GetVoltage());
//Get_Shunt_voltage();
osDelay(100);
}
return NULL;
}
void Ina226Init(void){ // 在主函数中调用此函数
osThreadAttr_t attr;
attr.name = "Ina226Task";
attr.attr_bits = 0U;
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
attr.stack_size = 10240;
attr.priority = 26;
GpioInit();
IoSetFunc(WIFI_IOT_IO_NAME_GPIO_13, WIFI_IOT_IO_FUNC_GPIO_13_I2C0_SDA);
IoSetFunc(WIFI_IOT_IO_NAME_GPIO_14, WIFI_IOT_IO_FUNC_GPIO_14_I2C0_SCL);
I2cInit(WIFI_IOT_I2C_IDX_0, 400*1000);
osDelay(10);
if (osThreadNew((osThreadFunc_t)Ina226Task, NULL, &attr) == NULL) {
printf("[Ina226Demo] Falied to create Ina226Task!\n");
}
}
INA226
INA226配置寄存器
RST :复位
AVG:取平均数模
V BUS CT : 总线电压转换时间
V SH CT : 分流电压转换时间
MODE : 操作模式
接线
总 线 电 压 寄 存器 器 02h
INA226中文数据手册
https://download.csdn.net/download/qq_33259323/15107124 免费下载
如有问题可以找我https://space.bilibili.com/309103931