?? 1302sub.asm
字號:
;************************************************
;* DS1302 RTC driver routines for AVR's *
;* *
;* Copyright 2001-2003 Wayne Peacock *
;* Wayne Peacock 7th July 2001 *
;* Version 1.02 *
;************************************************
;* See Schematic for Hardware connections *
;* *
;* Disclaimer: *
;* The Author takes no responsibility for the *
;* use of this code. Use at your own risk! *
;* *
;* This code or part there of is licensed only *
;* for private use and NOT for commercial use. *
;* *
;* Please contact the author 'Wayne Peacock' *
;* <wpeacock@senet.com.au> before using the code*
;* or part there of in a commercial project. *
;************************************************
;.def data = r17
;.def temp = r16
;.def loopcnt = r18
;************************************************
;* LCD CONSTANTS *
;************************************************
.equ SCLK = 3
.equ IO = 4
.equ RST = 5
.equ seconds = 0x60 ; SRAM Storage
.equ minutes = 0x61
.equ hours = 0x62
.equ date = 0x63
.equ month = 0x64
.equ day = 0x65
.equ year = 0x66
.equ control = 0x67
; Modify for correct port
.set RTCPORT = PORTD
.set RTCDIR = DDRD
.set RTCIN = PIND
;************************************************
;* Setup Ports for DS1302 and resets device *
;* *
;* Input: None *
;* Returns: None *
;************************************************
DS1302_init:
cbi RTCPORT, SCLK ; Clear Pin ready for reset
cbi RTCPORT, RST ; Reset low when pin enabled
sbi RTCDIR, SCLK ; Set Pin as Output
sbi RTCDIR, IO ; Set Pin as Output
sbi RTCDIR, RST ; set Pin as Output
ret
;************************************************
;* Reset Macros for DS1302 *
;* *
;* Input: None *
;* Returns: None *
;************************************************
.macro DS1302_start
cbi RTCPORT, SCLK ; SCLK MUST BE ZERO!!
sbi RTCPORT, RST ; Enable DS1302
rcall delay2us ; 2us delay!
.endm
.macro DS1302_stop
cbi RTCPORT, RST ; Place DS1302 into reset
rcall delay2us ; 2us delay!
.endm
;************************************************
;* Write Byte DS1302 (4Mhz) *
;* *
;* Input: data *
;* Returns: None *
;* Requires: temp *
;************************************************
DS1302_Write:
sbi RTCDIR, IO ; Set Pin as Output
ldi temp, 8 ; 8 bits to write
DS1302_wloop:
cbi RTCPORT, SCLK ; Clock Low
cbi RTCPORT, IO ; Data = Low
sbrc data, 0 ; If D0 = write 1
sbi RTCPORT, IO ; Data = high
ror data
rcall delay2us ; 2us delay!
sbi RTCPORT, SCLK ; Clock Data in (high)
rcall delay2us ; 2us delay!
dec temp
brne DS1302_wloop
ret
;************************************************
;* DS1302 Read Byte (4 Mhz) *
;* Must Follow a Write *
;* Output: data *
;* Requires: temp *
;************************************************
DS1302_read:
cbi RTCDIR, IO ; Set Pin as Input
ldi temp, 8 ; 8 bits to write
clr data
DS1302_rloop:
; Clock should already be high!
sbi RTCPORT, SCLK ; Clock high
rcall delay2us ; 2us delay!
cbi RTCPORT, SCLK ; Clock Low
; Get Data Now
lsr data
sbic RTCIN, IO ; check bit
sbr data, 0x80
rcall delay2us ; 2us delay!
dec temp
brne DS1302_rloop
ret
;************************************************
;* DS1302 Start Clock (4 Mhz) *
;* *
;* Output: none *
;* Requires : data *
;************************************************
DS1302_ck_start:
DS1302_start
ldi data, 0b10000001 ; Cmd: Read Sec
rcall DS1302_write
rcall DS1302_read
DS1302_stop
rol data ; Move D7 into Carry
brcs clock_start
ret ; Clock already running
clock_start:
DS1302_start
ldi data, 0b10000000 ; Cmd: Write to Sec register
rcall DS1302_Write
ldi data, 0b00000000 ; Value to Write
rcall DS1302_Write
DS1302_stop
ret
;************************************************
;* DS1302 Write Protect (4 Mhz) *
;* *
;* Input: none *
;* Requires: temp *
;************************************************
DS1302_wp:
DS1302_start
ldi data, 0b10001110 ; Cmd: Write to Control register
rcall DS1302_Write
ldi data, 0b10000000 ; Value to Write
rcall DS1302_Write
DS1302_stop
ret
DS1302_notwp:
DS1302_start
ldi data, 0b10001110 ; Cmd: Write to Control register
rcall DS1302_Write
ldi data, 0b00000000 ; Value to Write
rcall DS1302_Write
DS1302_stop
ret
;************************************************
;* DS1302 Trickle charge Super Capacitor (4 Mhz)*
;* *
;* Input: None *
;* Requires: Data *
;************************************************
DS1302_charge:
DS1302_start
ldi data, 0b10010000 ; Cmd: Write to Charge register
rcall DS1302_Write
ldi data, 0b10100101 ; 2K - 1 Diode
rcall DS1302_Write
DS1302_stop
ret
;************************************************
;* DS1302 Read Clock into internal SRAM *
;* *
;* *
;************************************************
DS1302_read_ck:
DS1302_start
ldi data, 0xbf ; Burst Read of Clock
rcall DS1302_write
ldi loopcnt, 0x08 ; Read 8 Bytes
ldi YH,high(seconds)
ldi YL,low(seconds) ;init Y-pointer
rom_loop1:
rcall DS1302_read ; Read a byte
st Y+,data ; store data to SRAM
dec loopcnt ;
brne rom_loop1 ;if not done, loop more
DS1302_stop
ret
;************************************************
;* Dump Raw Data from SRAM to RS232 *
;* Requires 'serial.asm' *
;* *
;************************************************
;DS1302_dump_data:
; ldi YH,high(seconds)
; ldi YL,low(seconds) ; init Y-pointer
; ldi loopcnt, 0x08 ; Read 8 values
;DS1302_dump_loop:
; ld data, Y+
; rcall comm_write
; dec loopcnt ;
; brne DS1302_dump_loop ;if not done, loop more
; ret
;************************************************
;* Dump Time from SRAM to RS232 & LCD Display *
;* Requires 'serial.asm' *
;* *
;************************************************
DS1302_dump_time:
ldi data, 0xC2 ; Bottom Line of LCD
rcall command
ldi YH,high(date)
ldi YL,low(date) ; init Y-pointer
; Get Hours (24 hour time)
ld data, -Y
swap data
andi data, 0x0F ; Remove first nibble
ori data, 0x30 ; Add 30 hex
;rcall comm_write ; Display tens
rcall onechar
ld data, Y
andi data, 0x0F ; Remove first nibble
ori data, 0x30 ; Add 30 hex
rcall onechar
;rcall comm_write ; Display units
ldi data, ':'
;rcall comm_write
rcall onechar
; Get Minutes
ld data, -Y
swap data
andi data, 0x0F ; Remove first nibble
ori data, 0x30 ; Add 30 hex
;rcall comm_write ; Display tens
rcall onechar
ld data, Y
andi data, 0x0F ; Remove first nibble
ori data, 0x30 ; Add 30 hex
;rcall comm_write ; Display units
rcall onechar
ldi data, ':'
;rcall comm_write
rcall onechar
; Get seconds
ld data, -Y
swap data
andi data, 0x07 ; Remove first nibble
ori data, 0x30 ; Add 30 hex
;rcall comm_write ; Display tens
rcall onechar
ld data, Y
andi data, 0x0F ; Remove first nibble
ori data, 0x30 ; Add 30 hex
;rcall comm_write ; Display units
rcall onechar
ldi data, ' '
;rcall comm_write
rcall onechar
; Get Date
; Day of month
ldi YH,high(date)
ldi YL,low(date) ; init Y-pointer
ld data, Y
swap data
andi data, 0x0F ; Remove first nibble
ori data, 0x30 ; Add 30 hex
;rcall comm_write ; Display tens
rcall onechar
ld data, Y+
andi data, 0x0F ; Remove first nibble
ori data, 0x30 ; Add 30 hex
;rcall comm_write ; Display units
rcall onechar
ldi data, '/'
;rcall comm_write
rcall onechar
; Month
ld data, Y
swap data
andi data, 0x0F ; Remove first nibble
ori data, 0x30 ; Add 30 hex
;rcall comm_write ; Display tens
rcall onechar
ld data, Y+
andi data, 0x0F ; Remove first nibble
ori data, 0x30 ; Add 30 hex
;rcall comm_write ; Display units
rcall onechar
ldi data, '/'
;rcall comm_write
rcall onechar
; Year
ldi data, '2'
;rcall comm_write
rcall onechar
ldi data, '0'
;rcall comm_write
rcall onechar
ld data, Y+ ; Day
ld data, Y ; Year
swap data
andi data, 0x0F ; Remove first nibble
ori data, 0x30 ; Add 30 hex
;rcall comm_write ; Display tens
rcall onechar
ld data, Y+
andi data, 0x0F ; Remove first nibble
ori data, 0x30 ; Add 30 hex
;rcall comm_write ; Display units
rcall onechar
ldi data, ' '
rcall onechar
;ldi data, 0x0d
;rcall comm_write
ret
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -