Single bus driver (DS18B20)

Publisher:温暖梦想Latest update time:2016-12-31 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

sbit DQ=P3^7; //DS18B20 data transmission line connects to the corresponding pin of the microcontroller

 

void delaymm(uint z)

{

     uint x,y;

     for(x=z;x>0;x--)

     for(y=110;y>0;y--);

}

 

 //Read one byte of data from DS18B20, starting from the low bit

ReadOneChar(void)       

{

     unsigned char i=0;         

     unsigned char dat=0;

    for (i=8;i>0;i--)      

         {

                 DQ=1;

                delay(1);

                DQ=0;     

                dat>>=1; //Shift the read value to the left by one bit to form a byte

                DQ=1;

            if(DQ)

              dat|=0x80; //If the highest DQ is 1, the highest position is 1

         delay(4);

         }

    return(that);

}

 

//Write one byte of data to DS18B20, starting from the lowest bit

void WriteOneChar(unsigned char dat) 

{

     unsigned char i=0;         

     for(i=8;i>0;i--) 

     {

         DQ=0;   

         DQ=dat&0x01; //If the lowest bit is 1, write 1

         delay(5);

         DQ=1;

         dat>>=1; //Shift left by one bit, for example, i = 8, after comparison, it is consistent, i = 7 is the first shift left by one bit, so that the second bit is compared in the next cycle & 0X01

     }

     delay(4);

}

 

void Init_DS18B20(void)/*Function: DS18B20 initialization subroutine*/

{

     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 temperature value from DS18B20

ReadTemperature(void) 

     Init_DS18B20(); //Initialization

     WriteOneChar(0xcc); //Skip the operation of reading the serial number

     WriteOneChar(0x44); //Start temperature conversion

     delaymm(125); //The conversion takes a little time, delay

     Init_DS18B20(); //Initialization

     WriteOneChar(0xcc); //Skip the operation of reading the serial number

     WriteOneChar(0xbe); //Read the temperature register (the first two values ​​are the low and high bits of the temperature respectively)

     delaymm(125);

     tempL=ReadOneChar(); //Read the low LSB of the temperature

     tempH=ReadOneChar(); //Read the high MSB of temperature 

     //Temperature conversion, convert the high and low bits into actual temperature

     temperature=((tempH*256)+tempL)*0.0625;

     delay(200);

     return(temperature);

}


Reference address:Single bus driver (DS18B20)

Previous article:A little knowledge about MCU
Next article:Serial communication uses parity check to send data

Recommended ReadingLatest update time:2024-11-16 13:23

SNUG World 2024: Synopsys releases new AI-driven EDA, IP and system design solutions
Sassine Ghazi, President and CEO of Synopsys, shares in-depth insights into the new opportunities in the era of intelligent everything Summary: Demonstrate the strong application momentum of Synopsys.ai among partners, including significant optimization of PPA, turnaround time, etc. by tools such as DSO.ai and V
[Semiconductor design/manufacturing]
In response to the "three elements" of high-quality development of the manufacturing industry, Universal Robots drives R&D innovation with customer insights
In response to the "three elements" of high-quality development of the manufacturing industry, Universal Robots drives R&D innovation with customer insights Shanghai, China, November 1, 2022 The 20th National Congress of the Communist Party of China (the "20th National Congress") was recent
[robot]
In response to the
Different applications determine different LED driving methods
Using a linear regulator to convert voltage will face power consumption issues, while the switching method will have noise issues. The choice of conversion method for LED drivers depends on the application. ——Bill Rypka Bill Rypka Muzahid Huda Maxim HB LED driver product marketing manager. What kind
[Power Management]
Design and Analysis of DS18B20 Digital Thermometer
    1. Basic knowledge of DS18B20   The DS18B20 digital thermometer is a 1-Wire device produced by DALLAS, which has the characteristics of simple circuit and small size. Therefore, it is used to form a temperature measurement system, which has simple circuit. Many such digital thermometers can be hung on one communic
[Microcontroller]
Design and Analysis of DS18B20 Digital Thermometer
ATMEGA8 microcontroller drives stepper motors
#include #include #defineucharunsignedchar #defineuintunsignedint ucharnp; //Stepper motor operation data table constucharmotortb[]={0x11,0x99,0x88,0xCC,0x44,0x66,0x22,0x33}; voiddelay_nms(uintms) //delay subroutine for each step { swimming; for(i=0;i_delay_loop_2(8*250); } void a_step (uchard, uchart) //stepper motor
[Microcontroller]
ATMEGA8 microcontroller drives stepper motors
51 MCU LCD1602 driver
/* ******************************************** **Header file name: LCD driver      ** Target : AT89s52 ** Crystal: 11.0952 ********************************************* */ #ifndef  _LCD_51_H_ #define  _LCD_51_H_ #include "DELAY_51.H" #include at89x52.h /* ------80s52 and LCD connection information------------------
[Microcontroller]
89C51 programmer end driver example
Introduction: This article provides an example of a 89C51 programmer driver /* 89C51 series CPU programmer receives CPU program*/ #include reg51.h #include intrins.h #include absacc.h #define e 8 #define p 9 #define l 10 sbit led=P3^2; sbit p27=P2^7; sbit p26=P2^6; sbit p36=P3^6; sbit p37=P3^7; sbit rst=P3^3; sbi
[Microcontroller]
Multi-level LED driver design easily achieves efficient dimming
Although the basic LED driver designed for general lighting applications is relatively simple, the design becomes very complex when additional features such as phase cut dimming and power factor correction are required. Non-dimming LED drivers without power factor correction generally include an offline switching power
[Power Management]
Multi-level LED driver design easily achieves efficient dimming
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号