Skip to content

Instantly share code, notes, and snippets.

@TG9541
Created March 17, 2019 20:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TG9541/1a90c05272253e41fe7e9ee53d25c858 to your computer and use it in GitHub Desktop.
Save TG9541/1a90c05272253e41fe7e9ee53d25c858 to your computer and use it in GitHub Desktop.
W1209 style NTC sensor signal to temperature conversion with LPF and un-chatter (GND -- NTC -(Ain)- 20k -- VCC)
\ W1209 temperature measurement with filter and noise suppression
\ © 2017 TG9541, refer to https://github.com/TG9541/W1209/blob/master/LICENSE
\ Note: W1209 thermostats may require adjustment,
\ especially when used outside the range of -5C to +20C
\ Refer to https://github.com/TG9541/W1209/wiki/W1209-Sensor
RAM
900 CONSTANT USENSMAX \ max temperature
NVM
#require @inter
$8000 CONSTANT DEFAULT \ Default value indicator (-32768)
\ note: adjust to specific sensor here if accuracy is required!
\ note: @inter accepts 2..n value pairs
\ interpolation table ADC digits * 32 -> temperature * 10
CREATE dig2tem 14 , \ number of value pairs
1216 , 1100 , \ 1 1216 -> 11.0 C
1534 , 1000 , \ 2
1982 , 900 , \ 3
2560 , 800 , \ 4
3296 , 700 , \ 5
4320 , 600 , \ 6
5634 , 500 , \ 7
7370 , 400 , \ 8
9632 , 300 , \ 9
12382 , 200 , \ 10
15522 , 100 , \ 11
19012 , 0 , \ 12
20768 , -50 , \ 13
22466 , -100 , \ 14 22466 digits -> -10.0 C
: lpf32 ( n1 alpf -- n2 )
\ low pass filter, multiplies n1 by 32, uses a as LPF memory
( alpf ) DUP >R @ DUP 32 / - + DUP R> !
;
: unchatter ( n1 ahy -- n2 )
\ remove chatter (+/- 0.5 digit window), divides n1 by 32
\ ahy: hysteresis storage call address
SWAP 16 / OVER @ OVER SWAP - ABS 2-
0< IF DROP @ ELSE DUP ROT ! THEN
2/
;
: ntctheta ( adc astr -- theta )
\ filter noisy W1209 sensor input, result in astr @
SWAP DUP USENSMAX < IF
OVER 2+ lpf32 \ low pass filter, "noise to digits", 32*ADC
dig2tem @inter \ interpolation: lpf32 digits to temperature
OVER 2+ 2+ lpf32 \ low pass filter, "smoothen", 32*theta
OVER 2+ 2+ 2+ unchatter \ remove chatter, divides by 32
ELSE
DROP DEFAULT \ sensor error - default
THEN
SWAP ! \ store theta in astr
;
RAM
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment