Use of DHT11 temperature and humidity sensor based on ATmega128

Like the DS18B20, the DHT11 is a single-bus chip. Unlike the DHT10, one of its four pins is an empty pin. Similar to the DS18B20, the timing requirements are relatively high. The difference lies in the data acquisition during program writing. Must be separated by more than 1s, otherwise the acquisition will fail.

This article refers to the address: http://

Also, the data port of the DHT11 should preferably be connected to a pull-up resistor, or the internal pull-up of the microcontroller can be used.

The DHT11 data sheet is available online and has a detailed description of the timing operations. Personally, it is recommended to write this program while writing and detecting it (for example, after calling the reset subroutine, call it once in the main function to see if it succeeds...), otherwise it is very likely that the error will not be found at the end. Where, I have been writing and then not good, and finally rewritten!

Gossip does not say, the following helps you analyze the timing diagram of DHT11 (databook), because DHT11 has high requirements on timing, so it is very likely that the program is not good. I suggest that the delay sub-function is best to use the oscilloscope to detect it, and the error calculated by itself will be very large under 10us.

Enter the topic: If you say something below, you can refer to the following procedure.

Some of the contents of the data sheet can be understood by yourself. First, look at the host reset signal and the corresponding signal of DHT11 in the data sheet.

The host first controls the bus, pulls it down for at least 18ms, and then pulls it up 20~40us. (At this time, if the hardware has no problem, DHT11 will respond.) So the host releases the bus (clears the DDRXN register) and waits for the DHT11 response. If successful DHT11 will produce a low level of 40~50us, and a high level of 40~50us. Here the test can be done by the program.

Next, in one acquisition, the bus is handed over to DHT11, which will send a 40-bit binary number to the host. The first 0~7 bits are the integer part of the humidity, and the 8~15 bits are the fractional part of the humidity; 16~23 The bit is the integer part of the temperature, the 24~31 bits are the fractional part of the temperature; the last eight bits are the check bits. These data are processed by the program and converted into actual values ​​of temperature and humidity, which are displayed by the display part. (I use a digital tube, it is recommended to use 1602 display will be more convenient).

I will not explain the processing part later. I have comments in the program. I can add a program to the project to see the effect is much better. I can also use the special reading software (source insignte), otherwise The fonts are all very messy.

=========================================================

//This is delay.h /************* The crystal oscillator of my development board is 16M. The specific delay sub-function should be carefully written by myself. ***/

#ifndef __DELAY_H

#define __DELAY_H

Void delay_us(unsigned int xus);

Void delay_ms(unsigned int xms);

#endif

=========================================================

//This is delay.c

#include"delay.h"

#include

//delay subtle subfunction

Void delay_us(unsigned int xus)

{

Unsigned int i,j;

For(i=0;i

{

NOP(); NOP(); NOP(); NOP(); NOP(); NOP();

NOP(); NOP(); NOP(); NOP();

}

}

/ / Delay milliseconds subfunction

Void delay_ms(unsigned int xms)

{

Unsigned int i,j;

For(i=0;i

{

For(j=0;j<2288;j++);

}

}

=========================================================

//This is dht11.h

#ifndef __DHT11_H

#define __DHT11_H

#ifndef __IOM128V_H

#include

#endif

#ifndef __MACROS_H

#include

#endif

#define DDR_1 DDRC|=BIT(PC0)

#define DDR_0 DDRC&=~BIT(PC0)

#define PORTC_1 PORTC|=BIT(PC0)

#define PORTC_0 PORTC&=~BIT(PC0)

#define DQ (PINC&0x01)

Void caiji(void);

Long int dht(void);

Void init_dht11(void);

//void ceshi(void);

#endif

=========================================================

//This is dht11.c

#include"dht11.h"

Unsigned char dht_data[5], a, b;

Unsigned int s1,s0,t1,t0,sd,wd,wsd;

Void caiji(void)

{

Unsigned char i,j;

//delay_ms(900);

For(i=0;i<5;i++)

{

Dht_data[i]=0x00; //Array clear

For(j=0;j<8;j++)

{

While(!DQ); //Check if it is high

/ / Delay 50us if it is high, it is one, otherwise zero

Delay_us(50);

If(DQ)

{

Dht_data[i]|=BIT(7-j); //Save data

While(DQ);//Low detection

}

}

}

}

Void init_dht11(void)

{

DDR_1; //Set host output

PORTC_0; //Bus pulled low for at least 18ms

Delay_ms(20);

PORTC_1; //The bus is pulled up by the host by about 30us

Delay_us(30);

DDR_0; //The host is set to input, detecting the slave signal

While(DQ);

}

Long int dht(void)

{

Init_dht11();

If(!DQ)

{

While(!DQ);

While(DQ); //Start receiving signals after the above two sentences

Caiji();

DDR_1;

PORTC_1;

//check

a=

(

Dht_data[0]+dht_data[1]+dht_data[2]+dht_data[3]

);

If(a==dht_data[4])

{

S1=dht_data[0];

S0=dht_data[1];

T1=dht_data[2];

T0=dht_data[3];

}

//s is humidity, t is temperature

Sd=s1;

Sd<<=8;

Sd|=s0;

Wd=t1;

Wd<<=8;

Wd|=t0;

Wsd=sd<<16;

Wsd|=wd;

}

Return wsd;

}

=========================================================

//This is xianshi.h

#ifndef __XIANSHI_H

#define __XIANSHI_H

#ifndef __IOM128V_H

#include

#endif

#define SCK_0 PORTB&=~(1<

#define SCK_1 PORTB|=(1<

#define LCK_0 PORTB&=~(1<

#define LCK_1 PORTB|=(1<

#define SDI_0 PORTB&=~(1<

#define SDI_1 PORTB|=(1<

Void init(void);

Void send_595(unsigned char dat);

Void digitron_show(unsigned int int_part,unsigned int float_part);

#endif

=========================================================

//This is xianshi.c

#include"xianshi.h"

#ifndef __DELAY_H

#include"delay.h"

#endif

#ifndef __DHT11_H

#include"dht11.h"

#endif

/ / Digital tube display array definition

Const unsigned char table[]=

{

0x3F, / / ​​0

0x06, / / ​​1

0x5B, / / ​​2

0x4F, / / ​​3

0x66, / / ​​4

0x6D, / / ​​5

0x7D, / / ​​6

0x07, / / ​​7

0x7F, / / ​​8

0x6F, / / ​​9

0x3F+0x80, / / ​​0.

0x06+0x80, / / ​​1.

0x5B+0x80, / / ​​2.

0x4F+0x80, // 3.

0x66+0x80, / / ​​4.

0x6D+0x80, / / ​​5.

0x7D+0x80, / / ​​6.

0x07+0x80, / / ​​7.

0x7F+0x80, // 8.

0x6F+0x80// 9.

};

Unsigned int s,t,st,int_part,float_part,temp,SH;

/ / Send a byte of data to 595

Void send_595(unsigned char dat)

{

Unsigned char i;

LCK_0;

SDI_1;

SCK_0;

/ / The above three statements in order to initialize the port state

For(i=0;i<8;i++)

{

LCK_0; / / clock line pulled low

If(dat&0x80)

SDI_1;

Else SDI_0;

Dat=dat<<1;

Delay_us(100);

LCK_1; //clock line is pulled high to read data into the shift register of 595

Delay_us(100);

}

SCK_1; //Send data to the parallel port

SCK_0;

}

Void show(void)

{

Unsigned char temp_shi, temp_ge, SH_shi, SH_ge, x, y;

Unsigned int i;

St=dht();

t=st&0x0000ffff;

s=st&0xffff0000;

s=s>>16;

/ / The following is the conversion of temperature and humidity into decimal and rounded

Temp=(t>>8);

Temp_shi=temp/10;

Temp_ge=temp%10;

SH=(s>>8);

SH_shi=SH/10;

SH_ge=SH%10;

Int_part=SH_shi*10+SH_ge;

Float_part=0;

For(i=0;i<50;i++)

{

Digitron_show(int_part,float_part);

}

}

Void digitron_show(unsigned int int_part,unsigned int float_part)

{

PORTA=0x01;

Send_595(table[float_part/10]);

Send_595(0x00);

Delay_ms(5);

PORTA=0x02;

Send_595(table[(int_part%10)+10]);

Send_595(0x00);

Delay_ms(5);

PORTA=0x04;

Send_595(table[int_part/10]);

Send_595(0x00);

Delay_ms(5);

}

=========================================================

//This is MAIN.C

#include

#include

#include"delay.h"

#include"dht11.h"

#include"xianshi.h"

#pragma interrupt_handler Timer0_COMP:16

#define uchar unsigned char

Uchar k=0;

Void init(void);

Void main()

{

Init();//initialization

TCCR0=0X0F;

DDRA=0XFF;

TCCR0=0X0f;//CTC mode

OCR0=145; //10ms

TIMSK=0X02;

SEI();

While(1);

}

/ / Initialize the subfunction

Void init(void)

{

DDRA=0XFF;

DDRB=0XFF;

}

Void Timer0_COMP(void)

{

TCCR0=0X08;

CLI();

k++;

If(k==255)

{

k=0;

Show();

}

TCCR0=0X0f; / / reset initial value

SEI();

}

48v Ebike Battery Series

48V Ebike Battery,48V Ebike Battery Series,48V Rechargeable Battery,Ebike Litthium Battery

ZHEJIANG TIANHONG LITHIUM-ION BATTERY CO.,LTD , https://www.tflbattery.com