51 single chip microcomputer DS18B20 temperature measurement

Publisher:CyberJoltLatest update time:2015-06-16 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
    Hello everyone, through previous learning, we have learned and are familiar with the use and learning methods of the 51 single-chip integrated learning system, learned the basic knowledge of stepper motor control, and experienced the ease of use and learning of the integrated learning system. In this issue, we will learn the basic principles and use methods of the digital temperature sensor DS18B20.
      Let's first look at what experiments and product development work the 51 single-chip integrated learning system we are going to use can complete: there are running lights, digital tube display, LCD display, key switches, buzzer music, relay control, IIC bus, SPI bus, PS/2 experiment, AD analog-to-digital conversion, optocoupler experiment, serial communication, infrared remote control, wireless remote control, temperature sensor, stepper motor control, etc.
     Introduction to Single Bus Temperature Sensor DS18B20
     DS18B20 is a single bus digital temperature sensor produced by DALLAS. It has the advantages of miniaturization, low power consumption, high performance, strong interference resistance, and easy matching with processors. It is particularly suitable for forming a multi-point temperature measurement and control system. It can directly convert the temperature into a serial digital signal (providing 9-bit binary numbers) for single-chip microcomputer processing, and multiple sensor chips can be connected on the same bus. It has a 3-pin TO-92 small volume package, a temperature measurement range of -55℃~+125℃, programmable 9-bit~12-bit A/D conversion accuracy, and a temperature measurement resolution of up to 0.0625℃. The measured temperature is serially output in a 16-bit digital quantity with symbol extension. Its working power supply can be introduced at the remote end or generated by parasitic power supply. Multiple DS18B20 can be connected in parallel to 3 or 2 lines. The CPU only needs one port line to communicate with multiple DS18B20, occupying fewer ports of the microprocessor, which can save a lot of leads and logic circuits. The above features make DS18B20 very suitable for long-distance multi-point temperature detection system.
    DS18B20 appearance and pin description
    The pins are different in TO-92 and SO-8 packages. Please refer to the PDF manual for the specific differences. The pin assignment in TO-92 package is as follows:
    1 (GND): Ground
    2 (DQ): Data input and output pin for single-line application
    3 (VDD): Optional power supply pin
    DS18B20 working process and timing
    The low temperature coefficient oscillator inside DS18B20 is an oscillator whose oscillation frequency varies very little with temperature, providing a counting pulse with a stable frequency for counter 1.
    The high temperature coefficient oscillator is an oscillator whose oscillation frequency is very sensitive to temperature, providing a counting pulse with a frequency that varies with temperature for counter 2.
    Initially, the temperature register is preset to -55℃. Every time counter 1 starts to count down from the preset number to 0, the temperature value stored in the temperature register increases by 1℃. This process is repeated until counter 2 counts to 0 and stops.
    Initially, counter 1 is preset to a preset value corresponding to -55℃. The preset number of each cycle of counter 1 is provided by the slope accumulator. In order to compensate for the nonlinearity of the oscillator's temperature characteristics, the preset number provided by the slope accumulator also changes with the temperature. The preset number of counter 1 is the number of counts required to increase the value of the temperature register by 1°C at a given temperature. The
    comparator inside the DS18B20 determines the least significant bit of the temperature register in a rounded quantization manner. After counter 2 stops counting, the comparator converts the count remaining value in counter 1 into a temperature value and compares it with 0.25°C. If it is lower than 0.25°C, the lowest bit of the temperature register is set to 0; if it is higher than 0.25°C, the lowest bit is set to 1; if it is higher than 0.75°C, the lowest bit of the temperature register is carried and then set to 0. In this way, the value of the temperature register obtained after comparison is the final temperature value read, and its last bit represents 0.5°C. The maximum quantization error after rounding is ±1/2LSB, that is, 0.25°C.
    The temperature value in the temperature register is represented in a 9-bit data format, with the highest bit being the sign bit, and the remaining 8 bits representing the temperature value in binary complement form. At the end of the temperature measurement, the 9-bit data is transferred to the first two bytes of the temporary memory, with the sign bit occupying the first byte and the 8-bit temperature data occupying the second byte.
    DS18B20 uses a unique temperature measurement technology when measuring temperature. The low temperature coefficient oscillator inside the DS18B20 can generate a stable frequency signal; similarly, the high temperature coefficient oscillator converts the measured temperature into a frequency signal. When the counting gate is opened, the DS18B20 counts, and the opening time of the counting gate is determined by the high temperature coefficient oscillator. There is also a slope accumulator inside the chip to compensate for the nonlinearity of the frequency. The measurement result is stored in the temperature register. Under normal circumstances, the temperature value should be 9 bits, but because the sign bit is extended to the upper 8 bits, it is finally read out in 16-bit complement form.
    The working process of DS18B20 generally follows the following protocol: Initialization-ROM operation command-memory operation command-processing data
    ① Initialization
    All processing on the single bus starts with the initialization sequence. The initialization sequence consists of a reset pulse from the bus master followed by a presence pulse from the slave device. The presence pulse lets the bus controller know that the DS1820 is on the bus and is ready to operate.
   ② ROM Operation Commands
    Once the bus master detects the presence of a slave device, it can issue one of the device ROM operation commands. All ROM operation commands are 8 bits long. These commands are listed below:
