comparison src/isr.asm @ 0:11d4fc797f74

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