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