針對(duì)cortexm系列
INA219 是一款具備 I 2C 或 SMBUS 兼容接口的分流器 和功率監(jiān)測(cè)計(jì)。該器件監(jiān)測(cè)分流器電壓降和總線電源電 壓,轉(zhuǎn)換次數(shù)和濾波選項(xiàng)可通過(guò)編程設(shè)定??删幊绦?zhǔn) 值與內(nèi)部乘法器相結(jié)合,支持直接讀取電流值(單位: 安培)。通過(guò)附加乘法寄存器可計(jì)算功率(單位: 瓦)。I 2C 或 SMBUS 兼容接口 具有 16 個(gè)可編程地 址。
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) //開(kāi)始信號(hào)
{
IIC_SDA_H;
IIC_SCL_H;
INA_IIC_Delay();
IIC_SDA_L;
INA_IIC_Delay();
IIC_SDA_L;
}
void INA_IIC_Stop(void ) //結(jié)束信號(hào)
{
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) //讀取應(yīng)答信號(hào)
{
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ā)送應(yīng)答信號(hào)
{
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); //設(shè)置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) //寫(xiě)寄存器 測(cè)試成功
{
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ī)定),得到當(dāng)前總線的電壓值
}
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); //得到寄存器的值在乘以每位對(duì)應(yīng)的值(IAN_I_LSB)得到實(shí)際的電流
}
unsigned int INA_GET_Power_MW(void) //獲取當(dāng)前功率(單位: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); //得到寄存器的值在乘以每位對(duì)應(yīng)的值(INA_Power_LSB)得到實(shí)際的功率
}
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) // 基準(zhǔn)值寄存器 (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 //配置檢測(cè)電阻上的最大檢測(cè)電壓
#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 //阻值大?。▎挝籵hm)
#define INA_I_MAX 3 //硬件最大檢測(cè)電流(單位:A) INA219_CONFIG_GAIN_電壓/INA_R
#define IAN_I_LSB 0.1 //(0x04)檢測(cè)出值對(duì)應(yīng)每位電流值 選擇接近 = (INA_I_MAX/32767(2^15))*1000(為了得到單位為A) (A)
#define INA_Power_LSB 2 //(0x03)檢測(cè)出值對(duì)應(yīng)每位功率值 = 20 * IAN_I_LSB (MW)
#define INA_CAL 4096 //(0x05)寄存器基準(zhǔn)值
//根據(jù)上面定義設(shè)置 配置寄存器(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 //端口時(shí)鐘
#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); //獲取當(dāng)前功率(單位:mw)
unsigned int INA_GET_R_MV(void);
#endif
審核編輯:湯梓紅
-
寄存器
+關(guān)注
關(guān)注
31文章
5343瀏覽量
120348 -
接口
+關(guān)注
關(guān)注
33文章
8596瀏覽量
151145 -
電流采集
+關(guān)注
關(guān)注
0文章
5瀏覽量
6553
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論