51 MCU Introduction - DS18B20 Temperature Sensor

Publisher:老卫Latest update time:2022-04-22 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

DS18B20——Temperature sensor. The microcontroller can communicate with DS18B20 through 1-Wire and finally read the temperature. The hardware interface of 1-Wire bus is very simple. You only need to connect the data pin of 18B20 to an IO port of the microcontroller to communicate. The temperature storage value with a maximum value of 12 is stored in the form of complement.
2 bytes, LSB low byte, MSB high byte, -55~125


1. Initialization

Detect the existence of pulses: If there is a DS18B20 on the bus, the bus will return a low-level pulse according to the timing requirements. The microcontroller needs to pull down this pin for about 480us to 960us, and it lasts for 500us in our program. Then, the microcontroller releases the bus, that is, gives a high level. After waiting for about 15 to 60us, the DS18B20 will actively pull down this pin for about 60 to 240us, and then the DS18B20 will actively release the bus, so that the IO port will be automatically pulled up by the pull-up resistor.


2. ROM operation instructions

Skip ROM: 0xCC. When there is only one device on the bus, ROM can be skipped and no ROM detection is performed.


3. RAM memory operation instructions

Read Scratchpad: 0xBE - The temperature data of DS18B20 is 2 bytes. When we read the data, we first read the low bit of the low byte. After reading the first byte, we read the low bit of the high byte until both bytes are read.


Convert Temperature (start temperature conversion): 0x44 - 12-bit maximum conversion time is 750ms


4. DS18B20 bit write timing

When writing '0' to DS18B20, the microcontroller directly pulls the pin low for a duration greater than 60us and less than 120us. The figure shows that after the microcontroller pulls the pin low for 15us, DS18B20 will read this bit between 15us and 60us. DS18B20 will read at the earliest 15us, the typical value is 30us, and it will not exceed 60us at most. DS18B20 will definitely finish reading, so the duration can be more than 60us.

       

When writing '1' to DS18B20, the microcontroller first pulls the pin low for more than 1us, then immediately releases the bus, i.e. pulls the pin high for more than 60us. Similar to writing '0', DS18B20 will read the '1' between 15 and 60us.


5. DS18B20 bit read timing

The MCU must first pull down this pin and keep it for at least 1us, then release the pin and read it as soon as possible after release. From pulling down this pin to reading the pin status, it cannot exceed 15us. As you can see from Figure 16-17, the master sampling time, that is, MASTER SAMPLES, must be completed within 15us.


#include

 

#include

 

typedef unsigned char uchar;

 

sbit IO_18B20 = P3 ^ 2; //DS18B20 communication pin

 

/* Software delay function, delay time (t*10)us */

 

void DelayX10us(volatile t)

 

{

 

  do

 

  {

 

    _nop_();

 

    _nop_();

 

    _nop_();

 

    _nop_();

 

    _nop_();

 

    _nop_();

 

    _nop_();

 

    _nop_();

 

  }

 

  while (--t);

 

}

 

/* Reset the bus and get a presence pulse to start a read or write operation */

 

bit Get18B20Ack()

 

{

 

  bit ack;

 

 

 

  EA = 0; //Disable general interrupt

 

  IO_18B20 = 0; //Generate 500us reset pulse

 

  DelayX10us(50);

 

  IO_18B20 = 1;

 

  DelayX10us(6); //delay 60us

 

  ack = IO_18B20; //Read the existence pulse

 

  while(!IO_18B20); //Wait for the pulse to end

 

  EA = 1; //Re-enable the general interrupt

 

 

 

  return ack;

 

}

 

/* Write a byte to DS18B20, dat-byte to be written*/

 

void Write18B20(uchar dat)

 

{

 

  flying mask;

 

 

 

  EA = 0; //Disable general interrupt

 

  for (mask = 0x01; mask != 0; mask <<= 1) // Low bit first, shift out 8 bits in sequence

 

  {

 

    IO_18B20 = 0; //Generate 2us low level pulse

 

    _nop_();

 

    _nop_();

 

    if ((mask & dat) == 0) //output the bit value

 

    {

 

      IO_18B20 = 0;

 

    }

 

    else

 

    {

 

      IO_18B20 = 1;

 

    }

 

    DelayX10us(6); //delay 60us

 

    IO_18B20 = 1; //Pull up the communication pin

 

  }

 

  EA = 1; //Re-enable the general interrupt

 

}

 

/* Read a byte from DS18B20, return value - the byte read*/

 

fly Read18B20()

 

{

 

  flying dat;

 

  flying mask;

 

 

 

  EA = 0; //Disable general interrupt

 

  for (mask = 0x01; mask != 0; mask <<= 1) //Low bit first, collect 8 bits in sequence

 

  {

 

    IO_18B20 = 0; //Generate 2us low level pulse

 

    _nop_();

 

    _nop_();

 

    IO_18B20 = 1; //End low level pulse, wait for 18B20 to output data

 

    _nop_(); //Delay 2us

 

    _nop_();

 

    if (!IO_18B20) //Read the value on the communication pin

 

    {

 

      dat &= ~mask;

 

    }

 

    else

 

    {

 

      that |= mask;

 

    }

 

    DelayX10us(6); //Delay another 60us

 

  }

 

  EA = 1; //Re-enable the general interrupt

 

 

 

  return that;

 

}

 

