annotate src/adc_lightsensor.asm @ 634:4050675965ea

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