Read ROM [33h]
    This command allows the bus master to read the DS18B20's 8-bit product series code, unique 48-bit serial number, and 8-bit CRC. This command can only be used when there is only one DS18B20 on the bus. If there is more than one slave device on the bus, data collisions will occur when all slaves attempt to transmit at the same time (open drain will produce a wired AND result).
    Match ROM [55h]
    This command is followed by a 64-bit ROM data sequence, allowing the bus master to address a specific DS18B20 on a multi-point bus. Only DS18B20s that strictly conform to the 64-bit ROM sequence will respond to subsequent memory operation commands. All slaves that do not conform to the 64-bit ROM sequence will wait for a reset pulse. This command can be used with either single or multiple devices on the bus.
    Skip ROM [CCh]
    In a single-point bus system, this command saves time by allowing the bus master to access memory operations without providing the 64-bit ROM code. If there is more than one slave on the bus and a read command is issued after the Skip ROM command, data conflicts will occur on the bus due to multiple slaves sending data at the same time (open-drain pull-downs will produce a wired-AND effect).
    Search ROM [F0h]
    When the system starts operating, the bus master may not know the number of devices on the single-wire bus or their 64-bit ROM codes. The Search ROM command allows the bus controller to identify the 64-bit codes of all slaves on the bus by a process of elimination.
    Alarm Search [ECh]
    The flow of this command is the same as the Search ROM command. However, the DS18B20 will respond to this command only if the most recent temperature measurement has an alarm. The alarm condition is defined as the temperature being above TH or below TL. As long as the DS18B20 is powered on, the alarm condition remains set until another temperature measurement shows a non-alarm value or the setting of TH or TL is changed so that the measured value is once again within the allowed range. The trigger value stored in the EEPROM is used for the alarm.
    ③ Memory Operation Command
    Write Scratchpad [4Eh]
    This command writes data to the scratchpad of the DS18B20, starting at address 2. The next two bytes written will be stored in address locations 2 and 3 in the scratchpad. A reset command can be issued at any time to abort the write.
    Read Scratchpad [BEh]
    This command reads the contents of the scratchpad. The read will start at byte 0 and continue until the 9th (byte 8, CRC) byte is read. If not all bytes are to be read, the controller can issue a reset command at any time to abort the read.
    Copy Scratchpad [48h]
    This command copies the contents of the scratchpad to the E2 memory of the DS18B20, i.e., stores the temperature alarm trigger byte in non-volatile memory. If the bus controller issues a read time slot after this command while the DS18B20 is busy copying the scratchpad to the E2 memory, the DS18B20 will output a "0". If the copy is complete, the DS18B20 will output a "1". If parasite power is used, the bus controller must start the strong pull-up immediately after this command is issued and maintain it for at least 10ms.
    Convert T [44h]
    This command initiates a temperature conversion without requiring any other data. The temperature conversion command is executed, and then the DS18B20 remains in a waiting state. If the bus controller issues a read time slot following this command and the DS18B20 is busy doing a temperature conversion, the DS18B20 will output a "0" on the bus, or a "1" if the temperature conversion is complete. If parasite power is used, the bus controller must initiate a strong pullup immediately after issuing this command and maintain it for 500ms.
    Recall E2 [B8h]
    This command recalls the value of the temperature trigger stored in E2 to the scratchpad memory. This recall also occurs automatically when the DS18B20 is powered up, so that as soon as the device is powered up, there is valid data in the scratchpad memory. After this command is issued, for the first read data time slot issued, the device will output a temperature conversion busy indication: "0" = busy, "1" = ready.
    Read Power Supply [B4h]
    For the first read data time slot issued after this command is sent to the DS18B20, the device will signal its power mode: "0" = parasite power supply, "1" = external power supply.
    ④ Data processing
    The high-speed temporary storage memory of DS18B20 consists of 9 bytes, and its allocation is shown in Figure 3. When the temperature conversion command is issued, the temperature value obtained by conversion is stored in the 0th and 1st bytes of the high-speed temporary storage memory in the form of two-byte complement. The microcontroller can read the data through the single-line interface, with the low bit first and the high bit last when reading.
    DS18B20 temperature data table
    The above table is the 12-bit data obtained after the DS18B20 temperature acquisition conversion, which is stored in two 8-bit RAMs of DS18B20. The first 5 bits in binary are sign bits. If the measured temperature is greater than or equal to 0, these 5 bits are 0. Just multiply the measured value by 0.0625 to get the actual temperature; if the temperature is less than 0, these 5 bits are 1, and the measured value needs to be inverted, added by 1, and then multiplied by 0.0625 to get the actual temperature.
    Example of temperature conversion calculation method:
    For example, when DS18B20 collects the actual temperature of +125℃, the output is 07D0H, then:
    actual temperature = 07D0H╳0.0625=2000╳0.0625=1250C.
    For example, when DS18B20 collects the actual temperature of -55℃, the output is FC90H, then the 11-bit data bit should be inverted and added by 1 to get 370H (the sign bit remains unchanged and is not used for calculation), then:
    actual temperature = 370H╳0.0625=880╳0.0625=550C.
    DS18B20 software and hardware design
    This example introduces the software and hardware interface between DS18B20 and the microcontroller, reads the temperature value of DS18B20 through the microcontroller, and displays the temperature value through the digital tube. In the experiment, the function selection switch should be adjusted to the DS18B20 position first. [page]
