comparison src/adc_lightsensor.asm @ 631:185ba2f91f59

3.09 beta 1 release
author heinrichsweikamp
date Fri, 28 Feb 2020 15:45:07 +0100
parents 237931377539
children 4050675965ea
comparison
equal deleted inserted replaced
630:4cd81bdbf15c 631:185ba2f91f59
1 ;============================================================================= 1 ;=============================================================================
2 ; 2 ;
3 ; File adc_lightsensor.asm combined next generation V3.06.1 3 ; File adc_lightsensor.asm combined next generation V3.08.8
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
18 adc_light CODE 18 adc_light CODE
19 19
20 ;============================================================================= 20 ;=============================================================================
21 21
22 wait_adc: ; bank-safe 22 wait_adc: ; bank-safe
23 movwf ADCON0 23 movwf ADCON0 ; select ADC channel
24 nop 24 nop ; wait a short moment
25 bsf ADCON0,1 ; start ADC 25 bsf ADCON0,1 ; start ADC
26 wait_adc2: 26 wait_adc2:
27 btfsc ADCON0,1 ; wait... 27 btfsc ADCON0,1 ; ADC done?
28 bra wait_adc2 28 bra wait_adc2 ; NO - wait
29 return 29 return ; YES - done
30 30
31 global get_battery_voltage 31 global get_battery_voltage
32 get_battery_voltage: ; start ADC and wait until finished 32 get_battery_voltage: ; start ADC and wait until finished
33 btfss battery_gauge_available ; battery gauge IC available? 33 btfss battery_gauge_available ; battery gauge IC available?
34 bra get_battery_voltage_2 ; NO - OSTC hardware without gauge IC 34 bra get_battery_voltage_2 ; NO - OSTC hardware without gauge IC
35 call lt2942_get_accumulated_charge ; YES - read coulomb counter 35 bsf battery_is_36v ; YES - gauge IC available, 3.6V battery
36 call lt2942_get_accumulated_charge ; - read coulomb counter
36 call lt2942_get_voltage ; - read battery voltage 37 call lt2942_get_voltage ; - read battery voltage
37 call lt2942_get_temperature ; - read battery temperature 38 call lt2942_get_temperature ; - read battery temperature
38 tstfsz batt_voltage+1 ; - read voltage < 256 mV ? 39 tstfsz batt_voltage+1 ; - read voltage < 256 mV ?
39 bra get_battery_voltage_1 ; NO - proceed 40 bra get_battery_voltage_1 ; NO - proceed
40 call lt2942_get_accumulated_charge ; YES - re-read coulomb counter 41 call lt2942_get_accumulated_charge ; YES - re-read coulomb counter
41 call lt2942_get_voltage ; - re-read battery voltage 42 call lt2942_get_voltage ; - re-read battery voltage
42 call lt2942_get_temperature ; - re-read battery temperature 43 call lt2942_get_temperature ; - re-read battery temperature
43 ;bra get_battery_voltage_1 ; - proceed 44 ;bra get_battery_voltage_1 ; - proceed
44 45
45 get_battery_voltage_1: 46 get_battery_voltage_1:
47 rcall get_battery_voltage_low ; check for battery low condition
46 btfsc divemode ; in dive mode? 48 btfsc divemode ; in dive mode?
47 return ; YES - done 49 return ; YES - done
48 50
49 bcf cv_active ; clear CV charing status by default 51 bcf cv_active ; clear CV charing status by default
50 bcf cc_active ; clear CC charing status ny default 52 bcf cc_active ; clear CC charing status ny default
59 btfsc CHRG_IN ; - still charging? 61 btfsc CHRG_IN ; - still charging?
60 return ; NO - done 62 return ; NO - done
61 ;bra charge_cv_active ; YES - charging in CV mode 63 ;bra charge_cv_active ; YES - charging in CV mode
62 64
63 charge_cv_active: 65 charge_cv_active:
64 decfsz get_bat_volt_counter,F 66 decfsz get_bat_volt_counter,F ; decrement counter, became zero?
65 return 67 return ; NO - not yet, done
66 movlw .15 68 movlw .15 ; YES - battery voltage >= 16*256mV (4.096V)
67 cpfsgt batt_voltage+1 ; battery voltage >= 16*256mV (4.096V) ? 69 cpfsgt batt_voltage+1 ; - ... ?
68 bra charge_cc_active ; NO 70 bra charge_cc_active ; NO
69 bsf cc_active 71 bsf cc_active ; YES - set CC charging status
70 bsf cv_active 72 bsf cv_active ; - set CV charging status
73 bsf LEDr ; - indicate charging
74 call lt2942_charge_done ; - reset accumulating registers to 0xFFFF
75 WAITMS d'10' ; - wait 10 ms
76 bcf LEDr ; - indicate charging
77 bsf get_bat_volt_counter,0 ; - set counter to 1
78 return ; - done
79
80 charge_cc_active:
81 bsf cc_active ; set CC charging mode
71 bsf LEDr ; indicate charging 82 bsf LEDr ; indicate charging
72 call lt2942_charge_done ; reset accumulating registers to 0xFFFF 83 bcf CHRG_OUT ;
73 WAITMS d'10'
74 bcf LEDr ; indicate charging
75 bsf get_bat_volt_counter,0 ; =1
76 return
77
78 charge_cc_active:
79 bsf cc_active
80 bsf LEDr ; indicate charging
81 bcf CHRG_OUT
82 bsf TRISJ,2 ; set chrg-Out output to high impedance 84 bsf TRISJ,2 ; set chrg-Out output to high impedance
83 movlw .15 85 movlw .15 ; battery voltage >= 16*256mV (4.096 V)
84 cpfsgt batt_voltage+1 ; battery voltage >= 16*256mV (4.096 V)? 86 cpfsgt batt_voltage+1 ; ... ?
85 bra charge_cc_active2 ; NO 87 bra charge_cc_active2 ; NO
86 movlw .81 88 movlw .81 ; YES - battery voltage >= 80mV (+4096mV from batt_voltage+1)
87 cpfslt batt_voltage+0 ; battery voltage >= 80mV (+4096mV from batt_voltage+1)? 89 cpfslt batt_voltage+0 ; - ... ?
88 bra charge_cv_active ; YES 90 bra charge_cv_active ; YES
89 charge_cc_active2: 91 charge_cc_active2:
90 movlw .10 92 movlw .10 ; NO - set counter to 10
91 movwf get_bat_volt_counter 93 movwf get_bat_volt_counter ; - ...
92 return 94 return ; - done
93 95
94 get_battery_voltage_2: ; no gauge IC available, use ADC to measure battery voltage 96 get_battery_voltage_2: ; no gauge IC available, use ADC to measure battery voltage
95 ; additional charging disable in software 97 ; additional charging disable in software
96 bsf charge_disable ; set charging-inhibit signal 98 bsf charge_disable ; set charging-inhibit signal
97 bcf charge_enable ; activate charging-inhibit signal 99 bcf charge_enable ; activate charging-inhibit signal
98 100
99 bsf adc_is_running ; =1: the ADC is in use 101 bsf adc_is_running ; =1: the ADC is in use
100 movlw b'00100000' ; 2.048 Volt Vref+ -> 1 LSB = 500 µV 102 movlw b'00100000' ; 2.048 Volt Vref+ -> 1 LSB = 500 µV
101 movwf ADCON1 103 movwf ADCON1 ; ...
102 movlw b'00011001' ; power on ADC, select AN6 104 movlw b'00011001' ; power on ADC, select AN6
103 rcall wait_adc 105 rcall wait_adc ; take measurement
104
105 MOVII ADRESL,batt_voltage ; store value 106 MOVII ADRESL,batt_voltage ; store value
106 bcf ADCON0,0 ; power off ADC 107 bcf ADCON0,0 ; power off ADC
107 108
108 ; Multiply with 2.006 to be exact here... 109 ; Multiply with 2.006 to be exact here...
109 ; bcf STATUS,C 110 ; bcf STATUS,C
128 bra get_battery_voltage_3 ; YES - battery is still ok 129 bra get_battery_voltage_3 ; YES - battery is still ok
129 movlw .128 ; NO - battery is probably dead very soon, set ">=24Ah used" 130 movlw .128 ; NO - battery is probably dead very soon, set ">=24Ah used"
130 movff WREG,battery_gauge+5 ; - into battery gauge registers 131 movff WREG,battery_gauge+5 ; - into battery gauge registers
131 132
132 get_battery_voltage_3: ; 3.6V battery gauge mode 133 get_battery_voltage_3: ; 3.6V battery gauge mode
133 ; SMOVFF "by hand" as the macro does not work with arguments that have a '+something' with them 134 ; SMOVQQ "by hand" as the macro does not work with arguments that have a '+something' with them
134 bcf trigger_isr_updates ; clear flag, it will be set by the ISR in case it had kicked in 135 bcf trigger_isr_updates ; clear flag, it will be set by the ISR in case it had kicked in
135 movff battery_gauge+5,xC+3 136 movff battery_gauge+5,xC+3
136 movff battery_gauge+4,xC+2 137 movff battery_gauge+4,xC+2
137 movff battery_gauge+3,xC+1 138 movff battery_gauge+3,xC+1
138 movff battery_gauge+2,xC+0 139 movff battery_gauge+2,xC+0
198 cpfsgt lo ; current battery % < last battery % ? 199 cpfsgt lo ; current battery % < last battery % ?
199 movff lo,batt_percent ; YES - take new value (keep batt_percent on the lowest value found) 200 movff lo,batt_percent ; YES - take new value (keep batt_percent on the lowest value found)
200 btfsc battery_is_36v ; using a 3.6 volt battery? 201 btfsc battery_is_36v ; using a 3.6 volt battery?
201 movff lo,batt_percent ; YES - take new value (always use computed value for 3.6V battery) 202 movff lo,batt_percent ; YES - take new value (always use computed value for 3.6V battery)
202 bcf adc_is_running ; done with ADC 203 bcf adc_is_running ; done with ADC
203 return 204 ;bra get_battery_voltage_low ; check for battery low condition (and return)
205
206 get_battery_voltage_low:
207 ; check for battery low condition
208 movlw battery_warn_level_36+1 ; get threshold for 3.6 Volt battery warning, incremented by 1
209 btfss battery_is_36v ; actually a 3.6 Volt battery detected?
210 movlw battery_warn_level_15+1 ; NO - replace with 1.5 Volt battery warning, incremented by 1
211 bsf battery_low_condition ; set battery low condition by default
212 cpfslt batt_percent ; current battery level <= warning threshold ?
213 bcf battery_low_condition ; NO - clear battery low condition
214 return ; - done
215
204 216
205 get_battery_voltage_9: 217 get_battery_voltage_9:
206 ; use 1.5V battery voltage mode 218 ; use 1.5V battery voltage mode
207 ; use approximation (batt_voltage-aa_15v_low)/4 = lo 219 ; use approximation (batt_voltage-aa_15v_low)/4 = lo
208 MOVII batt_voltage,sub_a ; load battery voltage 220 MOVII batt_voltage,sub_a ; load battery voltage
494 get_analog_switches_1: 506 get_analog_switches_1:
495 banksel common ; back to bank common 507 banksel common ; back to bank common
496 btfsc adc_is_running ; ADC in use? 508 btfsc adc_is_running ; ADC in use?
497 return ; YES - abort 509 return ; YES - abort
498 btfsc cc_active ; NO - charging? 510 btfsc cc_active ; NO - charging?
499 bra get_analog_switches0 ; YES - abort (And clear both flags) 511 bra get_analog_switches0 ; YES - abort (and clear both flags)
500 ;bra get_analog_switches_2 ; NO - read switches 512 ;bra get_analog_switches_2 ; NO - read switches
501 513
502 get_analog_switches_2: 514 get_analog_switches_2:
503 bsf adc_is_running 515 bsf adc_is_running
504 bcf ADCON2,ADFM ; left justified 516 bcf ADCON2,ADFM ; left justified
505 clrf ADCON1 517 clrf ADCON1
506 movlw b'00100101' ; power on ADC, select AN9 518 movlw b'00100101' ; power on ADC, select AN9
507 rcall wait_adc 519 rcall wait_adc
508 520
509 banksel isr_backup ; select bank ISR data 521 banksel isr_backup ; select bank ISR data
609 621
610 get_analog_switches_sw1_pressed: 622 get_analog_switches_sw1_pressed:
611 bsf analog_sw1_pressed ; set right button as pressed 623 bsf analog_sw1_pressed ; set right button as pressed
612 624
613 get_analog_switches_4: 625 get_analog_switches_4:
614 bsf ADCON2,ADFM ; restore to right justified 626 bsf ADCON2,ADFM ; restore to right justified
615 bcf adc_is_running 627 bcf adc_is_running
616 banksel common ; back to bank common 628 banksel common ; back to bank common
617 btfsc analog_sw1_pressed ; right button pressed? 629 btfsc analog_sw1_pressed ; right button pressed?
618 return ; YES - done 630 return ; YES - done
619 btfsc analog_sw2_pressed ; left button pressed? 631 btfsc analog_sw2_pressed ; left button pressed?
620 return ; YES - done 632 return ; YES - done