; THIS PROGRAM IS PROVIDED "AS IS". TI MAKES NO WARRANTIES OR
; REPRESENTATIONS, EITHER EXPRESS, IMPLIED OR STATUTORY, 
; INCLUDING ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS 
; FOR A PARTICULAR PURPOSE, LACK OF VIRUSES, ACCURACY OR 
; COMPLETENESS OF RESPONSES, RESULTS AND LACK OF NEGLIGENCE. 
; TI DISCLAIMS ANY WARRANTY OF TITLE, QUIET ENJOYMENT, QUIET 
; POSSESSION, AND NON-INFRINGEMENT OF ANY THIRD PARTY 
; INTELLECTUAL PROPERTY RIGHTS WITH REGARD TO THE PROGRAM OR 
; YOUR USE OF THE PROGRAM.
;
; IN NO EVENT SHALL TI BE LIABLE FOR ANY SPECIAL, INCIDENTAL, 
; CONSEQUENTIAL OR INDIRECT DAMAGES, HOWEVER CAUSED, ON ANY 
; THEORY OF LIABILITY AND WHETHER OR NOT TI HAS BEEN ADVISED 
; OF THE POSSIBILITY OF SUCH DAMAGES, ARISING IN ANY WAY OUT 
; OF THIS AGREEMENT, THE PROGRAM, OR YOUR USE OF THE PROGRAM. 
; EXCLUDED DAMAGES INCLUDE, BUT ARE NOT LIMITED TO, COST OF 
; REMOVAL OR REINSTALLATION, COMPUTER TIME, LABOR COSTS, LOSS 
; OF GOODWILL, LOSS OF PROFITS, LOSS OF SAVINGS, OR LOSS OF 
; USE OR INTERRUPTION OF BUSINESS. IN NO EVENT WILL TI'S 
; AGGREGATE LIABILITY UNDER THIS AGREEMENT OR ARISING OUT OF 
; YOUR USE OF THE PROGRAM EXCEED FIVE HUNDRED DOLLARS 
; (U.S.$500).
;
; Unless otherwise stated, the Program written and copyrighted 
; by Texas Instruments is distributed as "freeware".  You may, 
; only under TI's copyright in the Program, use and modify the 
; Program without any charge or restriction.  You may 
; distribute to third parties, provided that you transfer a 
; copy of this license to the third party and the third party 
; agrees to these terms by its first use of the Program. You 
; must reproduce the copyright notice and any other legend of 
; ownership on each copy or partial copy, of the Program.
;
; You acknowledge and agree that the Program contains 
; copyrighted material, trade secrets and other TI proprietary 
; information and is protected by copyright laws, 
; international copyright treaties, and trade secret laws, as 
; well as other intellectual property laws.  To protect TI's 
; rights in the Program, you agree not to decompile, reverse 
; engineer, disassemble or otherwise translate any object code 
; versions of the Program to a human-readable form.  You agree 
; that in no event will you alter, remove or destroy any 
; copyright notice included in the Program.  TI reserves all 
; rights not specifically granted under this license. Except 
; as specifically provided herein, nothing in this agreement 
; shall be construed as conferring by implication, estoppel, 
; or otherwise, upon you, any license or other right under any 
; TI patents, copyrights or trade secrets.
;
; You may not use the Program in non-TI devices.

;*******************************************************************************
;       
;       RTC USING THE WATCHDOG TIMER
;
;	Description:  This program demonstrates a real time clock with hours
;	minutes and seconds.  The Mainloop immediately puts the MSP430 into LPM3.
;	The Watchdog timer Interrupts at exactly 1-second intervals and returns the 
;	MSP430 to active mode which allows the program to complete 
;	the Mainloop.  Mainloop calls a Clock routine then returns to LPM3.
;
;       This program is written specifically for the MSP430F1121, but could
;       easily be adapted for any other MSP430 device. 
;
;	Three registers are used to store the seconds, minutes
;	and hour values.
;	 
;
;*******************************************************************************
;
#include        "msp430x11x1.h"         ; include std defs

;		RTC variables
#define         SEC     R13            
#define         MIN     R14            
#define         HR      R15            


