PIC microcontroller control clock chip DS1302 assembly program

Publisher:ShuxiangLatest update time:2011-08-12 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

#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

Reference address:PIC microcontroller control clock chip DS1302 assembly program

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

A modified 3-wire SPI bus implementation (for DS1302 clock chip)
  As the title says, this is the so-called abnormal version of the 3-wire SPI bus on the Internet: one clock line, one enable line, and one bidirectional IO line.   One module, two files: //spi3.c #include "typedef.h" #include "spi3.h" /*********************************************************************** Name
[Microcontroller]
DS1302 Hardware Information
Whether it is a single-chip microcomputer or some other electronic devices we use in daily life, they can be divided into commercial grade and industrial grade according to the constraints of usage conditions. The main difference is the operating temperature range. The purchase information of DS1302 is shown in Figure
[Microcontroller]
DS1302 Hardware Information
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components
Guess you like

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号