#INCLUDE
CBLOCK 20H
SEC ; Second
MIN ; Minute
HOU ; Hour
DATE ; Date
MON ; Month
DAY ; Day
YERR ; Year
DDD ; Write enable bit
TIME_TX ; 1302 transmit register
TIME_RX ; 1302 receive register
COUNT1 ;
COUNT2 ;
DELAY1
DELAY2
ENDC
** *****************************
; ; 1302 subroutine description
; ;****************** *******************
; DS1302INI ; 1302 initialization, disable RST first, set the charging mode
; SET_TIME ; Send 7 data at a time to set the time, including write enable register, 8 data
; GET_TIME; Receive 7 data at a time to display time
; TIME_WRITE_1; Write 1302 one byte at a time
; TIME_READ_1; Receive one byte of data 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 7 received data
#DEFINE TX_ADD 20H ; Start saving the 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 Write 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
Count seconds, you 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, and allow write enable
;********************************** *******************
DS1302_INI
BCF SCLK
BCF RST ;Disable
MOVLW B'10001110' first;Write enable register
MOVWF TIME_TX
BSF RST
CALL TIME_WRITE_1
MOVLW B'0';Allow write enable Can
MOVWF TIME_TX
CALL TIME_WRITE_1; send one byte of data
BCF RST
;Set charging mode
RETURN
is not set
;****************************************************** *
; SET_TIME
; Function: input 7 data at a time to set the time at one time, and enable writing the register at the same time
; for burst mode writing, 8 data must be written, not 7
; INPUT: 8 to be set The data has 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 ;Start 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 the 7 time data in the 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 ;Save the starting address of the 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
; Function: send one byte of data at a time, used to send 1302
; control word or send one data
; INPUT: TIME_TX
;************************************************
TIME_WRITE_1
MOVLW D'8'
MOVWF COUNT1
TLOP
BCF I_O ;The initial value is low level
BCF SCLK
BTFSC TIME_TX,0
BSF I_O
RRF TIME_TX,F
BSF SCLK ;High level sends data
DECFSZ COUNT1,F
GOTO TLOP
BCF SCLK ;Recover
RETURN
;* ***********************************************
; TIME_READ_1
; Function: Read 1302 one byte of data
; INPUT: NONE
; OUTPUT: The received data is stored in TIME_RX
; Principle: Receive data on the falling edge of SCLK, low byte first, according to
the level of I_O; Right shift with carry 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 ;Read falling edge
BCF STATUS,C
BTFSC I_O
BSF STATUS,C
RRF TIME_RX,F
BSF SCLK;Restore high level
DECFSZ COUNT1,F
GOTO TR1
BANK1
BCF TRISC,7;OUTPUT
BANK0
BCF SCLK;Restore
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 0 5H
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
Previous article:Tips on designing gas stove pulse ignition controller based on PICl6C711
Next article:Stepper Motor Control System Based on PIC16F873 Single Chip Microcomputer
Recommended ReadingLatest update time:2024-11-16 17:43
- Popular Resources
- Popular amplifiers
- 100 Examples of Microcontroller C Language Applications (with CD-ROM, 3rd Edition) (Wang Huiliang, Wang Dongfeng, Dong Guanqiang)
- Principles and Applications of Single Chip Microcomputers and C51 Programming (3rd Edition) (Xie Weicheng, Yang Jiaguo)
- Teach you to learn 51 single chip microcomputer-C language version (Second Edition) (Song Xuefeng)
- ATmega16 MCU C language programming classic example (Chen Zhongping)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications