1、时钟速度
2、占空比
3、数据格式
#include "stm32f4xx.h"
#include "smbus.h"
#include "stdint.h"
#include "bq4050.h"
#include "usart.h"
#include "delay.h"
#include "myiic.h"
#define BQ4050_ADDRRESS 0x16
#define INVALID_REG_ADDR 0xFF
#define BQ4050_REG_TEMP 0x08 //Temperature U2
#define BQ4050_REG_VOLT 0x09 //Voltage U2
#define BQ4050_REG_CURRENT 0x0A //CURRENT I2
#define BQ4050_REG_RSOC 0x0D //RelativeStateOfCharge U1
#define BQ4050_REG_FCC 0x10 //FullChargeCapacity U2
#define BQ4050_REG_TTE 0x12 //TimeToEmpty U2
#define BQ4050_REG_TTF 0x13 //TimeToFull U2
#define BQ4050_REG_RMC 0x0F ///* Remaining Capacity */
uint16_t TEMP_para = 0x0103;
uint32_t VOLE_para = 1;
uint32_t CURR_para = 1;
uint32_t RSOC_para = 1;
uint32_t FCC_para = 1;
uint32_t TTE_para = 1;
uint32_t TTF_para = 1;
uint32_t RMC_para = 1;
uint16_t bq4050Para[8];
u8 _ChargeProcess;
enum bq_fg_reg_idx {
BQ_FG_REG_MAC = 0,
BQ_FG_REG_TEMP, /* Battery Temperature */
BQ_FG_REG_VOLT, /* Battery Voltage */
BQ_FG_REG_AI, /* Average Current */
BQ_FG_REG_BATT_STATUS, /* BatteryStatus */
BQ_FG_REG_TTE, /* Time to Empty */
BQ_FG_REG_TTF, /* Time to Full */
BQ_FG_REG_FCC, /* Full Charge Capacity */
BQ_FG_REG_RM, /* Remaining Capacity */
BQ_FG_REG_CC, /* Cycle Count */
BQ_FG_REG_SOC, /* Relative State of Charge */
BQ_FG_REG_SOH, /* State of Health */
BQ_FG_REG_DC, /* Design Capacity */
BQ_FG_REG_MBA, /* ManufacturerBlockAccess*/
NUM_REGS,
};
static u8 bq4050_regs[NUM_REGS] = {
0x00, /* CONTROL */
0x08, /* TEMP */
0x09, /* VOLT */
0x0B, /* AVG CURRENT */
0x16, /* FLAGS */
0x12, /* Time to empty */
0x13, /* Time to full */
0x10, /* Full charge capacity */
0x0F, /* Remaining Capacity */
0x17, /* CycleCount */
0x0D, /* State of Charge */
0x4F, /* State of Health */
0x18, /* Design Capacity */
0x44, /* ManufacturerBlockAccess*/
};
enum {
SEAL_STATE_RSVED,
SEAL_STATE_UNSEALED,
SEAL_STATE_SEALED,
SEAL_STATE_FA,
};
/*
*/
u8 getBatteryInfo(u8 slaveAddr, u8 Comcode,u8 *data, u8 size)
{
u8 ackFlag;
uint8_t buff[2];
IIC_Start();
IIC_Send_Byte(slaveAddr);//发送地址
IIC_Wait_Ack();
IIC_Send_Byte(Comcode);
IIC_Wait_Ack();
delay_us(750);
IIC_Start();
IIC_Send_Byte(slaveAddr+1);//发送地址
IIC_Wait_Ack();
delay_us(50);
while(size-1){
*data = IIC_Read_Byte(1);
IIC_Ack();
delay_us(10);
data++;
size--;
}
delay_us(60);
*data++ = IIC_Read_Byte(0);
IIC_Stop();
delay_ms(5);
}
u8 getFW(u8 slaveAddr, u8 *data, u8 ReadAddr)
{
u8 ackFlag;
uint8_t buff[2];
IIC_Start();
IIC_Send_Byte(slaveAddr);//发送地址
IIC_Send_Byte(0x03);
IIC_Send_Byte(0x00);
delay_us(20);
IIC_Start();
IIC_Send_Byte(ReadAddr);//发送地址
*data = IIC_Read_Byte(1);
delay_us(10);
*data++ = IIC_Read_Byte(0);
IIC_Stop();
delay_ms(100);
}
//模拟IIC初始化
void SMBusEmulationInit()
{
IIC_Init();
}
#include "myiic.h"
#include "delay.h"
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//初始化IIC
void IIC_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);//使能GPIOB时钟
//GPIOB6,B7初始化设置
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//普通输出模式
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化
IIC_SCL=1;
IIC_SDA=1;
}
//产生IIC起始信号
void IIC_Start(void)
{
SDA_OUT(); //sda线输出
IIC_SCL=0;
delay_us(2);
IIC_SDA=1;
delay_us(1);
IIC_SCL=1;
delay_us(9);
IIC_SDA=0;//START:when CLK is high,DATA change form high to low
delay_us(9);
IIC_SCL=0;//钳住I2C总线,准备发送或接收数据
}
//产生IIC停止信号
void IIC_Stop(void)
{
SDA_OUT();//sda线输出
IIC_SCL=0;
delay_us(1);
IIC_SDA=0;//STOP:when CLK is high DATA change form low to high
delay_us(9);
IIC_SCL=1;
delay_us(9);
IIC_SDA=1;//发送I2C总线结束信号
delay_us(9);
}
//等待应答信号到来
//返回值:1,接收应答失败
// 0,接收应答成功
u8 IIC_Wait_Ack(void)
{
u8 ucErrTime=0;
SDA_IN(); //SDA设置为输入
IIC_SDA=1;delay_us(9);
IIC_SCL=1;delay_us(9);
while(READ_SDA)
{
ucErrTime++;
if(ucErrTime>250)
{
IIC_Stop();
return 1;
}
}
IIC_SCL=0;//时钟输出0
return 0;
}
//产生ACK应答
void IIC_Ack(void)
{
IIC_SCL=0;
SDA_OUT();
IIC_SDA=0;
delay_us(9);
IIC_SCL=1;
delay_us(9);
IIC_SCL=0;
}
//不产生ACK应答
void IIC_NAck(void)
{
IIC_SCL=0;
SDA_OUT();
IIC_SDA=1;
delay_us(9);
IIC_SCL=1;
delay_us(9);
IIC_SCL=0;
}
//IIC发送一个字节
//返回从机有无应答
//1,有应答
//0,无应答
void IIC_Send_Byte(u8 txd)
{
u8 t;
SDA_OUT();
IIC_SCL=0;//拉低时钟开始数据传输
for(t=0;t<8;t++){
IIC_SDA=(txd&0x80)>>7;
txd<<=1;
delay_us(8);
IIC_SCL=1;
delay_us(8);
IIC_SCL=0;
delay_us(8);
}
}
//读1个字节,ack=1时,发送ACK,ack=0,发送nACK
u8 IIC_Read_Byte(unsigned char ack)
{
unsigned char i,receive=0;
SDA_IN();//SDA设置为输入
for(i=0;i<8;i++ )
{
IIC_SCL=0;
delay_us(12);
IIC_SCL=1;
receive<<=1;
if(READ_SDA)receive++;
delay_us(9);
}
return receive;
}
/*
函数:I2C_Write()
功能:向I2C总线写1个字节的数据
参数:
dat:要写到总线上的数据
*/
void I2C_Write(unsigned char dat)
{
/*发送1,在SCL为高电平时使SDA信号为高*/
/*发送0,在SCL为高电平时使SDA信号为低*/
unsigned char t ;
for(t=0;t<8;t++)
{
IIC_SDA = (dat & 0x80);
delay_us(10);
IIC_SCL = 1; //置时钟线为高,通知被控器开始接收数据位
delay_us(10);
IIC_SCL = 0;
delay_us(10);
dat <<= 1;
}
}