⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ds18b20.c

📁 这是广西大学生设计大赛的源码
💻 C
字号:


#include "ds18b20.h"
#include "lcd.h"
#include <REGX51.H>

 


#define OWB   P3_0

 

BYTE   idata owb_nCntDevice = 0;
BYTE   idata owb_CacheDeviceIndex = 0xff;
OWBROM idata owb_CacheDeviceROM;
BYTE   idata owb_SearchDeviceIndex = 0xff;
OWBROM idata owb_SearchDeviceROM;


//////////////////////////////////////////////////////////////////
//
//               ds18b20 functions


void owbDeviceSave(BIT withSearchDevice)
{
 BYTE i;
  
 owbInit();
 owbWriteByte( 0x55 );
 for (i=0;i<8;i++)
 {
  if (withSearchDevice)
   owbWriteByte( owb_SearchDeviceROM.b[i] );
  else
   owbWriteByte( owb_CacheDeviceROM.b[i] );
 }
 owbWriteByte( 0x48 );
}


void owbReadMeasure(TEMPE * pT, BIT withSearchDevice)
{
 BYTE i;

 owbInit();
 owbWriteByte( 0x55 );
 for (i=0;i<8;i++)
 {
  if (withSearchDevice)
   owbWriteByte( owb_SearchDeviceROM.b[i] );
  else
   owbWriteByte( owb_CacheDeviceROM.b[i] );
 }
 owbWriteByte( 0xbe );
 pT->tl = owbReadByte();
 pT->th = owbReadByte();

  owbInit();
 owbWriteByte( 0x55 );
 for (i=0;i<8;i++)
 {
  if (withSearchDevice)
   owbWriteByte( owb_SearchDeviceROM.b[i] );
  else
   owbWriteByte( owb_CacheDeviceROM.b[i] );
 }
 owbWriteByte( 0x44 );
}


void owbReadAlarmValue(BYTE * pTH, BYTE * pTL, BIT withSearchDevice)
{
 BYTE i;

 owbInit();
 owbWriteByte( 0x55 );
 for (i=0;i<8;i++)
 {
  if (withSearchDevice)
   owbWriteByte( owb_SearchDeviceROM.b[i] );
  else
   owbWriteByte( owb_CacheDeviceROM.b[i] );
 }
 owbWriteByte( 0xbe );
 owbReadByte();
 owbReadByte();
 pTH[0] = owbReadByte();
 pTL[0] = owbReadByte();
}


void owbWriteConfig(BYTE th, BYTE tl, BYTE config, BIT withSearchDevice)
{
 BYTE i;
  
 owbInit();
 owbWriteByte( 0x55 );
 for (i=0;i<8;i++)
 {
  if (withSearchDevice)
   owbWriteByte( owb_SearchDeviceROM.b[i] );
  else
   owbWriteByte( owb_CacheDeviceROM.b[i] );
 }
 owbWriteByte( 0x4E );
 owbWriteByte( th );
 owbWriteByte( tl );
 owbWriteByte( config );

 owbDeviceSave(withSearchDevice);
}


void owbDeviceInit(BIT withSearchDevice)
{
 BYTE i,th,tl;
 
 //load config 
 owbInit();
 owbWriteByte( 0x55 );
 for (i=0;i<8;i++)
 {
  if (withSearchDevice)
   owbWriteByte( owb_SearchDeviceROM.b[i] );
  else
   owbWriteByte( owb_CacheDeviceROM.b[i] );
 }
 owbWriteByte( 0xB8 );

 owbReadAlarmValue( &th, &tl, withSearchDevice );
 owbWriteConfig( th, tl, OWB_DEFAULTCONFIG, withSearchDevice );
}

 

//////////////////////////////////////////////////////////////////
//               for debug only
/*
void owbDebugMain(void)
{
}
*/
 

////////////////////////////////////////////////////////////
//  1-wire 位操作

BIT owbInit(void)
{
 BYTE save = ACC;
 BIT rt;

//    P3 = 0x00;  //for debug only (supply power)
//    P3_2 = 1;  //for debug only (supply power)

 // write '0'
 ACC = 250;
 do{
  OWB = 0;
  ACC--;
 }while(ACC);
 // write '1'
 ACC = 10;
 do{
  OWB = 1;
  ACC--;
 }while(ACC);
 rt = ~OWB;
 ACC = 250;
 do{
  OWB = 1;
  ACC--;
 }while(ACC);


 ACC = save;

 return rt;
}

BIT owbReadBit(void)
{
 BIT rt;
 BYTE save;

 OWB = 0; OWB = 0;
 OWB = 1; OWB = 1; OWB = 1; OWB = 1;
 OWB = 1; OWB = 1; OWB = 1; OWB = 1;
 rt = OWB;

 save = ACC;
 ACC = 45;
 do{
  OWB = 1;
  ACC--;
 }while(ACC);
 ACC = save;

 return rt;
}

void owbWriteBit(BIT b)
{
 BYTE save = ACC;
 ACC = 30;

 if (b)
 {
  // write '1'
  OWB = 0; OWB = 0;
  do{
   OWB = 1;
   ACC--;
  }while(ACC);
 }
 else
 {
  // write '0'
  do{
   OWB = 0;
   ACC--;
  }while(ACC);
 }
 OWB = 1; OWB = 1; OWB = 1; OWB = 1;
 OWB = 1; OWB = 1; OWB = 1; OWB = 1;

 ACC = save;
}


//////////////////////////////////////////////////////////////////
//               字节操作

void owbWriteByte(BYTE byte)
{
 BYTE i;
 EA = 0;
 for (i=0;i<8;i++)
 {
  owbWriteBit(byte & 0x01);
  byte>>=1;
 }
 EA = 1;
}


BYTE owbReadByte(void)
{
 BYTE i,byte;
 EA = 0;
 byte = 0;
 for (i=0;i<8;i++)
 {
  byte>>=1;
  if (owbReadBit()) byte|=0x80;
 }
 EA = 1;
 return byte;
}

 

//////////////////////////////////////////////////////////////////
//               搜索操作


#define OWBS_AND_0 0x00
#define OWBS_AND_1 0x04
#define OWBS_NAN_0 0x00
#define OWBS_NAN_1 0x02
#define OWBS_SER_0 0x00
#define OWBS_SER_1 0x01


BIT owbGetRomBit(OWBROM * pRom, BYTE nBit)
{
 // 低位在先!
 nBit %= 64;
 return (((pRom->b[nBit / 8]) >> (nBit % 8)) & 0x01);
}

void owbSetRomBit(OWBROM * pRom, BYTE nBit)
{
 // 低位在先!
 BYTE mask = 1;
 nBit %= 64;
 mask <<= (nBit % 8);
 (pRom->b[nBit / 8]) |= mask;
}

void owbClrRomBit(OWBROM * pRom, BYTE nBit)
{
 // 低位在先!
 BYTE mask = 1;
 nBit %= 64;
 mask <<= (nBit % 8);
 (pRom->b[nBit / 8]) &= (~mask);
}


BIT owbIncRom(OWBROM * pRom, BYTE nBit)
// return 1 = 溢出 (将设 pRom = '111...111')
// nBit += '1'(并且可向前进位), all after nBit setted '0'
{
 char i;
 BIT c;

 c = 1;
 nBit %= 64;

 for (i=nBit; i>=0; i--)
 {
  if ( owbGetRomBit(pRom,i) )
  {
   if (c)
   {
//    c = 1;
    owbClrRomBit(pRom,i);
   }
   else
   {
//    c = 0;
//    owbSetRomBit(pRom,i);
   }
  }
  else
  {
   if (c)
   {
    c = 0;
    owbSetRomBit(pRom,i);
   }
   else
   {
//    c = 0;
//    owbClrRomBit(pRom,i);
   }
  }
 }

 if (c)
 {
  // 溢出
  for (i=0; i<8; i++) pRom->b[i]=0xff;
  return 1;
 }

 for (i=nBit+1; i<64; i++)
 {
  owbClrRomBit(pRom,i);
 }

 return 0;
}


