C program for DS18B20 of STC microcontroller

Publisher:trendsetter10Latest update time:2018-06-20 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Since the speed of STC microcontroller is faster than 8051, it belongs to 1 clock/machine cycle high-speed microcontroller. The high speed is of course its advantage, but for transplanting traditional 8051 programs, it is necessary to modify the timing to realize a certain function.

The following program is a simple driver for ds18b20. It was downloaded from the Internet. After a slight modification, it was debugged in STC12C5A32S2. Finally, the temperature is read and returned as an unsigned int. The lower 12 bits are the temperature data. The actual temperature value can be obtained by performing calculations in the main program. Hehe~~

/***********ds18b20 subroutine****************************/

/***********ds18b20 delay sub-function (crystal oscillator 11.0592MHz)*******/

void delay_18B20(unsigned int i)
{
while(i--);
}

/**********ds18b20 initialization function*************************/

void Init_DS18B20(void) 
{
unsigned char x=0;
DQ = 1; //DQ reset
delay_18B20(80); //Slight delay
DQ = 0; //MCU pulls DQ low
delay_18B20(800); //Precise delay greater than 480us
DQ = 1; //Pull the bus high
delay_18B20(140);
x=DQ; delay_18B20(200);
}

/***********ds18b20 reads a byte**************/

unsigned char ReadOneChar(void)
{
uchar i=0;
uchar dat = 0;
for (i=8;i>0;i--)
{
    DQ = 0; // give pulse signal
    dat>>=1;
    DQ = 1; // give pulse signal
    if(DQ)
    dat|=0x80;
    delay_18B20(40); //40
}
   return(dat);
}

/*************ds18b20 writes a byte****************/

void WriteOneChar(uchar dat)
{
   unsigned char i=0;
   for (i=8; i>0; i--)
   {
   DQ = 0;
    DQ = dat&0x01;
    delay_18B20(50); //50
    DQ = 1;
    dat>>=1;
}
}

/**************Read the current temperature of ds18b20************/

unsigned int ReadTemp(void)
{
unsigned char a=0;
unsigned char b=0;
unsigned int temp_value=0;

Init_DS18B20();
WriteOneChar(0xCC);     

WriteOneChar(0x44);  

delay_18B20(1000);      

Init_DS18B20();
WriteOneChar(0xCC);  
WriteOneChar(0xBE);  

delay_18B20(1000);

a=ReadOneChar(); //Read the low bit of the temperature value
b=ReadOneChar(); //Read the high bit of the temperature value

temp_value = b<<8;
temp_value |= a; 
return temp_value;
        
}


Reference address:C program for DS18B20 of STC microcontroller

Previous article:STC15F2K60S2 reads DS18B20 temperature serial port display
Next article:STC15F2K60S2 serial communication program

Recommended ReadingLatest update time:2024-11-16 20:52

ZDS2022 Oscilloscope 100 Practical Videos 76: DS18B20 Protocol Decoding
Hello everyone, DS18B20 is a digital temperature sensor. The digital transmission protocol followed by its interface is the so-called DS18B20 protocol. It is a single bus protocol with strong anti-interference ability and high precision, which is suitable for field temperature measurement in harsh environments. The ZD
[Test Measurement]
#51 MCU #DS18B20 hardware principle and communication working sequence
DS18B20 Hardware Principle - Temperature Memory The temperature measurement range of DS18B20 is -55~+125°C. As shown in the figure above, the DS18B20 temperature memory has two bytes in total. LSB is the low byte, MSB is the high byte. Msb is the high bit, Lsb is the low bit. The S in the figure represents the sign
[Microcontroller]
#51 MCU #DS18B20 hardware principle and communication working sequence
Basic parameters of DALLAS DS18B20
  The single-wire digital temperature sensors DS18B20 and DS1822 produced by DALLAS have an appearance as shown in Figure 5.8. They support the "one-wire bus" interface and have a measurement temperature range of -55°C to +125°C. In the range of -10 to +85°C, the accuracy is ±0.5°C. The accuracy of DS1822 is poor at ±2
[Microcontroller]
DS18b20 Single-Wire Digital Temperature Sensor
Digital temperature and humidity sensors are sensors that can convert temperature and humidity physical quantities into digital quantities that can be directly read by computers, PLCs, smart meters and other data acquisition devices through temperature and humidity sensitive components and corresponding cir
[Analog Electronics]
DS18B20 Temperature Measurement Experiment Based on 51 Single Chip Microcomputer
Experimental tasks A DS18B20 is used to form a temperature measurement system. The temperature measurement accuracy reaches 0.1 degrees. The temperature range is between -20 degrees and +50 degrees, and it is displayed with a 4-digit digital tube. DPY-1 experimental board connection Use a flat cable t
[Microcontroller]
DS18B20 Temperature Measurement Experiment Based on 51 Single Chip Microcomputer
Temperature measurement method using AT89C51 and DS18B20
  Temperature measurement and control are widely used in the use of lasers, fiber gratings, and other industrial and agricultural production and scientific research. The traditional method of temperature detection is to use analog temperature sensors such as thermocouples, thermal resistors, and semiconductor PN junct
[Microcontroller]
Temperature measurement method using AT89C51 and DS18B20
Classic digital temperature sensor DS18B20 test
    In the previous lesson, we learned the basic theoretical knowledge of the new digital temperature sensor DS18B20, and had a comprehensive understanding of the characteristics and applications of DS18B20. In this lesson, we will combine the S51 enhanced experimental board and ISP programmer to learn the application
[Microcontroller]
Classic digital temperature sensor DS18B20 test
The wireless transmission system of temperature measurement network in warehouse area based on DS18B20 and nRF2401
Introduction The temperature of the warehouse area is directly related to the safety and performance of the inventory materials. At present, the transmission of temperature data in the warehouse area is mostly wired, which has the disadvantages of difficult wiring, high material cost, inconvenient maintenance and re
[Microcontroller]
The wireless transmission system of temperature measurement network in warehouse area based on DS18B20 and nRF2401
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号