#include "BQData.h"
#define UV_THRESH_BASE 4096
/*****************************
file:BQData.c
decription:
1.采集BQ76930的14位ADC的增益和偏置电压:void get_offset(void)
2.采集单体电压:void Get_Batteryx(void),其中x取值1-8;
3.BQ76930初始化 void BQ_config(void)
4.读取BQ76930寄存器值: void readbqstate(void)
5.
******************************/
/***********************
fuction:void get_offset(void)
************************/
void get_offset(void)
{
byte gain[2];
byte Gain;
gain[0]=IIC_read_one_byte(0x50);
gain[1]=IIC_read_one_byte(0x59);
Gain = ((gain[0]&0x0c)<<1)+((gain[1]&0xe0)>>5);
ADC_offset=IIC_read_one_byte(0x51);
AD_Gain = 365+Gain;
}
/****************************************
fuction: void Get_Battery1(void)
description:获取第1、5号单体电池电压
Parameters:batterylval[0],battery1val[1];
******************************************/
void Get_Battery1(void)
{
byte readbattbuf[2];
dword batteryval1;
readbattbuf[1] = IIC_read_one_byte(0x0c);
readbattbuf[0] = IIC_read_one_byte(0x0d);
batteryval1= readbattbuf[1];
batteryval1= (batteryval1 << 8) |readbattbuf[0];
batteryval1=((batteryval1*AD_Gain)/1000)+ADC_offset;//单体电压计算公式,第1串
battery1val[1]=(byte)(batteryval1 >> 8);
battery1val[0]=(byte)(batteryval1);
}
/****************************************
fuction: void Get_Battery2(void)
description:获取第2号单体电池电压
Parameters:battery2val[0],battery2val[1];
******************************************/
void Get_Battery2(void)
{
byte readbattbuf[2];
dword batteryval1;
readbattbuf[1]=IIC_read_one_byte(0x0e);
readbattbuf[0]=IIC_read_one_byte(0x0f);
batteryval1= readbattbuf[1];
batteryval1= (batteryval1 << 8) |readbattbuf[0];
batteryval1=((batteryval1*AD_Gain)/1000)+ADC_offset;//单体电压计算公式,第2串
battery2val[1]=(byte)(batteryval1 >> 8);
battery2val[0]=(byte)(batteryval1);
}
/****************************************
fuction: void Get_Battery3(void)
description:获取第3号单体电池电压
Parameters:battery3val[0],battery3val[1];
******************************************/
void Get_Battery3(void)
{
byte readbattbuf[2];
dword batteryval1 ;
readbattbuf[1]=IIC_read_one_byte(0x10);
readbattbuf[0]=IIC_read_one_byte(0x11);
batteryval1= readbattbuf[1];
batteryval1= (batteryval1 << 8) |readbattbuf[0];
batteryval1=((batteryval1*AD_Gain)/1000)+ADC_offset;//单体电压计算公式,第3串
battery3val[1]=(byte)(batteryval1 >> 8);
battery3val[0]=(byte)(batteryval1 >> 0);
}
/****************************************
fuction: void Get_Battery4(void)
description:获取第4号单体电池电压
Parameters:battery4val[0],battery4val[1];
******************************************/
void Get_Battery4(void)
{
byte readbattbuf[2];
dword batteryval1 ;
readbattbuf[1]=IIC_read_one_byte(0x14);
readbattbuf[0]=IIC_read_one_byte(0x15);
batteryval1= readbattbuf[1];
batteryval1= (batteryval1 << 8) |readbattbuf[0];
batteryval1=((batteryval1*AD_Gain)/1000)+ADC_offset;//单体电压计算公式,第4串
battery4val[1]=(byte)(batteryval1 >> 8);
battery4val[0]=(byte)(batteryval1 >> 0);
}
/****************************************
fuction: void Get_Battery5(void)
description:获取第5号单体电池电压
Parameters:battery5val[0],battery5val[1];
******************************************/
void Get_Battery5(void)
{
byte readbattbuf[2];
dword batteryval1 ;
readbattbuf[1]=IIC_read_one_byte(0x16);
readbattbuf[0]=IIC_read_one_byte(0x17);
batteryval1= readbattbuf[1];
batteryval1= (batteryval1 << 8) |readbattbuf[0];
batteryval1=((batteryval1*AD_Gain)/1000)+ADC_offset;//单体电压计算公式,第5串
battery5val[1]=(byte)(batteryval1 >> 8);
battery5val[0]=(byte)(batteryval1 >> 0);
}
/****************************************
fuction: void Get_Battery5(void)
description:获取第6号单体电池电压
Parameters:battery6val[0],battery6val[1];
******************************************/
void Get_Battery6(void)
{
byte readbattbuf[2];
dword batteryval1 ;
readbattbuf[1]=IIC_read_one_byte(0x18);
readbattbuf[0]=IIC_read_one_byte(0x19);
batteryval1= readbattbuf[1];
batteryval1= (batteryval1 << 8) |readbattbuf[0];
batteryval1=((batteryval1*AD_Gain)/1000)+ADC_offset;//单体电压计算公式,第6串
battery6val[1]=(byte)(batteryval1 >> 8);
battery6val[0]=(byte)(batteryval1 >> 0);
}
/****************************************
fuction: void Get_Battery7(void)
description:获取第7号单体电池电压
Parameters:battery7val[0],battery7val[1];
******************************************/
void Get_Battery7(void)
{
byte readbattbuf[2];
dword batteryval1 ;
readbattbuf[1]=IIC_read_one_byte(0x1a);
readbattbuf[0]=IIC_read_one_byte(0x1b);
batteryval1= readbattbuf[1];
batteryval1= (batteryval1 << 8) |readbattbuf[0];
batteryval1=((batteryval1*AD_Gain)/1000)+ADC_offset;//单体电压计算公式,第7串
battery7val[1]=(byte)(batteryval1 >> 8);
battery7val[0]=(byte)(batteryval1 >> 0);
}
/****************************************
fuction: void Get_Battery8(void)
description:获取第8号单体电池电压
Parameters:battery8val[0],battery8val[1];
******************************************/
void Get_Battery8(void)
{
byte readbattbuf[2];
dword batteryval1 ;
readbattbuf[1]=IIC_read_one_byte(0x1e);
readbattbuf[0]=IIC_read_one_byte(0x1f);
batteryval1= readbattbuf[1];
batteryval1= (batteryval1 << 8) |readbattbuf[0];
batteryval1=((batteryval1*AD_Gain)/1000)+ADC_offset;//单体电压计算公式,第8串
battery8val[1]=(byte)(batteryval1 >> 8);
battery8val[0]=(byte)(batteryval1 >> 0);
}
/****************************************
fuction: void Get_Battery1_5(void)
description:获取第1,5号单体电池电压
Parameters:battery8val[0],battery8val[1];
******************************************/
void Get_Battery1_5(void)
{
get_offset();
Get_Battery1();
Get_Battery5();
}
/****************************************
fuction: void Get_Battery2_6(void)
description:获取第2,6号单体电池电压
Parameters:battery8val[0],battery8val[1];
******************************************/
void Get_Battery2_6(void)
{
Get_Battery2();
Get_Battery6();
}
/****************************************
fuction: void Get_Battery3_7(void)
description:获取第1,5号单体电池电压
Parameters:battery8val[0],battery8val[1];
******************************************/
void Get_Battery3_7(void)
{
Get_Battery3();
Get_Battery7();
}
/****************************************
fuction: void Get_Battery15(void)
description:获取第4,8号单体电池电压
Parameters:battery8val[0],battery8val[1];
******************************************/
void Get_Battery4_8(void)
{
get_offset();
Get_Battery4();
Get_Battery8();
}
/****************************
获取所有电池的电压值
*****************************/
void Get_Data(void)
{
get_offset();
Get_Battery1();
Get_Battery2();
Get_Battery3();
Get_Battery4();
Get_Battery5();
Get_Battery6();
Get_Battery7();
Get_Battery8();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
/**void OV_UV_PROTTECT(void)
{
byte OVTrip,UVTrip;
OVTrip = (byte)(((word)((OVPThreshold - ADC_offset)/(AD_Gain*1.0)*1000 + 0.5) - OV_THRESH_BASE))>>4);
UVTrip = (byte)(((word)((UVPThreshold - ADC_offset)/(AD_Gain*1.0)*1000 + 0.5) - UV_THRESH_BASE))>>4);;
WRITE_IIC(0x09,OVTrip);
WRITE_IIC(0x0a,UVTrip);
}*/
/****************************************
fuction: void BQ_config(void)
description:BQ76930初始化
Parameters: None
******************************************/
void BQ_config(void)
{
byte i;
for(i=0;i<11;i++)
{
WRITE_IIC(BQ769_INITAdd[i],BQ769_INITdata[i]);
}
}
/****************************************
fuction:void SHIP_BQ769(void)
description:BQ76930进入低功耗模式
Parameters: None
******************************************/
void SHIP_BQ769(void)
{
WRITE_IIC(0x04,0x19); //01
delay();
WRITE_IIC(0x04,0x1a); //10
}
/****************************************
fuction:void readbqstate(void)
description:读取BQ76930寄存器的值,并通过CAN接口发送
Parameters: None
******************************************/
void readbqstate(void)
{
cansendbuf[0] = IIC_read_one_byte(0x00);//SYS_STAT寄存器值
cansendbuf[1] = IIC_read_one_byte(0x04);//SYS_CTRL1寄存器值
cansendbuf[2] = IIC_read_one_byte(0x05);//SYS_CTRL2寄存器值
cansendbuf[3] = IIC_read_one_byte(0x06);//PROTECT1寄存器值
cansendbuf[4] = IIC_read_one_byte(0x07);//PROTECT2寄存器值
cansendbuf[5] = IIC_read_one_byte(0x08);//PROTECT3寄存器值
cansendbuf[6] = IIC_read_one_byte(0x09);//OV_TRIP寄存器值
cansendbuf[7] = IIC_read_one_byte(0x0b);//CC_CFG寄存器值
(void)CAN1_SendFrameExt(0x0028,0x00,8,cansendbuf);//CAN发送
}
/****************************************
fuction:void readbqstate(void)
description:读取报警信号值
Parameters: UV_Alarm_flagOV_Alarm_flag
SCD_Alarm_flag,OCD_Alarm_flag
******************************************/
void ALERT_Recognition(void )
{
byte sys_stat,UV,OU,SCD,OCD;
sys_stat = IIC_read_one_byte(0x00);//获取状态
UV = sys_stat&0x08; //取出UV位
OU = sys_stat&0x04;
SCD = sys_stat&0x02;
OCD = sys_stat&0x01;
if(UV == 0x08)
{
UV_Alarm_flag = 1; //欠压报警
PTAD_PTAD2 = 0;
}
else
UV_Alarm_flag = 0; //没有欠压
if(OU == 0x04)
{
OV_Alarm_flag = 1;
PTAD_PTAD1 = 0;
}
else
OV_Alarm_flag = 0;
if(SCD == 0x02)
{
SCD_Alarm_flag = 1;
PTAD_PTAD1 = 0;
PTAD_PTAD2 = 0;
}
else
SCD_Alarm_flag = 0;
if(OCD == 0x01)
{
OCD_Alarm_flag = 1;
PTAD_PTAD1 = 0;
PTAD_PTAD2 = 0;
}
else
OCD_Alarm_flag = 0;
}