-
Notifications
You must be signed in to change notification settings - Fork 0
/
mh-z19b.fs
70 lines (57 loc) · 1003 Bytes
/
mh-z19b.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
\ Winson MH-Z19B co2 sensor
\ pwm mode
\ sensor hooked to digital pin 2 = PORTD2
-mhz19b
marker -mhz19b
$002b constant portd
$002a constant ddrd
$0029 constant pind
: mhz-init
ddrd c@
$fb and \ set D2 as input
ddrd c!
;
: readpin2
pind c@ $04 and
;
: waitlow ( -- ) \ blocks until D2 is low
begin
readpin2 0=
until
;
: waithigh
begin
readpin2
until
;
\ this should be computed as below
\ : get1pulse
\ waitlow
\ waithigh ticks
\ waitlow ticks
\ waithigh ticks
\ over -
\ rot rot swap - \ now we have TL TH
\ over over . . cr
\ dup 2 - 2000 * \ TL TH 2000*TH-2
\ rot rot \ TH-2 TL TH
\ + 4 -
\ /
\ ;
\ a good enough approximation... the hi+low pulses
\ are about 1002 ms if all is ok so assume all is ok
: get1pulse
waitlow waithigh ticks
waitlow ticks swap - 2 - 2 *
;
: dnum s>d <# #s #> 0 do dup c@ drc 1 spc 1+ loop drop ;
: co2loop
ssdi clshome
s" CO2: " dtxt
begin
get1pulse
30 set-col-addr
dnum
key?
until
;