void owbSetSearchEnd(OWBROM * pRomWant)
{
 BYTE dp;
 for (dp=0;dp<8;dp++) pRomWant->b[dp] = 0xff;
}


BOOL owbIsSearchEnd(OWBROM * pRomWant)
{
 //检测是否已搜索结束
 BYTE dp,status = 0xff;
 for (dp=0;dp<8;dp++) status &= pRomWant->b[dp];

 if (status == 0xff) return TRUE;
 return FALSE;
}

BOOL owbSearchPath(OWBROM * pRomWant, BYTE cmd)
{
 BYTE dp,status;

 owbInit();
 owbWriteByte( (cmd==0xF0) ? 0xF0 : 0xEC );

 EA = 0;
 dp=0;
 while (dp < 64)
 {
  status = 0;
  if ( owbReadBit() ) status |= OWBS_AND_1;
  if ( owbReadBit() ) status |= OWBS_NAN_1;
  if ( owbGetRomBit(pRomWant,dp) ) status |= OWBS_SER_1;

  switch (status)
  {
  case (OWBS_AND_0 | OWBS_NAN_0 | OWBS_SER_0):
   owbWriteBit(0);
   break;
  case (OWBS_AND_0 | OWBS_NAN_0 | OWBS_SER_1):
   owbWriteBit(1);
   break;
  case (OWBS_AND_0 | OWBS_NAN_1 | OWBS_SER_0):
   owbWriteBit(0);
   break;
  case (OWBS_AND_0 | OWBS_NAN_1 | OWBS_SER_1): //进位重搜
   owbIncRom(pRomWant,dp);
   dp = 200;
   break;
  case (OWBS_AND_1 | OWBS_NAN_0 | OWBS_SER_0): //改为搜'1'
   owbIncRom(pRomWant,dp);
   owbWriteBit(1);
   break;
  case (OWBS_AND_1 | OWBS_NAN_0 | OWBS_SER_1):
   owbWriteBit(1);
   break;
  case (OWBS_AND_1 | OWBS_NAN_1 | OWBS_SER_0): //无器件
  case (OWBS_AND_1 | OWBS_NAN_1 | OWBS_SER_1): //无器件
  default:
   dp = 200;
   owbSetSearchEnd(pRomWant);
  }

  dp++;
 }//while end
 EA = 1;

 if (dp == 64) // find out a device
 {
  owb_SearchDeviceROM = *pRomWant;
  owb_SearchDeviceIndex++;
  owbDeviceInit(TRUE);
  owbIncRom( pRomWant,63 );
  return TRUE;
 }

 return FALSE;
}

void owbPreSearchDevices(OWBROM * pRomWant)
{
 BYTE i;
 for (i=0;i<8;i++) pRomWant->b[i] = 0;
 owb_SearchDeviceIndex = 0xff;
}

void owbUnpreSearchDevices(void)
{
 owb_nCntDevice = owb_SearchDeviceIndex + 1;
}


BYTE owbCountDevices(void)
{
 return owb_nCntDevice;
}

BYTE owbGetCacheIndex(void)
{
 return owb_CacheDeviceIndex;
}

BYTE owbGetSearchIndex(void)
{
 return owb_SearchDeviceIndex;
}

void owbCacheSearch(void)
{
 owb_CacheDeviceIndex = owb_SearchDeviceIndex;
 owb_CacheDeviceROM = owb_SearchDeviceROM;
}

BYTE owbGetCacheROM(BYTE i)
{
 return owb_CacheDeviceROM.b[i & 0x07];
}

BYTE owbGetSearchROM(BYTE i)
{
 return owb_SearchDeviceROM.b[i & 0x07];
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -