0
|
1 ; OSTC - diving computer code
|
|
2 ; Copyright (C) 2008 HeinrichsWeikamp GbR
|
|
3
|
|
4 ; This program is free software: you can redistribute it and/or modify
|
|
5 ; it under the terms of the GNU General Public License as published by
|
|
6 ; the Free Software Foundation, either version 3 of the License, or
|
|
7 ; (at your option) any later version.
|
|
8
|
|
9 ; This program is distributed in the hope that it will be useful,
|
|
10 ; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12 ; GNU General Public License for more details.
|
|
13
|
|
14 ; You should have received a copy of the GNU General Public License
|
|
15 ; along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
16
|
|
17
|
|
18 ; routines for AD converter, Realtime clock initialisation
|
|
19 ; written by: Matthias Heinrichs, info@heinrichsweikamp.com
|
|
20 ; written: 10/30/05
|
|
21 ; last updated: 05/15/08
|
|
22 ; known bugs:
|
|
23 ; ToDo:
|
|
24
|
|
25 get_battery_voltage: ; starts ADC and waits until fnished
|
|
26 bsf ADCON0,0 ; power on ADC
|
|
27 nop
|
|
28 bsf ADCON0,1 ; start ADC
|
|
29 get_battery_voltage2:
|
|
30 btfsc ADCON0,1 ; Wait...
|
|
31 bra get_battery_voltage2
|
|
32
|
|
33 ; 3.3V/1024=3,2227mV Input/Bit=9,6680mV Battery/Bit.
|
|
34 ; Example: 434*9,6680mV=4195,9mV Battery.
|
|
35
|
|
36 movff ADRESH,xA+1
|
|
37 movff ADRESL,xA+0
|
|
38 movlw LOW d'966'
|
|
39 movwf xB+0
|
|
40 movlw HIGH d'966'
|
|
41 movwf xB+1
|
|
42 call mult16x16 ; AD_Result*966
|
|
43 movlw d'100'
|
|
44 movwf xB+0
|
|
45 clrf xB+1
|
|
46 call div32x16 ;xC:4 / xB:2 = xC+3:xC+2 with xC+1:xC+0 as remainder
|
|
47 movff xC+0,batt_voltage+0 ; store value
|
|
48 movff xC+1,batt_voltage+1
|
|
49 bcf ADCON0,0 ; power off ADC
|
|
50
|
|
51 ; Check if we should enter deep-sleep mode
|
|
52
|
|
53 movff batt_voltage+0,sub_b+0
|
|
54 movff batt_voltage+1,sub_b+1
|
|
55 movlw LOW d'2600' ; must be greater then 2600mV...
|
|
56 movwf sub_a+0
|
|
57 movlw HIGH d'2600'
|
|
58 movwf sub_a+1
|
|
59 call sub16 ; sub_c = sub_a - sub_b
|
|
60 bcf enter_error_sleep ; Clear flag
|
|
61 btfsc neg_flag ; neg_flag=1 if eeprom40:41 < 2000
|
|
62 bra get_battery_voltage3 ; Battery in OK range
|
|
63
|
|
64 movlw d'2'
|
|
65 movwf fatal_error_code ; Battery very low!
|
|
66 bsf enter_error_sleep ; enter error routine
|
|
67
|
|
68 get_battery_voltage3:
|
|
69 movff amb_pressure+0,sub_b+0
|
|
70 movff amb_pressure+1,sub_b+1
|
|
71 movlw LOW d'15001' ; must be lower then 15001mBar
|
|
72 movwf sub_a+0
|
|
73 movlw HIGH d'15001'
|
|
74 movwf sub_a+1
|
|
75 call sub16 ; sub_c = sub_a - sub_b
|
|
76 bcf enter_error_sleep ; Clear flag
|
|
77 btfss neg_flag ;
|
|
78 bra get_battery_voltage4 ; Pressure in OK range
|
|
79
|
|
80 movlw d'3'
|
|
81 movwf fatal_error_code ; too deep
|
|
82 bsf enter_error_sleep ; enter error routine
|
|
83 ; Continue with rest of routine
|
|
84
|
|
85 get_battery_voltage4:
|
|
86 ; check if the battery control memory needs to be initialised!
|
|
87 bcf initialize_battery1 ; clear check-flags
|
|
88 bcf initialize_battery2
|
|
89
|
|
90 read_int_eeprom d'40' ; get lowest battery voltage seen in mV
|
|
91 movff EEDATA,sub_b+0
|
|
92 read_int_eeprom d'41'
|
|
93 movff EEDATA,sub_b+1
|
|
94
|
|
95 movlw LOW d'2000' ; must be greater then 2000mV...
|
|
96 movwf sub_a+0
|
|
97 movlw HIGH d'2000'
|
|
98 movwf sub_a+1
|
|
99 call sub16 ; sub_c = sub_a - sub_b
|
|
100 btfss neg_flag ; neg_flag=1 if eeprom40:41 < 2000
|
|
101 bsf initialize_battery1 ; battery need to be initialised
|
|
102
|
|
103 movlw LOW d'4500' ; must be lower then 4500mV...
|
|
104 movwf sub_a+0
|
|
105 movlw HIGH d'4500'
|
|
106 movwf sub_a+1
|
|
107 call sub16 ; sub_c = sub_a - sub_b
|
|
108 btfss neg_flag ; neg_flag=1 if eeprom40:41 < 4500
|
|
109 bsf initialize_battery2 ; battery need to be initialised
|
|
110
|
|
111 btfss initialize_battery1 ; battery need to be initialised?
|
|
112 bra get_battery_no_init ; No, we have already valid values, just check for new extremas
|
|
113
|
|
114 btfss initialize_battery2 ; battery need to be initialised?
|
|
115 bra get_battery_no_init ; No, we have already valid values, just check for new extremas
|
|
116
|
|
117 ; Init EEPROM for battery control
|
|
118 ; Reset lowest battery seen
|
|
119 movlw LOW d'4200' ; reset to 4.2V
|
|
120 movwf EEDATA
|
|
121 write_int_eeprom d'40'
|
|
122 movlw HIGH d'4200' ; reset to 4.2V
|
|
123 movwf EEDATA
|
|
124 write_int_eeprom d'41'
|
|
125 movff month,EEDATA
|
|
126 write_int_eeprom d'42'
|
|
127 movff day,EEDATA
|
|
128 write_int_eeprom d'43'
|
|
129 movff year,EEDATA
|
|
130 write_int_eeprom d'44'
|
|
131 movff temperature+0,EEDATA
|
|
132 write_int_eeprom d'45'
|
|
133 movff temperature+1,EEDATA
|
|
134 write_int_eeprom d'46'
|
|
135 ; Reset charge statistics
|
53
|
136 clrf EEDATA
|
|
137 write_int_eeprom d'47' ; last complete charge
|
|
138 write_int_eeprom d'48' ; last complete charge
|
|
139 write_int_eeprom d'49' ; last complete charge
|
|
140 write_int_eeprom d'50' ; total cycles
|
|
141 write_int_eeprom d'51' ; total cycles
|
|
142 write_int_eeprom d'52' ; total complete cycles
|
|
143 write_int_eeprom d'53' ; total complete cycles
|
0
|
144 ; Reset temperature extremas
|
|
145 movff temperature+0,EEDATA ; Reset mimimum extrema
|
|
146 write_int_eeprom d'54'
|
|
147 movff temperature+1,EEDATA
|
|
148 write_int_eeprom d'55'
|
|
149 movff month,EEDATA
|
|
150 write_int_eeprom d'56'
|
|
151 movff day,EEDATA
|
|
152 write_int_eeprom d'57'
|
|
153 movff year,EEDATA
|
|
154 write_int_eeprom d'58'
|
|
155 movff temperature+0,EEDATA ; Reset maximum extrema
|
|
156 write_int_eeprom d'59'
|
|
157 movff temperature+1,EEDATA
|
|
158 write_int_eeprom d'60'
|
|
159 movff month,EEDATA
|
|
160 write_int_eeprom d'61'
|
|
161 movff day,EEDATA
|
|
162 write_int_eeprom d'62'
|
|
163 movff year,EEDATA
|
|
164 write_int_eeprom d'63'
|
|
165
|
|
166 get_battery_no_init:
|
|
167 read_int_eeprom d'40' ; get lowest battery voltage seen in mV
|
|
168 movff EEDATA,sub_b+0
|
|
169 read_int_eeprom d'41'
|
|
170 movff EEDATA,sub_b+1
|
|
171 movff batt_voltage+0,sub_a+0
|
|
172 movff batt_voltage+1,sub_a+1
|
|
173 call sub16 ; sub_c = sub_a - sub_b
|
|
174 btfss neg_flag ; new lowest battery voltage?
|
|
175 return ; no, quit routine
|
|
176 ; Yes, store new value together with the date and temperature values
|
|
177 movff batt_voltage+0,EEDATA
|
|
178 write_int_eeprom d'40'
|
|
179 movff batt_voltage+1,EEDATA
|
|
180 write_int_eeprom d'41'
|
|
181 movff month,EEDATA
|
|
182 write_int_eeprom d'42'
|
|
183 movff day,EEDATA
|
|
184 write_int_eeprom d'43'
|
|
185 movff year,EEDATA
|
|
186 write_int_eeprom d'44'
|
|
187 movff temperature+0,EEDATA
|
|
188 write_int_eeprom d'45'
|
|
189 movff temperature+1,EEDATA
|
|
190 write_int_eeprom d'46'
|
|
191 return
|
|
192
|
|
193 RTCinit: ; resets RTC
|
|
194 movlw 0x80
|
|
195 movwf TMR1H
|
|
196 clrf TMR1L
|
|
197
|
|
198 ; Reset RTC if any part of the time/date is out of range
|
|
199 movlw d'60' ; Limit
|
|
200 cpfslt secs ; Check part
|
|
201 bra RTCinit2 ; Reset time...
|
|
202 movlw d'60' ; Limit
|
|
203 cpfslt mins ; Check part
|
|
204 bra RTCinit2 ; Reset time...
|
|
205 movlw d'24' ; Limit
|
|
206 cpfslt hours ; Check part
|
|
207 bra RTCinit2 ; Reset time...
|
|
208 movlw d'32' ; Limit
|
|
209 cpfslt day ; Check part
|
|
210 bra RTCinit2 ; Reset time...
|
|
211 movlw d'12' ; Limit
|
|
212 cpfslt month ; Check part
|
|
213 bra RTCinit2 ; Reset time...
|
|
214 movlw d'100' ; Limit
|
|
215 cpfslt year ; Check part
|
|
216 bra RTCinit2 ; Reset time...
|
|
217
|
|
218 bsf PIE1, TMR1IE
|
|
219 return
|
|
220
|
|
221 RTCinit2:
|
|
222 movlw .00
|
|
223 movwf secs
|
|
224 movlw .00
|
|
225 movwf mins
|
|
226 movlw .12
|
|
227 movwf hours
|
53
|
228 movlw .1
|
0
|
229 movwf day
|
53
|
230 movlw .8
|
0
|
231 movwf month
|
12
|
232 movlw .10
|
0
|
233 movwf year
|
|
234 bsf PIE1, TMR1IE
|
|
235 return |