aboutsummaryrefslogtreecommitdiff
path: root/forth/forth/tc74-app.fs
blob: db7db9b064f5d140a4116db633d7d364fbb21a45 (plain)
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
\ Read temperature from TC74 on I2C bus.
\ Requires i2c-base.txt to be previously loaded.
\ Modelled on Mikael Nordman's i2c_tcn75.txt.
\ Peter J. 2014-10-28

-tc74-app
marker -tc74-app

%1001000 constant addr-TC74A0

: tc74-init ( -- )
  \ Selects temperature register for subsequent reads.
  addr-TC74A0 i2c.addr.write if 0 i2c.c! drop then i2c.stop
;

: sign-extend ( c -- n )
  \ If the TC74 has returned a negative 8-bit value,
  \ we need to sign extend to 16-bits with ones.
  dup $7f > if $ff80 or then
;

: degrees@ ( -- n ) 
  \ Wake the TC74 and fetch its register value.
  addr-TC74A0 i2c.addr.read if i2c.c@.nack else 0 then i2c.stop 
  sign-extend
;

: tc74-main ( -- ) 
  i2c.init
  tc74-init
  begin
    degrees@ . 
    #1000 ms
  key? until
;

\ Now, report temperature in degrees C 
\ while we warm up the TC74 chip with our fingers...
\
\ decimal tc74-main 23 23 23 23 23 23 23 24 24 24 25 25 26 26 26 26 26 27