;----------------------------------------------------------------------------- 
;               Program RESET
                RSEG    CODE
;----------------------------------------------------------------------------- 

RESET           MOV     #02FEh,SP       ; Initialize stackpointer
                CALL    #Setup          ; Prepare LCD and basic timmer

                ; Mainloop
Mainloop        BIS     #LPM3,SR        ; Set SR bits for LPM3
                CALL    #Clock          ; Update Clock
                JMP     Mainloop        ; Endless Loop

;----------------------------------------------------------------------------- 
;       Clock: Update clock SEC and MIN and HR
;
;       Originally written by Lutz Bierl.
;
;       This is the routine that counts the hours, minutes and seconds.
;       It can be used with any counter peripheral on any MSP430 device,
;       as long as it is executed once per second.
;
;       This particular routine is very basic.  It only counts minutes, seconds
;       and hours as BCD numbers.  Hex values could also be used if desired.
;
;       Refer to the MSP430 Application Report Book for more extensive clock
;       routines that include counters for years, adjustments for leap years, etc.
;
;----------------------------------------------------------------------------- 

Clock           SETC                    ; Set Carry bit.
                DADC.b  SEC             ; Increment seconds decimally
                CMP.b   #060h,SEC       ; One minute elapsed?
                JLO     Clockend        ; No, return
                CLR.b   SEC             ; Yes, clear seconds
                DADC.b  MIN             ; Increment minutes decimally
                CMP.b   #060h,MIN       ; Sixty minutes elapsed?
                JLO     Clockend        ; No, return
                CLR.b   MIN             ; yes, clear minutes
                DADC.b  HR              ; Increment Hours decimally
                CMP.b   #024h,HR        ; 24 hours elapsed?
                JLO     Clockend        ; No, return
                CLR.b   HR              ; yes, clear hours	
Clockend        MOV.b   SEC,&P1OUT
                MOV.b   MIN,&P2OUT
                RET                     ;

;----------------------------------------------------------------------------- 
;	 Setup: Configure Modules and Control Registers 
;----------------------------------------------------------------------------- 
Setup           BIS.b   #BIT0,&IE1                    ; Enable Watchdog Int.                        
                MOV     #WDTPW+WDTTMSEL+WDTCNTCL+WDTSSEL,&WDTCTL  
                                                      ; Stop Watchdog Timer
                                                      ; Set in interval
                                                      ; timer mode and set 
                                                      ; interrupt interval
                                                      ; 1s with ACLK.

                MOV.b   #0FFh,&P1DIR
                MOV.b   #0FFh,&P2DIR
                MOV.b   #00,&P1OUT
                MOV.b   #00,&P2OUT

ClearRTC        MOV.b   #00h,SEC                ; Clear SEC
                MOV.b   #00h,MIN                ; Clear MIN
                MOV.b   #00h,HR                 ; Clear HR
                EINT                            ; Enable interrupts
		RET                             ; Done with setup.
;----------------------------------------------------------------------------- 
;       Watchdog ISR:
;       CPU is simply returned to active state on RETI by manipulated the SR
;       bits in the SR value that was pushed onto the stack.
;       Interrupt flag is reset automatically
;----------------------------------------------------------------------------- 

WDINT           BIC     #LPM3,0(SP)             ; Clear SR LPM3 Bits, on top of stack
                RETI                            ;

;----------------------------------------------------------------------------- 
	        RSEG    INTVEC                  ; Interrupt vectors
;----------------------------------------------------------------------------- 
                DW      RESET                   ;
                DW      RESET                   ; 
                DW      RESET                   ;
                DW      RESET                   ;
                DW      RESET                   ;
                DW      RESET                   ;
                DW      RESET                   ;
                DW      RESET                   ;
                DW      RESET                   ;
                DW      RESET                   ; Timer_A (CCIFG0)
                DW      WDINT                   ; Watchdog Timer
                DW      RESET                   ;
                DW      RESET                   ;
                DW      RESET                   ;
                DW      RESET                   ; NMI, Osc. fault
                DW      RESET                   ; POR, ext. Reset, Watchdog
                END               
