Mercurial > public > hwos_code
annotate src/adc_lightsensor.asm @ 302:be341d0df3ee
default branch
author | heinrichsweikamp |
---|---|
date | Mon, 01 Jun 2015 11:50:34 +0200 |
parents | 653a3ab08062 |
children | 34e2f5bdc5a2 |
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 | |
235
23311219dacc
under construction: new hardware_flag to configure different hardware versions
heinrichsweikamp
parents:
220
diff
changeset
|
30 btfss rechargeable |
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 | |
135 ; devide through 364 | |
136 ; Result is in percent of a 2,4Ah Battery | |
137 movlw LOW .364 | |
138 movwf xB+0 | |
139 movlw HIGH .364 | |
140 movwf xB+1 | |
141 call div32x16 ; xC:4 / xB:2 = xC+3:xC+2 with xC+1:xC+0 as remainder | |
142 movff xC+0,lo | |
143 ; Limit to 100 | |
144 movlw .100 | |
145 cpfslt lo | |
146 movwf lo | |
147 ; lo will be between 0 (Full) and 100 (empty) | |
148 movf lo,W | |
149 sublw .100 | |
150 movwf lo | |
151 get_battery_voltage3: | |
152 movlw .100 | |
153 cpfslt lo | |
154 movwf lo | |
155 ; 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
|
156 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
157 ; 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
|
158 ; 75% |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
159 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
|
160 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
|
161 movlw LOW 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+0 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
163 movlw HIGH lithium_36v_75 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
164 movwf sub_a+1 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
165 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
|
166 btfsc neg_flag |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
167 bra get_battery_voltage3a |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
168 movlw .75 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
169 movwf lo |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
170 get_battery_voltage3a: |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
171 ; 50% |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
172 movlw LOW 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+0 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
174 movlw HIGH lithium_36v_50 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
175 movwf sub_a+1 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
176 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
|
177 btfsc neg_flag |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
178 bra get_battery_voltage3b |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
179 movlw .50 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
180 movwf lo |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
181 get_battery_voltage3b: |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
182 ; 25% |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
183 movlw LOW 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+0 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
185 movlw HIGH lithium_36v_25 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
186 movwf sub_a+1 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
187 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
|
188 btfsc neg_flag |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
189 bra get_battery_voltage3c |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
190 movlw .25 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
191 movwf lo |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
192 get_battery_voltage3c: |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
193 ; 10% |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
194 movlw LOW 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+0 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
196 movlw HIGH lithium_36v_10 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
197 movwf sub_a+1 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
198 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
|
199 btfsc neg_flag |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
200 bra get_battery_voltage3d |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
201 movlw .10 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
202 movwf lo |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
203 get_battery_voltage3d: |
44
18fe4e668baa
BUGFIX: Percent display with 1,5V batteries
heinrichsweikamp
parents:
37
diff
changeset
|
204 movlw .100 |
18fe4e668baa
BUGFIX: Percent display with 1,5V batteries
heinrichsweikamp
parents:
37
diff
changeset
|
205 cpfslt lo |
18fe4e668baa
BUGFIX: Percent display with 1,5V batteries
heinrichsweikamp
parents:
37
diff
changeset
|
206 movwf lo |
18fe4e668baa
BUGFIX: Percent display with 1,5V batteries
heinrichsweikamp
parents:
37
diff
changeset
|
207 ; lo will be between 100 (Full) and 0 (empty) |
0 | 208 movf batt_percent,W |
209 cpfsgt lo ; keep batt_percent on the lowest value found | |
210 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
|
211 ; 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
|
212 ; movff lo,batt_percent ; store value |
0 | 213 bcf adc_running ; =1: The ADC is in use |
214 return | |
215 | |
216 get_battery_voltage4: | |
217 ; Use 1,5V battery voltage mode | |
218 ; Use approximation (batt_voltage:2-aa_15v_low)/4 = lo | |
219 movff batt_voltage+0,sub_a+0 | |
220 movff batt_voltage+1,sub_a+1 | |
221 movlw LOW aa_15v_low | |
222 movwf sub_b+0 | |
223 movlw HIGH aa_15v_low | |
224 movwf sub_b+1 | |
225 call subU16 ; sub_c = sub_a - sub_b | |
226 bcf STATUS,C | |
227 rrcf sub_c+1 | |
228 rrcf sub_c+0 ; /2 | |
229 bcf STATUS,C | |
230 rrcf sub_c+1 | |
231 rrcf sub_c+0 ; /4 | |
232 movff sub_c+0,lo | |
44
18fe4e668baa
BUGFIX: Percent display with 1,5V batteries
heinrichsweikamp
parents:
37
diff
changeset
|
233 bra get_battery_voltage3d ; Check limits and return |
0 | 234 |
235 global get_ambient_level | |
236 get_ambient_level: ; starts ADC and waits until finished | |
237 btfsc adc_running ; ADC in use? | |
238 return ; Yes, return | |
239 | |
236 | 240 btfsc ambient_sensor |
113 | 241 bra get_ambient_level1 ; Normal ostc3 hardware |
203 | 242 |
243 banksel isr_backup ; Back to Bank0 ISR data | |
244 movff opt_brightness,isr1_temp | |
245 incf isr1_temp,F ; adjust 0-2 to 1-3 | |
246 movlw ambient_light_max_high_cr; cR hardware brightest setting | |
247 dcfsnz isr1_temp,F | |
248 movlw ambient_light_max_eco ; brightest setting | |
249 dcfsnz isr1_temp,F | |
250 movlw ambient_light_max_medium; brightest setting | |
251 | |
252 movff WREG,ambient_light+0 ; Set to max. | |
253 movff ambient_light+0,max_CCPR1L ; Store value for dimming in TMR7 interrupt | |
254 return | |
113 | 255 |
256 get_ambient_level1: | |
0 | 257 movlw b'00000000' ; Vref+ = Vdd |
258 movwf ADCON1 | |
259 movlw b'00011101' ; power on ADC, select AN7 | |
260 rcall wait_adc | |
261 | |
262 movff ADRESH,ambient_light+1 | |
263 movff ADRESL,ambient_light+0 | |
264 bcf ADCON0,0 ; power off ADC | |
265 | |
266 ; ambient_light:2 is between 4096 (direct sunlight) and about 200 (darkness) | |
267 ; First: Devide through 16 | |
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 bcf STATUS,C | |
278 rrcf ambient_light+1 | |
279 rrcf ambient_light+0 | |
280 ; Result: ambient_light:2/16 | |
281 ; Now, make sure to have value between ambient_light_low and ambient_light_max | |
282 | |
283 movlw .254 | |
284 tstfsz ambient_light+1 ; >255? | |
285 movwf ambient_light+0 ; avoid ADC clipping | |
286 | |
287 incfsz ambient_light+0,W ; =255? | |
288 bra get_ambient_level2 ; No, continue | |
289 | |
290 movlw .254 | |
291 movwf ambient_light+0 ; avoid ADC clipping | |
292 | |
293 get_ambient_level2: | |
201 | 294 movlw .10 |
295 subwf ambient_light+0,F ; Subtract 10 (ADC Offset) | |
296 btfsc STATUS,N | |
297 movwf ambient_light+0 ; avoid clipping | |
298 | |
0 | 299 banksel isr_backup ; Back to Bank0 ISR data |
300 movff opt_brightness,isr1_temp | |
301 | |
302 btfsc RCSTA1,7 ; UART module on? | |
303 clrf isr1_temp ; Yes, set temporally to eco mode | |
304 | |
305 incf isr1_temp,F ; adjust 0-2 to 1-3 | |
306 | |
307 banksel common ; flag is in bank1 | |
200 | 308 movlw ambient_light_max_high_cr; cR hardware brightest setting |
235
23311219dacc
under construction: new hardware_flag to configure different hardware versions
heinrichsweikamp
parents:
220
diff
changeset
|
309 btfss rechargeable |
136 | 310 movlw ambient_light_max_high_15V; 1,5V battery brightest setting |
311 btfsc battery_is_36v ; 3,6V battery in use? | |
312 movlw ambient_light_max_high_36V ; 3,6V battery brightest setting | |
0 | 313 banksel isr_backup ; Back to Bank0 ISR data |
314 | |
315 dcfsnz isr1_temp,F | |
316 movlw ambient_light_max_eco ; brightest setting | |
317 dcfsnz isr1_temp,F | |
318 movlw ambient_light_max_medium; brightest setting | |
319 | |
320 banksel common ; ambient_light is in Bank1 | |
321 incf ambient_light+0,F ; +1 | |
322 cpfslt ambient_light+0 ; smaller then WREG? | |
323 movwf ambient_light+0 ; No, set to max. | |
324 | |
325 banksel isr_backup ; Back to Bank0 ISR data | |
326 movff opt_brightness,isr1_temp | |
327 incf isr1_temp,F ; adjust 0-2 to 1-3 | |
328 movlw ambient_light_min_high ; darkest setting | |
329 | |
330 dcfsnz isr1_temp,F | |
331 movlw ambient_light_min_eco ; darkest setting | |
332 dcfsnz isr1_temp,F | |
333 movlw ambient_light_min_medium; darkest setting | |
334 dcfsnz isr1_temp,F | |
335 movlw ambient_light_min_high ; darkest setting | |
336 | |
337 banksel common ; ambient_light is in Bank1 | |
338 cpfsgt ambient_light+0 ; bigger then WREG? | |
339 movwf ambient_light+0 ; No, set to min | |
340 | |
341 movff ambient_light+0,max_CCPR1L ; Store value for dimming in TMR7 interrupt | |
342 return | |
343 | |
113 | 344 global get_analog_inputs |
345 get_analog_inputs: ; starts ADC and waits until finished | |
0 | 346 bsf adc_running ; =1: The ADC is in use |
113 | 347 btfsc TFT_PWM |
348 bra get_analog_inputs ; Wait for PWM low | |
349 movlw b'00100000' ; 2.048V Vref+ -> 1LSB = 500µV | |
350 movwf ADCON1 | |
351 movlw b'00100001' ; power on ADC, select AN8 | |
352 rcall wait_adc | |
353 bcf STATUS,C | |
354 rrcf ADRESH,F ; /2 | |
189
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
355 rrcf ADRESL,W |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
356 ; add to o2_mv_sensor1:2 |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
357 addwf o2_mv_sensor1+0,F |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
358 movf ADRESH,W |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
359 addwfc o2_mv_sensor1+1,F |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
360 ; Devide by 2 |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
361 bcf STATUS,C |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
362 rrcf o2_mv_sensor1+1,F ; /2 |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
363 rrcf o2_mv_sensor1+0,F |
196
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
364 |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
365 movlw HIGH ignore_mv |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
366 cpfsgt o2_mv_sensor1+1 ; >ignore_mv? |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
367 bra get_analog_inputs2a ; No |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
368 ; Yes, ignore this reading |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
369 clrf o2_mv_sensor1+1 |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
370 clrf o2_mv_sensor1+0 |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
371 get_analog_inputs2a: |
187
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
372 ; Ignore 1,2mV noise for not-connected inputs |
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
373 tstfsz o2_mv_sensor1+1 ; >25,5mV? |
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
374 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
|
375 movlw .19 |
bddb63835035
increase min input voltage from 1,2mV to 1,9mV (cR only)
heinrichsweikamp
parents:
266
diff
changeset
|
376 cpfsgt o2_mv_sensor1+0 ; >1,9mV? |
187
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
377 clrf o2_mv_sensor1+0 ; no, clear result |
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
378 get_analog_inputs2: |
113 | 379 movlw b'00100101' ; power on ADC, select AN9 |
0 | 380 rcall wait_adc |
113 | 381 bcf STATUS,C |
382 rrcf ADRESH,F ; /2 | |
189
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
383 rrcf ADRESL,W |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
384 ; add to o2_mv_sensor2:2 |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
385 addwf o2_mv_sensor2+0,F |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
386 movf ADRESH,W |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
387 addwfc o2_mv_sensor2+1,F |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
388 ; Devide by 2 |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
389 bcf STATUS,C |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
390 rrcf o2_mv_sensor2+1,F ; /2 |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
391 rrcf o2_mv_sensor2+0,F |
196
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
392 |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
393 movlw HIGH ignore_mv |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
394 cpfsgt o2_mv_sensor2+1 ; >ignore_mv? |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
395 bra get_analog_inputs3a ; No |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
396 ; Yes, ignore this reading |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
397 clrf o2_mv_sensor2+1 |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
398 clrf o2_mv_sensor2+0 |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
399 get_analog_inputs3a: |
187
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
400 ; Ignore 1,2mV noise for not-connected inputs |
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
401 tstfsz o2_mv_sensor2+1 ; >25,5mV? |
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
402 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
|
403 movlw .19 |
bddb63835035
increase min input voltage from 1,2mV to 1,9mV (cR only)
heinrichsweikamp
parents:
266
diff
changeset
|
404 cpfsgt o2_mv_sensor2+0 ; >1,9mV? |
187
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
405 clrf o2_mv_sensor2+0 ; no, clear result |
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
406 get_analog_inputs3: |
113 | 407 movlw b'00101001' ; power on ADC, select AN10 |
408 rcall wait_adc | |
409 bcf STATUS,C | |
410 rrcf ADRESH,F ; /2 | |
189
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
411 rrcf ADRESL,W |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
412 ; add to o2_mv_sensor3:2 |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
413 addwf o2_mv_sensor3+0,F |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
414 movf ADRESH,W |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
415 addwfc o2_mv_sensor3+1,F |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
416 ; Devide by 2 |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
417 bcf STATUS,C |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
418 rrcf o2_mv_sensor3+1,F ; /2 |
e79bc535ef9e
ignore un-calibrated sensors even if they become valid
heinrichsweikamp
parents:
187
diff
changeset
|
419 rrcf o2_mv_sensor3+0,F |
196
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
420 |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
421 movlw HIGH ignore_mv |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
422 cpfsgt o2_mv_sensor3+1 ; >ignore_mv? |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
423 bra get_analog_inputs4a ; No |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
424 ; Yes, ignore this reading |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
425 clrf o2_mv_sensor3+1 |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
426 clrf o2_mv_sensor3+0 |
a0e0ddfc1b49
suppress ghost readings for long, open cables
heinrichsweikamp
parents:
189
diff
changeset
|
427 get_analog_inputs4a: |
187
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
428 ; Ignore 1,2mV noise for not-connected inputs |
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
429 tstfsz o2_mv_sensor3+1 ; >25,5mV? |
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
430 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
|
431 movlw .19 |
bddb63835035
increase min input voltage from 1,2mV to 1,9mV (cR only)
heinrichsweikamp
parents:
266
diff
changeset
|
432 cpfsgt o2_mv_sensor3+0 ; >1,9mV? |
187
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
433 clrf o2_mv_sensor3+0 ; no, clear result |
669b5d00706d
CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents:
158
diff
changeset
|
434 get_analog_inputs4: |
0 | 435 bcf ADCON0,0 ; power off ADC |
436 bcf adc_running ; =1: The ADC is in use | |
437 return | |
438 | |
113 | 439 global piezo_config ; Sets up piezo sensitivity of heinrichs weikamp Piezo buttons (~30ms) |
117 | 440 piezo_config: ; Settings between 20 and 200 |
220
effd7259f5a5
make button sensitivity configurable (cR hardware)
heinrichsweikamp
parents:
218
diff
changeset
|
441 movff opt_cR_button_right,WREG; right button |
117 | 442 rcall piezo_config_tx |
220
effd7259f5a5
make button sensitivity configurable (cR hardware)
heinrichsweikamp
parents:
218
diff
changeset
|
443 movff opt_cR_button_left,WREG ; left button |
204 | 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 |
260 | 483 btfsc rechargeable ; Something to reset? |
484 call lt2942_charge_done ; Yes, reset accumulating registers to 0xFFFF | |
113 | 485 clrf EEADRH |
0 | 486 clrf EEDATA ; Delete to zero |
487 write_int_eeprom 0x07 | |
488 write_int_eeprom 0x08 | |
489 write_int_eeprom 0x09 | |
490 write_int_eeprom 0x0A | |
491 write_int_eeprom 0x0B | |
492 write_int_eeprom 0x0C | |
493 banksel battery_gauge+0 | |
494 clrf battery_gauge+0 | |
495 clrf battery_gauge+1 | |
496 clrf battery_gauge+2 | |
497 clrf battery_gauge+3 | |
498 clrf battery_gauge+4 | |
499 clrf battery_gauge+5 | |
500 banksel common | |
501 movlw .100 | |
502 movwf batt_percent | |
503 return | |
504 | |
505 | |
506 END |