DHT11温湿度传感器
DHT11.h
#ifndef __DHT11_H__
#define __DHT11_H__
#ifndef uchar
#define uchar unsigned char
#endif
#ifndef uint
#define uint unsigned int
#endif
#include <reg52.h>
#include <intrins.h>
sbit OUT = P2^7;
void RH( uchar *s );
void Delay(uint x);
extern void Uart_send_str(char *str, int len);
extern void Uart1Init();
#endif
DHT11.c
#include "DHT11.h"
uchar U8FLAG;
uchar U8count, U8temp;
uchar U8T_data_H_temp, U8T_data_L_temp, U8RH_data_H_temp, U8RH_data_L_temp, U8checkdata_temp;
uchar U8comdata;
uchar count = 0, count_r = 0;
uint U16temp1, U16temp2;
void Delay(uint x)
{
uchar i;
for( ; x > 0; x --)
{
for(i = 0;i < 29; i ++);
}
}
void Delay_10us()
{
uchar i;
i = 25;
while( -- i );
}
void COM()
{
uchar i;
for(i = 0; i < 8; i ++)
{
U8FLAG = 2;
while((!OUT) && U8FLAG ++ );
Delay_10us();
Delay_10us();
Delay_10us();
U8temp = 0;
if( OUT )
{
U8temp = 1;
}
U8FLAG = 2;
while( ( OUT ) && U8FLAG ++);
if( U8FLAG == 1)
{
break;
}
U8comdata <<= 1;
U8comdata |= U8temp;
}
}
void RH( uchar *s )
{
OUT = 0;
Delay(900);
OUT = 1;
Delay(2);
OUT = 1;
if( ! OUT )
{
U8FLAG = 2;
while( ( !OUT ) && U8FLAG ++ );
U8FLAG = 2;
while( OUT && U8FLAG ++ );
COM();
U8RH_data_H_temp = U8comdata;
COM();
U8RH_data_L_temp = U8comdata;
COM();
U8T_data_H_temp = U8comdata;
COM();
U8T_data_L_temp = U8comdata;
COM();
U8checkdata_temp = U8comdata;
OUT = 1;
U8temp =( U8T_data_H_temp + U8T_data_L_temp + U8RH_data_H_temp + U8RH_data_L_temp );
if( U8temp == U8checkdata_temp )
{
s[0] = U8RH_data_H_temp;
s[1] = U8RH_data_L_temp;
s[2] = U8T_data_H_temp;
s[3] = U8T_data_L_temp;
s[4] = U8checkdata_temp;
}
}
}
uart.c
#include <reg52.h>
#define F_f 11059200UL
#define Baud_rate 9600UL
sfr AUXR = 0x8E;
sfr BRT = 0x9C;
void Uart_send_str(unsigned char *str, int len)
{
int i;
for(i = 0; i < len; i ++ )
{
SBUF = str[i];
while( !TI );
TI = 0;
}
}
void Uart1Init()
{
PCON &= 0x7F;
SCON = 0x50;
AUXR &= 0xFB;
BRT = 256 - F_f / (12 * Baud_rate * 32);
AUXR |= 0x11;
EA = 1;
ES = 1;
}
void Uart1Routine() interrupt 4
{
unsigned char uart_sbuf;
if(RI)
{
uart_sbuf = SBUF;
if(uart_sbuf == 0xaa )
{
Uart_send_str("ok", 2);
}
RI = 0;
}
}
LED_show.c
#include <reg52.h>
#include <stdio.h>
#include "DHT11.h"
sfr AUXR = 0x8e;
sbit ASEG = P1^2;
sbit BSEG = P1^3;
sbit CSEG = P1^4;
sbit DSEG = P1^5;
uchar code smgduan[]=
{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
uchar showdata[4] = { 0 };
uchar outdata[6] = { "RS232" };
uchar out[30] = { 0 };
void seg4show( uchar i )
{
switch( i )
{
case 0:
ASEG = 0; BSEG = 1; CSEG = 1; DSEG = 1; break;
case 1:
ASEG = 1; BSEG = 0; CSEG = 1; DSEG = 1; break;
case 2:
ASEG = 1; BSEG = 1; CSEG = 0; DSEG = 1; break;
case 3:
ASEG = 1; BSEG = 1; CSEG = 1; DSEG = 0; break;
default:
ASEG = 1; BSEG = 1; CSEG = 1; DSEG = 1; break;
}
P0 = showdata[i];
}
void dataProcess()
{
showdata[0] = smgduan[ outdata[0] / 10 ];
showdata[1] = smgduan[ outdata[0] % 10 ];
showdata[2] = smgduan[ outdata[2] / 10 ];
showdata[3] = smgduan[ outdata[2] % 10 ];
}
void timer0_init()
{
AUXR &= 0x7f;
TMOD |= 0x02;
TH0 = 0x48;
TL0 = 0x48;
EA = 1;
ET0 = 1;
TF0 = 0;
TR0 = 1;
}
void main()
{
Uart1Init();
timer0_init();
Delay(5000);
while(1)
{
RH(outdata);
dataProcess();
sprintf(out, "湿度:%d%%, 温度:%d℃\n", (int)(outdata[0]), (int)(outdata[2]));
Delay(50000);
Delay(50000);
}
}
void timer0_Routine() interrupt 1
{
static uchar i;
seg4show( i / 5 );
i ++;
if( i % 10 == 0 )
{
P0 = 0x00;
}
i = i % 20;
}