#include  "msp430x12x.h"
;******************************************************************************
;   MSP-FET430P120 Demo - BasicClock MCLK configured with HF XTAL and OscFault
;
;   Description; Proper selection of an external HF XTAL for MCLK is shown with 
;   enabled OSC fault interrupt - if HF XTAL fails, NMI interrupt is requested.
;   HF XTAL can only source MCLK when OSC fault is clear. For demonstration
;   purposes P1.0 is set durring OSC fault. MCLK/10 output on P1.1.   
;   ACLK = MCLK = LFXT1 = HFXTAL  
;   //** HF XTAL NOT INSTALLED ON FET **//
;
;               MSP430F123(2)
;             -----------------
;         /|\|              XIN|-  
;          | |                 | HF XTAL (455k - 8Mhz)
;          --|RST          XOUT|-
;            |                 |
;            |             P1.0|-->LED
;            |             P1.1|-->MCLK/10 = HF XTAL/10 (or DCO durring fault)
;
;   M.Buccini
;   Texas Instruments, Inc
;   January 2002
;******************************************************************************
;------------------------------------------------------------------------------ 
            ORG     0E000h                  ; Program Start
;------------------------------------------------------------------------------ 
RESET       mov.w   #300h,SP                ; Initialize stackpointer
SetupWDT    mov.w   #WDTPW+WDTHOLD,&WDTCTL  ; Stop WDT
            bis.b   #003h,&P1DIR            ; P1.0,1 = output direction
            bic.b   #001h,&P1OUT            ; P1.0 = reset
SetupBC     bis.b   #XTS,&BCSCTL1           ; LFXT1 = HF XTAL
            bis.b   #SELM1+SELM0,&BCSCTL2   ; MCLK = LFXT1
            bis.b   #OFIE,&IE1              ; Enable osc fault interrupt 
                                            ;           
Mainloop    bis.b   #002h,&P1OUT            ; P1.1 = 1
            bic.b   #002h,&P1OUT            ; P1.1 = 0
            jmp     Mainloop                ; Repeat
                                            ;           
;------------------------------------------------------------------------------ 
NMI_ISR;   Only osc fault enabled, R15 used temporarly and not saved
;------------------------------------------------------------------------------ 
            bis.b   #001h,&P1OUT            ; P1.0 = set
SetupOsc    bic.b   #OFIFG,&IFG1            ; Clear OSC fault flag 
            mov.w   #0FFh,R15               ; R15 = Delay 
SetupOsc1   dec.w   R15                     ; Additional delay to ensure start 
            jnz     SetupOsc1               ;
            bit.b   #OFIFG,&IFG1            ; OSC fault flag set? 
            jnz     SetupOsc                ; OSC Fault, clear flag again 
            bic.b   #001h,&P1OUT            ; P1.0 = reset
            bis.b   #OFIE,&IE1              ; re-enable osc fault interrupt 
            bis.b   #SELM1+SELM0,&BCSCTL2   ; MCLK = LFXT1 - now safe
            reti                            ; return from interrupt
                                            ;
;------------------------------------------------------------------------------ 
;           Interrupt Vectors Used MSP430F12x         
;------------------------------------------------------------------------------ 
            ORG     0FFFCh                  ; NMI vector
            DW      NMI_ISR                 ; 
            ORG     0FFFEh                  ; MSP430 RESET Vector
            DW      RESET                   ; 
            END     
