DHT11数据在串口助手显示
#include "reg52.h"
#include "intrins.h"
sbit led1 = P3^6;
sbit dht = P3^3;
sbit AUXR = 0x8E;
char datas[5];
void Delay1000ms(void) //@11.0592MHz
{
unsigned char data i, j, k;
_nop_();
i = 8;
j = 1;
k = 243;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void Delay30ms(void) //@11.0592MHz
{
unsigned char data i, j;
i = 54;
j = 199;
do
{
while (--j);
} while (--i);
}
void Delay40us(void) //@11.0592MHz
{
unsigned char data i;
_nop_();
i = 15;
while (--i);
}
void UartInit(void)
{
SCON = 0x40;
AUXR = 0x01;
TMOD &= 0x0F;
TMOD |= 0x20;
TH1 = 0xFD;
TH0 = 0xFD;
ET1 = 1;
TR1 = 1;
}
void sendByte(char data_msg)
{
SBUF = data_msg;
while(!TI);
TI = 0;
}
void sendString(char *data_msg)
{
while(*data_msg != '\0'){
sendByte(*data_msg);
data_msg++;
}
}
void DHT11_Start()
{
dht = 1;
dht = 0;
Delay30ms();
dht = 1;
while(dht);
while(!dht);
while(dht);
}
void DHT11_ReadData()
{
int i;
int j;
char flag;
char tmp;
DHT11_Start();
for(i=0;i<5;i++){
for(j=0;j<8;j++){
while(!dht);//卡g
Delay40us();
if(dht == 1){
flag = 1;
while(dht);
}else{
flag = 0;
}
tmp = tmp << 1;
tmp |= flag;
}
datas[i] = tmp;
}
}
int main()
{
UartInit();
Delay1000ms();
Delay1000ms();
while(1){
Delay1000ms();
DHT11_ReadData();
sendString("H:");
sendByte(datas[0]/10+0x30);
sendByte(datas[0]%10+0x30);
sendByte('.');
sendByte(datas[1]/10+0x30);
sendByte(datas[1]%10+0x30);
sendString("\r\n");
sendString("T:");
sendByte(datas[2]/10+0x30);
sendByte(datas[2]%10+0x30);
sendByte('.');
sendByte(datas[3]/10+0x30);
sendByte(datas[3]%10+0x30);
sendString("\r\n");
}
}