#include    "msp430x41x.h" ;  MSP430x41x Standard Definitions
;*******************************************************************************
;  MSP-FET430P410 Demo - LCD, displays a real time clock on a static LCD
;
;  Description "FET410_RTCwLCD": This program maintains a real time clock and
;  displays the current time on a 3.5 digit static LCD. The colon between the 
;  minutes and seconds toggles on or off each second to indicate that the clock
;  is running.  The digits of the display are only rewritten each minute once
;  the elapsed seconds roll over from 60 back to 0. The fourth digit is 
;  automatically blank for hours 1-9. The software is totally interrupt driven.
;  It only comes out of low power mode 3 and executes code when a Basic Timer
;  interrupt occurs, once each second.
;   ACLK = LFXT1 = 32768, MCLK = SMCLK = TACLK = DCO = 32xACLK = 1.048576MHz 
;   //*An external watch crystal is required on XIN/XOUT for ACLK*//	  
;
;  
;                                 Connections MSP430 -> LCD
;                                 -------------------------
;
;                                T.I.               Varitronix  
;                           MSP430x41x MCU     3.5 digit, static LCD
;                                                 # VI-302-DP-S
;                           ---------------       --------------
;                          |          COM0 |-----|1,40 COM      |
;                          |          SEG0 |-----|21            |
;                          |          SEG1 |-----|20            |
;                          |          SEG2 |-----|19            |
;                          |          SEG3 |-----|18            |  
;                          |          SEG4 |-----|17            |
;                          |          SEG5 |-----|22            |
;                          |          SEG6 |-----|23            |
;                          |          SEG7 |-----|38 (low batt) |
;                          |          SEG8 |-----|25            | 
;                          |          SEG9 |-----|24            |
;                          |          SEG10|-----|15            |
;                          |          SEG11|-----|14            |
;                          |          SEG12|-----|13            | 
;                          |          SEG13|-----|26            |
;                          |          SEG14|-----|27            |
;                          |          SEG15|-----|28 (L)  COLON |
;                          |          SEG16|-----|30            | 
;                          |          SEG17|-----|29            |
;                          |          SEG18|-----|11            |
;                          |          SEG19|-----|10            |
;                       ---|R03       SEG20|-----|9             | 
;                      |   |          SEG21|-----|31            |
;                      |   |          SEG22|-----|32            |
;                     \|/  |          SEG23|-----|3 (B-C) =     |
;                     GND  |               |     | .5 digit ="1"|
;                           ---------------       --------------  
;
;                 NOTE: Pin R03 on the MSP430 must be connected to GND
;
;  B. Merritt
;  Texas Instruments Inc.
;  January 2002
;
;--------CPU register used------------------------------------------------------
;;;;;;;       R13, R14, R15                    ;used as temporary registers, can
                                               ;be reused 
                                               
;--------RAM bytes used for variables-------------------------------------------                                                
SEC           equ    0200h                     ;byte for counting seconds
MIN           equ    0201h                     ;byte for counting minutes
HOUR          equ    0202h                     ;byte for counting hours
COLON         equ    0203h                     ;temporary storage to flash colon

;--------RAM words used for variables------------------------------------------- 
LCDTEMP       equ    0204h                     ;temporary storage in LCD routine

;-------------------------------------------------------------------------------
              ORG    0E000h                    ;Program reset
;-------------------------------------------------------------------------------
RESET         mov    #300h,SP                  ;itialize stackpointer          
              call   #Setup                    ;configure MSP430 for application
              
Mainloop      bis    #LPM3,SR                  ;set SR bits to wait in LPM3

;------------------------------------------------------------------------------- 
;             Increment clock values every second 
;-------------------------------------------------------------------------------
Clock         setc                             ;entry every second
              dadc.b SEC                       ;increment seconds, add carry bit
              cmp.b  #060h,SEC                 ;one minute elapsed?
              jlo    End_CLK                   ;no, (C = 0) w/o LCD update
              clr.b  SEC                       ;yes, clear seconds (C = 1)
