Home > other >  For help, STC89C52RC chip dht11 data is wrong, please look at the trouble is what problem
For help, STC89C52RC chip dht11 data is wrong, please look at the trouble is what problem

Time:04-12

Dht11 parts:
# include "dht11. H"

/* * * *
* * * * * * * DHT11 delay function
* * * * *
Void delay_dht11 (uint I)
{
While (I -);
}

/* * * *
* * * * * * * DHT11 delay 1 ms function
* * * * *
Void delay_ms_dht11 (uint I)
{
While (I -)
Delay_dht11 (90);
}

/* * * *
* * * * * * * DHT11 reset function
* * * * *
Void DHT11_Rst (void)
{
DHT11_DQ_OUT=0;//lower DQ
Delay_ms_dht11 (20);//lower at least 18 ms
DHT11_DQ_OUT=1;//DQ=1
Delay_dht11 (3);//host up 20 ~ 40 us
}

/* * * *
* * * * * * * wait DHT11 response function
* * * * * * * returns 1: not detected the presence of DHT11
* * * * * * * returns 0: there
* * * * *
Uchar DHT11_Check (void)
{
Uchar retry=0;
While (DHT11_DQ_OUT & amp; & Retry<100)//DHT11 will lower 40 ~ 50 us
{
Retry++;
_nop_();
}
If (retry>
=100)return 1;
The else
Retry=0;
while(! DHT11_DQ_OUT & amp; & Retry{
Retry++;
_nop_();
}
If (retry>
=100)return 1;

return 0;
}

/* * * *
* * * * * * * DHT11 initialization function
* * * * * * * returns 1: failed to initialize
* * * * * * * returns 0: initial success
* * * * *
Uchar DHT11_Init (void)
{
DHT11_Rst ();
Return DHT11_Check ();
}

/* * * *
* * * * * * * read a bit from DHT11 function
* * * * * * * return values: 1/0
* * * * *
Uchar DHT11_Read_Bit (void)
{
Uchar retry=0;
While (DHT11_DQ_OUT & amp; & Retry<100)//wait to low level 12 to 14 us start
{
Retry++;
_nop_();
}
Retry=0;
While ((! DHT11_DQ_OUT) & amp; & Retry<100)//wait for high level 26-28 us said us said 1, 0116-118
{
Retry++;
_nop_();
}
Delay_dht11 (1);//wait 40 us
If (DHT11_DQ_OUT)
return 1;
The else
return 0;
}

/* * * *
* * * * * * * read a byte from DHT11 function
* * * * * * * return values: read data
* * * * *
Uchar DHT11_Read_Byte (void)
{
Uchar I, dat=0;
for (i=0; i<8; I++)
{
Dat<=1;
Dat |=DHT11_Read_Bit ();
}
Return dat.
}

/* * * *
* * * * * * * a data read from the DHT11 function
* * * * * * * parameter definition: temp: temperature (range: 0 ~ 50)
* * * * * * * humi: humidity value (range: 20% ~ 95%)
* * * * * * * return values: 0: normal; 1: read failure
* * * * *
Uchar Dht11_Get_Temp_Humi_Value (uint * temp, uint * humi)
{
Uchar buf [5].
uchar i;
DHT11_Rst ();
If (DHT11_Check ()==0)
{
for(i=0; i<5; I++)//read 40 data
{
Buf [I]=DHT11_Read_Byte ();
}
If ((buf buf [0] + [1] + buf + buf [2] [3])==buf [4])
{
* humi=buf [0] * 10 + buf [1].
* temp=buf [2] * 10 + buf [3].
}
}
The else
return 1;

return 0;
}
The main part of the code:
# include "is the main. H"
# include "lcd1602. H"
H # include "is the key."
# include "motor_zhiliu. H"
# include "ds1302. H"
# include "adc. H"
# include "dht11. H"
# include "uart. H"

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Variable definitions
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
Uchar key_num=0;//button marks a
Uchar time_num=0;//delay flag bit
Uchar flag_light=0;
Bit flag_person_cont=0;//the number of total
Bit flag_per_add=0;//judge increase
Bit flag_per_cut=0;//judge fewer
Bit flag_display_obj=0;//display flags
Uint temp, humi, ch4_value light_value;//temperature, humidity, combustible gas, light intensity parameter
Extern uchar light, uart_num;//inheritance brightness parameter
Uchar send_buf [16].//send packets
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Function declaration
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
Void Delay_function (uint x);//delay function
Void Key_function (void);//button function
Void Monitor_function (void);//monitoring function
Void Display_function (void);//display function
Void Manage_function (void);//handler

/* * * *
* * * * * * * the main function
* * * * *
Void main ()
{
While (DHT11_Init ());//DHT11 initialization
Delay_function (10);//delay 10 ms
Lcd1602_Init ();//LCD1602 initialization
Delay_function (10);//delay 10 ms
Lcd1602_clean ();//clear screen
Delay_function (10);//delay 10 ms
Ds1302_Init ();//time chip initialization
Delay_function (10);//delay 10 ms
Init_timer ();//serial port initialization
Uart_Init ();//timer initialization

IR_per=1;
//ds1302_write_time ();//to write clock DS302 data, only the burn in the first time open
While (1)
{
Key_function ();//button function
Monitor_function ();//monitoring function
Display_function ();//display function
Manage_function ();//handler

Delay_function (1);//delay 10 ms
Time_num + +;//timing variable + 1
If (time_num & gt; 100)
{
Sprintf (send_buf, "temperature: % % 1 f", temp/10.0);//send the temperature data using floating-point
Uart_Sent_Str (send_buf);
Sprintf (send_buf, "humidity: % % % 1 f", humi/10.0);//send the humidity data using floating-point
Uart_Sent_Str (send_buf);
Time_num=0;
}
}
}
  • Related