'######################## DHT-22 Humidity & Temperature Sensor #################### $regfile = "m8def.dat" $crystal = 8000000 $baud = 9600 $hwstack = 64 $swstack = 64 $framesize = 64 Config Lcdpin = Pin , Db4 = Portd.4 , Db5 = Portd.5 , Db6 = Portd.6 , Db7 = Portd.7 , E = Portd.2 , Rs = Portd.3 Config Lcd = 16 * 2 Cursor Off Cls '##################################### Config Pinc.2 = Input Config Timer0 = Timer , Prescale = 8 '##################################### 'this has to be changed according to your frequency settings in $crystal Const Min_time = 70 Dim Count As Byte dim timec as byte dim x as byte dim chksum as byte Dim Humidity1 As Word Dim Temperature1 As word Dim Humidity2 As Word Dim Temperature2 As word dim tneg as byte 'indicate if temperature<0 Dim Humsens_chksum As Byte Dim Humiditys As String * 16 Dim Temperatures As String * 16 Do gosub Read_sensor1 'takes 3 seconds locate 1,1 lcd Format(humiditys , "0.0") locate 2,1 lcd Format(temperatures , "0.0") gosub Read_sensor2 'takes 3 seconds locate 1,8 lcd Format(humiditys , "0.0") locate 2,8 lcd Format(temperatures , "0.0") Loop End Read_sensor1: Humidity1 = 0 Temperature1 = 0 Humsens_chksum = 0 Wait 3 Count = 1 Config Pinc.2 = Output : Portc.2 = 0 ' request data Waitms 20 ' wait 20 ms Config Pinc.2 = Input ' wait for data, receive data While Count < 43 ' collect 42 timings / signals Bitwait Pinc.2 , Set ' signal goes high > start timer Start Timer0 Bitwait Pinc.2 , Reset ' signal goes low > stop timer Stop Timer0 timec=tcnt0 Tcnt0 = 0 Select Case Count Case 3 To 18 X = 18 - Count If timec > Min_time Then Toggle Humidity1.x Case 19 To 34 X = 34 - Count If timec > Min_time Then Toggle Temperature1.x Case 34 To 42 X = 42 - Count If timec > Min_time Then Toggle Humsens_chksum.x 'locate 1,1 'lcd bin(humidityw);" "; bin(Temperaturew) End Select Incr Count Wend '############################################################# ' Calculate Checksum and compare with trasnmitted value '############################################################# Chksum = Low(humidity1) + High(humidity1) Chksum = Chksum + Low(temperature1) Chksum = Chksum + High(temperature1) If chksum=Humsens_chksum Then temperatures="" if temperature1.15=1 then temperature1.15=0 temperatures="-" endif Humiditys = Str(humidity1) Temperatures = Temperatures+ Str(temperature1) end if return Read_sensor2: Humidity2 = 0 Temperature2 = 0 Humsens_chksum = 0 Wait 3 Count = 1 ' Pin an Hardware hier anpassen! Config Pinc.3 = Output : Portc.3 = 0 ' request data Waitms 20 ' wait 20 ms Config Pinc.3 = Input ' wait for data, receive data While Count < 43 ' collect 42 timings / signals Bitwait Pinc.3 , Set ' signal goes high > start timer Start Timer0 Bitwait Pinc.3 , Reset ' signal goes low > stop timer Stop Timer0 timec=tcnt0 Tcnt0 = 0 Select Case Count Case 3 To 18 X = 18 - Count If timec > Min_time Then Toggle Humidity2.x Case 19 To 34 X = 34 - Count If timec > Min_time Then Toggle Temperature2.x Case 34 To 42 X = 42 - Count If timec > Min_time Then Toggle Humsens_chksum.x 'locate 1,1 'lcd bin(humidityw);" "; bin(Temperaturew) End Select Incr Count Wend '############################################################# ' Calculate Checksum and compare with trasnmitted value '############################################################# Chksum = Low(humidity2) + High(humidity2) Chksum = Chksum + Low(temperature2) Chksum = Chksum + High(temperature2) If chksum=Humsens_chksum Then temperatures="" if temperature2.15=1 then temperature2.15=0 temperatures="-" endif Humiditys = Str(humidity2) Temperatures = Temperatures+ Str(temperature2) end if return