📄 18b20.c
字号:
/*==========================================================================================================
MODEL: Firmware Version : 010
============================================================================================================
H/W TB41 USE ONLY REV:0.2 24MHz
Editor by :antony 2007.2.26
Modify by :
===========================================================================================================*/
#include <REG52.H>
#include <absacc.h>
#include <intrins.h>
#include <math.h>
#include <setjmp.h>
//----------------------------------------------------------------------------------------------------------
#define U8 unsigned char
#define U16 unsigned int
//----------------------------------------------------------------------------------------------------------
#define T20us 1000
#define T40us 2000
#define T60us 3000
#define T80us 4000
#define T90us 4500
#define T480us 24000
//--------------------------------------------------------------------------------------------------------
//default value
#define Bit0 0x01
#define Bit1 0x02
#define Bit2 0x04
#define Bit3 0x08
#define Bit4 0x10
#define Bit5 0x20
#define Bit6 0x40
#define Bit7 0x80
#define XBit7 0x7f
#define XBit6 0xbf
#define XBit5 0xdf
#define XBit4 0xef
#define XBit3 0xf7
#define XBit2 0xfb
#define XBit1 0xfd
#define XBit0 0xfe
//--------------------------------------------------------------------------------------------------------
#define LCM_Data P1
#define Busy 0x80
sbit LCM_RS = P3^0;
sbit LCM_RW = P3^1;
sbit LCM_E = P3^3;
sbit OKLED = P3^5;
//-------------------------------------------------------------------------------------------------------
#define SetRelay XBYTE[0x4000]
U8 RelayBuffer=0xff;
#define SensorA 1
#define SensorB 2
sbit DQ1 = P3^4;
sbit DQ2 = P3^2;
//--------------------------------------------------------------------------------------------------------
void Delay100ms(void);
void Delay15(void);
void Delay60(void);
void InitTS(U8 Type);
void ResetTS(U8 Type);
void Write0TS(U8 Type);
void Write1TS(U8 Type);
bit ReadTS(U8 Type);
void WriteByteTS(U8 byte,U8 Type);
U8 ReadByteTS(U8 Type);
U16 GetTempTS(U8 Type);
void Delay(U16 cDly);
//---------------------------------------------------------------------------------------------------------
void WriteDataLCM(U8 WDLCM);
void WriteCommandLCM(U8 WCLCM,BuysC);
U8 ReadDataLCM(void);
U8 ReadStatusLCM(void);
void LCMInit(void);
void DisplayOneChar(U8 X, U8 Y, U8 DData);
void DisplayListChar(U8 X, U8 Y, U8 *DData);
void DisplayTemperature(U16 TempA,U16 TempB);
U8 code Line1[] = {" Q1 Q2 "};
U8 code Line2[] = {"000.000 000.000"};
U8 TempBuffer[16] = {"000.000 000.000"};
U8 code Notice1[] = {" Dragorn "};
U8 code Notice2[] = {"Oscillate Heater"};
U8 code NG[] = {" SENSOR Error!! "};
U8 code Q1_18B20NG[] = {" Q1_18B20 NG!! "};
U8 code Q2_18B20NG[] = {" Q2_18B20 NG!! "};
//---------------------------------------------------------------------------------------------------------
void main(void)
{
U16 BufferA,BufferB;
U8 j;
SetRelay=RelayBuffer & XBit0 & XBit1;
OKLED=1;
Delay100ms();
LCMInit();
DisplayListChar(0, 0, Notice1);
DisplayListChar(0, 1, Notice2);
for(j=0;j<30;j++)
Delay100ms();
DisplayListChar(0, 0, Line1);
DisplayListChar(0, 1, Line2);
InitTS(SensorA);
InitTS(SensorB);
SetRelay=RelayBuffer | Bit0 | Bit1;
while(1)
{
BufferA=GetTempTS(SensorA);
BufferA=BufferA-3500;
if(BufferA >= 57500)
RelayBuffer=RelayBuffer & XBit0;
else
RelayBuffer=RelayBuffer | Bit0;
SetRelay=RelayBuffer;
//-------------------------------------------------
BufferB=GetTempTS(SensorB);
BufferB=BufferB-3500;
if(BufferB >= 57500)
RelayBuffer=RelayBuffer & XBit1;
else
RelayBuffer=RelayBuffer | Bit1;
SetRelay=RelayBuffer;
//--------------------------------------------------
if((BufferA>56000)&&(BufferB>56000))
OKLED=0;
else
OKLED=1;
DisplayTemperature(BufferA,BufferB);
}
}
//----------------------------------------------------------------------------------------------------------
void Delay(U16 cDly)
{
cDly=cDly/1000;
do
{ //20us
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}while(cDly--);
}
//----------------------------------------------------------------------------------------------------------
void Delay100ms(void)
{
U8 i,j,k;
for(i=0;i<8*2;i++)
for(j=0;j<25;j++)
for(k=0;k<151;k++);
}
//------------------------------------------------------------------------------------------------------------
void InitTS(U8 Type) //init 18B20
{
ResetTS(Type);
WriteByteTS(0xCC,Type);
WriteByteTS(0x4E,Type);
WriteByteTS(0x1f,Type);
}
//------------------------------------------------------------------------------------------------------------
void ResetTS(U8 Type) //reset 18B20
{
U8 j;
j=255;
if(Type==SensorA)
{
DQ1=1;
DQ1=0;
Delay(T480us);
Delay(T40us);
DQ1=1;
//--------------------------------------------------------
while(j--) //if 18b20 presence DQ1=0 in 60~240us times
{
if(DQ1==0)
break;
}
if(j<2)
{
SetRelay=RelayBuffer & XBit0 & XBit1;
DisplayListChar(0, 0, NG);
DisplayListChar(0, 1, Q1_18B20NG);
while(1);
}
// while(DQ1);
//----------------------------------------------------------
Delay(T480us);
Delay(T40us);
}
else
{
DQ2=1;
DQ2=0;
Delay(T480us);
Delay(T40us);
DQ2=1;
//--------------------------------------------------------
while(j--) //if 18b20 presence DQ1=0 in 60~240us times
{
if(DQ2==0)
break;
}
if(j<2)
{
SetRelay=RelayBuffer & XBit0 & XBit1;
DisplayListChar(0, 0, NG);
DisplayListChar(0, 1, Q2_18B20NG);
while(1);
}
//while(DQ2);
//----------------------------------------------------------
Delay(T480us);
Delay(T40us);
}
}
//------------------------------------------------------------------------------------------------------------
void Write0TS(U8 Type) //WRITE '0' TO 18B20
{
if(Type==SensorA)
{
DQ1=1;
DQ1=0;
Delay(T90us);
DQ1=1;
_nop_();
_nop_();
}
else
{
DQ2=1;
DQ2=0;
Delay(T90us);
DQ2=1;
_nop_();
_nop_();
}
}
//------------------------------------------------------------------------------------------------------------
void Write1TS(U8 Type) //WRITE '1' TO 18B20
{
if(Type==SensorA)
{
DQ1=1;
DQ1=0;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
DQ1=1;
Delay(T80us);
}
else
{
DQ2=1;
DQ2=0;
_nop_(); //0.5us
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
DQ2=1;
Delay(T80us);
}
}
//------------------------------------------------------------------------------------------------------------
bit ReadTS(U8 Type) //READ 1 BIT FROM 18B20
{
bit b;
if(Type==SensorA)
{
DQ1=1;
DQ1=0;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
DQ1=1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
b=DQ1;
Delay(T40us);
}
else
{
DQ2=1;
DQ2=0;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
DQ2=1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
b=DQ2;
Delay(T40us);
}
return b;
}
//------------------------------------------------------------------------------------------------------------
void WriteByteTS(U8 byte,U8 Type) //Write one byte to 18B20
{
U8 i;
for(i=0;i<8;i++)
{
if(byte&0x01)
Write1TS(Type);
else
Write0TS(Type);
byte=byte>>1;
}
}
//-----------------------------------------------------------------------------------------------------------
U8 ReadByteTS(U8 Type) //Read one byte from 18B20
{
U8 i,j;
bit b;
j=0;
for(i=0;i<8;i++)
{
b=ReadTS(Type);
if(b)
j+=1;
j=_cror_(j,1);
}
return j;
}
//-----------------------------------------------------------------------------------------------------------
U16 GetTempTS(U8 Type) //READ 2 BYTES T
{
float Temperature;
U8 TempL,TempH;
U16 Temp;
ResetTS(Type);
WriteByteTS(0xCC,Type);
WriteByteTS(0x44,Type);
Delay100ms();
ResetTS(Type);
WriteByteTS(0xCC,Type);
WriteByteTS(0xBE,Type);
TempL=ReadByteTS(Type); //LOW byte
TempH=ReadByteTS(Type); //HIG BYTE
Temp=((TempH & 0x0f)<<4)|((TempL & 0xf0)>>4);
Temperature=Temp+(TempL & 0x0f)*0.0625;
Temp=Temperature;
Temp=Temperature*1000;
return(Temp);
}
//=======================LCD SUBPROGRAM START=================================================================
//write data
void WriteDataLCM(U8 WDLCM)
{
ReadStatusLCM();
LCM_Data = WDLCM;
LCM_RS = 1;
LCM_RW = 0;
Delay(T60us);
LCM_E = 1;
Delay(T480us);
LCM_E = 0;
}
//---------------------------------------------------------------------------------------
//write command
void WriteCommandLCM(U8 WCLCM,BuysC)
{
if (BuysC)
ReadStatusLCM();
LCM_Data = WCLCM;
LCM_RS = 0;
LCM_RW = 0;
Delay(T60us);
LCM_E = 1;
Delay(T480us);
LCM_E = 0;
}
//-----------------------------------------------------------------------------------------
//read status
U8 ReadStatusLCM(void)
{
LCM_Data = 0xFF;
LCM_RS = 0;
LCM_RW = 1;
Delay(T60us);
LCM_E = 1;
while (LCM_Data & Busy);
LCM_E = 0;
return(LCM_Data);
}
//------------------------------------------------------------------------------------------
//lcd initial
void LCMInit(void)
{
LCM_Data =0x00;
LCM_RS = 0;
LCM_RW = 0;
LCM_E = 0;
WriteCommandLCM(0x38,1);
WriteCommandLCM(0x38,1);
WriteCommandLCM(0x38,1);
WriteCommandLCM(0x38,1);
WriteCommandLCM(0x08,1);
WriteCommandLCM(0x01,1);
WriteCommandLCM(0x06,1);
WriteCommandLCM(0x0c,1);
}
//----------------------------------------------------------------------------------------
void DisplayOneChar(U8 X, U8 Y, U8 DData)
{
Y &= 0x1;
X &= 0xF;
if (Y)
X |= 0x40;
X |= 0x80;
WriteCommandLCM(X, 0);
WriteDataLCM(DData);
}
//-----------------------------------------------------------------------------------------
void DisplayListChar(U8 X, U8 Y, U8 *DData)
{
U8 ListLength;
ListLength = 0;
Y &= 0x1;
X &= 0xF;
while (ListLength<16)
{
if (X <= 0xF)
{
DisplayOneChar(X, Y, DData[ListLength]);
ListLength++;
X++;
}
}
}
//-------------------------------------------------------------------------------------------
void DisplayTemperature(U16 TempA,U16 TempB)
{
U16 cData;
cData=TempA/1000;
TempBuffer[0]=(cData/100)+0x30;
TempBuffer[1]=(cData-(TempBuffer[0]-0x30)*100)/10+0x30;
TempBuffer[2]=(cData-(TempBuffer[0]-0x30)*100-(TempBuffer[1]-0x30)*10)+0x30;
//0.xxxx
TempA=TempA-cData*1000;
TempBuffer[4]=(TempA/100)+0x30;
TempBuffer[5]=(TempA-(TempBuffer[4]-0x30)*100)/10+0x30;
TempBuffer[6]=TempA-(TempBuffer[4]-0x30)*100-(TempBuffer[5]-0x30)*10+0x30;
cData=TempB/1000;
TempBuffer[9]=(cData/100)+0x30;
TempBuffer[10]=(cData-(TempBuffer[9]-0x30)*100)/10+0x30;
TempBuffer[11]=(cData-(TempBuffer[9]-0x30)*100-(TempBuffer[10]-0x30)*10)+0x30;
//0.xxx
TempB=TempB-cData*1000;
TempBuffer[13]=(TempB/100)+0x30;
TempBuffer[14]=(TempB-(TempBuffer[13]-0x30)*100)/10+0x30;
TempBuffer[15]=TempB-(TempBuffer[13]-0x30)*100-(TempBuffer[14]-0x30)*10+0x30;;
DisplayListChar(0, 1,TempBuffer);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -