'**********************************************************
'* PICAXE BASIC FOR READING DHT-11 WITH PICAXE 28X2 *
'**********************************************************
#picaxe 40x2
#no_table
setfreq em64
high c.0
init:
symbol bit_num = b0
symbol humidity = b2
symbol temperature = b4
symbol numbits = b6
GET_DATA:
pause 12500 'Stabilize 2 seconds
setfreq em64 'Must read Sensor @ 64mhz !
ptr = 1 'Set Scratch Pad pointer to "1"
pulsout c.0, 35000 'Req Data from DHT-11
pulsin c.1,1,@ptr 'First bit (will be ignored so don't increment)
'Humidity bits (Integer)
pulsin c.1,1,@ptrinc 'SP Location 1
pulsin c.1,1,@ptrinc 'SP location 2
pulsin c.1,1,@ptrinc 'SP location 3
pulsin c.1,1,@ptrinc 'SP location 4
pulsin c.1,1,@ptrinc 'SP location 5
pulsin c.1,1,@ptrinc 'SP location 6
pulsin c.1,1,@ptrinc 'SP location 7
pulsin c.1,1,@ptrinc 'SP location 8
'Humidity Bits (Decimal)
pulsin c.1,1,@ptrinc 'not used with DHT11
pulsin c.1,1,@ptrinc 'not used
pulsin c.1,1,@ptrinc 'not used
pulsin c.1,1,@ptrinc 'not used
pulsin c.1,1,@ptrinc 'not used
pulsin c.1,1,@ptrinc 'not used
pulsin c.1,1,@ptrinc 'not used
pulsin c.1,1,@ptrinc 'not used
'Temperature bits (Integer)
pulsin c.1,1,@ptrinc
pulsin c.1,1,@ptrinc
pulsin c.1,1,@ptrinc
pulsin c.1,1,@ptrinc
pulsin c.1,1,@ptrinc
pulsin c.1,1,@ptrinc
pulsin c.1,1,@ptrinc
pulsin c.1,1,@ptrinc
'Temperature Bit (Decimal)
pulsin c.1,1,@ptrinc 'Not used
pulsin c.1,1,@ptrinc
pulsin c.1,1,@ptrinc
pulsin c.1,1,@ptrinc
pulsin c.1,1,@ptrinc
pulsin c.1,1,@ptrinc
pulsin c.1,1,@ptrinc
pulsin c.1,1,@ptrinc
'Checkusm bits
pulsin c.1,1,@ptrinc
pulsin c.1,1,@ptrinc
pulsin c.1,1,@ptrinc
pulsin c.1,1,@ptrinc
pulsin c.1,1,@ptrinc
pulsin c.1,1,@ptrinc
pulsin c.1,1,@ptrinc
pulsin c.1,1,@ptrinc
setfreq em32 'Reduce Frequency for Serial @38400
pause 100
'******* 'For testing / debugging *****************
numbits = ptr - 1
Sertxd ( "Received: ",#Numbits," Bits",cr,lf,cr,lf)
Sertxd ("Humidity Vals: ")
for ptr = 1 to 8
sertxd (#@ptr, " ")
next
sertxd(cr,lf,cr,lf)
sertxd ("Temperature Vals: ")
for ptr = 17 to 24
sertxd (#@ptr, " ")
next
Sertxd (Cr,lf,cr,lf)
'************************************************
'*** Process 8 Humidity bits ***
Humidity = 0 ' Clear humidity variable B2
For ptr = 1 to 8 'Read bits 1 - 8 (ignoring bit 0)
bit_num = 8 - ptr 'Bit# 7 - 0 for Humidity Var B2
if @ptr > 90 then
setbit humidity,bit_num 'Set b2 bit(x) to 1
endif
if @ptr < 60 then 'Set b2 bit(x) to 0
clearbit humidity,bit_num
endif
Next
'Don't care about bits 9- 16
'*** Process 8 temperature bits ***
Temperature = 0 'Clear Temperature variable B4
For ptr = 17 to 24 'Read DHT-11 bits 17 - 24
bit_num = 24 - ptr 'Bit# 7 - 0 for Temperature Var B2
if @ptr > 90 then 'Set b4 bit(x) to 1
setbit temperature,bit_num
endif
if @ptr < 60 then 'set b4 bit(x) to 0
clearbit temperature,bit_num
endif
Next
'**** Skipping Checksum ****
'*** Display data ***
sertxd ("Temperature: ",#temperature,176,"C",cr,lf)
sertxd ("Rel Humidity: ",#humidity,"%",cr,lf,cr,lf)
GOTO GET_DATA 'LOOP