/* Start a 18B20 temperature conversion, return value - indicates whether the start is successful*/

 

bit Start18B20()

 

{

 

  bit ack;

 

 

 

  ack = Get18B20Ack(); //Execute bus reset and get 18B20 response

 

  if (ack == 0) //If 18B20 responds correctly, a conversion is started

 

  {

 

    Write18B20(0xCC); //Skip ROM operation

 

    Write18B20(0x44); //Start a temperature conversion

 

  }

 

  return ~ack; //ack==0 means the operation is successful, so the return value is negated

 

}

 

/* Read the temperature value converted by DS18B20, and return value - indicates whether the reading is successful*/

 

bit Get18B20Temp(int *temp)

 

{

 

  bit ack;

 

  uchar LSB, MSB; //16bit temperature value low byte and high byte

 

 

 

  ack = Get18B20Ack(); //Execute bus reset and get 18B20 response

 

  if (ack == 0) //If 18B20 responds correctly, read the temperature value

 

  {

 

    Write18B20(0xCC); //Skip ROM operation

 

    Write18B20(0xBE); //Send read command

 

    LSB = Read18B20(); //Read the low byte of the temperature value

 

    MSB = Read18B20(); //Read the high byte of the temperature value

 

    *temp = ((int)MSB << 8) + LSB; //synthesize into 16-bit integer

 

  }

 

  return ~ack; //ack==0 indicates an operation response, so the return value is its inverse value

 

}


Reference address:51 MCU Introduction - DS18B20 Temperature Sensor

Previous article:51 MCU Introduction - IIC (I2C) Bus
Next article:51 MCU Introduction - SPI Bus

Recommended ReadingLatest update time:2024-11-15 09:12

Based on ds18b20 temperature band upper limit program--LCD1602 liquid crystal display
This is displayed using 1602. The program is a modification of the previous experiments. When the temperature is =40 degrees, the fan rotates. This is a microcontroller system experiment for participating in an electronic competition. The program is in assembly language (ASM) with detailed comments, which is worth refe
[Microcontroller]
Digital tube display program based on 51 single chip microcomputer and DS18B20 temperature sensor
#include reg52.h #include intrins.h sbit DQ=P1^4;//temperature acquisition sbit latch1=P2^2;//segment latch sbit latch2=P2^3;//bit latch unsigned char code dofly_DuanMa ={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//display segment code 0~9 unsigned char code dofly_WeiMa ={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,
[Microcontroller]
Design of MOA temperature remote monitoring system based on DS18B20
Metal oxide arrester (MOA) is the main facility to protect power supply system and electrical equipment from lightning hazards. Once a fault occurs, it will not only lose its proper lightning protection function, but also may cause power supply accidents. Therefore, in order to ensure the normal function of MOA, it is
[Microcontroller]
Design of MOA temperature remote monitoring system based on DS18B20
MCU drives ds18b20 to display temperature using 12864 LCD
  Download the complete source code: http://www.51hei.com/bbs/dpj-20391-1.html Below is the ds18b20.h file: /*------------------------------------ Name: ds18b20 driver and display module Written by: YuMin_Dong Time: 2013.01.21 ------------------------------------*/ #ifndef _DS18B20_H_ #define _DS18B20_H_
[Microcontroller]
MCU drives ds18b20 to display temperature using 12864 LCD
STM8 DS18B20 temperature sensor subroutine
software design /********************************************************************* Purpose: Create a DS18B20 operation library Target system: Based on STM8 microcontroller  Application software: Cosmic CxSTM8  *********************************************************************/ void _delay_us(u8 us) { while(-
[Microcontroller]
Design and simulation of multi-channel temperature monitoring system based on DS18B20
0 Introduction     In real-time temperature monitoring systems, such as greenhouse temperature monitoring, cold storage temperature measurement, intelligent building temperature control and other systems, it is often necessary to collect and detect multiple temperatures. Rapid and reliable acquisition of high-precisi
[Microcontroller]
Design and simulation of multi-channel temperature monitoring system based on DS18B20
DS18B20 Intelligent Temperature Sensor Driver
/************Port definition, can be modified*****************/ sbit ledrs=P2^3; //Read and write control word     sbit leden=P2^5; //Enable control word sbit ledrw=P2^4; void write_com(uchar com) {   ledrw=0;  ledrs=0;  P0 =com;  delayl(1);  leden=1;  delayl(1);  leden=0; } void write_data(uchar date) {
[Microcontroller]
DS18B20 header file for AVR M16 8M
Just call gettemp(); and it's done! The export parameter wmh is the high bit of the display, and wml is the low bit of the display, then call your display program!  /****************************************************************************  ds18b20 header file     M16 internal 8M          *********************
[Microcontroller]
Latest Microcontroller Articles
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号