📄 romserch.c
字号:
#include "DS18B20.h"
//Rev CRC8 register
//需要在使用前由调用它的程序将其清零
unsigned char revCRC8;
//temp memory of 18B20 ROM data, using for search command
//the last two byte contains the search infomation
unsigned char B20ROMTemp[9];
#define LastUnknow B20ROMTemp[8]
unsigned char DetectB20()
{
unsigned char i=ReDetectTime;
do{
PullDownDQ(); //Pull DQ to Low votage
Delay480us(); //Delay 480us
ReleaseDQ(); //Release DQ to High
Delay60us(); //Delay 60us
if(!B20DQ) //is DQ is Low, Detect Success, return 1
{ Delay480us();
return 1;
}
Delay480us();
}while(--i);
//Detect over time
return 0;
}
void WriteB20Byte(unsigned char tmp)
{
unsigned char i=8;
do{
// the LSB tmp bit will write to 18B20
WriteB20Bit(&tmp);
tmp>>=1;
}while(--i);
}
unsigned char ReadB20Byte()
{
unsigned char tmp;
unsigned char i=8;
do{
tmp>>=1;
//The bit value will write to LSB of tmp
ReadB20Bit(&tmp);
}while(--i);
return tmp;
}
unsigned char ReadB20ROM(unsigned char *ROM)
{
unsigned char i=8;
revCRC8=0;
if(DetectB20())
{
WriteB20Byte(ReadROM);
do{
*ROM=ReadB20Byte();
ROM++;
}while(--i);
if(revCRC8)
return CRCError;
return 0;
}
return OverTime;
}
unsigned char ReadEERAM(unsigned char *RAM)
{
unsigned char i=9;
revCRC8=0;
if(DetectB20())
{
WriteB20Byte(SkipROM);
WriteB20Byte(B20ReadScr);
do{
*RAM=ReadB20Byte();
RAM++;
}while(--i);
if(revCRC8)
return CRCError;
return 0;
}
return OverTime;
}
void Delay10us(unsigned char tmp)
{
unsigned char i;
do{
for(i=5;--i;);
}while(--tmp);
}
unsigned char SearchDevice(unsigned char FirstNext)
{ //*
unsigned char i,tmp;
unsigned char *j=B20ROMTemp;
unsigned char ROMOut; //will read out ROM
unsigned char ThisUnknow = 0xFF;
if(FirstNext==FindFirst)
{
for(;j<(B20ROMTemp+8);j++)
*j=0;
j=B20ROMTemp;
LastUnknow=ThisUnknow;
}
if(!DetectB20())
return OverTime;
WriteB20Byte(SearchROM);
for(i=0;!(i&64);)
{
if(!(i&0x07)) // Load old ROM infomation
ROMOut=*j;
tmp=0;
ReadB20Bit(&tmp);
tmp>>=1;
ReadB20Bit(&tmp);
// I'll judge the device by highest two bits of tmp
// 00 there has multiple device
// 01 the first bit is 1
// 10 the first bit is 0
// 11 there has no device
if(tmp&0x40){ // if the sytle is 1x
if(tmp&0x80) // 11
return OverTime; //no device so return directly
tmp|=1; // 10 this bit is 1 so write 1 to device
}
else{ // sytle is 0x
if(!(tmp&0x80)){ //00 there has multiple device
//there has two choice there
//if the bit here set last time is 1
//this is a normal bit, do as read 10
if( (ROMOut&0x01) || (LastUnknow==i) )
tmp|=1;
else // if it not the LastUnknoe bit
//set it to thisunknow bit
//and set it to 0 , write it to 0
ThisUnknow=i;
}
}
WriteB20Bit(&tmp);
ROMOut>>=1;
if(tmp&0x01)
ROMOut|=0x80;
if(!((++i)&0x07))
*j++=ROMOut ; // Save new ROM infomation
}
LastUnknow=ThisUnknow; //set new unknow bit
if(ThisUnknow==0xFF) //if ThisUnknow didn't change,
return SearchFinish; //there is no unknown bit
return 0; //if there still have unknow bit, just return 0
}
void WriteB20Bit(unsigned char *tp)
{
PullDownDQ();
NOP();
if((*tp)&0x01)
{
ReleaseDQ();
}
Delay60us();
ReleaseDQ();
}
void ReadB20Bit(unsigned char *tp)
{
PullDownDQ();
NOP();
NOP();
ReleaseDQ();
NOP();
revCRC8>>=1;
if(CARRY)
revCRC8^=v_revCRC8;
if(B20DQ)
{
(*tp)|=0x80;
revCRC8^=v_revCRC8;
}
Delay60us();
}
unsigned char StartConvertTp()
{
if(DetectB20())
{
WriteB20Byte(SkipROM);
WriteB20Byte(B20Convert);
return 0;
}
return OverTime;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -