Mercurial > public > hwos_code
annotate src/adc_lightsensor.asm @ 114:109df032cc54
minor fix: assert should pass for pure helium diluant
author | heinrichsweikamp |
---|---|
date | Mon, 23 Jun 2014 17:44:18 +0200 |
parents | f3062a611eef |
children | 6c577737c34e |
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 | |
113 | 30 btfss c3_hardware |
31 bra get_battery_voltage1 ; Normal ostc3 hardware | |
32 | |
33 call lt2942_get_accumulated_charge | |
34 call lt2942_get_voltage | |
35 return | |
36 | |
37 get_battery_voltage1: | |
0 | 38 bsf adc_running ; =1: The ADC is in use |
39 movlw b'00100000' ; 2.048V Vref+ -> 1LSB = 500µV | |
40 movwf ADCON1 | |
41 movlw b'00011001' ; power on ADC, select AN6 | |
42 rcall wait_adc | |
43 | |
44 movff ADRESH,batt_voltage+1 ; store value | |
45 movff ADRESL,batt_voltage+0 ; store value | |
46 bcf ADCON0,0 ; power off ADC | |
47 | |
48 ; Multiply with 2,006 to be excact here... | |
49 ; bcf STATUS,C | |
50 ; rlcf xA+0,F | |
51 ; | |
52 ; rlcf xA+1,F ; x2 | |
53 | |
54 ; movff xA+0,batt_voltage+0 ; store value | |
55 ; movff xA+1,batt_voltage+1 | |
56 | |
57 movlw LOW lithium_36v_low | |
58 movwf sub_a+0 | |
59 movlw HIGH lithium_36v_low | |
60 movwf sub_a+1 | |
61 movff batt_voltage+0,sub_b+0 | |
62 movff batt_voltage+1,sub_b+1 | |
63 call subU16 ; sub_c = sub_a - sub_b | |
64 ; Battery is 3,6V (>lithium_36v_low?) | |
65 btfss neg_flag | |
66 bra get_battery_voltage4 ; No, use 1,5V | |
67 | |
68 bsf battery_is_36v ; Yes, set flag (Cleared in power-on reset only!) | |
69 | |
70 ; Check if the battery is near-dead already | |
71 movlw LOW lithium_36v_empty | |
72 movwf sub_a+0 | |
73 movlw HIGH lithium_36v_empty | |
74 movwf sub_a+1 | |
75 call subU16 ; sub_c = sub_a - sub_b | |
76 ; Battery is not dead yet (>lithium_36v_empty?) | |
77 btfsc neg_flag | |
78 bra get_battery_voltage2 ; Yes, battery is still ok | |
79 | |
80 ; Battery is probably dead very soon | |
81 ; Set ">=24Ah used" into battery gauge registers | |
82 movlw .128 | |
83 movff WREG,battery_gauge+5 | |
84 | |
85 get_battery_voltage2: | |
86 ; Use 3,6V battery gauging mode | |
87 movff battery_gauge+5,xC+3 | |
88 movff battery_gauge+4,xC+2 | |
89 movff battery_gauge+3,xC+1 | |
90 movff battery_gauge+2,xC+0 | |
91 ; battery_gauge:6 is nAs | |
92 ; devide through 65536 | |
93 ; devide through 364 | |
94 ; Result is in percent of a 2,4Ah Battery | |
95 movlw LOW .364 | |
96 movwf xB+0 | |
97 movlw HIGH .364 | |
98 movwf xB+1 | |
99 call div32x16 ; xC:4 / xB:2 = xC+3:xC+2 with xC+1:xC+0 as remainder | |
100 movff xC+0,lo | |
101 ; Limit to 100 | |
102 movlw .100 | |
103 cpfslt lo | |
104 movwf lo | |
105 ; lo will be between 0 (Full) and 100 (empty) | |
106 movf lo,W | |
107 sublw .100 | |
108 movwf lo | |
109 get_battery_voltage3: | |
110 movlw .100 | |
111 cpfslt lo | |
112 movwf lo | |
113 ; 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
|
114 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
115 ; 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
|
116 ; 75% |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
117 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
|
118 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
|
119 movlw LOW lithium_36v_75 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
120 movwf sub_a+0 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
121 movlw HIGH lithium_36v_75 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
122 movwf sub_a+1 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
123 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
|
124 btfsc neg_flag |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
125 bra get_battery_voltage3a |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
126 movlw .75 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
127 movwf lo |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
128 get_battery_voltage3a: |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
129 ; 50% |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
130 movlw LOW lithium_36v_50 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
131 movwf sub_a+0 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
132 movlw HIGH lithium_36v_50 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
133 movwf sub_a+1 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
134 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
|
135 btfsc neg_flag |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
136 bra get_battery_voltage3b |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
137 movlw .50 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
138 movwf lo |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
139 get_battery_voltage3b: |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
140 ; 25% |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
141 movlw LOW lithium_36v_25 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
142 movwf sub_a+0 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
143 movlw HIGH lithium_36v_25 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
144 movwf sub_a+1 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
145 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
|
146 btfsc neg_flag |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
147 bra get_battery_voltage3c |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
148 movlw .25 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
149 movwf lo |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
150 get_battery_voltage3c: |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
151 ; 10% |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
152 movlw LOW lithium_36v_10 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
153 movwf sub_a+0 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
154 movlw HIGH lithium_36v_10 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
155 movwf sub_a+1 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
156 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
|
157 btfsc neg_flag |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
158 bra get_battery_voltage3d |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
159 movlw .10 |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
160 movwf lo |
0e1723f2761e
use four additional data points for the battery monitor
mh@mh-THINK.fritz.box
parents:
0
diff
changeset
|
161 get_battery_voltage3d: |
44
18fe4e668baa
BUGFIX: Percent display with 1,5V batteries
heinrichsweikamp
parents:
37
diff
changeset
|
162 movlw .100 |
18fe4e668baa
BUGFIX: Percent display with 1,5V batteries
heinrichsweikamp
parents:
37
diff
changeset
|
163 cpfslt lo |
18fe4e668baa
BUGFIX: Percent display with 1,5V batteries
heinrichsweikamp
parents:
37
diff
changeset
|
164 movwf lo |
18fe4e668baa
BUGFIX: Percent display with 1,5V batteries
heinrichsweikamp
parents:
37
diff
changeset
|
165 ; lo will be between 100 (Full) and 0 (empty) |
0 | 166 movf batt_percent,W |
167 cpfsgt lo ; keep batt_percent on the lowest value found | |
168 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
|
169 ; 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
|
170 ; movff lo,batt_percent ; store value |
0 | 171 bcf adc_running ; =1: The ADC is in use |
172 return | |
173 | |
174 get_battery_voltage4: | |
175 ; Use 1,5V battery voltage mode | |
176 ; Use approximation (batt_voltage:2-aa_15v_low)/4 = lo | |
177 movff batt_voltage+0,sub_a+0 | |
178 movff batt_voltage+1,sub_a+1 | |
179 movlw LOW aa_15v_low | |
180 movwf sub_b+0 | |
181 movlw HIGH aa_15v_low | |
182 movwf sub_b+1 | |
183 call subU16 ; sub_c = sub_a - sub_b | |
184 bcf STATUS,C | |
185 rrcf sub_c+1 | |
186 rrcf sub_c+0 ; /2 | |
187 bcf STATUS,C | |
188 rrcf sub_c+1 | |
189 rrcf sub_c+0 ; /4 | |
190 movff sub_c+0,lo | |
44
18fe4e668baa
BUGFIX: Percent display with 1,5V batteries
heinrichsweikamp
parents:
37
diff
changeset
|
191 bra get_battery_voltage3d ; Check limits and return |
0 | 192 |
193 global get_ambient_level | |
194 get_ambient_level: ; starts ADC and waits until finished | |
195 btfsc adc_running ; ADC in use? | |
196 return ; Yes, return | |
197 | |
113 | 198 btfss c3_hardware |
199 bra get_ambient_level1 ; Normal ostc3 hardware | |
200 movlw .250 | |
201 movwf ambient_light+0 | |
202 clrf ambient_light+1 ; Set to max | |
203 bra get_ambient_level2 ; Continue as normal | |
204 | |
205 get_ambient_level1: | |
0 | 206 movlw b'00000000' ; Vref+ = Vdd |
207 movwf ADCON1 | |
208 movlw b'00011101' ; power on ADC, select AN7 | |
209 rcall wait_adc | |
210 | |
211 movff ADRESH,ambient_light+1 | |
212 movff ADRESL,ambient_light+0 | |
213 bcf ADCON0,0 ; power off ADC | |
214 | |
215 ; ambient_light:2 is between 4096 (direct sunlight) and about 200 (darkness) | |
216 ; First: Devide through 16 | |
217 bcf STATUS,C | |
218 rrcf ambient_light+1 | |
219 rrcf ambient_light+0 | |
220 bcf STATUS,C | |
221 rrcf ambient_light+1 | |
222 rrcf ambient_light+0 | |
223 bcf STATUS,C | |
224 rrcf ambient_light+1 | |
225 rrcf ambient_light+0 | |
226 bcf STATUS,C | |
227 rrcf ambient_light+1 | |
228 rrcf ambient_light+0 | |
229 ; Result: ambient_light:2/16 | |
230 ; Now, make sure to have value between ambient_light_low and ambient_light_max | |
231 | |
232 movlw .254 | |
233 tstfsz ambient_light+1 ; >255? | |
234 movwf ambient_light+0 ; avoid ADC clipping | |
235 | |
236 incfsz ambient_light+0,W ; =255? | |
237 bra get_ambient_level2 ; No, continue | |
238 | |
239 movlw .254 | |
240 movwf ambient_light+0 ; avoid ADC clipping | |
241 | |
242 get_ambient_level2: | |
243 banksel isr_backup ; Back to Bank0 ISR data | |
244 movff opt_brightness,isr1_temp | |
245 | |
246 btfsc RCSTA1,7 ; UART module on? | |
247 clrf isr1_temp ; Yes, set temporally to eco mode | |
248 | |
249 incf isr1_temp,F ; adjust 0-2 to 1-3 | |
250 | |
251 banksel common ; flag is in bank1 | |
252 movlw ambient_light_max_high ; brightest setting | |
253 btfsc battery_is_36v ; 3,6V battery in use? | |
254 movlw ambient_light_max_high_36V ; Yes... | |
255 banksel isr_backup ; Back to Bank0 ISR data | |
256 | |
257 dcfsnz isr1_temp,F | |
258 movlw ambient_light_max_eco ; brightest setting | |
259 dcfsnz isr1_temp,F | |
260 movlw ambient_light_max_medium; brightest setting | |
261 | |
262 banksel common ; ambient_light is in Bank1 | |
263 incf ambient_light+0,F ; +1 | |
264 cpfslt ambient_light+0 ; smaller then WREG? | |
265 movwf ambient_light+0 ; No, set to max. | |
266 | |
267 banksel isr_backup ; Back to Bank0 ISR data | |
268 movff opt_brightness,isr1_temp | |
269 incf isr1_temp,F ; adjust 0-2 to 1-3 | |
270 movlw ambient_light_min_high ; darkest setting | |
271 | |
272 dcfsnz isr1_temp,F | |
273 movlw ambient_light_min_eco ; darkest setting | |
274 dcfsnz isr1_temp,F | |
275 movlw ambient_light_min_medium; darkest setting | |
276 dcfsnz isr1_temp,F | |
277 movlw ambient_light_min_high ; darkest setting | |
278 | |
279 banksel common ; ambient_light is in Bank1 | |
280 cpfsgt ambient_light+0 ; bigger then WREG? | |
281 movwf ambient_light+0 ; No, set to min | |
282 | |
283 movff ambient_light+0,max_CCPR1L ; Store value for dimming in TMR7 interrupt | |
284 return | |
285 | |
113 | 286 global get_analog_inputs |
287 get_analog_inputs: ; starts ADC and waits until finished | |
0 | 288 bsf adc_running ; =1: The ADC is in use |
113 | 289 btfsc TFT_PWM |
290 bra get_analog_inputs ; Wait for PWM low | |
291 movlw b'00100000' ; 2.048V Vref+ -> 1LSB = 500µV | |
292 movwf ADCON1 | |
293 movlw b'00100001' ; power on ADC, select AN8 | |
294 rcall wait_adc | |
295 bcf STATUS,C | |
296 rrcf ADRESH,F ; /2 | |
297 rrcf ADRESL,F | |
298 movff ADRESL,o2_mv_sensor1+0 ; in 0.1mV steps | |
299 movff ADRESH,o2_mv_sensor1+1 | |
300 bcf STATUS,C | |
301 rlcf ADRESL,F | |
302 rlcf ADRESH,F | |
0 | 303 movlw b'00100000' ; 2.048V Vref+ |
304 movwf ADCON1 | |
113 | 305 movlw b'00100101' ; power on ADC, select AN9 |
0 | 306 rcall wait_adc |
113 | 307 bcf STATUS,C |
308 rrcf ADRESH,F ; /2 | |
309 rrcf ADRESL,F | |
310 movff ADRESL,o2_mv_sensor2+0 ; in 0.1mV steps | |
311 movff ADRESH,o2_mv_sensor2+1 | |
312 movlw b'00100000' ; 2.048V Vref+ | |
313 movwf ADCON1 | |
314 movlw b'00101001' ; power on ADC, select AN10 | |
315 rcall wait_adc | |
316 bcf STATUS,C | |
317 rrcf ADRESH,F ; /2 | |
318 rrcf ADRESL,F | |
319 movff ADRESL,o2_mv_sensor3+0 ; in 0.1mV steps | |
320 movff ADRESH,o2_mv_sensor3+1 | |
0 | 321 bcf ADCON0,0 ; power off ADC |
322 bcf adc_running ; =1: The ADC is in use | |
323 return | |
324 | |
113 | 325 global piezo_config ; Sets up piezo sensitivity of heinrichs weikamp Piezo buttons (~30ms) |
326 piezo_config: | |
327 movlw .20 | |
328 rcall piezo_config_tx ; Send one byte | |
329 movlw .20 | |
330 rcall piezo_config_tx ; Send one byte | |
331 movlw .100 | |
332 rcall piezo_config_tx ; Send one byte | |
333 movlw .200 | |
334 rcall piezo_config_tx ; Send one byte | |
335 return | |
336 | |
337 piezo_config_tx: | |
338 movwf uart1_temp ; Store byte | |
339 movlw .8 | |
340 movwf uart2_temp ; Bit counter | |
341 bcf TX3_PIEZO_CFG ; Startbit | |
342 rcall piezo_config_wait_bit | |
343 piezo_config_tx_loop: | |
344 btfss uart1_temp,0 ; LSB first | |
345 bcf TX3_PIEZO_CFG | |
346 btfsc uart1_temp,0 ; LSB first | |
347 bsf TX3_PIEZO_CFG | |
348 rcall piezo_config_wait_bit | |
349 rrncf uart1_temp,F | |
350 decfsz uart2_temp,F | |
351 bra piezo_config_tx_loop | |
352 bsf TX3_PIEZO_CFG ; Stopbit | |
353 rcall piezo_config_wait_bit | |
354 return | |
355 | |
356 piezo_config_wait_bit: | |
357 setf TMR5H | |
358 movlw .255-.26 ;26 x 31,5µs = 819us | |
359 movwf TMR5L | |
360 bcf PIR5,TMR5IF ; Clear flag | |
361 piezo_config_wait_bit3: | |
362 btfss PIR5,TMR5IF | |
363 bra piezo_config_wait_bit3 ; Wait loop | |
364 return | |
365 | |
0 | 366 global reset_battery_pointer |
367 reset_battery_pointer: ; Resets battery pointer 0x07-0x0C and battery_gauge:5 | |
113 | 368 extern lt2942_charge_done |
369 call lt2942_charge_done ; Reset accumulating registers to 0xFFFF | |
370 clrf EEADRH | |
0 | 371 clrf EEDATA ; Delete to zero |
372 write_int_eeprom 0x07 | |
373 write_int_eeprom 0x08 | |
374 write_int_eeprom 0x09 | |
375 write_int_eeprom 0x0A | |
376 write_int_eeprom 0x0B | |
377 write_int_eeprom 0x0C | |
378 banksel battery_gauge+0 | |
379 clrf battery_gauge+0 | |
380 clrf battery_gauge+1 | |
381 clrf battery_gauge+2 | |
382 clrf battery_gauge+3 | |
383 clrf battery_gauge+4 | |
384 clrf battery_gauge+5 | |
385 banksel common | |
386 movlw .100 | |
387 movwf batt_percent | |
388 return | |
389 | |
390 | |
391 END |