;-------------------------------------------
; REAL TIME CLOCK ISR SUBROUTINES
;
; LAST MODIFIED: 13.04.2002
;
; DISCLAIMER:
; This software is provided "as is" for evaluation of our MSP430-1121STK
; development board. You are free to use this code in your projects, but 
; without warranty of any kind. If you can't understand how and what the 
; code does it's better to start by learning the MSP430 datasheets, and 
; assembly language, but don't call OLIMEX with questions regarding this 
; software. OLIMEX will not teach you how to write assembly language nor 
; how to use MSP430 peripherials, there is plenty of datasheets about them.
;
; (C) 2002 OLIMEX Ltd, All rights reserved
;-------------------------------------------------------------------------

#define     LEAP0     0FFECh                /* RTC constants              */
#define     LEAP1     12h                   /* RTC constants              */                
#define     LEAP2     0h                    /* RTC constants              */
#define     TEMP      R11                   /* ISR REGISTER temporary vars  */
#define     SECONDS   R12                   /* ISR REGISTER R12: seconds:00 */
#define     HOURMIN   R13                   /* ISR REGISTER R13: hours:mins */
#define     MODAY     R14                   /* ISR REGISTER R14: day:month  */
#define     YEAR      R15                   /* ISR REGISTER R15: year       */

;--------------------------------------------
; RTC routines                              
;--------------------------------------------
DAYTAB      db      31h, 28h, 31h, 30h      ;day of the month table no leap year
            db      31h, 30h, 31h, 31h      ;
            db      30h, 31h, 30h, 31h      ;
;--------------------------------------------
; WDT interrupt routines                    
;--------------------------------------------
WDT_ISR
RTC         clrc                            ;time update each second
            dadd.w  #0100h,SECONDS          ;
            cmp.w   #6000h,SECONDS          ;
            jne     RTC_exit                ;
            clrc                            ;
            dadd.w  #4000h,SECONDS          ;
            dadc.w  HOURMIN                 ;
            cmp.b   #60h,HOURMIN            ;
            jne     RTC_exit                ;
            clrc                            ;
            dadd.w  #0040h,HOURMIN          ;
            cmp.w   #2400h,HOURMIN          ;
            jne     RTC_exit                ;
            clr     HOURMIN                 ;
Calendar                                    ;calendar update each day
            mov     MODAY,TEMP              ;
            swpb    TEMP                    ;
            bic.w   #0FF00h,TEMP            ;
            cmp.b   #10,TEMP                ;
            jlo     Cal_1                   ;
            sub.b   #6,TEMP                 ;
Cal_1       cmp.b   DAYTAB-1(TEMP),MODAY    ;
            jlo     Cal_4                   ;
            cmp.w   #0228h,MODAY            ;
            jne     Cal_3                   ;
            mov.w   YEAR,TEMP               ;
            bic.w   #LEAP0,TEMP             ;
            cmp.w   #LEAP1,TEMP             ;
            jeq     Cal_2                   ;
            cmp.w   #LEAP2,TEMP             ;
            jeq     Cal_2                   ;
            jmp     Cal_3                   ;
Cal_2       inc.w   MODAY                   ;
            jmp     Cal_exit                ;
Cal_3       clrc                            ;
            dadd.w  #0100h,MODAY            ;
            bic.w   #00FFh,MODAY            ;
Cal_4       clrc                            ;
            dadd.w  #1,MODAY                ;
            cmp.w   #1300h,MODAY            ;
            jlo     Cal_exit                ;
            mov.w   #0101h,MODAY            ;
            clrc                            ;
            dadd.w  #1,YEAR                 ;
Cal_exit                                    ;
RTC_exit                                    ;
            reti                            ;
