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