انا افضل dht22 على dht11
و هو موضح في الصورة
بالنسبة للdht11 يمكنك الاعتماد على الموضوع الاخ zoro39
http://www.qariya.info/vb/showthread.php?t=174639
هذا كود ميكورسي للdht22
كود:
// LCD module connections
sbit LCD_RS at LATD2_bit;
sbit LCD_EN at LATD3_bit;
sbit LCD_D4 at LATD4_bit;
sbit LCD_D5 at LATD5_bit;
sbit LCD_D6 at LATD6_bit;
sbit LCD_D7 at LATD7_bit;
sbit LCD_RS_Direction at TRISD2_bit;
sbit LCD_EN_Direction at TRISD3_bit;
sbit LCD_D4_Direction at TRISD4_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D7_Direction at TRISD7_bit;
// End LCD module connections
// AM2302 module connections
sbit AM2302_Bus_In at Ra0_bit;
sbit AM2302_Bus_Out at Ra0_bit;
sbit AM2302_Bus_Direction at TRISA0_bit;
// END AM2302 module connections
char AM2302_Data[5] = {0, 0, 0, 0, 0};
unsigned humidity = 0, temperature = 0;
//Get Sensor values
char AM2302_Read(unsigned *humidity, unsigned *temperature) {
char i = 0, j = 1;
char timeout = 0;
char sensor_byte;
AM2302_Bus_Out = 0;
delay_ms(100);
AM2302_Bus_Direction = 1; //Set AM2302_Bus as input
AM2302_Bus_Direction = 0; //Set AM2302_Bus as output
AM2302_Bus_Out = 1; //Set AM2302_Bus HIGH for 100 milliseconds
Delay_ms(100); //Delay 100ms
AM2302_Bus_Out = 0; //Host the start signal down time min: 0.8ms, typ: 1ms, max: 20ms
Delay_ms(2); //Delay 2ms
AM2302_Bus_Out = 1; //Set AM2302_Bus HIGH
AM2302_Bus_Direction = 1; //Set AM2302_Bus as input
// Bus master has released time min: 20us, typ: 30us, max: 200us
timeout = 200;
while (AM2302_Bus_In == 1) {
Delay_us(1);
if (!timeout--) {
return 1;
} //ERROR: Sensor not responding
}
// AM2302 response signal min: 75us, typ: 80us, max: 85us
while (!AM2302_Bus_In) { //response to low time
Delay_us(85);
}
while (AM2302_Bus_In) { //response to high time
Delay_us(55);
}
/*
* time in us: min typ max
* signal 0 high time: 22 26 30 (bit=0)
* signal 1 high time: 68 70 75 (bit=1)
* signal 0,1 down time: 48 50 55
*/
i = 0; //get 5 byte
for (i = 0; i < 5; i++) {
j = 1;
for (j = 1; j <= 8; j++) { //get 8 bits from sensor
while (!AM2302_Bus_In) { //signal "0", "1" low time
Delay_us(1);
}
Delay_us(30);
sensor_byte <<= 1; //add new lower byte
if (AM2302_Bus_In) { //if sda high after 30us => bit=1 else bit=0
sensor_byte |= 1;
delay_us(45);
while (AM2302_Bus_In) {
Delay_us(1);
}
}
}
AM2302_Data[i] = sensor_byte;
}
*humidity = (AM2302_Data[0] << 8) + AM2302_Data[1];
*temperature = (AM2302_Data[2] << 8) + AM2302_Data[3];
return 0;
}
// Process and display current value to LCD
void processValue(unsigned humidity, unsigned temperature) {
char txt[15];
float temp;
LCD_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
temp = humidity / 10.0;
Lcd_Out(1,1,"Humid.:");
sprintf(txt, "%2.1f", temp); // Format Relative_humidity_val and store it to txt
Lcd_Out(1,9,txt);
Lcd_Chr(1,14,0x25);
temp = temperature / 10.0;
Lcd_Out(2,1,"Temp. :");
sprintf(txt, "%2.1f", temp); // Format Temperature and store it to txt
Lcd_Out(2,9,txt);
Lcd_Chr(2,13,0xB2);
Lcd_Chr(2,14,0x43);
}
void Display_Init(){
LCD_Init();
LCD_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
// Display string on the LCD
Lcd_Out(1,3,"DHT22 click");
Lcd_Out(2,3,"Demo Example");
delay_ms(2000);
}
void main() {
char k = 0, t;
ADCON1=0x0F;
CMCON=0x07;
Display_Init();
while (1) {
if (AM2302_Read(&humidity, &temperature) == 0) // Display AM2302_Read sensor values via LCD
processValue(humidity, temperature); // Display AM2302_Read sensor values via LCD
else {
LCD_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,3,"Sensor not");
Lcd_Out(2,3,"responding");
}
Delay_ms(2000); //Delay 2000ms
}
}
كان الله في عونك بتوفيق والقادم أحسن