51 + DS18B20 temperature measurement (continued)

Source: Internet
Author: User

In the previous article, we mentioned that we should call the encapsulated DS18B20 in a specific project to obtain the current temperature. Let's take a look at how to call the completed gettemp function in the following example to measure the temperature.

First, our design includes the following:

1. Use the DS18B20 digital temperature sensor to obtain the current temperature value.

2. display the current temperature value on the digital tube.

3. Send the temperature value to the host computer using the serial communication function of 51 single-chip microcomputer.

Next, let's take a look at the specific implementation:

Our idea is still modular. We divide the project into the following parts: DS18B20 module, digital control module, and serial communication module. The DS18B20 module has been encapsulated in the previous article, and the digital control and serial communication modules have also been introduced in my previous article. Therefore, we will first provide the code for each part of the module:

1. Overall Layout

The general layout mainly involves writing several important header files. These header files are the most basic things. Each module must contain them.

(1) Common. h file

Purpose: Define macro definitions commonly used in projects.

The Code is as follows:

# Ifndef _ common_h _ # DEFINE _ common_h _ # define bit (N) (0x01 <n) // used for bitwise operations # define enableinterrupt (Ea = 1) // enable/disable # define disableinterrupt (Ea = 0) // Guanzhong disconnected typedefunsigned char uchar; typedef unsigned int uint; void delay (uchar time); # endif

In the comon. h header file, a delay function is also declared as a common latency function. The definition of this function must be implemented in common. C.

The content of the common. c file is as follows:

#include "common.h"void delay(uchar time){uchar i=0;for(;i<time;i++);}

(2) des. h file

Purpose: Define the header files shared by other code files in the project.

#ifndef _INCLUDE_H_#define _INCLUDE_H_ #include <reg52.h>#include "common.h"#endif

2. DS18B20 Module

(1) header file DS18B20. h

Purpose: provide interfaces for external functions to call the functions of the DS18B20 module.

# Ifndef _ DS18B20 b20_h _ # DEFINE _ DS18B20 _ # include <reg52.h> // Rom operation instruction # define start_zh 0x44 # define read_rom 0x33 # define match_rom 0x55 # define skip_rom 0xcc # define search_rom 0xf0 # define alarm_search 0xec # define write_ram 0x4e # define read_ram 0xbe # define copy_ram 0x48 # define ready 0xb8 # define read_power limit DS = P2 ^ 2; /********************************** function: device initialization function parameters: no return value: no *********************************/void init_ds18b20 (); /********************************** function: waiting for device response parameters: no return value: no *********************************/void wait_ds18b20 (); /********************************** function: read the one-digit parameter of DS18B20: no return value: */bit read_ds18b20 b20_bit (); /********************************** function: read the One-byte parameter of DS18B20: no return value: * (); /********************************** function: write the one-bit parameter of DS18B20: the return value to be written: no ********************************/void write_ds18b20 b20_bit (bit dat ); /********************************** function: write the One-byte parameter of DS18B20: the return value of a byte to be written: no ********************************/void write_ds18b20 b20_byte (uchar dat ); /********************************** function: Release command parameters: command byte return value: no ********************************/void write_ds18b20 b20_command (uchar com ); /********************************** function: get the current temperature value parameter: no return value: current temperature value *********************************/INT gettemp (); # endif

(2) DS18B20. c file

Purpose: corresponds to the specific implementation of the function declaration in the header file.

# Include "DES. H "# include" DS18B20. H "/********************************* function: device initialization function parameter: no return value: No remarks: Call wait_ds18b20 () to receive the DS18B20 response ********************************/void init_ds18b20 () {uint I = 100; // bus pulling low DS = 0; // latency of over us while (I) {I --;} // bus pulling high, wait for the DS18B20 response DS = 1 ;} /********************************** function: waiting for device response parameters: no return value: no *********************************/void wait_ds18b20 () {// wait for the response while (DS); While (~ DS);}/********************************* function: read the one-digit parameter of DS18B20: no return value: */bit read_ds18b20 b20_bit () {uint I = 1; bit res; DS = 0; I ++; // stop pulling down the bus and read the value DS = 1 from the DQ pin; // I ++; Res = Ds; // latency I = 10; while (I) {I --;} return res ;} /********************************** function: read the One-byte parameter of DS18B20: no return value: * () {uchar res = 0; uchar TEM = 0; uint I = 0; For (; I <8; I ++) {TEM = read_ds18b20 b20_bit (); // the I-th Read is the I-th res |=( TEM <I);} return res ;} /********************************** function: write the one-bit parameter of DS18B20: the return value to be written: no ********************************/void write_ds18b20 b20_bit (bit dat) {uint J = 0; // when the bus is low, DS = 0; j ++; // when the latency is reached, data is placed on the bus DS = dat; j = 8; while (j> 0) {J --;} // After 18b20 reads data, pull the bus to DS = 1; j ++ ;} /********************************** function: write the One-byte parameter of DS18B20: the return value of a byte to be written: no ********************************/void write_ds18b20 b20_byte (uchar dat) {uint I = 0; For (; I <8; I ++) {// gets the last digit of dat, to determine whether to write 1 or write 0 if (DAT> I) & bit (0) write_ds18b20 b20_bit (1); elsewrite_ds18b20 b20_bit (0 );}} /********************************** function: Release command parameters: command byte return value: no ********************************/void write_ds18b20 b20_command (uchar com) {Bit Flag = 0; If (Ea = 1) {flag = 1 ;}// disconnect first to prevent the interruption from affecting the time series disableinterrupt; // initialize the DS18B20 b20init_ds18b20 (); // wait for response wait_ds18b20 (); // skip romwrite_ds18b20 b20_byte (skip_rom); // write command byte write_ds18b20 b20_byte (COM); // enable interrupt if (flag = 1) {enableinterrupt ;}} /********************************** function: get the current temperature value parameter: no return value: current temperature value *********************************/INT gettemp () {uchar low, high; int value = 0; float TMP = 0.0; Bit Flag = 0; // publish the command to start the conversion write_ds18b20 command (start_zh); If (Ea = 1) {flag = 1;} disableinterrupt; // read Ram to obtain the temperature write_ds18b20 command (read_ram); low = read_ds18b20 b20_byte (); high = read_ds18b20 b20_byte (); // obtain intvalue = (high <8 | low); // obtain the actual temperature TMP = value * 0.0625; // return an integer when the actual temperature is increased by 100 times, that is, two values = (INT) (TMP * 100) after the decimal point; if (flag = 1) {enableinterrupt;} return value ;}

