Mercurial > public > hwos_code
annotate src/isr.asm @ 120:e2f04bb2539c
battery check in sleep
author | heinrichsweikamp |
---|---|
date | Fri, 27 Jun 2014 15:52:43 +0200 |
parents | f3062a611eef |
children | e3ac5b2021bc |
rev | line source |
---|---|
0 | 1 ;============================================================================= |
2 ; | |
3 ; File isr.asm | |
4 ; | |
5 ; INTERUPT subroutines | |
6 ; | |
7 ; Copyright (c) 2011, JD Gascuel, HeinrichsWeikamp, all right reserved. | |
8 ;============================================================================= | |
9 ; HISTORY | |
10 ; 2011-05-24 : [jDG] Cleanups from initial Matthias code. | |
11 | |
12 #include "ostc3.inc" | |
13 #include "shared_definitions.h" ; Mailbox from/to p2_deco.c | |
14 #include "ms5541.inc" | |
15 #include "adc_lightsensor.inc" | |
16 | |
17 ;============================================================================= | |
18 | |
19 extern start | |
20 | |
21 isr_high CODE 0x0008 ;High Priority Interrups | |
22 bra HighInt | |
23 nop | |
24 nop | |
25 nop | |
26 nop | |
27 nop | |
28 nop | |
29 bra HighInt | |
30 | |
31 isr_low CODE 0x00018 ;Low Priority Interrups | |
32 ; *** low priority interrupts not used | |
33 retfie FAST ; Restores BSR, STATUS and WREG | |
34 | |
35 HighInt: | |
36 movff PRODL,isr_prod+0 | |
37 movff PRODH,isr_prod+1 | |
38 | |
39 ; Buttons | |
40 btfsc PIR1,TMR1IF ; Timer1 INT (Button hold-down Timer) | |
41 rcall timer1int | |
42 btfsc INTCON,INT0IF ; Buttons | |
43 rcall isr_switch_right | |
44 btfsc INTCON3,INT1IF ; Buttons | |
45 rcall isr_switch_left | |
46 | |
47 ; IR-Link | |
113 | 48 btfsc PIR3,RC2IF ; UART2 |
49 rcall isr_uart2 ; IR-Link | |
0 | 50 btfsc PIR2,TMR3IF ; Timer 3 |
51 rcall isr_timer3 ; IR-Link Timeout | |
113 | 52 |
53 ; Pressure sensor and others | |
54 btfsc PIR5,TMR7IF ; Timer 7 | |
55 rcall isr_tmr7 ; Every 62,5ms | |
56 | |
57 ;; IR-Link (again) | |
58 ; btfsc PIR3,RC2IF ; UART2 | |
59 ; rcall isr_uart2 ; IR-Link | |
0 | 60 |
61 ; RTCC | |
62 btfsc PIR3,RTCCIF ; Real-time-clock interrupt | |
63 rcall isr_rtcc ; May return in bank common! | |
64 | |
65 movff isr_prod+1,PRODH | |
66 movff isr_prod+0,PRODL | |
67 retfie FAST ; Restores BSR, STATUS and WREG | |
68 | |
69 ;============================================================================= | |
70 | |
71 isr_uart2: ; IR-Link | |
72 banksel RCREG2 | |
73 movf RCREG2,W | |
74 bcf RCSTA2,CREN ; Clear receiver status | |
75 bsf RCSTA2,CREN | |
76 banksel isr_backup | |
77 bcf PIR3,RC2IF ; Clear flag | |
78 incf ir_counter,F ; Increase counter | |
79 movff ir_counter,isr1_temp ; Copy | |
80 dcfsnz isr1_temp,F | |
81 movwf ir_buffer+.0 | |
82 dcfsnz isr1_temp,F | |
83 movwf ir_buffer+.1 | |
84 dcfsnz isr1_temp,F | |
85 movwf ir_buffer+.2 | |
86 dcfsnz isr1_temp,F | |
87 movwf ir_buffer+.3 | |
88 dcfsnz isr1_temp,F | |
89 movwf ir_buffer+.4 | |
90 dcfsnz isr1_temp,F | |
91 movwf ir_buffer+.5 | |
92 dcfsnz isr1_temp,F | |
93 movwf ir_buffer+.6 | |
94 dcfsnz isr1_temp,F | |
95 movwf ir_buffer+.7 | |
96 dcfsnz isr1_temp,F | |
97 movwf ir_buffer+.8 | |
98 dcfsnz isr1_temp,F | |
99 movwf ir_buffer+.9 | |
100 dcfsnz isr1_temp,F | |
101 movwf ir_buffer+.10 | |
102 dcfsnz isr1_temp,F | |
103 movwf ir_buffer+.11 | |
104 dcfsnz isr1_temp,F | |
105 movwf ir_buffer+.12 | |
106 dcfsnz isr1_temp,F | |
107 movwf ir_buffer+.13 | |
108 dcfsnz isr1_temp,F | |
109 movwf ir_buffer+.14 | |
110 dcfsnz isr1_temp,F | |
111 movwf ir_buffer+.15 | |
113 | 112 dcfsnz isr1_temp,F |
113 movwf ir_buffer+.16 | |
114 dcfsnz isr1_temp,F | |
115 movwf ir_buffer+.17 | |
116 | |
0 | 117 clrf TMR3L ; Preload timer |
118 movlw .253 | |
119 movwf TMR3H | |
120 bsf T3CON,TMR3ON ; (Re)Start Timeout counter | |
121 return | |
122 | |
123 isr_timer3: ; IR-Link Timeout | |
124 bcf T3CON,TMR3ON ; Stop Timer3 | |
113 | 125 movff ir_counter,char_I_extra_time |
0 | 126 banksel isr_backup ; Select Bank0 for ISR data. |
127 movlw .15 | |
128 cpfseq ir_counter ; Got exact 15bytes? | |
129 bra isr_timer3_1 ; No, test for 16bytes | |
113 | 130 bra isr_timer3_ir ; Got 15 bytes, compute local checksum |
0 | 131 isr_timer3_1: |
132 movlw .16 | |
133 cpfseq ir_counter ; Got exact 16bytes? | |
113 | 134 bra isr_timer3_2 ; No, test for 17bytes |
0 | 135 tstfsz ir_buffer+.15 ; Last byte=0x00 |
136 bra isr_timer3_exit ; No, exit | |
113 | 137 bra isr_timer3_ir ; Got 16 bytes, compute local checksum |
138 isr_timer3_2: | |
139 movlw .17 | |
140 cpfseq ir_counter ; Got exact 17bytes? | |
141 bra isr_timer3_exit ; No, exit | |
142 bra isr_timer3_s8 ; S8 data | |
0 | 143 |
113 | 144 isr_timer3_ir: ; IR input |
0 | 145 movff ir_buffer+.0,PRODL |
146 clrf PRODH | |
147 movf ir_buffer+.1,W | |
148 rcall isr_timer3_checksum | |
149 movf ir_buffer+.2,W | |
150 rcall isr_timer3_checksum | |
151 movf ir_buffer+.3,W | |
152 rcall isr_timer3_checksum | |
153 movf ir_buffer+.4,W | |
154 rcall isr_timer3_checksum | |
155 movf ir_buffer+.5,W | |
156 rcall isr_timer3_checksum | |
157 movf ir_buffer+.6,W | |
158 rcall isr_timer3_checksum | |
159 movf ir_buffer+.7,W | |
160 rcall isr_timer3_checksum | |
161 movf ir_buffer+.8,W | |
162 rcall isr_timer3_checksum | |
163 movf ir_buffer+.9,W | |
164 rcall isr_timer3_checksum | |
165 movf ir_buffer+.10,W | |
166 rcall isr_timer3_checksum | |
167 movf ir_buffer+.11,W | |
168 rcall isr_timer3_checksum | |
169 movf ir_buffer+.12,W | |
170 rcall isr_timer3_checksum | |
171 | |
172 ; Compare checksum | |
173 movf ir_buffer+.13,W | |
174 cpfseq PRODL ; Checksum ok? | |
175 bra isr_timer3_exit ; No, exit | |
176 movf ir_buffer+.14,W | |
177 cpfseq PRODH ; Checksum ok? | |
178 bra isr_timer3_exit ; No, exit | |
179 | |
180 ; Checksum OK, copy results | |
181 movff ir_buffer+.1,hud_status_byte | |
182 movff ir_buffer+.2,o2_mv_sensor1+0 | |
183 movff ir_buffer+.3,o2_mv_sensor1+1 | |
184 movff ir_buffer+.4,o2_mv_sensor2+0 | |
185 movff ir_buffer+.5,o2_mv_sensor2+1 | |
186 movff ir_buffer+.6,o2_mv_sensor3+0 | |
187 movff ir_buffer+.7,o2_mv_sensor3+1 | |
188 movff ir_buffer+.8,o2_ppo2_sensor1 | |
189 movff ir_buffer+.9,o2_ppo2_sensor2 | |
190 movff ir_buffer+.10,o2_ppo2_sensor3 | |
191 movff ir_buffer+.11,hud_battery_mv+0 | |
192 movff ir_buffer+.12,hud_battery_mv+1 | |
193 | |
194 movlw ir_timeout_value ; multiples of 62,5ms | |
195 movwf ir_timeout ; Reload timeout | |
196 | |
197 isr_timer3_exit: | |
198 clrf ir_counter ; Clear pointer | |
199 bcf PIR2,TMR3IF ; Clear flag | |
200 return | |
201 | |
202 isr_timer3_checksum: | |
203 addwf PRODL,F | |
204 movlw .0 | |
205 addwfc PRODH,F | |
206 return | |
207 | |
113 | 208 isr_timer3_s8: ; IR input |
209 movff ir_buffer+.0,PRODL | |
210 clrf PRODH | |
211 movf ir_buffer+.1,W | |
212 rcall isr_timer3_checksum | |
213 movf ir_buffer+.2,W | |
214 rcall isr_timer3_checksum | |
215 movf ir_buffer+.3,W | |
216 rcall isr_timer3_checksum | |
217 movf ir_buffer+.4,W | |
218 rcall isr_timer3_checksum | |
219 movf ir_buffer+.5,W | |
220 rcall isr_timer3_checksum | |
221 movf ir_buffer+.6,W | |
222 rcall isr_timer3_checksum | |
223 movf ir_buffer+.7,W | |
224 rcall isr_timer3_checksum | |
225 movf ir_buffer+.8,W | |
226 rcall isr_timer3_checksum | |
227 movf ir_buffer+.9,W | |
228 rcall isr_timer3_checksum | |
229 movf ir_buffer+.10,W | |
230 rcall isr_timer3_checksum | |
231 movf ir_buffer+.11,W | |
232 rcall isr_timer3_checksum | |
233 movf ir_buffer+.12,W | |
234 rcall isr_timer3_checksum | |
235 movf ir_buffer+.13,W | |
236 rcall isr_timer3_checksum | |
237 movf ir_buffer+.14,W | |
238 rcall isr_timer3_checksum | |
239 | |
240 ; Compare checksum | |
241 movf ir_buffer+.15,W | |
242 cpfseq PRODL ; Checksum ok? | |
243 bra isr_timer3_exit ; No, exit | |
244 movf ir_buffer+.16,W | |
245 cpfseq PRODH ; Checksum ok? | |
246 bra isr_timer3_exit ; No, exit | |
247 | |
248 ; Checksum OK, copy results | |
249 movff ir_buffer+.3,hud_status_byte | |
250 movff ir_buffer+.13,hud_battery_mv+0 | |
251 movff ir_buffer+.14,hud_battery_mv+1 | |
252 | |
253 banksel common | |
254 bsf new_s8_data_available ; set flag | |
255 banksel ir_timeout | |
256 | |
257 movlw ir_timeout_value ; multiples of 62,5ms | |
258 movwf ir_timeout ; Reload timeout | |
259 bra isr_timer3_exit ; Exit | |
260 | |
261 | |
0 | 262 ;============================================================================= |
263 | |
264 isr_tmr7: ; each 62,5ms | |
265 bcf PIR5,TMR7IF ; clear flag | |
266 banksel 0xF16 ; Addresses, F16h through F5Fh, are also used by SFRs, but are not part of the Access RAM. | |
267 movlw .248 | |
268 movwf TMR7H ; -> Rollover after 2048 cycles -> 62,5ms | |
269 | |
270 banksel isr_backup | |
271 movf max_CCPR1L,W ; Dimm value | |
272 cpfseq CCPR1L ; = current PWM value? | |
273 rcall isr_dimm_tft ; No, adjust until max_CCPR1L=CCPR1L ! | |
274 | |
275 banksel isr_backup | |
276 decfsz ir_timeout,F ; IR Data still valid? | |
277 bra isr_tmr7_2 ; Yes, continue | |
278 ; timeout, clear IR-Data | |
279 | |
280 movlw ir_timeout_value ; multiples of 62,5ms | |
281 movwf ir_timeout ; Reload timeout | |
282 | |
283 banksel common | |
113 | 284 btfss c3_hardware |
285 bra isr_tmr7_1a ; Always with normal ostc3 hardware | |
286 btfss s8_digital | |
287 bra isr_tmr7_2 ; only when digital | |
288 isr_tmr7_1a: | |
0 | 289 clrf o2_mv_sensor1+0 |
290 clrf o2_mv_sensor1+1 | |
291 clrf o2_mv_sensor2+0 | |
292 clrf o2_mv_sensor2+1 | |
293 clrf o2_mv_sensor3+0 | |
294 clrf o2_mv_sensor3+1 | |
295 clrf hud_battery_mv+0 | |
296 clrf hud_battery_mv+1 | |
297 clrf hud_status_byte | |
298 clrf o2_ppo2_sensor1 | |
299 clrf o2_ppo2_sensor2 | |
300 clrf o2_ppo2_sensor3 | |
301 | |
302 isr_tmr7_2: | |
303 banksel common | |
304 btfss no_sensor_int ; No sensor interrupt (because it's addressed during sleep) | |
305 bra isr_sensor_state2 ; No, continue | |
306 banksel isr_backup ; Back to Bank0 ISR data | |
307 return | |
308 | |
309 isr_set_speed_to_normal: | |
310 ; Set Speed to normal | |
311 movlw b'01110010' | |
312 movwf OSCCON ; 16MHz INTOSC | |
313 movlw b'00000000' | |
314 movwf OSCTUNE ; 4x PLL Disable (Bit6) - only works with 8 or 16MHz (=32 or 64MHz) | |
315 movlw b'00001101' ; 1:2 Postscaler, 1:4 Prescaler, Timer 2 start -> 1960Hz (no-flicker) | |
316 movwf T2CON | |
317 btfss OSCCON,HFIOFS | |
318 bra $-2 ; Wait until clock is stable | |
319 return | |
320 | |
321 isr_sensor_state2: | |
322 banksel common | |
323 movff sensor_state_counter,WREG | |
324 btfss WREG,0 ; every 1/4 second | |
325 bsf quarter_second_update ; Set flag | |
326 movlw d'2' | |
327 cpfseq speed_setting ; Set to normal in case it's not already in normal speed mode | |
328 rcall isr_set_speed_to_normal | |
329 banksel isr_backup ; Back to Bank0 ISR data | |
330 | |
331 incf sensor_state_counter,F ; counts to eight for state maschine | |
332 | |
333 ; State 1: Clear flags and average registers, get temperature (51us) and start pressure integration (73,5us) | |
334 ; State 2: Get pressure (51us), start temperature integration (73,5us) and calculate temperature compensated pressure (233us) | |
335 ; State 3: Get temperature (51us) and start pressure integration (73,5us) | |
336 ; State 4: Get pressure (51us), start temperature integration (73,5us) and calculate temperature compensated pressure (233us) | |
337 ; State 5: Get temperature (51us) and start pressure integration (73,5us) | |
338 ; State 6: Get pressure (51us), start temperature integration (73,5us) and calculate temperature compensated pressure (233us) | |
339 ; State 7: Get temperature (51us) and start pressure integration (73,5us) | |
340 ; State 8: Get pressure (51us), start temperature integration (73,5us), calculate temperature compensated pressure (233us) and build average for half-second update of tempperature and pressure | |
341 | |
342 movff sensor_state_counter,WREG ; WREG used as temp here... | |
343 dcfsnz WREG,F | |
344 bra sensor_int_state1_plus_restart ; Do State 1 | |
345 dcfsnz WREG,F | |
346 bra sensor_int_state2 ; Do State 2 | |
347 dcfsnz WREG,F | |
348 bra sensor_int_state1 ; Do State 3 | |
349 dcfsnz WREG,F | |
350 bra sensor_int_state2 ; Do State 4 | |
351 dcfsnz WREG,F | |
352 bra sensor_int_state1 ; Do State 5 | |
353 dcfsnz WREG,F | |
354 bra sensor_int_state2 ; Do State 6 | |
355 dcfsnz WREG,F | |
356 bra sensor_int_state1 ; Do State 7 | |
357 ; bra sensor_int2_plus_average ; Do State 8 | |
358 ;sensor_int2_plus_average: | |
359 ; First, do state2: | |
360 call get_pressure_value ; State2: Get pressure (51us) | |
361 call get_temperature_start ; and start temperature integration (73,5us) | |
362 call calculate_compensation ; calculate temperature compensated pressure (27us) | |
363 ; Build average | |
364 bcf STATUS,C ; clear carry bit. | |
365 rrcf amb_pressure_avg+1 ; amb_pressure sum / 2 | |
366 rrcf amb_pressure_avg+0 | |
367 bcf STATUS,C ; clear carry bit, twice. | |
368 rrcf amb_pressure_avg+1 ; amb_pressure sum / 4 | |
369 rrcf amb_pressure_avg+0 | |
370 | |
371 ; ; Even more averaging | |
372 ; movf amb_pressure_avg+0,W | |
373 ; addwf amb_pressure+0 | |
374 ; movf amb_pressure_avg+1,W | |
375 ; addwfc amb_pressure+1 | |
376 ; bcf STATUS,C ; clear carry bit | |
377 ; rrcf amb_pressure+1 ; amb_pressure sum / 2 | |
378 ; rrcf amb_pressure+0 | |
379 | |
380 movff amb_pressure_avg+1,amb_pressure+1 ; copy into actual register | |
381 movff amb_pressure_avg+0,amb_pressure+0 | |
382 | |
383 bcf STATUS,C | |
384 btfsc temperature_avg+1,7 ; Copy sign bit to carry | |
385 bsf STATUS,C | |
386 rrcf temperature_avg+1 ; Signed temperature /2 | |
387 rrcf temperature_avg+0 | |
388 bcf STATUS,C | |
389 btfsc temperature_avg+1,7 ; Copy sign bit to carry | |
390 bsf STATUS,C | |
391 rrcf temperature_avg+1 ; Signed temperature /4 | |
392 rrcf temperature_avg+0 | |
393 | |
394 movff temperature_avg+1,temperature+1 ; copy into actual register | |
395 movff temperature_avg+0,temperature+0 | |
396 | |
397 banksel common ; flag1 is in Bank1 | |
398 bcf temp_changed ; Clear flag for temperature update | |
399 bcf pressure_refresh ; Clear flag for pressure update | |
400 banksel isr_backup ; Back to Bank0 ISR data | |
401 | |
402 ; Temp changed? | |
403 movf temperature+0,W | |
404 cpfseq last_temperature+0 | |
405 bra isr_sensor_state2_2 ; Yes | |
406 movf temperature+1,W | |
407 cpfseq last_temperature+1 | |
408 bra isr_sensor_state2_2 ; Yes | |
409 | |
410 bra isr_sensor_state2_3 ; no change | |
411 | |
412 isr_sensor_state2_2: | |
413 banksel common ; flag1 is in Bank1 | |
414 bsf temp_changed ; Yes | |
415 banksel isr_backup ; Back to Bank0 ISR data | |
416 isr_sensor_state2_3: | |
417 movff temperature+0,last_temperature+0 ; Copy for compare | |
418 movff temperature+1,last_temperature+1 | |
419 | |
420 movf amb_pressure+0,W | |
421 cpfseq last_pressure+0 | |
422 bra isr_sensor_state2_4 ; Yes | |
423 movf amb_pressure+1,W | |
424 cpfseq last_pressure+1 | |
425 bra isr_sensor_state2_4 ; Yes | |
426 | |
427 bra isr_sensor_state2_5 ; No change | |
428 isr_sensor_state2_4: | |
429 banksel common ; flag1 is in Bank1 | |
430 bsf pressure_refresh ; Yes | |
431 banksel isr_backup ; Back to Bank0 ISR data | |
432 isr_sensor_state2_5: | |
433 movff amb_pressure+0,last_pressure+0 ; Copy for compare | |
434 movff amb_pressure+1,last_pressure+1 | |
435 | |
436 clrf sensor_state_counter ; Then reset State counter | |
437 btfss simulatormode_active ; are we in simulator mode? | |
438 bra comp_air_pressure ; no | |
439 comp_air_pressure0: | |
440 movlw LOW d'1000' ; yes, so simulate 1000mbar surface pressure | |
441 movwf last_surfpressure+0 | |
442 movlw HIGH d'1000' | |
443 movwf last_surfpressure+1 | |
444 ; Always set pressure_refresh flag in simulator mode | |
445 banksel common ; flag1 is in Bank1 | |
446 bsf pressure_refresh ; Yes | |
447 banksel isr_backup ; Back to Bank0 ISR data | |
448 | |
449 comp_air_pressure: | |
450 movf last_surfpressure+0,W ; compensate airpressure | |
451 subwf amb_pressure+0,W | |
452 movwf rel_pressure+0 ; rel_pressure stores depth! | |
453 | |
454 movf last_surfpressure+1,W | |
455 subwfb amb_pressure+1,W | |
456 movwf rel_pressure+1 | |
457 btfss STATUS,N ; result is below zero? | |
458 bra sensor_int_state_exit | |
459 clrf rel_pressure+0 ; Yes, do not display negative depths | |
460 clrf rel_pressure+1 ; e.g. when surface air pressure dropped during the dive | |
461 bra sensor_int_state_exit | |
462 | |
463 sensor_int_state1_plus_restart: | |
464 clrf amb_pressure_avg+0 ; pressure average registers | |
465 clrf amb_pressure_avg+1 | |
466 clrf temperature_avg+0 | |
467 clrf temperature_avg+1 | |
468 | |
469 sensor_int_state1: | |
470 call get_temperature_value ; State 1: Get temperature | |
471 call get_pressure_start ; and start pressure integration. | |
472 bra sensor_int_state_exit | |
473 | |
474 sensor_int_state2: | |
475 call get_pressure_value ; State2: Get pressure (51us) | |
476 call get_temperature_start ; and start temperature integration (73,5us) | |
477 call calculate_compensation ; calculate temperature compensated pressure (233us) | |
478 ; bra sensor_int_state_exit | |
479 sensor_int_state_exit: | |
480 rcall isr_restore_clock ; Restore clock | |
481 return | |
482 ;============================================================================= | |
483 | |
484 isr_dimm_tft: ; Adjust until max_CCPR1L=CCPR1L ! | |
485 banksel common | |
486 btfsc tft_is_dimming ; Ignore while dimming | |
487 return | |
488 banksel isr_backup | |
489 movf max_CCPR1L,W | |
490 cpfsgt CCPR1L ; CCPR1L>max_CCPR1L? | |
491 bra isr_dimm_tft2 ; No, dimm up | |
492 ; dimm down | |
493 decf CCPR1L,F ; -1 | |
494 return | |
495 isr_dimm_tft2: | |
496 movf max_CCPR1L,W | |
31
53a09c1b7410
BUGFIX: alternating brightness at very low ambient light
heinrichsweikamp
parents:
30
diff
changeset
|
497 sublw ambient_light_min_eco |
53a09c1b7410
BUGFIX: alternating brightness at very low ambient light
heinrichsweikamp
parents:
30
diff
changeset
|
498 cpfsgt CCPR1L ; CCPR1L>max_CCPR1L-ambient_light_min_eco? |
0 | 499 bra isr_dimm_tft3 ; No, dimm up slow |
500 ; dimm up faster | |
501 movlw .5 | |
502 addwf CCPR1L,F | |
503 isr_dimm_tft3: | |
504 incf CCPR1L,F ; +1 | |
505 return | |
506 | |
507 | |
508 isr_rtcc: ; each second | |
509 bcf PIR3,RTCCIF ; clear flag | |
510 banksel 0xF16 ; Addresses, F16h through F5Fh, are also used by SFRs, but are not part of the Access RAM. | |
511 bsf RTCCFG,RTCPTR1 | |
512 bsf RTCCFG,RTCPTR0 ; year | |
513 movff RTCVALL,year ; format is BCD! | |
514 movff RTCVALH,day ; dummy read | |
515 movff RTCVALL,day ; format is BCD! | |
516 movff RTCVALH,month ; format is BCD! | |
517 movff RTCVALL,hours ; format is BCD! | |
518 movff RTCVALH,secs ; format is BCD! | |
519 movff RTCVALL,secs ; format is BCD! | |
520 movff RTCVALH,mins ; format is BCD! | |
521 banksel isr_backup ; Back to Bank0 ISR data | |
522 | |
523 ; Convert BCD to DEC and set registers | |
524 movff mins, isr1_temp | |
525 rcall isr_rtcc_convert ; Converts to dec with result in WREG | |
526 movff WREG,mins | |
527 movff secs, isr1_temp | |
528 rcall isr_rtcc_convert ; Converts to dec with result in WREG | |
529 movff WREG,secs | |
530 movff hours, isr1_temp | |
531 rcall isr_rtcc_convert ; Converts to dec with result in WREG | |
532 movff WREG,hours | |
533 movff month, isr1_temp | |
534 rcall isr_rtcc_convert ; Converts to dec with result in WREG | |
535 movff WREG,month | |
536 movff day, isr1_temp | |
537 rcall isr_rtcc_convert ; Converts to dec with result in WREG | |
538 movff WREG,day | |
539 movff year, isr1_temp | |
540 rcall isr_rtcc_convert ; Converts to dec with result in WREG | |
541 movff WREG,year | |
542 | |
543 ; Place once/second tasks for ISR here (Be sure of the right bank!) | |
544 banksel common ; flag1 is in Bank1 | |
545 btfss sleepmode ; in Sleepmode? | |
546 call get_ambient_level ; No, get ambient light level and set max_CCPR1L | |
547 | |
548 rcall isr_battery_gauge ; Add amount of battery consumption to battery_gauge:6 | |
549 | |
550 banksel common ; flag1 is in Bank1 | |
551 bsf onesecupdate ; A new second has begun | |
552 btfsc divemode ; in divemode? | |
553 rcall isr_divemode_1sec ; Yes, do some divemode stuff in bank common | |
554 | |
555 tstfsz secs ; Secs == 0 ? | |
556 return ; No, Done. | |
557 | |
558 ; banksel isr_backup ; Back to Bank0 ISR data | |
559 ; movff secs,isr1_temp ; Copy to Bank0 | |
560 ; movlw d'0' | |
561 ; cpfseq isr1_temp ; Secs == 0 ? | |
562 ; return ; Done. | |
563 | |
564 ; banksel common ; flag1 is in Bank1 | |
565 bsf oneminupdate ; A new minute has begun | |
566 | |
567 btfss divemode ; In Divemode? | |
568 rcall check_nofly_desat_time ; No, so reduce NoFly and Desat and increase interval | |
569 | |
570 ; Check if a new hour has just begun | |
571 ; banksel common ; flag1 is in Bank1 | |
572 tstfsz mins ; mins=0? | |
573 bra isr_rtcc2 ; No | |
574 bsf onehourupdate ; Yes, set flag | |
575 | |
576 isr_rtcc2: | |
577 banksel isr_backup ; Back to Bank0 ISR data | |
578 return ; Done. | |
579 | |
580 isr_battery_gauge: | |
581 banksel isr_backup ; Bank0 ISR data | |
582 movlw current_sleepmode ; 100µA/3600 -> nAs (Sleepmode current) | |
583 movwf isr1_temp ; Store value (low byte) | |
584 clrf isr2_temp ; High byte | |
585 | |
586 banksel common ; flag1 is in Bank1 | |
587 btfss sleepmode ; in Sleepmode? | |
588 rcall isr_battery_gauge2 ; No, compute current consumtion value into isr1_temp and isr2_temp | |
589 | |
590 banksel isr_backup ; Bank0 ISR data | |
591 movf isr1_temp,W ; 48Bit add of isr1_temp and isr2_temp into battery_gauge:6 | |
592 addwf battery_gauge+0,F | |
593 movf isr2_temp,W | |
594 addwfc battery_gauge+1,F | |
595 movlw .0 | |
596 addwfc battery_gauge+2,F | |
597 movlw .0 | |
598 addwfc battery_gauge+3,F | |
599 movlw .0 | |
600 addwfc battery_gauge+4,F | |
601 movlw .0 | |
602 addwfc battery_gauge+5,F | |
603 return | |
604 | |
605 isr_battery_gauge2: | |
606 ; set consumtion rate in nAs for an one second interval | |
607 ; Example: | |
608 ; movlw LOW .55556 ; 0,2A/3600*1e9s = nAs | |
609 ; movwf isr1_temp ; Low byte | |
610 ; movlw HIGH .55556 ; 0,2A/3600*1e9s = nAs | |
611 ; movwf isr2_temp ; High byte | |
612 | |
613 ; Current consumption for LED backlight is 47*CCPR1L+272 | |
614 movf CCPR1L,W | |
615 mullw current_backlight_multi | |
616 movlw LOW current_backlight_offset | |
617 addwf PRODL,F | |
618 movlw HIGH current_backlight_offset | |
619 addwfc PRODH,F | |
620 movff PRODL,isr1_temp | |
621 movff PRODH,isr2_temp ; isr1_temp and isr2_temp hold value for backlight | |
622 | |
623 ; Add current for CPU and GPU | |
624 ; speed_setting=1: ECO (3,1mA -> 861nAs), =2: NORMAL (5,50mA -> 1528nAs) or =3: FASTEST (8,04mA -> 2233nAs) | |
625 movlw .1 | |
626 cpfseq speed_setting | |
627 bra isr_battery_gauge3 | |
628 banksel isr_backup ; Bank0 ISR data | |
629 movlw LOW current_speed_eco | |
630 addwf isr1_temp,F | |
631 movlw HIGH current_speed_eco | |
632 addwfc isr2_temp,F | |
633 bra isr_battery_gauge5 | |
634 isr_battery_gauge3: | |
635 movlw .2 | |
636 cpfseq speed_setting | |
637 bra isr_battery_gauge4 | |
638 banksel isr_backup ; Bank0 ISR data | |
639 movlw LOW current_speed_normal | |
640 addwf isr1_temp,F | |
641 movlw HIGH current_speed_normal | |
642 addwfc isr2_temp,F | |
643 bra isr_battery_gauge5 | |
644 isr_battery_gauge4: | |
645 ; movlw .3 | |
646 ; cpfseq speed_setting | |
647 ; bra isr_battery_gauge5 | |
648 banksel isr_backup ; Bank0 ISR data | |
649 movlw LOW current_speed_fastest | |
650 addwf isr1_temp,F | |
651 movlw HIGH current_speed_fastest | |
652 addwfc isr2_temp,F | |
653 isr_battery_gauge5: | |
654 ; Add current if IR reciever is on | |
655 btfss ir_power ; IR enabled? | |
656 bra isr_battery_gauge6 ; no | |
657 movlw LOW current_ir_reciever | |
658 addwf isr1_temp,F | |
659 movlw HIGH current_ir_reciever | |
660 addwfc isr2_temp,F | |
661 isr_battery_gauge6: | |
662 ; Add current for compass/accelerometer | |
663 btfss compass_enabled ; compass active? | |
664 bra isr_battery_gauge7 ; no | |
665 movlw LOW current_compass | |
666 addwf isr1_temp,F | |
667 movlw HIGH current_compass | |
668 addwfc isr2_temp,F | |
669 isr_battery_gauge7: | |
670 return | |
671 | |
672 isr_divemode_1sec: | |
673 incf samplesecs,F ; "samplingrate" diving seconds done | |
674 decf samplesecs_value,W ; holds "samplingrate" value (minus 1 into WREG) | |
675 cpfsgt samplesecs ; Done? | |
676 bra isr_divemode_1sec2 ; no | |
677 | |
678 clrf samplesecs ; clear counter... | |
679 bsf store_sample ; ...and set bit for profile storage | |
680 isr_divemode_1sec2: | |
681 ; Increase re-setable average depth divetime counter | |
682 incf average_divesecs+0,F ; increase stopwatch registers | |
683 btfsc STATUS,Z | |
684 incf average_divesecs+1,F ; increase stopwatch registers | |
685 ; Increase total divetime (Regardless of start_dive_threshold) | |
686 incf total_divetime_seconds+0,F | |
687 movlw .0 | |
688 addwfc total_divetime_seconds+1,F ; Total dive time (Regardless of start_dive_threshold) | |
689 | |
690 btfss divemode2 ; displayed divetime is running? | |
691 return ; No (e.g. too shallow) | |
692 | |
693 ; increase divetime registers (Displayed dive time) | |
694 incf divesecs,F | |
695 movlw d'59' | |
696 cpfsgt divesecs | |
697 bra isr_divemode_1sec2a | |
698 | |
699 clrf divesecs | |
700 bsf realdive ; this bit is always set (again) if the dive is longer then one minute | |
701 incf divemins+0,F | |
702 movlw .0 | |
703 addwfc divemins+1,F ; increase divemins | |
704 | |
705 isr_divemode_1sec2a: | |
706 btfss FLAG_apnoe_mode ; Are we in Apnoe mode? | |
707 return ; No | |
708 | |
709 incf apnoe_secs,F ; increase descent registers | |
710 movlw d'59' | |
711 cpfsgt apnoe_secs ; full minute? | |
712 return ; No | |
713 clrf apnoe_secs | |
714 incf apnoe_mins,F ; increase descent mins | |
715 return | |
716 | |
717 ;============================================================================= | |
718 ; BCD to Binary convertion. | |
719 ; Input: isr1_temp = Value in BCD | |
720 ; Output WREG = value in binary. | |
721 isr_rtcc_convert: | |
722 swapf isr1_temp, W | |
723 andlw 0x0F ; W= tens | |
724 rlncf WREG, W ; W= 2*tens | |
725 subwf isr1_temp, F ; 16*tens + ones - 2*tens | |
726 subwf isr1_temp, F ; 14*tens + ones - 2*tens | |
727 subwf isr1_temp, W ; 12*tens + ones - 2*tens | |
728 return | |
729 | |
730 ;============================================================================= | |
731 | |
732 isr_switch_right: ; | |
733 bcf INTCON,INT0IE ; Disable INT0 | |
734 ; bcf power_sw2 ; Power-down switch circuity | |
735 banksel common ; flag1 is in Bank1 | |
736 bsf switch_right ; Set flag, button press is OK | |
737 bra isr_switch_common ; Continue... | |
738 | |
739 isr_switch_left: ; | |
740 bcf INTCON3,INT1IE ; Disable INT1 | |
741 ; bcf power_sw1 ; Power-down switch circuity | |
742 banksel common ; flag1 is in Bank1 | |
743 bsf switch_left ; Set flag, button press is OK | |
744 | |
745 isr_switch_common: | |
746 ; load timer1 for first press | |
747 clrf TMR1L | |
748 movlw TMR1H_VALUE_FIRST ; in steps of 7,8125ms | |
749 movwf TMR1H | |
750 bsf T1CON,TMR1ON ; Start Timer 1 | |
751 ; nop | |
752 ; nop | |
753 ; nop | |
754 ; nop | |
755 ; nop | |
756 ; nop | |
757 ; nop | |
758 ; nop | |
759 ; nop | |
760 ; nop | |
761 ; nop | |
762 ; nop | |
763 ; nop | |
764 ; nop | |
765 ; nop | |
766 ; nop | |
767 ; nop | |
768 ; nop | |
769 ; nop | |
770 ; nop | |
771 ; nop | |
772 ; nop | |
773 ; nop | |
774 ; nop | |
775 | |
776 banksel isr_backup ; Select Bank0 for ISR data. | |
777 ; bsf power_sw1 | |
778 bcf INTCON3,INT1IF ; Clear flag | |
779 ; bsf power_sw2 ; Power-up switch circuity | |
780 bcf INTCON,INT0IF ; Clear flag | |
781 return | |
782 | |
783 timer1int: | |
784 bcf PIR1,TMR1IF ; Clear flag | |
785 banksel common ; flag1 is in Bank1 | |
786 btfss switch_left1 ; Left button hold-down? | |
787 bra timer1int_left ; Yes | |
788 btfss switch_right2 ; Right button hold-down? | |
789 bra timer1int_right ; Yes | |
790 | |
791 ; No button hold-down, stop Timer 1 | |
792 bcf T1CON,TMR1ON ; Stop Timer 1 | |
793 bsf INTCON,INT0IE ; Enable INT0 | |
794 bsf INTCON3,INT1IE ; Enable INT1 | |
795 bcf INTCON,INT0IF ; Clear flag | |
796 bcf INTCON3,INT1IF ; Clear flag | |
797 ; bcf switch_left | |
798 ; bcf switch_right | |
799 return | |
800 | |
801 timer1int_left: | |
802 ; bcf power_sw1 ; Power-down switch circuity | |
803 bsf switch_left ; (Re-)Set flag | |
804 bra timer1int_common ; Continue | |
805 timer1int_right: | |
806 ; bcf power_sw2 ; Power-down switch circuity | |
807 bsf switch_right ; (Re-)Set flag | |
808 timer1int_common: | |
809 ; load timer1 for next press | |
810 clrf TMR1L | |
811 movlw TMR1H_VALUE_CONT ; Surface mode | |
812 btfsc divemode | |
813 movlw TMR1H_VALUE_CONT_DIVE ; Dive mode | |
814 movwf TMR1H | |
815 ; nop | |
816 ; nop | |
817 ; nop | |
818 ; nop | |
819 ; nop | |
820 ; nop | |
821 ; nop | |
822 ; nop | |
823 ; nop | |
824 ; nop | |
825 ; nop | |
826 ; nop | |
827 ; nop | |
828 ; nop | |
829 ; nop | |
830 ; nop | |
831 ; | |
832 ; nop | |
833 ; nop | |
834 ; nop | |
835 ; nop | |
836 ; nop | |
837 ; nop | |
838 ; nop | |
839 ; nop | |
840 ; bsf power_sw1 | |
841 bcf INTCON3,INT1IF ; Clear flag | |
842 ; bsf power_sw2 ; Power-up switch circuity | |
843 bcf INTCON,INT0IF ; Clear flag | |
844 return ; Return from timer1int with timer1 kept running | |
845 | |
846 ;============================================================================= | |
847 | |
848 check_nofly_desat_time: | |
849 movf nofly_time+0,W ; Is nofly null ? | |
850 iorwf nofly_time+1,W | |
851 bz check_nofly_desat_time2 ; yes... | |
852 | |
853 movlw d'1' | |
854 subwf nofly_time+0,F | |
855 movlw d'0' | |
856 subwfb nofly_time+1,F ; reduce by one | |
857 | |
858 check_nofly_desat_time2: | |
859 movf desaturation_time+0,W ; Is Desat null ? | |
860 iorwf desaturation_time+1,W | |
861 bz check_nofly_desat_time3 ; yes... | |
862 | |
863 movlw d'1' | |
864 subwf desaturation_time+0,F | |
865 movlw d'0' | |
866 subwfb desaturation_time+1,F ; reduce by one... | |
867 | |
868 ; Increase surface interval timer | |
869 movlw d'1' | |
870 addwf surface_interval+0,F | |
871 movlw d'0' | |
872 addwfc surface_interval+1,F | |
873 return ; Done | |
874 | |
875 check_nofly_desat_time3: | |
876 clrf surface_interval+0 | |
877 clrf surface_interval+1 ; Clear surface interval timer | |
878 return ; Done. | |
879 | |
880 ;============================================================================= | |
881 | |
882 isr_restore_clock: | |
883 banksel isr_backup | |
884 movff speed_setting,isr1_temp ; Copy to Bank0 | |
885 movlw d'1' | |
886 cpfseq isr1_temp | |
887 bra isr_restore_speed2 | |
888 ; Reset to eco | |
889 movlw b'00000000' | |
890 movwf OSCTUNE ; 4x PLL Disable (Bit6) - only works with 8 or 16MHz (=32 or 64MHz) | |
891 movlw b'00110010' | |
892 movwf OSCCON ; 1MHz INTOSC | |
893 movlw T2CON_ECO | |
894 movwf T2CON | |
895 bra isr_restore_exit | |
896 isr_restore_speed2: | |
897 movlw d'2' | |
898 cpfseq isr1_temp | |
899 bra isr_restore_speed3 | |
900 ; Reset to normal | |
901 movlw b'01110010' | |
902 movwf OSCCON ; 16MHz INTOSC | |
903 movlw b'00000000' | |
904 movwf OSCTUNE ; 4x PLL Disable (Bit6) - only works with 8 or 16MHz (=32 or 64MHz) | |
905 movlw T2CON_NORMAL | |
906 movwf T2CON | |
907 bra isr_restore_exit | |
908 | |
909 isr_restore_speed3: | |
910 ; Reset to fastest | |
911 movlw b'01110010' ; 16MHz INTOSC | |
912 movwf OSCCON | |
913 movlw b'01000000' | |
914 movwf OSCTUNE ; 4x PLL Enable (Bit6) - only works with 8 or 16MHz (=32 or 64MHz) | |
915 movlw T2CON_FASTEST | |
916 movwf T2CON | |
917 ; bra isr_restore_exit | |
918 isr_restore_exit: | |
919 btfss OSCCON,HFIOFS | |
920 bra isr_restore_exit ; loop until PLL is stable | |
921 return | |
922 | |
923 END |