📄 ds2438.c
字号:
#include "reg51.h"
#include <intrins.h>
#define Ds2438 0
#define Standard 1
#define uint unsigned int
#define uchar unsigned char
#if (Standard) //24M 0.5*8=4 快速读写DQ,1.5us
/*#define A 6/2
#define B 64/2
#define C 60/2
#define D 10/2
#define E 4/2
#define F 55/2
#define G 0/2
#define H 480/2
#define I 70/2
#define J 410/2
*/
#define A 4/2
#define B 20 //80us
#define C 20
#define D 5
#define E 8/4
#define F 55/4
#define G 0/4
#define H 130
#define I 70/4
#define J 110
#else
#define A 1
#define B 10
#define C 9
#define D 2
#define E 1
#define F 8
#define G 0
#define H 70
#define I 9
#define J 52
#endif
sbit DQ=P1^6;
void Delay(uint a) //8个state
{
while(a)
a--;
}
void WriteBit(bit Num)
{ EA=0;
if (Num)
{
// Write '1' bit
DQ=0; // Drives DQ low
_nop_();
_nop_();
_nop_();
_nop_();
DQ=1; // Releases the bus
Delay(B); // Complete the time slot and 10us recovery
}
else
{
// Write '0' bit
DQ=0; // Drives DQ low
Delay(C);
DQ=1; // Releases the bus
Delay(D);
}
EA=1;
}
bit Reset(void)
{
bit result;
EA=0;
Delay(G);
DQ=0; // Drives DQ low
Delay(H);
DQ=1; // Releases the bus
Delay(I);
result= DQ; // Sample for presence pulse from slave
Delay(J); // Complete the reset sequence recovery
DQ=1;
EA=1;
return result; // Return sample presence pulse result
}
/*bit ReadBit(void)
{
bit result;
DQ=0; // Drives DQ low
Delay(A);
//DQ=1; // Releases the bus
P1M1|=0x1;
//P1M2=0;
Delay(E);
result= DQ; // Sample the bit value from the slave
Delay(F); // Complete the time slot and 10us recovery
P1M1&=0xfe;
DQ=1;
return result;
}
*/
bit ReadBit(void)
{
bit result;
EA=0;
DQ=0; // Drives DQ low
_nop_();
_nop_();
_nop_();
DQ=1; // Releases the bus
_nop_();
_nop_();
_nop_(); result= DQ; // Sample the bit value from the slave
Delay(F); // Complete the time slot and 10us recovery
EA=1;
return result;
}
void WriteByte(uchar databuf)
{
uchar loop;
for (loop = 0; loop < 8; loop++)
{
WriteBit(databuf & 0x01);
databuf >>= 1;
}
}
uchar ReadByte(void)
{
uchar loop, result=0;
for (loop = 0; loop < 8; loop++)
{
result >>= 1;
if (ReadBit())
result |= 0x80;
}
return result;
}
#if Ds2438
struct Page0{
uchar state;
uchar Teml;
char Temh;
uchar Voll;
uchar Volh;
uint Cur;
uchar ThresHold;
uchar CrcCode;
};
struct Page0 idata *PPage0;
struct Page1{
long state;
uchar Capacity;
uint Offset;
uint state1;
};
struct Page1 idata *PPage1;
uchar idata page_data[9];
uint VolValue,CurValue;
int TemValue;
uchar ICA;
uchar ReadPage()
{
uchar i;
if (Reset()) // Reset the 1-Wire bus
return 0; // Return if no devices found
WriteByte(0xcc);
WriteByte(0x44);
while(!ReadBit());
Reset();
WriteByte(0xcc);
WriteByte(0xb4);
while(!ReadBit());
Reset();
WriteByte(0xcc); // Send Skip ROM command to select single device
WriteByte(0xb8);
WriteByte(0);
while(!ReadBit());
Reset();
WriteByte(0xcc);
// read the page
WriteByte(0xbe);
WriteByte(0);
// read the page data
for (i = 0; i <9; i++)
page_data[i] = ReadByte();
Reset();
PPage0=(struct Page0 idata *)page_data;
TemValue=PPage0->Temh;
if(TemValue<0)
TemValue=TemValue+1;
VolValue=PPage0->Volh;
VolValue=VolValue*0x100+PPage0->Voll;
CurValue=PPage0->Cur;
CurValue=PPage0->Cur>>8;
CurValue=PPage0->Cur<<8|CurValue;
CurValue=(CurValue*10000l)/1024;//电流单位ma
Reset();
WriteByte(0xcc); // Send Skip ROM command to select single device
WriteByte(0xb8);
WriteByte(1);
while(!ReadBit());
Reset();
WriteByte(0xcc);
// read the page
WriteByte(0xbe);
WriteByte(1);
// read the page data
for (i = 0; i <9; i++)
page_data[i] = ReadByte();
PPage1=(struct Page1 idata *)page_data;
ICA=PPage1->Capacity;
/*Buf=(OldICA+ICA)/2
Buf1=Buf-ICA;
if(Buf1>=0)
{
if(Buf1>25)
ICA=OldICA;
else
OldICA=ICA;
}
else
{
if(Buf1<-25)
ICA=OldICA;
else
OldICA=ICA;
}*/
if (Reset()) // Reset the 1-Wire bus
return 0;
return 1;
}
#else
struct Page0{
uchar Volh;
uchar Voll;
uchar Curh;
uchar Curl;
uchar ICAh;
uchar ICAl;
};
struct Page0 idata *PPage0;
uchar idata page_data[6];
uint VolValue,CurValue=0;
int TemValue;
uint ICA;
uchar ReadPage()
{
uchar i;
if (Reset()) // Reset the 1-Wire bus
return 0; // Return if no devices found
WriteByte(0xcc);
WriteByte(0x69);
WriteByte(0x0c);
for (i = 0; i <6; i++)
page_data[i] = ReadByte();
Reset();
PPage0=(struct Page0 idata *)page_data;
VolValue=PPage0->Volh;
VolValue=VolValue*0x100+PPage0->Voll;
VolValue=(VolValue>>5)*4.88;
CurValue=PPage0->Curh;
CurValue=CurValue*0x100+PPage0->Curl;
CurValue=(CurValue>>6)*5;//电流单位ma
if(CurValue==5115)
CurValue=0;
ICA=PPage0->ICAh;
ICA=ICA*0x100+PPage0->ICAl;
ICA>>=2;
Reset();
if (Reset()) // Reset the 1-Wire bus
return 0;
return 1;
}
void ClrICA()
{
Reset();
WriteByte(0xcc);
WriteByte(0x6c);
WriteByte(0x10);
WriteByte(0);
WriteByte(0);
Reset();
}
#endif
/*void WritePage()
{
if (Reset()) // Reset the 1-Wire bus
return ; // Return if no devices found
WriteByte(0xCC); // Send Skip ROM command to select single device
// read the page
WriteByte(0xb4);
//WriteByte(3);
//WriteByte(0xff);
Reset();
}*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -