comparison src/rtc.asm @ 623:c40025d8e750

3.03 beta released
author heinrichsweikamp
date Mon, 03 Jun 2019 14:01:48 +0200
parents d866684249bd
children 185ba2f91f59
comparison
equal deleted inserted replaced
622:02d1386429a6 623:c40025d8e750
1 ;============================================================================= 1 ;=============================================================================
2 ; 2 ;
3 ; File rtc.asm ## V2.98c 3 ; File rtc.asm combined next generation V3.02.1
4 ; 4 ;
5 ; 5 ;
6 ; Copyright (c) 2011, JD Gascuel, HeinrichsWeikamp, all right reserved. 6 ; Copyright (c) 2011, JD Gascuel, HeinrichsWeikamp, all right reserved.
7 ;============================================================================= 7 ;=============================================================================
8 ; HISTORY 8 ; HISTORY
14 rtc CODE 14 rtc CODE
15 15
16 ;============================================================================= 16 ;=============================================================================
17 17
18 global rtc_init 18 global rtc_init
19 global rtc_set_rtc
19 rtc_init: 20 rtc_init:
20 movlw .1 21 banksel isr_backup ; select bank ISR data
21 movwf secs 22 movlw .0
22 movlw .59 23 movwf rtc_latched_secs
23 movwf mins 24 movlw .0
25 movwf rtc_latched_mins
24 movlw .12 26 movlw .12
25 movwf hours 27 movwf rtc_latched_hour
26 movlw .2 28 movlw firmware_creation_day
27 movwf day 29 movwf rtc_latched_day
28 movlw .12 30 movlw firmware_creation_month
29 movwf month 31 movwf rtc_latched_month
30 movlw .17 32 movlw firmware_creation_year
31 movwf year 33 movwf rtc_latched_year
32 ; rcall rtc_set_rtc ; writes mins,sec,hours,day,month and year to rtc module 34 rtc_set_rtc:
33 ; return 35 banksel isr_backup
36 movlw d'24' ; safeguard hour
37 cpfslt rtc_latched_hour
38 clrf rtc_latched_hour
39 movlw d'60' ; safeguard minutes
40 cpfslt rtc_latched_mins
41 clrf rtc_latched_mins
42 movlw d'60' ; safeguard seconds
43 cpfslt rtc_latched_secs
44 clrf rtc_latched_secs
45 movlw d'99' ; safeguard year
46 cpfslt rtc_latched_year
47 movwf rtc_latched_year
48 movlw d'12' ; safeguard month
49 cpfslt rtc_latched_month
50 movwf rtc_latched_month
34 51
35 global rtc_set_rtc 52 banksel common ; select bank common
36 rtc_set_rtc: 53 rcall rtc_check_day ; safeguard day
37 banksel 0xF16 ; Addresses, F16h through F5Fh, are also used by SFRs, but are not part of the Access RAM. 54 bsf block_rtc_access ; suspend the ISR from accessing the RTC
38 movlw 0x55 55
39 movwf EECON2 56
40 movlw 0xAA 57 banksel 0xF16 ; addresses F16h through F5Fh are also used by SFRs, but are not part of the access RAM
41 movwf EECON2 58
42 bsf RTCCFG,RTCWREN ; Unlock sequence for RTCWREN 59 movlw 0x55 ; | unlock sequence for RTCWREN, EECON2 is located in the access RAM
43 bsf RTCCFG,RTCPTR1 60 movwf EECON2 ; |
44 bsf RTCCFG,RTCPTR0 ; year 61 movlw 0xAA ; |
45 movff year,WREG 62 movwf EECON2 ; |
46 rcall rtc_dec2bcd ; IN: temp1 in WREG, OUT: WREG in BCD, also sets to bank16h! 63 bsf RTCCFG,RTCWREN ; RTC write unlock, must follow directly after above unlock sequence!
64 bsf RTCCFG,RTCPTR1
65 bsf RTCCFG,RTCPTR0
66 movff rtc_latched_year,WREG
67 rcall rtc_dec2bcd ; IN: WREG in decimal, OUT: WREG in BCD, also sets to bank16h!
47 movwf RTCVALL ; year 68 movwf RTCVALL ; year
48 movwf RTCVALH ; dummy write 69 movwf RTCVALH ; dummy write
49 movff day,WREG 70 movff rtc_latched_day,WREG
50 rcall rtc_dec2bcd ; IN: temp1 in WREG, OUT: WREG in BCD, also sets to bank16h! 71 rcall rtc_dec2bcd ; IN: WREG in decimal, OUT: WREG in BCD, also sets to bank16h!
51 movwf RTCVALL ;day 72 movwf RTCVALL ; day
52 movff month,WREG 73 movff rtc_latched_month,WREG
53 rcall rtc_dec2bcd ; IN: temp1 in WREG, OUT: WREG in BCD, also sets to bank16h! 74 rcall rtc_dec2bcd ; IN: WREG in decimal, OUT: WREG in BCD, also sets to bank16h!
54 movwf RTCVALH ;month 75 movwf RTCVALH ; month
55 movff hours,WREG 76 movff rtc_latched_hour,WREG
56 rcall rtc_dec2bcd ; IN: temp1 in WREG, OUT: WREG in BCD, also sets to bank16h! 77 rcall rtc_dec2bcd ; IN: WREG in decimal, OUT: WREG in BCD, also sets to bank16h!
57 movwf RTCVALL ;hours 78 movwf RTCVALL ; hour
58 movlw d'0' 79 movlw d'0'
59 rcall rtc_dec2bcd ; IN: temp1 in WREG, OUT: WREG in BCD, also sets to bank16h! 80 rcall rtc_dec2bcd ; IN: WREG in decimal, OUT: WREG in BCD, also sets to bank16h!
60 movwf RTCVALH ;weekday 81 movwf RTCVALH ; weekday
61 movff secs,WREG 82 movff rtc_latched_secs,WREG
62 rcall rtc_dec2bcd ; IN: temp1 in WREG, OUT: WREG in BCD, also sets to bank16h! 83 rcall rtc_dec2bcd ; IN: WREG in decimal, OUT: WREG in BCD, also sets to bank16h!
63 movwf RTCVALL ;secs 84 movwf RTCVALL ; seconds
64 movff mins,WREG 85 movff rtc_latched_mins,WREG
65 rcall rtc_dec2bcd ; IN: temp1 in WREG, OUT: WREG in BCD, also sets to bank16h! 86 rcall rtc_dec2bcd ; IN: WREG in decimal, OUT: WREG in BCD, also sets to bank16h!
66 movwf RTCVALH ;minutes 87 movwf RTCVALH ; minutes
67 movlw 0x55 88
68 movwf EECON2 89 movlw 0x55 ; | probably not needed when clearing RTCWREN
69 movlw 0xAA 90 movwf EECON2 ; |
70 movwf EECON2 91 movlw 0xAA ; |
71 bcf RTCCFG,RTCWREN ; Lock sequence for RTCWREN 92 movwf EECON2 ; |
93 bcf RTCCFG,RTCWREN ; lock sequence for RTCWREN
94
72 banksel common 95 banksel common
96
97 ; update the "live" RTC variables to bridge the time until the ISR reads the updated data from the RTC
98 movff rtc_latched_year, rtc_year
99 movff rtc_latched_month,rtc_month
100 movff rtc_latched_day, rtc_day
101 movff rtc_latched_hour, rtc_hour
102 movff rtc_latched_mins, rtc_mins
103 movff rtc_latched_secs, rtc_secs
104
105 bcf block_rtc_access ; allow the ISR to access the RTC again
73 return 106 return
74 107
108
75 rtc_dec2bcd: 109 rtc_dec2bcd:
76 banksel lo 110 banksel common ; switch to bank common
77 movwf lo ; Input in decimal 111 movwf lo ; input in decimal
78 setf hi ; 10s 112 setf hi ; 10s
79 rtc_dec2bcd2: 113 rtc_dec2bcd2:
80 incf hi,F ; Count 10's 114 incf hi,F ; count 10's
81 movlw d'10' 115 movlw d'10'
82 subwf lo,F 116 subwf lo,F
83 btfss STATUS,N 117 btfss STATUS,N
84 bra rtc_dec2bcd2 118 bra rtc_dec2bcd2
85 movlw d'10' 119 movlw d'10'
86 addwf lo,F ; 1s 120 addwf lo,F ; 1s
87 swapf hi,W ; swap to bit 7-4 -> WREG 121 swapf hi,W ; swap to bit 7-4 -> WREG
88 addwf lo,W ; Result in BCD 122 addwf lo,W ; result in BCD
89 banksel 0xF16 ; Addresses, F16h through F5Fh, are also used by SFRs, but are not part of the Access RAM 123 banksel 0xF16 ; switch back to bank for I/O registers
90 return 124 return
91 125
126 rtc_check_day:
127 movlw .28 ; the default February has 28 days
128 movff rtc_latched_year,lo ; bank-safe get of the current year
129 btfsc lo,0 ; is the current year an even year?
130 bra rtc_check_day_1 ; NO
131 btfss lo,1 ; YES - is it a multiple of 4 years?
132 movlw .29 ; YES - leap year, February has 29 days
133 rtc_check_day_1: ; NO - keep the 28 days
134 movwf hi ; store highest day in February in hi
135 movff rtc_latched_month,lo; bank-safe get of the current month
136 dcfsnz lo,F ; current month = January?
137 movlw .31 ; YES - highest day is 31
138 dcfsnz lo,F ; current month = February?
139 movf hi,W ; YES - highest day is 28/29
140 dcfsnz lo,F ; current month = March?
141 movlw .31 ; YES - highest day is 31
142 dcfsnz lo,F ; current month = April?
143 movlw .30 ; YES - highest day is 30
144 dcfsnz lo,F ; current month = May?
145 movlw .31 ; YES - highest day is 31
146 dcfsnz lo,F ; current month = June?
147 movlw .30 ; YES - highest day is 30
148 dcfsnz lo,F ; current month = July?
149 movlw .31 ; YES - highest day is 31
150 dcfsnz lo,F ; current month = August?
151 movlw .31 ; YES - highest day = 31
152 dcfsnz lo,F ; current month = September?
153 movlw .30 ; YES - highest day = 30
154 dcfsnz lo,F ; current month = October?
155 movlw .31 ; YES - highest day = 31
156 dcfsnz lo,F ; current month = November?
157 movlw .30 ; YES - highest day = 30
158 dcfsnz lo,F ; current month = December?
159 movlw .31 ; YES - highest day = 31
160 movff rtc_latched_day,hi ; bank-safe get of the current day
161 cpfsgt hi ; current day > highest day?
162 return ; NO - day is ok
163 movlw .1 ; YES - wrap around to 1st day in month
164 movff WREG,rtc_latched_day; - bank-safe write-back of corrected day
165 return ; - done
166
92 END 167 END