ina230 操作和stm8的i2c
2018-02-02 16:33
323 查看
ina230 是电压电流检测芯片,TI出的,大概记录下用法。
我还没用他的告警的功能,现在只说计算电流电压功率之类的东西
算电流的话 我看来有两个做法
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 读写例子
寄存器 | 说明 | 备注 |
---|---|---|
00 | 配置寄存器 | 没管,用的默认的配置 |
01 | 分流器电压 | 简单讲就是用来分压的电阻上面的电压。 |
02 | 总线电压 | bus线上的电压 |
03 | 功率 | 功率值 |
04 | 电流 | 设置了校准寄存器的值之后就可以得出准确的值 |
05 | 校准 | 根据最大电流和分流器的电阻阻值计算出来的一个数据 |
06 | 屏蔽使能 | 是否能够触发alert的设置 |
07 | alert值 | alert的值 |
ff | id | 芯片的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; }
相关文章推荐
- linux IIC子系统分析(七)——实例分析通过i2c-dev操作I2C设备
- F28027第十一课---I2C操作解读
- 单片机 I2C操作笔记——以 AT24C04为例
- stm8 调试硬件I2C心得http://bbs.21ic.com/icview-688762-1-1.html
- 不同层面上操控I2C进行读写操作
- stm32 i2c通信 [操作寄存器+库函数]
- stm32 i2c通信 [操作寄存器+库函数]
- Linux I2C 对16位寄存器地址 进行读写操作
- STM8 I2C的学习
- linux i2c子系统代码分析5 ---操作函数i2c_new_device i2c_new_probed_device i2c_register_board_info介绍
- stm8及DS1302的一些操作
- rt5350 i2c 操作wm8988
- I2C操作笔记——以 AT24C04为例
- stm32 stm8 I2C相关总结
- linux i2c子系统代码分析2 ---操作函数i2c_init介绍
- I2C操作笔记——以 AT24C04为例
- I2C操作笔记——以 AT24C04为例
- STM32F407VE单片机使用I2C接口操作内部控制芯片为RA8816的FYD12864-1001A型的12864液晶
- [STM8L]EEPROM操作读与写
- iTop4412之I2C裸机操作