Hi! my friends!
I am starting a project for my father's home-made incubator egg machine.
Αs I had an DHT22 (ΑΜ2302) humidity sensor in-hand I tried to make a quick code to see if I can use it....
I studied carefully this datasheet http://dlnmh9ip6v2uc.cloudfront.net/...ther/RHT03.pdf
BUT I also found another datasheet elsewhere (I will upload the link as soon as I find the link) with slightly different timming values for starting pulse etc
Here is a rough description of connected circuit + the code I tried out
DHT-22 connected to PORTB.4 of 18F2320
I use NO external pull-up on pin2 (DATA) but pic PORTB PULL-UPS ,instead.
my code:
(see remarks inside the code)Code:'*************************************** '* Name : 18f2320_DHT22_AM2302.BAS * '* Author : BITmaniac * '* Notice : Copyright (c) 2013 * '* Date : 1/3/2013 * '*************************************** ;include "ALLDIGITAL.pbp" ;@ __CONFIG _CONFIG1H, _INTIO2_OSC_1H & _IESO_OFF_1H ' use internal osc @ __CONFIG _CONFIG1H, _XT_OSC_1H & _IESO_OFF_1H ' use internal osc @ __CONFIG _CONFIG2L, _PWRT_OFF_2L & _BOR_OFF_2L @ __CONFIG _CONFIG2H, _WDT_OFF_2H & _WDTPS_512_2H @ __CONFIG _CONFIG3H, _MCLRE_OFF_3H & _PBAD_DIG_3H ;& _CCP2MX_OFF_3H @ __CONFIG _CONFIG4L, _LVP_OFF_4L define osc 4 DEFINE LCD_DREG PORTC ' LCD Data Port DEFINE LCD_DBIT 4 ' Starting Data Bit DEFINE LCD_RSREG PORTC ' Register Select Port DEFINE LCD_RSBIT 2 ' Register Select Bit DEFINE LCD_RWREG PORTC ' LCD read/write port DEFINE LCD_RWBIT 1 ' LCD read/write bit DEFINE LCD_EREG PORTC ' Enable Port DEFINE LCD_EBIT 0 ' Enable Bit DEFINE LCD_BITS 4 ' Data Bus Size DEFINE LCD_LINES 2 ' Number of Lines on LCD DEFINE LCD_COMMANDUS 2000 DEFINE LCD_DATAUS 50 pause 1000 ADCON0=0 ADCON1=00001111 'All digital CMCON=7 'Comparators OFF INTCON2.7=0 'RBPU =0 , TURN ON PORTB PULL-UPS TRISA=%00000000 TRISB=%00001111 PORTA=0 PORTB=0 humread var word '16-bit var to store humidity tmpread var word '16-bit var to store temperature checksum var BYTE '8-bit var to store checksum x var byte 'general var p var byte [40] 'store pulse var led var LATA.0 clear high LATA.1:high LATA.2:high LATA.3:high LATA.4 high PORTB.4 ;---------- main program ------------- high led pause 500 LCDout $FE,$1,"DHT-22 sensor" LCDout $FE,$c0,"reading..." start: ;---- check_DHT22 ---- ;-== send StartSignal ==- TRISB.4 = 0 'Data port is output high PORTB.4 low PORTB.4 pause 20 high PORTB.4 pauseus 20 ;-== wait response from Sensor ==- TRISB.4 = 1 'Data port is input while PORTB.4=1:wend 'wait for low pauseus 80 while PORTB.4=0:wend 'wait for high pauseus 20 low led ;-== read 40bits data from DHT22 ==- for x=1 to 40 pulsin PORTB.4,1,p(x) 'measure 40 high pulses only & store (resoloution is 10us) next x ;-== convert to '0' & '1' ==- for x=1 to 40 if p(x)>=2 and p(x)<=4 then p(x)=0 'if pulsewidth between 20 and 40uS then read as '0' if p(x)>=6 and p(x)<=8 then p(x)=1 'if pulsewidth between 60 and 80uS then read as '1' next x ;-== convert bin to dec ==- humread=32768*p(1)+16384*p(2)+8192*p(3)+4096*p(4)+2048*p(5)+1024*p(6)+512*p(7)+256*p(8)+128*p(9)+64*p(10)+32*p(11)+16*p(12)+8*p(13)+4*p(14)+2*p(15)+1*p(16) tmpread=16384*p(18)+8192*p(19)+4096*p(20)+2048*p(21)+1024*p(22)+512*p(23)+256*p(24)+128*p(25)+64*p(26)+32*p(27)+16*p(28)+8*p(29)+4*p(30)+2*p(31)+1*p(32) checksum=128*p(33)+64*p(34)+32*p(35)+16*p(36)+8*p(37)+4*p(38)+2*p(39)+1*p(40) ;-== show real values ==- ;-== show real values ==- lcdout $fe,$01,"Hum:",dec humread,"% >",dec checksum lcdout $fe,$C0,"Temp:",dec tmpread,"o" goto start
The problem I have is -as far as I can see- I get response from the sensor BUT with strange results ...see attached foto
Any ideas are welcome...
maybe the problem is with pulsin command trying to catch-up with the sensor pulses.... or timming problems....
Also noticed that I get no CRC checksum data ! (following > charackter in the foto)
Any advice is welcome.






Bookmarks