comparison src/adc_lightsensor.asm @ 604:ca4556fb60b9

bump to 2.99beta, work on 3.00 stable
author heinrichsweikamp
date Thu, 22 Nov 2018 19:47:26 +0100
parents 00b24fb4324d
children d866684249bd
comparison
equal deleted inserted replaced
603:00b24fb4324d 604:ca4556fb60b9
1 ;============================================================================= 1 ;=============================================================================
2 ; 2 ;
3 ; File adc.asm V2.98 3 ; File adc.asm V2.99e
4 ; 4 ;
5 ; 5 ;
6 ; Copyright (c) 2011, JD Gascuel, HeinrichsWeikamp, all right reserved. 6 ; Copyright (c) 2011, JD Gascuel, HeinrichsWeikamp, all right reserved.
7 ;============================================================================= 7 ;=============================================================================
8 ; HISTORY 8 ; HISTORY
14 #include "eeprom_rs232.inc" 14 #include "eeprom_rs232.inc"
15 #include "i2c.inc" 15 #include "i2c.inc"
16 16
17 extern reset_battery_internal_only 17 extern reset_battery_internal_only
18 18
19 sensors CODE 19 adc_light CODE
20 20
21 ;============================================================================= 21 ;=============================================================================
22 22
23 wait_adc: 23 wait_adc:
24 movwf ADCON0 24 movwf ADCON0
25 nop 25 nop
26 bsf ADCON0,1 ; start ADC 26 bsf ADCON0,1 ; start ADC
27 wait_adc2: 27 wait_adc2:
28 btfsc ADCON0,1 ; Wait... 28 btfsc ADCON0,1 ; wait...
29 bra wait_adc2 29 bra wait_adc2
30 return 30 return
31 31
32 global get_battery_voltage 32 global get_battery_voltage
33 get_battery_voltage: ; starts ADC and waits until finished 33 get_battery_voltage: ; starts ADC and waits until finished
34 btfss battery_gauge_available 34 btfss battery_gauge_available
35 bra get_battery_voltage1 ; Normal ostc3 hardware 35 bra get_battery_voltage1 ; normal ostc3 hardware
36 36
37 call lt2942_get_accumulated_charge 37 call lt2942_get_accumulated_charge
38 call lt2942_get_voltage 38 call lt2942_get_voltage
39 39
40 tstfsz batt_voltage+1 ; <256mV? 40 tstfsz batt_voltage+1 ; < 256 mV ?
41 bra get_battery_voltage_noretry ; No 41 bra get_battery_voltage_noretry ; NO
42 42
43 ; Retry 43 ; Retry
44 call lt2942_get_accumulated_charge 44 call lt2942_get_accumulated_charge
45 call lt2942_get_voltage 45 call lt2942_get_voltage
46 46
47 get_battery_voltage_noretry: 47 get_battery_voltage_noretry:
48 btfsc divemode 48 btfsc divemode
49 return ; Not in divemode 49 return ; not in divemode
50 50
51 bcf cv_active 51 bcf cv_active
52 bcf cc_active 52 bcf cc_active
53 bcf LEDr 53 bcf LEDr
54 bcf TRISJ,2 ; Chrg-Out output 54 bcf TRISJ,2 ; Chrg-Out output
66 return 66 return
67 charge_cv_active: 67 charge_cv_active:
68 decfsz get_bat_volt_counter,F 68 decfsz get_bat_volt_counter,F
69 return 69 return
70 movlw .15 70 movlw .15
71 cpfsgt batt_voltage+1 ; Batt Voltage >= 16*256mV (4,096V)? 71 cpfsgt batt_voltage+1 ; battery voltage >= 16*256mV (4,096V)?
72 bra charge_cc_active ; No 72 bra charge_cc_active ; NO
73 bsf cc_active 73 bsf cc_active
74 bsf cv_active 74 bsf cv_active
75 bsf LEDr ; Indicate charging 75 bsf LEDr ; indicate charging
76 call lt2942_charge_done ; Reset accumulating registers to 0xFFFF 76 call lt2942_charge_done ; reset accumulating registers to 0xFFFF
77 WAITMS d'10' 77 WAITMS d'10'
78 bcf LEDr ; Indicate charging 78 bcf LEDr ; indicate charging
79 bsf get_bat_volt_counter,0 ; =1 79 bsf get_bat_volt_counter,0 ; =1
80 return 80 return
81 81
82 charge_cc_active: 82 charge_cc_active:
83 bsf cc_active 83 bsf cc_active
84 bsf LEDr ; Indicate charging 84 bsf LEDr ; indicate charging
85 bcf CHRG_OUT 85 bcf CHRG_OUT
86 bsf TRISJ,2 ; Chrg-Out high impedance 86 bsf TRISJ,2 ; chrg-Out high impedance
87 movlw .15 87 movlw .15
88 cpfsgt batt_voltage+1 ; Batt Voltage >= 16*256mV (4,096V)? 88 cpfsgt batt_voltage+1 ; battery voltage >= 16*256mV (4.096 V)?
89 bra charge_cc_active2 ; No 89 bra charge_cc_active2 ; NO
90 movlw .81 90 movlw .81
91 cpfslt batt_voltage+0 ; Batt Voltage >= 80mV(+4096mV from batt_voltage+1)? 91 cpfslt batt_voltage+0 ; battery voltage >= 80mV (+4096mV from batt_voltage+1)?
92 bra charge_cv_active ; Yes 92 bra charge_cv_active ; YES
93 charge_cc_active2: 93 charge_cc_active2:
94 movlw .10 94 movlw .10
95 movwf get_bat_volt_counter 95 movwf get_bat_volt_counter
96 return 96 return
97 97
104 104
105 movff ADRESH,batt_voltage+1 ; store value 105 movff ADRESH,batt_voltage+1 ; store value
106 movff ADRESL,batt_voltage+0 ; store value 106 movff ADRESL,batt_voltage+0 ; store value
107 bcf ADCON0,0 ; power off ADC 107 bcf ADCON0,0 ; power off ADC
108 108
109 ; Multiply with 2,006 to be exact here... 109 ; Multiply with 2.006 to be exact here...
110 ; bcf STATUS,C 110 ; bcf STATUS,C
111 ; rlcf xA+0,F 111 ; rlcf xA+0,F
112 ; 112 ;
113 ; rlcf xA+1,F ; x2 113 ; rlcf xA+1,F ; x2
114 114
120 movlw HIGH lithium_36v_low 120 movlw HIGH lithium_36v_low
121 movwf sub_a+1 121 movwf sub_a+1
122 movff batt_voltage+0,sub_b+0 122 movff batt_voltage+0,sub_b+0
123 movff batt_voltage+1,sub_b+1 123 movff batt_voltage+1,sub_b+1
124 call subU16 ; sub_c = sub_a - sub_b 124 call subU16 ; sub_c = sub_a - sub_b
125 ; Battery is 3,6V (>lithium_36v_low?) 125 ; Battery is 3.6V (> lithium_36v_low?)
126 btfss neg_flag 126 btfss neg_flag
127 bra get_battery_voltage4 ; No, use 1,5V 127 bra get_battery_voltage4 ; NO - use 1.5V
128 128 bsf battery_is_36v ; YES - set flag (Cleared in power-on reset only!)
129 bsf battery_is_36v ; Yes, set flag (Cleared in power-on reset only!)
130 129
131 ; Check if the battery is near-dead already 130 ; Check if the battery is near-dead already
132 movlw LOW lithium_36v_empty 131 movlw LOW lithium_36v_empty
133 movwf sub_a+0 132 movwf sub_a+0
134 movlw HIGH lithium_36v_empty 133 movlw HIGH lithium_36v_empty
135 movwf sub_a+1 134 movwf sub_a+1
136 call subU16 ; sub_c = sub_a - sub_b 135 call subU16 ; sub_c = sub_a - sub_b
137 ; Battery is not dead yet (>lithium_36v_empty?) 136 ; Battery is not dead yet (> lithium_36v_empty)?
138 btfsc neg_flag 137 btfsc neg_flag
139 bra get_battery_voltage2 ; Yes, battery is still ok 138 bra get_battery_voltage2 ; YES - battery is still ok
140 139
141 ; Battery is probably dead very soon 140 ; Battery is probably dead very soon
142 ; Set ">=24Ah used" into battery gauge registers 141 ; Set ">=24Ah used" into battery gauge registers
143 movlw .128 142 movlw .128
144 movff WREG,battery_gauge+5 143 movff WREG,battery_gauge+5
145 144
146 get_battery_voltage2: 145 get_battery_voltage2:
147 ; Use 3,6V battery gauging mode 146 ; Use 3.6V battery gauging mode
148 movff battery_gauge+5,xC+3 147 movff battery_gauge+5,xC+3
149 movff battery_gauge+4,xC+2 148 movff battery_gauge+4,xC+2
150 movff battery_gauge+3,xC+1 149 movff battery_gauge+3,xC+1
151 movff battery_gauge+2,xC+0 150 movff battery_gauge+2,xC+0
152 ; battery_gauge:6 is nAs 151 ; battery_gauge: 6 is nAs
153 ; devide through 65536 152 ; divide through 65536
154 ; devide through battery_capacity:2 153 ; divide through battery_capacity:2
155 ; Result is in percent 154 ; result is in percent
156 movff internal_battery_capacity+0,xB+0 155 movff internal_battery_capacity+0,xB+0
157 movff internal_battery_capacity+1,xB+1 156 movff internal_battery_capacity+1,xB+1
158 call div32x16 ; xC:4 / xB:2 = xC+3:xC+2 with xC+1:xC+0 as remainder 157 call div32x16 ; xC:4 = xC:4 / xB:2 with xA as remainder
159 movff xC+0,lo 158 movff xC+0,lo
160 ; Limit to 100 159 ; limit to 100
161 movlw .100 160 movlw .100
162 cpfslt lo 161 cpfslt lo
163 movwf lo 162 movwf lo
164 ; lo will be between 0 (Full) and 100 (empty) 163 ; lo will be between 0 (full) and 100 (empty)
165 movf lo,W 164 movf lo,W
166 sublw .100 165 sublw .100
167 movwf lo 166 movwf lo
168 get_battery_voltage3: 167 get_battery_voltage3:
169 movlw .100 168 movlw .100
170 cpfslt lo 169 cpfslt lo
171 movwf lo 170 movwf lo
172 ; lo will be between 100 (Full) and 0 (empty) 171 ; lo will be between 100 (full) and 0 (empty)
173 172
174 ; use 3,6V battery sensing based on 50mA load 173 ; use 3.6V battery sensing based on 50 mA load
175 ; 75% 174 ; 75%
176 movff batt_voltage+0,sub_b+0 175 movff batt_voltage+0,sub_b+0
177 movff batt_voltage+1,sub_b+1 176 movff batt_voltage+1,sub_b+1
178 movlw LOW lithium_36v_75 177 movlw LOW lithium_36v_75
179 movwf sub_a+0 178 movwf sub_a+0
219 movwf lo 218 movwf lo
220 get_battery_voltage3d: 219 get_battery_voltage3d:
221 movlw .100 220 movlw .100
222 cpfslt lo 221 cpfslt lo
223 movwf lo 222 movwf lo
224 ; lo will be between 100 (Full) and 0 (empty) 223 ; lo will be between 100 (full) and 0 (empty)
225 movf batt_percent,W 224 movf batt_percent,W
226 cpfsgt lo ; keep batt_percent on the lowest value found 225 cpfsgt lo ; keep batt_percent on the lowest value found
227 movff lo,batt_percent ; store value 226 movff lo,batt_percent ; store value
228 btfsc battery_is_36v ; but always use computed value for 3,6V battery 227 btfsc battery_is_36v ; but always use computed value for 3.6V battery
229 movff lo,batt_percent ; store value 228 movff lo,batt_percent ; store value
230 bcf adc_running ; =1: The ADC is in use 229 bcf adc_running ; =1: the ADC is in use
231 return 230 return
232 231
233 get_battery_voltage4: 232 get_battery_voltage4:
234 ; Use 1,5V battery voltage mode 233 ; use 1.5V battery voltage mode
235 ; Use approximation (batt_voltage:2-aa_15v_low)/4 = lo 234 ; use approximation (batt_voltage:2-aa_15v_low)/4 = lo
236 movff batt_voltage+0,sub_a+0 235 movff batt_voltage+0,sub_a+0
237 movff batt_voltage+1,sub_a+1 236 movff batt_voltage+1,sub_a+1
238 movlw LOW aa_15v_low 237 movlw LOW aa_15v_low
239 movwf sub_b+0 238 movwf sub_b+0
240 movlw HIGH aa_15v_low 239 movlw HIGH aa_15v_low
245 rrcf sub_c+0 ; /2 244 rrcf sub_c+0 ; /2
246 bcf STATUS,C 245 bcf STATUS,C
247 rrcf sub_c+1 246 rrcf sub_c+1
248 rrcf sub_c+0 ; /4 247 rrcf sub_c+0 ; /4
249 movff sub_c+0,lo 248 movff sub_c+0,lo
250 bra get_battery_voltage3d ; Check limits and return 249 bra get_battery_voltage3d ; check limits and return
251 250
252 global get_ambient_level 251 global get_ambient_level
253 get_ambient_level: ; starts ADC and waits until finished 252 get_ambient_level: ; starts ADC and waits until finished
254 btfsc adc_running ; ADC in use? 253 btfsc adc_running ; ADC in use?
255 return ; Yes, return 254 return ; YES - return
256 255
257 btfsc ambient_sensor 256 btfsc ambient_sensor
258 bra get_ambient_level1 ; Normal ostc3 hardware 257 bra get_ambient_level1 ; normal OSTC3 hardware
259 258
260 banksel isr_backup ; Back to Bank0 ISR data 259 banksel isr_backup ; back to bank0 ISR data
261 movff opt_brightness,isr1_temp 260 movff opt_brightness,isr1_temp
262 incf isr1_temp,F ; adjust 0-2 to 1-3 261 incf isr1_temp,F ; adjust 0-2 to 1-3
263 movlw ambient_light_max_high_cr ; cR and 2 hardware brightest setting 262 movlw ambient_light_max_high_cr ; cR and 2 hardware brightest setting
264 dcfsnz isr1_temp,F 263 dcfsnz isr1_temp,F
265 movlw ambient_light_max_eco ; brightest setting 264 movlw ambient_light_max_eco ; brightest setting
266 dcfsnz isr1_temp,F 265 dcfsnz isr1_temp,F
267 movlw ambient_light_max_medium ; brightest setting 266 movlw ambient_light_max_medium ; brightest setting
268 267
269 movff WREG,ambient_light+0 ; Set to max. 268 movff WREG,ambient_light+0 ; set to max.
270 movff ambient_light+0,max_CCPR1L ; Store value for dimming in TMR7 interrupt 269 movff ambient_light+0,max_CCPR1L ; store value for dimming in TMR7 interrupt
271 return 270 return
272 271
273 get_ambient_level1: 272 get_ambient_level1:
274 movlw b'00000000' ; Vref+ = Vdd 273 movlw b'00000000' ; Vref+ = Vdd
275 movwf ADCON1 274 movwf ADCON1
279 movff ADRESH,ambient_light+1 278 movff ADRESH,ambient_light+1
280 movff ADRESL,ambient_light+0 279 movff ADRESL,ambient_light+0
281 bcf ADCON0,0 ; power off ADC 280 bcf ADCON0,0 ; power off ADC
282 281
283 ; ambient_light:2 is between 4096 (direct sunlight) and about 200 (darkness) 282 ; ambient_light:2 is between 4096 (direct sunlight) and about 200 (darkness)
284 ; First: Divide by 16 283 ; first: divide by 16
285 banksel ambient_light 284 banksel ambient_light
286 bcf STATUS,C 285 bcf STATUS,C
287 rrcf ambient_light+1 286 rrcf ambient_light+1
288 rrcf ambient_light+0 287 rrcf ambient_light+0
289 bcf STATUS,C 288 bcf STATUS,C
293 rrcf ambient_light+1 292 rrcf ambient_light+1
294 rrcf ambient_light+0 293 rrcf ambient_light+0
295 bcf STATUS,C 294 bcf STATUS,C
296 rrcf ambient_light+1 295 rrcf ambient_light+1
297 rrcf ambient_light+0 296 rrcf ambient_light+0
298 ; Result: ambient_light:2/16 297 ; result: ambient_light:2/16
299 ; Now, make sure to have value between ambient_light_low and ambient_light_max 298 ; now make sure to have value between ambient_light_low and ambient_light_max
300 299
301 movlw .254 300 movlw .254
302 tstfsz ambient_light+1 ; >255? 301 tstfsz ambient_light+1 ; > 255 ?
303 movwf ambient_light+0 ; avoid ADC clipping 302 movwf ambient_light+0 ; YES - avoid ADC clipping
304 303
305 incfsz ambient_light+0,W ; =255? 304 incfsz ambient_light+0,W ; = 255 ?
306 bra get_ambient_level2 ; No, continue 305 bra get_ambient_level2 ; NO - continue
307 306
308 movlw .254 307 movlw .254
309 movwf ambient_light+0 ; avoid ADC clipping 308 movwf ambient_light+0 ; avoid ADC clipping
310 309
311 get_ambient_level2: 310 get_ambient_level2:
312 ; movlw .10 311 ; movlw .10
313 ; subwf ambient_light+0,F ; Subtract 10 (ADC Offset) 312 ; subwf ambient_light+0,F ; subtract 10 (ADC Offset)
314 ; btfsc STATUS,N 313 ; btfsc STATUS,N
315 ; movwf ambient_light+0 ; avoid clipping 314 ; movwf ambient_light+0 ; avoid clipping
316 315
317 banksel isr_backup ; Back to Bank0 ISR data 316 banksel isr_backup ; back to bank0 ISR data
318 movff opt_brightness,isr1_temp 317 movff opt_brightness,isr1_temp
319 318
320 btfsc RCSTA1,7 ; UART module on? 319 btfsc RCSTA1,7 ; UART module on?
321 clrf isr1_temp ; Yes, set temporally to eco mode 320 clrf isr1_temp ; YES - set temporally to eco mode
322 321
323 incf isr1_temp,F ; adjust 0-2 to 1-3 322 incf isr1_temp,F ; adjust 0-2 to 1-3
324 323
325 banksel common ; flag is in bank1 324 banksel common ; flag is in bank1
326 movlw ambient_light_max_high_cr ; cR and 2 hardware brightest setting 325 movlw ambient_light_max_high_cr ; cR and 2 hardware brightest setting
327 btfss battery_gauge_available 326 btfss battery_gauge_available
328 movlw ambient_light_max_high_15V ; 1,5V battery brightest setting 327 movlw ambient_light_max_high_15V ; 1.5V battery brightest setting
329 btfsc battery_is_36v ; 3,6V battery in use? 328 btfsc battery_is_36v ; 3.6V battery in use?
330 movlw ambient_light_max_high_36V ; 3,6V battery brightest setting 329 movlw ambient_light_max_high_36V ; YES - 3.6V battery brightest setting
331 banksel isr_backup ; Back to Bank0 ISR data 330 banksel isr_backup ; back to bank0 ISR data
332 331
333 dcfsnz isr1_temp,F 332 dcfsnz isr1_temp,F
334 movlw ambient_light_max_eco ; brightest setting 333 movlw ambient_light_max_eco ; brightest setting
335 dcfsnz isr1_temp,F 334 dcfsnz isr1_temp,F
336 movlw ambient_light_max_medium ; brightest setting 335 movlw ambient_light_max_medium ; brightest setting
337 336
338 banksel ambient_light 337 banksel ambient_light
339 incf ambient_light+0,F ; +1 338 incf ambient_light+0,F ; +1
340 cpfslt ambient_light+0 ; smaller then WREG? 339 cpfslt ambient_light+0 ; smaller than WREG?
341 movwf ambient_light+0 ; No, set to max. 340 movwf ambient_light+0 ; NO - set to max.
342 341
343 banksel isr_backup ; Back to Bank0 ISR data 342 banksel isr_backup ; back to bank0 ISR data
344 movff opt_brightness,isr1_temp 343 movff opt_brightness,isr1_temp
345 incf isr1_temp,F ; adjust 0-2 to 1-3 344 incf isr1_temp,F ; adjust 0-2 to 1-3
346 movlw ambient_light_min_high ; darkest setting 345 movlw ambient_light_min_high ; darkest setting
347 346
348 dcfsnz isr1_temp,F 347 dcfsnz isr1_temp,F
351 movlw ambient_light_min_medium ; darkest setting 350 movlw ambient_light_min_medium ; darkest setting
352 dcfsnz isr1_temp,F 351 dcfsnz isr1_temp,F
353 movlw ambient_light_min_high ; darkest setting 352 movlw ambient_light_min_high ; darkest setting
354 353
355 banksel ambient_light 354 banksel ambient_light
356 cpfsgt ambient_light+0 ; bigger then WREG? 355 cpfsgt ambient_light+0 ; bigger than WREG?
357 movwf ambient_light+0 ; No, set to min 356 movwf ambient_light+0 ; NO - set to min
358 banksel common 357 banksel common
359 358
360 movff ambient_light+0,max_CCPR1L ; Store value for dimming in TMR7 interrupt 359 movff ambient_light+0,max_CCPR1L ; store value for dimming in TMR7 interrupt
361 return 360 return
362 361
363 global get_analog_inputs 362 global get_analog_inputs
364 get_analog_inputs: ; starts ADC and waits until finished 363 get_analog_inputs: ; start ADC and wait until finished
365 bsf adc_running ; =1: The ADC is in use 364 bsf adc_running ; =1: The ADC is in use
366 btfsc TFT_PWM 365 btfsc TFT_PWM
367 bra get_analog_inputs ; Wait for PWM low 366 bra get_analog_inputs ; wait for PWM low
368 movlw b'00100000' ; 2.048V Vref+ -> 1LSB = 500µV 367 movlw b'00100000' ; 2.048V Vref+ -> 1 LSB = 500 µV
369 movwf ADCON1 368 movwf ADCON1
370 movlw b'00100001' ; power on ADC, select AN8 369 movlw b'00100001' ; power on ADC, select AN8
371 rcall wait_adc 370 rcall wait_adc
372 bcf STATUS,C 371 bcf STATUS,C
373 rrcf ADRESH,F ; /2 372 rrcf ADRESH,F ; /2
374 rrcf ADRESL,W 373 rrcf ADRESL,W
375 ; add to o2_mv_sensor1:2 374 ; add to o2_mv_sensor1:2
376 addwf o2_mv_sensor1+0,F 375 addwf o2_mv_sensor1+0,F
377 movf ADRESH,W 376 movf ADRESH,W
378 addwfc o2_mv_sensor1+1,F 377 addwfc o2_mv_sensor1+1,F
379 ; Devide by 2 378 ; divide by 2
380 bcf STATUS,C 379 bcf STATUS,C
381 rrcf o2_mv_sensor1+1,F ; /2 380 rrcf o2_mv_sensor1+1,F ; /2
382 rrcf o2_mv_sensor1+0,F 381 rrcf o2_mv_sensor1+0,F
383 382
384 movlw HIGH ignore_mv 383 movlw HIGH ignore_mv
385 cpfsgt o2_mv_sensor1+1 ; >ignore_mv? 384 cpfsgt o2_mv_sensor1+1 ; > ignore_mv ?
386 bra get_analog_inputs2a ; No 385 bra get_analog_inputs2a ; NO
387 ; Yes, ignore this reading 386 ; YES - ignore this reading
388 clrf o2_mv_sensor1+1 387 clrf o2_mv_sensor1+1
389 clrf o2_mv_sensor1+0 388 clrf o2_mv_sensor1+0
390 get_analog_inputs2a: 389 get_analog_inputs2a:
391 ; Ignore 1,9mV noise for not-connected inputs 390 ; ignore 1.9 mV noise for not-connected inputs
392 tstfsz o2_mv_sensor1+1 ; >25,5mV? 391 tstfsz o2_mv_sensor1+1 ; > 25.5mV ?
393 bra get_analog_inputs2 ; Yes, skip here 392 bra get_analog_inputs2 ; YES - skip here
394 movlw .19 393 movlw .19
395 cpfsgt o2_mv_sensor1+0 ; >1,9mV? 394 cpfsgt o2_mv_sensor1+0 ; > 1.9mV ?
396 clrf o2_mv_sensor1+0 ; no, clear result 395 clrf o2_mv_sensor1+0 ; NO - clear result
397 get_analog_inputs2: 396 get_analog_inputs2:
398 movlw b'00100101' ; power on ADC, select AN9 397 movlw b'00100101' ; power on ADC, select AN9
399 rcall wait_adc 398 rcall wait_adc
400 bcf STATUS,C 399 bcf STATUS,C
401 rrcf ADRESH,F ; /2 400 rrcf ADRESH,F ; /2
402 rrcf ADRESL,W 401 rrcf ADRESL,W
403 ; add to o2_mv_sensor2:2 402 ; add to o2_mv_sensor2:2
404 addwf o2_mv_sensor2+0,F 403 addwf o2_mv_sensor2+0,F
405 movf ADRESH,W 404 movf ADRESH,W
406 addwfc o2_mv_sensor2+1,F 405 addwfc o2_mv_sensor2+1,F
407 ; Devide by 2 406 ; divide by 2
408 bcf STATUS,C 407 bcf STATUS,C
409 rrcf o2_mv_sensor2+1,F ; /2 408 rrcf o2_mv_sensor2+1,F ; /2
410 rrcf o2_mv_sensor2+0,F 409 rrcf o2_mv_sensor2+0,F
411 410
412 movlw HIGH ignore_mv 411 movlw HIGH ignore_mv
413 cpfsgt o2_mv_sensor2+1 ; >ignore_mv? 412 cpfsgt o2_mv_sensor2+1 ; > ignore_mv ?
414 bra get_analog_inputs3a ; No 413 bra get_analog_inputs3a ; NO
415 ; Yes, ignore this reading 414 ; YES - ignore this reading
416 clrf o2_mv_sensor2+1 415 clrf o2_mv_sensor2+1
417 clrf o2_mv_sensor2+0 416 clrf o2_mv_sensor2+0
418 get_analog_inputs3a: 417 get_analog_inputs3a:
419 ; Ignore 1,9mV noise for not-connected inputs 418 ; ignore 1.9 mV noise for not-connected inputs
420 tstfsz o2_mv_sensor2+1 ; >25,5mV? 419 tstfsz o2_mv_sensor2+1 ; > 25.5 mV ?
421 bra get_analog_inputs3 ; Yes, skip here 420 bra get_analog_inputs3 ; YES - skip here
422 movlw .19 421 movlw .19
423 cpfsgt o2_mv_sensor2+0 ; >1,9mV? 422 cpfsgt o2_mv_sensor2+0 ; > 1.9 mV ?
424 clrf o2_mv_sensor2+0 ; no, clear result 423 clrf o2_mv_sensor2+0 ; NO - clear result
425 get_analog_inputs3: 424 get_analog_inputs3:
426 movlw b'00101001' ; power on ADC, select AN10 425 movlw b'00101001' ; power on ADC, select AN10
427 rcall wait_adc 426 rcall wait_adc
428 bcf STATUS,C 427 bcf STATUS,C
429 rrcf ADRESH,F ; /2 428 rrcf ADRESH,F ; /2
430 rrcf ADRESL,W 429 rrcf ADRESL,W
431 ; add to o2_mv_sensor3:2 430 ; add to o2_mv_sensor3:2
432 addwf o2_mv_sensor3+0,F 431 addwf o2_mv_sensor3+0,F
433 movf ADRESH,W 432 movf ADRESH,W
434 addwfc o2_mv_sensor3+1,F 433 addwfc o2_mv_sensor3+1,F
435 ; Devide by 2 434 ; divide by 2
436 bcf STATUS,C 435 bcf STATUS,C
437 rrcf o2_mv_sensor3+1,F ; /2 436 rrcf o2_mv_sensor3+1,F ; /2
438 rrcf o2_mv_sensor3+0,F 437 rrcf o2_mv_sensor3+0,F
439 438
440 movlw HIGH ignore_mv 439 movlw HIGH ignore_mv
441 cpfsgt o2_mv_sensor3+1 ; >ignore_mv? 440 cpfsgt o2_mv_sensor3+1 ; > ignore_mv ?
442 bra get_analog_inputs4a ; No 441 bra get_analog_inputs4a ; NO
443 ; Yes, ignore this reading 442 ; YES - ignore this reading
444 clrf o2_mv_sensor3+1 443 clrf o2_mv_sensor3+1
445 clrf o2_mv_sensor3+0 444 clrf o2_mv_sensor3+0
446 get_analog_inputs4a: 445 get_analog_inputs4a:
447 ; Ignore 1,9mV noise for not-connected inputs 446 ; ignore 1.9mV noise for not-connected inputs
448 tstfsz o2_mv_sensor3+1 ; >25,5mV? 447 tstfsz o2_mv_sensor3+1 ; > 25.5 mV ?
449 bra get_analog_inputs4 ; Yes, skip here 448 bra get_analog_inputs4 ; YES - skip here
450 movlw .19 449 movlw .19
451 cpfsgt o2_mv_sensor3+0 ; >1,9mV? 450 cpfsgt o2_mv_sensor3+0 ; > 1.9 mV ?
452 clrf o2_mv_sensor3+0 ; no, clear result 451 clrf o2_mv_sensor3+0 ; NO - clear result
453 get_analog_inputs4: 452 get_analog_inputs4:
454 bcf ADCON0,0 ; power off ADC 453 bcf ADCON0,0 ; power off ADC
455 bcf adc_running ; =1: The ADC is in use 454 bcf adc_running ; =1: the ADC is in use
456 return 455 return
457 456
458 global piezo_config ; sets up piezo sensitivity of heinrichs weikamp piezo buttons (~30ms) 457 global piezo_config ; set up piezo sensitivity of heinrichs weikamp piezo buttons (~30ms)
459 piezo_config: ; Settings between 20 and 200 458 piezo_config: ; settings between 20 and 200
460 clrf TMR5H 459 clrf TMR5H
461 clrf TMR5L ; ~2sec 460 clrf TMR5L ; ~2sec
462 bcf PIR5,TMR5IF ; Clear flag 461 bcf PIR5,TMR5IF ; clear flag
463 bcf switch_right 462 bcf switch_right
464 bcf switch_left 463 bcf switch_left
465 piezo_config0: 464 piezo_config0:
466 btfsc switch_right 465 btfsc switch_right
467 bra piezo_config 466 bra piezo_config
468 btfsc switch_left 467 btfsc switch_left
469 bra piezo_config ; Restart on button press 468 bra piezo_config ; restart on button press
470 469
471 btfss PIR5,TMR5IF 470 btfss PIR5,TMR5IF
472 bra piezo_config0 ; Wait loop 471 bra piezo_config0 ; wait loop
473 472
474 bcf INTCON,GIE 473 bcf INTCON,GIE
474
475 movff opt_cR_button_right,WREG ; right button 475 movff opt_cR_button_right,WREG ; right button
476 btfsc flip_screen ; 180° rotation ? 476 btfsc flip_screen ; 180° rotation ?
477 movff opt_cR_button_left,WREG ; Yes, left button 477 movff opt_cR_button_left,WREG ; YES - left button
478 rcall piezo_config_tx 478 rcall piezo_config_tx
479 479
480 movff opt_cR_button_left,WREG ; left button 480 movff opt_cR_button_left,WREG ; left button
481 btfsc flip_screen ; 180° rotation ? 481 btfsc flip_screen ; 180° rotation ?
482 movff opt_cR_button_right,WREG ; Yes, right button 482 movff opt_cR_button_right,WREG ; YES - right button
483 rcall piezo_config_tx 483 rcall piezo_config_tx
484 484
485 movlw .20 ; reserved 485 movlw .20 ; reserved
486 rcall piezo_config_tx 486 rcall piezo_config_tx
487 movlw .20 ; reserved 487 movlw .20 ; reserved
488 rcall piezo_config_tx 488 rcall piezo_config_tx
489
489 bsf INTCON,GIE 490 bsf INTCON,GIE
490 return 491 return
491 492
492 piezo_config_tx: ; send one byte 493 piezo_config_tx: ; send one byte
493 movwf lo ; store byte 494 movwf lo ; store byte
510 511
511 piezo_config_wait_bit: 512 piezo_config_wait_bit:
512 setf TMR5H 513 setf TMR5H
513 movlw .255-.26 ; 26 x 31,5µs = 819us 514 movlw .255-.26 ; 26 x 31,5µs = 819us
514 movwf TMR5L 515 movwf TMR5L
515 bcf PIR5,TMR5IF ; Clear flag 516 bcf PIR5,TMR5IF ; clear flag
516 piezo_config_wait_bit3: 517 piezo_config_wait_bit3:
517 btfss PIR5,TMR5IF 518 btfss PIR5,TMR5IF
518 bra piezo_config_wait_bit3 ; Wait loop 519 bra piezo_config_wait_bit3 ; wait loop
519 return 520 return
520 521
521 global reset_battery_pointer 522 global reset_battery_pointer
522 reset_battery_pointer: ; Resets battery pointer 0x07-0x0C and battery_gauge:5 523 reset_battery_pointer: ; reset battery pointer 0x07-0x0C and battery_gauge:5
523 extern lt2942_charge_done 524 extern lt2942_charge_done
524 btfsc battery_gauge_available ; Something to reset? 525 btfsc battery_gauge_available ; something to reset?
525 call lt2942_charge_done ; Yes, reset accumulating registers to 0xFFFF 526 call lt2942_charge_done ; YES - reset accumulating registers to 0xFFFF
526 goto reset_battery_internal_only ; and return 527 goto reset_battery_internal_only ; and return
527 528
528 529
529 global get_analog_switches 530 global get_analog_switches
530 get_analog_switches: ; starts ADC and waits until finished 531 get_analog_switches: ; start ADC and wait until finished
531 btfsc analog_switches 532 btfsc analog_switches ; does the OSTC have analog switches?
532 bra get_analog_switches2 533 bra get_analog_switches2 ; YES
533 ; no analog switches 534 ; NO
534 bcf analog_sw2_pressed 535 bcf analog_sw1_pressed ; NO - clear flag for analog switch 1
535 bcf analog_sw1_pressed 536 bcf analog_sw2_pressed ; - clear flag for analog switch 2
536 return ; Done. 537 return ; - done
537 get_analog_switches2: 538 get_analog_switches2:
538 btfsc adc_running ; ADC in use? 539 btfsc adc_running ; ADC in use?
539 return ; Yes, return 540 return ; YES - abort
540 541 ; NO
541 movlw b'00001001' ; left justified 542 movlw b'00001001' ; left justified
542 movwf ADCON2 543 movwf ADCON2
543 ; movlw b'00000000' ; Vref+ = Vdd 544 ; movlw b'00000000' ; Vref+ = Vdd
544 clrf ADCON1 545 clrf ADCON1
545 movlw b'00100101' ; power on ADC, select AN9 546 movlw b'00100101' ; power on ADC, select AN9
548 movff ADRESH,WREG 549 movff ADRESH,WREG
549 addwf analog_sw2_raw+0 550 addwf analog_sw2_raw+0
550 movlw .0 551 movlw .0
551 addwfc analog_sw2_raw+1 552 addwfc analog_sw2_raw+1
552 decfsz analog_counter,F ; continue averaging? 553 decfsz analog_counter,F ; continue averaging?
553 bra get_analog_switches2a ; Yes 554 bra get_analog_switches2a ; YES
554 ; Done. Compute average 555 ; NO - done, compute average
555 bcf STATUS,C 556 bcf STATUS,C
556 rrcf analog_sw2_raw+1 557 rrcf analog_sw2_raw+1
557 rrcf analog_sw2_raw+0 ; /2 558 rrcf analog_sw2_raw+0 ; /2
558 bcf STATUS,C 559 bcf STATUS,C
559 rrcf analog_sw2_raw+1 560 rrcf analog_sw2_raw+1
564 bcf STATUS,C 565 bcf STATUS,C
565 rrcf analog_sw2_raw+1 566 rrcf analog_sw2_raw+1
566 rrcf analog_sw2_raw+0 ; /16 567 rrcf analog_sw2_raw+0 ; /16
567 movff analog_sw2_raw+0,analog_sw2 568 movff analog_sw2_raw+0,analog_sw2
568 clrf analog_sw2_raw+1 569 clrf analog_sw2_raw+1
569 clrf analog_sw2_raw+0 ; Reset average registers 570 clrf analog_sw2_raw+0 ; reset average registers
570 ; movlw .16 571 ; movlw .16
571 ; movwf analog_counter ; only once... 572 ; movwf analog_counter ; only once...
572 get_analog_switches2a: 573 get_analog_switches2a:
573 banksel common 574 banksel common
574 bcf analog_sw2_pressed 575 bcf analog_sw2_pressed
575 movff opt_cR_button_left,WREG ;20-100 576 movff opt_cR_button_left,WREG ; 20-100
576 bcf STATUS,C 577 bcf STATUS,C
577 rrcf WREG ;/2 -> 10-50 578 rrcf WREG ; /2 -> 10-50
578 bcf STATUS,C 579 bcf STATUS,C
579 rrcf WREG ;/2 -> 5-25 580 rrcf WREG ; /2 -> 5-25
580 decf WREG,W ;-1 581 decf WREG,W ; -1
581 decf WREG,W ;-1 582 decf WREG,W ; -1
582 decf WREG,W ;-1 -> 2-22 583 decf WREG,W ; -1 -> 2-22
583 banksel analog_sw2 584 banksel analog_sw2
584 btfss button_polarity,1 ;(1= normal, 0=inverted) 585 btfss button_polarity,1 ; (1= normal, 0=inverted)
585 bra sw2_inverted 586 bra sw2_inverted
586 addwf analog_sw2,W ; average (~128) 587 addwf analog_sw2,W ; average (~128)
587 cpfsgt ADRESH 588 cpfsgt ADRESH
588 bra get_analog_sw1 589 bra get_analog_sw1
589 banksel common 590 banksel common
590 bsf analog_sw2_pressed ; Left button normal 591 bsf analog_sw2_pressed ; left button normal
591 bra get_analog_sw1 592 bra get_analog_sw1
592 sw2_inverted: 593 sw2_inverted:
593 subwf analog_sw2,W ; average (~128) 594 subwf analog_sw2,W ; average (~128)
594 cpfslt ADRESH 595 cpfslt ADRESH
595 bra get_analog_sw1 596 bra get_analog_sw1
596 banksel common 597 banksel common
597 bsf analog_sw2_pressed ; Left button inverted 598 bsf analog_sw2_pressed ; left button inverted
598 get_analog_sw1: 599 get_analog_sw1:
599 banksel common 600 banksel common
600 movlw b'00101001' ; power on ADC, select AN10 601 movlw b'00101001' ; power on ADC, select AN10
601 rcall wait_adc 602 rcall wait_adc
602 banksel analog_counter 603 banksel analog_counter
603 movff ADRESH,WREG 604 movff ADRESH,WREG
604 addwf analog_sw1_raw+0 605 addwf analog_sw1_raw+0
605 movlw .0 606 movlw .0
606 addwfc analog_sw1_raw+1 607 addwfc analog_sw1_raw+1
607 tstfsz analog_counter ; continue averaging? 608 tstfsz analog_counter ; continue averaging?
608 bra get_analog_switches1a ; Yes 609 bra get_analog_switches1a ; YES
609 ; Done. Compute average 610 ; NO - done, compute average
610 bcf STATUS,C 611 bcf STATUS,C
611 rrcf analog_sw1_raw+1 612 rrcf analog_sw1_raw+1
612 rrcf analog_sw1_raw+0 ; /2 613 rrcf analog_sw1_raw+0 ; /2
613 bcf STATUS,C 614 bcf STATUS,C
614 rrcf analog_sw1_raw+1 615 rrcf analog_sw1_raw+1
619 bcf STATUS,C 620 bcf STATUS,C
620 rrcf analog_sw1_raw+1 621 rrcf analog_sw1_raw+1
621 rrcf analog_sw1_raw+0 ; /16 622 rrcf analog_sw1_raw+0 ; /16
622 movff analog_sw1_raw+0,analog_sw1 623 movff analog_sw1_raw+0,analog_sw1
623 clrf analog_sw1_raw+1 624 clrf analog_sw1_raw+1
624 clrf analog_sw1_raw+0 ; Reset average registers 625 clrf analog_sw1_raw+0 ; reset average registers
625 movlw .16 626 movlw .16
626 movwf analog_counter ; only once... 627 movwf analog_counter ; only once...
627 get_analog_switches1a: 628 get_analog_switches1a:
628 banksel common 629 banksel common
629 bcf analog_sw1_pressed 630 bcf analog_sw1_pressed
630 movff opt_cR_button_right,WREG ;20-100 631 movff opt_cR_button_right,WREG ; 20-100
631 bcf STATUS,C 632 bcf STATUS,C
632 rrcf WREG ;/2 -> 10-50 633 rrcf WREG ; /2 -> 10-50
633 bcf STATUS,C 634 bcf STATUS,C
634 rrcf WREG ;/2 -> 5-25 635 rrcf WREG ; /2 -> 5-25
635 decf WREG,W ;-1 636 decf WREG,W ; -1
636 decf WREG,W ;-1 637 decf WREG,W ; -1
637 decf WREG,W ;-1 -> 2-22 638 decf WREG,W ; -1 -> 2-22
638 banksel analog_sw1 639 banksel analog_sw1
639 btfss button_polarity,0 ;(1= normal, 0=inverted) 640 btfss button_polarity,0 ; (1= normal, 0=inverted)
640 bra sw1_inverted 641 bra sw1_inverted
641 addwf analog_sw1,W ; average (~128) 642 addwf analog_sw1,W ; average (~128)
642 cpfsgt ADRESH 643 cpfsgt ADRESH
643 bra get_analog_sw_done 644 bra get_analog_sw_done
644 banksel common 645 banksel common
650 bra get_analog_sw_done 651 bra get_analog_sw_done
651 banksel common 652 banksel common
652 bsf analog_sw1_pressed ; right button inverted 653 bsf analog_sw1_pressed ; right button inverted
653 get_analog_sw_done: 654 get_analog_sw_done:
654 banksel common 655 banksel common
655 movlw b'10001101' ; Restore to right justified 656 movlw b'10001101' ; restore to right justified
656 movwf ADCON2 657 movwf ADCON2
657 btfsc analog_sw1_pressed 658 btfsc analog_sw1_pressed
658 return 659 return
659 btfsc analog_sw2_pressed 660 btfsc analog_sw2_pressed
660 return 661 return
661 setf TMR1H ; No button pressed, enhance timer1 to overflow quickly 662 setf TMR1H ; no button pressed, enhance timer1 to overflow quickly
662 return 663 return
663 664
664 END 665 END