本帖最后由 hlln20100410 于 2015-4-17 15:34 编辑
使用DS2438芯片测量电池的电压、温度、电流,都没有问题;
可最终要测量的是电池的剩余容量,看芯片手册进行函数操作,可是读出来的ICA CCA DCA寄存器都是0,不知哪位前辈用过该芯片,小弟虚心求教
程序和电路如下,读取电池剩余容量时只读了第四个字节ICA寄存器,仿真看值是0;
/******************************************************************
******函数名称:读取端口电压值
*****返回值:无
*****作者:
*****时间:2015年3月31日10:55:54
*******************************************************************/
unsigned int DS2438_Read_Voltage(void)
{
unsigned char temp_V[9],i;
float Voltage;
i= Reset_DS2438();
while(i==0x80);
write_byte(0xcc);
write_byte(0xB4); //0xB4
delay_ms(10);
i= Reset_DS2438();
while(i==0x80);
write_byte(0xcc);
write_byte(0xB8); //0xB8
write_byte(0x00);
i= Reset_DS2438();
while(i==0x80);
write_byte(0xCC);
write_byte(0XBE); //0xBE
write_byte(0x00);
for(i=0;i<9;i++)
{
temp_V=read_byte();
}
Voltage=((temp_V[4]&0x03)*256+temp_V[3])*0.01;
return(Voltage);
}
/******************************************************************
******读取当前电池温度
*****返回值:无
*****作者:
*****时间:2015年3月31日10:55:54
*******************************************************************/
unsigned int Read_Temperature(void)
{
unsigned char g[9];
unsigned char i,j;
unsigned int u;
float temperature;
union
{
unsigned int temp16;
struct
{
unsigned char bytel;
unsigned char byteh;
};
}word;
i= Reset_DS2438() ;
while(i==0x80);
write_byte(0xCC); //Skip ROM
write_byte(0x44); // Start Conversion
delay_ms(30);
i= Reset_DS2438() ;
while(i==0x80);
write_byte(0xCC); //Skip ROM
write_byte(0xB4); // Start Conversion
i= Reset_DS2438() ;
while(i==0x80);
write_byte(0xCC); // Skip ROM
write_byte(0xB8); //READ SCRATCHPAD
write_byte(0x00); //READ 0TH PAGE
i=Reset_DS2438() ;
while(i==0x80);
write_byte(0xCC); // Skip ROM
write_byte(0xBE); // Read Scratch Pad
write_byte(0x00);
for(j=0;j<9;j++)
{
g[j]=read_byte();
}
word.byteh=g[2];
word.bytel=g[1];
u=word.temp16>>3;
temperature=u*0.03125;
return (temperature);
}
/******************************************************************
******读取当前电池读电流
*****返回值:CURENT y=(-0.2441X+250)/500;计算电流
*****作者:
*****时间:2015年4月11日16:12:13
*******************************************************************/
unsigned int DS2438_Read_Current(void)
{
unsigned char temp_C[9];
unsigned int i,aac;
float Current,temp_1;
initcommand();
write_byte(0xCC);
write_byte(0XB4);
delay_ms(200);
i= Reset_DS2438();
while(i==0x80);
write_byte(0xCC);
write_byte(0XB8); //0xB8
write_byte(0x00);
delay_ms(200);
i= Reset_DS2438();
while(i==0x80);
write_byte(0XCC);
write_byte(0XBE); //0xBE
write_byte(0x00);
delay_ms(200);
for(i=0;i<9;i++)
{
temp_C=read_byte();
}
temp_C[6]= temp_C[6]&0x03;
aac=temp_C[6]<<8;
aac=aac+temp_C[5];
Current=(-0.2441*aac+250)/500.0;//单位mA V=-0.2441X+250;X是二进制数据
return( Current);
}
/////////////////////////////////////////////////////////////////////////////////////////////
//读电池的当前剩余容量
unsigned char Read_ICAcurrent(void)
{ // unsigned char a[9];
unsigned char d[9];
unsigned char i,j;
float p;
initcommand();
i= Reset_DS2438() ;
while(i==0x80);
//write_byte(0xCC); // Skip ROM
// write_byte(0x4E);
//write_byte(0x00);
//write_byte(0x0F);
//i= Reset_DS2438() ;
//while(i==0x80);
write_byte(0xCC); // Skip ROM
write_byte(0xB8);
write_byte(0x01);
i= Reset_DS2438() ;
while(i==0x80);
write_byte(0xCC); // Skip ROM
write_byte(0xBE);
write_byte(0x01);
for(j=0;j<9;j++)
{
d[j]=read_byte();
}
p =d[4]/(2048*0.5);
return (p);
}
|