DHT11 سنسور رطوبت و دما
کمکو کنید چطور DHT11 سنسور رطوبت و دما رو با avr راه اندازی کنم...
کمکو کنید چطور DHT11 سنسور رطوبت و دما رو با avr راه اندازی کنم...
$regfile = "m8def.dat" $crystal = 8000000 Config Lcd = 20 * 4 Config Lcdpin = Pin , Rs = Portc.5 , E = Portc.4 , Db4 = Portc.3 , Db5 = Portc.2 , Db6 = Portc.1 , Db7 = Portc.0 Cursor Off Cls Lcd "be name KHODA" Waitms 1000 Cls Declare Sub Get_th(t As Byte , H As Byte) Config Serialin = Buffered , Size = 128 Config Serialout = Buffered , Size = 128 Dht_put Alias Portd.6 Dht_get Alias Pind.6 Dht_io_set Alias Ddrd.6 Dim T As Byte Dim H As Byte Dim Crc As Byte Dim Mybyte As Byte Dim Sensor_data As String * 40 Dim Tmp_str8 As String * 8 Dim Count As Byte Enable Interrupts Set Dht_io_set Set Dht_put Cls Do Waitms 200 Call Get_th(t , H) Home Lcd "TMP: " ; T ; "C " Lowerline Lcd "HDT: " ; H ; "% " Loop Sub Get_th(t As Byte , H As Byte) Count = 0 Sensor_data = "" Set Dht_io_set Reset Dht_put Waitms 30 Set Dht_put Waitus 50 Reset Dht_io_set Waitus 40 If Dht_get = 1 Then H = 1 Exit Sub End If Waitus 80 If Dht_get = 0 Then H = 2 Exit Sub End If While Dht_get = 1 : Wend Do While Dht_get = 0 : Wend Waitus 30 If Dht_get = 1 Then Sensor_data = Sensor_data + "1" While Dht_get = 1 : Wend Else Sensor_data = Sensor_data + "0" End If Incr Count Loop Until Count = 40 Set Dht_io_set Set Dht_put Tmp_str8 = Left(sensor_data , 8) H = Binval(tmp_str8) Tmp_str8 = Mid(sensor_data , 17 , 8) T = Binval(tmp_str8) Tmp_str8 = Right(sensor_data , 8) Crc = Binval(tmp_str8) Mybyte = T + H If Mybyte <> Crc Then H = 3 End If End Sub
#define DHT22_PIN PIND.5 #define DHT22_DDR DDRD.5 #define DHT22_PORT PORTD.5 #define DHT22_INPUT_MODE DHT22_DDR=0; #define DHT22_OUTPUT_MODE DHT22_DDR=1; #define DHT22_LOW DHT22_PORT=0; #define DHT22_HIGH DHT22_PORT=1; #define DHT22_READ DHT22_PIN //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #define DHT_BUS_HUNG 0 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ int dht22_read(float *temperature, float *humidity); //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // This should be 40, but the sensor is adding an extra bit at the start #define DHT22_DATA_BIT_COUNT 41 // // Read the 40 bit data stream from the DHT 22 // Store the results in private member data to be read by public member functions // int dht22_read(float *temperature, float *humidity) { // uint8_t bitmask = _bitmask; //volatile uint8_t *reg asm("r30") = _baseReg; int Count, i; unsigned char bitTimes[DHT22_DATA_BIT_COUNT]; int Humidity; int Temperature; unsigned char checkSum, csPart1, csPart2, csPart3, csPart4; Humidity = 0; Temperature = 0; checkSum = 0; //return currentTime; for(i = 0; i < DHT22_DATA_BIT_COUNT; i++) { bitTimes[i] = 0; } // Pin needs to start HIGH, wait until it is HIGH with a timeout //cli(); DHT22_INPUT_MODE //sei(); Count = 0; do { if(Count > 125) { return DHT_BUS_HUNG; } Count++; delay_us(2); }while(!DHT22_READ); // Send the activate pulse //cli(); DHT22_LOW DHT22_OUTPUT_MODE // Output Low //sei(); delay_us(1100); // 1.1 ms //cli(); DHT22_INPUT_MODE // Switch back to input so pin can float //sei(); // Find the start of the ACK Pulse Count = 0; do { if(Count > 25) //(Spec is 20 to 40 us, 25*2 == 50 us) { return DHT_BUS_HUNG; } Count++; delay_us(2); } while(!DHT22_READ); // Find the end of the ACK Pulse Count = 0; do { if(Count > 50) //(Spec is 80 us, 50*2 == 100 us) { return DHT_BUS_HUNG; } Count++; delay_us(2); }while(DHT22_READ); // Read the 40 bit data stream for(i = 0; i < DHT22_DATA_BIT_COUNT; i++) { // Find the start of the sync pulse Count = 0; do { if(Count > 35) //(Spec is 50 us, 35*2 == 70 us) { return DHT_BUS_HUNG; } Count++; delay_us(2); } while(!DHT22_READ); // Measure the width of the data pulse Count = 0; do { if(Count > 50) //(Spec is 80 us, 50*2 == 100 us) { return DHT_BUS_HUNG; } Count++; delay_us(2); }while(DHT22_READ); bitTimes[i] = Count; } // Now bitTimes have the number of retries (us *2) // that were needed to find the end of each data bit // Spec: 0 is 26 to 28 us // Spec: 1 is 70 us // bitTimes[x] <= 11 is a 0 // bitTimes[x] > 11 is a 1 // Note: the bits are offset by one from the data sheet, not sure why for(i = 0; i < 16; i++) { if(bitTimes[i + 1] > 11) { Humidity |= (1 << (15 - i)); } } for(i = 0; i < 16; i++) { if(bitTimes[i + 17] > 11) { Temperature |= (1 << (15 - i)); } } for(i = 0; i < 8; i++) { if(bitTimes[i + 33] > 11) { checkSum |= (1 << (7 - i)); } } *humidity = ((float)(Humidity & 0x7FFF)) / 10.0; if(Temperature & 0x8000) { // Below zero, non standard way of encoding negative numbers! Temperature &= 0x7FFF; *temperature = ((float)Temperature / 10.0) * -1.0; } else { *temperature = (float)Temperature / 10.0; } csPart1 = Humidity >> 8; csPart2 = Humidity & 0xFF; csPart3 = Temperature >> 8; csPart4 = Temperature & 0xFF; if(checkSum == ((csPart1 + csPart2 + csPart3 + csPart4) & 0xFF)) { //*humidity = 10; //return DHT_ERROR_NONE; return 1; } //*humidity = 20; //return DHT_ERROR_CHECKSUM; //*temperature = -100; //*humidity = -100; return 0; }
#define DHT22_PIN PIND.5 #define DHT22_DDR DDRD.5 #define DHT22_PORT PORTD.5 #define DHT22_INPUT_MODE DHT22_DDR=0; #define DHT22_OUTPUT_MODE DHT22_DDR=1; #define DHT22_LOW DHT22_PORT=0; #define DHT22_HIGH DHT22_PORT=1; #define DHT22_READ DHT22_PIN //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #define DHT_BUS_HUNG 0 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ int dht22_read(float *temperature, float *humidity); //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // This should be 40, but the sensor is adding an extra bit at the start #define DHT22_DATA_BIT_COUNT 41 // // Read the 40 bit data stream from the DHT 22 // Store the results in private member data to be read by public member functions // int dht22_read(float *temperature, float *humidity) { // uint8_t bitmask = _bitmask; //volatile uint8_t *reg asm("r30") = _baseReg; int Count, i; unsigned char bitTimes[DHT22_DATA_BIT_COUNT]; int Humidity; int Temperature; unsigned char checkSum, csPart1, csPart2, csPart3, csPart4; Humidity = 0; Temperature = 0; checkSum = 0; //return currentTime; for(i = 0; i < DHT22_DATA_BIT_COUNT; i++) { bitTimes[i] = 0; } // Pin needs to start HIGH, wait until it is HIGH with a timeout //cli(); DHT22_INPUT_MODE //sei(); Count = 0; do { if(Count > 125) { return DHT_BUS_HUNG; } Count++; delay_us(2); }while(!DHT22_READ); // Send the activate pulse //cli(); DHT22_LOW DHT22_OUTPUT_MODE // Output Low //sei(); delay_us(1100); // 1.1 ms //cli(); DHT22_INPUT_MODE // Switch back to input so pin can float //sei(); // Find the start of the ACK Pulse Count = 0; do { if(Count > 25) //(Spec is 20 to 40 us, 25*2 == 50 us) { return DHT_BUS_HUNG; } Count++; delay_us(2); } while(!DHT22_READ); // Find the end of the ACK Pulse Count = 0; do { if(Count > 50) //(Spec is 80 us, 50*2 == 100 us) { return DHT_BUS_HUNG; } Count++; delay_us(2); }while(DHT22_READ); // Read the 40 bit data stream for(i = 0; i < DHT22_DATA_BIT_COUNT; i++) { // Find the start of the sync pulse Count = 0; do { if(Count > 35) //(Spec is 50 us, 35*2 == 70 us) { return DHT_BUS_HUNG; } Count++; delay_us(2); } while(!DHT22_READ); // Measure the width of the data pulse Count = 0; do { if(Count > 50) //(Spec is 80 us, 50*2 == 100 us) { return DHT_BUS_HUNG; } Count++; delay_us(2); }while(DHT22_READ); bitTimes[i] = Count; } // Now bitTimes have the number of retries (us *2) // that were needed to find the end of each data bit // Spec: 0 is 26 to 28 us // Spec: 1 is 70 us // bitTimes[x] <= 11 is a 0 // bitTimes[x] > 11 is a 1 // Note: the bits are offset by one from the data sheet, not sure why for(i = 0; i < 16; i++) { if(bitTimes[i + 1] > 11) { Humidity |= (1 << (15 - i)); } } for(i = 0; i < 16; i++) { if(bitTimes[i + 17] > 11) { Temperature |= (1 << (15 - i)); } } for(i = 0; i < 8; i++) { if(bitTimes[i + 33] > 11) { checkSum |= (1 << (7 - i)); } } *humidity = ((float)(Humidity & 0x7FFF)) / 10.0; if(Temperature & 0x8000) { // Below zero, non standard way of encoding negative numbers! Temperature &= 0x7FFF; *temperature = ((float)Temperature / 10.0) * -1.0; } else { *temperature = (float)Temperature / 10.0; } csPart1 = Humidity >> 8; csPart2 = Humidity & 0xFF; csPart3 = Temperature >> 8; csPart4 = Temperature & 0xFF; if(checkSum == ((csPart1 + csPart2 + csPart3 + csPart4) & 0xFF)) { //*humidity = 10; //return DHT_ERROR_NONE; return 1; } //*humidity = 20; //return DHT_ERROR_CHECKSUM; //*temperature = -100; //*humidity = -100; return 0; }
#define DHT22_PIN PIND.5 #define DHT22_DDR DDRD.5 #define DHT22_PORT PORTD.5 #define DHT22_INPUT_MODE DHT22_DDR=0; #define DHT22_OUTPUT_MODE DHT22_DDR=1; #define DHT22_LOW DHT22_PORT=0; #define DHT22_HIGH DHT22_PORT=1; #define DHT22_READ DHT22_PIN //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #define DHT_BUS_HUNG 0 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ int dht22_read(float *temperature, float *humidity); //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // This should be 40, but the sensor is adding an extra bit at the start #define DHT22_DATA_BIT_COUNT 41 // // Read the 40 bit data stream from the DHT 22 // Store the results in private member data to be read by public member functions // int dht22_read(float *temperature, float *humidity) { // uint8_t bitmask = _bitmask; //volatile uint8_t *reg asm("r30") = _baseReg; int Count, i; unsigned char bitTimes[DHT22_DATA_BIT_COUNT]; int Humidity; int Temperature; unsigned char checkSum, csPart1, csPart2, csPart3, csPart4; Humidity = 0; Temperature = 0; checkSum = 0; //return currentTime; for(i = 0; i < DHT22_DATA_BIT_COUNT; i++) { bitTimes[i] = 0; } // Pin needs to start HIGH, wait until it is HIGH with a timeout //cli(); DHT22_INPUT_MODE //sei(); Count = 0; do { if(Count > 125) { return DHT_BUS_HUNG; } Count++; delay_us(2); }while(!DHT22_READ); // Send the activate pulse //cli(); DHT22_LOW DHT22_OUTPUT_MODE // Output Low //sei(); delay_us(1100); // 1.1 ms //cli(); DHT22_INPUT_MODE // Switch back to input so pin can float //sei(); // Find the start of the ACK Pulse Count = 0; do { if(Count > 25) //(Spec is 20 to 40 us, 25*2 == 50 us) { return DHT_BUS_HUNG; } Count++; delay_us(2); } while(!DHT22_READ); // Find the end of the ACK Pulse Count = 0; do { if(Count > 50) //(Spec is 80 us, 50*2 == 100 us) { return DHT_BUS_HUNG; } Count++; delay_us(2); }while(DHT22_READ); // Read the 40 bit data stream for(i = 0; i < DHT22_DATA_BIT_COUNT; i++) { // Find the start of the sync pulse Count = 0; do { if(Count > 35) //(Spec is 50 us, 35*2 == 70 us) { return DHT_BUS_HUNG; } Count++; delay_us(2); } while(!DHT22_READ); // Measure the width of the data pulse Count = 0; do { if(Count > 50) //(Spec is 80 us, 50*2 == 100 us) { return DHT_BUS_HUNG; } Count++; delay_us(2); }while(DHT22_READ); bitTimes[i] = Count; } // Now bitTimes have the number of retries (us *2) // that were needed to find the end of each data bit // Spec: 0 is 26 to 28 us // Spec: 1 is 70 us // bitTimes[x] <= 11 is a 0 // bitTimes[x] > 11 is a 1 // Note: the bits are offset by one from the data sheet, not sure why for(i = 0; i < 16; i++) { if(bitTimes[i + 1] > 11) { Humidity |= (1 << (15 - i)); } } for(i = 0; i < 16; i++) { if(bitTimes[i + 17] > 11) { Temperature |= (1 << (15 - i)); } } for(i = 0; i < 8; i++) { if(bitTimes[i + 33] > 11) { checkSum |= (1 << (7 - i)); } } *humidity = ((float)(Humidity & 0x7FFF)) / 10.0; if(Temperature & 0x8000) { // Below zero, non standard way of encoding negative numbers! Temperature &= 0x7FFF; *temperature = ((float)Temperature / 10.0) * -1.0; } else { *temperature = (float)Temperature / 10.0; } csPart1 = Humidity >> 8; csPart2 = Humidity & 0xFF; csPart3 = Temperature >> 8; csPart4 = Temperature & 0xFF; if(checkSum == ((csPart1 + csPart2 + csPart3 + csPart4) & 0xFF) && status.humi>0) { //*humidity = 10; //return DHT_ERROR_NONE; return 1; } //*humidity = 20; //return DHT_ERROR_CHECKSUM; //*temperature = -100; //*humidity = -100; return 0; }
دیدگاه