Learning DS18B20 Based on stm32f103zet6

Source: Internet
Author: User

Any DS18B20 chip ID that uses its internal 64-bit ROM to store the displacement (this provides great convenience for our multi-point collection), I use a 3pin Package Chip, it is worth noting that DS18B20 can work normally with a data line and a ground line, that is, VCC is not necessary. Why? Because DS18B20 has a parasitic capacitor inside, when the voltage range of our data line is between 3.3 and 5 V, it will be converted to power, so when we have an external power failure, this power provides a power supply for the IC, which plays a good role in energy saving ..

Some other features are summarized as follows:

-- Full digital temperature conversion and output.
-- Advanced single-bus data communication.
-- The resolution is up to 12 digits, and the accuracy can reach 0.5 degrees Celsius.
-- The maximum working cycle of a 12-bit resolution is 750 milliseconds.
-- The parasitic working mode can be selected.
-- The temperature range is-55 °C ~ + 125 °C (-67 °C F ~ + 257 ° F)
-- Built-in EEPROM and temperature limit alarm function.
-- 64-bit optical ROM with built-in product serial numbers to facilitate multi-host mounting.
-- Diverse encapsulation formats to adapt to different hardware systems.

I. Hardware Introduction

1. First, let's take a look at the overall diagram of DS13B20.

To sum up my understanding of these parts:

1. power Supply detection is used to detect whether an external power supply or data line conversion is used

2. 64 is the ROM used to read the chip ID.

3. The temperature sensitivity component is used as the temperature sensor in DS18B20 to measure the temperature. The 12-bit conversion is used as an example to provide a 16-bit Extended Binary complement reading, expressed in 0.0625 ℃/LSB format, where S is the symbol bit

4. The next step is the high and low temperature trigger,

5. There are also configuration registers. The configuration registers are configured with different digits to determine the temperature and number conversion. The structure format of the configuration register is as follows:

The relationship between R1 and R2 and controller resolution is as follows:

The core function of DS18B20 is its direct reading of digital temperature sensors. The accuracy of the temperature sensor is user-programmable 9, 10, 11 or 12 bits, respectively at 0.5 deg C, 0.25 deg C, 0.125 deg C and 0.0625 deg C increment. The default precision is 12 characters.

So we can clearly see the relationship between the conversion digits and the conversion time.

The temperature register format is as follows:

Ii. Let's take a look at how to control this chip.

A. Agreement:

Single-bus serial communication protocol, which is slightly different from common spi protocol

B. Operation Procedure: The procedure is based on the Internet, which is easy to understand.

1. Reset: first, we must reset the DS18B20 chip. Reset is a low-level signal from the Controller (single-chip microcomputer) to at least us on the single-bus DS18B20. When 18B20 receives this reset signal, it will be in the range of 15 ~ After 60 us, a chip pulse is sent back.
 
2. Pulse: After the reset level ends, the Controller should increase the data single bus so that ~ There is a pulse after receiving 60 us, and the existing pulse is a 60 ~ US low-level signal. Now, the communication parties have reached a basic agreement, and the next step will be the data communication between the Controller and 18B20. If the reset time is low or the circuit breaking of a single bus is not connected to an existing pulse, you should pay attention to the handling of unexpected situations during design.
 
3. The controller sends the ROM command: after the two sides say hello, they will communicate with each other. There are five ROM commands, and only one can be sent in each work cycle, ROM Commands include read ROM data, specified matching chip, skip ROM, chip search, and alarm chip search. The ROM instruction is 8-bit in length, which is used to operate the 64-bit optical print ROM in the chip. The main purpose is to identify and process multiple devices attached to a bus. It is true that multiple devices can be mounted on a single bus at the same time and are differentiated by the unique ID number of each device. Generally, ROM commands can be skipped only when a single 18B20 chip is mounted (note: here, the ROM skipping command does not send ROM commands, but uses a unique "Skip command "). The ROM commands are described in detail below.
 
4. The controller sends the Memory Operation Command: After the ROM command is sent to 18B20, The Memory Operation Command is sent continuously. The operation commands are also 8-bit, with 6 in total, memory Operation commands are used to write RAM data, read RAM data, copy RAM data to EEPROM, temperature conversion, copy the alarm values in EEPROM to RAM, and switch the working mode. The function of memory operation commands is what the 18B20 command does, and is the key to Chip Control.
 
5. execution or data read/write: After a memory operation command is completed, the command execution or data read/write will be performed. This operation depends on the Memory Operation Command. If the temperature conversion command is executed, the Controller (single-chip microcomputer) must wait for 18B20 to execute its command. Generally, the conversion time is us. For example, to execute the data read/write command, you must strictly follow the read/write sequence of 18B20. The data read and write methods are described in detail below.
 
To read the current temperature data, we need to execute two work cycles. The first cycle is reset, skip the ROM command, execute the temperature conversion Memory Operation Command, and wait for the temperature conversion time of us. Then, the second cycle is reset, the ROM instruction is skipped, the memory operation instruction for reading RAM is executed, and the data is read (up to 9 bytes, can be stopped in the middle, read-only simple temperature values read the first two bytes ). Other operation procedures are similar, so we will not discuss them here.

