This blog post last modified: January 06, 2017, 11:06.
First, Introduction
This paper describes how to collect the temperature and humidity value of DHT11 in Simplebleperipheral Engineering.
second, the experimental platform
Protocol stack version: ble-cc254x-1.4.0
Compiling software: IAR 8.20.2
Hardware platform: Smart RF (main chip CC2541)
three . Copyright notice
Bo Main: Sweet melon
Statement: Water do not forget to dig well people, reproduced please indicate the source.
Original address: Http://blog.csdn.NET/feilusia
Contact Way: 897503845@qq.com
Melon ble CC2541 Group: 127442605
Melon ble CC2640 Group: 557278427
Melon ble android Group: 541462902 melon Single chip computer Stm8/stm32 group: 164311667
Sweet Big melon Shop (Taobao): https://shop217632629.taobao.com/?spm=2013.1.1000126.d21.hd2o8i
Four, the experiment Premise 1, before the step of this article, please read the following blog: No
2, before the steps of this article, please implement the following blog:
1 "CC2541 Serial transceiver": http://blog.csdn.net/feilusia/article/details/47431659
v. Basic knowledge
1, DHT11 is what.
A: The DHT11 is a digital temperature and humidity sensor, which includes 1 resistive sensing elements and a NTC thermometer, and is connected to 1 high-performance 8-bit monolithic microcomputer.
1) Humidity
Measuring range: 20-90%
Precision: ±5%RH
2) temperature
Measuring range: RH 0-50 ℃
Precision: ±2℃
2, how to hardware connection DHT11.
For:
As pictured above, although the DHT11 has 4 pins, it actually uses only 3, and one remains vacant. (module will eventually lead to only 3 pins)
The 2nd PIN can transmit data in two-way bidirectional with an IO port of MCU.
Note: This article uses the CC2541 P0_0 port to connect the DHT11 's 2nd pin.
Vi. Steps of the experiment
1, write and add DHT11 drive
1 Write a dht11.c (stored in the "... \ble-cc254x-1.4.0\projects\ble\simplebleperipheral\source\gua" path)
name:dht11.c//i
Ntroduce: Read DHT11 temperature and humidity//author: Sweet melon//changetime:2016.04.11//email:897503845@qq.com Map #include "DHT11.h" #include <ioCC2540.h> #include "string.h"//define #define Data_pin P0_0 The input port is P0.0 #define SET_PIN_IN p0dir &= ~0x01; ASM ("NOP"); ASM ("NOP")//config IO for input #define SET_PIN_OUT p0dir |= (1 << 0); ASM ("NOP"); ASM ("NOP")//config IO for output #define Set_data_pin data_pin = 1; ASM ("NOP"); ASM ("NOP")//Pull High P0.0 #define Clr_data_pin data_pin = 0; ASM ("NOP");
ASM ("NOP")//Pull Low P0.0 typedef signed CHAR int8;
typedef unsigned char uint8;
typedef signed short Int16; typedef unsigned Short uint16;
typedef signed long Int32;
typedef unsigned long UInt32;
function static void Delay_us (UInt16 nUs);
Static uint8 Dht11_read_char (void); Name:delay_u
//parameter:nus:us number//return:none of S//introduce:us delay function
Author: Sweet Big melon//email:897503845@qq.com//changetime:2016.04.11
static void Delay_us (UInt16 nUs) {
while (nus--) {asm ("NOP"), ASM ("NOP"), ASM ("NOP"), ASM ("NOP");
ASM ("NOP"), ASM ("NOP"), ASM ("NOP"), ASM ("NOP");
ASM ("NOP"), ASM ("NOP"), ASM ("NOP"), ASM ("NOP");
ASM ("NOP"), ASM ("NOP"), ASM ("NOP"), ASM ("NOP");
ASM ("NOP"), ASM ("NOP"), ASM ("NOP"), ASM ("NOP");
ASM ("NOP"), ASM ("NOP"), ASM ("NOP"), ASM ("NOP"); ASM ("NOP"); ASM ("NOP");M ("NOP"); ASM ("NOP");
ASM ("NOP"), ASM ("NOP"), ASM ("NOP"), ASM ("NOP"); }}//******************************************************************************//name:dh T11_read_char//introduce:dht11 reads a single byte function//parameter:none//return: None//author: Sweet big melon//email:897503845@qq.com//changetime:2016 .04.11//****************************************************************************** Static uint8 DHT11_Rea
D_char (void) {uint8 i;
Uint8 Ndht11_bit;
Uint8 Ndht11_char;
Uint8 Nflag;
for (i=0 i < 8; i++) {nflag = 2;
while ((data_pin = 0) && (nflag++));
Delay_us (19);
if (Data_pin = = 1) {ndht11_bit = 1;
} else{ndht11_bit = 0;
} Nflag = 2; while (Data_PIN = = 1) && (nflag++));
if (Nflag = = 1) {break;
} Ndht11_char <<= 1;
Ndht11_char |= ndht11_bit;
return Ndht11_char; }//******************************************************************************//name:dht1 1_read_humiture//introduce:dht11 's reading temperature and humidity function//parameter:none//return: True: Read correctly, false: Read error//author: Sweet big melon//email:897503845@qq.com//chang etime:2016.04.11//****************************************************************************** Boole
An dht11_read_humiture (uint8 *pdht11_data) {uint8 nhumidity_integer;
Uint8 Nhumidity_decimal;
Uint8 Ntemperature_integer;
Uint8 Ntemperature_decimal;
Uint8 nchecksum;
Uint8 Nchecksum_count;
Uint8 Nflag; Host start signal set_pin_out; IO port is configured to loseOut Set_data_pin; Output high level clr_data_pin; Output Low Level Delay_us (20000); Delay at least 18MS set_data_pin;
Output High level Delay_us (20); Switch to DHT11 end-to-end data set_pin_in;
IO port configuration for input delay_us (20);
if (Data_pin = = 0) {//dht11 response Nflag = 2;
while ((data_pin = 0) && (nflag++));
Nflag = 2;
while ((Data_pin = 1) && (nflag++)); DHT11 Read Value Nhumidity_integer = Dht11_read_char (); High humidity, Integer nhumidity_decimal = Dht11_read_char (); Low humidity, Fractional Ntemperature_integer = Dht11_read_char (); Temperature high, Integer ntemperature_decimal = Dht11_read_char (); temperature low, decimal nchecksum = Dht11_read_char (); Checksum//End Set_data_pin;
Compute checksum Nchecksum_count = Nhumidity_integer + nhumidity_decimal + Ntemperature_integer + ntemperature_decimal;
Verify data correctness if (nchecksum = = Nchecksum_count) {//correct, then save the read value to global variable
Pdht11_data[0] = Nhumidity_integer;
PDHT11_DATA[1] = Nhumidity_decimal;
PDHT11_DATA[2] = Ntemperature_integer;
PDHT11_DATA[3] = Ntemperature_decimal;
else {//error, the value is all 0 pdht11_data[0] = 0;
PDHT11_DATA[1] = 0;
PDHT11_DATA[2] = 0;
PDHT11_DATA[3] = 0;
return dht11_false; } else {//error, the value is all 0 pdht11_data[0] =
0;
PDHT11_DATA[1] = 0;
PDHT11_DATA[2] = 0;
PDHT11_DATA[3] = 0;
return dht11_false;
return dht11_true;
}
2 Write a header file DHT11
. h (Stored under "... \ble-cc254x-1.4.0\projects\ble\simplebleperipheral\source\gua" path)
//name: DHT11.h
/ /introduce: read DHT11 temperature
and humidity//author: sweet melon
//changetime: 2016.04.11
//email: 897503845@qq.com
//******************************************************************************
# ifndef __dht11_h__
#define __DHT11_H__
//define
typedef enum{
dht11_true,
dht11_false
}boolean;
function
extern boolean dht11_read_humiture (unsigned char *pdht11_data); Temperature and humidity sensing start
#endif
3) Add DHT11 to the project
. C and DHT11
. h
4 Add the Melon task's source file path in the IAR settings
2, add DHT11 related events
1) Define DHT11 collection events, DHT11 serial transmission events (SIMPLEBLEPERIPHERAL.C simplebleperipheral_processevent)
Cantaloupe//Cycle collection DHT11 temperature and humidity event if (events & SBP_DHT11_READ_HUMITURE_EVT) {//off total interruption, not off will appear 0 of the data EA = 0;
Collecting DHT11 temperature and humidity dht11_read_humiture (sbdht11_data);
Open Total Interrupt EA = 1; Start timer perform serial printing DHT11 temperature and humidity event Osal_start_timerex (Simplebleperipheral_taskid, sbp_dht11_usart_evt, Sbp_dht11_usart_evt_
PERIOD); Start timer execution cycle collect DHT11 temperature and humidity event Osal_start_timerex (Simplebleperipheral_taskid, sbp_dht11_read_humiture_evt, sbp_dht11_
Read_humiture_evt_period);
Return (events ^ sbp_dht11_read_humiture_evt);
}//Serial printing DHT11 temperature and humidity event if (events & sbp_dht11_usart_evt) {uint8 nstring[15] = {0};
Temperature sprintf ((char *) nstring, "DHT11 temp:%d.%d", sbdht11_data[2], sbdht11_data[3]);
Npi_printstring (nstring);
Npi_printstring ("\ r \ n");
Humidity sprintf ((char *) nstring, "DHT11 Humi:%d.%d", sbdht11_data[0], sbdht11_data[1]);
Npi_printstring (nstring);
Npi_printstring ("\ r \ n"); A few more lines to see the serial data NPI_PRIntstring ("\ r \ n");
Npi_printstring ("\ r \ n");
Return (events ^ sbp_dht11_usart_evt); }//Cantaloupe
2 define DHT11 collection events, DHT11 serial port to send events macros (SimpleBLEPeripheral.h)
#define SBP_DHT11_READ_HUMITURE_EVT 0x0004 //Cycle sampling DHT11 temperature and humidity Events
#define SBP_DHT11_USART_EVT 0x0008 //Serial printing DHT11 temperature and humidity events
3) Add a Time macro (SIMPLEBLEPERIPHERAL.C) used to DHT11 collection events, DHT11 serial delivery events
Cantaloupe
#define SBP_DHT11_USART_EVT_PERIOD
#define SBP_DHT11_READ_HUMITURE_EVT_PERIOD 1000
//Cantaloupe
3. Add Call DHT11 Collection event (sbp_start_device_evt event in simplebleperipheral.c simplebleperipheral_processevent)
if (Events & Sbp_start_device_evt)
{
//START the DEVICE
VOID gaprole_startdevice (& SIMPLEBLEPERIPHERAL_PERIPHERALCBS);
Start Bond Manager
VOID gapbondmgr_register (&SIMPLEBLEPERIPHERAL_BONDMGRCBS);
Set timer for the periodic event
Osal_start_timerex (Simplebleperipheral_taskid, sbp_periodic_evt, Sbp_ Periodic_evt_period);
Cantaloupe
/Start timer execution cycle collect DHT11 temperature and humidity event
Osal_start_timerex (Simplebleperipheral_taskid, sbp_dht11_read_humiture_ EVT, sbp_dht11_read_humiture_evt_period);
Cantaloupe return
(events ^ sbp_start_device_evt);
}
4, add DHT11 data buffer (in SIMPLEBLEPERIPHERAL.C)
Cantaloupe
static uint8 sbdht11_data[4] = {0};
Cantaloupe
5, add DHT11 related header file (SIMPLEBLEPERIPHERAL.C)
Cantaloupe
#include "DHT11.h"
#include "stdio.h"
/cantaloupe
Vii. Matters needing attention
Temporarily No
viii. Results of the experiment
Cantaloupe with two Android phone connection test, can be connected at the same time to ensure that the serial tool to receive DHT11 data.
And when the DHT11 breathe, you can see the temperature and humidity will rise.