void DS18B20_ReSet(void)
{
uchar i;
DQ = 0;
i = 240;
while (--i);
DQ = 1;
i = 30;
while (--i);
while (~DQ);
i = 4;
while (--i);
void DS18B20_WriteByte(uchar dat)
{
uchar j;
uchar btmp;
for (j = 0; j < 8; j++)
{
btmp = 0x01;
btmp = btmp << j;
btmp = btmp & dat;
if (btmp > 0) // D′1
{
DQ = 0;
Delay15us();
DQ = 1;
Delay15us();
Delay15us();
Delay15us();
Delay15us();
}
else //
{
DQ = 0;
Delay15us();
Delay15us();
Delay15us();
Delay15us();
DQ = 1;
Delay15us();
}
}
int DS18B20_ReadTemp(void)
{
uchar j;
int b, temp = 0;
EA = 0;
DS18B20_ReSet();
DS18B20_WriteByte(0xcc);
DS18B20_WriteByte(0x44);
DS18B20_ReSet();
DS18B20_WriteByte(0xcc);
DS18B20_WriteByte(0xbe);
for (j = 0; j < 16; j++)
{
DQ = 0;
_nop_();
_nop_();
DQ = 1;
Delay15us();
b = DQ;
Delay15us();
Delay15us();
Delay15us();
b = b << j;
temp = temp | b;
}
EA = 1;
temp = temp * 0.0625+0.5;
return (temp);
}
void WaitForEnable(void)
{
P0 = 0xff;
LcdRs_P = 0;
LcdRw_P = 1;
_nop_();
LcdEn_P = 1;
_nop_();
_nop_();
while (P0 & 0x80);
LcdEn_P = 0;
}
|