Clock1        dadc.b MIN                       ;increment minutes if set carry
              cmp.b  #060h,MIN                 ;sixty minutes elapsed?
              jlo    Dis_Clock                 ;no, jump to update time display
              clr.b  MIN                       ;yes, clear minutes
              dadc.b HOUR                      ;increment hour
              cmp.b  #013h,HOUR		       ;12 hours elapsed?	 
              jlo    Dis_Clock                 ;no, jump to update time display
              mov.b  #01h,HOUR                 ;yes, set hours back to 1 &
                                               ;fall through to display time
                                               
;-------------------------------------------------------------------------------
;             Display clock values contained in RAM Bytes MIN & HOUR. 
;             CPU Registers R15 and R13 used temporarily.
;-------------------------------------------------------------------------------
Dis_Clock     mov.b  #LCDM1,R15;               ;R15 points to first LCD location
              mov.b  MIN,R14                   ;minute BCD value to R14
              call   #Pass_Bytes               ;call routine to display minutes
              mov.b  HOUR,R14                  ;hour BCD value to R14
              call   #Pass_Bytes               ;call routine to display hours
End_CLK       ret                              ;return to Basic Timer ISR

Pass_Bytes    mov.b  R14,R13                   ;copy R14 to R13, so R14 saved
              call   #ToLCD                    ;call to display 1st digit
              mov.b  R14,R13                   ;recopy R14 to R13, so R14 saved
              rra.b  R13                       ;right shift R13 4 times to 
              rra.b  R13                       ;move 2nd nibble to lower 4 bits
              rra.b  R13                       ;
              rra.b  R13                       ;
              call   #ToLCD                    ;call to display 2nd digit
              ret                              ;return to Dis_Clock routine
               
;-------------------------------------------------------------------------------
;             Display 1 digit on static LCD. CPU Registers R15, R14 and R13
;             used temporarily
;-------------------------------------------------------------------------------
ToLCD         cmp    #09Dh,R15                 ;pointer incremented for 4th
                                               ;digit = 1/2 digit position?
              jeq    Halfdigit                 ;yes, jump to test/set leading 1       
              and    #000Fh,R13                ;blank upper 12 bits
              add    R13,R13                   ;double R13 since is word data
                                               ;where addresses = 2 bytes apart 
              add    R13,R13                   ;double R13 again since 2 words
                                               ;per digit in static mode
              mov    LCD_Tab(R13),LCDTEMP      ;low word into temp memory
              mov.b  LCDTEMP,0(R15)            ;low byte of low word to LCD
              swpb   LCDTEMP                   ;swap bytes in temp memory
              mov.b  LCDTEMP,1(R15)            ;high byte of low word to LCD
              incd   R13                       ;double increment to high word of
                                               ;digit
              mov    LCD_Tab(R13),LCDTEMP      ;high word into temp memory                                    
              mov.b  LCDTEMP,2(R15)            ;low byte of high word to LCD
              swpb   LCDTEMP                   ;swap bytes in temp memory
              mov.b  LCDTEMP,3(R15)            ;high byte of high word to LCD
              add    #4,R15                    ;increase R15 (LCD mem pointer) 
                                               ;by 4 since 4 bytes per digit
EndLCD        ret                              ;return to Pass_Bytes routine

;-------------------------------------------------------------------------------
;            Test if 1 is to be displayed in 1/2 digit position and display it
;            If leading zero then nothing is displayed
;-------------------------------------------------------------------------------
Halfdigit     cmp.b  #1,R13                    ;digit to display = 1?
              jne    EndHalf                   ;no, jump to not display digit
              bis.b  #10h,-1(R15)              ;yes, set h bit in 12th byte(0-11)
                                               ; = 1/2 digit (leading 1) bit
EndHalf       mov.b  #LCDM1,R15                ;set pointer to start of LCD mem.
              ret                              ;return to Pass_Bytes routine

;-------------------------------------------------------------------------------
Setup         ;Configure modules and control registers 
;-------------------------------------------------------------------------------
StopWDT       mov    #WDTPW+WDTHOLD,&WDTCTL    ;stop Watchdog Timer
SetupFLL2     bis.b  #XCAP18PF,&FLL_CTL0       ;set load capacitance for xtal
              mov    #10000,R15
