(* 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 PERIOD            \ Extra divider
\ code INT-ON     ( -- )  #8 sr bis  next  end-code
\ code INT-OFF    ( -- )  #8 sr bic  next  end-code
\ code INIT-TIMER ( -- )  adr period & xx mov  #8 sr bis  next  end-code
\
\ Interrupt subroutine
\ routine TIMER-INT   ( -- )
\    #-1 xx add              \ Decrease divider
\    =? if,                  \ Zero?
\        adr period & xx mov \ Reload divider
\        21 # 029 & xor>     \ P1OUT  Toggle both LEDs
\    then,
\    reti
\ end-code

code INIT-TIMER   421B ,
adr period ,  D232 ,  4F00 ,  end-code

routine TIMER-INT   533B ,  2005 ,
421B ,  adr period ,  E0B2 ,  21 ,
29 ,  1300 ,  end-code

\ Timer compare interrupt off
: TIMER-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  21 02A *bis    \ P1DIR    Set pins with LEDs to output
    01 029 *bis 20 029 *bic \ P1OUT    Set led on, other led off
    ( 20 53 *bis ) 32khz    \ 15kHz or 32kHz on
    8000 192 !              \ 1 Second tick
    114 0180 !              \ TA1CTL   Set timer mode to ACLK/2
    010 0182 **bis          \ TA1CCTL0 Enable interrupts on Compare 0
    01 to period init-timer \ Activate timer
    ;

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