if you don't have a oscilloscope or logic analyser add some more diagnostics to the program
Code:
; config settings 18F4580, 10mhz crystal HSPPL
ASM
__CONFIG _CONFIG1H, _OSC_HSPLL_1H
__CONFIG _CONFIG2L, _PWRT_ON_2L
__CONFIG _CONFIG2H, _WDT_ON_2H & _WDTPS_512_2H
__CONFIG _CONFIG3H, _PBADEN_OFF_3H
__CONFIG _CONFIG4L, _LVP_OFF_4L & _XINST_OFF_4L
ENDASM
DEFINE OSC 40
ADCON1 = $0F
clear
INCLUDE "alldigital.pbp"
CMCON = 7
TRISA = %11001111
DEFINE LCD_DREG PORTB ' LCD Data port
DEFINE LCD_DBIT 0 ' starting Data bit (0 or 4)
DEFINE LCD_EREG PORTB ' LCD Enable port
DEFINE LCD_EBIT 5 ' Enable bit (on EasyPIC 5 LCD)
DEFINE LCD_RSREG PORTB ' LCD Register Select port
DEFINE LCD_RSBIT 4 ' Register Select bit (on EasyPIC 5 LCD)
DEFINE LCD_BITS 4 ' LCD bus size (4 or 8 bits)
DEFINE LCD_LINES 4 ' number of lines on LCD
DEFINE LCD_COMMANDUS 2000 ' Command delay time in us
DEFINE LCD_DATAUS 50 ' Data delay time in us
ASM
hum = _dht+3
tempa = _dht+1
ENDASM
T1CON = %00000001 ; free-running, 1:1 prescaler
dht_data var PORTA.1
dht_dir var TRISA.1
dht var byte[5]
tempa var word EXT
bits var byte
crc var byte
hum var word EXT
main:
gosub read_dht
LCDOut $FE,$94,"rh ",#hum/10,".",dec1 hum//10
pause 4000
goto main
read_dht:
for bits=4 to 0 step-1
dht[bits]=0
next
tmr2=0
dht_dir=0
dht_data=0
pauseus 1000 'start it up
dht_data=1
pauseus 30
dht_dir=1
pauseus 40 'wait till middle of response pulse window
if ( dht_data)then goto badresponce 'no response then give up
while (!dht_data)
wend
t2con=6
while ( dht_data)
wend
t2con=0
if ((tmr2 <45)||(tmr2>55))then goto badpresence ' confirm presence ?
for bits =39 to 0 step -1
tmr2=0
while (!dht_data)
wend
t2con=6
while ( dht_data)
wend
t2con=0
if ((tmr2>12)&&(tmr2<50)) then ' noise ?
if (tmr2 >31 ) then
dht.0[bits] = 1
endif
else
goto badread ' noise ?
endif
next
crc=0
for bits=1 to 4
crc=crc+ dht[bits]
next
if crc != dht[0] then goto badcrc 'crc
return
badread:
LCDOut $FE,$94 +8,"tmr2 ",#tmr2
next
return
badcrc:
for bits=4 to 0 step-1
LCDOut $FE,$94 +16,hex dht[bits],","
pause 5000
next
return
badresponse:
LCDOut $FE,$94 +8,"nr "
return
badpresence:
LCDOut $FE,$94 +8,"pr",#tmr2
return
Bookmarks