0
|
1 ;=============================================================================
|
|
2 ;
|
631
|
3 ; File rtc.asm combined next generation V3.08.8
|
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"
|
|
13
|
604
|
14 rtc CODE
|
582
|
15
|
|
16 ;=============================================================================
|
0
|
17
|
|
18 global rtc_init
|
631
|
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
|
623
|
35 global rtc_set_rtc
|
0
|
36 rtc_set_rtc:
|
631
|
37 banksel isr_backup ; select bank ISR data
|
|
38 movlw d'24' ; safeguard hour
|
|
39 cpfslt rtc_latched_hour ; hour < 24 ?
|
|
40 clrf rtc_latched_hour ; NO - reset to 0
|
|
41 movlw d'60' ; safeguard minutes and seconds
|
|
42 cpfslt rtc_latched_mins ; minute < 60 ?
|
|
43 clrf rtc_latched_mins ; NO - reset to 0
|
|
44 cpfslt rtc_latched_secs ; seconds < 60 ?
|
|
45 clrf rtc_latched_secs ; NO - reset to 0
|
|
46 movlw d'100' ; safeguard year
|
|
47 cpfslt rtc_latched_year ; year < 100 ?
|
|
48 clrf rtc_latched_year ; NO - reset to 0
|
|
49 movlw d'12' ; safeguard month
|
|
50 cpfslt rtc_latched_month ; month < 12 ?
|
|
51 movwf rtc_latched_month ; NO - clip at 12
|
623
|
52
|
631
|
53 rcall rtc_check_day ; safeguard day
|
|
54 bsf block_rtc_access ; suspend the ISR from accessing the RTC
|
623
|
55
|
631
|
56 banksel 0xF16 ; addresses F16h through F5Fh are also used by SFRs, but are not part of the access RAM
|
623
|
57
|
631
|
58 movlw 0x55 ; | unlock sequence for RTCWREN, EECON2 is located in the access RAM
|
|
59 movwf EECON2 ; |
|
|
60 movlw 0xAA ; |
|
|
61 movwf EECON2 ; |
|
|
62 bsf RTCCFG,RTCWREN ; | RTC write unlock, must follow directly after above unlock sequence!
|
|
63 bsf RTCCFG,RTCPTR1 ; |
|
|
64 bsf RTCCFG,RTCPTR0 ; |
|
|
65 movff rtc_latched_year,WREG ; get year
|
|
66 rcall rtc_dec2bcd ; IN: WREG in decimal, OUT: WREG in BCD, also sets to bank16h!
|
|
67 movwf RTCVALL ; write year
|
|
68 movwf RTCVALH ; dummy write
|
|
69 movff rtc_latched_day,WREG ; get day
|
|
70 rcall rtc_dec2bcd ; IN: WREG in decimal, OUT: WREG in BCD, also sets to bank16h!
|
|
71 movwf RTCVALL ; write day
|
|
72 movff rtc_latched_month,WREG ; get month
|
|
73 rcall rtc_dec2bcd ; IN: WREG in decimal, OUT: WREG in BCD, also sets to bank16h!
|
|
74 movwf RTCVALH ; write month
|
|
75 movff rtc_latched_hour,WREG ; get hour
|
|
76 rcall rtc_dec2bcd ; IN: WREG in decimal, OUT: WREG in BCD, also sets to bank16h!
|
|
77 movwf RTCVALL ; write hour
|
|
78 movlw d'0' ; set weekday to 0 (unused)
|
|
79 rcall rtc_dec2bcd ; IN: WREG in decimal, OUT: WREG in BCD, also sets to bank16h!
|
|
80 movwf RTCVALH ; (dummy) write weekday
|
|
81 movff rtc_latched_secs,WREG ; get seconds
|
|
82 rcall rtc_dec2bcd ; IN: WREG in decimal, OUT: WREG in BCD, also sets to bank16h!
|
|
83 movwf RTCVALL ; write seconds
|
|
84 movff rtc_latched_mins,WREG ; get minutes
|
|
85 rcall rtc_dec2bcd ; IN: WREG in decimal, OUT: WREG in BCD, also sets to bank16h!
|
|
86 movwf RTCVALH ; write minutes
|
623
|
87
|
631
|
88 movlw 0x55 ; | probably not needed when clearing RTCWREN
|
|
89 movwf EECON2 ; |
|
|
90 movlw 0xAA ; |
|
|
91 movwf EECON2 ; |
|
|
92 bcf RTCCFG,RTCWREN ; lock sequence for RTCWREN
|
623
|
93
|
631
|
94 banksel common ; back to bank common
|
623
|
95
|
|
96 ; update the "live" RTC variables to bridge the time until the ISR reads the updated data from the RTC
|
|
97 movff rtc_latched_year, rtc_year
|
|
98 movff rtc_latched_month,rtc_month
|
|
99 movff rtc_latched_day, rtc_day
|
|
100 movff rtc_latched_hour, rtc_hour
|
|
101 movff rtc_latched_mins, rtc_mins
|
|
102 movff rtc_latched_secs, rtc_secs
|
|
103
|
631
|
104 bcf block_rtc_access ; allow the ISR to access the RTC again
|
|
105 return ; done
|
0
|
106
|
623
|
107
|
0
|
108 rtc_dec2bcd:
|
631
|
109 banksel common ; switch to bank common
|
|
110 movwf lo ; input in decimal
|
|
111 setf hi ; 10s
|
0
|
112 rtc_dec2bcd2:
|
631
|
113 incf hi,F ; count 10's
|
0
|
114 movlw d'10'
|
582
|
115 subwf lo,F
|
631
|
116 btfss STATUS,N ; result negative?
|
|
117 bra rtc_dec2bcd2 ; NO - loop
|
|
118 movlw d'10' ; YES -
|
|
119 addwf lo,F ; - 1s
|
|
120 swapf hi,W ; - swap to bit 7-4 -> WREG
|
|
121 addwf lo,W ; - result in BCD
|
|
122 banksel 0xF16 ; - switch back to bank for I/O registers
|
|
123 return ; - done
|
0
|
124
|
631
|
125
|
|
126 ; wrap-around the month depending on the number of days per month
|
|
127 ; Attention: needs to be called in bank isr_backup!
|
623
|
128 rtc_check_day:
|
631
|
129 movlw .28 ; the default February has 28 days
|
|
130 btfsc rtc_latched_year,0 ; is the year an even year?
|
|
131 bra rtc_check_day_1 ; NO - keep the 28 days
|
|
132 btfss rtc_latched_year,1 ; YES - is it a multiple of 4 years?
|
|
133 movlw .29 ; YES - leap year, February has 29 days
|
|
134 rtc_check_day_1:
|
|
135 movwf backup_hi ; store highest day in February in backup_hi
|
|
136 decf rtc_latched_month,W ; compute month - 1 and...
|
|
137 movwf backup_lo ; store result in backup_lo
|
|
138 movlw .31 ; default highest day is 31
|
|
139 dcfsnz backup_lo,F ; month = February?
|
|
140 movf backup_hi,W ; YES - highest day is 28/29
|
|
141 dcfsnz backup_lo,F ; month = March?
|
|
142 movlw .31 ; YES - highest day is 31
|
|
143 dcfsnz backup_lo,F ; month = April?
|
|
144 movlw .30 ; YES - highest day is 30
|
|
145 dcfsnz backup_lo,F ; month = May?
|
|
146 movlw .31 ; YES - highest day is 31
|
|
147 dcfsnz backup_lo,F ; month = June?
|
|
148 movlw .30 ; YES - highest day is 30
|
|
149 dcfsnz backup_lo,F ; month = July?
|
|
150 movlw .31 ; YES - highest day is 31
|
|
151 dcfsnz backup_lo,F ; month = August?
|
|
152 movlw .31 ; YES - highest day = 31
|
|
153 dcfsnz backup_lo,F ; month = September?
|
|
154 movlw .30 ; YES - highest day = 30
|
|
155 dcfsnz backup_lo,F ; month = October?
|
|
156 movlw .31 ; YES - highest day = 31
|
|
157 dcfsnz backup_lo,F ; month = November?
|
|
158 movlw .30 ; YES - highest day = 30
|
|
159 dcfsnz backup_lo,F ; month = December?
|
|
160 movlw .31 ; YES - highest day = 31
|
|
161 cpfsgt rtc_latched_day ; current day > highest day?
|
|
162 retlw .1 ; NO - done, signal day was ok
|
|
163 movlw .1 ; YES - wrap around to 1st day in month
|
|
164 movwf rtc_latched_day ; - ...
|
|
165 retlw .0 ; - done, signal a correction was made
|
|
166
|
|
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 ;-----------------------------------------------------------------------------
|
623
|
216
|
|
217 END
|