Mercurial > public > hwos_code
comparison src/adc_lightsensor.asm @ 643:7d8a4c60ec1a
3.15 release
| author | heinrichsweikamp |
|---|---|
| date | Mon, 24 May 2021 18:40:53 +0200 |
| parents | 8c1f1f334275 |
| children | 070528a88715 357341239438 |
comparison
equal
deleted
inserted
replaced
| 642:a9a0188091e4 | 643:7d8a4c60ec1a |
|---|---|
| 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 ; ..... |
| 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: |
