#include  "msp430x14x.h"
;******************************************************************************
;   MSP-FET430P140 Demo - Timer_B Toggle P4.0-6 CCRx ContMode, 32kHz ACLK 
;
;   Description; Use timer_B CCRx units and overflow to generate eight 
;   independent timing intervals. For demonstration, TBCCR0 - TBCCR6 output
;   units are optionally selected with  P4.0 - P4.6 respectively in toggle 
;   mode. As such, these pins will toggle when respective TBCCRx registers 
;   match the TBR counter. Interrupts are also enabled with all TBCCRx units,
;   software only adds offset to next interval - as long as the interval 
;   offset is aded to TBCCRx, toggle rate is generated in hardware. Timer_B 
;   overflow ISR is used to toggle P1.0 with software.  
;   ACLK = TACLK = 32kHz, MCLK = SMCLK = DCO ~ 3Mhz 
;   As coded and with TACLK = 32768, toggle rates:
;   P4.0 = TBCCR0 = 32768/(2*4) = 4096Hz
;   P4.1 = TBCCR1 = 32768/(2*8) = 2048Hz
;   P4.2 = TBCCR2 = 32768/(2*16) = 1024Hz
;   P4.3 = TBCCR3 = 32768/(2*32) = 512Hz
;   P4.4 = TBCCR4 = 32768/(2*100) = 163.84Hz
;   P4.5 = TBCCR5 = 32768/(2*200) = 81.92Hz
;   P4.6 = TBCCR6 = 32768/(2*250) = 40.96Hz
;   P1.0 = overflow = 32768/(2*65536) = 0.25Hz
;   Proper use of TBIV interrupt vector generator demonstrated.   
;   //*An external watch crystal on XIN XOUT is required for ACLK*//	  
;
;                MSP430F149
;             -----------------
;         /|\|              XIN|-  
;          | |                 | 32k
;          --|RST          XOUT|-
;            |                 |
;            |             P4.0|--> TBCCR0
;            |             P4.1|--> TBCCR1
;            |             P4.2|--> TBCCR2
;            |             P4.3|--> TBCCR3
;            |             P4.4|--> TBCCR4
;            |             P4.5|--> TBCCR5
;            |             P4.6|--> TBCCR6
;            |             P1.0|--> overflow/software
;
;   M.Buccini
;   Texas Instruments, Inc
;   January 2002
;******************************************************************************
;------------------------------------------------------------------------------ 
            ORG     01100h                  ; Progam Start 
;------------------------------------------------------------------------------ 
RESET       mov.w   #0A00h,SP               ; Initialize 'F149 stackpointer
StopWDT     mov.w   #WDTPW+WDTHOLD,&WDTCTL  ; Stop WDT
SetupBC     bis.b   #07h,&BCSCTL1           ; DCO ~ 3Mhz
SetupTB     mov.w   #TBSSEL0+TBCLR+TBIE,&TBCTL   ; ACLK, clear TAB, interrupt
SetupCx     mov.w   #OUTMOD_4+CCIE,&TBCCTL0 ; TBCCR0 interrupt enabled
            mov.w   #OUTMOD_4+CCIE,&TBCCTL1 ; TBCCR1 interrupt enabled
            mov.w   #OUTMOD_4+CCIE,&TBCCTL2 ; TBCCR2 interrupt enabled
            mov.w   #OUTMOD_4+CCIE,&TBCCTL3 ; TBCCR3 interrupt enabled
            mov.w   #OUTMOD_4+CCIE,&TBCCTL4 ; TBCCR4 interrupt enabled
            mov.w   #OUTMOD_4+CCIE,&TBCCTL5 ; TBCCR5 interrupt enabled
            mov.w   #OUTMOD_4+CCIE,&TBCCTL6 ; TBCCR6 interrupt enabled
            mov.w   #4,&TBCCR0              ; 
            mov.w   #8,&TBCCR1              ; 
            mov.w   #16,&TBCCR2             ; 
            mov.w   #32,&TBCCR3             ; 
            mov.w   #100,&TBCCR4            ; 
            mov.w   #200,&TBCCR5            ; 
            mov.w   #400,&TBCCR6            ; 
SetupP4     bis.b   #07Fh,&P4SEL            ; P4.x - P4.6 option select
            bis.b   #07Fh,&P4DIR            ; P4.x = outputs 
SetupP1     bis.b   #001h,&P1DIR            ; P1.0 = output 
            bis.w   #MC1,&TBCTL             ; Start Timer_B in continous mode
            eint                            ; Enable interrupts
                                            ;													  
Mainloop    bis.w   #LPM3,SR                ; Enter LPM3
            nop                             ; Required only for C-spy
                                            ;
;------------------------------------------------------------------------------ 
TB0_ISR;    Toggle P1.0
;------------------------------------------------------------------------------ 
            add.w   #4,&TBCCR0              ; Offset until next interrupt
            reti                            ;		 
;
;------------------------------------------------------------------------------ 
TBX_ISR;    Common ISR for TBCCR1-6 and overflow
;------------------------------------------------------------------------------ 
            add.w   &TBIV,PC                ; Add Timer_B offset vector
            reti                            ; CCR0 - no source
            jmp     TBCCR1_ISR              ; TBCCR1
            jmp     TBCCR2_ISR              ; TBCCR2
            jmp     TBCCR3_ISR              ; TBCCR3
            jmp     TBCCR4_ISR              ; TBCCR4
            jmp     TBCCR5_ISR              ; TBCCR5
            jmp     TBCCR6_ISR              ; TBCCR6
TB_over     xor.b   #001h,&P1OUT            ; Toggle P1.0
            reti                            ; Return from overflow ISR		 
                                            ;
TBCCR1_ISR  add.w   #8,&TBCCR1              ; Offset until next interrupt
            jmp     TBX_ISR                 ;
            reti                            ; Return ISR		 
                                            ;            
TBCCR2_ISR  add.w   #16,&TBCCR2             ; Offset until next interrupt
            jmp     TBX_ISR                 ;
            reti                            ; Return ISR		 
                                            ;
TBCCR3_ISR  add.w   #32,&TBCCR3             ; Offset until next interrupt
            jmp     TBX_ISR                 ;
            reti                            ; Return ISR		 
                                            ;
TBCCR4_ISR  add.w   #100,&TBCCR4            ; Offset until next interrupt
            jmp     TBX_ISR                 ;
            reti                            ; Return ISR		 
                                            ;            
TBCCR5_ISR  add.w   #200,&TBCCR5            ; Offset until next interrupt
            jmp     TBX_ISR                 ;
            reti                            ; Return ISR		 
                                            ;
TBCCR6_ISR  add.w   #400,&TBCCR6            ; Offset until next interrupt
            jmp     TBX_ISR                 ;
            reti                            ; Return ISR		 
                                            ;
;------------------------------------------------------------------------------ 
;           Interrupt Vectors Used MSP430x13x/14x          
;------------------------------------------------------------------------------ 
            ORG     0FFFEh                  ; MSP430 RESET Vector
            DW      RESET                   ; 
            ORG     0FFFAh                  ; Timer_B0 Vector
            DW      TB0_ISR                 ; 
            ORG     0FFF8h                  ; Timer_BX Vector
            DW      TBX_ISR                 ; 
            END
