annotate src/adc_lightsensor.asm @ 627:bf5fee575701

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