DS18B20 digital thermometer is a 1-Wire made by DALLAS company, which is a single bus device. It has the characteristics of simple circuit and small volume. Therefore, it can be used to form a temperature measurement system, which has a simple circuit and can hang many such digital thermometers in a communication line. It is very convenient.
1. Characteristics of DS18B20 Products
(1) Communication can be achieved by requiring one port.
(2) Each device in DS18B20 has a unique serial number.
(3) Temperature measurement can be realized without any external components in practical application.
(4) The temperature range of measurement is from - 55 to + 125 C.
(5) The resolution of the digital thermometer can be chosen by users from 9 to 12 bits.
(6) There are alarm settings for upper and lower temperature limits.
![]() |
![]() |
![]() |
![]() |
![]() |
Because DS18B20 reads and writes data on an I/O line, there are strict timing requirements for read and write data bits.
DS18B20 has strict communication protocol to ensure the correctness and integrity of data transmission. The protocol defines several signal timing: initialization timing, read timing and write timing.
All the time series are based on the host as the main device and the single bus device as the slave device. Each transmission of commands and data starts with the host initiatively starting the write sequence. If the single bus device is required to send back the data, after writing the command, the host needs to start the read sequence to complete the data reception.
Data and commands are transmitted online.
1. Reset Sequence
2. Writing Timing
Writing timing for DS18B2 0 is still divided into writing 0 sequence and writing 1 sequence. For DS18B20, the requirement of writing 0 sequence is different from that of writing 1 sequence.
When writing 0 sequence, the single bus should be pulled down at least 60 us to ensure that DS18B20 can correctly use the "0" level on the IO bus between 15us and 45us.
When the time sequence of 1 is to be written, after the single bus is pulled down, the single bus must be released within 15us.
3. Reading Timing
The reading sequence of DS18B2 0 is divided into two processes: reading 0 sequence and reading 1 sequence.
For DS18B20, the read gap is lowered from the host to release the single bus within 15 seconds so that DS18B20 can transfer data to the single bus. DS18B20 needs at least 60us to complete a reading sequence.
# 4. Operating procedures
Examples of Delay and Reset
Delay case | Reset instance |
---|---|
![]() |
![]() |
Examples of Read and Write Bits
Read bit instance | Write instance |
---|---|
![]() |
![]() |
Examples of byte reading and section writing
Read byte instances | Examples of Section Writing |
---|---|
![]() |
![]() |
Experiment of Acupuncture
Password code
* [Course 20] ****DS18B20 Temperature reading*********** * * [Explanation] **** * * [Description] ****Read ambient temperature * ******************************************************************/ #include<reg51.h> #include <intrins.h> //Two commonly used macro definitions-------------------------------------------------------------------------------------------------------------------- #define uint8 unsigned char #define uint16 unsigned int //Definition of control pin 1602 - -------------------------------------------------------------------------------------------------------------- sbit RS = P2^0; sbit RW = P2^1; sbit EN = P2^2; sbit DQ = P3^3; //Statement of LCD Module------------------------------------------------------------------------------------------------------------------ bit BUSY(void); void Write_cmd(uint8 cmd); void Write_dat(uint8 dat); void LCD_Init(void); //Functional Statement of DS18B20 void DelayUs(uint16 time); void Init_Ds18b20(void); void WriteByte(uint8 date); uint8 ReadTemperature(void); //The declaration of function - ------------------------------------------------------------------------------------------------------------------------ void DelayMS(uint16 dly); //------------------------------------ void main(void) { uint8 temp,temp1,temp2; LCD_Init(); Init_Ds18b20(); while(1) { temp = ReadTemperature(); temp1 = temp/10; temp2 = temp%10; Write_cmd(0x80); Write_dat(temp1+0x30); Write_dat(temp2+0x30); Write_dat(0xdf); Write_dat(0x43); } } /******************************************************** ** Name: void DelayMS(uint16 dly) ** Function: millisecond delay (12M crystal oscillator) ** Entry parameters: Delay parameters for users ** Export parameters: none *********************************************************/ void DelayMS(uint16 dly) { uint16 x,y; for(x=dly;x>0;x--) for(y=124;y>0;y--); } /******************************************************** ** Name: bit BUSY(void) ** Function: To detect whether LCD1602 is busy or not. ** Entry parameters: none ** Export parameter: bit Bit Return 1, then BUSY Return 0, then OK *********************************************************/ bit BUSY(void) { bit Bit; RS = 0; RW = 1; EN = 1; DelayMS(1); Bit = (bit)(P0 & 0x80); //The highest bit is the busy signal bit. EN = 0; return Bit; } /******************************************************** ** Name: void Write_cmd(uint8 cmd) ** Function: Write commands ** Entry parameters: cmd control command ** Export parameters: none *********************************************************/ void Write_cmd(uint8 cmd) { while(BUSY()); //Busy schedule RS = 0; RW = 0; EN = 0; P0 = cmd; EN = 1; DelayMS(1); EN = 0; } /******************************************************** ** Name: void Write_dat(uint8 dat) ** Function: Write data ** Entry parameters: dat Data to be displayed ** Export parameters: none *********************************************************/ void Write_dat(uint8 dat) { while(BUSY()); //Busy schedule RS = 1; RW = 0; EN = 0; P0 = dat; EN = 1; DelayMS(1); EN = 0; } /******************************************************** ** Name: void LCD_Init(void) ** Function: Initialization of liquid crystal ** Entry parameters: none ** Export parameters: none *********************************************************/ void LCD_Init(void) { Write_cmd(0x38); //Function settings DelayMS(1); Write_cmd(0x0c); //Display switch control DelayMS(1); Write_cmd(0x06); //Input Mode Settings DelayMS(1); Write_cmd(0x01); //Clear the LCD display DelayMS(1); } /************************************** **Function Name: void DelayUs(uint time) **Description:delay US **Input: Time **Output: None **Return: None **************************************/ void DelayUs(uint16 time) { while(time--); } /************************************** **Function Name: void Init_Ds18b20(void) **Description:DS18B20 Initialization **Input : None **Output: None **Return: None **************************************/ void Init_Ds18b20(void) { uint8 i; DQ=1; //First, DQ is high level. DelayUs(15); //Delay, equal DQ stability DQ=0; //Pull down DelayUs(75); //Delay is about 600 US DQ=1; //Pull up (release) DelayUs(15); //About 15US i=DQ; //Start sampling DelayUs(7); //Delay (This delay is important in simulation time) // return i // Return 0, indicating device response on the bus //Return to 1, indicating that there is no device response on the bus } /************************************** **Fuction Name: void WriteByte(uchar date) **Description:Write a byte **Input: Write Data Byte **Output: None **Return: None **************************************/ void WriteByte(uint8 date) { uint8 i; for(i=0;i<8;i++) { DQ=0; DQ=date&0x01; DelayUs(7); DQ=1; date>>=1; } DelayUs(7); } /************************************** **Function Name: uchar ReadByte(void) **Description:Read a byte **Input: None **Output: Read Data Byte **Return: Read Data Byte **************************************/ uint8 ReadByte(void) { uint8 i,value; for(i=0;i<8;i++) { DQ=0; value>>=1; DQ=1; if(DQ) { value|=0x80; } DelayUs(7); } return value; } /************************************** **Function Name: uchar ReadTemperature(void) **Description:Read out the temperature value **Input: None **Output: Read Temperature **Return: Read Temperature **************************************/ uint8 ReadTemperature(void) { uint8 a,b; Init_Ds18b20(); //Initialization WriteByte(0xcc); //Skip ROM WriteByte(0x44); //Startup conversion DelayUs(350); Init_Ds18b20(); //Initialization WriteByte(0xcc); //Skip ROM WriteByte(0xbe); //Ready to read a=ReadByte(); b=ReadByte(); b<<=4; //Integer part of reading temperature b+=(a&0xf0)>>4; return b; }