📄 ds1302.asm
字号:
;
;//*DS1302 PIN:
;//*
;//* SCLK (Serial Clock Input) - SCLK is used to synchronize data movement on
;//* the serial interface. This pin has a 40k. internal pull-down resistor.
p1302SCLK BIT P1.2
;//* I/O (Data Input/Output) - The I/O pin is the bi-directional data pin for
;//* the 3-wire interface. This pin has a 40k. internal pull-down resistor.
p1302IO BIT P1.3
;//* RST (Reset) - The reset signal must be asserted high during a read or a
;//* write. This pin has a 40k.internal pull-down resistor.
p1302RST BIT P1.4
;//* DS1302 Command code
SecondWCom EQU 0x80
SecondRDCom EQU 0x81
MinuteWCom EQU 0x82
MinuteRDCom EQU 0x83
HourWCom EQU 0x84
HourRDCom EQU 0x85
DateWCom EQU 0x86 ;//* How much day of a month
DateRDCom EQU 0x87
MonthWCom EQU 0x88
MonthRDCom EQU 0x89
DayWCom EQU 0x8A ;//* How much day of a week
DayRDCom EQU 0x8B
YearWCom EQU 0x8C
YearRDCom EQU 0x8D
;//*
ControlWCom EQU 0x8E ;//* enable only 7 bit, 7bit=0 may operate all register
;//* of DS1302
ControRDCom EQU 0x8F ;//* only 7bit availability, 0-6bit = 0
TrChaWCom EQU 0x90 ;//* This register controls the trickle charge
;//* characteristics of the DS1302.
TrChaRDCom EQU 0x91 ;//* The trickle charge select (TCS) bits (bits 4-7)
;//* control the selection of the trickle charger. In
;//* order to prevent accidental enabling, only a
;//* pattern of 1010 will enable the trickle charger.
CBurstWCom EQU 0xBE ;//* The clock/calendar command byte specifies burst
;//* mode operation. In this mode the first eight
CBurstRDCom EQU 0xBF ;//* clock/calendar registers can be consecutively read
;//* or written starting with bit 0 of address 0.
;//* DS1302 inside buffer command code(0-31)
DSBufWCom0 EQU 0xC0
DSBufRDCom0 EQU 0xC1
;//* .
;//* .
;//* .
DSBufWCom30 EQU 0xFC
DSBuRDCom30 EQU 0xFD
DSBufBurWC EQU 0xFE ;//* The RAM command byte specifies burst mode operation.
;//* In this mode, the 31 RAM registers can be
DSBufBurRDC EQU 0xFF ;//* consecutively read or written starting with bit 0
;//* of address 0.
;//* date and time buffer
vYearBuf EQU 0x30
vMonthBuf EQU 0x31
vDateBuf EQU 0x32
vDayBuf EQU 0x33
vHourBuf EQU 0x34
vMinuteBuf EQU 0x35
vSecondBuf EQU 0x36
;//*
;//* **************************************************************
;//*
ORG 0x0000
AJMP Start
ORG 0x0030
Start:
MOV SP, #0x60
MOV R0, #0x7F
ClearRAM:
MOV @R0, #0
DJNZ R0, ClearRAM
MOV SCON, #0x50;
MOV TMOD, #0x21; //Timer0 - 16bit timer mode
MOV TL1, #253;
MOV TH1, #253;
SETB TR1
SETB TI
NOP
NOP
aaaa:
NOP
NOP
JB P1.7, aaaa
CLR P1.0
CLR P1.1
LCALL Delay1s
SETB P1.1
SETB P1.0
LCALL Delay1s
AJMP aaaa
LCALL INITDS1302
NOP
NOP
Loop:
LCALL ReadTime
NOP
CLR TI
MOV SBUF, vYearBuf
JNB TI, $
CLR TI
MOV SBUF, vMonthBuf
JNB TI, $
CLR TI
MOV SBUF, vDateBuf
JNB TI, $
CLR TI
MOV SBUF, vDayBuf
JNB TI, $
CLR TI
MOV SBUF, vHourBuf
JNB TI, $
CLR TI
MOV SBUF, vMinuteBuf
JNB TI, $
CLR TI
MOV SBUF, vSecondBuf
JNB TI, $
MOV R6, #0
Del:
MOV R7, #0
DJNZ R7, $
DJNZ R6, Del
AJMP Loop
NOP
; LCALL WriteTime
NOP
NOP
AJMP $
;//* **************************************************************
;//* Get year month date hour minute second
;//*
ReadTime:
MOV A, #YearRDCom
LCALL RDInfoFromDS1302
MOV vYearBuf, A
MOV A, #MonthRDCom
LCALL RDInfoFromDS1302
MOV vMonthBuf, A
MOV A, #DateRDCom
LCALL RDInfoFromDS1302
MOV vDateBuf, A
MOV A, #HourRDCom
LCALL RDInfoFromDS1302
MOV vHourBuf, A
MOV A, #MinuteRDCom
LCALL RDInfoFromDS1302
MOV vMinuteBuf, A
MOV A, #SecondRDCom
LCALL RDInfoFromDS1302
MOV vSecondBuf, A
MOV A, #DayRDCom
LCALL RDInfoFromDS1302
MOV vDayBuf, A
RET
;//* **************************************************************
;//* set year month date hour minute second
;//*
WriteTime:
MOV A, #YearWCom
MOV B, vYearBuf
LCALL WInfoToDS1302
MOV A, #DateWCom
MOV B, vDateBuf
LCALL WInfoToDS1302
MOV A, #MonthWCom
MOV B, vMonthBuf
LCALL WInfoToDS1302
MOV A, #HourWCom
MOV B, vHourBuf
LCALL WInfoToDS1302
MOV A, #SecondWCom
MOV B, vSecondBuf
LCALL WInfoToDS1302
MOV A, #MinuteWCom
MOV B, vMinuteBuf
LCALL WInfoToDS1302
MOV A, #DayWCom
MOV B, vDayBuf
LCALL WInfoToDS1302
RET
;//* **************************************************************
;//* Function: get byte from ds1302 register
;//* Input: A(command code)
;//* Output: A(data byte)
;//* Register: A,R7
RDInfoFromDS1302:
CLR p1302RST
NOP
CLR p1302SCLK
NOP
SETB p1302RST
NOP
MOV R7, #0x08 ;//* 8bits/byte
WNextComBit:
RRC A ;//* command code
MOV p1302IO,C
NOP
SETB p1302SCLK
NOP
CLR p1302SCLK
DJNZ R7, WNextComBit
MOV R7, #0x08 ;//* 8bits/byte
RDNextDataBit:
MOV C, p1302IO
RRC A
SETB p1302SCLK
NOP
CLR p1302SCLK
DJNZ R7, RDNextDataBit
CLR p1302RST
RET
;//* **************************************************************
;//* Function: set info to ds1302 register
;//* Input: A(command code)
;//* B(data byte)
;//* Register: A,B,R7
WInfoToDS1302:
CLR p1302RST
NOP
CLR p1302SCLK
NOP
SETB p1302RST
NOP
MOV R7, #0x08
WrNextComBit:
RRC A ;//* command code
MOV p1302IO,C
NOP
SETB p1302SCLK
NOP
CLR p1302SCLK
DJNZ R7, WrNextComBit
MOV A, B
MOV R7, #0x08
WNextDataBit:
RRC A ;//* data byte
MOV p1302IO,C
NOP
SETB p1302SCLK
NOP
CLR p1302SCLK
DJNZ R7, WNextDataBit
CLR p1302RST
RET
;//* **************************************************************
;//* Function: initialization DS1302, set initial time
;//*
INITDS1302:
;//* COMMAND BYTE
;//* The command byte is shown in Figure 1. Each data transfer is
;//* initiated by a command byte. The MSB(Bit 7) must be a logic 1.
;//* If it is 0, writes to the DS1302 will be disabled. Bit 6 specifies
;//* clock/calendar data if logic 0 or RAM data if logic 1. Bits 1
;//* through 5 specify the designated registers to be input or output,
;//* and the LSB (bit 0) specifies a write operation (input) if logic
;//* 0 or read operation (output) if logic 1. The command byte is always
;//* input starting with the LSB (bit 0).
;//* ADDRESS/COMMAND BYTE Figure 1
;//* --------------------------------------------------
;//* | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
;//* --------------------------------------------------
;//* | | RAM / | | | | | | RD /|
;//* | 1 | / | A4 | A3 | A2 | A1 | A0 | / |
;//* | | / CK | | | | | |/ W |
;//* --------------------------------------------------
MOV A, #SecondRDCom
LCALL RDInfoFromDS1302
JNB ACC.7, DisableWrite
;//*
MOV A, #YearWCom
MOV B, #0x00 ;//* 2000 year
LCALL WInfoToDS1302
;//*
MOV A, #MonthWCom
MOV B, #0x07 ;//* 7 month
LCALL WInfoToDS1302
;//*
MOV A, #DateWCom
MOV B, #0x01 ;//* 1 day
LCALL WInfoToDS1302
;//*
MOV A, #HourWCom
MOV B, #0x12 ;//* 12 hour
LCALL WInfoToDS1302
;//*
MOV A, #MinuteWCom
MOV B, #0x00 ;//* 0 minute
LCALL WInfoToDS1302
;//*
MOV A, #SecondWCom
MOV B, #0x00 ;//* 0 second
LCALL WInfoToDS1302
;//*
MOV A, #DayWCom
MOV B, #0x07 ;//* sunday
LCALL WInfoToDS1302
DisableWrite:
MOV A, #SecondRDCom
LCALL RDInfoFromDS1302
MOV vSecondBuf, A
RET
Delay1s:
MOV R7, #10
Delay:
MOV R6, #0
Dela:
MOV R5, #0
DJNZ R5, $
DJNZ R6, Dela
DJNZ R7, Delay
RET
END
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -