Implementation of temperature Acquisition Programming for DS18B20 Based on fs4412 (1-Time Series Analysis)

Source: Internet
Author: User

Instructor Qin,Hua Qing vision embedded college lecturer.

1. Introduction to DS18B20

DS18B20 is a commonly used digital thermometer. The DS18B20 digital thermometer provides 9 to 12-bit (configurable) temperature readings, indicating the temperature of the device.

Information is sent to or from DS18B20 through a single bus interface, so there is only one line (and ground line) needed from the central microprocessor to the DS18B20 ).

The power supply required for reading, writing, and temperature conversion can be provided by the data line itself without external power supply. Since each DS18B20 contains a unique serial number, multiple DS18B20 can be mounted on a single bus. This allows temperature sensors to be placed in many different places.

This function is very useful and can be applied in fields including HVAC environmental control, temperature sensing within a building, equipment or machinery, process monitoring and control.

Ii. DS18B20 hardware connection


DS18B20 pin diagram DS18B20 schematic

The DS18B20 has three pins. According to the schematic diagram, the DS18B20 adopts the external power supply connection mode, while the DQ pin pulls a 10 k resistance. The DQ pin is connected to the gpk1_1 pin on the 4412 chip.

Iii. DS18B20 Timing Analysis

DS18B20 is a single bus, and the input and output are all through the DQ pin. We need to continuously operate the DQ pin according to the time sequence diagram to implement specific functions.

(1) DS18B20 reset sequence and function implementation

Through the reset signal, you can check whether the DS18B20 works normally and notify the DS18B20 to prepare for communication.

(1) 4412 lower the DQ pin and the reset cycle starts.

(2) The low level on the DQ pin is kept at us-960us, and then the bus is released.

(3) 4412 delay: 16us-60us: wait for DS18B20 to respond.

(4) DS18B20 lowers the DQ pin 60us-240us, indicating a response.

4412 read the DQ pin level. If it is low, Initialization is complete.

(5) After the DS18B20 response signal ends, the bus is released. The DQ pin is pulled up by the pull-up resistor and the reset cycle ends.

Code for resetting DS18B20:

Void DS18B20 ()
{
Gpk1.pud = 0; // gpk1_1 disable pulling up/down
Gpk1.con = (gpk1.con &~ (0xf <4) | 0x1 <4; // set gpk1_1 as the output pin.
Gpk1.dat & = ~ (0x1 <1); // set gpk1_1 to output '0' to lower the DQ pin.
Delay_us (700); // The latency is 600us.
Gpk1.dat | = 0x1 <1; // gpk1_1 release the bus
Gpk1.con & = ~ (0xf <4); // set gpk1_1 as the input pin
While (gpk1.dat & (0x1 <1); // wait for the DS18B20 response signal (DQ pin low level)
While (! (Gpk1.dat & (0x1 <1); // wait until the DS18B20 response signal ends (DQ pin high level)
}

(2) DS18B20 write time series and function implementation

When we need to write data from DS18B20, we must strictly write the time sequence of DS18B20 for operations.

Write logic 0 to DS18B20:

(1) 4412 lower the DQ pin and the write cycle starts.

(2) The DQ pin remains low by around 15 us.

(3) The DQ pin continues to maintain a low level of 20us -- 45us.

(4) release the bus.

Write logic 1 to DS18B20:

(1) 4412 lower the DQ pin and the write cycle starts.

(2) After the low level on the DQ pin remains greater than 1 us, the DQ pin is pulled up to 15 us in total.

(3) The DQ pin continues to maintain a high level of 20us-45us

(4) release the bus.

Write a byte code to DS18B20:

Void DS18B20 (unsigned char data)
{
Unsigned char I;
Gpk1.con = (gpk1.con &~ (0xf <4) | 0x1 <4; // set gpk1_1 as the output pin.

For (I = 0; I <8; I ++)
{
Gpk1.dat | = 0x1 <1; // set gpk1_1 output 1
Delay_us (1); // delay 1us
Gpk1.dat & = ~ (0x1 <1); // set gpk1_1 to output 0 to form a falling edge.
Delay_us (12); // delay of 12 US, greater than 1 US and less than 15 us
If (Data & 0x1) // if the data percentile is 1
Gpk1.dat | = 0x1 <1; // set gpk1_1 output 1
Delay_us (40); // latency: 40 us
Data = data> 2; // shift data to the right by 1
}
}

(3) DS18B20 write time series and function implementation

When we need to read data from DS18B20, we must strictly read the time sequence of DS18B20 for operations.

(1) lower the DQ pin by 4412 and start reading cycle.

(2) The low level on the DQ pin is kept at least 1 us time and then released.

(3) 4412 delay of several US (leaving the physical level reaction time) to read data from the DQ pin.

Note: According to the master samples instructions in the figure, the physical level conversion time is left from the start of the low-level conversion, and the read operation on the DQ pin in the last 4412 should be completed within 15 us.

(4) After 15 us, when the read Time Slot ends, the DQ pin is pulled back to the high level through the external pull-up resistor.

4412 latency: 45 US: wait until the reading cycle ends.

To read a byte data code from DS18B20:

Unsigned char DS18B20 ()
{
Unsigned char I, temp = 0; // The initial acceptance variable temp is 0.
For (I = 0; I <8; I ++)
{
Gpk1.con = (gpk1.con &~ (0xf <4) | 0x1 <4; // set gpk1_1 as the output pin.
Gpk1.dat | = 0x1 <1; // set gpk1_1 output 1
Gpk1.dat & = ~ (0x1 <1); // set gpk1_1 to output 0 to form a falling edge.
Gpk1.dat | = 0x1 <1; // gpk1_1 release the bus
Gpk1.con & = ~ (0xf <4); // set gpk1_1 as the output pin.
Temp> = 1; // The receiver variable shifts one bit to the right.
If (gpk1.dat & (0x1 <1) // read the DQ pin, if the DQ is 1
Temp | = 0x80; // accept the variable temp. the maximum position is 1.
Delay_us (30); // delay 30us
}
Return temp; // return the accept variable
}

Source:Huaqing vision embedded College,Original article address:Http://www.embedu.org/Column/Column909.htm

For more information about embedded systems, seeHua Qing visionInstructor blog>

Implementation of temperature Acquisition Programming for DS18B20 Based on fs4412 (1-Time Series Analysis)

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.