#include  "msp430x20x3.h"
SCK   EQU   BIT4
SDA   EQU   BIT5
ENA   EQU   BIT6
ave   EQU   0x200
;------------------------------------------------------------------------------
            ORG     0x0F800                 ; rogam Start (2K Flash device)
;------------------------------------------------------------------------------
RESET       mov.w   #0x280, SP              ; initialize stackpointer
            mov.w   #WDTPW+WDTHOLD, &WDTCTL ; stop WDT

            clr.b   &DCOCTL                 ; select lowest DCO & MOD settings
            mov.b   &CALBC1_1MHZ, &BCSCTL1  ; calibrated range for DCO
            mov.b   &CALDCO_1MHZ, &DCOCTL   ; calibrated tap and mod

            mov.b   #0x40, &P1OUT           ; P1.4 - P1.6 are outputs
            mov.b   #0xF3, &P1DIR
            mov.b   #8, &P1SEL              ; enable pin for output capacitor
            
            clr.b   &P2SEL                  ; use crystal pins as outputs
            clr.b   &P2OUT
            bis.b   #BIT7+BIT6, &P2DIR
            
            mov.w   #SD16REFON+SD16SSEL_1, &SD16CTL      ; 1.2V ref, SMCLK
            mov.w   #SD16DF+SD16IE+SD16SNGL, &SD16CCTL0  ; unipolar, single, IE
            mov.b   #SD16INCH_1, &SD16INCTL0             ; A1+/-
            mov.b   #SD16AE2, &SD16AE       ; P1.2 = A1+, A1- = VSS

            call    #init_MAX6950
  
loop        call    #delay                  ; 0.25sec delay
            call    #delay
            
            mov.w   #256, R15
            clr.w   &ave
            clr.w   &ave+2
ave_loop    bis.w   #SD16SC,&SD16CCTL0      ; start conversion
            bis.w   #CPUOFF+GIE, SR         ; CPU off, enable interrupts
            add.w   R4, &ave
            addc.w  #0, &ave+2
            dec.w   R15
            jnz     ave_loop
            
            mov.b   #10, R15
div_loop    bic.w   #BITC, SR
            rrc.w   &ave+2
            rrc.w   &ave
            dec.b   R15
            jnz     div_loop
            
            jnc     $+6
            inc.w   &ave
            mov.w   &ave, R4
                       
            call    #display         				
            jmp     loop
            
;------------------------------------------------------------------------------
init_MAX6950
            mov.w   #0x10F, R10             ; enable decoding for all digits
	    call    #write_MAX6950          
 
            mov.w   #0x201, R10             ; initial intensity 1/16			
	    call    #write_MAX6950          

            mov.w   #0x303, R10             ; scan 4 digits				
	    call    #write_MAX6950
	
            mov.w   #0x421, R10             ; no blinking, normal mode, reset			
	    call    #write_MAX6950
	    ret

write_MAX6950				    ; write a 16-bit word to MAX6950
	    mov.b   #16, R15		    ; bit counter
	    bic.b   #SCK, &P1OUT	    ; make sure that SCK=0
	    bic.b   #ENA, &P1OUT	    ; enable the device

w_loop      rlc.w   R10			    ; get the leftmost data bit
	    jc      $+8
	    bic.b   #SDA, &P1OUT            ; set 0 on data line
	    jnc     $+8
	    bis.b   #SDA, &P1OUT	    ; set 1 on data line
	    bis.b   #SCK, &P1OUT	    ; raise clock up
	    nop
	    bic.b   #SCK, &P1OUT	    ; ... and low
	    dec.b   R15
	    jnz	    w_loop		    ; repeat this 8 times

	    bis.b   #ENA, &P1OUT	    ; latch in the data and exit
	    ret	

display     mov.w   R4, R10		    ; display ADC data in R4
            swpb    R10
            rra.b   R10
            rra.b   R10
            rra.b   R10
            rra.b   R10
            and.w   #0x0F, R10
            add.w   #0x6300, R10            ; digit 3 address
            call    #write_MAX6950

            mov.w   R4, R10
            swpb    R10
            and.w   #0x0F, R10
            add.w   #0x6200, R10            ; digit 2 address
            call    #write_MAX6950

            mov.w   R4, R10		   
            rra.b   R10
            rra.b   R10
            rra.b   R10
            rra.b   R10
            and.w   #0x0F, R10
            add.w   #0x6100, R10            ; digit 1 address
            call    #write_MAX6950

            mov.w   R4, R10		   
            and.w   #0x0F, R10
            add.w   #0x6000, R10            ; digit 0 address
            call    #write_MAX6950
  	    ret

delay       clr.w   R15                     ; 0.25sec delay
            dec.w   R15
            nop
            jnz     $-4
            ret
;------------------------------------------------------------------------------
SD16_ISR    mov.w   &SD16MEM0, R4           ; get ADVC value, clear IF
            bic.w   #CPUOFF, 0(SP)          ; CPU on, enable interrupts
            reti                           
;------------------------------------------------------------------------------
;           Interrupt Vectors
;------------------------------------------------------------------------------
            ORG     0x0FFEA                  ; SDR_16A Vector
            DW      SD16_ISR

            ORG     0x0FFFE                  ; MSP430 RESET Vector
            DW      RESET                   

            END
