comparison src/adc_lightsensor.asm @ 582:b455b31ce022

work on 2.97 stable
author heinrichsweikamp
date Mon, 26 Feb 2018 16:40:28 +0100
parents 237afdfb4918
children 00b24fb4324d
comparison
equal deleted inserted replaced
581:f5de1ff88814 582:b455b31ce022
1 ;============================================================================= 1 ;=============================================================================
2 ; 2 ;
3 ; File adc.asm 3 ; File adc.asm V2.98
4 ; 4 ;
5 ; 5 ;
6 ; Copyright (c) 2011, JD Gascuel, HeinrichsWeikamp, all right reserved. 6 ; Copyright (c) 2011, JD Gascuel, HeinrichsWeikamp, all right reserved.
7 ;============================================================================= 7 ;=============================================================================
8 ; HISTORY 8 ; HISTORY
12 #include "math.inc" 12 #include "math.inc"
13 #include "wait.inc" 13 #include "wait.inc"
14 #include "eeprom_rs232.inc" 14 #include "eeprom_rs232.inc"
15 #include "i2c.inc" 15 #include "i2c.inc"
16 16
17 sensors CODE 17 extern reset_battery_internal_only
18
19 sensors CODE
20
21 ;=============================================================================
18 22
19 wait_adc: 23 wait_adc:
20 movwf ADCON0 24 movwf ADCON0
21 nop 25 nop
22 bsf ADCON0,1 ; start ADC 26 bsf ADCON0,1 ; start ADC
23 wait_adc2: 27 wait_adc2:
24 btfsc ADCON0,1 ; Wait... 28 btfsc ADCON0,1 ; Wait...
25 bra wait_adc2 29 bra wait_adc2
26 return 30 return
27 31
28 global get_battery_voltage 32 global get_battery_voltage
29 get_battery_voltage: ; starts ADC and waits until fnished 33 get_battery_voltage: ; starts ADC and waits until finished
30 btfss battery_gauge_available 34 btfss battery_gauge_available
31 bra get_battery_voltage1 ; Normal ostc3 hardware 35 bra get_battery_voltage1 ; Normal ostc3 hardware
32 36
33 call lt2942_get_accumulated_charge 37 call lt2942_get_accumulated_charge
34 call lt2942_get_voltage 38 call lt2942_get_voltage
35 39
36 tstfsz batt_voltage+1 ; <256mV? 40 tstfsz batt_voltage+1 ; <256mV?
37 bra get_battery_voltage_noretry ; No 41 bra get_battery_voltage_noretry ; No
38 42
39 ; Retry 43 ; Retry
40 call lt2942_get_accumulated_charge 44 call lt2942_get_accumulated_charge
41 call lt2942_get_voltage 45 call lt2942_get_voltage
42 46
43 get_battery_voltage_noretry: 47 get_battery_voltage_noretry:
44 btfsc divemode 48 btfsc divemode
45 return ; Not in divemode 49 return ; Not in divemode
46 50
47 bcf cv_active 51 bcf cv_active
48 bcf cc_active 52 bcf cc_active
49 bcf LEDr 53 bcf LEDr
50 bcf TRISJ,2 ; Chrg-Out output 54 bcf TRISJ,2 ; Chrg-Out output
51 bsf CHRG_OUT 55 bsf CHRG_OUT
52 56
53 btfss CHRG_IN 57 btfss CHRG_IN
54 bra charge_cc_active 58 bra charge_cc_active
55 59
56 bcf CHRG_OUT 60 bcf CHRG_OUT
57 bsf TRISJ,2 ; Chrg-Out high impedance 61 bsf TRISJ,2 ; Chrg-Out high impedance
58 62
59 WAITMS d'1' 63 WAITMS d'1'
60 64
61 btfsc CHRG_IN 65 btfsc CHRG_IN
62 return 66 return
63 ;cv_active: 67 ;cv_active:
64 decfsz safety_stop_countdown,F 68 decfsz get_bat_volt_counter,F
65 return 69 return
66 movlw .15 70 movlw .15
67 cpfsgt batt_voltage+1 ; Batt Voltage >= 16*256mV (4,096V)? 71 cpfsgt batt_voltage+1 ; Batt Voltage >= 16*256mV (4,096V)?
68 bra charge_cc_active ; No 72 bra charge_cc_active ; No
69 bsf cc_active 73 bsf cc_active
70 bsf cv_active 74 bsf cv_active
71 bsf LEDr ; Indicate charging 75 bsf LEDr ; Indicate charging
72 call lt2942_charge_done ; Reset accumulating registers to 0xFFFF 76 call lt2942_charge_done ; Reset accumulating registers to 0xFFFF
73 WAITMS d'10' 77 WAITMS d'10'
74 bcf LEDr ; Indicate charging 78 bcf LEDr ; Indicate charging
75 bsf safety_stop_countdown,0 ; =1 79 bsf get_bat_volt_counter,0 ; =1
76 return 80 return
77 81
78 charge_cc_active: 82 charge_cc_active:
79 bsf cc_active 83 bsf cc_active
80 bsf LEDr ; Indicate charging 84 bsf LEDr ; Indicate charging
81 bcf CHRG_OUT 85 bcf CHRG_OUT
82 bsf TRISJ,2 ; Chrg-Out high impedance 86 bsf TRISJ,2 ; Chrg-Out high impedance
83 movlw .10 87 movlw .10
84 movwf safety_stop_countdown 88 movwf get_bat_volt_counter
85 return 89 return
86 90
87 get_battery_voltage1: 91 get_battery_voltage1:
88 bsf adc_running ; =1: The ADC is in use 92 bsf adc_running ; =1: The ADC is in use
89 movlw b'00100000' ; 2.048V Vref+ -> 1LSB = 500µV 93 movlw b'00100000' ; 2.048V Vref+ -> 1LSB = 500µV
90 movwf ADCON1 94 movwf ADCON1
91 movlw b'00011001' ; power on ADC, select AN6 95 movlw b'00011001' ; power on ADC, select AN6
92 rcall wait_adc 96 rcall wait_adc
93 97
94 movff ADRESH,batt_voltage+1 ; store value 98 movff ADRESH,batt_voltage+1 ; store value
95 movff ADRESL,batt_voltage+0 ; store value 99 movff ADRESL,batt_voltage+0 ; store value
96 bcf ADCON0,0 ; power off ADC 100 bcf ADCON0,0 ; power off ADC
97 101
98 ; Multiply with 2,006 to be excact here... 102 ; Multiply with 2,006 to be exact here...
99 ; bcf STATUS,C 103 ; bcf STATUS,C
100 ; rlcf xA+0,F 104 ; rlcf xA+0,F
101 ; 105 ;
102 ; rlcf xA+1,F ; x2 106 ; rlcf xA+1,F ; x2
103 107
104 ; movff xA+0,batt_voltage+0 ; store value 108 ; movff xA+0,batt_voltage+0 ; store value
105 ; movff xA+1,batt_voltage+1 109 ; movff xA+1,batt_voltage+1
106 110
107 movlw LOW lithium_36v_low 111 movlw LOW lithium_36v_low
108 movwf sub_a+0 112 movwf sub_a+0
109 movlw HIGH lithium_36v_low 113 movlw HIGH lithium_36v_low
110 movwf sub_a+1 114 movwf sub_a+1
111 movff batt_voltage+0,sub_b+0 115 movff batt_voltage+0,sub_b+0
112 movff batt_voltage+1,sub_b+1 116 movff batt_voltage+1,sub_b+1
113 call subU16 ; sub_c = sub_a - sub_b 117 call subU16 ; sub_c = sub_a - sub_b
114 ; Battery is 3,6V (>lithium_36v_low?) 118 ; Battery is 3,6V (>lithium_36v_low?)
115 btfss neg_flag 119 btfss neg_flag
116 bra get_battery_voltage4 ; No, use 1,5V 120 bra get_battery_voltage4 ; No, use 1,5V
117 121
118 bsf battery_is_36v ; Yes, set flag (Cleared in power-on reset only!) 122 bsf battery_is_36v ; Yes, set flag (Cleared in power-on reset only!)
119 123
120 ; Check if the battery is near-dead already 124 ; Check if the battery is near-dead already
121 movlw LOW lithium_36v_empty 125 movlw LOW lithium_36v_empty
122 movwf sub_a+0 126 movwf sub_a+0
123 movlw HIGH lithium_36v_empty 127 movlw HIGH lithium_36v_empty
124 movwf sub_a+1 128 movwf sub_a+1
125 call subU16 ; sub_c = sub_a - sub_b 129 call subU16 ; sub_c = sub_a - sub_b
126 ; Battery is not dead yet (>lithium_36v_empty?) 130 ; Battery is not dead yet (>lithium_36v_empty?)
127 btfsc neg_flag 131 btfsc neg_flag
128 bra get_battery_voltage2 ; Yes, battery is still ok 132 bra get_battery_voltage2 ; Yes, battery is still ok
129 133
130 ; Battery is probably dead very soon 134 ; Battery is probably dead very soon
131 ; Set ">=24Ah used" into battery gauge registers 135 ; Set ">=24Ah used" into battery gauge registers
132 movlw .128 136 movlw .128
133 movff WREG,battery_gauge+5 137 movff WREG,battery_gauge+5
134 138
135 get_battery_voltage2: 139 get_battery_voltage2:
136 ; Use 3,6V battery gauging mode 140 ; Use 3,6V battery gauging mode
137 movff battery_gauge+5,xC+3 141 movff battery_gauge+5,xC+3
138 movff battery_gauge+4,xC+2 142 movff battery_gauge+4,xC+2
139 movff battery_gauge+3,xC+1 143 movff battery_gauge+3,xC+1
140 movff battery_gauge+2,xC+0 144 movff battery_gauge+2,xC+0
141 ; battery_gauge:6 is nAs 145 ; battery_gauge:6 is nAs
142 ; devide through 65536 146 ; devide through 65536
143 ; devide through battery_capacity:2 147 ; devide through battery_capacity:2
144 ; Result is in percent 148 ; Result is in percent
145 movff internal_battery_capacity+0,xB+0 149 movff internal_battery_capacity+0,xB+0
146 movff internal_battery_capacity+1,xB+1 150 movff internal_battery_capacity+1,xB+1
147 call div32x16 ; xC:4 / xB:2 = xC+3:xC+2 with xC+1:xC+0 as remainder 151 call div32x16 ; xC:4 / xB:2 = xC+3:xC+2 with xC+1:xC+0 as remainder
148 movff xC+0,lo 152 movff xC+0,lo
149 ; Limit to 100 153 ; Limit to 100
150 movlw .100 154 movlw .100
151 cpfslt lo 155 cpfslt lo
152 movwf lo 156 movwf lo
153 ; lo will be between 0 (Full) and 100 (empty) 157 ; lo will be between 0 (Full) and 100 (empty)
154 movf lo,W 158 movf lo,W
155 sublw .100 159 sublw .100
156 movwf lo 160 movwf lo
157 get_battery_voltage3: 161 get_battery_voltage3:
158 movlw .100 162 movlw .100
159 cpfslt lo 163 cpfslt lo
160 movwf lo 164 movwf lo
161 ; lo will be between 100 (Full) and 0 (empty) 165 ; lo will be between 100 (Full) and 0 (empty)
162 166
163 ; use 3,6V battery sensing based on 50mA load 167 ; use 3,6V battery sensing based on 50mA load
164 ; 75% 168 ; 75%
165 movff batt_voltage+0,sub_b+0 169 movff batt_voltage+0,sub_b+0
166 movff batt_voltage+1,sub_b+1 170 movff batt_voltage+1,sub_b+1
167 movlw LOW lithium_36v_75 171 movlw LOW lithium_36v_75
168 movwf sub_a+0 172 movwf sub_a+0
169 movlw HIGH lithium_36v_75 173 movlw HIGH lithium_36v_75
170 movwf sub_a+1 174 movwf sub_a+1
171 call subU16 ; sub_c = sub_a - sub_b 175 call subU16 ; sub_c = sub_a - sub_b
172 btfsc neg_flag 176 btfsc neg_flag
173 bra get_battery_voltage3a 177 bra get_battery_voltage3a
174 movlw .75 178 movlw .75
175 movwf lo 179 movwf lo
176 get_battery_voltage3a: 180 get_battery_voltage3a:
177 ; 50% 181 ; 50%
178 movlw LOW lithium_36v_50 182 movlw LOW lithium_36v_50
179 movwf sub_a+0 183 movwf sub_a+0
180 movlw HIGH lithium_36v_50 184 movlw HIGH lithium_36v_50
181 movwf sub_a+1 185 movwf sub_a+1
182 call subU16 ; sub_c = sub_a - sub_b 186 call subU16 ; sub_c = sub_a - sub_b
183 btfsc neg_flag 187 btfsc neg_flag
184 bra get_battery_voltage3b 188 bra get_battery_voltage3b
185 movlw .50 189 movlw .50
186 movwf lo 190 movwf lo
187 get_battery_voltage3b: 191 get_battery_voltage3b:
188 ; 25% 192 ; 25%
189 movlw LOW lithium_36v_25 193 movlw LOW lithium_36v_25
190 movwf sub_a+0 194 movwf sub_a+0
191 movlw HIGH lithium_36v_25 195 movlw HIGH lithium_36v_25
192 movwf sub_a+1 196 movwf sub_a+1
193 call subU16 ; sub_c = sub_a - sub_b 197 call subU16 ; sub_c = sub_a - sub_b
194 btfsc neg_flag 198 btfsc neg_flag
195 bra get_battery_voltage3c 199 bra get_battery_voltage3c
196 movlw .25 200 movlw .25
197 movwf lo 201 movwf lo
198 get_battery_voltage3c: 202 get_battery_voltage3c:
199 ; 10% 203 ; 10%
200 movlw LOW lithium_36v_10 204 movlw LOW lithium_36v_10
201 movwf sub_a+0 205 movwf sub_a+0
202 movlw HIGH lithium_36v_10 206 movlw HIGH lithium_36v_10
203 movwf sub_a+1 207 movwf sub_a+1
204 call subU16 ; sub_c = sub_a - sub_b 208 call subU16 ; sub_c = sub_a - sub_b
205 btfsc neg_flag 209 btfsc neg_flag
206 bra get_battery_voltage3d 210 bra get_battery_voltage3d
207 movlw .10 211 movlw .10
208 movwf lo 212 movwf lo
209 get_battery_voltage3d: 213 get_battery_voltage3d:
210 movlw .100 214 movlw .100
211 cpfslt lo 215 cpfslt lo
212 movwf lo 216 movwf lo
213 ; lo will be between 100 (Full) and 0 (empty) 217 ; lo will be between 100 (Full) and 0 (empty)
214 movf batt_percent,W 218 movf batt_percent,W
215 cpfsgt lo ; keep batt_percent on the lowest value found 219 cpfsgt lo ; keep batt_percent on the lowest value found
216 movff lo,batt_percent ; store value 220 movff lo,batt_percent ; store value
217 btfsc battery_is_36v ; but always use computed value for 3,6V battery 221 btfsc battery_is_36v ; but always use computed value for 3,6V battery
218 movff lo,batt_percent ; store value 222 movff lo,batt_percent ; store value
219 bcf adc_running ; =1: The ADC is in use 223 bcf adc_running ; =1: The ADC is in use
220 return 224 return
221 225
222 get_battery_voltage4: 226 get_battery_voltage4:
223 ; Use 1,5V battery voltage mode 227 ; Use 1,5V battery voltage mode
224 ; Use approximation (batt_voltage:2-aa_15v_low)/4 = lo 228 ; Use approximation (batt_voltage:2-aa_15v_low)/4 = lo
225 movff batt_voltage+0,sub_a+0 229 movff batt_voltage+0,sub_a+0
226 movff batt_voltage+1,sub_a+1 230 movff batt_voltage+1,sub_a+1
227 movlw LOW aa_15v_low 231 movlw LOW aa_15v_low
228 movwf sub_b+0 232 movwf sub_b+0
229 movlw HIGH aa_15v_low 233 movlw HIGH aa_15v_low
230 movwf sub_b+1 234 movwf sub_b+1
231 call subU16 ; sub_c = sub_a - sub_b 235 call subU16 ; sub_c = sub_a - sub_b
232 bcf STATUS,C 236 bcf STATUS,C
233 rrcf sub_c+1 237 rrcf sub_c+1
234 rrcf sub_c+0 ; /2 238 rrcf sub_c+0 ; /2
235 bcf STATUS,C 239 bcf STATUS,C
236 rrcf sub_c+1 240 rrcf sub_c+1
237 rrcf sub_c+0 ; /4 241 rrcf sub_c+0 ; /4
238 movff sub_c+0,lo 242 movff sub_c+0,lo
239 bra get_battery_voltage3d ; Check limits and return 243 bra get_battery_voltage3d ; Check limits and return
240 244
241 global get_ambient_level 245 global get_ambient_level
242 get_ambient_level: ; starts ADC and waits until finished 246 get_ambient_level: ; starts ADC and waits until finished
243 btfsc adc_running ; ADC in use? 247 btfsc adc_running ; ADC in use?
244 return ; Yes, return 248 return ; Yes, return
245 249
246 btfsc ambient_sensor 250 btfsc ambient_sensor
247 bra get_ambient_level1 ; Normal ostc3 hardware 251 bra get_ambient_level1 ; Normal ostc3 hardware
248 252
249 banksel isr_backup ; Back to Bank0 ISR data 253 banksel isr_backup ; Back to Bank0 ISR data
250 movff opt_brightness,isr1_temp 254 movff opt_brightness,isr1_temp
251 incf isr1_temp,F ; adjust 0-2 to 1-3 255 incf isr1_temp,F ; adjust 0-2 to 1-3
252 movlw ambient_light_max_high_cr; cR and 2 hardware brightest setting 256 movlw ambient_light_max_high_cr ; cR and 2 hardware brightest setting
253 dcfsnz isr1_temp,F 257 dcfsnz isr1_temp,F
254 movlw ambient_light_max_eco ; brightest setting 258 movlw ambient_light_max_eco ; brightest setting
255 dcfsnz isr1_temp,F 259 dcfsnz isr1_temp,F
256 movlw ambient_light_max_medium; brightest setting 260 movlw ambient_light_max_medium ; brightest setting
257 261
258 movff WREG,ambient_light+0 ; Set to max. 262 movff WREG,ambient_light+0 ; Set to max.
259 movff ambient_light+0,max_CCPR1L ; Store value for dimming in TMR7 interrupt 263 movff ambient_light+0,max_CCPR1L ; Store value for dimming in TMR7 interrupt
260 return 264 return
261 265
262 get_ambient_level1: 266 get_ambient_level1:
263 movlw b'00000000' ; Vref+ = Vdd 267 movlw b'00000000' ; Vref+ = Vdd
264 movwf ADCON1 268 movwf ADCON1
265 movlw b'00011101' ; power on ADC, select AN7 269 movlw b'00011101' ; power on ADC, select AN7
266 rcall wait_adc 270 rcall wait_adc
267 271
268 movff ADRESH,ambient_light+1 272 movff ADRESH,ambient_light+1
269 movff ADRESL,ambient_light+0 273 movff ADRESL,ambient_light+0
270 bcf ADCON0,0 ; power off ADC 274 bcf ADCON0,0 ; power off ADC
271 275
272 ; ambient_light:2 is between 4096 (direct sunlight) and about 200 (darkness) 276 ; ambient_light:2 is between 4096 (direct sunlight) and about 200 (darkness)
273 ; First: Devide through 16 277 ; First: Divide by 16
278 banksel ambient_light
274 bcf STATUS,C 279 bcf STATUS,C
275 rrcf ambient_light+1 280 rrcf ambient_light+1
276 rrcf ambient_light+0 281 rrcf ambient_light+0
277 bcf STATUS,C 282 bcf STATUS,C
278 rrcf ambient_light+1 283 rrcf ambient_light+1
284 rrcf ambient_light+1 289 rrcf ambient_light+1
285 rrcf ambient_light+0 290 rrcf ambient_light+0
286 ; Result: ambient_light:2/16 291 ; Result: ambient_light:2/16
287 ; Now, make sure to have value between ambient_light_low and ambient_light_max 292 ; Now, make sure to have value between ambient_light_low and ambient_light_max
288 293
289 movlw .254 294 movlw .254
290 tstfsz ambient_light+1 ; >255? 295 tstfsz ambient_light+1 ; >255?
291 movwf ambient_light+0 ; avoid ADC clipping 296 movwf ambient_light+0 ; avoid ADC clipping
292 297
293 incfsz ambient_light+0,W ; =255? 298 incfsz ambient_light+0,W ; =255?
294 bra get_ambient_level2 ; No, continue 299 bra get_ambient_level2 ; No, continue
295 300
296 movlw .254 301 movlw .254
297 movwf ambient_light+0 ; avoid ADC clipping 302 movwf ambient_light+0 ; avoid ADC clipping
298 303
299 get_ambient_level2: 304 get_ambient_level2:
300 ; movlw .10 305 ; movlw .10
301 ; subwf ambient_light+0,F ; Subtract 10 (ADC Offset) 306 ; subwf ambient_light+0,F ; Subtract 10 (ADC Offset)
302 ; btfsc STATUS,N 307 ; btfsc STATUS,N
303 ; movwf ambient_light+0 ; avoid clipping 308 ; movwf ambient_light+0 ; avoid clipping
304 309
305 banksel isr_backup ; Back to Bank0 ISR data 310 banksel isr_backup ; Back to Bank0 ISR data
306 movff opt_brightness,isr1_temp 311 movff opt_brightness,isr1_temp
307 312
308 btfsc RCSTA1,7 ; UART module on? 313 btfsc RCSTA1,7 ; UART module on?
309 clrf isr1_temp ; Yes, set temporally to eco mode 314 clrf isr1_temp ; Yes, set temporally to eco mode
310 315
311 incf isr1_temp,F ; adjust 0-2 to 1-3 316 incf isr1_temp,F ; adjust 0-2 to 1-3
312 317
313 banksel common ; flag is in bank1 318 banksel common ; flag is in bank1
314 movlw ambient_light_max_high_cr; cR and 2 hardware brightest setting 319 movlw ambient_light_max_high_cr ; cR and 2 hardware brightest setting
315 btfss battery_gauge_available 320 btfss battery_gauge_available
316 movlw ambient_light_max_high_15V; 1,5V battery brightest setting 321 movlw ambient_light_max_high_15V ; 1,5V battery brightest setting
317 btfsc battery_is_36v ; 3,6V battery in use? 322 btfsc battery_is_36v ; 3,6V battery in use?
318 movlw ambient_light_max_high_36V ; 3,6V battery brightest setting 323 movlw ambient_light_max_high_36V ; 3,6V battery brightest setting
319 banksel isr_backup ; Back to Bank0 ISR data 324 banksel isr_backup ; Back to Bank0 ISR data
320 325
321 dcfsnz isr1_temp,F 326 dcfsnz isr1_temp,F
322 movlw ambient_light_max_eco ; brightest setting 327 movlw ambient_light_max_eco ; brightest setting
323 dcfsnz isr1_temp,F 328 dcfsnz isr1_temp,F
324 movlw ambient_light_max_medium; brightest setting 329 movlw ambient_light_max_medium ; brightest setting
325 330
326 banksel common ; ambient_light is in Bank1 331 banksel ambient_light
327 incf ambient_light+0,F ; +1 332 incf ambient_light+0,F ; +1
328 cpfslt ambient_light+0 ; smaller then WREG? 333 cpfslt ambient_light+0 ; smaller then WREG?
329 movwf ambient_light+0 ; No, set to max. 334 movwf ambient_light+0 ; No, set to max.
330 335
331 banksel isr_backup ; Back to Bank0 ISR data 336 banksel isr_backup ; Back to Bank0 ISR data
332 movff opt_brightness,isr1_temp 337 movff opt_brightness,isr1_temp
333 incf isr1_temp,F ; adjust 0-2 to 1-3 338 incf isr1_temp,F ; adjust 0-2 to 1-3
334 movlw ambient_light_min_high ; darkest setting 339 movlw ambient_light_min_high ; darkest setting
335 340
336 dcfsnz isr1_temp,F 341 dcfsnz isr1_temp,F
337 movlw ambient_light_min_eco ; darkest setting 342 movlw ambient_light_min_eco ; darkest setting
338 dcfsnz isr1_temp,F 343 dcfsnz isr1_temp,F
339 movlw ambient_light_min_medium; darkest setting 344 movlw ambient_light_min_medium ; darkest setting
340 dcfsnz isr1_temp,F 345 dcfsnz isr1_temp,F
341 movlw ambient_light_min_high ; darkest setting 346 movlw ambient_light_min_high ; darkest setting
342 347
343 banksel common ; ambient_light is in Bank1 348 banksel ambient_light
344 cpfsgt ambient_light+0 ; bigger then WREG? 349 cpfsgt ambient_light+0 ; bigger then WREG?
345 movwf ambient_light+0 ; No, set to min 350 movwf ambient_light+0 ; No, set to min
346 351 banksel common
347 movff ambient_light+0,max_CCPR1L ; Store value for dimming in TMR7 interrupt 352
353 movff ambient_light+0,max_CCPR1L ; Store value for dimming in TMR7 interrupt
348 return 354 return
349 355
350 global get_analog_inputs 356 global get_analog_inputs
351 get_analog_inputs: ; starts ADC and waits until finished 357 get_analog_inputs: ; starts ADC and waits until finished
352 bsf adc_running ; =1: The ADC is in use 358 bsf adc_running ; =1: The ADC is in use
353 btfsc TFT_PWM 359 btfsc TFT_PWM
354 bra get_analog_inputs ; Wait for PWM low 360 bra get_analog_inputs ; Wait for PWM low
355 movlw b'00100000' ; 2.048V Vref+ -> 1LSB = 500µV 361 movlw b'00100000' ; 2.048V Vref+ -> 1LSB = 500µV
356 movwf ADCON1 362 movwf ADCON1
357 movlw b'00100001' ; power on ADC, select AN8 363 movlw b'00100001' ; power on ADC, select AN8
358 rcall wait_adc 364 rcall wait_adc
359 bcf STATUS,C 365 bcf STATUS,C
360 rrcf ADRESH,F ; /2 366 rrcf ADRESH,F ; /2
361 rrcf ADRESL,W 367 rrcf ADRESL,W
362 ; add to o2_mv_sensor1:2 368 ; add to o2_mv_sensor1:2
363 addwf o2_mv_sensor1+0,F 369 addwf o2_mv_sensor1+0,F
364 movf ADRESH,W 370 movf ADRESH,W
365 addwfc o2_mv_sensor1+1,F 371 addwfc o2_mv_sensor1+1,F
366 ; Devide by 2 372 ; Devide by 2
367 bcf STATUS,C 373 bcf STATUS,C
368 rrcf o2_mv_sensor1+1,F ; /2 374 rrcf o2_mv_sensor1+1,F ; /2
369 rrcf o2_mv_sensor1+0,F 375 rrcf o2_mv_sensor1+0,F
370 376
371 movlw HIGH ignore_mv 377 movlw HIGH ignore_mv
372 cpfsgt o2_mv_sensor1+1 ; >ignore_mv? 378 cpfsgt o2_mv_sensor1+1 ; >ignore_mv?
373 bra get_analog_inputs2a ; No 379 bra get_analog_inputs2a ; No
374 ; Yes, ignore this reading 380 ; Yes, ignore this reading
375 clrf o2_mv_sensor1+1 381 clrf o2_mv_sensor1+1
376 clrf o2_mv_sensor1+0 382 clrf o2_mv_sensor1+0
377 get_analog_inputs2a: 383 get_analog_inputs2a:
378 ; Ignore 1,9mV noise for not-connected inputs 384 ; Ignore 1,9mV noise for not-connected inputs
379 tstfsz o2_mv_sensor1+1 ; >25,5mV? 385 tstfsz o2_mv_sensor1+1 ; >25,5mV?
380 bra get_analog_inputs2 ; Yes, skip here 386 bra get_analog_inputs2 ; Yes, skip here
381 movlw .19 387 movlw .19
382 cpfsgt o2_mv_sensor1+0 ; >1,9mV? 388 cpfsgt o2_mv_sensor1+0 ; >1,9mV?
383 clrf o2_mv_sensor1+0 ; no, clear result 389 clrf o2_mv_sensor1+0 ; no, clear result
384 get_analog_inputs2: 390 get_analog_inputs2:
385 movlw b'00100101' ; power on ADC, select AN9 391 movlw b'00100101' ; power on ADC, select AN9
386 rcall wait_adc 392 rcall wait_adc
387 bcf STATUS,C 393 bcf STATUS,C
388 rrcf ADRESH,F ; /2 394 rrcf ADRESH,F ; /2
389 rrcf ADRESL,W 395 rrcf ADRESL,W
390 ; add to o2_mv_sensor2:2 396 ; add to o2_mv_sensor2:2
391 addwf o2_mv_sensor2+0,F 397 addwf o2_mv_sensor2+0,F
392 movf ADRESH,W 398 movf ADRESH,W
393 addwfc o2_mv_sensor2+1,F 399 addwfc o2_mv_sensor2+1,F
394 ; Devide by 2 400 ; Devide by 2
395 bcf STATUS,C 401 bcf STATUS,C
396 rrcf o2_mv_sensor2+1,F ; /2 402 rrcf o2_mv_sensor2+1,F ; /2
397 rrcf o2_mv_sensor2+0,F 403 rrcf o2_mv_sensor2+0,F
398 404
399 movlw HIGH ignore_mv 405 movlw HIGH ignore_mv
400 cpfsgt o2_mv_sensor2+1 ; >ignore_mv? 406 cpfsgt o2_mv_sensor2+1 ; >ignore_mv?
401 bra get_analog_inputs3a ; No 407 bra get_analog_inputs3a ; No
402 ; Yes, ignore this reading 408 ; Yes, ignore this reading
403 clrf o2_mv_sensor2+1 409 clrf o2_mv_sensor2+1
404 clrf o2_mv_sensor2+0 410 clrf o2_mv_sensor2+0
405 get_analog_inputs3a: 411 get_analog_inputs3a:
406 ; Ignore 1,9mV noise for not-connected inputs 412 ; Ignore 1,9mV noise for not-connected inputs
407 tstfsz o2_mv_sensor2+1 ; >25,5mV? 413 tstfsz o2_mv_sensor2+1 ; >25,5mV?
408 bra get_analog_inputs3 ; Yes, skip here 414 bra get_analog_inputs3 ; Yes, skip here
409 movlw .19 415 movlw .19
410 cpfsgt o2_mv_sensor2+0 ; >1,9mV? 416 cpfsgt o2_mv_sensor2+0 ; >1,9mV?
411 clrf o2_mv_sensor2+0 ; no, clear result 417 clrf o2_mv_sensor2+0 ; no, clear result
412 get_analog_inputs3: 418 get_analog_inputs3:
413 movlw b'00101001' ; power on ADC, select AN10 419 movlw b'00101001' ; power on ADC, select AN10
414 rcall wait_adc 420 rcall wait_adc
415 bcf STATUS,C 421 bcf STATUS,C
416 rrcf ADRESH,F ; /2 422 rrcf ADRESH,F ; /2
417 rrcf ADRESL,W 423 rrcf ADRESL,W
418 ; add to o2_mv_sensor3:2 424 ; add to o2_mv_sensor3:2
419 addwf o2_mv_sensor3+0,F 425 addwf o2_mv_sensor3+0,F
420 movf ADRESH,W 426 movf ADRESH,W
421 addwfc o2_mv_sensor3+1,F 427 addwfc o2_mv_sensor3+1,F
422 ; Devide by 2 428 ; Devide by 2
423 bcf STATUS,C 429 bcf STATUS,C
424 rrcf o2_mv_sensor3+1,F ; /2 430 rrcf o2_mv_sensor3+1,F ; /2
425 rrcf o2_mv_sensor3+0,F 431 rrcf o2_mv_sensor3+0,F
426 432
427 movlw HIGH ignore_mv 433 movlw HIGH ignore_mv
428 cpfsgt o2_mv_sensor3+1 ; >ignore_mv? 434 cpfsgt o2_mv_sensor3+1 ; >ignore_mv?
429 bra get_analog_inputs4a ; No 435 bra get_analog_inputs4a ; No
430 ; Yes, ignore this reading 436 ; Yes, ignore this reading
431 clrf o2_mv_sensor3+1 437 clrf o2_mv_sensor3+1
432 clrf o2_mv_sensor3+0 438 clrf o2_mv_sensor3+0
433 get_analog_inputs4a: 439 get_analog_inputs4a:
434 ; Ignore 1,9mV noise for not-connected inputs 440 ; Ignore 1,9mV noise for not-connected inputs
435 tstfsz o2_mv_sensor3+1 ; >25,5mV? 441 tstfsz o2_mv_sensor3+1 ; >25,5mV?
436 bra get_analog_inputs4 ; Yes, skip here 442 bra get_analog_inputs4 ; Yes, skip here
437 movlw .19 443 movlw .19
438 cpfsgt o2_mv_sensor3+0 ; >1,9mV? 444 cpfsgt o2_mv_sensor3+0 ; >1,9mV?
439 clrf o2_mv_sensor3+0 ; no, clear result 445 clrf o2_mv_sensor3+0 ; no, clear result
440 get_analog_inputs4: 446 get_analog_inputs4:
441 bcf ADCON0,0 ; power off ADC 447 bcf ADCON0,0 ; power off ADC
442 bcf adc_running ; =1: The ADC is in use 448 bcf adc_running ; =1: The ADC is in use
443 return 449 return
444 450
445 global piezo_config ; Sets up piezo sensitivity of heinrichs weikamp Piezo buttons (~30ms) 451 global piezo_config ; sets up piezo sensitivity of heinrichs weikamp piezo buttons (~30ms)
446 piezo_config: ; Settings between 20 and 200 452 piezo_config: ; Settings between 20 and 200
447 clrf TMR5H 453 clrf TMR5H
448 clrf TMR5L ; ~2sec 454 clrf TMR5L ; ~2sec
449 bcf PIR5,TMR5IF ; Clear flag 455 bcf PIR5,TMR5IF ; Clear flag
450 bcf switch_right 456 bcf switch_right
451 bcf switch_left 457 bcf switch_left
452 piezo_config0: 458 piezo_config0:
453 btfsc switch_right 459 btfsc switch_right
454 bra piezo_config 460 bra piezo_config
455 btfsc switch_left 461 btfsc switch_left
456 bra piezo_config ; Restart on button press 462 bra piezo_config ; Restart on button press
457 463
458 btfss PIR5,TMR5IF 464 btfss PIR5,TMR5IF
459 bra piezo_config0 ; Wait loop 465 bra piezo_config0 ; Wait loop
460 466
461 bcf INTCON,GIE 467 bcf INTCON,GIE
462 movff opt_cR_button_right,WREG; right button 468 movff opt_cR_button_right,WREG ; right button
463 btfsc flip_screen ; 180° rotation ? 469 btfsc flip_screen ; 180° rotation ?
464 movff opt_cR_button_left,WREG ; Yes, left button 470 movff opt_cR_button_left,WREG ; Yes, left button
465 rcall piezo_config_tx 471 rcall piezo_config_tx
466 472
467 movff opt_cR_button_left,WREG ; left button 473 movff opt_cR_button_left,WREG ; left button
468 btfsc flip_screen ; 180° rotation ? 474 btfsc flip_screen ; 180° rotation ?
469 movff opt_cR_button_right,WREG; Yes, right button 475 movff opt_cR_button_right,WREG ; Yes, right button
470 rcall piezo_config_tx 476 rcall piezo_config_tx
471 477
472 movlw .20 ; reserved 478 movlw .20 ; reserved
473 rcall piezo_config_tx 479 rcall piezo_config_tx
474 movlw .20 ; reserved 480 movlw .20 ; reserved
475 rcall piezo_config_tx 481 rcall piezo_config_tx
476 bsf INTCON,GIE 482 bsf INTCON,GIE
477 return 483 return
478 484
479 piezo_config_tx: ; Send one byte 485 piezo_config_tx: ; send one byte
480 movwf uart1_temp ; Store byte 486 movwf lo ; store byte
481 movlw .8 487 movlw .8
482 movwf uart2_temp ; Bit counter 488 movwf hi ; bit counter
483 bcf TX3_PIEZO_CFG ; Startbit 489 bcf TX3_PIEZO_CFG ; start bit
484 rcall piezo_config_wait_bit 490 rcall piezo_config_wait_bit
485 piezo_config_tx_loop: 491 piezo_config_tx_loop:
486 btfss uart1_temp,0 ; LSB first 492 btfss lo,0 ; LSB first
487 bcf TX3_PIEZO_CFG 493 bcf TX3_PIEZO_CFG
488 btfsc uart1_temp,0 ; LSB first 494 btfsc lo,0 ; LSB first
489 bsf TX3_PIEZO_CFG 495 bsf TX3_PIEZO_CFG
490 rcall piezo_config_wait_bit 496 rcall piezo_config_wait_bit
491 rrncf uart1_temp,F 497 rrncf lo,F
492 decfsz uart2_temp,F 498 decfsz hi,F
493 bra piezo_config_tx_loop 499 bra piezo_config_tx_loop
494 bsf TX3_PIEZO_CFG ; Stopbit 500 bsf TX3_PIEZO_CFG ; stop bit
495 rcall piezo_config_wait_bit 501 rcall piezo_config_wait_bit
496 return 502 return
497 503
498 piezo_config_wait_bit: 504 piezo_config_wait_bit:
499 setf TMR5H 505 setf TMR5H
500 movlw .255-.26 ;26 x 31,5µs = 819us 506 movlw .255-.26 ; 26 x 31,5µs = 819us
501 movwf TMR5L 507 movwf TMR5L
502 bcf PIR5,TMR5IF ; Clear flag 508 bcf PIR5,TMR5IF ; Clear flag
503 piezo_config_wait_bit3: 509 piezo_config_wait_bit3:
504 btfss PIR5,TMR5IF 510 btfss PIR5,TMR5IF
505 bra piezo_config_wait_bit3 ; Wait loop 511 bra piezo_config_wait_bit3 ; Wait loop
506 return 512 return
507 513
508 global reset_battery_pointer, reset_battery_internal_only 514 global reset_battery_pointer
509 reset_battery_pointer: ; Resets battery pointer 0x07-0x0C and battery_gauge:5 515 reset_battery_pointer: ; Resets battery pointer 0x07-0x0C and battery_gauge:5
510 extern lt2942_charge_done 516 extern lt2942_charge_done
511 btfsc battery_gauge_available ; Something to reset? 517 btfsc battery_gauge_available ; Something to reset?
512 call lt2942_charge_done ; Yes, reset accumulating registers to 0xFFFF 518 call lt2942_charge_done ; Yes, reset accumulating registers to 0xFFFF
513 reset_battery_internal_only: 519 goto reset_battery_internal_only ; and return
514 clrf EEADRH 520
515 clrf EEDATA ; Delete to zero 521
516 write_int_eeprom 0x07 522 global get_analog_switches
517 write_int_eeprom 0x08 523 get_analog_switches: ; starts ADC and waits until finished
518 write_int_eeprom 0x09 524 btfsc analog_switches
519 write_int_eeprom 0x0A 525 bra get_analog_switches2
520 write_int_eeprom 0x0B 526 ; no analog switches
521 write_int_eeprom 0x0C 527 bcf analog_sw2_pressed
522 banksel battery_gauge+0 528 bcf analog_sw1_pressed
523 clrf battery_gauge+0 529 return ; Done.
524 clrf battery_gauge+1 530 get_analog_switches2:
525 clrf battery_gauge+2 531 btfsc adc_running ; ADC in use?
526 clrf battery_gauge+3 532 return ; Yes, return
527 clrf battery_gauge+4 533
528 clrf battery_gauge+5 534 movlw b'00001001' ; left justified
529 banksel common 535 movwf ADCON2
530 movlw .100 536 ; movlw b'00000000' ; Vref+ = Vdd
531 movwf batt_percent 537 clrf ADCON1
532 return 538 movlw b'00100101' ; power on ADC, select AN9
533 539 rcall wait_adc
534 global get_analog_switches 540 banksel analog_counter
535 get_analog_switches: ; starts ADC and waits until finished 541 movff ADRESH,WREG
536 btfsc analog_switches 542 addwf analog_sw2_raw+0
537 bra get_analog_switches2 543 movlw .0
538 ; no analog switches 544 addwfc analog_sw2_raw+1
539 bcf analog_sw2_pressed 545 decfsz analog_counter,F ; continue averaging?
540 bcf analog_sw1_pressed 546 bra get_analog_switches2a ; Yes
541 return ; Done. 547 ; Done. Compute average
542 get_analog_switches2: 548 bcf STATUS,C
543 btfsc adc_running ; ADC in use? 549 rrcf analog_sw2_raw+1
544 return ; Yes, return 550 rrcf analog_sw2_raw+0 ; /2
545 551 bcf STATUS,C
546 movlw b'00001001' ; left justified 552 rrcf analog_sw2_raw+1
547 movwf ADCON2 553 rrcf analog_sw2_raw+0 ; /4
548 ; movlw b'00000000' ; Vref+ = Vdd 554 bcf STATUS,C
549 clrf ADCON1 555 rrcf analog_sw2_raw+1
550 movlw b'00100101' ; power on ADC, select AN9 556 rrcf analog_sw2_raw+0 ; /8
551 rcall wait_adc 557 bcf STATUS,C
552 banksel analog_counter 558 rrcf analog_sw2_raw+1
553 movff ADRESH,WREG 559 rrcf analog_sw2_raw+0 ; /16
554 addwf analog_sw2_raw+0 560 movff analog_sw2_raw+0,analog_sw2
555 movlw .0 561 clrf analog_sw2_raw+1
556 addwfc analog_sw2_raw+1 562 clrf analog_sw2_raw+0 ; Reset average registers
557 decfsz analog_counter,F ; continue averaging? 563 ; movlw .16
558 bra get_analog_switches2a ; Yes 564 ; movwf analog_counter ; only once...
559 ; Done. Compute average 565 get_analog_switches2a:
560 bcf STATUS,C 566 banksel common
561 rrcf analog_sw2_raw+1 567 bcf analog_sw2_pressed
562 rrcf analog_sw2_raw+0 ; /2 568 movff opt_cR_button_left,WREG ;20-100
563 bcf STATUS,C 569 bcf STATUS,C
564 rrcf analog_sw2_raw+1 570 rrcf WREG ;/2 -> 10-50
565 rrcf analog_sw2_raw+0 ; /4 571 bcf STATUS,C
566 bcf STATUS,C 572 rrcf WREG ;/2 -> 5-25
567 rrcf analog_sw2_raw+1 573 decf WREG,W ;-1
568 rrcf analog_sw2_raw+0 ; /8 574 decf WREG,W ;-1
569 bcf STATUS,C 575 decf WREG,W ;-1 -> 2-22
570 rrcf analog_sw2_raw+1 576 banksel analog_sw2
571 rrcf analog_sw2_raw+0 ; /16 577 btfss button_polarity,1 ;(1= normal, 0=inverted)
572 movff analog_sw2_raw+0, analog_sw2 578 bra sw2_inverted
573 clrf analog_sw2_raw+1 579 addwf analog_sw2,W ; average (~128)
574 clrf analog_sw2_raw+0 ; Reset average registers 580 cpfsgt ADRESH
575 ; movlw .16 581 bra get_analog_sw1
576 ; movwf analog_counter ; only once... 582 banksel common
577 get_analog_switches2a: 583 bsf analog_sw2_pressed ; Left button normal
578 banksel common 584 bra get_analog_sw1
579 bcf analog_sw2_pressed
580 movff opt_cR_button_left,WREG ;20-100
581 bcf STATUS,C
582 rrcf WREG ;/2 -> 10-50
583 bcf STATUS,C
584 rrcf WREG ;/2 -> 5-25
585 decf WREG,W ;-1
586 decf WREG,W ;-1
587 decf WREG,W ;-1 -> 2-22
588 banksel analog_sw2
589 btfss button_polarity,1;(1= normal, 0=inverted)
590 bra sw2_inverted
591 addwf analog_sw2,W ; average (~128)
592 cpfsgt ADRESH
593 bra get_analog_sw1
594 banksel common
595 bsf analog_sw2_pressed ; Left button normal
596 bra get_analog_sw1
597 sw2_inverted: 585 sw2_inverted:
598 subwf analog_sw2,W ; average (~128) 586 subwf analog_sw2,W ; average (~128)
599 cpfslt ADRESH 587 cpfslt ADRESH
600 bra get_analog_sw1 588 bra get_analog_sw1
601 banksel common 589 banksel common
602 bsf analog_sw2_pressed ; Left button inverted 590 bsf analog_sw2_pressed ; Left button inverted
603 get_analog_sw1: 591 get_analog_sw1:
604 banksel common 592 banksel common
605 movlw b'00101001' ; power on ADC, select AN10 593 movlw b'00101001' ; power on ADC, select AN10
606 rcall wait_adc 594 rcall wait_adc
607 banksel analog_counter 595 banksel analog_counter
608 movff ADRESH,WREG 596 movff ADRESH,WREG
609 addwf analog_sw1_raw+0 597 addwf analog_sw1_raw+0
610 movlw .0 598 movlw .0
611 addwfc analog_sw1_raw+1 599 addwfc analog_sw1_raw+1
612 tstfsz analog_counter ; continue averaging? 600 tstfsz analog_counter ; continue averaging?
613 bra get_analog_switches1a ; Yes 601 bra get_analog_switches1a ; Yes
614 ; Done. Compute average 602 ; Done. Compute average
615 bcf STATUS,C 603 bcf STATUS,C
616 rrcf analog_sw1_raw+1 604 rrcf analog_sw1_raw+1
617 rrcf analog_sw1_raw+0 ; /2 605 rrcf analog_sw1_raw+0 ; /2
618 bcf STATUS,C 606 bcf STATUS,C
619 rrcf analog_sw1_raw+1 607 rrcf analog_sw1_raw+1
620 rrcf analog_sw1_raw+0 ; /4 608 rrcf analog_sw1_raw+0 ; /4
621 bcf STATUS,C 609 bcf STATUS,C
622 rrcf analog_sw1_raw+1 610 rrcf analog_sw1_raw+1
623 rrcf analog_sw1_raw+0 ; /8 611 rrcf analog_sw1_raw+0 ; /8
624 bcf STATUS,C 612 bcf STATUS,C
625 rrcf analog_sw1_raw+1 613 rrcf analog_sw1_raw+1
626 rrcf analog_sw1_raw+0 ; /16 614 rrcf analog_sw1_raw+0 ; /16
627 movff analog_sw1_raw+0, analog_sw1 615 movff analog_sw1_raw+0,analog_sw1
628 clrf analog_sw1_raw+1 616 clrf analog_sw1_raw+1
629 clrf analog_sw1_raw+0 ; Reset average registers 617 clrf analog_sw1_raw+0 ; Reset average registers
630 movlw .16 618 movlw .16
631 movwf analog_counter ; only once... 619 movwf analog_counter ; only once...
632 get_analog_switches1a: 620 get_analog_switches1a:
633 banksel common 621 banksel common
634 bcf analog_sw1_pressed 622 bcf analog_sw1_pressed
635 movff opt_cR_button_right,WREG ;20-100 623 movff opt_cR_button_right,WREG ;20-100
636 bcf STATUS,C 624 bcf STATUS,C
637 rrcf WREG ;/2 -> 10-50 625 rrcf WREG ;/2 -> 10-50
638 bcf STATUS,C 626 bcf STATUS,C
639 rrcf WREG ;/2 -> 5-25 627 rrcf WREG ;/2 -> 5-25
640 decf WREG,W ;-1 628 decf WREG,W ;-1
641 decf WREG,W ;-1 629 decf WREG,W ;-1
642 decf WREG,W ;-1 -> 2-22 630 decf WREG,W ;-1 -> 2-22
643 banksel analog_sw1 631 banksel analog_sw1
644 btfss button_polarity,0;(1= normal, 0=inverted) 632 btfss button_polarity,0 ;(1= normal, 0=inverted)
645 bra sw1_inverted 633 bra sw1_inverted
646 addwf analog_sw1,W ; average (~128) 634 addwf analog_sw1,W ; average (~128)
647 cpfsgt ADRESH 635 cpfsgt ADRESH
648 bra get_analog_sw_done 636 bra get_analog_sw_done
649 banksel common 637 banksel common
650 bsf analog_sw1_pressed ; right button normal 638 bsf analog_sw1_pressed ; right button normal
651 bra get_analog_sw_done 639 bra get_analog_sw_done
652 sw1_inverted: 640 sw1_inverted:
653 subwf analog_sw1,W ; average (~128) 641 subwf analog_sw1,W ; average (~128)
654 cpfslt ADRESH 642 cpfslt ADRESH
655 bra get_analog_sw_done 643 bra get_analog_sw_done
656 banksel common 644 banksel common
657 bsf analog_sw1_pressed ; right button inverted 645 bsf analog_sw1_pressed ; right button inverted
658 get_analog_sw_done: 646 get_analog_sw_done:
659 banksel common 647 banksel common
660 movlw b'10001101' ; Restore to right justified 648 movlw b'10001101' ; Restore to right justified
661 movwf ADCON2 649 movwf ADCON2
662 btfsc analog_sw1_pressed 650 btfsc analog_sw1_pressed
663 return 651 return
664 btfsc analog_sw2_pressed 652 btfsc analog_sw2_pressed
665 return 653 return
666 setf TMR1H ; No button pressed, enhance timer1 to overflow quickly 654 setf TMR1H ; No button pressed, enhance timer1 to overflow quickly
667 return 655 return
668 656
669 END 657 END