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