Mercurial > public > hwos_code
comparison src/rtc.asm @ 631:185ba2f91f59
3.09 beta 1 release
author | heinrichsweikamp |
---|---|
date | Fri, 28 Feb 2020 15:45:07 +0100 |
parents | c40025d8e750 |
children | 4050675965ea |
comparison
equal
deleted
inserted
replaced
630:4cd81bdbf15c | 631:185ba2f91f59 |
---|---|
1 ;============================================================================= | 1 ;============================================================================= |
2 ; | 2 ; |
3 ; File rtc.asm combined next generation V3.02.1 | 3 ; File rtc.asm combined next generation V3.08.8 |
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 rtc_init: | |
20 banksel isr_backup ; select bank ISR data | |
21 movlw .0 ; reset time to 12:00:00 | |
22 movwf rtc_latched_secs ; ... | |
23 movlw .0 ; ... | |
24 movwf rtc_latched_mins ; ... | |
25 movlw .12 ; ... | |
26 movwf rtc_latched_hour ; ... | |
27 movlw firmware_creation_day ; reset date to firmware creation date | |
28 movwf rtc_latched_day ; ... | |
29 movlw firmware_creation_month ; ... | |
30 movwf rtc_latched_month ; ... | |
31 movlw firmware_creation_year ; ... | |
32 movwf rtc_latched_year ; ... | |
33 ;bra rtc_set_rtc ; set the real time clock | |
34 | |
19 global rtc_set_rtc | 35 global rtc_set_rtc |
20 rtc_init: | |
21 banksel isr_backup ; select bank ISR data | |
22 movlw .0 | |
23 movwf rtc_latched_secs | |
24 movlw .0 | |
25 movwf rtc_latched_mins | |
26 movlw .12 | |
27 movwf rtc_latched_hour | |
28 movlw firmware_creation_day | |
29 movwf rtc_latched_day | |
30 movlw firmware_creation_month | |
31 movwf rtc_latched_month | |
32 movlw firmware_creation_year | |
33 movwf rtc_latched_year | |
34 rtc_set_rtc: | 36 rtc_set_rtc: |
35 banksel isr_backup | 37 banksel isr_backup ; select bank ISR data |
36 movlw d'24' ; safeguard hour | 38 movlw d'24' ; safeguard hour |
37 cpfslt rtc_latched_hour | 39 cpfslt rtc_latched_hour ; hour < 24 ? |
38 clrf rtc_latched_hour | 40 clrf rtc_latched_hour ; NO - reset to 0 |
39 movlw d'60' ; safeguard minutes | 41 movlw d'60' ; safeguard minutes and seconds |
40 cpfslt rtc_latched_mins | 42 cpfslt rtc_latched_mins ; minute < 60 ? |
41 clrf rtc_latched_mins | 43 clrf rtc_latched_mins ; NO - reset to 0 |
42 movlw d'60' ; safeguard seconds | 44 cpfslt rtc_latched_secs ; seconds < 60 ? |
43 cpfslt rtc_latched_secs | 45 clrf rtc_latched_secs ; NO - reset to 0 |
44 clrf rtc_latched_secs | 46 movlw d'100' ; safeguard year |
45 movlw d'99' ; safeguard year | 47 cpfslt rtc_latched_year ; year < 100 ? |
46 cpfslt rtc_latched_year | 48 clrf rtc_latched_year ; NO - reset to 0 |
47 movwf rtc_latched_year | 49 movlw d'12' ; safeguard month |
48 movlw d'12' ; safeguard month | 50 cpfslt rtc_latched_month ; month < 12 ? |
49 cpfslt rtc_latched_month | 51 movwf rtc_latched_month ; NO - clip at 12 |
50 movwf rtc_latched_month | 52 |
51 | 53 rcall rtc_check_day ; safeguard day |
52 banksel common ; select bank common | 54 bsf block_rtc_access ; suspend the ISR from accessing the RTC |
53 rcall rtc_check_day ; safeguard day | 55 |
54 bsf block_rtc_access ; suspend the ISR from accessing the RTC | 56 banksel 0xF16 ; addresses F16h through F5Fh are also used by SFRs, but are not part of the access RAM |
55 | 57 |
56 | 58 movlw 0x55 ; | unlock sequence for RTCWREN, EECON2 is located in the access RAM |
57 banksel 0xF16 ; addresses F16h through F5Fh are also used by SFRs, but are not part of the access RAM | 59 movwf EECON2 ; | |
58 | 60 movlw 0xAA ; | |
59 movlw 0x55 ; | unlock sequence for RTCWREN, EECON2 is located in the access RAM | 61 movwf EECON2 ; | |
60 movwf EECON2 ; | | 62 bsf RTCCFG,RTCWREN ; | RTC write unlock, must follow directly after above unlock sequence! |
61 movlw 0xAA ; | | 63 bsf RTCCFG,RTCPTR1 ; | |
62 movwf EECON2 ; | | 64 bsf RTCCFG,RTCPTR0 ; | |
63 bsf RTCCFG,RTCWREN ; RTC write unlock, must follow directly after above unlock sequence! | 65 movff rtc_latched_year,WREG ; get year |
64 bsf RTCCFG,RTCPTR1 | 66 rcall rtc_dec2bcd ; IN: WREG in decimal, OUT: WREG in BCD, also sets to bank16h! |
65 bsf RTCCFG,RTCPTR0 | 67 movwf RTCVALL ; write year |
66 movff rtc_latched_year,WREG | 68 movwf RTCVALH ; dummy write |
67 rcall rtc_dec2bcd ; IN: WREG in decimal, OUT: WREG in BCD, also sets to bank16h! | 69 movff rtc_latched_day,WREG ; get day |
68 movwf RTCVALL ; year | 70 rcall rtc_dec2bcd ; IN: WREG in decimal, OUT: WREG in BCD, also sets to bank16h! |
69 movwf RTCVALH ; dummy write | 71 movwf RTCVALL ; write day |
70 movff rtc_latched_day,WREG | 72 movff rtc_latched_month,WREG ; get month |
71 rcall rtc_dec2bcd ; IN: WREG in decimal, OUT: WREG in BCD, also sets to bank16h! | 73 rcall rtc_dec2bcd ; IN: WREG in decimal, OUT: WREG in BCD, also sets to bank16h! |
72 movwf RTCVALL ; day | 74 movwf RTCVALH ; write month |
73 movff rtc_latched_month,WREG | 75 movff rtc_latched_hour,WREG ; get hour |
74 rcall rtc_dec2bcd ; IN: WREG in decimal, OUT: WREG in BCD, also sets to bank16h! | 76 rcall rtc_dec2bcd ; IN: WREG in decimal, OUT: WREG in BCD, also sets to bank16h! |
75 movwf RTCVALH ; month | 77 movwf RTCVALL ; write hour |
76 movff rtc_latched_hour,WREG | 78 movlw d'0' ; set weekday to 0 (unused) |
77 rcall rtc_dec2bcd ; IN: WREG in decimal, OUT: WREG in BCD, also sets to bank16h! | 79 rcall rtc_dec2bcd ; IN: WREG in decimal, OUT: WREG in BCD, also sets to bank16h! |
78 movwf RTCVALL ; hour | 80 movwf RTCVALH ; (dummy) write weekday |
79 movlw d'0' | 81 movff rtc_latched_secs,WREG ; get seconds |
80 rcall rtc_dec2bcd ; IN: WREG in decimal, OUT: WREG in BCD, also sets to bank16h! | 82 rcall rtc_dec2bcd ; IN: WREG in decimal, OUT: WREG in BCD, also sets to bank16h! |
81 movwf RTCVALH ; weekday | 83 movwf RTCVALL ; write seconds |
82 movff rtc_latched_secs,WREG | 84 movff rtc_latched_mins,WREG ; get minutes |
83 rcall rtc_dec2bcd ; IN: WREG in decimal, OUT: WREG in BCD, also sets to bank16h! | 85 rcall rtc_dec2bcd ; IN: WREG in decimal, OUT: WREG in BCD, also sets to bank16h! |
84 movwf RTCVALL ; seconds | 86 movwf RTCVALH ; write minutes |
85 movff rtc_latched_mins,WREG | 87 |
86 rcall rtc_dec2bcd ; IN: WREG in decimal, OUT: WREG in BCD, also sets to bank16h! | 88 movlw 0x55 ; | probably not needed when clearing RTCWREN |
87 movwf RTCVALH ; minutes | 89 movwf EECON2 ; | |
88 | 90 movlw 0xAA ; | |
89 movlw 0x55 ; | probably not needed when clearing RTCWREN | 91 movwf EECON2 ; | |
90 movwf EECON2 ; | | 92 bcf RTCCFG,RTCWREN ; lock sequence for RTCWREN |
91 movlw 0xAA ; | | 93 |
92 movwf EECON2 ; | | 94 banksel common ; back to bank common |
93 bcf RTCCFG,RTCWREN ; lock sequence for RTCWREN | |
94 | |
95 banksel common | |
96 | 95 |
97 ; update the "live" RTC variables to bridge the time until the ISR reads the updated data from the RTC | 96 ; 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 | 97 movff rtc_latched_year, rtc_year |
99 movff rtc_latched_month,rtc_month | 98 movff rtc_latched_month,rtc_month |
100 movff rtc_latched_day, rtc_day | 99 movff rtc_latched_day, rtc_day |
101 movff rtc_latched_hour, rtc_hour | 100 movff rtc_latched_hour, rtc_hour |
102 movff rtc_latched_mins, rtc_mins | 101 movff rtc_latched_mins, rtc_mins |
103 movff rtc_latched_secs, rtc_secs | 102 movff rtc_latched_secs, rtc_secs |
104 | 103 |
105 bcf block_rtc_access ; allow the ISR to access the RTC again | 104 bcf block_rtc_access ; allow the ISR to access the RTC again |
106 return | 105 return ; done |
107 | 106 |
108 | 107 |
109 rtc_dec2bcd: | 108 rtc_dec2bcd: |
110 banksel common ; switch to bank common | 109 banksel common ; switch to bank common |
111 movwf lo ; input in decimal | 110 movwf lo ; input in decimal |
112 setf hi ; 10s | 111 setf hi ; 10s |
113 rtc_dec2bcd2: | 112 rtc_dec2bcd2: |
114 incf hi,F ; count 10's | 113 incf hi,F ; count 10's |
115 movlw d'10' | 114 movlw d'10' |
116 subwf lo,F | 115 subwf lo,F |
117 btfss STATUS,N | 116 btfss STATUS,N ; result negative? |
118 bra rtc_dec2bcd2 | 117 bra rtc_dec2bcd2 ; NO - loop |
119 movlw d'10' | 118 movlw d'10' ; YES - |
120 addwf lo,F ; 1s | 119 addwf lo,F ; - 1s |
121 swapf hi,W ; swap to bit 7-4 -> WREG | 120 swapf hi,W ; - swap to bit 7-4 -> WREG |
122 addwf lo,W ; result in BCD | 121 addwf lo,W ; - result in BCD |
123 banksel 0xF16 ; switch back to bank for I/O registers | 122 banksel 0xF16 ; - switch back to bank for I/O registers |
124 return | 123 return ; - done |
125 | 124 |
125 | |
126 ; wrap-around the month depending on the number of days per month | |
127 ; Attention: needs to be called in bank isr_backup! | |
126 rtc_check_day: | 128 rtc_check_day: |
127 movlw .28 ; the default February has 28 days | 129 movlw .28 ; the default February has 28 days |
128 movff rtc_latched_year,lo ; bank-safe get of the current year | 130 btfsc rtc_latched_year,0 ; is the year an even year? |
129 btfsc lo,0 ; is the current year an even year? | 131 bra rtc_check_day_1 ; NO - keep the 28 days |
130 bra rtc_check_day_1 ; NO | 132 btfss rtc_latched_year,1 ; YES - is it a multiple of 4 years? |
131 btfss lo,1 ; YES - is it a multiple of 4 years? | 133 movlw .29 ; YES - leap year, February has 29 days |
132 movlw .29 ; YES - leap year, February has 29 days | 134 rtc_check_day_1: |
133 rtc_check_day_1: ; NO - keep the 28 days | 135 movwf backup_hi ; store highest day in February in backup_hi |
134 movwf hi ; store highest day in February in hi | 136 decf rtc_latched_month,W ; compute month - 1 and... |
135 movff rtc_latched_month,lo; bank-safe get of the current month | 137 movwf backup_lo ; store result in backup_lo |
136 dcfsnz lo,F ; current month = January? | 138 movlw .31 ; default highest day is 31 |
137 movlw .31 ; YES - highest day is 31 | 139 dcfsnz backup_lo,F ; month = February? |
138 dcfsnz lo,F ; current month = February? | 140 movf backup_hi,W ; YES - highest day is 28/29 |
139 movf hi,W ; YES - highest day is 28/29 | 141 dcfsnz backup_lo,F ; month = March? |
140 dcfsnz lo,F ; current month = March? | 142 movlw .31 ; YES - highest day is 31 |
141 movlw .31 ; YES - highest day is 31 | 143 dcfsnz backup_lo,F ; month = April? |
142 dcfsnz lo,F ; current month = April? | 144 movlw .30 ; YES - highest day is 30 |
143 movlw .30 ; YES - highest day is 30 | 145 dcfsnz backup_lo,F ; month = May? |
144 dcfsnz lo,F ; current month = May? | 146 movlw .31 ; YES - highest day is 31 |
145 movlw .31 ; YES - highest day is 31 | 147 dcfsnz backup_lo,F ; month = June? |
146 dcfsnz lo,F ; current month = June? | 148 movlw .30 ; YES - highest day is 30 |
147 movlw .30 ; YES - highest day is 30 | 149 dcfsnz backup_lo,F ; month = July? |
148 dcfsnz lo,F ; current month = July? | 150 movlw .31 ; YES - highest day is 31 |
149 movlw .31 ; YES - highest day is 31 | 151 dcfsnz backup_lo,F ; month = August? |
150 dcfsnz lo,F ; current month = August? | 152 movlw .31 ; YES - highest day = 31 |
151 movlw .31 ; YES - highest day = 31 | 153 dcfsnz backup_lo,F ; month = September? |
152 dcfsnz lo,F ; current month = September? | 154 movlw .30 ; YES - highest day = 30 |
153 movlw .30 ; YES - highest day = 30 | 155 dcfsnz backup_lo,F ; month = October? |
154 dcfsnz lo,F ; current month = October? | 156 movlw .31 ; YES - highest day = 31 |
155 movlw .31 ; YES - highest day = 31 | 157 dcfsnz backup_lo,F ; month = November? |
156 dcfsnz lo,F ; current month = November? | 158 movlw .30 ; YES - highest day = 30 |
157 movlw .30 ; YES - highest day = 30 | 159 dcfsnz backup_lo,F ; month = December? |
158 dcfsnz lo,F ; current month = December? | 160 movlw .31 ; YES - highest day = 31 |
159 movlw .31 ; YES - highest day = 31 | 161 cpfsgt rtc_latched_day ; current day > highest day? |
160 movff rtc_latched_day,hi ; bank-safe get of the current day | 162 retlw .1 ; NO - done, signal day was ok |
161 cpfsgt hi ; current day > highest day? | 163 movlw .1 ; YES - wrap around to 1st day in month |
162 return ; NO - day is ok | 164 movwf rtc_latched_day ; - ... |
163 movlw .1 ; YES - wrap around to 1st day in month | 165 retlw .0 ; - done, signal a correction was made |
164 movff WREG,rtc_latched_day; - bank-safe write-back of corrected day | 166 |
165 return ; - done | 167 |
168 ; Add minutes in mpr:2 to the time/date in in rtc_latched and rounds up/down to next full minute | |
169 ; | |
170 ; Attention: This code works for a maximum of 1439 minutes to add (23 hours, 59 minutes)! | |
171 ; | |
172 global rtc_add_minutes | |
173 rtc_add_minutes: | |
174 call convert_time ; convert minutes in hi:lo to hours (up:hi) and minutes (lo) | |
175 banksel isr_backup ; switch to bank isr_backup | |
176 bcf STATUS,C ; clear carry bit | |
177 movlw .30 ; rounding point for seconds | |
178 cpfslt rtc_latched_secs ; seconds < rounding point? | |
179 bsf STATUS,C ; NO - set carry bit -> round up to next full minute | |
180 clrf rtc_latched_secs ; set the second to zero | |
181 movff lo,WREG ; get minutes to add | |
182 addwfc rtc_latched_mins,F ; add minutes to add | |
183 movlw .59 ; limit for minute | |
184 cpfsgt rtc_latched_mins ; resulting minute > 59 ? | |
185 bra rtc_add_minutes_1 ; NO - minute is ok | |
186 movlw .60 ; YES - subtract 60 from resulting minute | |
187 subwf rtc_latched_mins,F ; - ... | |
188 bsf STATUS,C ; - set carry bit | |
189 rtc_add_minutes_1: | |
190 movff hi,WREG ; get hours to add | |
191 addwfc rtc_latched_hour,F ; add hours to add plus carry bit | |
192 movlw .23 ; limit for hour | |
193 cpfsgt rtc_latched_hour ; resulting hour > 23 ? | |
194 bra rtc_add_minutes_2 ; NO - hour is ok | |
195 movlw .24 ; YES - subtract 24 from resulting hour | |
196 subwf rtc_latched_hour ; - ... | |
197 bsf STATUS,C ; - set carry bit | |
198 rtc_add_minutes_2: | |
199 clrf WREG ; create a zero | |
200 addwfc rtc_latched_day,F ; add the carry bit | |
201 rcall rtc_check_day ; check & correct the resulting day with respect to #days in month | |
202 tstfsz WREG ; was the day beyond the last day of the month before correction? | |
203 bra rtc_add_minutes_3 ; NO - done | |
204 banksel isr_backup ; YES - select bank isr_backup again | |
205 incf rtc_latched_month,F ; - increment month | |
206 movlw .12 ; - limit for month | |
207 cpfsgt rtc_latched_month ; - resulting month > 12 ? | |
208 bra rtc_add_minutes_3 ; NO - month is ok | |
209 subwf rtc_latched_month,F ; YES - subtract 12 from resulting month (actually sets back to January) | |
210 incf rtc_latched_year,F ; - increment year | |
211 rtc_add_minutes_3 | |
212 banksel common ; back to bank common | |
213 return ; done | |
214 | |
215 ;----------------------------------------------------------------------------- | |
166 | 216 |
167 END | 217 END |