\ DS1821 Digital Thermometer and Thermostat with 1-Wire Interface     MM-141222
\
\ Requires: * Mecrisp on a MSP430G2553 launchpad
\           * e4thcom-0.6.1 -t mecrisp-msp430xas

compiletoflash

#require onewire.xas
#require ms

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 ( -- )
  \ Two calls to owreset may be required to detect a device after a COLD start,
  \ see the definition of owreset.
\    owreset if exit then owreset 0= ?abort ;
    owreset if exit then owreset 0= if ." msg from ow?reset" quit then ;

: 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 the temperature every second.
    ds1821.start 500 ms 
    begin ds1821.read 1000 ms key? until key drop ;

\index  ds1821.scan

\ Last Revision: MM-170116