3. Digital Tube module

The digital tube module has been written in previous articles. It can be used directly here, which also reflects the advantages of modularization.

(1) shumaguan. h file

# Ifndef _ shumaguan_h _ # DEFINE _ shumaguan_h _ # include "includes. H "# define shuma_port P0 # define shuma_num 6 sbit Dula = P2 ^ 6; sbit wela = P2 ^ 7; ********** * *************/uchar shuma_style [] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x27, 0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79,0x71,0x00}; # endif

Similarly, this header file defines the variable for operating the digital tube on the Development Board.

This module does not have a. c file.

4. Serial Communication UART Module

51 Single-Chip Microcomputer communicates with other single-chip microcomputer or computer and other upper computers by using the chip peripherals UART, our module provides UART encapsulation.

(1) UART. h header file

# Ifndef _ uart_h _ # DEFINE _ uart_h _ # define enableinterrupt_uart (ES = 1) # define disableinterrupt_uart (ES = 0) /*************************************** * function: initialize and set the working mode and baud rate parameters: working mode, baud rate return value: no *************************************** **/void init_uart (INT workstyle, int borate ); /*************************************** function: send a byte parameter: Return Value of the sent byte: no *************************************** */void send_uart_byte (uchar dat ); # endif

(2) UART. c file

# Include "DES. H "# include" UART. H "Void init_uart (INT workstyle, int borate) {Switch (workstyle) {Case 0: break; Case 1: scon | = bit (6); break; Case 2: scon | = bit (7); Case 3: scon | = (BIT (6) | bit (7); default: break;} scon | = bit (4 ); tmod | = bit (5); // enable the working method of counter 1 2 switch (borate) {Case 9600: Th1 = 0xfd; TL1 = 0xfd; break; Case 4800: th1 = 0xfa; TL1 = 0xfa; break; // other baud rates are not listed here. // you can refer to the Data Manual for setting} tr1 = 1;} void send_uart_by Te (uchar dat) {sbuf = dat; while (! Ti); Ti = 0 ;}

5. Write the main function

These modules have been encapsulated. The next step is to call them in the main function to achieve the corresponding purpose.

After the main function is executed, first initialize the resources and variables on the board, and then enter the while endless loop. In this loop, we constantly use the gettemp function to query the current temperature and send the temperature to the computer's serial port. Due to the fact that the sending speed is too fast, we have considered sending at a certain interval. At the same time, in order to achieve dynamic scanning and display, the digital tube needs to be refreshed on a regular basis, so the microcontroller timer is used.

# Include "DES. H "# include" shumaguan. H "# include" DS18B20. H "# include" UART. H "int temp; uchar code th0 = 0xfe; uchar code tl0 = 0x33; uchar dispbuf [6]; volatile uchar COUNT = 0; volatile uchar uart_count = 0; bit Flag = 0; // initialization timer 0 void init_timer0 () {tmod = 0x01; th0 = th0; tl0 = tl0; enableinterrupt; et0 = 1; tr0 = 1 ;} // initialize void Init () {init_timer0 (); init_uart ();} // send a four-digit void send_uart (INT dat) to the serial port) {uchar Qian = DAT/1000; UC Har Bai = dat % 1000/100; uchar Shi = dat % 100/10; uchar Ge = dat % 10; send_uart_byte (Qian + '0'); send_uart_byte (BAI + '0 '); send_uart_byte (Shi + '0'); send_uart_byte (Ge + '0'); send_uart_byte ('E');} int main () {int value_temp; Init (); while (1) {value_temp = gettemp (); temp = value_temp> 0? Value_temp:-value_temp; // the scheduled time is up. send data to the serial port if (FLAG) {flag = 0; send_uart (temp) ;}} return 0 ;} // digital display function. Void display (INT dat) {uchar style = 0x00 is called in the interrupt service function of the timer; // store dispbuf [0] = DAT/1000; dispbuf [1] = dat % 1000/100; dispbuf [2] = dat % 100/10; dispbuf [3] = dat % 10; // select wela = 1; shuma_port = ~ Bit (count); // The number of digital tubes on which wela = 0; // select Dula = 1; if (COUNT = 0 | COUNT = shuma_NUM-1) {style = shuma_style [0];} else {style = shuma_style [dispbuf [count-1]; If (COUNT = 2) {style | = bit (7 ); // light up the decimal point, that is, set the highest position to 1} shuma_port = style ;} // interrupt service function of the timer 0 // call the display function to display the current temperature of the digital tube // time to determine whether to send data to the serial port, modify the flag Flag void timer0_ser () interrupt 1 {uart_count ++; If (uart_count = 200) {flag = 1; uart_count = 0 ;}count ++; If (COUNT = shuma_num) {COUNT = 0;} display (temp); th0 = th0; tl0 = tl0 ;}

In this way, we have implemented the functions to be implemented. The following is a reality:

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.