Mercurial > public > hwos_code
annotate src/adc_lightsensor.asm @ 219:4b2622e0fd50
init new 4kB Page in logbook memory when pointer is not valid.
author | heinrichsweikamp |
---|---|
date | Sun, 04 Jan 2015 21:24:01 +0100 |
parents | 8fbd8c5ac51f |
children | effd7259f5a5 |
rev | line source |
---|---|
0 | 1 ;============================================================================= |
2 ; | |
3 ; File adc.asm | |
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 | |
11 #include "ostc3.inc" | |
12 #include "math.inc" | |
13 #include "wait.inc" | |
14 #include "eeprom_rs232.inc" | |
113 | 15 #include "i2c.inc" |
0 | 16 |
17 sensors CODE | |
18 | |
19 wait_adc: | |
20 movwf ADCON0 | |
21 nop | |
22 bsf ADCON0,1 ; start ADC | |
23 wait_adc2: | |
24 btfsc ADCON0,1 ; Wait... | |
25 bra wait_adc2 | |
26 return | |
27 | |
28 global get_battery_voltage | |
29 get_battery_voltage: ; starts ADC and waits until fnished | |
200 | 30 btfss cr_hardware |
113 | 31 bra get_battery_voltage1 ; Normal ostc3 hardware |
32 | |
33 call lt2942_get_accumulated_charge | |
34 call lt2942_get_voltage | |
218 | 35 |
36 btfsc divemode | |
37 return ; Not in divemode | |
38 | |
207 | 39 bcf cv_active |
40 bcf cc_active | |
120 | 41 bcf LEDr |
42 bcf TRISJ,2 ; Chrg-Out output | |
43 bsf CHRG_OUT | |
44 | |
45 btfss CHRG_IN | |
207 | 46 bra charge_cc_active |
120 | 47 |
48 bcf CHRG_OUT | |
49 bsf TRISJ,2 ; Chrg-Out high impedance | |
50 | |
51 WAITMS d'1' | |
52 | |
53 btfsc CHRG_IN | |
54 return | |
55 ;cv_active: | |
218 | 56 decfsz safety_stop_countdown,F |
57 return | |
207 | 58 bsf cc_active |
59 bsf cv_active | |
120 | 60 bsf LEDr ; Indicate charging |
61 call lt2942_charge_done ; Reset accumulating registers to 0xFFFF | |
62 WAITMS d'10' | |
63 bcf LEDr ; Indicate charging | |
218 | 64 bsf safety_stop_countdown,0 ; =1 |
120 | 65 return |
66 | |
207 | 67 charge_cc_active: |
68 bsf cc_active | |
120 | 69 bsf LEDr ; Indicate charging |
70 bcf CHRG_OUT | |
71 bsf TRISJ,2 ; Chrg-Out high impedance | |
218 | 72 movlw .5 |
73 movwf safety_stop_countdown | |
113 | 74 return |
75 | |
76 get_battery_voltage1: | |
0 | 77 bsf adc_running ; =1: The ADC is in use |
78 movlw b'00100000' ; 2.048V Vref+ -> 1LSB = 500µV | |
79 movwf ADCON1 | |
80 movlw b'00011001' ; power on ADC, select AN6 | |
81 rcall wait_adc | |
82 | |
83 movff ADRESH,batt_voltage+1 ; store value | |
84 movff ADRESL,batt_voltage+0 ; store value | |
85 bcf ADCON0,0 ; power off ADC | |
86 | |
87 ; Multiply with 2,006 to be excact here... | |
88 ; bcf STATUS,C | |
89 ; rlcf xA+0,F | |
90 ; | |
91 ; rlcf xA+1,F ; x2 | |
92 | |
93 ; movff xA+0,batt_voltage+0 ; store value | |
94 ; movff xA+1,batt_voltage+1 | |
95 | |
96 movlw LOW lithium_36v_low | |
97 movwf sub_a+0 | |
98 movlw HIGH lithium_36v_low | |
99 movwf sub_a+1 | |
100 movff batt_voltage+0,sub_b+0 | |
101 movff batt_voltage+1,sub_b+1 | |
102 call subU16 ; sub_c = sub_a - sub_b | |
103 ; Battery is 3,6V (>lithium_36v_low?) | |
104 btfss neg_flag | |
105 bra get_battery_voltage4 ; No, use 1,5V | |
106 | |
107 bsf battery_is_36v ; Yes, set flag (Cleared in power-on reset only!) | |
108 | |
109 ; Check if the battery is near-dead already | |
110 movlw LOW lithium_36v_empty | |
111 movwf sub_a+0 | |
112 movlw HIGH lithium_36v_empty | |
113 movwf sub_a+1 | |
114 call subU16 ; sub_c = sub_a - sub_b | |
115 ; Battery is not dead yet (>lithium_36v_empty?) | |
116 btfsc neg_flag | |
117 bra get_battery_voltage2 ; Yes, battery is still ok | |
118 | |
119 ; Battery is probably dead very soon | |
120 ; Set ">=24Ah used" into battery gauge registers | |
121 movlw .128 | |
122 movff WREG,battery_gauge+5 | |
123 | |
124 get_battery_voltage2: | |
125 ; Use 3,6V battery gauging mode | |
126 movff battery_gauge+5,xC+3 | |
127 movff battery_gauge+4,xC+2 | |
128 movff battery_gauge+3,xC+1 | |
129 movff battery_gauge+2,xC+0 | |
130 ; battery_gauge:6 is nAs | |
131 ; devide through 65536 | |
132 ; devide through 364 | |
133 ; Result is in percent of a 2,4Ah Battery | |
134 movlw LOW .364 | |
135 movwf xB+0 | |
136 movlw HIGH .364 | |
137 movwf xB+1 | |
138 call div32x16 ; xC:4 / xB:2 = xC+3:xC+2 with xC+1:xC+0 as remainder | |
139 movff xC+0,lo | |
140 ; Limit to 100 | |
141 movlw .100 | |
142 cpfslt lo | |
143 movwf lo | |
144 ; lo will be between 0 (Full) and 100 (empty) | |
145 movf lo,W | |
146 sublw .100 | |
147 movwf lo | |
148 get_battery_voltage3: | |
149 movlw .100 | |
150 cpfslt lo | |
151 movwf lo | |
152 ; lo will be between 100 (Full) and 0 (empty) | |
37
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
153 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
154 ; use 3,6V battery sensing based on 50mA load |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
155 ; 75% |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
156 movff batt_voltage+0,sub_b+0 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
157 movff batt_voltage+1,sub_b+1 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
158 movlw LOW lithium_36v_75 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
159 movwf sub_a+0 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
160 movlw HIGH lithium_36v_75 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
161 movwf sub_a+1 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
162 call subU16 ; sub_c = sub_a - sub_b |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
163 btfsc neg_flag |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
164 bra get_battery_voltage3a |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
165 movlw .75 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
166 movwf lo |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
167 get_battery_voltage3a: |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
168 ; 50% |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
169 movlw LOW lithium_36v_50 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
170 movwf sub_a+0 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
171 movlw HIGH lithium_36v_50 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
172 movwf sub_a+1 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
173 call subU16 ; sub_c = sub_a - sub_b |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
174 btfsc neg_flag |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
175 bra get_battery_voltage3b |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
176 movlw .50 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
177 movwf lo |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
178 get_battery_voltage3b: |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
179 ; 25% |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
180 movlw LOW lithium_36v_25 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
181 movwf sub_a+0 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
182 movlw HIGH lithium_36v_25 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
183 movwf sub_a+1 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
184 call subU16 ; sub_c = sub_a - sub_b |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
185 btfsc neg_flag |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
186 bra get_battery_voltage3c |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
187 movlw .25 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
188 movwf lo |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
189 get_battery_voltage3c: |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
190 ; 10% |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
191 movlw LOW lithium_36v_10 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
192 movwf sub_a+0 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
193 movlw HIGH lithium_36v_10 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
194 movwf sub_a+1 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
195 call subU16 ; sub_c = sub_a - sub_b |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
196 btfsc neg_flag |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
197 bra get_battery_voltage3d |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
198 movlw .10 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
199 movwf lo |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
200 get_battery_voltage3d: |
44
18fe4e668baa
BUGFIX: Percent display with 1,5V batteries
heinrichsweikamp
parents:
37
diff
changeset
|
201 movlw .100 |
18fe4e668baa
BUGFIX: Percent display with 1,5V batteries
heinrichsweikamp
parents:
37
diff
changeset
|
202 cpfslt lo |
18fe4e668baa
BUGFIX: Percent display with 1,5V batteries
heinrichsweikamp
parents:
37
diff
changeset
|
203 movwf lo |
18fe4e668baa
BUGFIX: Percent display with 1,5V batteries
heinrichsweikamp
parents:
37
diff
changeset
|
204 ; lo will be between 100 (Full) and 0 (empty) |
0 | 205 movf batt_percent,W |
206 cpfsgt lo ; keep batt_percent on the lowest value found | |
207 movff lo,batt_percent ; store value | |
37
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
208 ; btfsc battery_is_36v ; but always use computed value for 3,6V battery |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
209 ; movff lo,batt_percent ; store value |
0 | 210 bcf adc_running ; =1: The ADC is in use |
211 return | |
212 | |
213 get_battery_voltage4: | |
214 ; Use 1,5V battery voltage mode | |
215 ; Use approximation (batt_voltage:2-aa_15v_low)/4 = lo | |
216 movff batt_voltage+0,sub_a+0 | |
217 movff batt_voltage+1,sub_a+1 | |
218 movlw LOW aa_15v_low | |
219 movwf sub_b+0 | |
220 movlw HIGH aa_15v_low | |
221 movwf sub_b+1 | |
222 call subU16 ; sub_c = sub_a - sub_b | |
223 bcf STATUS,C | |
224 rrcf sub_c+1 | |
225 rrcf sub_c+0 ; /2 | |
226 bcf STATUS,C | |
227 rrcf sub_c+1 | |
228 rrcf sub_c+0 ; /4 | |
229 movff sub_c+0,lo | |
44
18fe4e668baa
BUGFIX: Percent display with 1,5V batteries
heinrichsweikamp
parents:
37
diff
changeset
|
230 bra get_battery_voltage3d ; Check limits and return |
0 | 231 |
232 global get_ambient_level | |
233 get_ambient_level: ; starts ADC and waits until finished | |
234 btfsc adc_running ; ADC in use? | |
235 return ; Yes, return | |
236 | |
200 | 237 btfss cr_hardware |
113 | 238 bra get_ambient_level1 ; Normal ostc3 hardware |
203 | 239 |
240 banksel isr_backup ; Back to Bank0 ISR data | |
241 movff opt_brightness,isr1_temp | |
242 incf isr1_temp,F ; adjust 0-2 to 1-3 | |
243 movlw ambient_light_max_high_cr; cR hardware brightest setting | |
244 dcfsnz isr1_temp,F | |
245 movlw ambient_light_max_eco ; brightest setting | |
246 dcfsnz isr1_temp,F | |
247 movlw ambient_light_max_medium; brightest setting | |
248 | |
249 movff WREG,ambient_light+0 ; Set to max. | |
250 movff ambient_light+0,max_CCPR1L ; Store value for dimming in TMR7 interrupt | |
251 return | |
113 | 252 |
253 get_ambient_level1: | |
0 | 254 movlw b'00000000' ; Vref+ = Vdd |
255 movwf ADCON1 | |
256 movlw b'00011101' ; power on ADC, select AN7 | |
257 rcall wait_adc | |
258 | |
259 movff ADRESH,ambient_light+1 | |
260 movff ADRESL,ambient_light+0 | |
261 bcf ADCON0,0 ; power off ADC | |
262 | |
263 ; ambient_light:2 is between 4096 (direct sunlight) and about 200 (darkness) | |
264 ; First: Devide through 16 | |
265 bcf STATUS,C | |
266 rrcf ambient_light+1 | |
267 rrcf ambient_light+0 | |
268 bcf STATUS,C | |
269 rrcf ambient_light+1 | |
270 rrcf ambient_light+0 | |
271 bcf STATUS,C | |
272 rrcf ambient_light+1 | |
273 rrcf ambient_light+0 | |
274 bcf STATUS,C | |
275 rrcf ambient_light+1 | |
276 rrcf ambient_light+0 | |
277 ; Result: ambient_light:2/16 | |
278 ; Now, make sure to have value between ambient_light_low and ambient_light_max | |
279 | |
280 movlw .254 | |
281 tstfsz ambient_light+1 ; >255? | |
282 movwf ambient_light+0 ; avoid ADC clipping | |
283 | |
284 incfsz ambient_light+0,W ; =255? | |
285 bra get_ambient_level2 ; No, continue | |
286 | |
287 movlw .254 | |
288 movwf ambient_light+0 ; avoid ADC clipping | |
289 | |
290 get_ambient_level2: | |
201 | 291 movlw .10 |
292 subwf ambient_light+0,F ; Subtract 10 (ADC Offset) | |
293 btfsc STATUS,N | |
294 movwf ambient_light+0 ; avoid clipping | |
295 | |
0 | 296 banksel isr_backup ; Back to Bank0 ISR data |
297 movff opt_brightness,isr1_temp | |
298 | |
299 btfsc RCSTA1,7 ; UART module on? | |
300 clrf isr1_temp ; Yes, set temporally to eco mode | |
301 | |
302 incf isr1_temp,F ; adjust 0-2 to 1-3 | |
303 | |
304 banksel common ; flag is in bank1 | |
200 | 305 movlw ambient_light_max_high_cr; cR hardware brightest setting |
306 btfss cr_hardware | |
136 | 307 movlw ambient_light_max_high_15V; 1,5V battery brightest setting |
308 btfsc battery_is_36v ; 3,6V battery in use? | |
309 movlw ambient_light_max_high_36V ; 3,6V battery brightest setting | |
0 | 310 banksel isr_backup ; Back to Bank0 ISR data |
311 | |
312 dcfsnz isr1_temp,F | |
313 movlw ambient_light_max_eco ; brightest setting | |
314 dcfsnz isr1_temp,F | |
315 movlw ambient_light_max_medium; brightest setting | |
316 | |
317 banksel common ; ambient_light is in Bank1 | |
318 incf ambient_light+0,F ; +1 | |
319 cpfslt ambient_light+0 ; smaller then WREG? | |
320 movwf ambient_light+0 ; No, set to max. | |
321 | |
322 banksel isr_backup ; Back to Bank0 ISR data | |
323 movff opt_brightness,isr1_temp | |
324 incf isr1_temp,F ; adjust 0-2 to 1-3 | |
325 movlw ambient_light_min_high ; darkest setting | |
326 | |
327 dcfsnz isr1_temp,F | |
328 movlw ambient_light_min_eco ; darkest setting | |
329 dcfsnz isr1_temp,F | |
330 movlw ambient_light_min_medium; darkest setting | |
331 dcfsnz isr1_temp,F | |
332 movlw ambient_light_min_high ; darkest setting | |
333 | |
334 banksel common ; ambient_light is in Bank1 | |
335 cpfsgt ambient_light+0 ; bigger then WREG? | |
336 movwf ambient_light+0 ; No, set to min | |
337 | |
338 movff ambient_light+0,max_CCPR1L ; Store value for dimming in TMR7 interrupt | |
339 return | |
340 | |
113 | 341 global get_analog_inputs |
342 get_analog_inputs: ; starts ADC and waits until finished | |
0 | 343 bsf adc_running ; =1: The ADC is in use |
113 | 344 btfsc TFT_PWM |
345 bra get_analog_inputs ; Wait for PWM low | |
346 movlw b'00100000' ; 2.048V Vref+ -> 1LSB = 500µV | |
347 movwf ADCON1 | |
348 movlw b'00100001' ; power on ADC, select AN8 | |
349 rcall wait_adc | |
350 bcf STATUS,C | |
351 rrcf ADRESH,F ; /2 | |
189
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
352 rrcf ADRESL,W |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
353 ; add to o2_mv_sensor1:2 |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
354 addwf o2_mv_sensor1+0,F |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
355 movf ADRESH,W |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
356 addwfc o2_mv_sensor1+1,F |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
357 ; Devide by 2 |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
358 bcf STATUS,C |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
359 rrcf o2_mv_sensor1+1,F ; /2 |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
360 rrcf o2_mv_sensor1+0,F |
196
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
361 |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
362 movlw HIGH ignore_mv |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
363 cpfsgt o2_mv_sensor1+1 ; >ignore_mv? |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
364 bra get_analog_inputs2a ; No |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
365 ; Yes, ignore this reading |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
366 clrf o2_mv_sensor1+1 |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
367 clrf o2_mv_sensor1+0 |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
368 get_analog_inputs2a: |
187
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
369 ; Ignore 1,2mV noise for not-connected inputs |
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
370 tstfsz o2_mv_sensor1+1 ; >25,5mV? |
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
371 bra get_analog_inputs2 ; Yes, skip here |
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
372 movlw .12 |
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
373 cpfsgt o2_mv_sensor1+0 ; >1,2mV? |
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
374 clrf o2_mv_sensor1+0 ; no, clear result |
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
375 get_analog_inputs2: |
113 | 376 movlw b'00100101' ; power on ADC, select AN9 |
0 | 377 rcall wait_adc |
113 | 378 bcf STATUS,C |
379 rrcf ADRESH,F ; /2 | |
189
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
380 rrcf ADRESL,W |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
381 ; add to o2_mv_sensor2:2 |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
382 addwf o2_mv_sensor2+0,F |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
383 movf ADRESH,W |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
384 addwfc o2_mv_sensor2+1,F |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
385 ; Devide by 2 |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
386 bcf STATUS,C |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
387 rrcf o2_mv_sensor2+1,F ; /2 |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
388 rrcf o2_mv_sensor2+0,F |
196
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
389 |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
390 movlw HIGH ignore_mv |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
391 cpfsgt o2_mv_sensor2+1 ; >ignore_mv? |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
392 bra get_analog_inputs3a ; No |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
393 ; Yes, ignore this reading |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
394 clrf o2_mv_sensor2+1 |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
395 clrf o2_mv_sensor2+0 |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
396 get_analog_inputs3a: |
187
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
397 ; Ignore 1,2mV noise for not-connected inputs |
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
398 tstfsz o2_mv_sensor2+1 ; >25,5mV? |
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
399 bra get_analog_inputs3 ; Yes, skip here |
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
400 movlw .12 |
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
401 cpfsgt o2_mv_sensor2+0 ; >1,2mV? |
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
402 clrf o2_mv_sensor2+0 ; no, clear result |
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
403 get_analog_inputs3: |
113 | 404 movlw b'00101001' ; power on ADC, select AN10 |
405 rcall wait_adc | |
406 bcf STATUS,C | |
407 rrcf ADRESH,F ; /2 | |
189
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
408 rrcf ADRESL,W |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
409 ; add to o2_mv_sensor3:2 |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
410 addwf o2_mv_sensor3+0,F |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
411 movf ADRESH,W |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
412 addwfc o2_mv_sensor3+1,F |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
413 ; Devide by 2 |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
414 bcf STATUS,C |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
415 rrcf o2_mv_sensor3+1,F ; /2 |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
416 rrcf o2_mv_sensor3+0,F |
196
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
417 |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
418 movlw HIGH ignore_mv |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
419 cpfsgt o2_mv_sensor3+1 ; >ignore_mv? |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
420 bra get_analog_inputs4a ; No |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
421 ; Yes, ignore this reading |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
422 clrf o2_mv_sensor3+1 |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
423 clrf o2_mv_sensor3+0 |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
424 get_analog_inputs4a: |
187
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
425 ; Ignore 1,2mV noise for not-connected inputs |
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
426 tstfsz o2_mv_sensor3+1 ; >25,5mV? |
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
427 bra get_analog_inputs4 ; Yes, skip here |
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
428 movlw .12 |
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
429 cpfsgt o2_mv_sensor3+0 ; >1,2mV? |
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
430 clrf o2_mv_sensor3+0 ; no, clear result |
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
431 get_analog_inputs4: |
0 | 432 bcf ADCON0,0 ; power off ADC |
433 bcf adc_running ; =1: The ADC is in use | |
434 return | |
435 | |
113 | 436 global piezo_config ; Sets up piezo sensitivity of heinrichs weikamp Piezo buttons (~30ms) |
117 | 437 piezo_config: ; Settings between 20 and 200 |
158 | 438 movlw .75 ; right button |
117 | 439 rcall piezo_config_tx |
158 | 440 movlw .75 ; left button |
117 | 441 rcall piezo_config_tx |
442 movlw .200 ; reserved | |
443 rcall piezo_config_tx | |
444 movlw .200 ; reserved | |
445 rcall piezo_config_tx | |
113 | 446 return |
447 | |
204 | 448 global piezo_config_dive ; Sets up piezo sensitivity of heinrichs weikamp Piezo buttons (~30ms) |
449 piezo_config_dive: ; Settings between 20 and 200 | |
450 movlw .60 ; right button | |
451 rcall piezo_config_tx | |
452 movlw .60 ; left button | |
453 rcall piezo_config_tx | |
454 movlw .200 ; reserved | |
455 rcall piezo_config_tx | |
456 movlw .200 ; reserved | |
457 rcall piezo_config_tx | |
458 return | |
459 | |
117 | 460 piezo_config_tx: ; Send one byte |
113 | 461 movwf uart1_temp ; Store byte |
462 movlw .8 | |
463 movwf uart2_temp ; Bit counter | |
464 bcf TX3_PIEZO_CFG ; Startbit | |
465 rcall piezo_config_wait_bit | |
466 piezo_config_tx_loop: | |
467 btfss uart1_temp,0 ; LSB first | |
468 bcf TX3_PIEZO_CFG | |
469 btfsc uart1_temp,0 ; LSB first | |
470 bsf TX3_PIEZO_CFG | |
471 rcall piezo_config_wait_bit | |
472 rrncf uart1_temp,F | |
473 decfsz uart2_temp,F | |
474 bra piezo_config_tx_loop | |
475 bsf TX3_PIEZO_CFG ; Stopbit | |
476 rcall piezo_config_wait_bit | |
477 return | |
478 | |
479 piezo_config_wait_bit: | |
480 setf TMR5H | |
481 movlw .255-.26 ;26 x 31,5µs = 819us | |
482 movwf TMR5L | |
483 bcf PIR5,TMR5IF ; Clear flag | |
484 piezo_config_wait_bit3: | |
485 btfss PIR5,TMR5IF | |
486 bra piezo_config_wait_bit3 ; Wait loop | |
487 return | |
488 | |
0 | 489 global reset_battery_pointer |
490 reset_battery_pointer: ; Resets battery pointer 0x07-0x0C and battery_gauge:5 | |
113 | 491 extern lt2942_charge_done |
492 call lt2942_charge_done ; Reset accumulating registers to 0xFFFF | |
493 clrf EEADRH | |
0 | 494 clrf EEDATA ; Delete to zero |
495 write_int_eeprom 0x07 | |
496 write_int_eeprom 0x08 | |
497 write_int_eeprom 0x09 | |
498 write_int_eeprom 0x0A | |
499 write_int_eeprom 0x0B | |
500 write_int_eeprom 0x0C | |
501 banksel battery_gauge+0 | |
502 clrf battery_gauge+0 | |
503 clrf battery_gauge+1 | |
504 clrf battery_gauge+2 | |
505 clrf battery_gauge+3 | |
506 clrf battery_gauge+4 | |
507 clrf battery_gauge+5 | |
508 banksel common | |
509 movlw .100 | |
510 movwf batt_percent | |
511 return | |
512 | |
513 | |
514 END |