# include & ltintrins.h & gt
# include & lt math. H & gt// Use the absolute value function abs ()
# Define uchar unsigned characters
# Define uint unsigned integer
sbit ds=p2^ 1; //sbit ds=p3^2; //DS 18B20
sbit duan=p2^6;
sbit wei=p2^7;
Ucal I;
Unsigned character code table []={0x3f, 0x06, 0x5b, 0x4f,
0x66、0x6d、0x7d、0x07、
0x7f,0x6f,0x00,0x 40 }; //0x00 is the 0x40 display "-"(minus sign) used in the display function.
//****************************************************************
Invalid delay (uchiri)
{
uint j;
When (I-)
{
for(j = 0; j & lt 125; j++);
}
}
//****************************************************************
//Delay function. For 1 1.0592MHz clock, for example, i= 10, the delay is about10 ms. 。
void dsInit()
{
//For 1 1.0592MHz clock, the time of an i++ operation exceeds 8us.
Unsigned int I;;
ds = 0;
I = 100; //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-;
}
void dsWait()
{
Unsigned int I;;
while(ds); //Wait for the reply signal
while(~ ds); //Response pulse detected
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 read bit ()
{
Unsigned int I;;
B position;
ds = 0;
i++; //The delay is about 8us, which meets the protocol requirements and keeps at least 1us.
ds = 1;
i++;
i++; //The delay is about 16us, at least 15us meets the protocol requirements.
b = 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 to b;
}
//Read a byte of data by calling readBit ().
Unsigned character read byte ()
{
Unsigned int I;;
Unsigned characters j, dat
dat = 0;
for(I = 0; I<8; i++)
{
j = read bit();
//Read the least significant data first.
dat =(j & lt; & lt7)|(dat & gt; & gt 1);
}
Return dat
}
//Write one byte of data to DS 18B20.
Void writeByte (unsigned character data)
{
Unsigned int I;;
Unsigned character j;
B position;
for(j = 0; j & lt8; j++)
{
b = dat & amp0x 0 1;
dat & gt& gt= 1;
//Write 1. After decreasing DQ 15us, increase DQ within 15us~60us, that is, write 1.
If (b)
{
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 low DQ 60 us ~ 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.
void sendChangeCmd()
{
dsInit(); //initialize DS 18B20. Whatever the command is, initialize it first.
ds wait(); //Wait for a reply from DS 18B20.
Delay (1); //delay 1ms, because DS 18B20 will pull down DQ 60~240us as the reply signal.
write byte(0x cc); //Write the skip sequence number command word Skip Rom
write byte(0x 44); //Write the temperature conversion command word convert t.
}
//Send a command to read data to DS 18B20.
void sendReadCmd()
{
//EA = 0; //Turn off the interrupt, because the interrupt entering the display will affect the reading and writing timing of DS 18B20.
dsInit();
ds wait();
Delay (1);
write byte(0x cc); //Write the skip sequence number command word Skip Rom
write byte(0x be); //Write read data command word read temporary storage
//EA = 1;
}
//Get the current temperature value
int getTmpValue()
{
Unsigned int tmpvalue
Int value; //Store the temperature value
Floating t;
Unsigned characters are low and high;
//EA = 0;
sendReadCmd();
//Read two bytes of data continuously.
low = read byte();
High = readbyte ();
//Combine the upper and lower bytes into an integer variable.
//Negative numbers are represented by complement in the computer.
//If it is negative, the read value is represented by complement, which can be directly assigned to the value of type int.
Tmpvalue = high;
tmpvalue & lt& lt= 8;
Tmpvalue |= low;
value = tmpvalue
//ds 18b 20 has a default resolution of 12 bits and an accuracy of 0.0625 degrees, that is, the lowest bit of the read-back data represents 0.0625 degrees.
T = value * 0.0625;
//Enlarge 100 times, so that two decimal places can be displayed, and the third decimal place can be divided into four parts and five parts.
//If t= 1 1.0625, after calculation, the value is = 1 106, that is, 1 1.06 degrees.
//For example, t=- 1 1.0625, after counting, value =-1 106, that is-1 1.06 degrees.
value = t * 100+(value & gt; 0 ? 0.5 : -0.5); //More than 0 plus 0.5, less than 0 minus 0.5
Return value;
//EA = 1;
}
/*
void Init_timer0()
{
TMOD = 0x 0 1;
TH0 = th0
TL0 = tl0
EA = 1;
ET0 = 1;
TR0 = 1;
} */
Empty display (int dd)
{
uchar SH,SZ,SL,GH,GL,I,temp
uchar dis[8]={ 10, 10, 10, 10, 10, 10, 10, 10 }; The number from //5 to 8 is 10, and the corresponding segment code is 0x00, which means it is not displayed.
uint dda
DDA = ABS(DD);
SH = DDA/ 10000;
SZ = DDA % 10000/ 1000;
SL = DDA % 1000/ 100;
GH = DDA % 100/ 10;
GL = DDA % 10;
dis[0]= GL;
dis[ 1]= GH;
dis[2]= SL;
if((SZ = = 0)& amp; & amp(SH==0))
dis[3]= 10;
other
dis[3]= SZ;
if(DD & lt; 0)
dis[4]= 1 1; //is negative temperature, and "-"is displayed.
other
{
If (SH==0)
dis[4]= 10; //If the high temperature is 0, it will not be displayed.
other
dis[4]= SH;
}
temp = 0xbf
for(I = 0; I<8; i++)
{
P0 = 0xff
Wei =1;
temp=_cror_(temp, 1);
P0 = temperature;
Wei = 0;
P0 = 0;
Segment =1;
If (i==2)
{
P0 = 0x 80 | table[dis[2]];
}
other
P0 = table[dis[I]];
Segment = 0;
Delay (1);
}
/*
P0 = 0xff
Wei =1;
P0 = 0xdf
Wei = 0;
P0 = 0;
Segment =1;
P0 = Table [GL];
Delay (1);
Segment = 0;
P0 = 0xff
Wei =1;
P0 = 0xef
Wei = 0;
P0 = 0;
Segment =1;
P0 = Table [GH];
Delay (1);
Segment = 0;
P0 = 0xff
Wei =1;
P0 = 0xf7
Wei = 0;
P0 = 0;
Segment =1;
P0 = table[SL]| 0x 80;
Delay (1);
Segment = 0;
P0 = 0xff
Wei =1;
P0 = 0xfb
Wei = 0;
P0 = 0;
Segment =1;
P0 = Table [SZ];
Delay (1);
Segment = 0;
P0 = 0xff
Wei =1;
P0 = 0xfd
Wei = 0;
P0 = 0;
Segment =1;
P0 = table [sh];
Delay (1);
Segment = 0;
P0 = 0xff
Wei =1;
P0 = 0xff
Wei = 0; */
}
void main()
{
int tempvalue 1;
//init();
//Init _ timer 0();
while( 1)
{
//Start temperature conversion
sendChangeCmd();
tempvalue 1 = getTmpValue();
display(tempvalue 1);
}
}
//I have successfully changed the port according to my own hardware.