您的位置:首页 > 其它

ina230 操作和stm8的i2c

2018-02-02 16:33 323 查看
ina230 是电压电流检测芯片,TI出的,大概记录下用法。

寄存器说明备注
00配置寄存器没管,用的默认的配置
01分流器电压简单讲就是用来分压的电阻上面的电压。
02总线电压bus线上的电压
03功率功率值
04电流设置了校准寄存器的值之后就可以得出准确的值
05校准根据最大电流和分流器的电阻阻值计算出来的一个数据
06屏蔽使能是否能够触发alert的设置
07alert值alert的值
ffid芯片的id
我还没用他的告警的功能,现在只说计算电流电压功率之类的东西

算电流的话 我看来有两个做法

1. 直接读取分流器电压,然后软件上直接除以电阻阻值

2. 直接读取电流值

第一种方法的话,比较x丝,读取01寄存器,除一下就好

第二种方法需要设置校准寄存器,校准寄存器的值cal计算方法如下

cal = 0.00512/ ((I_max/2^15) * R_shunt)

I_max 最大输入电流

R_shunt 分流器阻值

(I_max/2^15) 这个部分可能不是整数,为了计算方便的话 建议换成整数,这个部分也是电流分辨率

在我现在的电路里面,最大输入电流为3a,分流器阻值为50mΩ,所以

3/2^15 ≈ 0.0001

cal = 1024 = 0x0400

设置校准寄存器就好。就可以做直接读了。

以下是STM8的i2c 读写例子

/*****************************************************************************
/*****************************************************************************
*   Prototype    : inaGPIOInit
*   Description  : init gpio for ina230
*   Input        : int chipno
*   Output       : None
*   Return Value : int
*   Calls        :
*   Called By    :
*
*   History:
*
*       1.  Date         : 2018/1/31
*           Author       : zhuanghj
*           Modification : Created function
*
*****************************************************************************/
static int inaGPIOInit( int chipno )
{
GPIO_Init(GPIOC, GPIO_Pin_0, GPIO_Mode_Out_PP_High_Slow);
GPIO_Init(GPIOC, GPIO_Pin_1, GPIO_Mode_Out_PP_High_Slow);
return 0;
}

/*****************************************************************************
*   Prototype    : inaIICIinit
*   Description  : init iic
*   Input        : int chipno
*   Output       : None
*   Return Value : int
*   Calls        :
*   Called By    :
*
*   History:
*
*       1.  Date         : 2018/1/31
*           Author       : zhuanghj
*           Modification : Created function
*
*****************************************************************************/
int inaIICIinit( int chipno )
{
//Todo:should write a function that select clk source from the input i2c source
//but it's to much ,next time

I2C_TypeDef * I2Cx = NULL;
I2Cx = getIICBusNo();

I2C_DeInit(I2Cx);
CLK_PeripheralClockConfig(CLK_Peripheral_I2C1, ENABLE);     /* IT IS I2C1 HERE */
I2C_Cmd(I2Cx, ENABLE);
I2C_Init(I2Cx, 100000, 0X00,
I2C_Mode_I2C, I2C_DutyCycle_2,
I2C_Ack_Enable, I2C_AcknowledgedAddress_7bit);
return 0;
}

*   Prototype    : inaReadReg
*   Description  : read ina reg
*   Input        : char reg
*   Output       : None
*   Return Value : int
*   Calls        :
*   Called By    :
*
*   History:
*
*       1.  Date         : 2018/1/31
*           Author       : zhuanghj
*           Modification : Created function
*
*****************************************************************************/
int inaReadReg(char addr, int reg ,char * value,int len)
{
I2C_TypeDef * I2Cx = NULL;
int tmpaddr = 0;
I2Cx = getIICBusNo();
if(addr < 0)
return -1;
tmpaddr = addr + 1;

while (I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY));
I2C_GenerateSTART(I2Cx, ENABLE);
while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2Cx, tmpaddr, I2C_Direction_Transmitter);
while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_ClearFlag(I2Cx, I2C_FLAG_ADDR);
I2C_SendData(I2Cx, reg & 0xFF);
while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

I2C_GenerateSTART(I2Cx, ENABLE);
while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2Cx, tmpaddr, I2C_Direction_Receiver);
while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
I2C_Cl
c77c
earFlag(I2Cx, I2C_FLAG_ADDR);

#if 1
while (len)
{
while ((I2C_GetLastEvent(I2Cx) & 0x04) != 0x04); /* Poll on BTF */
*(value++) = I2C_ReceiveData(I2Cx);
len--;
}
#else
while ((I2C_GetLastEvent(I2Cx) & 0x04) != 0x04); /* Poll on BTF */
*(value++) = I2C_ReceiveData(I2Cx);
#endif

I2C_AcknowledgeConfig(I2Cx, ENABLE);
I2C_GenerateSTOP(I2Cx,ENABLE);
return 0;
}

/*****************************************************************************
*   Prototype    : inaWriteReg
*   Description  : write reg
*   Input        : char reg
*                  char value
*   Output       : None
*   Return Value : int
*   Calls        :
*   Called By    :
*
*   History:
*
*       1.  Date         : 2018/1/31
*           Author       : zhuanghj
*           Modification : Created function
*
*****************************************************************************/
int inaWriteReg(char addr, char reg, int value )
{
I2C_TypeDef * I2Cx = NULL;
int tmpaddr = 0;
I2Cx = getIICBusNo();
if(addr < 0)
return -1;
tmpaddr = addr;
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY)); //等待空闲

I2C_GenerateSTART(I2Cx, ENABLE);//开启I2C1
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT));/*EV5,主模式*/

I2C_Send7bitAddress(I2Cx, tmpaddr, I2C_Direction_Transmitter);//器件地址 -- 默认0xD0
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

I2C_SendData(I2Cx, reg);//寄存器地址
while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

I2C_SendData(I2Cx, (value >> 8) &0x00ff);//发送数据
while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

I2C_SendData(I2Cx, value & 0xff);//发送数据
while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_AcknowledgeConfig(I2Cx,ENABLE);

I2C_GenerateSTOP(I2Cx, ENABLE);//关闭I2C1总线
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  stm8