How to design a fun smart hardware for programming enthusiasts (VI)-first try • Collect the temperature and humidity (bottom)!

Source: Internet
Author: User

First, my idea: how to design a fun smart hardware for programming enthusiasts (a)--Plug and Play, building blocks, functional reorganization of the intelligent hardware module concept

Second, the children of other families: How to design a fun smart hardware for programming enthusiasts (ii)--How others design hardware bricks!

Third, MCU selection: How to design a fun smart hardware for programming enthusiasts (iii)-what kind of MCU to choose?

four, temperature and humidity sensor DHT11 drive Package (upper): How to design a fun smart hardware for programming enthusiasts (four)--first try to collect the temperature and humidity (on)!

five, temperature and humidity sensor DHT11 drive Package (medium): How to design a fun smart hardware for programming enthusiasts (five)--first try to collect the temperature and humidity (medium)!

Six, temperature and humidity sensor DHT11 drive package (bottom):

  

The process has been slightly interrupted these days, on the one hand because it helps a good friend deal with the insurance, and on the other hand, because I am thinking about whether a piece of paper is too frequent and lacks dry goods. So I decided that the model of the month 3 more than 0 days summed up ~ hereby notice! In four or 52, we analyzed DHT11 's drive flow in detail, because we needed a portable version, so the last time we dug a hole, we used a macro definition to make it as easy as possible to transplant. Therefore, this section will first discuss the knowledge of the macro definition and then encapsulate a slightly improved DHT11 driver.

C Macros

References & references: http://c.biancheng.net/cpp/html/1552.html Understanding Macros:   A macro is a preprocessing directive that provides a mechanism that can be used to replace a string in the source code, which is defined with a "#define" statement, and the following is an example of a macro definition:
#define VERSION-STAMP "1. 02 "
This form of macro, as defined in the example above, is often referred to as an identifier. In the example above, the identifier Version_stamp represents the string "1." 02 "--at compile preprocessing, each Version_stamp identifier in the source code is replaced by the string" 1.02 ".

Here is an example of another macro definition:
#define CUBE (x) ((x) * (x) * (x))
In the example above, a macro named cube with a parameter x is defined. The cube macro has its own macro body, which is ((x) * (x) * (x))--Each cube (x) macro in the source code is replaced ((x) * (x) * (x)) when the preprocessing is compiled.

using macros has the following advantages:
  (1) When entering the source code, you can save a lot of typing operations.
(2) Because macros are only defined once, but can be used multiple times, the use of macros can enhance the readability and reliability of the program.
(3) The use of macros does not require additional overhead, because the code represented by the macro expands only where the macro appears, so it does not cause jumps in the program.
(4) Macro parameters are not sensitive to types, so you do not have to consider which data types to pass to the macro. error-prone areas:
  It is important to note that there must be no spaces between the macro name and the parentheses that enclose the argument. In addition, in order to avoid ambiguity in translating macros, the macro body should also be enclosed in parentheses.

you may not know:
  A macro can also use some special operators, such as the string operator "#" and. The Join operator "# #". The "#" operator can convert a macro's arguments to a string with double quotes, see the following example:
Define Debug_value (v) printf (#v "is equal to%d. \ n ", V)
You can use the Debug_value macro to check the value of a variable in the program, see the following example:
int x=20;
Debug_value (x);
The above statement will print "X is equal to 20" on the screen. This example shows that the "#" operator used by the macro is a very handy debugging tool.
The function of the "# #" operator is to concatenate two separate strings into a single string.This article provides a simple introduction to the types of macros: http://blog.csdn.net/ocean181/article/details/6818687This article reads the macro in separate # to introduce: http://blog.sina.com.cn/s/blog_550405a10100bqar.htmlThis article speaks about several common pits used by macros: http://hbprotoss.github.io/posts/cyu-yan-hong-de-te-shu-yong-fa-he-ji-ge-keng.htmlThis article speaks about several advanced uses of macros: http://www.cnblogs.com/Anker/p/3418792.html ( do While) DHT11 Drive PackageDHT11.H
1 #ifndef __dht11_h__2 #define__dht11_h__3 4 #include "delay.h"5 6 //some typedef need to do7 typedef unsigned char T_UC; 8 typedef unsigned short T_us; 9 typedef unsigned int t_ui;10 typedef char T_C;11 typedef short T_S;12 typedef int t_i; - #define DHT11_DELAY_US (x) Delay_us ((x)) #define DHT11_DELAY_MS (x) Delay_ms ((x)) #define Dht11_data_pin p0_017 #define Dht11_data_pin_read Dht11_data_pin//read The Data_pin ' s DATA function #define Dht11_d Ata_pin_set dht11_data_pin=1 #define Dht11_data_pin_clear dht11_data_pin=020 #define DHT11_DATA_PIN_IN P 0dir&=~0x0121 #define Dht11_data_pin_out p0dir|=0x01 A  at the extern t_i dht11_getdata (T_us *humidity, T_us *temperature); -  -  - #endif

porting as long as it's right. H to make the changes on the line!

Above is the encapsulated DHT11 drive. h file, visible in order to achieve the purpose of portability to each platform, we redefine the data type in the Red section, and in the Green section we will define several external dependencies all macro. This way, when migrating to different platforms, you only need to move the data types that are redefined by the red section slightly based on the specific platform, and encapsulate and modify them according to the corresponding functions of the macro definition. C file is not required to move, is not very convenient! )

  Here is a description of some of the macro definitions:

