程序中的问题,大神帮忙看看,这是DS18B20的那部分程序,大家帮看看在匹配ROM哪有错?
时间:10-02
整理:3721RD
点击:
#include <reg51.H>
#include <Intrins.h>
#include "DS18B20.H"
#define uchar unsigned char
#define uint unsigned int
sbit DQ = P2^7;
const unsigned char code ROMData1[8] = {0x28, 0xFF, 0x34, 0xE2, 0x38, 0x16, 0x04, 0x41}; //U1
const unsigned char code ROMData2[8] = {0x28, 0xFF, 0x33, 0x60, 0x38, 0x16, 0x03, 0x4D}; //U2
const unsigned char code ROMData3[8] = {0x28, 0xFF, 0x36, 0xE8, 0x38, 0x16, 0x04, 0xD9}; //U3
const unsigned char code ROMData4[8] = {0x28, 0xFF, 0x7B, 0xE7, 0x38, 0x16, 0x04, 0x46}; //U4
const unsigned char code ROMData5[8] = {0x28, 0xFF, 0x9A, 0x1A, 0x39, 0x16, 0x04, 0x16}; //U5
const unsigned char code ROMData6[8] = {0x28, 0xFF, 0x96, 0x17, 0x39, 0x16, 0x04, 0xAA}; //U6
const unsigned char code ROMData7[8] = {0x28, 0xFF, 0xA0, 0x5F, 0x38, 0x16, 0x03, 0x66}; //U7
const unsigned char code ROMData8[8] = {0x28, 0xFF, 0x5C, 0x60, 0x38, 0x16, 0x03, 0x33}; //U8
void Delay16us()
{
unsigned char a;
for (a = 0; a < 4; a++);
}
void Delay60us()
{
unsigned char a;
for (a = 0; a < 18; a++);
}
void Delay480us()
{
unsigned char a;
for (a = 0; a < 158; a++);
}
void Delay240us()
{
unsigned char a;
for (a = 0; a < 78; a++);
}
void Delay500ms()
{
unsigned char a, b, c;
for (a = 0; a < 250; a++)
for (b = 0; b < 3; b++)
for (c = 0; c < 220; c++);
}
void Initialization()
{
while(1)
{
DQ = 0;
Delay480us();
DQ = 1;
Delay60us();
if(!DQ)
{
DQ = 1;
Delay240us();
break;
}
}
}
void WriteByte(unsigned char btData)
{
unsigned char i, btBuffer;
for (i = 0; i < 8; i++)
{
btBuffer = btData >> i;
if (btBuffer & 1)
{
DQ = 0;
_nop_();
_nop_();
DQ = 1;
Delay60us();
}
else
{
DQ = 0;
Delay60us();
DQ = 1;
}
}
}
unsigned char ReadByte()
{
unsigned char i, btDest;
for (i = 0; i < 8; i++)
{
btDest >>= 1;
DQ = 0;
_nop_();
_nop_();
DQ = 1;
Delay16us();
if (DQ) btDest |= 0x80;
Delay60us();
}
return btDest;
}
//DòáDo??¥??
void MatchROM(const unsigned char *pMatchData)
{
unsigned char i;
Initialization();
WriteByte(MATCH_ROM);
for (i = 0; i < 8; i++) WriteByte(*(pMatchData + i));
}
/*void GetROMSequence()
{
unsigned char i;
Initialization();
WriteByte(READ_ROM);
for (i = 0; i < 8; i++)
P1 = ReadByte();
}*/
TEMPDATA ReadTemperature()
{
TEMPDATA TempData;
unsigned int iTempDataH;
unsigned char btDot, iTempDataL;
static unsigned char i = 0;
TempData.btNegative = 0;
i++;
if (i == 9) i = 1;
Initialization();
WriteByte(SKIP_ROM);
WriteByte(TEMP_SWITCH);
Delay500ms();
Delay500ms();
Initialization();
switch (i)
{
case 1 : MatchROM(ROMData1); break; //?¥??1
case 2 : MatchROM(ROMData2); break; //?¥??2
case 3 : MatchROM(ROMData3); break; //?¥??3
case 4 : MatchROM(ROMData4); break; //?¥??4
case 5 : MatchROM(ROMData5); break; //?¥??5
case 6 : MatchROM(ROMData6); break; //?¥??6
case 7 : MatchROM(ROMData7); break; //?¥??7
case 8 : MatchROM(ROMData8); break; //?¥??8
}
//WriteByte(SKIP_ROM);
WriteByte(READ_MEMORY);
iTempDataL = ReadByte();
iTempDataH = ReadByte();
iTempDataH <<= 8;
iTempDataH |= iTempDataL;
if (iTempDataH & 0x8000)
{
TempData.btNegative = 1;
iTempDataH = ~iTempDataH + 1;
}
btDot = (unsigned char)(iTempDataH & 0x000F);
iTempDataH >>= 4;
btDot *= 5;
btDot >>= 3;
TempData.btThird = (unsigned char)iTempDataH / 100;
TempData.btSecond = (unsigned char)iTempDataH % 100 / 10;
TempData.btFirst = (unsigned char)iTempDataH % 10;
TempData.btDecimal = btDot;
return TempData;
}
已退回5积分