PIC microcontroller controls clock chip DS1302 assembler

#INCLUDE
CBLOCK 20H
SEC; sec
MIN ; points
HOU; hours
DATE ; date
MON ; month
DAY ; day
YERR ; year
DDD ; write enable bit
TIME_TX ; 1302 send register
TIME_RX ; 1302 receive register
COUNT1 ;
COUNT2;
DELAY1
DELAY2
ENDC
********************************
; ; 1302 subroutine description
; ;********************************
; DS1302INI ; 1302 initialization, first disable RST, set the charging mode
; SET_TIME ; send 7 data set time at a time, including write enable register, 8 data
; GET_TIME ; Receive 7 data Display time at a time
; TIME_WRITE_1; write 1302 bytes at a time
; TIME_READ_1 ; Receive data one byte at a time
; ;******* 1302 Definition **************
; ;********************************
#DEFINE I_O PORTC,7 ;1302I_O
#DEFINE SCLK PORTC, 6 ; 1302 clock
#DEFINE RST PORTA,0 ;1302 enable bit
#DEFINE RX_ADD 40H ; save the accepted 7 data
#DEFINE TX_ADD 20H; start saving 7 time data to be written
BANK1 MACRO
BSF STATUS,RP0
ENDM
BANK0 MACRO
BCF STATUS,RP0
ENDM

ORG 000H
NOP
GOTO MAIN
MAIN

CALL CLR_RAM Clear RAN
CALL PORT_INI port definition
CALL DS1302_INI
CALL SET_TIME writes 7 time data at a time
CALL GET_TIME ; read back 7 time data at a time
GOTO $

;; Write seconds, you can write any 7 time data, single byte
MOVLW B’10000000′
MOVWF TIME_TX
BSF RST
CALL TIME_WRITE_1
MOVLW B’00100101′
MOVWF TIME_TX
CALL TIME_WRITE_1
BCF RST

Countdown, can read any 7 times, single byte

MOVLW B’10000001′
MOVWF TIME_TX
BSF RST
CALL TIME_WRITE_1
CALL TIME_READ_1
BCF RST

GOTO $

: subroutine

****************************************************
; DS1302_INI
; Function: Initialize 1302, disable RST first, allow write enable
;************************************************ *
DS1302_INI
BCF SCLK
BCF RST ; disable first
MOVLW B’10001110′ ; write enable register
MOVWF TIME_TX
BSF RST
CALL TIME_WRITE_1
MOVLW B’0′ ; allow write enable
MOVWF TIME_TX
CALL TIME_WRITE_1; send one byte of data
BCF RST

;Set the charging method

not set
RETURN

;************************************************ *
; SET_TIME
; Function: Input 7 data at a time to set the time at one time, and enable the write register at the same time
; Burst mode write, must write 8 data, not 7
; INPUT: The 8 data to be set have been written into the 8 bytes starting from TIME_TX_ADD
;************************************************ *
SET_TIME
MOVLW B’10111110′ ; flag bit
MOVWF TIME_TX
BSF RST
CALL TIME_WRITE_1
;TIME_WRITE_8 ;Send 8 data
MOVLW TIME_TX_ADD ; The starting address of sending data
MOVWF FSR
MOVLW D’8′
MOVWF COUNT2
TW1
MOVF INDF,W
MOVWF TIME_TX
CALL TIME_WRITE_1
INCF FSR,F
DECFSZ COUNT2,F
GOTO TW1
BCF RST
RETURN
;************************************************
; GET_TIME
; Function: accept 7 bytes of time data at a time, read in burst mode
; INPUT:NONE
; OUTPUT: Save 7 time data in 7 registers starting from TIME_RX_ADD
;************************************************
GET_TIME
MOVLW B’10111111′
MOVWF TIME_TX
BSF RST
CALL TIME_WRITE_1
;TIME_READ_7 ;Receive 7 data
MOVLW TIME_RX_ADD ; start address for saving data
MOVWF FSR
MOVLW D’7′
MOVWF COUNT2
TR2
CALL TIME_READ_1
MOVF TIME_RX,W
MOVWF INDF
INCF FSR,F
DECFSZ COUNT2,F
GOTO TR2
BCF RST
RETURN
;************************************************
; TIME_WRITE_1
; Role: send one byte of data at a time for sending 1302
; control word or send a data
; INPUT: TIME_TX
;************************************************
TIME_WRITE_1
MOVLW D’8′
MOVWF COUNT1
TLOP
BCF I_O ; initial value is low level
BCF SCLK
BTFSC TIME_TX,0
BSF I_O
RRF TIME_TX,F
BSF SCLK ; high level send data
DECFSZ COUNT1,F
GOTO TLOP
BCF SCLK ; resume
RETURN
;************************************************
; TIME_READ_1
; Function: read 1302 bytes of data
; INPUT: NONE
; OUTPUT: The received data is stored in TIME_RX
; Principle: Receive data on the falling edge of SCLK, the low byte is first, according to the I_O
; The level of the level is shifted right with and without carry.
;************************************************
TIME_READ_1
BANK1
BSF TRISC,7 ;I_O IS INPUT
BANK0
MOVLW D’8′
MOVWF COUNT1 ; read 8 bytes
TR1
BCF SCLK ; falling edge read
BCF STATUS,C
BTFSC I_O
BSF STATUS,C
RRF TIME_RX,F
BSF SCLK ; resume high
DECFSZ COUNT1,F
GOTO TR1
BANK1
BCF TRISC,7 ;OUTPUT
BANK0
BCF SCLK ; resume
RETURN
NOP

PORT_INI
BANK1
MOVLW B’00000000′
MOVWF TRISC
MOVLW B’0′
MOVWF TRISB
MOVLW B’00000111′
MOVWF ADCON1
MOVLW B’00000000′
MOVWF TRISA
BANK0
MOVLW 18H
MOVWF 20H
MOVLW 19H
MOVWF 21H
MOVLW 05H
MOVWF 22H
MOVLW 13H
MOVWF 23H
MOVLW 14H
MOVWF 24H
MOVLW 15H
MOVWF 25H
MOVLW 16H
MOVWF 26H
RETURN
CLR_RAM
MOVLW 20H
MOVWF FSR
LOOP
CLRF INDF
INCF FSR,F
BTFSS FSR,7
GOTO LOOP ; clear 32
RETURN
END

The Links:   EP2SGX60EF1152C4N CM800HA-34H