①dht11_data_pin is the pin of the DHT11 communication data cable connected to the microcontroller
②dht11_data_pin_read is to read the data function on the Dht11_data_pin pin, returning to the state of the pin (high level is 1, low level is 0)
the ③dht11_data_pin_set and dht11_data_pin_clear functions are dht11_data_pin pin-high and low-level functions, respectively
the ④dht11_data_pin_in and Dht11_data_pin_out functions are set Dht11_data_pin PIN for input and output modes, respectively

PS:

The external file introduced here in line 4th provides the delay-delay correlation function, and the same 14th, 15-line macro definition also defines the function to be used for the delay microseconds and milliseconds, which is, in general, easy to transplant ~

DHT11. C

1#include"DHT11.H"2 3 4T_uc COM (void)//read 8bits data from COM5 {6T_UC flag, temp;//Flag and Temp7T_uc Comdata;//The 8bits data read from COM8 T_UC i;9      for(i =0; I <8; i++)Ten     { OneFlag =2; A          while((! Dht11_data_pin_read) && flag++); -Dht11_delay_us ( -); -temp =0; the         if(dht11_data_pin_read) temp =1; -Flag =2; -          while((dht11_data_pin_read) && flag++); -         if(Flag = =1) Break; +Comdata <<=1; -Comdata |=temp; +     } A     returnComdata; at } -  -T_i Dht11_getdata (T_us *humidity, T_us *temperature)//get the hunidity and temperature data - { -T_UC flag, temp;//Flag and Temp - T_uc uchart_data_h_temp, Uchart_data_l_temp, in ucharrh_data_h_temp, Ucharrh_data_l_temp, - ucharcheckdata_temp; to 32//1, start33 dht11_data_pin_out;//set the DATA PIN as out Mode34 dht11_data_pin_clear;35 Dht11_dela  Y_ms (19); >18ms36 dht11_data_pin_set;37 dht11_data_pin_in; Set the data pin as input mode38 dht11_delay_us (+); the      +     if(!dht11_data_pin_read) A     {42//2, WAIT43 flag = 2;44 while (! Dht11_data_pin_read) && flag++); flag = 2;46 while ((dht11_data_pin_read) && flag++);  - 48//3, read data49 ucharrh_data_h_temp = com (), ucharrh_data_l_temp = COM (); Wuyi Uchar T_data_h_temp = com (); uchart_data_l_temp = com (); ucharcheckdata_temp = com (); Wu  4, stop dht11_data_pin_set;  $          -Temp = (uchart_data_h_temp + uchart_data_l_temp + ucharrh_data_h_temp +ucharrh_data_l_temp); -         if(temp = = ucharcheckdata_temp)//Check the data, then updata humidity and temperature -         { A*humidity =ucharrh_data_h_temp; +*temperature =uchart_data_h_temp; the         } -     } $     Else //fail return-1 the     { the         return-1; the     } the      -     return 0; in}

. c file is not changed ~ and the first two sections of the comparison, here basically unchanged, just a part of the function replaced by a macro

To illustrate:

The first two sections of the 40bits data read from DHT11 include temperature and humidity integers and fractional portions of each 8bit representation + one 8bit checksum above. C file 61st, line 62 extracts only the integer part

Summary & Next plan:

At this point, we finally DHT11 to the package, the next will not be so frequent update (after all, too fast dry!) )。 Next time I'll show you the drive in CC2541 (which is based on it), the STM32, the porting method on the 51 platform, and then turn on a new driver package-the display. As to which one to choose, for the time being not decided, but also to leave a suspense it ~

Link

Experimental engineering based on the CC2541 platform

> Only dht11.c and DHT11.H after finishing, others may be a bit messy, forgive <

http://pan.baidu.com/s/1hq6N168

DHT11. C and DHT11.H files

Http://pan.baidu.com/s/1dD0ibDJ

Http://pan.baidu.com/s/1qeHBK

  

@beautifulzzzz

2015-9-9 Continuous Update in ~

How to design a fun smart hardware for programming enthusiasts (VI)-first try • Collect the temperature and humidity (bottom)!

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.