In addition, there is also an article that may solve some of your questions about DS13b20, Which I reproduced in Baidu space.

Bytes

Iii. Code Analysis

1. First, the main code for obtaining the temperature

/*************************************** ****************************************: DS18B20 b20_get_temp * function: Obtain the temperature value from DS18B20, precision: 0.1c * parameter number: none * return value: temperature value (-550 ~ 1250) **************************************** **************************************** * *****/short DS18B20 b20_get_temp (void) {u8 temp; u8 TL, th; short tem; // 2bytesds18b20 b20_start (); // DS18B20 start convertds18b20 b20_reset (); DS18B20 b20_check (); DS18B20 b20_write_byte (0xcc ); // skip romds18b20 b20_write_byte (0xbe); // convert TL = DS18B20 _read_byte (); // LSB th = DS18B20 b20_read_byte (); // MSB if (Th> 7) // judge plus and minus {th = ~ Th; TL = ~ TL; temp = 0; // The temperature is negative} else temp = 1; // The temperature is positive TEM = th; // obtain the high eight-bit TEM <= 8; TEM + = TL; // obtain the low eight-bit TEM = (float) TEM * 0.625; // convert if (temp) return TEM; // return the temperature value else return-tem ;}

Read the temperature and you have to look at this table.

The following is an official example table, which describes what we should pay attention to during conversion.

Note the default 12-bit precision power-on. So how to determine the accuracy? I asked a senior student to explain this. The fractional part is 4 digits, so the minimum resolution is one of the 4 power points of 2.

That is, 1/16. = 0.0625

2. Start Signal

/*************************************** ****************************************: DS18B20 b20_start * function: start signal * parameter: No * return value: no *************************************** **************************************** * ******/void DS18B20 b20_start (void) // DS18B20 start convert {DS18B20 _reset (); DS18B20 check (); DS18B20 b20_write_byte (0xcc); // skip rom DS18B20 _write_byte (0x44); // convert}

3. Reset Functions

/*************************************** ****************************************: DS18b20 reset function * parameter: none * return value: no *************************************** **************************************** * ******/void DS18B20 b20_reset (void) {DS18B20 _ set_io_in; DS18B20 _ data_l; Delay_us (750); DS18B20 _ data_h; Delay_us (20 );}

4. Detection Functions

/*************************************** ****************************************: DS18B20 b20_check * function: Detection Signal * parameter number: none * return value: 0 indicates success, 1 indicates failure ************************************* **************************************** * ********/u8 DS18B20 b20_check (void) {u8 retry = 0; DS18B20 b20_set_io_in; while (DS18B20 b20_data_in & (retry <200) {retry ++; delay_us (1);} If (retry> = 200) return 1; else retry = 0; while (! DS18B20 b20_data_in & (retry <240) {retry ++; delay_us (1) ;}; if (retry> = 240) return 1; return 0 ;}

5. Write the byte Function

/*************************************** ****************************************: DS18B20 * function: Write a byte to DS18B20 * parameter number: Read data * return value: no *************************************** **************************************** * ******/void DS18B20 b20_write_byte (u8 dat) {u8 j; u8 testb; DS18B20 b20_set_io_out; // SET PA0 OUTPUT; for (j = 1; j <= 8; j ++) {testb = dat & 0x01; dat = dat> 1; if (testb) {DS18B20 _ data_l; // Write 1 Delay_us (2); DS18B20 _ data_h; Delay_us (60);} else {DS18B20 b20_data_l; // Write 0 Delay_us (60); DS18B20 b20_data_h; Delay_us (2 );}}}

6. Read byte Functions

/*************************************** ****************************************: DS18B20 * function: Read a byte from DS18B20 * parameter number: none * return value: read data ************************************* **************************************** * ********/u8 DS18B20 b20_read_byte (void) // read one byte {u8 I, j, dat; dat = 0; for (I = 1; I <= 8; I ++) {j = DS18B20 _read_bit (); dat = (j <7) | (dat> 1) ;}return dat ;}

7. There is also a resolution Function

/*************************************** ****************************************: Adjust_res * function: adjust resolution * parameter: resolution value * return value: no *************************************** **************************************** * ****** // * void adjust_res (unsigned char res) /// res are equal to 0x1f, 0x3f, 0x5f, and 0x7f respectively. The temperature reading resolutions correspond to // 0.5, 0.25, 0.125, 0.0625 {DS18B20 _ reset (); // reset DS18B20 20_write_byte (0xcc ); // skip Rom DS18B20_Write_Byte (0x4e); // write the temporarily written memory DS18B20 (0x02); // write TH DS18B20 (20_write_byte (0x01); // write TL DS18B20 b20_write_byte (res ); // write the structure register DS18B20 _reset (); // reset the DS18B20 b20_write_byte (0xcc); // skip the Rom DS18B20 (write_byte (0x48); // write the content of the current Register to EPRam }*/

Functions are easy to analyze.

DS18B20

The temperature sensor in can complete the temperature measurement, taking 12-bit conversion as an example: Provides the binary complement reading form with 16-bit symbol extended, expressed in 0.0625 ℃/LSB form, where S is the symbol bit

The temperature sensor in DS18B20 can measure the temperature.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.