/***************************************************************************/
/*DS18B20 demonstration program*/
/*Target device: AT89S51 */
/*Crystal: 11.0592MHZ */
/*Compilation environment: Keil 7.50A */
/***************************************************************************/
/*************************************Include header file********************************/
#include
/***********************************Common anode LED segment code table*******************************/
unsigned char code tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
/*************************************Port definition**********************************/
sbit DQ=P3^3; //Data transmission line connected to the corresponding pin of the microcontroller
/************************************Define global variables******************************/
unsigned char tempL=0; //Temporary variable low bit
unsigned char tempH=0; //Temporary variable high bit
float temperature; //temperature value
/****************************************************************************
Function: delay subroutine
Input parameter:
kExit parameter:
/************************************************************************/
void delay(unsigned int k)
{
unsigned int n;
n=0;
while(n < k)
{n++;}
return;
}
/****************************************************************************
Function: digital tube scanning delay subroutine
Input parameter:
Exit parameter:
***************************************************************************/
void delay1(void)
{
int k;
for(k=0;k<400;k++);
}
/****************************************************************************
Function: digital tube display subroutine
Input parameter:
kExit parameter:
/****************************************************************************/
void display(int k)
{
P2=0xfe;
P0=tab[k/1000];
delay1();
P2=0xfd;
P0=tab[k%1000/100];
delay1();
P2=0xfb;
P0=tab[k%100/10];
delay1();
P2=0xf7;
P0=tab[k%10];
delay1();
P2=0xff;
}
/********************************************************************************
Function: DS18B20 initialization subroutine
Input parameters:
Output parameters:
/****************************************************************************/
Init_DS18B20(void)
{
unsigned char x=0;
DQ=1; //DQ is set high first
delay(8); //Delay
DQ=0; //Send reset pulse
delay(85); //Delay (>480ms)
DQ=1; //Pull up the data line
delay(14); //Wait (15~60ms)
}
/****************************************************************************
Function: Read one byte of data from DS18B20
Input parameter:
Output parameter: dat
/****************************************************************************/
ReadOneChar(void)
{
unsigned char i=0;
unsigned char dat=0;
for (i=8;i>0;i--)
{
DQ=1;
delay(1);
DQ=0;
dat>>=1;
DQ=1;
if(DQ)
dat|=0x80;
delay(4);
}
return(dat);
}
/****************************************************************************
Function: Write one byte of data to DS18B20
Input parameter: dat
Output parameter:
******************************************************************************/
WriteOneChar(unsigned char dat)
{
unsigned char i=0;
for(i=8;i>0;i--)
{
DQ=0;
DQ=dat&0x01;
delay(5);
DQ=1;
dat>>=1;
}
delay(4);
}
/****************************************************************************
Function: Read temperature value from DS18B20
Input parameter:
Output parameter: temperature
********************************************************************************/
ReadTemperature(void)
{
Init_DS18B20(); //Initialize
WriteOneChar(0xcc); //Skip the operation of reading serial number
WriteOneChar(0x44); //Start temperature conversion
delay(125); //Conversion takes a little time, delay
Init_DS18B20(); //Initialize
WriteOneChar(0xcc); //Skip the operation of reading serial number
WriteOneChar(0xbe); //Read temperature register (the first two values ​​are the low and high bits of temperature)
tempL=ReadOneChar(); //Read the low LSB of the temperature
tempH=ReadOneChar(); //Read the high MSB of the temperature
//Temperature conversion, convert the high and low bits into actual temperature
temperature=((tempH*256)+tempL)*0.0625;
delay(200);
return(temperature);
}
/****************************************************************************
Function: Main program
Input parameter:
Output parameter:
****************************************************************************/
void main()
{
float i;
while(1)
{
i=ReadTemperature();
display(i);
}
}
I believe that after reading this, you should be able to understand the principle of DS18B20 digital temperature sensor. You can also write a program for temperature detection and related control according to your needs. Due to limited space, readers can communicate and learn together through the website or email.
Reference address:51 single chip microcomputer DS18B20 temperature measurement

Previous article:An ultrasonic distance measuring board with LCD display
Next article:51 MCU timer timing mode

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号