\ DS1821 Digital Thermometer and Thermostat with 1-Wire Interface     MM-141222
\
\ Requires: * 4e4th 0.34 on a MSP430G2553 launchpad
\           * e4thcom-0.6.1 -t 4e4th-xas

#require onewire.xas

hex

: ow.t/r ( c -- c' ) \ Transmit and receive one byte to/from the 1-Wire Bus.
    8 0 do owslot ( pause ) loop ;

: ow.tx ( c -- ) \ Send one byte to the 1-Wire Bus.
    ow.t/r drop ;

: ow.rx ( -- c ) \ Receive one byte from the 1-Wire Bus.
    0FF ow.t/r ;


\ : ow?reset ( -- ) owreset 0= ?abort ;  \ noForth
: ow?reset ( -- ) owreset 0= abort" ow-bus?" ;  \ 4e4th

: ow.c@ ( a -- c' )
  \ Send an addr byte a to 1-Wire Bus and read a data byte back.
    ow?reset ow.tx ow.rx ;

: ow.c! ( c a -- )
  \ Send an addr and data byte to the 1-Wire Bus.
    ow?reset ow.tx ow.tx ;


: ds1821.read.status ( -- c ) 0AC ow.c@ ;

: ds1821.write.status ( c -- ) 0C ow.c! ;

: ds1821.start ( -- ) 0EE ow?reset ow.tx ;
: ds1821.stop  ( -- ) 022 ow?reset ow.tx ;

: ds1821.1shot ( -- ) ds1821.read.status 01 or ds1821.write.status ;
: ds1821.cont  ( -- ) ds1821.read.status 0FE and ds1821.write.status ;

: ds1821.read ( -- c ) 0AA ow.c@  base @ >r decimal . r> base ! ;

decimal

: ds1821.scan ( -- )
  \ Read and display the temperature every second.
    ds1821.start 500 ms 
    begin ds1821.read 1000 ms key? until key drop ;

hex

\index  ds1821.scan

\\

#require cbits

: ds1821.status ( -- ) \ Show the DS1821 Status Register
  cr
  ." DS1821 Status Register:" ds1821.read.status cbits
  ."  DONE      1       NVB      THF      TLF      T/R      POL     1SHOT    "
  cr ;

