On Sunday, September 25, 2022 at 12:01:57 PM UTC+2, Jos Ven wrote:
> Thank you for looking so close to my source.
I should thank you! It was a an entertaining and satisfying exercise.
> In the other Forth systems, I got the following:
> For Cforth:
> - Has no find
> - Must use f# to get a float on the stack
Guilty on both. I used [DEFINED] for it. I don't have F# either (name clash),
but its equivalent is F%.
And as I said: I don't have FVALUE or a "true" KEY? The latter is an alias
for FALSE. And finally: MS has a resolution of 1000 MS ;-)
> What happens in your Forth when you use:
create &buf
Creates a table for R/O integer constants;
4 floats
Throws the size for 4 floating points numbers on the stack (not
quite - but let's not go into detail);
allot
Syntax error: No such word. In order to do what you want to do
I have to write:
4 FLOATS ARRAY &BUF
That does it.
> &buf f@ f.
No problem. It will work as intended.
Hans Bezemer
BTW, here is a list of changes to your code:
> cr decimal fclear
FCLEAR initializes floating point properly
> f% 3300 fvariable Vs latest f! \ In mV, measure it
> f% 10000 fvariable R0 latest f! \ 10K
> f% 298.15e0 fconstant T0 \ Temperature in Kelvin at 25C
> f% 273.15e0 fconstant 0C \ Temperature in Kelvin at 0C
> f% 0.7556958984e-3 fvariable A_sh latest f!
> f% 2.334204104e-4 fvariable B_sh latest f!
> f% 0.6102744539e-7 fvariable C_sh latest f!
> : 1/f ( F: f - 1/f ) f% 1 fswap f/ ;
Obviously due to the use of F% and FVALUE conversion
> Vs f@ s>f f- ;
> fdup R0 f@ f* Vs f@ frot f- f/ ;
> fln fdup fdup fdup f* f* C_sh f@ f* \ C*(ln(Rnct))^3
> fswap B_sh f@ f* f+
> A_sh f@ f+ \ 1/T
> cr .( A: ) A_sh f@ f.
> cr .( B: ) B_sh f@ f.
> cr .( C: ) C_sh f@ f.
> cr .( Vs: ) Vs f@ f. ( mV )
> cr .( Resistor R0:) R0 f@ f.
Obviously to the conversion of FVALUE to FVARIABLE
> cr .( Rt:) f% 29456 fdup f>s . ntc-sh
Another due to the use of F%
> [defined] adc-mv [if] \ For Cforth on a ESP32:
Here you got your FIND
> 32 constant /adc_chars
I don't do prefixed numbers
> /adc_chars floats array &adc_chars
Here you got my ARRAY
> 1500000 value Mv \ fake sensor reading
> : read-adc-mv drop Mv 1000 / 10001 choose 5000 - +to Mv ;
> : init-adc drop drop drop ;
This is my fake sensor
> cr .( to monitor the NTC.) cr cr
> [then]
Obviously - I can't QUIT here
> f% 1.292290081e-3 A_sh f!
> f% 2.164041451e-4 B_sh f!
> f% 0.8776278596e-7 C_sh f!
> f% 0 fvariable av-trim latest f! \ if needed
> cr .( A ) A_sh f@ f.
> cr .( B ) B_sh f@ f.
> cr .( C ) C_sh f@ f.
> cr .( av-trim :) av-trim f@ f.
> cr .( Vs :) Vs f@ f.
> cr .( Resistor R0:) R0 f@ f.
More FVALUE and F% stuff
> 10 constant #samples
I don't do prefixed numbers
> 60 constant /ring-buf \ To reduce the ADC noise further
In 4tH, you can't initialize with a VALUE
> /ring-buf floats array &ring-buf
Here you got my ARRAY again
> f% 0 ring-buf-count /ring-buf min dup 0
Obviously due to the use of F%
> dup 3 3 init-adc 3 set-precision
I just liked a slightly higher precision ;-)
> 0 to ring-buf-count
I split this line in two lines
> /ring-buf 0 ?do f% 0 &ring-buf i floats + f! loop \ erase
All memory is zero-ed at startup, but let's stay in the spirit ;-)
> ring-buf-count . av-ntc av-trim f@ f+ f.
Another victim of FVALUE
> 5 monitor-ntc
In 4tH, you have to start something. If not, it just compiles it
and quits immediately.
Running your program (first 12 "measurements"):
$ pp4th -x ntc_sh.4pp
ntc_sh.fth the Steinhart-Hart equation.
ntc-sh ( Rt - Celsius ) can be used.
adc-mv ( adc_chars reading - mV ) is missing.
See:
https://github.com/MitchBradley/cforth/tree/WIP
Or define your own accurate read-adc-mv and init-adc here
to monitor the NTC.
Parameters for NTC B57164K0103:
A 0.0012922900810
B 0.0002164041451
C 0.0000000877627
av-trim :0.00
Vs :33
Resistor R0:10
To monitor the NTC on adc-channel 5 enter: 5 monitor-ntc
Sec C
1 21.0
2 21.0
3 21.0
4 21.1
5 21.2
6 21.2
7 21.2
8 21.2
9 21.2
10 21.2
11 21.2
12 21.2