comparison src/adc_lightsensor.asm @ 646:5b7fe7777425

3.16 release
author heinrichs weikamp
date Thu, 14 Oct 2021 12:03:24 +0200
parents 8c1f1f334275
children 357341239438
comparison
equal deleted inserted replaced
642:a9a0188091e4 646:5b7fe7777425
23 ;----------------------------------------------------------------------------- 23 ;-----------------------------------------------------------------------------
24 ; Helper Function - wait on ADC to finish conversion 24 ; Helper Function - wait on ADC to finish conversion
25 ; 25 ;
26 ; bank safe 26 ; bank safe
27 ; 27 ;
28 global wait_adc
28 wait_adc: 29 wait_adc:
29 movwf ADCON0 ; select ADC channel 30 movwf ADCON0 ; select ADC channel
30 nop ; wait a short moment 31 nop ; wait a short moment
31 nop ; ... 32 nop ; ...
32 nop ; ..... 33 nop ; .....
259 return ; YES - abort 260 return ; YES - abort
260 banksel HW_descriptor ; NO - select bank where hardware descriptor is stored 261 banksel HW_descriptor ; NO - select bank where hardware descriptor is stored
261 btfsc ambient_sensor ; - ambient sensor available? 262 btfsc ambient_sensor ; - ambient sensor available?
262 bra get_ambient_level1 ; YES - use sensor 263 bra get_ambient_level1 ; YES - use sensor
263 banksel isr_backup ; NO - back to ISR default bank 264 banksel isr_backup ; NO - back to ISR default bank
264 movff opt_brightness,isr_lo ; - get brightness selection 265 movff brightness,isr_lo ; - get brightness selection
265 incf isr_lo,F ; - 0-2 -> 1-3 266 incf isr_lo,F ; - 0-2 -> 1-3
266 movlw ambient_light_max_high_cr ; - default selection to brightest setting 267 movlw ambient_light_max_high_cr ; - default selection to brightest setting
267 dcfsnz isr_lo,F ; - level 0 (eco) selected? 268 dcfsnz isr_lo,F ; - level 0 (eco) selected?
268 movlw ambient_light_max_eco ; YES - select eco brightness 269 movlw ambient_light_max_eco ; YES - select eco brightness
269 dcfsnz isr_lo,F ; - level 1 (medium) selected? 270 dcfsnz isr_lo,F ; - level 1 (medium) selected?
276 banksel isr_backup ; back to ISR default bank 277 banksel isr_backup ; back to ISR default bank
277 clrf ADCON1 ; Vref+ = Vdd 278 clrf ADCON1 ; Vref+ = Vdd
278 movlw b'00011101' ; power on ADC, select AN7 279 movlw b'00011101' ; power on ADC, select AN7
279 rcall wait_adc 280 rcall wait_adc
280 MOVII ADRESL,ambient_light 281 MOVII ADRESL,ambient_light
281 bcf ADCON0,0 ; power off ADC 282 bcf ADCON0,0 ; power off ADC
282 283
284 btfsc ambient_light+1,7 ; result negative?
285 return ; Yes, skip this measurement
283 ; ambient_light:2 is between 4096 (direct sunlight) and about 200 (darkness) 286 ; ambient_light:2 is between 4096 (direct sunlight) and about 200 (darkness)
284 ; first: divide by 16 287 ; first: divide by 16
285
286 movlw .4 ; divide by 2^4 = 16 288 movlw .4 ; divide by 2^4 = 16
287 get_ambient_level1_loop: 289 get_ambient_level1_loop:
288 bcf STATUS,C ; clear carry 290 bcf STATUS,C ; clear carry
289 rrcf ambient_light+1 ; rotate right high byte, carry into MSB, LSB into carry 291 rrcf ambient_light+1 ; rotate right high byte, carry into MSB, LSB into carry
290 rrcf ambient_light+0 ; rotate right low byte, carry into MSB, LSB into carry 292 rrcf ambient_light+0 ; rotate right low byte, carry into MSB, LSB into carry
297 movlw .254 299 movlw .254
298 tstfsz ambient_light+1 ; > 255 ? 300 tstfsz ambient_light+1 ; > 255 ?
299 movwf ambient_light+0 ; YES - avoid ADC clipping 301 movwf ambient_light+0 ; YES - avoid ADC clipping
300 302
301 incfsz ambient_light+0,W ; = 255 ? 303 incfsz ambient_light+0,W ; = 255 ?
302 bra get_ambient_level2 ; NO - continue 304 bra get_ambient_level2 ; NO - continue
303 305
304 movlw .254 306 movlw .254
305 movwf ambient_light+0 ; avoid ADC clipping 307 movwf ambient_light+0 ; avoid ADC clipping
306 308
307 get_ambient_level2: 309 get_ambient_level2:
308 movff opt_brightness,isr_lo ; get brightness setting 310 movff brightness,isr_lo ; get brightness setting
309 311
310 btfsc RCSTA1,7 ; UART module on? 312 btfsc RCSTA1,7 ; UART module on?
311 clrf isr_lo ; YES - set temporary to eco mode 313 clrf isr_lo ; YES - set temporary to eco mode
312 314
313 incf isr_lo,F ; adjust 0-2 to 1-3 315 incf isr_lo,F ; adjust 0-2 to 1-3
327 329
328 incf ambient_light+0,F ; +1 330 incf ambient_light+0,F ; +1
329 cpfslt ambient_light+0 ; smaller than WREG? 331 cpfslt ambient_light+0 ; smaller than WREG?
330 movwf ambient_light+0 ; NO - set to max. 332 movwf ambient_light+0 ; NO - set to max.
331 333
332 movff opt_brightness,isr_lo ; get brightness setting 334 movff brightness,isr_lo ; get brightness setting
333 incf isr_lo,F ; adjust 0-2 to 1-3 335 incf isr_lo,F ; adjust 0-2 to 1-3
334 movlw ambient_light_min_high ; default to highest setting 336 movlw ambient_light_min_high ; default to highest setting
335 dcfsnz isr_lo,F ; eco setting? 337 dcfsnz isr_lo,F ; eco setting?
336 movlw ambient_light_min_eco ; YES 338 movlw ambient_light_min_eco ; YES
337 dcfsnz isr_lo,F ; medium setting? 339 dcfsnz isr_lo,F ; medium setting?