Noticed I made a small mistake on using the correct variable (used j instead of k) during the Humidity/Temperature Pulsewidth-to-Bit value conversions.
Code:
dht_data var PORTA.1
dht var byte[40] 'Holds pulsewidth counts of sensor data
fHum var byte 'Final humidity value integral and decimal place
fTemp var byte 'Final temperature value, integral and decimal place degC
checksum var byte 'Checksum of payload data from sensor
chkError var bit 'Flag for checksum error
j var byte 'Generic counter
k var byte 'Generic counter
chkError = 0
main:
gosub read_dht
if !chkError then
'Print out sensor values
LCDOut $FE,$80,"T ",dec fTemp/10, "." dec fTemp //10
LCDOut $FE,$80+9,"RH ",dec fHum/10, "." dec fHum //10
else
'Print out checksum error
endif
pause 2000
goto main
read_dht:
'*** NOTE: You should disable interrputs during this subroutine.
'*** Sensor communications are timing sensitive
TRISA.1 = 0 ' Make the sensor data pin an input
'**Intialization sequence to sensor**
high dht_data '250ms High on data pin to sensor
pause 250
low dht_data '20ms Low on data pin to sensor
pause 20 ' send 20ms low
high dht_data '40us High
pauseus 40
'Read the sensor's acknowledgment of init sequence
PulsIn PORTA.1, 1, dht[0] 'Read a High pulse from the sensor data line
if dht < 20 then goto badresponse 'Not a good signal, maybe noise? Changed this to < 20
'**End of Initialization sequence"
'**Load the data from the sensor into the dht byte array
for j = 0 to 39 step 1 'Record the bits in correct order (not reverse order)
PulsIn PORTA.1, 1, dht[j] 'Read data High pulses (bits) from the sensor and store them as pulsewidth counts in individual bytes in the dht byte array.
next j
'**End of Load Data**
'**Convert the Humidity dht byte array to a 16bit word variable
'Stuff values into hum word variable based on the pulsewidth value in dht byte array
'Use "fHum.0(j)" to select a specific bit position in the fHum word variable.
'0.[j] is treated as an bit-offset into the hum variable. E.g. 0.[0] = fHum.0, 0.[1] = fHum.0+1, 0.[2] = fHum.0+2, etc
fHum = 0 'Clear fHum word variable which holds the final 16bit humidity value
k = 0 'Used to select the correct bit in the fHum word variable
for j = 0 to 15 step 1 'Read the 16 bytes for humidity in the dht byte array (humidity pulsewidth values)
if dht(j)>=35 then fHum.0[k] = 1 'If the pulsewidth is => 35us then the value is a "1".
'otherwise, the value is a "0", so we do not need to do anything, all bits in "hum" were initialized to "0" with "hum = 0"
k = k + 1
next j
'**End Conversion**
'**Convert the Temperature dht byte array to a 16bit word variable
fTemp = 0 'Clear fTemp word variable which holds the final 16bit temperature value
k = 16 'Used to select the correct bit in the fTemp word variable
for j = 16 to 31 step 1 'Read the 16 bytes for temperature in the dht byte array (temperature pulsewidth values)
if dht(j)>=35 then fTemp.0[k] = 1 'If the pulsewidth is => 35us then the value is a "1".
'otherwise, the value is a "0", so we do not need to do anything, all bits in "hum" were initialized to "0" with "hum = 0"
k = k + 1
next j
'**End Conversion**
'**Convert the Checksum dht byte array to a 16bit word variable
checksum = 0 'Clear Checksum word variable which holds the final 8bit checksum value
k = 32 'Used to select the correct bit in the fTemp word variable
for j = 32 to 39 step 1 'Read the 8 bytes for checksum in the dht byte array (checksum pulsewidth values)
if dht(j)>=35 then checksum.0[k] = 1 'If the pulsewidth is => 35us then the value is a "1".
'otherwise, the value is a "0", so we do not need to do anything, all bits in "hum" were initialized to "0" with "hum = 0"
k = k + 1
next j
'**End Conversion**
'Test the checksum
chkError = 0 'Reset checksum error flag
if checksum <> fHum + fTemp then
chkError = 1
endif
return
badresponse:
LCDOut $FE,$80+9,"RH N/C"
return
Bookmarks