DS18B20 Temperature Sensor

Source: Internet
Author: User
Tags ack

Does not accumulate Kuibu not even thousands of miles, does not accumulate the small flow not to become jianghai.

program code can be downloaded to github < portal >.

Note: The 18B20 is strictly required for timing and cannot be interrupted. Once interrupted will appear garbled, the temperature shown is garbled. So remember, the operation of the EA ...

March 30, 2018 even more, emmm noon eat meal carefully think of this inside still have a problem, shut down means our real-time task can't handle. may cause some functionality to be impossible. EMM, now that the problem is: The temperature display is garbled. think again, because we read directly from the 18B20 and then direct real-time display caused, we do not display in real time, but put into the buffer. then the temperature of the legality test and then read from the buffer in the display can not ... haha haha ...

note the software in the bottom of the program delay operation skills, it microcontroller and 12T microcontroller, about NOP delay, you can see here. < Portal >

Altogether 2 bytes, the LSB is a low byte, the MSB is a high byte, where the MSB is a byte high, and the LSB is the low byte. It can be seen that the binary numbers, each one represents the meaning of the temperature, are expressed. where s represents the sign bit, the low 11 bits are a power of 2, and are used to indicate the final temperature. The temperature measurement range of the DS18B20 is from 55 degrees to 125 degrees, and the temperature data is expressed in the form of positive and negative temperatures, and each number in the register is distributed as the scale of the caliper.

The minimum bit changeof binary digit is 1, which represents the mapping relationship of 0.0625 degrees of temperature change. When 0 degrees, that is 0x0000, when the temperature of 125 degrees, the corresponding hexadecimal is 0x07d0, when the temperature is 零下55度, the corresponding number is 0XFC90. Conversely, when the number is 0x0001, the temperature is 0.0625 degrees.

The corresponding protocol is 1-wire!!!

1. Initialization

Similar to the addressing of I 2 C, the 1-wire bus will begin to detect the presence of DS18B20 on the bus.
This device. If there is a ds18b20 on this bus, the bus will return a low-level pulse based on the timing requirements, and if it does not, it will not return a pulse, that is, the bus remains high, so it is customary to call the detection of the presence of pulses.

There is a pulse detection process , the first single-chip to pull down this pin, the duration of about 480US to 960us between the time can be, our program continued in 500US. Then, the microcontroller release bus, is to give high level, ds18b20 wait about 15 to 60US, will be active pull down this pin is about 60 to 240US, and then DS18B20 will actively release the bus, so that the IO port will be automatically pulled up by the pull-up resistor.

Because the DS18B20 timing requirements are very stringent, the total interrupt is first turned off in order to prevent interruption of the bus timing when the timing is in operation. Then the first step, pull down ds18b20 this pin, continue 500us; the second step, IO release bus, delay 60US, third step, read the presence of pulses, and wait for the presence of pulse end.

2. ROM Operation Instruction

Multiple devices can be hung on the bus, with different device addresses to access different devices. Similarly, the 1-wire bus can hang multiple devices, but it has only one line, how to differentiate between different devices.

There is a unique 64-bit long serial number inside each DS18B20, and this serial number value exists in the DS18B20 internal ROM. The first 8 bits are the product Type code (DS18B20 is 0x10), and the next 48 bits are the unique serial number for each device, and the last 8 bits are the CRC check code. Ds18b20 can lead out a long line, up to dozens of meters, measuring the temperature of different locations. The microcontroller can obtain the temperature information collected by each sensor through communication with DS18B20, and can send some instructions to all ds18b20 at the same time.

Skip ROM (skip ROM): 0xCC. When there is only one device on the bus, you can skip the ROM and do not perform ROM detection.

3. RAM Memory Operation Instruction

RAM read instructions, commonly used on two

Read Scratchpad (reading staging register): 0xBE

DS18B20 temperature data is 2 bytes, we read the data, the first read to the low, and then the high.

Convert temperature (start temperature conversion): 0x44

When we send an instruction to start the temperature conversion, Ds18b20 starts the conversion. From the start of the conversion to the acquisition of temperature, Ds18b20 takes time, and the length of time depends on the accuracy of the ds18b20. Front said Ds18b20 can be used up to 12 bits to store temperature, but can also be used 11-bit, 10-bit and 9-bit altogether four formats. The higher the number of digits, the higher the accuracy, the 9-bit mode minimum bit change 1 digital temperature change 0.5 degrees, while the conversion speed is also faster.

