\ get_temp.fs
\ prototype of temperature getting routine.
\
\ some hi-memory buffer spaces for storing values..
\
$2E00   constant bufr_S    \ xram location of buffer space
$2E00   constant id_hi     \ ascii ID high char
$2E01   constant id_lo     \ ascii ID low char
$2E02   constant ver_hi    \ software version high byte
$2E03   constant ver_lo    \ software version low byte
$2E04   constant tmp_hi    \ high byte of temperature
$2E05   constant tmp_lo    \ low  byte of temperature
$2E06   constant data_1    \ data value #1 ( high if double )
$2E07   constant data_2    \ data value #2 ( low  if double )
$2E08   constant data_3    \ data value #3 ( high if double )
$2E09   constant data_4    \ data value #4 ( low  if double )
$2E10   constant bufr_D    \ location of buffer for text strings
$70     constant bufr_L    \ length of buffer for text string

\ initialize and setup buffer space
: init_bufr
     clr_xram             \ zero out all of xram
     [ char T ] # id_hi  ## !xram \ store ID 
     [ char B ] # id_lo  ## !xram \ store ID 
     [ char 0 ] # ver_hi ## !xram \ store version 
     [ char 1 ] # ver_lo ## !xram \ store version 
       $0A      # data_1 ## !xram \ store data 
       $18      # data_2 ## !xram \ store data 
       $95      # data_3 ## !xram \ start data
       $A5      # data_4 ## !xram \ start data
     ;

\ setup for getting a temperature
: init_rdtmp  
                          \ all ADCO registers are page 0
     $07 # REF0CN  #!     \ enable temp sensor, on-chip VREF,
                          \ and VREF output buffer
     $00 # AMX0CF  #!     \ Select TEMP sense as ADC mux input
     $0f # AMX0SL  #!     \ Select TEMP sense as ADC mux input
     $c1 # ADC0CF  #!     \ ADC conversion clock 100mhz/4mhz -1 = 11000b
                          \ PGA gain = 2
     $c1 # ADC0CN  #!     \ ADC0 enabled; AD0BUSY conversion start
                          \ ADC0 data is left-justified
     ;

\ actual read temperature
: get_temp  ( - n n )     \ returns hi lo values
     chT                  \ set input to temp sensor + settle time..
     $c1 # ADC0CN  #!     \ reset bits to init state, clear ints.
     adint_c              \ make sure int is cleared
     ad0_conv             \ start the conversion
     begin ad0int until.  \ wait till gets set
     ADC0L #@ 
     ADC0H #@ 
     ;

\ main entry to get temperature and print it out
: .get_temp 
     get_temp
     dh.
     ;

\ raw write of temp into buffer..
: xget_temp
     get_temp
     tmp_hi ## !xram
     tmp_lo ## !xram
     ;

\ ascii write of temp into bufr..
: xbuftemp
     get_temp
     16 # u/mod (digit) !xbyptr (digit) !xbyptr
     16 # u/mod (digit) !xbyptr (digit) !xbyptr
     $20 # !xbyptr
     ;

\ do the sub double
: dusub ( d - d ) \ sub double with fixed values
        swap      \ hi lo - do lo first
        [ $8c negate ] # + \ sub $8c from lsb
        setc       \ set carry bit
        if'        \ had carry - negative number
          swap
          1-
          [ $a5 negate ] # + \ sub $A5 from MSB of temp
            ;
        then
          swap
          [ $a5 negate ] # + \ sub $A5 from MSB of temp
          ;

\ take 2347 and print 23.47
: u.d. 10000s drop 1000s digit 100s digit 
       drop $2e # emit 10 # u/mod digit digit
       space 
       ;

\ convert and put in xram
: xu.d. 10000s drop 1000s (digit) !xbyptr
        100s (digit) !xbyptr
        drop $2e # !xbyptr
        10 # u/mod (digit) !xbyptr (digit) !xbyptr
        ;

\ use mac0 to process hex into C/F 
: mactemp
     get_temp    \ return hi (top)  lo of temp = b430
        SFR-3
         $09 # MAC0CF #! \ Multiply only and clear
         [ nop nop ]
        SFR-0
        dusub            \ temp - $a58c = hi lo
          SFR-3
          MAC0AH #! \ store answer in msb of acc
          MAC0AL #! \ store answer of lsb sub
          $02 # MAC0BH #! $90 # MAC0BL #! \ mulitply by 656
          [ nop nop ]  
        $30 # MAC0CF #! [ nop nop ] \ 2
        $30 # MAC0CF #! [ nop nop ] \ 4
        $30 # MAC0CF #! [ nop nop ] \ 8
        $30 # MAC0CF #! [ nop nop ] \ 16
        $30 # MAC0CF #! [ nop nop ] \ 32
        $30 # MAC0CF #! [ nop nop ] \ 64
        $30 # MAC0CF #! [ nop nop ] \ 128
        $30 # MAC0CF #! [ nop nop ] \ 256
        $30 # MAC0CF #! [ nop nop ] \ 512
        $30 # MAC0CF #! [ nop nop ] \ 1024
        MAC0ACC0  #@ MAC0ACC1 #@ 
        SFR-0
     ;

: xmactemp \ puts 26.67 in xram - no space
        mactemp
        xu.d.
     ;


: amactemp \ emit 26.67
        mactemp
        u.d.
     ;



