Code:
'***************************************
'* Name : 18f2320_DHT22_AM2302.BAS *
'* Author : bitmaniac Alexandros Z.*
'* Notice : Copyright (c) 2013 *
'* Date : 5/4/2013 *
'***************************************
;include "ALLDIGITAL.pbp"
;@ __CONFIG _CONFIG1H, _INTIO2_OSC_1H & _IESO_OFF_1H ' use internal osc
@ __CONFIG _CONFIG1H, _HS_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 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 300
define osc 4
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
pulse var byte [81] 'store pulse var
p var bit [81] 'store pulse var
led var LATA.0
dht_port var PORTB.4
clear
high dht_port
high led
;---------- main program -------------
high led
LCDout $FE,$01,"DHT-22"
LCDout $FE,$C0,"testing..."
pause 2000
start:
;---- check_DHT22 ----
;-== send StartSignal ==-
TRISB.4 = 0 'Data port is output
high dht_port
pauseus 10 'send 10uS high pulse
low dht_port
pause 1 'send 1mS high pulse
high dht_port
;-== wait response from Sensor ==-
TRISB.4 = 1 'Data port is input
while dht_port=1:wend 'wait for low from DHT
while dht_port=0:wend 'wait for high FROM DHT
high led
;-== Grab 40bits data from DHT22 ==-
pulsin PORTB.4,1,pulse(1) 'Humidity HighBit data
pulsin PORTB.4,1,pulse(2)
pulsin PORTB.4,1,pulse(3)
pulsin PORTB.4,1,pulse(4)
pulsin DHT_port,1,pulse(5)
pulsin DHT_port,1,pulse(6)
pulsin DHT_port,1,pulse(7)
pulsin DHT_port,1,pulse(8)
pulsin DHT_port,1,pulse(9)
pulsin DHT_port,1,pulse(10)
pulsin DHT_port,1,pulse(11)
pulsin DHT_port,1,pulse(12)
pulsin DHT_port,1,pulse(13)
pulsin DHT_port,1,pulse(14)
pulsin DHT_port,1,pulse(15)
pulsin DHT_port,1,pulse(16) 'Humidity LowBit data
pulsin DHT_port,1,pulse(17) 'Temperature HighBit data (1 means below 0 degree!)
pulsin DHT_port,1,pulse(18)
pulsin DHT_port,1,pulse(19)
pulsin DHT_port,1,pulse(20)
pulsin DHT_port,1,pulse(21)
pulsin DHT_port,1,pulse(22)
pulsin DHT_port,1,pulse(23)
pulsin DHT_port,1,pulse(24)
pulsin DHT_port,1,pulse(25)
pulsin DHT_port,1,pulse(26)
pulsin DHT_port,1,pulse(27)
pulsin DHT_port,1,pulse(28)
pulsin DHT_port,1,pulse(29)
pulsin DHT_port,1,pulse(30)
pulsin DHT_port,1,pulse(31)
pulsin DHT_port,1,pulse(32) 'Temperature LowBit data
;pulsin DHT_port,1,pulse(33) 'Checksum HighBit
;pulsin DHT_port,1,pulse(34)
;pulsin DHT_port,1,pulse(35)
;pulsin DHT_port,1,pulse(36)
;pulsin DHT_port,1,pulse(37)
;pulsin DHT_port,1,pulse(38)
;pulsin DHT_port,1,pulse(39)
;pulsin DHT_port,1,pulse(40) 'Checksum LowBit
;-== convert to '0' & '1' ==-
for x=1 to 40
if pulse(x)>=2 and pulse(x)<=4 then p(x)=0 'if pulsewidth between 20 and 40uS then read as '0'
if pulse(x)>=6 and pulse(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 ==-
LCDout $FE,$01,"RHdata Tdata"
LCDout $FE,$C0,dec humread/10,".",dec humread//10,"% ",dec tmpread/10,".",dec tmpread//10,"oC" 'print integral part only
low led
pause 2000 'give some time to DHT to stabilize!....
goto start
END
Bookmarks