0
|
1 ;=============================================================================
|
|
2 ;
|
623
|
3 ; File rtc.asm combined next generation V3.02.1
|
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
|
623
|
19 global rtc_set_rtc
|
0
|
20 rtc_init:
|
623
|
21 banksel isr_backup ; select bank ISR data
|
|
22 movlw .0
|
|
23 movwf rtc_latched_secs
|
|
24 movlw .0
|
|
25 movwf rtc_latched_mins
|
604
|
26 movlw .12
|
623
|
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
|
0
|
34 rtc_set_rtc:
|
623
|
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
|
|
51
|
|
52 banksel common ; select bank common
|
|
53 rcall rtc_check_day ; safeguard day
|
|
54 bsf block_rtc_access ; suspend the ISR from accessing the RTC
|
|
55
|
|
56
|
|
57 banksel 0xF16 ; addresses F16h through F5Fh are also used by SFRs, but are not part of the access RAM
|
|
58
|
|
59 movlw 0x55 ; | unlock sequence for RTCWREN, EECON2 is located in the access RAM
|
|
60 movwf EECON2 ; |
|
|
61 movlw 0xAA ; |
|
|
62 movwf EECON2 ; |
|
|
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!
|
0
|
68 movwf RTCVALL ; year
|
|
69 movwf RTCVALH ; dummy write
|
623
|
70 movff rtc_latched_day,WREG
|
|
71 rcall rtc_dec2bcd ; IN: WREG in decimal, OUT: WREG in BCD, also sets to bank16h!
|
|
72 movwf RTCVALL ; day
|
|
73 movff rtc_latched_month,WREG
|
|
74 rcall rtc_dec2bcd ; IN: WREG in decimal, OUT: WREG in BCD, also sets to bank16h!
|
|
75 movwf RTCVALH ; month
|
|
76 movff rtc_latched_hour,WREG
|
|
77 rcall rtc_dec2bcd ; IN: WREG in decimal, OUT: WREG in BCD, also sets to bank16h!
|
|
78 movwf RTCVALL ; hour
|
0
|
79 movlw d'0'
|
623
|
80 rcall rtc_dec2bcd ; IN: WREG in decimal, OUT: WREG in BCD, also sets to bank16h!
|
|
81 movwf RTCVALH ; weekday
|
|
82 movff rtc_latched_secs,WREG
|
|
83 rcall rtc_dec2bcd ; IN: WREG in decimal, OUT: WREG in BCD, also sets to bank16h!
|
|
84 movwf RTCVALL ; seconds
|
|
85 movff rtc_latched_mins,WREG
|
|
86 rcall rtc_dec2bcd ; IN: WREG in decimal, OUT: WREG in BCD, also sets to bank16h!
|
|
87 movwf RTCVALH ; minutes
|
|
88
|
|
89 movlw 0x55 ; | probably not needed when clearing RTCWREN
|
|
90 movwf EECON2 ; |
|
|
91 movlw 0xAA ; |
|
|
92 movwf EECON2 ; |
|
|
93 bcf RTCCFG,RTCWREN ; lock sequence for RTCWREN
|
|
94
|
0
|
95 banksel common
|
623
|
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
|
0
|
106 return
|
|
107
|
623
|
108
|
0
|
109 rtc_dec2bcd:
|
623
|
110 banksel common ; switch to bank common
|
|
111 movwf lo ; input in decimal
|
582
|
112 setf hi ; 10s
|
0
|
113 rtc_dec2bcd2:
|
623
|
114 incf hi,F ; count 10's
|
0
|
115 movlw d'10'
|
582
|
116 subwf lo,F
|
0
|
117 btfss STATUS,N
|
|
118 bra rtc_dec2bcd2
|
|
119 movlw d'10'
|
582
|
120 addwf lo,F ; 1s
|
|
121 swapf hi,W ; swap to bit 7-4 -> WREG
|
623
|
122 addwf lo,W ; result in BCD
|
|
123 banksel 0xF16 ; switch back to bank for I/O registers
|
0
|
124 return
|
|
125
|
623
|
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
|
|
167 END
|