;*************************************************************************** ; ; File Name :'RTC8564.asm" ; Title :Epson RTC8564JE Driver ; Date :2003.04.30. Lastmod.:[2003.04.30.] ; Version :1.0.0 ; Support telephone :+36-70-333-4034, Old: +36-30-9541-658 VFX ; Support fax : ; Support Email :info@vfx.hu ; Target MCU :ATmaga128 ; ;*************************************************************************** ; D E S C R I P T I O N ; ; Real Time Clock Modul Driver ; ; ;*************************************************************************** ; M O D I F I C A T I O N H I S T O R Y ; ; ; rev. date who why ; ---- ---------- --- -------------------------------------------- ; 0.01 2003.04.30 VFX Creation ; ;*************************************************************************** ; ; ; ;Hardware ;*************************************************************************** ;* ;************************************************************************** ;* Const Def ; .equ RTC_SlaveAddr = 0b10100010 ; RTC internal Registers ; All data address 0x02 through 05 & 0x08 through 0x0B is in BCD format .equ RTC_Control1 = 0x00 .equ RTC_Control2 = 0x01 .equ RTC_Seconds = 0x02 .equ RTC_Minutes = 0x03 .equ RTC_Hours = 0x04 .equ RTC_Days = 0x05 .equ RTC_WeekDays = 0x06 .equ RTC_Months = 0x07 .equ RTC_Years = 0x08 .equ RTC_MinuteAlarm = 0x09 .equ RTC_HourAlarm = 0x0A .equ RTC_DayAlarm = 0x0B .equ RTC_WeekdayAlarm= 0x0C .equ RTC_CLKOUT_Freq = 0x0D .equ RTC_TimerControl= 0x0E .equ RTC_Timer = 0x0F ;RTC Commands ;Control Register 1 .equ RTC_StopRunning = 0b00100000 .equ RTC_ContRunning = 0b00000000 ;Control Register 2 .equ RTC_AlarmIntEnable = 0b00000010 .equ RTC_AlarmIntDisable= 0b00000000 ;Seconds [addr 0x02] .equ RTC_LowBat = 0b10000000 ;century [Addr 0x07] .equ RTC_Cetury = 0b10000000 ;*************************************************************************** .DSEG IO_CTR_RTC: .byte 17 ;IO buffer for RTC controll ;Time -> 00:00:00 + Line end ;Date -> 2000-01-01 + Line end ;************************************************************************* ;* Text Const ;* .CSEG ;************************************************************************* ;* Read RTC All dataa ;* RTC_ReadALL: ldi XL,Low(IO_CTR_RTC) ldi XH,High(IO_CTR_RTC) ldi R17,16 ;reads 16 byte ldi R18,RTC_SlaveAddr ;Slave Address ldi R19,0 ;Begining fo read rcall TWI_StartRead ret ;************************************************************************* ;* Start RTC ;* RTC_Start: rcall WaitToTWI ldi XL,Low(IO_CTR_RTC) ldi XH,High(IO_CTR_RTC) ldi R16,RTC_ContRunning st X,R16 ldi R17,1 ;write 16 byte ldi R18,RTC_SlaveAddr ;Slave Address ldi R19,0 ;Begining of write rcall TWI_StartWrite ret ;************************************************************************* ;* Stop RTC ;* RTC_Stop: rcall WaitToTWI ldi XL,Low(IO_CTR_RTC) ldi XH,High(IO_CTR_RTC) ldi R16,RTC_StopRunning st X,R16 ldi R17,1 ;write 1 byte ldi R18,RTC_SlaveAddr ;Slave Address ldi R19,0 ;Begining of write rcall TWI_StartWrite ret ;****************************************************************** ;* RTC Low Battery detector ; ; Out: ; z = 1 TWI Error ; z = 0 No error and ; c=1 Battery Low ; c=0 Battery Ok RTC_BatLow: ldi XL,Low(IO_CTR_RTC) ldi XH,High(IO_CTR_RTC) ldi R17,1 ;read 1 byte ldi R18,RTC_SlaveAddr ;Slave Address ldi R19,RTC_Seconds ;Begining of read rcall TWI_StartRead rcall WaitToTWI brcs RTC_BatLow1 ldi XL,Low(IO_CTR_RTC) ldi XH,High(IO_CTR_RTC) ld R16,X rol R16 ;csak a c=VL bit marad clz ret RTC_BatLow1: sez ;TWI Error ret LowBatS: .db "RTC Battery is Low - Check Date/Time",CR,0 noRTC: .db "RTC not found - setup date/time manualy ",CR,0 ;*************************************************************************** ; RTC Get Time ; ; In: - ; Out: var X -> IO_CTR_RTC (00:00:00+zero byte) ASCCI ; R4:R3:R2 - hh:mm:ss BCD ; Used: RTC_GetTime: rcall RTC_Stop ldi XL,Low(IO_CTR_RTC+1) ldi XH,High(IO_CTR_RTC+1) ldi R17,3 ;noumber of byte ldi R18,RTC_SlaveAddr ;Slave Address ldi R19,RTC_Seconds rcall TWI_StartRead rcall RTC_Start ldi ZL,Low(IO_CTR_RTC+1) ldi ZH,High(IO_CTR_RTC+1) ldd R16,Z+0 ;sec andi R16,0b01111111 mov R2,R16 ldd R16,Z+1 ;min andi R16,0b01111111 mov R3,R16 ldd R16,Z+2 ;hrs andi R16,0b00111111 mov R0,R16 mov R4,R16 rcall BCDToASCII std Z+0,R1 std Z+1,R0 ldi R17,':' std Z+2,R17 mov R0,R3 rcall BCDToASCII std Z+3,R1 std Z+4,R0 std Z+5,R17 mov R0,R2 rcall BCDToASCII std Z+6,R1 std Z+7,R0 clr R16 std Z+8,R16 movw XL,ZL ret ;*************************************************************************** ; RTC Set Time ; ; In: R4:R3:R2 -> hh:mm:ss BCD ; Out: c = 0 sucess ; c = 1 error ; Used: ; RTC_SetTime: rcall RTC_Stop ldi XL,Low(IO_CTR_RTC) ldi XH,High(IO_CTR_RTC) rcall WaitToTWI st X+,R2 st X+,R3 st X,R4 ldi XL,Low(IO_CTR_RTC) ldi XH,High(IO_CTR_RTC) ldi R17,3 ;noumber of byte ldi R18,RTC_SlaveAddr ;Slave Address ldi R19,RTC_Seconds rcall TWI_StartWrite rcall RTC_Start ret ;*************************************************************************** ; RTC Set Date ; ; In: R4:R3:R2 -> yy:mm:dd BCD ; R5 - Century bit 0/1 ; Out: c = 0 sucess ; c = 1 error ; Used: ; RTC_SetDate: clr R16 ror R5 ror R16 or R3,R16 rcall RTC_Stop ldi XL,Low(IO_CTR_RTC) ldi XH,High(IO_CTR_RTC) rcall WaitToTWI st X+,R3 st X,R4 sbiw XL,1 ldi R17,2 ;noumber of byte ldi R18,RTC_SlaveAddr ;Slave Address ldi R19,RTC_Months rcall TWI_StartWrite rcall WaitToTWI ldi XL,Low(IO_CTR_RTC) ldi XH,High(IO_CTR_RTC) st X,R2 ldi R17,1 ;noumber of byte ldi R18,RTC_SlaveAddr ;Slave Address ldi R19,RTC_Days rcall TWI_StartWrite rcall RTC_Start ret ;*************************************************************************** ; RTC Get Date ; ; In: - ; Out: var X -> IO_CTR_RTC (yyyy-mm-dd+zero byte) ASCCI ; R4:R3:R2 - yy-mm-dd BCD ; R5 - Century bit ; Used: RTC_GetDate: rcall RTC_Stop ldi XL,Low(IO_CTR_RTC+1) ldi XH,High(IO_CTR_RTC+1) ldi R17,4 ;noumber of byte ldi R18,RTC_SlaveAddr ;Slave Address ldi R19,RTC_Days rcall TWI_StartRead rcall RTC_Start ldi ZL,Low(IO_CTR_RTC+1) ldi ZH,High(IO_CTR_RTC+1) ldd R16,Z+0 ;days andi R16,0b00111111 mov R2,R16 ldd R16,Z+2 mov R5,R16 andi R16,0b00011111 mov R3,R16 rol R5 clr R5 rol R5 tst R5 brne XXICentury ldi R16,'1' std Z+0,R16 ldi R16,'9' rjmp CenturyOk XXICentury: ldi R16,'2' std Z+0,R16 ldi R16,'0' CenturyOk: std Z+1,R16 ldd R16,Z+3 ;years mov R0,R16 mov R4,R16 rcall BCDToASCII std Z+2,R1 std Z+3,R0 ldi R17,'-' std Z+4,R17 mov R0,R3 rcall BCDToASCII std Z+5,R1 std Z+6,R0 std Z+7,R17 mov R0,R2 rcall BCDToASCII std Z+8,R1 std Z+9,R0 clr R16 std Z+10,R16 movw XL,ZL ret