(* For noForth C&V2553 lp.0, bit output, timer interrupt with machine code,
  on MSP430G2553 using port-1. More info, see page 369 and beyond of SLAU144J.PDF
  Flashing LEDS using a timer compare interrupt, using SMCLK or ACLK
  Compare mode 2 is used here, means counting to FFFF. Uses register R11=XX.
  The frequency may be adjusted by changing the value PERIOD
  A higher value means slower flash frequency, default is 1
  Forth words are: SMCLK-ON  ACLK-ON  TIMER-OFF

  Register addresses for Timer-A
180 - TA1CTL   Timer A control
182 - TA1CCTL0 Timer A Comp/Capt. control 0
192 - TA1CCR0  Timer A Compare register

  Bits in TA1CCTL0
010 - CCIE       \ Enable compare capture intrpt
  Bits in TA1CTL with ACLK
000 - ID-3       \ Input divider /1 {bit =6,7}
100 - TASSEL-2   \ 32kHz ACLK as clock source {bit8,9}
021 - P1OUT      \ Output reg.
022 - P1DIR      \ Direction reg.
008 - GIE        \ General interrupt enable bit
 *)

hex

\ Activate 32 kHz XT
: 32KHZ     ( -- )
    000C 0053 c!    \ BCSCTL3   32kHz with 12pF
    begin           \ LFXT osc. running?
        02 002 *bic \ IFG1      Clear bit 2
        noop  noop  \ Wait, Bit 2 remains clear
    02 002 bit* 0= until \ when osc. runs fine!
    ;

value SECS          \ seconds
value MINS          \ Minutes
value HOURS         \ Hours
value DAYS          \ Day counter

: SET-CLOCK     ( hours minutes -- )
    0 to secs  to mins  to hours ;

: GET-CLOCK     ( -- hours minutes seconds )
    secs  mins  hours ;

\ code INT-ON     ( -- )  #8 sr bis  next  end-code
\ code INT-OFF    ( -- )  #8 sr bic  next  end-code
\
\ 24 hour clock interrupt subroutine
routine CLOCK-INT   ( -- )
    41 # 21 & bix
    #1 adr secs & add       \ Increase seconds
    dm 60 # adr secs & cmp
    =? if,                  \ Minute done?
        #0 adr secs & mov   \ Yes
        #1 adr mins & add
        dm 60 # adr mins & cmp
        =? if,              \ Hour done?
            #0 adr mins & mov \ Yes
            #1 adr hours & add
            dm 24 # adr hours & cmp
            =? if,          \ Day done?
                #0 adr hours & mov \ Yes
                #1 adr days & add
            then,
        then,
    then,
    reti
end-code

\ Timer compare interrupt off
: CLOCK-OFF     ( -- )  int-off  010 182 **bic ; \ TA0CCTL0

\ Set timer compare interrupt on with ACLK and init. leds
\ Clock divider=/1, Up mode & Timer A1 clear.
: ACLK-ON       ( -- )
    int-off  41 022 *bis    \ P2DIR    Set pins with LEDs to output
    01 029 *bis 40 029 *bic \ P2OUT    Set led on, other led off
    32khz                   \ 32kHz on
    8000 192 !              \ One Second tick
    114 0180 !              \ TA1CTL   Set timer mode to ACLK/2
    010 0182 **bis          \ TA1CCTL0 Enable interrupts on Compare 0
    int-on ;                \ Activate timer

clock-int  FFFA vec!        \ Timer-A1 vector
shield TIMER0\  freeze
aclk-on
