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