#include  "msp430x11x1.h"
;******************************************************************************
;   MSP-FET430x110 Demo - Comp_A Slope ADC to Detect Temp Level Set P1.0 > 25c  
;
;   Description: Comparator_A is used to detect a resistance threashold.  
;   Discharge times of a 0.1uf capacitor through a 10k-NTC (25c) and 10k ohm 
;   reference resistor are compared.  If the NTC discharge time is is lower
;   than the 10k referece, P1.0 is set.  If the NTC discharge time is higher
;   than refernce, P1.0 is reset.  The LED is ON if the temperature is greater
;   than 25c. 
;   ACLK = n/a, MCLK = SMCLK = default DCO ~ 800k 
;
;                MSP430F1121
;             -----------------
;        /|\ |              XIN|-  
;         |  |                 |
;         ---|RST          XOUT|-
;            |                 | 
;      +-10k-|P2.0             |
;      |     |                 |	
;      +-NTC-|P2.1             |
;      |     |                 |
;      +-----|P2.3         P1.0|-->LED
;      |     |                 |
;     ===.1uf|                 |
;      |     |                 |			
;      ------|VSS
;
;            NTC = 10k @25c, (P/N 271-110A Radio Shack)
;
Ref         equ    001h         ; P2.0 = Reference 
Sensor      equ    002h         ; P2.1 = Sensor 
;
;   M.Buccini
;   Texas Instruments, Inc
;   January 2002
;******************************************************************************
;------------------------------------------------------------------------------ 
            ORG     0F000h                  ; Program Start
;------------------------------------------------------------------------------ 
RESET       mov.w   #300h,SP                ; Initialize stackpointer
Init_Sys    mov.w   #WDTPW+WDTHOLD,&WDTCTL  ; Stop WDT
SetupP1     bic.b   #001h,&P1OUT            ; P1.0 reset
            bis.b   #001h,&P1DIR            ; P1.0 output direction
SetupP2     bic.b   #Sensor+Ref,&P2OUT      ; Sensor, Ref reset
            bis.b   #Sensor+Ref,&P2DIR      ; Sensor, Ref output
SetupCA     mov.b   #CARSEL+CAREF0+CAON,&CACTL1   ; -Comp = 0.25*Vcc -  on
            mov.b   #P2CA0,&CACTL2                ; +Comp = P2.3 
SetupTA     mov.w   #TASSEL1+TACLR,&TACTL   ; SMCLK, clear TAR
            eint                            ; Enable interrupts
                                            ;													  
Mainloop    mov.b   #Sensor,R14             ; R14 = discharge pin 
            call    #Measure                ;
            mov.w   R14,R15                 ; R15 = Sensor discharge time			  
            mov.b   #Ref,R14                ; R14 = discharge pin
            call    #Measure                ; 
            cmp.w   R14,R15                 ; R14 = Reference discharge time
            jlo     ON                      ; jump if R15 < R14 ( >25c )
;	    Display Modes                   ;
OFF         bic.b   #001h,&P1OUT            ; LED off - Reset P1.0 
            jmp     Mainloop                ;  
ON          bis.b   #001h,&P1OUT            ; LED on  - Set P1.0 
            jmp     Mainloop                ;  
                                            ;
;------------------------------------------------------------------------------ 
Measure;    Input:   R14 = Sensor or Ref P2.x bit location
;           Output:  R14 = Sensor or Ref discharge time
;------------------------------------------------------------------------------ 
Charge;     Capacitor is charged via Ref 
            bis.w   #MC1,&TACTL             ; Start timer_a continous mode
            bis.b   #Ref,&P2OUT             ; Ref Set
            bis.b   #Ref,&P2DIR             ; Ref output
            mov.w   &TAR,&CCR1              ; CCR1 = TAR
            add.w   #5000,&CCR1             ; CCR1 ~ TAR+5ms (5tu)
            mov.w   #CCIE,&CCTL1            ; Comp,interrupt
            bis.w   #LPM0,SR                ; Wait for CCR1 interrupt				 
            bic.b   #Ref,&P2DIR             ; Ref = HiZ, Charge complete 
            bic.b   #Ref,&P2OUT             ; Ref = Reset						   
Discharge;  Measure Discharge Time 
            mov.w   #CM1+CCIS0+CAP+CCIE,&CCTL1    ; Neg, CCIB,Cap,interrupt
            push    &TAR                    ; TOS = TAR at SOC
            bis.b   R14,&P2DIR              ; Temp = Sensor or REF 
            bis.w   #LPM0,SR                ; Wait for CCR1 interrupt				 
            mov.w   &CCR1,R14               ; R14 = TAR (CCR1) at EOC
            sub.w   @SP+,R14                ; R14 = discharge time
            bic.b   #Sensor+Ref,&P2DIR      ; Disable Sensor or Ref 
            clr.w   &CCTL1                  ; Disable CCTL1
            bic.w   #MC1,&TACTL             ; Stop timer_a , save power
            ret                             ;
                                            ;
;------------------------------------------------------------------------------ 
TAX_ISR;    Common ISR for CCR1-4 and overflow
;------------------------------------------------------------------------------ 
            add.w   &TAIV,PC                ; Add TA interrupt offset to PC
            reti                            ; CCR0 - no source
            jmp     CCR1_ISR                ; CCR1
;            reti                            ; CCR2
;            reti                            ; CCR3
;            reti                            ; CCR4
;TA_over     reti                            ; Timer_A overflow
                                            ;
CCR1_ISR    bic.w   #LPM0,0(SP)             ; Exit LPM0 on reti
            reti                            ;
                                            ;
;------------------------------------------------------------------------------ 
;           Interrupt Vectors Used MSP430x11x1         
;------------------------------------------------------------------------------ 
            ORG     0FFFEh                  ; MSP430 RESET Vector
            DW      RESET                   ; 
            ORG     0FFF0h                  ; Timer_AX Vector
            DW      TAX_ISR                 ; 
            END
