;------------------------------------------
; Experiment lab no.7 INCLUDED FILE
;------------------------------------------
;
; Initialize DS1202 RTC with parameters
;
RTC_INIT:
        MOV     R2,#8EH         ;write WRITE PROTECT command
        MOV     R3,#00H         ;with zero bits
        LCALL   RTC_WRITE_CH
        MOV     R2,#80H         ;write SECONDS command
        MOV     R3,#05H         ;with value of 05 (BCD)
        LCALL   RTC_WRITE_CH
        MOV     R2,#82H         ;write MINUTE command
        MOV     R3,#55H         ;with value of 55 (BCD)
        LCALL   RTC_WRITE_CH
        MOV     R2,#84H         ;write HOUR command
        MOV     R3,#23H         ;with value of 23 (BCD)
        LCALL   RTC_WRITE_CH
        MOV     R2,#8EH         ;write WRITE PROTECT command
        MOV     R3,#80H         ;to restore write protect
        LCALL   RTC_WRITE_CH
        RET
;
; Read datas from RTC and shown on LED display
;
RTC_DISPLAY:
        MOV     R2,#81H         ;read SECONDS
        LCALL   RTC_READ_CH
        MOV     HEXBUF+2,R3
        MOV     R2,#83H         ;read MINUTE
        LCALL   RTC_READ_CH
        MOV     HEXBUF+1,R3
        MOV     R2,#85H         ;read HOUR
        LCALL   RTC_READ_CH
        MOV     HEXBUF+0,R3
        LCALL   HTOS
        MOV     R2,#10
        LCALL   SCAND
        RET
;
; Write Command and parameter byte to DS1202 RTC
; IN: R2 = COMMAND, R3 = DATA
;
RTC_WRITE_CH:
        CLR     RTC_CLK         ;low CLK line
        LCALL   DELAY
        SETB    RTC_RST         ;high RST line
        LCALL   DELAY
        MOV     A,R2            ;write COMMAND byte
        LCALL   RTC_WRITE_8BIT
        MOV     A,R3            ;and DATA byte
        LCALL   RTC_WRITE_8BIT
        CLR     RTC_RST         ;low RST line
        LCALL   DELAY
        RET
;
; Transfer 8-bits data to DS1202
; IN: A = data
;
RTC_WRITE_8BIT:
        MOV     R4,#08H         ;8 bits to transfer
WR8BIT1:
        RRC     A
        MOV     RTC_DATA,C
        SETB    RTC_CLK         ;Rising edge clock
        LCALL   DELAY
        CLR     RTC_CLK
        LCALL   DELAY
        DJNZ    R4,WR8BIT1
        RET
;
; Perform READ Command and read parameter byte from DS1202 RTC
; IN:  R2 = COMMAND
; OUT: R3 = DATA
;
RTC_READ_CH:
        CLR     RTC_CLK         ;low clk line
        LCALL   DELAY
        SETB    RTC_RST         ;high RST line
        LCALL   DELAY
        MOV     A,R2            ;write COMMAND byte
        LCALL   RTC_WRITE_8BIT
;
        MOV     R4,#8           ;then read DATA byte
        CLR     A
RD_CH1: CLR     RTC_CLK
        LCALL   DELAY
        MOV     C,RTC_DATA
        RRC     A
        SETB    RTC_CLK
        LCALL   DELAY
        DJNZ    R4,RD_CH1
        MOV     R3,A
;
        CLR     RTC_RST          ;low RST line
        LCALL   DELAY
        RET
;
; Delay
;
DELAY:
        NOP        ;delay
        RET
;