Where registers R1 and R0 determine the number of bits converted, the factory default value of 11, that is, 12-bit represents the temperature, the maximum conversion time is 750ms. When the conversion is started, at least 750ms before the temperature can be read, otherwise the read temperature may be the wrong value.

4. Ds18b20 bit reading and writing timing

Write related

When to write to Ds18b20 0, the microcontroller directly pull the pin low, the duration of more than 60us is less than 120us can be. The figure shows that the microcontroller first pull down 15us, the DS18B20 will be in the time from 15us to 60us to read this bit, DS18B20 first will be read at 15us time, the typical value is at the time of 30us read, up to not more than 60US,DS18B20 must After reading, so the duration is longer than 60US.

When to write to Ds18b20 1, the microcontroller first pull the pin low, pull the lower time is greater than 1us, and then immediately release the bus, that is, pull the high pin, and the duration of more than 60US. Similar to write 0, Ds18b20 will read this 1 from 15us to 60US.

Read Related

When we want to read the data of Ds18b20, our microcontroller first to pull down this pin, and at least to maintain a 1us of time, and then release the PIN, after release to be read as soon as possible. From the pull-down pin to the read pin state, it cannot exceed 15us. As you can see from Figure 16-18, the host sampling time, which is the master SAMPLES, must be done within 15us.

There are two kinds of data processing methods, which are commonly used with decimals,
One is defined as a floating-point direct processing, the second is defined as an integer, and then the fractional and integer parts are separated, at the appropriate location points on the decimal point.

Special emphasis. The timing of the DS18B20 is strict, it is best not to interrupt during the writing process, but the interval between two "bits" is greater than 1 is less than infinity, that is, in this time period, we can open interrupts to deal with other programs.

Post the underlying code,

#include "config.h" #include <intrins.h> void Delayus (U8 t) {do{_nop_ ();
        _nop_ ();
        _nop_ ();
        _nop_ ();
        _nop_ ();
        _nop_ ();
        _nop_ ();
    _nop_ ();
}while (--T);

    } bit Get18b20ack () {bit ACK;
    EA = 0;
    Io_18b20 = 0;
    Delayus (250);
    Delayus (250);
    Io_18b20 = 1;
    Delayus (60);
    ack = IO_18B20; while (!
    IO_18B20);

    EA = 1;
return ~ack;

    } void Write18b20 (U8 dat) {U8 mask;
    EA = 0;
        for (mask=0x01; mask!=0; mask<<=1) {io_18b20 = 0;
        _nop_ ();
        _nop_ ();
        if (mask&dat) Io_18b20 = 1;
        else Io_18b20 = 0;
        Delayus (60);
    Io_18b20 = 1;
} EA = 1;
    } U8 Read18b20 () {U8 mask;

    U8 dat = 0;
    EA = 0;
        for (mask=0x01; mask!=0; mask<<=1) {io_18b20 = 0;
        _nop_ ();
        _nop_ ();
        Io_18b20 = 1;
        _nop_ ();
        _nop_ ();
   if (IO_18B20)         DAT |= mask;
    Delayus (60);

    } EA = 1;
return dat;

    } bit Start18b20 () {bit ACK;
    ACK = Get18b20ack ();
        if (ack = = 0) {write18b20 (0xCC);
    Write18b20 (0x44);
} return ~ack;
    } bit get18b20temp (int *temp) {bit ACK;

    U8 MSB, LSB;
    ACK = Get18b20ack ();
        if (ack = = 0) {write18b20 (0xCC);
        Write18b20 (0xBE);
        LSB = Read18b20 ();
        MSB = Read18b20 ();
    *temp = ((int) MSB << 8) + LSB;
} return ACK;

 }

If you want to implement it, it is possible to display the x digits after the decimal point.
Decimal in the digital tube processing, two ideas: unified definition for the integer type, and finally add a decimal point. Another is defined as floating point, then enlarging the corresponding multiple and then adding a decimal point. (Emmm, it looks the same.) ^_^: )

Here is an implementation of their own: through the datasheet can actually know that its accuracy is 0.0625, and then if we want to take two digits after the decimal point, in fact, calculate the integer multiplied by 6.25 on the line.

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.