annotate src/adc_lightsensor.asm @ 633:690c48db7b5b

3.09 beta 2 release
author heinrichsweikamp
date Thu, 05 Mar 2020 15:06:14 +0100
parents 185ba2f91f59
children 4050675965ea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
heinrichsweikamp
parents:
diff changeset
1 ;=============================================================================
heinrichsweikamp
parents:
diff changeset
2 ;
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 629
diff changeset
3 ; File adc_lightsensor.asm combined next generation V3.08.8
0
heinrichsweikamp
parents:
diff changeset
4 ;
heinrichsweikamp
parents:
diff changeset
5 ;
heinrichsweikamp
parents:
diff changeset
6 ; Copyright (c) 2011, JD Gascuel, HeinrichsWeikamp, all right reserved.
heinrichsweikamp
parents:
diff changeset
7 ;=============================================================================
heinrichsweikamp
parents:
diff changeset
8 ; HISTORY
heinrichsweikamp
parents:
diff changeset
9 ; 2011-08-08 : [mH] moving from OSTC code
heinrichsweikamp
parents:
diff changeset
10
275
653a3ab08062 rename into hwOS
heinrichsweikamp
parents: 270
diff changeset
11 #include "hwos.inc"
0
heinrichsweikamp
parents:
diff changeset
12 #include "math.inc"
heinrichsweikamp
parents:
diff changeset
13 #include "wait.inc"
heinrichsweikamp
parents:
diff changeset
14 #include "eeprom_rs232.inc"
113
heinrichsweikamp
parents: 44
diff changeset
15 #include "i2c.inc"
0
heinrichsweikamp
parents:
diff changeset
16
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
17
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
18 adc_light CODE
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
19
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
20 ;=============================================================================
0
heinrichsweikamp
parents:
diff changeset
21
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
22 wait_adc: ; bank-safe
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 629
diff changeset
23 movwf ADCON0 ; select ADC channel
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 629
diff changeset
24 nop ; wait a short moment
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
25 bsf ADCON0,1 ; start ADC
0
heinrichsweikamp
parents:
diff changeset
26 wait_adc2:
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 629
diff changeset
27 btfsc ADCON0,1 ; ADC done?
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 629
diff changeset
28 bra wait_adc2 ; NO - wait
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 629
diff changeset
29 return ; YES - done
0
heinrichsweikamp
parents:
diff changeset
30
heinrichsweikamp
parents:
diff changeset
31 global get_battery_voltage
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
32 get_battery_voltage: ; start ADC and wait until finished
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
33 btfss battery_gauge_available ; battery gauge IC available?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
34 bra get_battery_voltage_2 ; NO - OSTC hardware without gauge IC
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 629
diff changeset
35 bsf battery_is_36v ; YES - gauge IC available, 3.6V battery
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 629
diff changeset
36 call lt2942_get_accumulated_charge ; - read coulomb counter
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
37 call lt2942_get_voltage ; - read battery voltage
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
38 call lt2942_get_temperature ; - read battery temperature
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
39 tstfsz batt_voltage+1 ; - read voltage < 256 mV ?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
40 bra get_battery_voltage_1 ; NO - proceed
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
41 call lt2942_get_accumulated_charge ; YES - re-read coulomb counter
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
42 call lt2942_get_voltage ; - re-read battery voltage
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
43 call lt2942_get_temperature ; - re-read battery temperature
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
44 ;bra get_battery_voltage_1 ; - proceed
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
45
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
46 get_battery_voltage_1:
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 629
diff changeset
47 rcall get_battery_voltage_low ; check for battery low condition
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
48 btfsc divemode ; in dive mode?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
49 return ; YES - done
120
e2f04bb2539c battery check in sleep
heinrichsweikamp
parents: 118
diff changeset
50
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
51 bcf cv_active ; clear CV charing status by default
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
52 bcf cc_active ; clear CC charing status ny default
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
53 bcf LEDr ; switch off red LED
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
54 bcf TRISJ,2 ; activate Chrg-Out output
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
55 bsf CHRG_OUT ; start charger
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
56 btfss CHRG_IN ; charging?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
57 bra charge_cc_active ; YES - charging in CC mode
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
58 bcf CHRG_OUT ; NO - stop charger
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
59 bsf TRISJ,2 ; - set Chrg-Out output to high impedance
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
60 WAITMS d'1' ; - wait 1 ms
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
61 btfsc CHRG_IN ; - still charging?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
62 return ; NO - done
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
63 ;bra charge_cv_active ; YES - charging in CV mode
120
e2f04bb2539c battery check in sleep
heinrichsweikamp
parents: 118
diff changeset
64
603
00b24fb4324d second method to detect a full charge
heinrichsweikamp
parents: 582
diff changeset
65 charge_cv_active:
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 629
diff changeset
66 decfsz get_bat_volt_counter,F ; decrement counter, became zero?
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 629
diff changeset
67 return ; NO - not yet, done
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 629
diff changeset
68 movlw .15 ; YES - battery voltage >= 16*256mV (4.096V)
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 629
diff changeset
69 cpfsgt batt_voltage+1 ; - ... ?
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 629
diff changeset
70 bra charge_cc_active ; NO
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 629
diff changeset
71 bsf cc_active ; YES - set CC charging status
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 629
diff changeset
72 bsf cv_active ; - set CV charging status
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 629
diff changeset
73 bsf LEDr ; - indicate charging
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 629
diff changeset
74 call lt2942_charge_done ; - reset accumulating registers to 0xFFFF
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 629
diff changeset
75 WAITMS d'10' ; - wait 10 ms
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 629
diff changeset
76 bcf LEDr ; - indicate charging
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 629
diff changeset
77 bsf get_bat_volt_counter,0 ; - set counter to 1
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 629
diff changeset
78 return ; - done
120
e2f04bb2539c battery check in sleep
heinrichsweikamp
parents: 118
diff changeset
79
207
f5d909e03f52 charge indicator on surface screen
heinrichsweikamp
parents: 204
diff changeset
80 charge_cc_active:
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 629
diff changeset
81 bsf cc_active ; set CC charging mode
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
82 bsf LEDr ; indicate charging
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 629
diff changeset
83 bcf CHRG_OUT ;
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
84 bsf TRISJ,2 ; set chrg-Out output to high impedance
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 629
diff changeset
85 movlw .15 ; battery voltage >= 16*256mV (4.096 V)
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 629
diff changeset
86 cpfsgt batt_voltage+1 ; ... ?
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
87 bra charge_cc_active2 ; NO
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 629
diff changeset
88 movlw .81 ; YES - battery voltage >= 80mV (+4096mV from batt_voltage+1)
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 629
diff changeset
89 cpfslt batt_voltage+0 ; - ... ?
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 629
diff changeset
90 bra charge_cv_active ; YES
603
00b24fb4324d second method to detect a full charge
heinrichsweikamp
parents: 582
diff changeset
91 charge_cc_active2:
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 629
diff changeset
92 movlw .10 ; NO - set counter to 10
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 629
diff changeset
93 movwf get_bat_volt_counter ; - ...
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 629
diff changeset
94 return ; - done
113
heinrichsweikamp
parents: 44
diff changeset
95
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
96 get_battery_voltage_2: ; no gauge IC available, use ADC to measure battery voltage
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
97 ; additional charging disable in software
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
98 bsf charge_disable ; set charging-inhibit signal
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
99 bcf charge_enable ; activate charging-inhibit signal
615
b87f23fae743 work on new battery menu
heinrichsweikamp
parents: 608
diff changeset
100
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
101 bsf adc_is_running ; =1: the ADC is in use
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
102 movlw b'00100000' ; 2.048 Volt Vref+ -> 1 LSB = 500 µV
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 629
diff changeset
103 movwf ADCON1 ; ...
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
104 movlw b'00011001' ; power on ADC, select AN6
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 629
diff changeset
105 rcall wait_adc ; take measurement
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
106 MOVII ADRESL,batt_voltage ; store value
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
107 bcf ADCON0,0 ; power off ADC
0
heinrichsweikamp
parents:
diff changeset
108
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
109 ; Multiply with 2.006 to be exact here...
0
heinrichsweikamp
parents:
diff changeset
110 ; bcf STATUS,C
heinrichsweikamp
parents:
diff changeset
111 ; rlcf xA+0,F
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
112 ; rlcf xA+1,F ; x2
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
113 ; MOVII xA,batt_voltage ; store value
0
heinrichsweikamp
parents:
diff changeset
114
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
115 bcf battery_is_36v ; by default assume it is a 1.5 battery
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
116 MOVII batt_voltage,sub_b ; load measured battery voltage
0
heinrichsweikamp
parents:
diff changeset
117
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
118 ; Check if the battery is a 3.6V lithium
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
119 MOVLI lithium_36v_low,sub_a ; load threshold for 3.6 Volt lithium battery
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
120 call cmpU16 ; sub_a - sub_b
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
121 btfss neg_flag ; battery voltage > 3.6 V lithium threshold ?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
122 bra get_battery_voltage_9 ; NO - keep assumption of 1.5V battery
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
123 bsf battery_is_36v ; YES - set flag
0
heinrichsweikamp
parents:
diff changeset
124
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
125 ; Check if the battery is near-dead already
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
126 MOVLI lithium_36v_empty,sub_a ; load threshold for near-dead lithium battery
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
127 call cmpU16 ; sub_a - sub_b
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
128 btfsc neg_flag ; battery voltage > dead-battery threshold ?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
129 bra get_battery_voltage_3 ; YES - battery is still ok
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
130 movlw .128 ; NO - battery is probably dead very soon, set ">=24Ah used"
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
131 movff WREG,battery_gauge+5 ; - into battery gauge registers
0
heinrichsweikamp
parents:
diff changeset
132
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
133 get_battery_voltage_3: ; 3.6V battery gauge mode
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 629
diff changeset
134 ; SMOVQQ "by hand" as the macro does not work with arguments that have a '+something' with them
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
135 bcf trigger_isr_updates ; clear flag, it will be set by the ISR in case it had kicked in
0
heinrichsweikamp
parents:
diff changeset
136 movff battery_gauge+5,xC+3
heinrichsweikamp
parents:
diff changeset
137 movff battery_gauge+4,xC+2
heinrichsweikamp
parents:
diff changeset
138 movff battery_gauge+3,xC+1
heinrichsweikamp
parents:
diff changeset
139 movff battery_gauge+2,xC+0
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
140 btfsc trigger_isr_updates ; did the ISR kicked in since we cleared the flag?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
141 bra get_battery_voltage_3 ; YES - retry copy
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
142
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
143 ; battery_gauge: 6 is nAs
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
144 ; divide through 65536
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
145 ; divide through battery_capacity:2
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
146 ; result is in percent
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
147 MOVII battery_capacity_internal,xB
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
148 call div32x16 ; xC:4 = xC:4 / xB:2 with xA as remainder
0
heinrichsweikamp
parents:
diff changeset
149 movff xC+0,lo
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
150 ; limit to 100
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
151 movlw .100
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
152 cpfslt lo
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
153 movwf lo
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
154 ; lo will be between 0 (full) and 100 (empty)
0
heinrichsweikamp
parents:
diff changeset
155 movf lo,W
heinrichsweikamp
parents:
diff changeset
156 sublw .100
heinrichsweikamp
parents:
diff changeset
157 movwf lo
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
158 get_battery_voltage_4:
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
159 movlw .100 ; start with default of 100%
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
160 cpfslt lo ; > 100%
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
161 movwf lo ; YES - limit to 100%
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
162 ; lo will be between 100 (full) and 0 (empty)
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
163 ; use 3.6V battery sensing based on 50 mA load
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
164 MOVLI lithium_36v_75,sub_a ; load threshold for > 75%
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
165 call cmpU16 ; sub_a - sub_b
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
166 btfsc neg_flag ; battery voltage above 75% level?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
167 bra get_battery_voltage_5 ; YES
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
168 movlw .75 ; NO - set to 75%
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
169 movwf lo
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
170 get_battery_voltage_5:
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
171 ; 50%
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
172 MOVLI lithium_36v_50,sub_a ; load threshold for > 50%
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
173 call cmpU16 ; sub_a - sub_b
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
174 btfsc neg_flag ; battery voltage above 75% level?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
175 bra get_battery_voltage_6 ; YES
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
176 movlw .50 ; NO - set to 50%
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
177 movwf lo
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
178 get_battery_voltage_6:
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
179 ; 25%
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
180 MOVLI lithium_36v_25,sub_a ; load threshold for > 25%
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
181 call cmpU16 ; sub_a - sub_b
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
182 btfsc neg_flag ; battery voltage above 25% level?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
183 bra get_battery_voltage_7 ; YES
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
184 movlw .25 ; NO - set to 25%
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
185 movwf lo
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
186 get_battery_voltage_7:
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
187 ; 10%
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
188 MOVLI lithium_36v_10,sub_a ; load threshold for > 10%
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
189 call cmpU16 ; sub_a - sub_b
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
190 btfsc neg_flag ; battery voltage above 10% level?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
191 bra get_battery_voltage_8 ; YES
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
192 movlw .10 ; NO - set to 10%
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
193 movwf lo
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
194 get_battery_voltage_8:
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
195 movlw .100 ; maximum is 100 (%)
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
196 cpfslt lo ; > 100% ?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
197 movwf lo ; YES - limit to 100%
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
198 movf batt_percent,W ; get last battery percentage
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
199 cpfsgt lo ; current battery % < last battery % ?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
200 movff lo,batt_percent ; YES - take new value (keep batt_percent on the lowest value found)
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
201 btfsc battery_is_36v ; using a 3.6 volt battery?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
202 movff lo,batt_percent ; YES - take new value (always use computed value for 3.6V battery)
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
203 bcf adc_is_running ; done with ADC
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 629
diff changeset
204 ;bra get_battery_voltage_low ; check for battery low condition (and return)
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 629
diff changeset
205
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 629
diff changeset
206 get_battery_voltage_low:
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 629
diff changeset
207 ; check for battery low condition
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 629
diff changeset
208 movlw battery_warn_level_36+1 ; get threshold for 3.6 Volt battery warning, incremented by 1
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 629
diff changeset
209 btfss battery_is_36v ; actually a 3.6 Volt battery detected?
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 629
diff changeset
210 movlw battery_warn_level_15+1 ; NO - replace with 1.5 Volt battery warning, incremented by 1
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 629
diff changeset
211 bsf battery_low_condition ; set battery low condition by default
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 629
diff changeset
212 cpfslt batt_percent ; current battery level <= warning threshold ?
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 629
diff changeset
213 bcf battery_low_condition ; NO - clear battery low condition
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 629
diff changeset
214 return ; - done
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 629
diff changeset
215
0
heinrichsweikamp
parents:
diff changeset
216
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
217 get_battery_voltage_9:
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
218 ; use 1.5V battery voltage mode
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
219 ; use approximation (batt_voltage-aa_15v_low)/4 = lo
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
220 MOVII batt_voltage,sub_a ; load battery voltage
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
221 MOVLI aa_15v_low, sub_b ; load offset
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
222 call subU16 ; sub_c = sub_a - sub_b
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
223 bcf STATUS,C ; shift right to divide / 2
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
224 rrcf sub_c+1
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
225 rrcf sub_c+0
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
226 bcf STATUS,C ; another shift right to divide / 4
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
227 rrcf sub_c+1
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
228 rrcf sub_c+0
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
229 movff sub_c+0,lo ; store result
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
230 bra get_battery_voltage_8 ; check limits and return
0
heinrichsweikamp
parents:
diff changeset
231
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
232
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
233 global get_ambient_level ; called from ISR only, in context bank isr_backup
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
234 get_ambient_level:
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
235 btfsc sleepmode ; in sleep mode?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
236 return ; YES - done
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
237 btfsc adc_is_running ; NO - ADC in use?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
238 return ; YES - return
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
239 banksel HW_descriptor ; NO - select bank where hardware descriptor is stored
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
240 btfsc ambient_sensor ; - ambient sensor available?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
241 bra get_ambient_level1 ; YES - use sensor
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
242 banksel isr_backup ; NO - back to ISR default bank
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
243 movff opt_brightness,isr_lo ; - get brightness selection
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
244 incf isr_lo,F ; - 0-2 -> 1-3
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
245 movlw ambient_light_max_high_cr ; - default selection to brightest setting
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
246 dcfsnz isr_lo,F ; - level 0 (eco) selected?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
247 movlw ambient_light_max_eco ; YES - select eco brightness
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
248 dcfsnz isr_lo,F ; - level 1 (medium) selected?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
249 movlw ambient_light_max_medium ; YES - select medium brightness
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
250 movwf ambient_light+0 ; - store selection
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
251 movwf max_CCPR1L ; - store value for dimming in TMR7 interrupt
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
252 return ; - done
203
dd9b167e82dc 1.64 (Fix ambient light for cR)
heinrichsweikamp
parents: 201
diff changeset
253
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
254 get_ambient_level1: ; using ambient sensor
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
255 banksel isr_backup ; back to ISR default bank
629
237931377539 3.07 stable release
heinrichsweikamp
parents: 628
diff changeset
256 clrf ADCON1 ; Vref+ = Vdd
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
257 movlw b'00011101' ; power on ADC, select AN7
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
258 rcall wait_adc
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
259 MOVII ADRESL,ambient_light
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
260 bcf ADCON0,0 ; power off ADC
0
heinrichsweikamp
parents:
diff changeset
261
heinrichsweikamp
parents:
diff changeset
262 ; ambient_light:2 is between 4096 (direct sunlight) and about 200 (darkness)
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
263 ; first: divide by 16
0
heinrichsweikamp
parents:
diff changeset
264 bcf STATUS,C
heinrichsweikamp
parents:
diff changeset
265 rrcf ambient_light+1
heinrichsweikamp
parents:
diff changeset
266 rrcf ambient_light+0
heinrichsweikamp
parents:
diff changeset
267 bcf STATUS,C
heinrichsweikamp
parents:
diff changeset
268 rrcf ambient_light+1
heinrichsweikamp
parents:
diff changeset
269 rrcf ambient_light+0
heinrichsweikamp
parents:
diff changeset
270 bcf STATUS,C
heinrichsweikamp
parents:
diff changeset
271 rrcf ambient_light+1
heinrichsweikamp
parents:
diff changeset
272 rrcf ambient_light+0
heinrichsweikamp
parents:
diff changeset
273 bcf STATUS,C
heinrichsweikamp
parents:
diff changeset
274 rrcf ambient_light+1
heinrichsweikamp
parents:
diff changeset
275 rrcf ambient_light+0
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
276 ; result: ambient_light:2/16
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
277 ; now make sure to have value between ambient_light_low and ambient_light_max
0
heinrichsweikamp
parents:
diff changeset
278
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
279 movlw .254
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
280 tstfsz ambient_light+1 ; > 255 ?
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
281 movwf ambient_light+0 ; YES - avoid ADC clipping
0
heinrichsweikamp
parents:
diff changeset
282
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
283 incfsz ambient_light+0,W ; = 255 ?
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
284 bra get_ambient_level2 ; NO - continue
0
heinrichsweikamp
parents:
diff changeset
285
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
286 movlw .254
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
287 movwf ambient_light+0 ; avoid ADC clipping
0
heinrichsweikamp
parents:
diff changeset
288
heinrichsweikamp
parents:
diff changeset
289 get_ambient_level2:
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
290 ; movlw .10
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
291 ; subwf ambient_light+0,F ; subtract 10 (ADC Offset)
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
292 ; btfsc STATUS,N
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
293 ; movwf ambient_light+0 ; avoid clipping
201
82faedf8fd58 lower minimum backlight in eco mode
heinrichsweikamp
parents: 200
diff changeset
294
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
295 movff opt_brightness,isr_lo ; get brightness setting
0
heinrichsweikamp
parents:
diff changeset
296
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
297 btfsc RCSTA1,7 ; UART module on?
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
298 clrf isr_lo ; YES - set temporary to eco mode
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
299 incf isr_lo,F ; adjust 0-2 to 1-3
0
heinrichsweikamp
parents:
diff changeset
300
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
301 movlw ambient_light_max_high_cr ; cR and 2 hardware brightest setting
0
heinrichsweikamp
parents:
diff changeset
302
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
303 banksel HW_descriptor ; select bank where hardware descriptor and model variant is stored
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
304 btfss battery_gauge_available
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
305 movlw ambient_light_max_high_15V ; 1.5V battery brightest setting
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
306 btfsc battery_is_36v ; 3.6V battery in use?
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
307 movlw ambient_light_max_high_36V ; YES - 3.6V battery brightest setting
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
308 banksel isr_backup ; back to ISR default bank
0
heinrichsweikamp
parents:
diff changeset
309
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
310 dcfsnz isr_lo,F
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
311 movlw ambient_light_max_eco ; eco setting
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
312 dcfsnz isr_lo,F
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
313 movlw ambient_light_max_medium ; brightest setting
0
heinrichsweikamp
parents:
diff changeset
314
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
315 incf ambient_light+0,F ; +1
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
316 cpfslt ambient_light+0 ; smaller than WREG?
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
317 movwf ambient_light+0 ; NO - set to max.
0
heinrichsweikamp
parents:
diff changeset
318
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
319 movff opt_brightness,isr_lo
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
320 incf isr_lo,F ; adjust 0-2 to 1-3
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
321 movlw ambient_light_min_high ; darkest setting
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
322 dcfsnz isr_lo,F
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
323 movlw ambient_light_min_eco ; darkest setting
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
324 dcfsnz isr_lo,F
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
325 movlw ambient_light_min_medium ; darkest setting
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
326 dcfsnz isr_lo,F
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
327 movlw ambient_light_min_high ; darkest setting
0
heinrichsweikamp
parents:
diff changeset
328
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
329 cpfsgt ambient_light+0 ; bigger than WREG?
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
330 movwf ambient_light+0 ; NO - set to min
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
331
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
332 movff ambient_light+0,max_CCPR1L ; store value for dimming in TMR7 interrupt
0
heinrichsweikamp
parents:
diff changeset
333 return
heinrichsweikamp
parents:
diff changeset
334
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
335
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
336 ;=============================================================================
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
337 ; routines for reading analog-attached external sensors
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
338 ;
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
339 IFDEF _external_sensor
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
340
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
341 global get_analog_inputs ; called from outside ISR only
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
342 get_analog_inputs: ; start ADC and wait until finished
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
343 bsf adc_is_running ; =1: the ADC is in use
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
344 btfsc TFT_PWM
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
345 bra get_analog_inputs ; wait for PWM low
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
346 movlw b'00100000' ; 2.048V Vref+ -> 1 LSB = 500 µV
113
heinrichsweikamp
parents: 44
diff changeset
347 movwf ADCON1
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
348 banksel sensor1_mv ; select bank where sensor1_mv is stored
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
349 ; Sensor 1
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
350 movlw b'00100001' ; power on ADC, select AN8
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
351 rcall wait_adc
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
352 bcf STATUS,C
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
353 rrcf ADRESH,F ; /2
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
354 rrcf ADRESL,W
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
355 addwf sensor1_mv+0,F ; add to sensor1_mv:2
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
356 movf ADRESH,W
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
357 addwfc sensor1_mv+1,F
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
358 bcf STATUS,C
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
359 rrcf sensor1_mv+1,F ; divide /2
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
360 rrcf sensor1_mv+0,F
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
361 movlw HIGH ignore_mv
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
362 cpfsgt sensor1_mv+1 ; > ignore_mv ?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
363 bra get_analog_inputs1a ; NO
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
364 CLRI sensor1_mv ; YES - ignore this reading
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
365 get_analog_inputs1a: ; ignore 1.9 mV noise for not-connected inputs
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
366 tstfsz sensor1_mv+1 ; > 25.5 mV ?
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
367 bra get_analog_inputs2 ; YES - skip here
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
368 movlw .19
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
369 cpfsgt sensor1_mv+0 ; > 1.9 mV ?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
370 clrf sensor1_mv+0 ; NO - clear result
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
371
187
669b5d00706d CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents: 158
diff changeset
372 get_analog_inputs2:
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
373 ; Sensor 2
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
374 movlw b'00100101' ; power on ADC, select AN9
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
375 rcall wait_adc
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
376 bcf STATUS,C
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
377 rrcf ADRESH,F ; /2
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
378 rrcf ADRESL,W
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
379 addwf sensor2_mv+0,F ; add to sensor2_mv:2
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
380 movf ADRESH,W
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
381 addwfc sensor2_mv+1,F
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
382 bcf STATUS,C
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
383 rrcf sensor2_mv+1,F ; divide /2
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
384 rrcf sensor2_mv+0,F
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
385 movlw HIGH ignore_mv
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
386 cpfsgt sensor2_mv+1 ; > ignore_mv ?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
387 bra get_analog_inputs2a ; NO
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
388 CLRI sensor2_mv ; YES - ignore this reading
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
389 get_analog_inputs2a: ; ignore 1.9 mV noise for not-connected inputs
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
390 tstfsz sensor2_mv+1 ; > 25.5 mV ?
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
391 bra get_analog_inputs3 ; YES - skip here
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
392 movlw .19
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
393 cpfsgt sensor2_mv+0 ; > 1.9 mV ?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
394 clrf sensor2_mv+0 ; NO - clear result
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
395
187
669b5d00706d CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents: 158
diff changeset
396 get_analog_inputs3:
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
397 ; Sensor 3
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
398 movlw b'00101001' ; power on ADC, select AN10
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
399 rcall wait_adc
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
400 bcf STATUS,C
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
401 rrcf ADRESH,F ; /2
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
402 rrcf ADRESL,W
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
403 addwf sensor3_mv+0,F ; add to sensor3_mv:2
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
404 movf ADRESH,W
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
405 addwfc sensor3_mv+1,F
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
406 bcf STATUS,C
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
407 rrcf sensor3_mv+1,F ; divide /2
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
408 rrcf sensor3_mv+0,F
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
409 movlw HIGH ignore_mv
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
410 cpfsgt sensor3_mv+1 ; > ignore_mv ?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
411 bra get_analog_inputs3a ; NO
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
412 CLRI sensor3_mv ; YES - ignore this reading
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
413 get_analog_inputs3a: ; ignore 1.9 mV noise for not-connected inputs
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
414 tstfsz sensor3_mv+1 ; > 25.5 mV ?
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
415 bra get_analog_inputs4 ; YES - skip here
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
416 movlw .19
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
417 cpfsgt sensor3_mv+0 ; > 1.9 mV ?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
418 clrf sensor3_mv+0 ; NO - clear result
187
669b5d00706d CHANGE: Longer timeout (4 min) for calibration menu
heinrichsweikamp
parents: 158
diff changeset
419 get_analog_inputs4:
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
420 banksel common ; back to bank common
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
421 bcf ADCON0,0 ; power off ADC
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
422 bcf adc_is_running ; done with ADC
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
423 return
0
heinrichsweikamp
parents:
diff changeset
424
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
425 ENDIF ; _external_sensor
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
426
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
427 ;=============================================================================
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
428
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
429 global piezo_config ; called from outside ISR only
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
430 piezo_config: ; set up sensitivity of heinrichsweikamp piezo buttons (~30ms)
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
431 clrf TMR5H ; clear TMR5H first
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
432 clrf TMR5L ; clear TMR5L thereafter
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
433 bcf PIR5,TMR5IF ; clear timer 5 overrun flag, will take ~ 2 seconds to overrun after reset
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
434 bcf switch_right ; clear left-over button events
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
435 bcf switch_left ; ...
416
4389fe9673b2 NEW: Support for new logbook memory chip (minor hardware change)
heinrichsweikamp
parents: 357
diff changeset
436 piezo_config0:
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
437 btfsc switch_right ; user still pressing the right button?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
438 bra piezo_config ; YES - loop to wait for release
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
439 btfsc switch_left ; user still pressing the left button?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
440 bra piezo_config ; YES - loop to wait for release
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
441
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
442 btfss PIR5,TMR5IF ; timeout?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
443 bra piezo_config0 ; NO - not yet, loop
468
heinrichsweikamp
parents: 466
diff changeset
444
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
445 bcf INTCON,GIE
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
446
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
447 movff opt_cR_button_right,WREG ; right button
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
448 btfsc flip_screen ; 180° rotation ?
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
449 movff opt_cR_button_left,WREG ; YES - left button
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
450 rcall piezo_config_tx
343
34e2f5bdc5a2 BUGFIX: Rotate button sensitivity with screen (OSTC 2 and cR)
heinrichsweikamp
parents: 275
diff changeset
451
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
452 movff opt_cR_button_left,WREG ; left button
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
453 btfsc flip_screen ; 180° rotation ?
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
454 movff opt_cR_button_right,WREG ; YES - right button
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
455 rcall piezo_config_tx
343
34e2f5bdc5a2 BUGFIX: Rotate button sensitivity with screen (OSTC 2 and cR)
heinrichsweikamp
parents: 275
diff changeset
456
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
457 movlw .20 ; reserved
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
458 rcall piezo_config_tx
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
459 movlw .20 ; reserved
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
460 rcall piezo_config_tx
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
461
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
462 bsf INTCON,GIE
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
463 return
204
heinrichsweikamp
parents: 203
diff changeset
464
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
465 piezo_config_tx: ; send one byte
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
466 movwf lo ; store byte
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
467 movlw .8
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
468 movwf hi ; bit counter
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
469 bcf TX3_PIEZO_CFG ; start bit
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
470 rcall piezo_config_wait_bit
113
heinrichsweikamp
parents: 44
diff changeset
471 piezo_config_tx_loop:
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
472 btfss lo,0 ; LSB first
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
473 bcf TX3_PIEZO_CFG
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
474 btfsc lo,0 ; LSB first
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
475 bsf TX3_PIEZO_CFG
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
476 rcall piezo_config_wait_bit
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
477 rrncf lo,F
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
478 decfsz hi,F
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
479 bra piezo_config_tx_loop
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
480 bsf TX3_PIEZO_CFG ; stop bit
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
481 rcall piezo_config_wait_bit
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
482 return
113
heinrichsweikamp
parents: 44
diff changeset
483
heinrichsweikamp
parents: 44
diff changeset
484 piezo_config_wait_bit:
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
485 setf TMR5H ; set TMR5H first (to 255)
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
486 movlw .255-.26 ; 26 x 31.5 µs = 819 us
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
487 movwf TMR5L ; set TMR5L thereafter
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
488 bcf PIR5,TMR5IF ; clear timer 5 overrun flag
113
heinrichsweikamp
parents: 44
diff changeset
489 piezo_config_wait_bit3:
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
490 btfss PIR5,TMR5IF ; timeout?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
491 bra piezo_config_wait_bit3 ; NO - loop
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
492 return ; YES - done
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
493
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
494
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
495 global get_analog_switches ; called from ISR and from sleep mode, returns in bank common
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
496 get_analog_switches: ; start ADC and wait until finished
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
497 banksel HW_variants ; switch to bank where OSTC model variant is stored
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
498 btfsc analog_switches ; does the OSTC have analog switches?
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
499 bra get_analog_switches_1 ; YES
629
237931377539 3.07 stable release
heinrichsweikamp
parents: 628
diff changeset
500 get_analog_switches0:
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
501 banksel common ; NO - back to bank common
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
502 bcf analog_sw1_pressed ; - clear flag for analog switch 1
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
503 bcf analog_sw2_pressed ; - clear flag for analog switch 2
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
504 return ; - done
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
505
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
506 get_analog_switches_1:
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
507 banksel common ; back to bank common
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
508 btfsc adc_is_running ; ADC in use?
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
509 return ; YES - abort
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
510 btfsc cc_active ; NO - charging?
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 629
diff changeset
511 bra get_analog_switches0 ; YES - abort (and clear both flags)
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 623
diff changeset
512 ;bra get_analog_switches_2 ; NO - read switches
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
513
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
514 get_analog_switches_2:
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 629
diff changeset
515 bsf adc_is_running
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 629
diff changeset
516 bcf ADCON2,ADFM ; left justified
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
517 clrf ADCON1
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
518 movlw b'00100101' ; power on ADC, select AN9
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
519 rcall wait_adc
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
520
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
521 banksel isr_backup ; select bank ISR data
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
522 movff ADRESH,WREG
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
523 addwf analog_sw2_raw+0
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
524 movlw .0
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
525 addwfc analog_sw2_raw+1
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
526 decfsz analog_counter,F ; continue averaging?
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
527 bra get_analog_switches_2a ; YES
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
528 bcf STATUS,C ; NO - done, compute average
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
529 rrcf analog_sw2_raw+1
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
530 rrcf analog_sw2_raw+0 ; /2
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
531 bcf STATUS,C
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
532 rrcf analog_sw2_raw+1
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
533 rrcf analog_sw2_raw+0 ; /4
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
534 bcf STATUS,C
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
535 rrcf analog_sw2_raw+1
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
536 rrcf analog_sw2_raw+0 ; /8
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
537 bcf STATUS,C
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
538 rrcf analog_sw2_raw+1
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
539 rrcf analog_sw2_raw+0 ; /16
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
540 movff analog_sw2_raw+0,analog_sw2
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
541 clrf analog_sw2_raw+1
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
542 clrf analog_sw2_raw+0 ; reset average registers
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
543 ; movlw .16
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
544 ; movwf analog_counter ; only once...
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
545
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
546 get_analog_switches_2a:
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
547 bcf analog_sw2_pressed ; default to not pressed
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
548 movff opt_cR_button_left,WREG ; 20-100
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
549 bcf STATUS,C
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
550 rrcf WREG ; /2 -> 10-50
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
551 bcf STATUS,C
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
552 rrcf WREG ; /2 -> 5-25
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
553 decf WREG,W ; -1
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
554 decf WREG,W ; -1
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
555 decf WREG,W ; -1 -> 2-22
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
556 btfss button_polarity,1 ; (1= normal, 0=inverted)
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
557 bra get_analog_switches_sw2_inv
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
558 addwf analog_sw2,W ; average (~128)
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
559 cpfsgt ADRESH ; pressed?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
560 bra get_analog_switches_3 ; NO
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
561 bra get_analog_switches_sw2_pressed ; YES (left button normal)
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
562
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
563 get_analog_switches_sw2_inv:
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
564 subwf analog_sw2,W ; average (~128)
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
565 cpfslt ADRESH ; pressed?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
566 bra get_analog_switches_3 ; NO
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
567 ;bra get_analog_switches_sw2_pressed ; YES (left button inverted)
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
568
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
569 get_analog_switches_sw2_pressed:
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
570 bsf analog_sw2_pressed ; set left button as pressed
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
571
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
572 get_analog_switches_3:
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
573 movlw b'00101001' ; power on ADC, select AN10
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
574 rcall wait_adc
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
575 movff ADRESH,WREG
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
576 addwf analog_sw1_raw+0
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
577 movlw .0
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
578 addwfc analog_sw1_raw+1
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
579 tstfsz analog_counter ; continue averaging?
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
580 bra get_analog_switches_3a ; YES
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
581 bcf STATUS,C ; NO - done, compute average
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
582 rrcf analog_sw1_raw+1
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
583 rrcf analog_sw1_raw+0 ; /2
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
584 bcf STATUS,C
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
585 rrcf analog_sw1_raw+1
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
586 rrcf analog_sw1_raw+0 ; /4
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
587 bcf STATUS,C
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
588 rrcf analog_sw1_raw+1
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
589 rrcf analog_sw1_raw+0 ; /8
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
590 bcf STATUS,C
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
591 rrcf analog_sw1_raw+1
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
592 rrcf analog_sw1_raw+0 ; /16
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
593 movff analog_sw1_raw+0,analog_sw1
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
594 clrf analog_sw1_raw+1
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
595 clrf analog_sw1_raw+0 ; reset average registers
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
596 movlw .16
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
597 movwf analog_counter ; only once...
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
598
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
599 get_analog_switches_3a:
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
600 bcf analog_sw1_pressed ; default to not pressed
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
601 movff opt_cR_button_right,WREG ; 20-100
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
602 bcf STATUS,C
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
603 rrcf WREG ; /2 -> 10-50
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
604 bcf STATUS,C
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
605 rrcf WREG ; /2 -> 5-25
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
606 decf WREG,W ; -1
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
607 decf WREG,W ; -1
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
608 decf WREG,W ; -1 -> 2-22
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 603
diff changeset
609 btfss button_polarity,0 ; (1= normal, 0=inverted)
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
610 bra get_analog_switches_sw1_inv
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
611 addwf analog_sw1,W ; average (~128)
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
612 cpfsgt ADRESH ; pressed?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
613 bra get_analog_switches_4 ; NO
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
614 bra get_analog_switches_sw1_pressed ; YES (right button normal)
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
615
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
616 get_analog_switches_sw1_inv:
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
617 subwf analog_sw1,W ; average (~128)
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
618 cpfslt ADRESH ; pressed?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
619 bra get_analog_switches_4 ; NO
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
620 ;bra get_analog_switches_sw1_pressed ; YES (right button inverted)
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
621
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
622 get_analog_switches_sw1_pressed:
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
623 bsf analog_sw1_pressed ; set right button as pressed
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
624
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
625 get_analog_switches_4:
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 629
diff changeset
626 bsf ADCON2,ADFM ; restore to right justified
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 629
diff changeset
627 bcf adc_is_running
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
628 banksel common ; back to bank common
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
629 btfsc analog_sw1_pressed ; right button pressed?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
630 return ; YES - done
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
631 btfsc analog_sw2_pressed ; left button pressed?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
632 return ; YES - done
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 615
diff changeset
633 setf TMR1H ; NO to both - no button pressed, set timer1 to overflow quickly
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 475
diff changeset
634 return
448
aadfe9f2edaf work on new battery options
heinrichsweikamp
parents: 416
diff changeset
635
0
heinrichsweikamp
parents:
diff changeset
636 END