FLL_Wait      dec    R15                       ;delay for FLL to lock
              jnz    FLL_Wait		 
SetupLCD      mov.b  #065h,&LCDCTL             ;static LCD, segments = 0 - 23
SetupBT       mov.b  #BT_ADLY_1000,&BTCTL      ;Basic Timer interrupt = 1 second
              bis.b  #BTFRFQ1+BTFRFQ0,&BTCTL   ;set fLCD = ACLK / 256
              mov.b  #BTIE,&IE2                ;enable Basic Timer interrupt
SetupPorts    mov.b  #0FFh,&P1DIR              ;set port to outputs
SetupPx       mov.b  #0FFh,&P2DIR              ;set port to outputs
              mov.b  #0FFh,&P3DIR              ;set port to outputs
              mov.b  #0FFh,&P4DIR              ;set port to outputs
              mov.b  #0FFh,&P5DIR              ;set port to outputs
              mov.b  #0FFh,&P6DIR              ;set port to outputs
              
ClearRAM      bis.b  #10h,COLON                ;set bit used to flash colon
              mov.b  #59h,SEC                  ;preload SEC        # rolls over
              mov.b  #59h,MIN                  ;preload MIN        # to 12:00 @     
              mov.b  #11h,HOUR                 ;preload HOUR       # 1st second
ClearLCD      mov    #15,R15                   ;15 LCD memory bytes to clear
Clear1        mov.b  #0,LCDM1(R15)             ;write zeros in LCD RAM locations
                                               ;so clear display
              dec    R15                       ;all LCD mem clear?
              jc     Clear1                    ;more LCD mem to clear, use JC
                                               ;to get memory location 0
              eint                             ;enable interrupts
              ret          
              
;-------------------------------------------------------------------------------
;             Basic Timer causes 1 second interrupt. Mode bits changed on stack
;             so CPU is returned in active upon return from the interrupt
;-------------------------------------------------------------------------------
BT_ISR        call   #Clock                    ;update hours and minutes each
                                               ;second even if not displayed
              xor.b  COLON,&LCDM8              ;blink ":" between min and hour
              reti
              		 
;-------------------------------------------------------------------------------
;             Define characters for VI-302 3.5 digit static LCD
;-------------------------------------------------------------------------------
a             equ    0001h
b             equ    0010h
c             equ    0100h
d             equ    1000h
e             equ    0001h
f             equ    0010h 
g             equ    0100h                     
h             equ    1000h                     ;decimal point or special
                                               ;character

              EVEN                             ;align words on even boundry
LCD_Tab       DW     a+b+c+d                   ;displays "0" low word
              DW     e+f                       ;displays "0" high word 
              DW     b+c                       ;displays "1" low word
              DW     0                         ;displays "1" high word (empty) 
              DW     a+b+d                     ;displays "2" low word
              DW     e+g                       ;displays "2" high word              
              DW     a+b+c+d                   ;displays "3" low word
              DW     g                         ;displays "3" high word              
              DW     b+c                       ;displays "4" low word
              DW     f+g                       ;displays "4" high word              
              DW     a+c+d                     ;displays "5" low word
              DW     f+g                       ;displays "5" high word              
              DW     a+c+d                     ;displays "6" low word
              DW     e+f+g                     ;displays "6" high word              
              DW     a+b+c                     ;displays "7" low word
              DW     0                         ;displays "7" high word (empty)             
              DW     a+b+c+d                   ;displays "8" low word
              DW     e+f+g                     ;displays "8" high word              
              DW     a+b+c+d                   ;displays "9" low word
              DW     f+g                       ;displays "9" high word              

;-------------------------------------------------------------------------------
;             41x Interrupt vectors
;-------------------------------------------------------------------------------
              ORG    0FFFEh                    ;MSP430 RESET Vector
              DW     RESET
              ORG    0FFE0h                    ;Basic Timer Vector
              DW     BT_ISR                                         
              END     

