針對cortexm系列
INA219 是一款具備 I 2C 或 SMBUS 兼容接口的分流器 和功率監(jiān)測計。該器件監(jiān)測分流器電壓降和總線電源電 壓,轉(zhuǎn)換次數(shù)和濾波選項可通過編程設定。可編程校準 值與內(nèi)部乘法器相結(jié)合,支持直接讀取電流值(單位: 安培)。通過附加乘法寄存器可計算功率(單位: 瓦)。I 2C 或 SMBUS 兼容接口 具有 16 個可編程地 址。
c文件
#include "ina219.h"
void INA_REG_Write(unsigned char reg,unsigned int data);
#ifdef INA_INSIDE_IIC
void INA_IIC_Delay()
{unsigned char x;
for(x=1;x>0;x--)
{
__NOP();__NOP();__NOP();__NOP();__NOP();
}
}
void INA_IIC_INIT(void) //IIC初始化
{
GPIO_InitTypeDef GPIO_INIT;
RCC_APB2PeriphClockCmd(IIC_RCC,ENABLE);
GPIO_INIT.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_INIT.GPIO_Pin=IIC_SDA | IIC_SCL;
GPIO_INIT.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(IIC_PORT,&GPIO_INIT);
IIC_SDA_H;
IIC_SCL_H;
}
void INA_IIC_SDA_OUT(void) //SDA配置為輸出
{
GPIO_InitTypeDef GPIO_INIT;
GPIO_INIT.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_INIT.GPIO_Pin=IIC_SDA ;
GPIO_INIT.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(IIC_PORT,&GPIO_INIT);
}
void INA_IIC_SDA_IN(void) //SDA配置為輸入
{
GPIO_InitTypeDef GPIO_INIT;
GPIO_INIT.GPIO_Mode=GPIO_Mode_IPU;
GPIO_INIT.GPIO_Pin=IIC_SDA ;
GPIO_INIT.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(IIC_PORT,&GPIO_INIT);
}
void INA_IIC_Start(void) //開始信號
{
IIC_SDA_H;
IIC_SCL_H;
INA_IIC_Delay();
IIC_SDA_L;
INA_IIC_Delay();
IIC_SDA_L;
}
void INA_IIC_Stop(void ) //結(jié)束信號
{
IIC_SDA_L;
IIC_SCL_H;
INA_IIC_Delay();
IIC_SDA_H;
INA_IIC_Delay();
IIC_SCL_L;
IIC_SCL_H;
IIC_SDA_H;
}
bool INA_IIC_ACK_Read(void) //讀取應答信號
{
bool ack;
IIC_SDA_H;
INA_IIC_SDA_IN();
IIC_SCL_H;
INA_IIC_Delay();
if(IIC_SDA_READ() == SET) ack=false;
else ack=true;
IIC_SCL_L;
INA_IIC_SDA_OUT();
return ack;
}
void INA_IIC_ACK_Send(bool ack) //發(fā)送應答信號
{
IIC_SCL_L;
if(ack == true)
IIC_SDA_L;
else IIC_SDA_H;
IIC_SCL_H;
INA_IIC_Delay();
IIC_SCL_L;
}
void INA_IIC_Send_Byte(unsigned char byte) //IIC發(fā)送一位數(shù)據(jù)
{
unsigned char i;
IIC_SCL_L;
for(i=0;i8;i++)
{
if(byte & 0x80 ) IIC_SDA_H;
else IIC_SDA_L;
IIC_SCL_H;
INA_IIC_Delay();
IIC_SCL_L;
INA_IIC_Delay();
byte<=1;
}
INA_IIC_ACK_Read();
}
unsigned char INA_IIC_Read_Byte(void) //IIC讀取一位數(shù)據(jù)
{
unsigned char i,byte=0;
INA_IIC_SDA_IN();
for(i=0;i8;i++)
{
IIC_SCL_H;
byte<=1;
if(IIC_SDA_READ() == SET) byte |= 0x01;
else byte &= 0xFE;
IIC_SCL_L;
INA_IIC_Delay();
}
INA_IIC_SDA_OUT();
return byte;
}
#endif
void INA_Read_Byte_s(unsigned char reg,unsigned char *data) //讀兩位數(shù)據(jù)
{
INA_IIC_Start();
INA_IIC_Send_Byte(INA219_ADDRESS); //發(fā)送INA219地址s
INA_IIC_Send_Byte(reg);
INA_IIC_Start();
INA_IIC_Send_Byte(INA219_ADDRESS+0x01); //設置iic為讀模式
*data=INA_IIC_Read_Byte();
data++;
INA_IIC_ACK_Send(1);
*data=INA_IIC_Read_Byte();
INA_IIC_ACK_Send(0);
INA_IIC_Stop();
}
void INA_REG_Write(unsigned char reg,unsigned int data) //寫寄存器 測試成功
{
unsigned char data_temp[2];
data_temp[0]=(unsigned char )(data?>>8);
data_temp[1]=(unsigned char )(data & 0xFF);
INA_IIC_Start();
INA_IIC_Send_Byte(INA219_ADDRESS); //發(fā)送INA219地址
INA_IIC_Send_Byte(reg); //發(fā)送寄存器地址
INA_IIC_Send_Byte(data_temp[0]); //發(fā)送高8位數(shù)據(jù)
data++;
INA_IIC_Send_Byte(data_temp[1]) ; //發(fā)送低8位數(shù)據(jù)
INA_IIC_Stop();
}
void INA_Init(void )
{
INA_IIC_INIT();
INA_REG_Write(INA219_REG_CONFIG,INA219_CONFIG_value);
INA_REG_Write(INA219_REG_CALIBRATION,INA_CAL);
}
unsigned int INA_GET_Voltage_MV(void) //獲取電壓(單位:mv)
{
unsigned char data_temp[2];
INA_Read_Byte_s(0x02,data_temp);
return (int)((((data_temp[0]<8)+data_temp[1]) >> 3)*4); //右移3為去掉:bit2,CNVR,OVF三位,再乘以 4MV (官方文檔規(guī)定),得到當前總線的電壓值
}
unsigned int INA_GET_Current_MA(void) //獲取電流(單位:mA)
{
unsigned char data_temp[2];
INA_REG_Write(INA219_REG_CONFIG,INA219_CONFIG_value);
INA_Read_Byte_s(INA219_REG_CURRENT,data_temp);
return (int)((((data_temp[0]<8)+data_temp[1]))*IAN_I_LSB); //得到寄存器的值在乘以每位對應的值(IAN_I_LSB)得到實際的電流
}
unsigned int INA_GET_Power_MW(void) //獲取當前功率(單位:mw)
{
unsigned char data_temp[2];
INA_Read_Byte_s(INA219_REG_POWER,data_temp);
return (int)(((data_temp[0]<8)+data_temp[1])*INA_Power_LSB); //得到寄存器的值在乘以每位對應的值(INA_Power_LSB)得到實際的功率
}
H文件
#ifndef _INA219_H_
#define _INA219_H_
#include "stm32f10x.h"
#include "stdbool.h"
// I2C Address Options
/*
#define INA219_I2C_ADDRESS_CONF_0 (u8)(0x40 < 1) // A0 = GND, A1 = GND
#define INA219_I2C_ADDRESS_CONF_1 (u8)(0x41 < 1) // A0 = VS+, A1 = GND
#define INA219_I2C_ADDRESS_CONF_2 (u8)(0x42 < 1) // A0 = SDA, A1 = GND
#define INA219_I2C_ADDRESS_CONF_3 (u8)(0x43 < 1) // A0 = SCL, A1 = GND
#define INA219_I2C_ADDRESS_CONF_4 (u8)(0x44 < 1) // A0 = GND, A1 = VS+
#define INA219_I2C_ADDRESS_CONF_5 (u8)(0x45 < 1) // A0 = VS+, A1 = VS+
#define INA219_I2C_ADDRESS_CONF_6 (u8)(0x46 < 1) // A0 = SDA, A1 = VS+
#define INA219_I2C_ADDRESS_CONF_7 (u8)(0x47 < 1) // A0 = SCL, A1 = VS+
#define INA219_I2C_ADDRESS_CONF_8 (u8)(0x48 < 1) // A0 = GND, A1 = SDA
#define INA219_I2C_ADDRESS_CONF_9 (u8)(0x49 < 1) // A0 = VS+, A1 = SDA
#define INA219_I2C_ADDRESS_CONF_A (u8)(0x4A < 1) // A0 = SDA, A1 = SDA
#define INA219_I2C_ADDRESS_CONF_B (u8)(0x4B < 1) // A0 = SCL, A1 = SDA
#define INA219_I2C_ADDRESS_CONF_C (u8)(0x4C < 1) // A0 = GND, A1 = SCL
#define INA219_I2C_ADDRESS_CONF_D (u8)(0x4D < 1) // A0 = VS+, A1 = SCL
#define INA219_I2C_ADDRESS_CONF_E (u8)(0x4E < 1) // A0 = SDA, A1 = SCL
#define INA219_I2C_ADDRESS_CONF_F (u8)(0x4F < 1) // A0 = SCL, A1 = SCL
*/
#define INA219_ADDRESS (0x40 < 1) // A0 = GND, A1 = GND
// 寄存器地址
#define INA219_REG_CONFIG (u8)(0x00) // 模式配置寄存器 (R/W)
#define INA219_REG_SHUNTVOLTAGE (u8)(0x01) // 分流電阻電壓寄存器 (R)
#define INA219_REG_BUSVOLTAGE (u8)(0x02) // 總線電壓寄存器 (R)
#define INA219_REG_POWER (u8)(0x03) // 功率寄存器 (R)
#define INA219_REG_CURRENT (u8)(0x04) // 電流寄存器 (R)
#define INA219_REG_CALIBRATION (u8)(0x05) // 基準值寄存器 (R/W)
//寄存器(0x01)參數(shù)定義
#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)
//INA配置的參數(shù)
#define INA_R 0.1 //阻值大小(單位ohm)
#define INA_I_MAX 3 //硬件最大檢測電流(單位:A) INA219_CONFIG_GAIN_電壓/INA_R
#define IAN_I_LSB 0.1 //(0x04)檢測出值對應每位電流值 選擇接近 = (INA_I_MAX/32767(2^15))*1000(為了得到單位為A) (A)
#define INA_Power_LSB 2 //(0x03)檢測出值對應每位功率值 = 20 * IAN_I_LSB (MW)
#define INA_CAL 4096 //(0x05)寄存器基準值
//根據(jù)上面定義設置 配置寄存器(0x00)的值:0x3C1F
#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
#define INA_INSIDE_IIC //使用自帶的io模擬iic
#ifdef INA_INSIDE_IIC
#define IIC_PORT GPIOA //IIC IO端口
#define IIC_RCC RCC_APB2Periph_GPIOA //端口時鐘
#define IIC_SDA GPIO_Pin_5 //SDA引腳
#define IIC_SDA_H GPIO_SetBits(IIC_PORT,IIC_SDA) //拉高SDA
#define IIC_SDA_L GPIO_ResetBits(IIC_PORT,IIC_SDA) //拉低SDA
#define IIC_SDA_READ() GPIO_ReadInputDataBit(GPIOA,IIC_SDA) //讀取SDA
#define IIC_SCL GPIO_Pin_6 //SCL引腳
#define IIC_SCL_H GPIO_SetBits(IIC_PORT,IIC_SCL) //拉高SCL
#define IIC_SCL_L GPIO_ResetBits(IIC_PORT,IIC_SCL) //拉低SCL
#endif
void INA_Init(void );
unsigned int INA_GET_Voltage_MV(void);//獲取電壓(單位:mv)
unsigned int INA_GET_Current_MA(void);//獲取電流(單位:mA)
unsigned int INA_GET_Power_MW(void); //獲取當前功率(單位:mw)
unsigned int INA_GET_R_MV(void);
#endif
審核編輯:湯梓紅
聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學習之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請聯(lián)系本站處理。
舉報投訴
-
寄存器
+關(guān)注
關(guān)注
31文章
5336瀏覽量
120230 -
接口
+關(guān)注
關(guān)注
33文章
8575瀏覽量
151015 -
電流采集
+關(guān)注
關(guān)注
0文章
5瀏覽量
6550
發(fā)布評論請先 登錄
相關(guān)推薦
利用INA219作為電池輸入的電壓電流采集,會出現(xiàn)燒毀的情況怎么解決?
利用INA219作為電池輸入的電壓電流采集(前端未加TVS),在使用過程中出現(xiàn)INA219整體燒毀直接冒火碳化,請問出現(xiàn)這種問題一般什么原因?qū)е拢坷擞浚?
發(fā)表于 08-05 08:11
INA219電流過大怎么處理?
如下圖的設計,其他所有電路均不焊接情況下,排除短路,只要焊接上INA219,3V3就會達到104mA (不過不是LDO的極限),這看起來并不正常,但是我從電路上沒看到不正常的地方,我是INA219,top mark是A219
發(fā)表于 08-19 06:40
INA219讀取電流不正常的原因?怎么解決?
1.用INA219讀取電流,會讀到和分流電阻電壓寄存器一樣的定值,在沒有負載的情況下也是這樣
2.按照數(shù)據(jù)手冊計算的配置值沒用,隨便改對電流電壓的測量都不會造成什么影響
3.電流的L
發(fā)表于 12-03 06:26
有關(guān)INA219芯片的使用問題
在使用TI 公司的INA219芯片進行電流,電壓以及功率的數(shù)值進行測量時,遇到了如下幾個問題1.只有先配置Calibration Register 才能檢測到電流嗎?如何精準的配置
發(fā)表于 01-20 10:26
請問INA219軟件怎樣讀出eZdspC5535上INA219的數(shù)據(jù)?
INA219軟件 怎樣讀出eZdspC5535上INA219的數(shù)據(jù)?
發(fā)表于 06-18 11:33
INA219電壓電流表的資料分享
描述電壓電流表INA219這真的是一個很酷的小設備,它不僅可以讓你測量電流,還可以測量電壓。通過一點乘法,您甚至可以測量功率。在電壓方面,您可以直接超過 Arduino 5 伏的限制。事實上,您最多
發(fā)表于 08-18 07:16
INA219,pdf(Zerø-Drift,
The INA219 is a high-side current shunt and power monitor with an I2C interface. The INA219
發(fā)表于 09-24 23:17
?57次下載
INA219電量監(jiān)測芯片的使用經(jīng)驗和資料及使用步驟詳細說明
手冊如上,INA219 是TI推出的具有I2C 接口的零漂移雙向電流/功率監(jiān)測計,INA219內(nèi)部結(jié)構(gòu)圖如上,芯片同是支持差分分壓電流檢測跟總線電壓檢測
評論