# include & ltreg52.h & gt
# include & ltstdio.h & gt
# Define uchar unsigned characters
# Define uint unsigned integer
sbit ds=p 1^7; //Temperature sensor signal line
sbit a 1=p 1^0; //thousands
sbit a2=p 1^ 1; //Hundred
sbit a3=p 1^2; //ten
sbit a4=p 1^3; //each
sbit a5=p 1^4; //segment selection control
sbit d =p0^2; //decimal point
sbit sound=p3^3; //Defines the pin of the single chip microcomputer that controls the buzzer.
Unit temperature, t, I, b, xs;
Unsigned char yima[]={0xeb, 0x88, 0xb3, 0xba, 0xd8, 0x7a, 0x7b, 0xa8, 0xfb, 0xfa};
Void delayms (unsigned int I); /* Delay function */
//**********************************************************************************
//Delay function, for a clock with 12MHz, such as z= 10, the delay is about10 ms. 。
Invalid delay (unit z)
{
uint x,y;
for(x = z; x & gt0; x-);
for(y = 124; y & gt0; y-);
}
//**********************************************************************************
//**********************************************************************************
//Initialize DS 18B20
//Let DS 18B20 go low for a long time, and then go high for a short time, then it can be started.
void dsreset(void)
{//For 1 1.0592MHz clock, what is the time ratio of an i++ operation? we
uint I;
ds = 0;
I = 103; //Pull down about 800us, which is more than the 480us required by the protocol.
while(I & gt; 0)I-;
ds = 1; //Generate a rising edge and enter the waiting state.
I = 4;
while(I & gt; 0)I-;
}
//**********************************************************************************
//**********************************************************************************
//Read a bit of data from DS 18B20.
//Read a little, so that DS 18B20 is one cycle lower and then two cycles higher.
//After that, DS 18B20 will output one bit of data lasting for a period of time.
Bit tempreadbit(void)
{
uint I;
bit dat
ds = 0;
i++; //i++ plays a delaying role, and the delay is about 8us, which meets the requirements of the protocol and at least keeps 1us.
ds = 1;
i++; i++; //The delay is about 16us, at least 15us meets the protocol requirements.
dat = ds
I = 8;
while(I & gt; 0)I-; //The delay is about 64us, which meets the requirement that the reading time slot is not less than 60us.
return(dat);
}
//**********************************************************************************
//**********************************************************************************
//Read a byte of data by calling tempreadbit ().
Uchar tempread (empty)
{
uchar i,j,dat
dat = 0;
for(I = 1; I < = 8; i++)
{
j = tempreadbit();
dat =(j & lt; & lt7)|(dat & gt; & gt 1); //The lowest bit of the read data comes first, so there is only one byte in the dat.
}
return(dat);
}
//**********************************************************************************
//**********************************************************************************
//Write one byte of data to DS 18B20.
void tempwritebyte(uchar dat)
{
uint I;
uchar j;
Bit testb
for(j = 1; j & lt=8; j++)
{
dat & amp0x 0 1;
dat = dat & gt& gt 1;
If(testb) // Write "1", pull ds down to 15us, and then pull ds up within 15us~60us, that is, write 1.
{
ds = 0;
i++; i++; //Pull down about 16us, and the symbol is required to be within 15~60us.
ds = 1;
I = 8; while(I & gt; 0)I-; //The delay is about 64us, which meets the requirement that the write time slot is not less than 60us.
}
Otherwise//write "0" to decrease ds 60us ~ 120 us.
{
ds = 0;
I = 8; while(I & gt; 0)I-; //Pull down about 64us, and the symbol is required.
ds = 1;
i++; i++; //The whole process of writing 0 time slot has exceeded 60us, so there is no need to delay 64us like writing 1.
}
}
}
//**********************************************************************************
//**********************************************************************************
//send a temperature conversion command to DS 18B20.
Invalid temperature change (invalid)
{
dsreset(); //initialize DS 18B20. Whatever the command is, initialize it first.
Delay (1); //delay 1ms, because DS 18B20 will pull down ds 60~240us as a reply signal.
tempwritebyte(0x cc); //write skip read ROM instruction
tempwritebyte(0x 44); //Write temperature conversion instruction
}
//**********************************************************************************
//**********************************************************************************
//Send a data reading command to DS 18B20 to obtain the current temperature value.
uint tmp()
{
Floating TT;
//int TT;
Uchar is low and high;
dsreset();
Delay (1);
tempwritebyte(0x cc); //Write the skip sequence number command word Skip Rom
tempwritebyte(0x be); //Write read data command word read temporary storage
//Read two bytes of data continuously.
low = tempread(); //Read the lower 8 bits
high = tempread(); //Read the upper 8 bits
Temp = high; //The upper 8 bits are given to temp.
temp & lt& lt=8; //The high and low bytes are combined into a plastic variable, and the temp is 16 bits, with the upper 8 bits shifted to the left and the lower 8 bits supplemented by 0.
Temp = temp | low// high and low 8-bit phases or =temp is 16 bits = high 8+ low 8 bits.
Xs = low & amp0x0f///The lower 4 digits are decimal parts.
TT = temp & gt; & gt4; //Integer part
//temp = TT * 100+xs * 100/ 16;
temp = TT * 10+((((xs * 5))& gt; & gt2)+ 1)>& gt 1);
// **************************************
//y =(((x * 5)& gt; & gt2)+ 1)>& gt 1;
//Equivalent Y = ((x * 5)/4+1)/2;
//Re-equivalence y = ((x * 20)/16+1)/2;
//y = 2 *(x * 10/ 16+0.5)/2;
//Actually y = x *10/16+0.5; //+0.5 rounded.
// **********************************************
//TT = temp * 0.0625; The default resolution of //DS 18B20 is 12 bits, and the accuracy is 0.0625 degrees, that is, the lowest bit of the read-back data represents 0.0625 degrees.
//temp = TT * 100+(temp & gt; 0 ? 0.5 : -0.5); //More than 0 plus 0.5, less than 0 minus 0.5
//temp = TT * 100; //Enlarge 100 times, so that two decimal places (t= 1 1.0625, calculated temp = 1 106, i.e.11.
Return temperature; //temp is an integer.
}
//**********************************************************************************
//**********************************************************************************
//Display function
Blank display
{
Unsigned char a 1=0, a2=0, a3=0, a4 = 0;; //Temporary variable
Static int k = 0;;
a 1 = t/ 1000; //take thousands of t//
a2 = t % 1000/ 100; //Take one percent of t//
a3 = t % 100/ 10; //Take the tenth place of t//
a4 = t % 10; //Take the unit of t//
if(k = = 0){ a 1 = 1; a2 = 0; a3 = 0; a4 = 0; P0 = one horse [a1]; }
else if(k = = 1){ a 1 = 0; a2 = 1; a3 = 0; a4 = 0; P0 = Yima[a2]; }
else if(k = = 2){ a 1 = 0; a2 = 0; a3 = 1; a4 = 0; P0 = Yima[a3]; }
else if(k = = 3){ a 1 = 0; a2 = 0; a3 = 0; a4 = 1; P0 = Yima[a4]; }
k++;
if(k & gt; 3)k = 0;
}
//**********************************************************************************
Master ()
{
/* TMOD = 0x 0 1;
TH0 =(65536-2000)/256;
TL0 =(65536-2000)% 256;
EA = 1;
TR0 = 1;
ET0 = 1; */
while( 1)
{
temp change(); //Start temperature conversion
//There is a delay after starting the temperature conversion, about 750 ms.
for(I = 0; I & lt40; I++) // display for 5 times (the first 3 digits of the digital tube are dark and there is no for) or the display function is changed to interrupt writing.
{
Display (t);
Delay (1);
}
t = tmp();
}
}
/*
Void t0 () interrupts 1 Use1/The interrupt program is responsible for displaying the value of t.
{
Unsigned char a 1=0, a2=0, a3=0, a4 = 0;; //Temporary variable
Static int k = 0;;
TH0 =(65536-2000)/256;
TL0 =(65536-2000)% 256;
a 1 = t/ 1000; //take thousands of t//
a2 = t % 1000/ 100; //Take one percent of t//
a3 = t % 100/ 10; //Take the tenth place of t//
a4 = t % 10; //Take the unit of t//
if(k = = 0){ a 1 = 1; a2 = 0; a3 = 0; a4 = 0; P0 = Yima[a4]; }
else if(k = = 1){ a 1 = 0; a2 = 1; a3 = 0; a4 = 0; P0 = Yima[a3]; }
else if(k = = 2){ a 1 = 0; a2 = 0; a3 = 1; a4 = 0; P0 = Yima[a2]; }
else if(k = = 3){ a 1 = 0; a2 = 0; a3 = 0; a4 = 1; P0 = one horse [a1]; }
k++;
if(k & gt; 3)k = 0;
//TH0 = 240; //scanning speed
} */