go back
(* DHT22 sensor, using PIO: 480 bytes
Willem Ouwerkerk 20250627
40-bits pulse stream 32-bits data & 8-bits checksum
*)
chere
need arshift \ Load ARSHIFT from library
need pio\ \ Load PIO (dis)assembler
clean-pio decimal \ Empty code space, start at zero
0 0 {pio \ Use state machine-0 on PIO-0
160000 =freq \ On 4 * 40 kHz frequency (6.25 Ás ticks)
05 1 =out-pins \ GPIO 5 for OUT & SET
05 1 =set-pins
05 =in-pin \ GPIO 5 for IN & JMP
05 =jmp-pin
0 =in-dir \ Shift left
32 1 =autopush
0 pindirs set, \ Pin is input
wrap-target
4 [] begin, again, \ Wait for start command
one ( Sensor readout )
1 pindirs set, \ Pin is output
3 y set, begin, \ Generate start pulse low .8 ms
31 [] 0 pins set,
y--? until,
6 [] 1 pins set, \ 44 Ás start pulse high
6 [] 0 pindirs set, \ Now input, wait 44 Ás, skip response low
6 [] high 0 pin wait, \ Wait for response high 44 Ás
31 y set, \ Now read the 32-bits answer 6.25 Ás
begin, \ This contains the sensor data
low 0 pin wait,
5 [] high 0 pin wait, \ Wait for high bit 37.5 Ás
1 pins in, \ Shift in low or high bit 6.25 Ás ( autopush
y--? until, \ Count all 32 bits 6.25 Ás)
7 y set, \ Read 8-bit checksum 6.25 Ás
begin,
low 0 pin wait,
5 [] high 0 pin wait, \ Wait for high bit 37.5 Ás
1 pins in, \ Shift in checksum bit 6.25 Ás
y--? until, \ Count 8 bits 6.25 Ás
push, \ Result 8-bits to fifo 6.25 Ás
wrap
0 =exec \ Start with wait loop
pio}
v: pios also extra
: READ) ( -- data chk )
one> 0 exec-opc \ Start new measurement
begin 0 rx-depth 1- until \ Stack minimal two deep?
0 rxf> 0 rxf> ; \ 32-bits data & 8-bits checksum
: .DHT ( h -- ) \ Show scaled result
16 lshift 16 arshift \ Extend sign bit
s>d tuck dabs
<# # ch . hold #s rot sign #>
type space ;
: CHKS ( data -- chks ) \ Calculate checksum
h-h b-b + swap \ Split & calc. checksum
b-b + + hx FF and ;
: READ ( -- t h )
0 begin read) \ Read sensor
>r dup chks \ save & split reading
r> <> while \ Not ok?
drop 1+ \ Drop reading, count retries
dup 1- ?abort \ More then one retry, abort
2000 ms
repeat
nip h-h ; \ Split in temp. & hum.
: DHT22 ( -- )
base @ decimal
read .dht ." %rel, " .dht ." Celcius " \ Show scaled result
base ! ;
v: fresh
hex
chere swap - dm .