comparison src/isr.asm @ 623:c40025d8e750

3.03 beta released
author heinrichsweikamp
date Mon, 03 Jun 2019 14:01:48 +0200
parents 1ad0531e9078
children cd58f7fc86db
comparison
equal deleted inserted replaced
622:02d1386429a6 623:c40025d8e750
1 ;============================================================================= 1 ;=============================================================================
2 ; 2 ;
3 ; File isr.asm REFACTORED VERSION V2.99f 3 ; File isr.asm combined next generation V3.03.4
4 ; 4 ;
5 ; INTERUPT subroutines 5 ; INTERUPT subroutines
6 ; 6 ;
7 ; Copyright (c) 2011, JD Gascuel, HeinrichsWeikamp, all right reserved. 7 ; Copyright (c) 2011, JD Gascuel, HeinrichsWeikamp, all right reserved.
8 ;============================================================================= 8 ;=============================================================================
11 11
12 #include "hwos.inc" 12 #include "hwos.inc"
13 #include "shared_definitions.h" ; Mailbox from/to p2_deco.c 13 #include "shared_definitions.h" ; Mailbox from/to p2_deco.c
14 #include "ms5541.inc" 14 #include "ms5541.inc"
15 #include "adc_lightsensor.inc" 15 #include "adc_lightsensor.inc"
16 #include "eeprom_rs232.inc" 16
17 17 extern restore_flash
18 ;============================================================================= 18
19 19 ;=============================================================================
20 extern start 20 ; Code to be placed at fixed position
21 21 ;
22 isr_high CODE 0x0008 ; high priority interrupts 22 isr_high CODE 0x0008 ; high priority interrupts
23 bra HighInt 23 bra HighInt
24 nop 24 nop
25 nop 25 nop
26 nop 26 nop
27 nop 27 nop
28 nop 28 nop
29 nop 29 nop
30 bra HighInt 30 bra HighInt
31 31 ; *** low priority interrupts are not used ***
32 isr_low CODE 0x00018 ; low priority interrupts 32 isr_low CODE 0x00018 ; low priority interrupts
33 ; *** low priority interrupts not used 33 retfie FAST ; do an immediate return with restore of BSR, STATUS and WREG
34 retfie FAST ; restores BSR, STATUS and WREG 34
35 35
36 ;=============================================================================
37 ; Interrupt Dispatcher
38 ;
36 HighInt: 39 HighInt:
37 movff PRODL,isr_prod+0 40 ; initialize interrupt code
38 movff PRODH,isr_prod+1 41 banksel isr_backup ; default bank for all ISR code is bank ISR data
39 42 rcall isr_registers_backup ; back-up registers
40 ; Buttons 43
41 btfsc PIR1,TMR1IF ; timer 1 INT (button hold-down timer) 44 ; serve buttons
42 rcall timer1int 45 btfsc PIR1,TMR1IF ; timer 1 interrupt (button hold-down timer)?
43 btfsc INTCON,INT0IF ; buttons 46 rcall timer1int ; YES - reset timer
44 rcall isr_switch_right 47 btfsc INTCON,INT0IF ; right button activity?
45 btfsc INTCON3,INT1IF ; buttons 48 rcall isr_switch_right ; YES - check right switch
46 rcall isr_switch_left 49 btfsc INTCON3,INT1IF ; left button activity?
47 50 rcall isr_switch_left ; YES - check left switch
48 ; IR/S8 link timer int 51
49 btfsc PIR3,RC2IF ; UART 2 52 IFDEF _external_sensor
50 rcall isr_uart2 ; IR/S8 link 53 ; serve IR/S8 link timer
51 btfsc PIR2,TMR3IF ; timer 3 54 btfsc PIR3,RC2IF ; UART 2 interrupt?
52 rcall isr_timer3 ; IR-Link timeout 55 rcall isr_uart2 ; YES - get a byte from the IR/S8 link
53 56 btfsc PIR2,TMR3IF ; timer 3 interrupt?
54 ; Pressure sensor and others 57 rcall isr_timer3 ; YES - check bytes received from IR/S8 link for being a valid telegram
55 btfsc PIR5,TMR7IF ; timer 7 58 ENDIF
56 rcall isr_tmr7 ; every 62.5ms 59
57 60 ; serve pressure and temperature sensor
58 ; RTCC 61 btfsc PIR5,TMR7IF ; timer 7 interrupt?
59 btfsc PIR3,RTCCIF ; real-time-clock interrupt 62 rcall isr_tmr7 ; YES - do every 62.5 ms tasks: read sensors, set CPU speed
60 rcall isr_rtcc ; may return in bank common! 63
61 64 ; serve real-time clock (RTCC)
62 movff isr_prod+1,PRODH 65 btfsc PIR3,RTCCIF ; real-time-clock interrupt?
63 movff isr_prod+0,PRODL 66 rcall isr_rtcc ; YES - do every 1/2 s tasks: read RTC, trigger timed tasks, adjust CPU speed, gauge battery, etc.
64 retfie FAST ; restores BSR, STATUS and WREG 67
68 ; clean up and exit
69 rcall isr_registers_restore ; restore registers
70 bsf trigger_isr_updates ; signal that the ISR had kicked in
71 retfie FAST ; return from interrupt restoring BSR, STATUS and WREG
72
73
74 ;=============================================================================
75 ; CPU speed adjustment
76 ;
77 isr_adjust_speed:
78 movff cpu_speed_request,cpu_speed_state ; acknowledge CPU speed request
79
80 btfsc speed_is_eco ; speed 'eco' requested?
81 bra isr_set_speed_to_eco ; YES - set eco speed
82 btfsc speed_is_fastest ; NO - speed 'fastest' requested?
83 bra isr_set_speed_to_fastest ; YES - set fastest speed
84 ;bra isr_set_speed_to_normal ; NO - default to normal speed
65 85
66 isr_set_speed_to_normal: 86 isr_set_speed_to_normal:
67 ; Set speed to normal 87 movlw b'00000000' ; coding for x4 PLL disabled
68 movlw b'01110010' 88 movwf OSCTUNE ; switch off x4 PLL
69 movwf OSCCON ; 16 MHz INTOSC 89 movlw b'01110010' ; select 16 MHz
70 movlw b'00000000' 90 movwf OSCCON ; set prescaler
71 movwf OSCTUNE ; 4x PLL disable (bit 6) - only works with 8 or 16 MHz (=32 or 64 MHz) 91 movlw T2CON_NORMAL ; PWM1 dimming factor for speed 'normal'
72 movlw T2CON_NORMAL 92 movwf T2CON ; adjust PWM1 for LED dimming
73 movwf T2CON 93 bra isr_adjust_speed_exit
74 btfss OSCCON,HFIOFS 94
75 bra $-2 ; wait until clock is stable 95 isr_set_speed_to_eco:
76 return 96 movlw b'00000000' ; coding for x4 PLL disabled
77 97 movwf OSCTUNE ; switch off x4 PLL
78 isr_dimm_tft: ; adjust until max_CCPR1L=CCPR1L 98 movlw b'00110010' ; select 1 MHz
79 banksel common 99 movwf OSCCON ; set prescaler
80 btfsc tft_is_dimming ; ignore while dimming 100 movlw T2CON_ECO ; PWM1 dimming factor for speed 'eco'
81 return 101 movwf T2CON ; adjust PWM1 for LED dimming
82 banksel isr_backup 102 bra isr_adjust_speed_exit
83 movf max_CCPR1L,W 103
84 cpfsgt CCPR1L ; CCPR1L > max_CCPR1L ? 104 isr_set_speed_to_fastest:
85 bra isr_dimm_tft2 ; NO - dimm up 105 movlw b'01110010' ; select 16 MHz by default
86 ; dimm down 106 btfsc lv_core ; on OSTC with low voltage core?
87 decf CCPR1L,F ; -1 107 movlw b'01100010' ; YES - reduce to 8 MHz
88 return 108 movwf OSCCON ; set prescaler
89 isr_dimm_tft2: 109 movlw b'01000000' ; coding for x4 PLL enable
90 movf max_CCPR1L,W 110 movwf OSCTUNE ; switch on x4 PLL -> 64 MHz on high voltage core, 32 MHz on low voltage core
91 sublw ambient_light_min_eco 111 movlw T2CON_FASTEST ; PWM1 dimming factor for speed 'fastest'
92 cpfsgt CCPR1L ; CCPR1L > max_CCPR1L-ambient_light_min_eco ? 112 movwf T2CON ; adjust PWM1 for LED dimming
93 bra isr_dimm_tft3 ; NO - dimm up slow 113 ;bra isr_adjust_speed_exit
94 ; dimm up faster 114
95 movlw .10 115 isr_adjust_speed_exit:
96 addwf CCPR1L,F 116 btfss OSCCON,HFIOFS ; PLL stabilized?
97 isr_dimm_tft3: 117 bra isr_adjust_speed_exit ; NO - loop to give it some more time
98 incf CCPR1L,F ; +1 118 return ; YES - done
99 return 119
120 ; Attention: fill-up the gap between the end of this section
121 ; and the next section which starts at 0x00080 !!
100 nop 122 nop
101 nop ; block flash here 123
102 124 block_0_code_end: ; marker to find end of code in block 0 in linker report file
125
126 ;=============================================================================
127 ; jump vector for the bootloader, placed at an appointed position here
128 ;
103 isr_restore CODE 0x00080 ; restore first flash page from EEPROM 129 isr_restore CODE 0x00080 ; restore first flash page from EEPROM
104 restore_flash_0x00080: 130 restore_flash_0x00080:
105 goto restore_flash 131 goto restore_flash
106 132
107 isr_routines ; CODE 133
108 ;============================================================================= 134 ;=============================================================================
109 135 ; back-up and restore registers
110 isr_uart2: ; IR/S8 link 136 ;
111 banksel RCREG2 137 isr_registers_backup:
112 movf RCREG2,W 138 MOVII PRODL,PROD_backup ; back-up PRODH:PRODL
139 return
140
141 isr_registers_restore:
142 MOVII PROD_backup,PRODL ; restore PRODH:PRODL
143 return
144
145
146 ;=============================================================================
147 ; routines for handling digital-attached external sensors
148 ;
149 IFDEF _external_sensor
150
151 ; Take a byte received on IR/S8 link and slot it into the RX buffer
152 ;
153 isr_uart2:
154 banksel RCREG2 ; RC*2 is outside access RAM
155 movff RCREG2,isr_lo ; copy received byte to isr_lo
113 bcf RCSTA2,CREN ; clear receiver status 156 bcf RCSTA2,CREN ; clear receiver status
114 bsf RCSTA2,CREN 157 bsf RCSTA2,CREN ; ...
115 banksel isr_backup 158 banksel isr_backup ; back to default ISR bank
116 incf ir_s8_counter,F ; increase counter 159 movlw .18 ; size of the buffer
117 movff ir_s8_counter,isr1_temp ; copy 160 cpfslt ir_s8_counter ; number of received bytes < buffer size?
118 dcfsnz isr1_temp,F 161 bra isr_uart2_1 ; NO - buffer full, do not store the byte
119 movwf ir_s8_buffer+.0 162 movf ir_s8_counter,W ; YES - copy number of received bytes to WREG
120 dcfsnz isr1_temp,F 163 MOVII FSR0L,FSR0_backup ; - back-up FSR0
121 movwf ir_s8_buffer+.1 164 lfsr FSR0,ir_s8_buffer ; - load base address of buffer
122 dcfsnz isr1_temp,F 165 movff isr_lo,PLUSW0 ; - store received byte
123 movwf ir_s8_buffer+.2 166 MOVII FSR0_backup,FSR0L ; - restore FSR0
124 dcfsnz isr1_temp,F 167 incf ir_s8_counter,F ; - increment number of received bytes by 1
125 movwf ir_s8_buffer+.3 168 isr_uart2_1:
126 dcfsnz isr1_temp,F 169 clrf TMR3L ; reload timer 3
127 movwf ir_s8_buffer+.4 170 movlw .253 ; ...
128 dcfsnz isr1_temp,F 171 movwf TMR3H ; ...
129 movwf ir_s8_buffer+.5 172 bsf T3CON,TMR3ON ; (re)start timeout timer 3
130 dcfsnz isr1_temp,F 173 return
131 movwf ir_s8_buffer+.6 174
132 dcfsnz isr1_temp,F 175
133 movwf ir_s8_buffer+.7 176 ; Timeout on IR/S8 link: check the checksum and gather the received data
134 dcfsnz isr1_temp,F 177 ;
135 movwf ir_s8_buffer+.8 178 isr_timer3:
136 dcfsnz isr1_temp,F
137 movwf ir_s8_buffer+.9
138 dcfsnz isr1_temp,F
139 movwf ir_s8_buffer+.10
140 dcfsnz isr1_temp,F
141 movwf ir_s8_buffer+.11
142 dcfsnz isr1_temp,F
143 movwf ir_s8_buffer+.12
144 dcfsnz isr1_temp,F
145 movwf ir_s8_buffer+.13
146 dcfsnz isr1_temp,F
147 movwf ir_s8_buffer+.14
148 dcfsnz isr1_temp,F
149 movwf ir_s8_buffer+.15
150 dcfsnz isr1_temp,F
151 movwf ir_s8_buffer+.16
152 dcfsnz isr1_temp,F
153 movwf ir_s8_buffer+.17
154
155 clrf TMR3L ; preload timer
156 movlw .253
157 movwf TMR3H
158 bsf T3CON,TMR3ON ; (re)start timeout counter
159 return
160
161 isr_timer3: ; IR/S8 link timeout
162 bcf T3CON,TMR3ON ; stop timer 3 179 bcf T3CON,TMR3ON ; stop timer 3
163 banksel isr_backup ; select bank 0 for ISR data
164 movlw .15 180 movlw .15
165 cpfseq ir_s8_counter ; got exactly 15 bytes? 181 cpfseq ir_s8_counter ; got exactly 15 bytes?
166 bra isr_timer3_1 ; NO - test for 16bytes 182 bra isr_timer3_1 ; NO - test for 16 bytes
167 bra isr_timer3_ir ; YES - got 15 bytes, compute local checksum 183 bra isr_timer3_ir ; YES - got 15 bytes, compute local checksum
168 isr_timer3_1: 184 isr_timer3_1:
169 movlw .16 185 movlw .16
170 cpfseq ir_s8_counter ; got exactly 16 bytes? 186 cpfseq ir_s8_counter ; got exactly 16 bytes?
171 bra isr_timer3_2 ; NO - test for 17 bytes 187 bra isr_timer3_2 ; NO - test for 17 bytes
172 tstfsz ir_s8_buffer+.15 ; YES - last byte = 0x00 ? 188 tstfsz ir_s8_buffer+.15 ; YES - last byte = 0x00 ?
173 bra isr_timer3_exit ; No - exit 189 bra isr_timer3_exit ; No - exit
174 bra isr_timer3_ir ; YES - got 16 bytes, compute local checksum 190 bra isr_timer3_ir ; YES - got 16 bytes, compute local checksum
175 isr_timer3_2: 191 isr_timer3_2:
176 movlw .17 192 movlw .17
177 cpfseq ir_s8_counter ; got exactly 17 bytes? 193 cpfseq ir_s8_counter ; got exactly 17 bytes?
178 bra isr_timer3_exit ; NO - exit 194 bra isr_timer3_exit ; NO - exit
179 bra isr_timer3_s8 ; YES - S8 data 195 bra isr_timer3_s8 ; YES - S8 data
180 196
181 isr_timer3_ir: ; IR input 197 ; Process telegram received on IR link
182 movff ir_s8_buffer+.0,PRODL 198 isr_timer3_ir:
183 clrf PRODH 199 movlw .12 ; checksum shall be computed for 1st and next 12 bytes
184 movf ir_s8_buffer+.1,W 200 rcall compute_IR_S8_checksum ; compute checksum
185 rcall isr_timer3_checksum 201 movf ir_s8_buffer+.13,W ; get low byte of the received checksum
186 movf ir_s8_buffer+.2,W 202 cpfseq isr_mpr+0 ; = low byte of the calculated local checksum?
187 rcall isr_timer3_checksum
188 movf ir_s8_buffer+.3,W
189 rcall isr_timer3_checksum
190 movf ir_s8_buffer+.4,W
191 rcall isr_timer3_checksum
192 movf ir_s8_buffer+.5,W
193 rcall isr_timer3_checksum
194 movf ir_s8_buffer+.6,W
195 rcall isr_timer3_checksum
196 movf ir_s8_buffer+.7,W
197 rcall isr_timer3_checksum
198 movf ir_s8_buffer+.8,W
199 rcall isr_timer3_checksum
200 movf ir_s8_buffer+.9,W
201 rcall isr_timer3_checksum
202 movf ir_s8_buffer+.10,W
203 rcall isr_timer3_checksum
204 movf ir_s8_buffer+.11,W
205 rcall isr_timer3_checksum
206 movf ir_s8_buffer+.12,W
207 rcall isr_timer3_checksum
208
209 ; Compare checksum
210 movf ir_s8_buffer+.13,W
211 cpfseq PRODL ; checksum ok?
212 bra isr_timer3_exit ; NO - exit 203 bra isr_timer3_exit ; NO - exit
213 movf ir_s8_buffer+.14,W 204 movf ir_s8_buffer+.14,W ; get high byte of the received checksum
214 cpfseq PRODH ; checksum ok? 205 cpfseq isr_mpr+1 ; = high byte of the calculated local checksum?
215 bra isr_timer3_exit ; NO - exit 206 bra isr_timer3_exit ; NO - exit
216 207 ; YES to both - received telegram valid, copy data
217 ; Checksum OK, copy results 208
218 movff ir_s8_buffer+.1,hud_status_byte 209 movff ir_s8_buffer+.1, hud_status_byte
219 movff ir_s8_buffer+.2,o2_mv_sensor1+0 210 movff ir_s8_buffer+.2, sensor1_mv+0
220 movff ir_s8_buffer+.3,o2_mv_sensor1+1 211 movff ir_s8_buffer+.3, sensor1_mv+1
221 movff ir_s8_buffer+.4,o2_mv_sensor2+0 212 movff ir_s8_buffer+.4, sensor2_mv+0
222 movff ir_s8_buffer+.5,o2_mv_sensor2+1 213 movff ir_s8_buffer+.5, sensor2_mv+1
223 movff ir_s8_buffer+.6,o2_mv_sensor3+0 214 movff ir_s8_buffer+.6, sensor3_mv+0
224 movff ir_s8_buffer+.7,o2_mv_sensor3+1 215 movff ir_s8_buffer+.7, sensor3_mv+1
225 movff ir_s8_buffer+.8,o2_ppo2_sensor1 216 movff ir_s8_buffer+.8, sensor1_ppO2
226 movff ir_s8_buffer+.9,o2_ppo2_sensor2 217 movff ir_s8_buffer+.9, sensor2_ppO2
227 movff ir_s8_buffer+.10,o2_ppo2_sensor3 218 movff ir_s8_buffer+.10,sensor3_ppO2
228 movff ir_s8_buffer+.11,hud_battery_mv+0 219 movff ir_s8_buffer+.11,hud_battery_mv+0
229 movff ir_s8_buffer+.12,hud_battery_mv+1 220 movff ir_s8_buffer+.12,hud_battery_mv+1
230 221
231 movlw ir_timeout_value ; multiples of 62.5 ms 222 bsf hud_connection_ok ; set manually for hwHUD w/o the HUD module
223 bra isr_timer3_reload ; reload timer and exit
224
225 ; Process telegram received on S8 link
226 isr_timer3_s8:
227 movlw .14 ; checksum shall be computed for 1st and next 14 bytes
228 rcall compute_IR_S8_checksum ; compute checksum
229
230 movf ir_s8_buffer+.15,W ; get low byte of the received checksum
231 cpfseq isr_mpr+0 ; = low byte of the calculated local checksum?
232 bra isr_timer3_exit ; NO - exit
233 movf ir_s8_buffer+.16,W ; get high byte of the received checksum
234 cpfseq isr_mpr+1 ; = high byte of the calculated local checksum?
235 bra isr_timer3_exit ; NO - exit
236 ; YES to both - received telegram valid, copy data
237 movff ir_s8_buffer+.3, hud_status_byte
238 movff ir_s8_buffer+.13,hud_battery_mv+0
239 movff ir_s8_buffer+.14,hud_battery_mv+1
240
241 btfsc trigger_S8_data_update ; last data already processed?
242 bra isr_timer3_reload ; NO - skip copying new results
243 bsf trigger_S8_data_update ; YES - set flag for new data available
244 ; - copy more data
245
246 movff ir_s8_buffer+.4, s8_rawdata_sensor1+0
247 movff ir_s8_buffer+.5, s8_rawdata_sensor1+1
248 movff ir_s8_buffer+.6, s8_rawdata_sensor1+2
249 movff ir_s8_buffer+.7, s8_rawdata_sensor2+0
250 movff ir_s8_buffer+.8, s8_rawdata_sensor2+1
251 movff ir_s8_buffer+.9, s8_rawdata_sensor2+2
252 movff ir_s8_buffer+.10,s8_rawdata_sensor3+0
253 movff ir_s8_buffer+.11,s8_rawdata_sensor3+1
254 movff ir_s8_buffer+.12,s8_rawdata_sensor3+2
255
256 isr_timer3_reload:
257 movlw ir_timeout_value ; in multiples of 62.5 ms
232 movwf ir_s8_timeout ; reload timeout 258 movwf ir_s8_timeout ; reload timeout
233
234 banksel hud_status_byte
235 bsf hud_connection_ok ; set manually for hwHUD w/o the HUD module...
236 banksel isr_backup ; select bank 0 for ISR data
237
238 isr_timer3_exit: 259 isr_timer3_exit:
239 clrf ir_s8_counter ; clear pointer 260 clrf ir_s8_counter ; clear pointer
240 bcf PIR2,TMR3IF ; clear flag 261 bcf PIR2,TMR3IF ; clear flag
241 return 262 return
242 263
243 isr_timer3_checksum: 264
244 addwf PRODL,F 265 ; compute checksum on data in RX buffer
245 movlw .0 266 ;
246 addwfc PRODH,F 267 compute_IR_S8_checksum:
247 return 268 movwf ir_s8_counter ; initialize loop counter from WREG
248 269 movff ir_s8_buffer+0,isr_mpr+0 ; initialize low byte of the local checksum with first byte in buffer
249 isr_timer3_s8: ; S8 input 270 clrf isr_mpr+1 ; clear the high byte of the local checksum
250 movff ir_s8_buffer+.0,PRODL 271 lfsr FSR0,ir_s8_buffer ; load base address of the buffer
251 clrf PRODH 272 compute_IR_S8_checksum_loop:
252 movf ir_s8_buffer+.1,W 273 movf PREINC0,W ; get next byte
253 rcall isr_timer3_checksum 274 addwf isr_mpr+0,F ; add it to the to checksum, low byte
254 movf ir_s8_buffer+.2,W 275 movlw .0 ; no explicit data to add to the high byte...
255 rcall isr_timer3_checksum 276 addwfc isr_mpr+1,F ; ... besides the carry
256 movf ir_s8_buffer+.3,W 277 decfsz ir_s8_counter ; decrement number of bytes yet to do, all done?
257 rcall isr_timer3_checksum 278 bra compute_IR_S8_checksum_loop ; NO - loop
258 movf ir_s8_buffer+.4,W 279 return ; YES - done
259 rcall isr_timer3_checksum 280
260 movf ir_s8_buffer+.5,W 281 ENDIF ; _external_sensor
261 rcall isr_timer3_checksum 282
262 movf ir_s8_buffer+.6,W 283 ;=============================================================================
263 rcall isr_timer3_checksum 284
264 movf ir_s8_buffer+.7,W 285 ;=============================================================================
265 rcall isr_timer3_checksum 286 ; Tasks every 62.5 ms: buttons, dimming, pressure/temp sensor and CPU speed
266 movf ir_s8_buffer+.8,W 287 ;
267 rcall isr_timer3_checksum 288 isr_tmr7:
268 movf ir_s8_buffer+.9,W
269 rcall isr_timer3_checksum
270 movf ir_s8_buffer+.10,W
271 rcall isr_timer3_checksum
272 movf ir_s8_buffer+.11,W
273 rcall isr_timer3_checksum
274 movf ir_s8_buffer+.12,W
275 rcall isr_timer3_checksum
276 movf ir_s8_buffer+.13,W
277 rcall isr_timer3_checksum
278 movf ir_s8_buffer+.14,W
279 rcall isr_timer3_checksum
280
281 ; Compare checksum
282 movf ir_s8_buffer+.15,W
283 cpfseq PRODL ; checksum ok?
284 bra isr_timer3_exit ; NO - exit
285 movf ir_s8_buffer+.16,W
286 cpfseq PRODH ; checksum ok?
287 bra isr_timer3_exit ; NO - exit
288
289 ; Checksum OK, copy results
290 movff ir_s8_buffer+.3,hud_status_byte
291 movff ir_s8_buffer+.13,hud_battery_mv+0
292 movff ir_s8_buffer+.14,hud_battery_mv+1
293
294 banksel common
295 btfsc new_s8_data_available ; =1: old data already processed?
296 bra isr_timer3_skip ; NO - skip copying new results
297
298 movff ir_s8_buffer+.6,s8_rawdata_sensor1+2
299 movff ir_s8_buffer+.5,s8_rawdata_sensor1+1
300 movff ir_s8_buffer+.4,s8_rawdata_sensor1+0
301 movff ir_s8_buffer+.9,s8_rawdata_sensor2+2
302 movff ir_s8_buffer+.8,s8_rawdata_sensor2+1
303 movff ir_s8_buffer+.7,s8_rawdata_sensor2+0
304 movff ir_s8_buffer+.12,s8_rawdata_sensor3+2
305 movff ir_s8_buffer+.11,s8_rawdata_sensor3+1
306 movff ir_s8_buffer+.10,s8_rawdata_sensor3+0
307 banksel common
308 bsf new_s8_data_available ; set flag
309
310 isr_timer3_skip:
311 banksel ir_s8_timeout
312 movlw ir_timeout_value ; multiples of 62.5ms
313 movwf ir_s8_timeout ; reload timeout
314 bra isr_timer3_exit ; exit
315
316
317 ;=============================================================================
318
319 isr_tmr7: ; each 62.5ms
320 bcf PIR5,TMR7IF ; clear flag 289 bcf PIR5,TMR7IF ; clear flag
321 banksel 0xF16 ; addresses F16h through F5Fh, are also used by SFRs, but are not part of the Access RAM 290 movlw .248 ; rollover after 248 cycles -> 62.5 ms
322 movlw .248 291 movff WREG,TMR7H ; timer 7 is outside access RAM
323 movwf TMR7H ; rollover after 2048 cycles -> 62.5ms 292
324 293 call get_analog_switches ; get analog readings, CAUTION: returns in bank common
325 banksel common 294 banksel isr_backup ;back to ISR default bank
326 call get_analog_switches ; get analog readings 295
327 btfss INTCON3,INT1IE 296 btfss INTCON3,INT1IE
328 bra isr_tmr7_a 297 bra isr_tmr7_a
329 btfsc analog_sw2_pressed 298 btfsc analog_sw2_pressed
330 rcall isr_switch_left 299 rcall isr_switch_left ; get digital readings of left switch
331 isr_tmr7_a: 300 isr_tmr7_a:
332 banksel common
333 btfss INTCON,INT0IE 301 btfss INTCON,INT0IE
334 bra isr_tmr7_b 302 bra isr_tmr7_b
335 btfsc analog_sw1_pressed 303 btfsc analog_sw1_pressed
336 rcall isr_switch_right 304 rcall isr_switch_right ; get digital readings of right switch
337 isr_tmr7_b: 305 isr_tmr7_b:
338 banksel common 306 btfss block_sensor_interrupt ; sensor interrupts disabled?
339 btfss no_sensor_int ; sensor interrupt (because it's addressed during sleep)?
340 bra isr_tmr7_c ; NO - continue 307 bra isr_tmr7_c ; NO - continue
341 banksel isr_backup ; YES - back to bank 0 ISR data 308 return ; YES - done
342 return
343 isr_tmr7_c: 309 isr_tmr7_c:
344 banksel isr_backup 310 movf max_CCPR1L,W ; dim value
345 movf max_CCPR1L,W ; dimm value
346 cpfseq CCPR1L ; = current PWM value? 311 cpfseq CCPR1L ; = current PWM value?
347 rcall isr_dimm_tft ; NO - adjust until max_CCPR1L=CCPR1L 312 rcall isr_dimm_tft ; NO - adjust until max_CCPR1L = CCPR1L
348 313
349 banksel isr_backup 314 IFDEF _external_sensor
350 decfsz ir_s8_timeout,F ; IR data still valid? 315 decfsz ir_s8_timeout,F ; IR / S8 digital data still valid?
351 bra isr_tmr7_2 ; YES - continue 316 bra isr_tmr7_2 ; YES - continue
352 ; timeout, clear IR-Data 317 movlw ir_timeout_value ; NO - get timer reload in multiples of 62.5 ms
353 318 movwf ir_s8_timeout ; - reload the timer
354 movlw ir_timeout_value ; multiples of 62.5ms 319 btfss analog_o2_input ; - analog input available?
355 movwf ir_s8_timeout ; reload timeout 320 bra isr_tmr7_1a ; NO - clear data
356 321 btfss s8_digital_avail ; YES - S8 digital interface available?
357 banksel common 322 bra isr_tmr7_2 ; NO - must be analog interface in use, keep data
358 btfss analog_o2_input
359 bra isr_tmr7_1a ; always with normal ostc3 hardware
360 btfss s8_digital
361 bra isr_tmr7_2 ; only when digital
362 isr_tmr7_1a: 323 isr_tmr7_1a:
363 clrf o2_mv_sensor1+0 ; S8/IR timeout clears all analog input readings to zero -> fallback will be triggered when sensor mode was used 324 clrf hud_status_byte ; S8/IR timeout clears all analog input readings to zero -> fallback will be triggered when in sensor mode
364 clrf o2_mv_sensor1+1 325 CLRI hud_battery_mv ; clear battery voltage
365 clrf o2_mv_sensor2+0 326
366 clrf o2_mv_sensor2+1 327 banksel sensor1_mv ; select bank where sensor data are stored
367 clrf o2_mv_sensor3+0 328 CLRI sensor1_mv ; clear all sensor data
368 clrf o2_mv_sensor3+1 329 CLRI sensor2_mv ; ...
369 banksel hud_battery_mv 330 CLRI sensor3_mv ; ...
370 clrf hud_battery_mv+0 331 clrf sensor1_ppO2 ; ...
371 clrf hud_battery_mv+1 332 clrf sensor2_ppO2 ; ...
372 banksel hud_status_byte 333 clrf sensor3_ppO2 ; ...
373 clrf hud_status_byte 334 banksel isr_backup ; back to ISR default bank
374 clrf o2_ppo2_sensor1 ; for IR/S8 UD 335
375 clrf o2_ppo2_sensor2 336 bsf trigger_S8_data_update ; signal a data update
376 clrf o2_ppo2_sensor3 337 ENDIF
377
378 banksel common
379 bsf new_s8_data_available ; set flag to update in surface mode
380 338
381 isr_tmr7_2: 339 isr_tmr7_2:
382 banksel common 340 btfsc block_sensor_interrupt ; sensor interrupts disabled?
383 btfss no_sensor_int ; sensor interrupt (because it's addressed during sleep)? 341 return ; YES - abort
384 bra isr_sensor_state2 ; NO - continue
385 banksel isr_backup ; YES - back to Bank0 ISR data
386 return
387 342
388 isr_sensor_state2: 343 isr_sensor_state2:
389 banksel common 344 btfss sensor_state_counter,0 ; every 1/4 second
390 movff sensor_state_counter,WREG 345 bsf trigger_quarter_second ; set flag
391 btfss WREG,0 ; every 1/4 second 346
392 bsf quarter_second_update ; set flag 347 btfss speed_is_normal ; CPU running on normal speed?
393 banksel isr_backup ; back to Bank0 ISR data 348 rcall isr_set_speed_to_normal ; NO - set CPU speed to normal
394 movlw d'2' ; coding for normal speed 349
395 cpfseq cpu_speed_state ; CPU running on normal speed? 350 ; update surface pressure
396 rcall isr_set_speed_to_normal ; NO - set CPU speed to normal 351 btfss update_surface_pressure ; shall update the surface pressure?
397 352 bra isr_sensor_state2_1 ; NO
398 incf sensor_state_counter,F ; counts to eight for state machine 353 bcf update_surface_pressure ; YES - clear request flag
399 354 MOVII pressure_abs_ref,pressure_surf ; - update surface pressure
400 ; State 1: Clear flags and average registers, get temperature (51 us) and start pressure integration (73.5 us) 355
401 ; State 2: Get pressure (51 us), start temperature integration (73.5 us) and calculate temperature compensated pressure (233 us) 356 isr_sensor_state2_1:
402 ; State 3: Get temperature (51 us) and start pressure integration (73.5 us) 357 incf sensor_state_counter,F ; counts to eight for state machine
403 ; State 4: Get pressure (51 us), start temperature integration (73.5 us) and calculate temperature compensated pressure (233 us) 358
404 ; State 5: Get temperature (51 us) and start pressure integration (73.5 us) 359 ; State 1: clear flags and average registers, get temperature (51 us) and start pressure integration (73.5 us)
405 ; State 6: Get pressure (51 us), start temperature integration (73.5 us) and calculate temperature compensated pressure (233 us) 360 ; State 2: get pressure (51 us), start temperature integration (73.5 us) and calculate temperature compensated pressure (233 us)
406 ; State 7: Get temperature (51 us) and start pressure integration (73.5 us) 361 ; State 3: get temperature (51 us) and start pressure integration (73.5 us)
407 ; State 8: Get pressure (51 us), start temperature integration (73.5 us), calculate temperature compensated pressure (233 us) and build average for half-second update of temperature and pressure 362 ; State 4: get pressure (51 us), start temperature integration (73.5 us) and calculate temperature compensated pressure (233 us)
408 363 ; State 5: get temperature (51 us) and start pressure integration (73.5 us)
409 movff sensor_state_counter,WREG ; WREG used as temp here... 364 ; State 6: get pressure (51 us), start temperature integration (73.5 us) and calculate temperature compensated pressure (233 us)
365 ; State 7: get temperature (51 us) and start pressure integration (73.5 us)
366 ; State 8: get pressure (51 us), start temperature integration (73.5 us) and calculate temperature compensated pressure (233 us) and build average for half-second update of temperature and pressure
367
368 movff sensor_state_counter,WREG ; WREG used as temp here...
410 dcfsnz WREG,F 369 dcfsnz WREG,F
411 bra sensor_int_state1_plus_restart ; do State 1 370 bra sensor_int_state1_plus_restart ; do state 1
412 dcfsnz WREG,F 371 dcfsnz WREG,F
413 bra sensor_int_state2 ; do State 2 372 bra sensor_int_state2 ; do state 2
414 dcfsnz WREG,F 373 dcfsnz WREG,F
415 bra sensor_int_state1 ; do State 3 374 bra sensor_int_state1 ; do state 3
416 dcfsnz WREG,F 375 dcfsnz WREG,F
417 bra sensor_int_state2 ; do State 4 376 bra sensor_int_state2 ; do state 4
418 dcfsnz WREG,F 377 dcfsnz WREG,F
419 bra sensor_int_state1 ; do State 5 378 bra sensor_int_state1 ; do state 5
420 dcfsnz WREG,F 379 dcfsnz WREG,F
421 bra sensor_int_state2 ; do State 6 380 bra sensor_int_state2 ; do state 6
422 dcfsnz WREG,F 381 dcfsnz WREG,F
423 bra sensor_int_state1 ; do State 7 382 bra sensor_int_state1 ; do state 7
424 ; bra sensor_int2_plus_average ; do State 8 383
425 ;sensor_int2_plus_average: 384 ; first, do state 2:
426 ; First, do state2: 385 call get_pressure_value ; state 2: get pressure (51 us)
427 call get_pressure_value ; state 2: get pressure (51 us) 386 call get_temperature_start ; and start temperature integration (73.5 us)
428 call get_temperature_start ; and start temperature integration (73.5 us) 387 call calculate_compensation ; calculate temperature compensated pressure (27 us)
429 call calculate_compensation ; calculate temperature compensated pressure (27 us) 388
430 ; Build average 389 ; build average for pressure
431 bcf STATUS,C ; clear carry bit 390 bcf STATUS,C ; clear carry bit
432 rrcf amb_pressure_avg+1 ; amb_pressure sum / 2 391 rrcf pressure_abs_avg+1 ; divide by 2
433 rrcf amb_pressure_avg+0 392 rrcf pressure_abs_avg+0
434 bcf STATUS,C ; clear carry bit, twice 393 bcf STATUS,C ; clear carry bit
435 rrcf amb_pressure_avg+1 ; amb_pressure sum / 4 394 rrcf pressure_abs_avg+1 ; divide by 2, again
436 rrcf amb_pressure_avg+0 395 rrcf pressure_abs_avg+0
437 396
438 movff amb_pressure_avg+1,amb_pressure+1 ; copy into actual register 397 ; copy into result register
439 movff amb_pressure_avg+0,amb_pressure+0 398 MOVII pressure_abs_avg,pressure_abs
440 399
441 bcf STATUS,C 400 ; build average for temperature
442 btfsc temperature_avg+1,7 ; copy sign bit to carry 401 bcf STATUS,C ; clear carry bit by default
443 bsf STATUS,C 402 btfsc temperature_avg+1,7 ; sign bit set?
444 rrcf temperature_avg+1 ; signed temperature /2 403 bsf STATUS,C ; YES - copy sign bit to carry bit
445 rrcf temperature_avg+0 404 rrcf temperature_avg+1 ; divide signed temperature by 2
446 bcf STATUS,C 405 rrcf temperature_avg+0 ; ...
447 btfsc temperature_avg+1,7 ; copy sign bit to carry 406 bcf STATUS,C ; clear carry bit by default
448 bsf STATUS,C 407 btfsc temperature_avg+1,7 ; sign bit set?
449 rrcf temperature_avg+1 ; signed temperature /4 408 bsf STATUS,C ; YES - copy sign bit to carry bit
450 rrcf temperature_avg+0 409 rrcf temperature_avg+1 ; divide signed temperature by 2 again (by 4 in total now)
451 410 rrcf temperature_avg+0 ; ...
452 movff temperature_avg+1,temperature+1 ; copy into actual register 411 MOVII temperature_avg,temperature_cur ; store final result
453 movff temperature_avg+0,temperature+0 412
454 413 ; check for temperature change
455 banksel common ; flag1 is in bank 1 414 movf temperature_cur+0,W ; get current temperature, low byte
456 bcf temp_changed ; clear flag for temperature update 415 cpfseq temperature_last+0 ; compare with last temperature, equal?
457 bcf pressure_refresh ; clear flag for pressure update 416 bra isr_sensor_state2_2 ; NO - temperature has changed
458 banksel isr_backup ; back to bank 0 ISR data 417 movf temperature_cur+1,W ; get current temperature, high byte
459 418 cpfseq temperature_last+1 ; compare with last temperature, equal?
460 ; Temp changed? 419 bra isr_sensor_state2_2 ; NO - temperature has changed
461 movf temperature+0,W 420 bra isr_sensor_state2_3 ; YES to both - no change
462 cpfseq last_temperature+0
463 bra isr_sensor_state2_2 ; YES
464 movf temperature+1,W
465 cpfseq last_temperature+1
466 bra isr_sensor_state2_2 ; YES
467
468 bra isr_sensor_state2_3 ; no change
469 421
470 isr_sensor_state2_2: 422 isr_sensor_state2_2:
471 banksel common ; flag1 is in bank 1 423 MOVII temperature_cur,temperature_last ; store current temperature as last temperature for next round
472 bsf temp_changed ; YES 424 bsf trigger_temp_changed ; set flag for temperature change
473 banksel isr_backup ; back to bank 0 ISR data 425
474 isr_sensor_state2_3: 426 isr_sensor_state2_3:
475 movff temperature+0,last_temperature+0 ; copy for compare 427 ; reset state counter and set update flag
476 movff temperature+1,last_temperature+1 428 clrf sensor_state_counter ; reset state counter
477 429 bsf trigger_pres_update ; signal a pressure update
478 ; movf amb_pressure+0,W 430 btfss reset_max_pressure ; shall clear the max pressure?
479 ; cpfseq last_pressure+0 431 bra isr_sensor_state2_3a ; NO - continue with checking for pressure change
480 ; bra isr_sensor_state2_4 ; YES 432 bcf reset_max_pressure ; YES - clear request flag
481 ; movf amb_pressure+1,W 433 CLRI pressure_rel_max ; - clear max. pressure
482 ; cpfseq last_pressure+1 434
483 ; bra isr_sensor_state2_4 ; YES 435 isr_sensor_state2_3a:
484 ; 436 ; check for pressure change
485 ; bra isr_sensor_state2_5 ; no change 437 movf pressure_abs+0,W ; get current pressure, low byte
486 ;isr_sensor_state2_4: 438 cpfseq pressure_abs_last+0 ; compare with last pressure, equal?
487 banksel common ; flag1 is in bank 1 439 bra isr_sensor_state2_4 ; NO - pressure has changed
488 bsf pressure_refresh ; Always set this flag 440 movf pressure_abs+1,W ; YES - get current pressure, high byte
489 banksel isr_backup ; back to bank 0 ISR data 441 cpfseq pressure_abs_last+1 ; - compare with last pressure, equal?
490 ;isr_sensor_state2_5: 442 bra isr_sensor_state2_4 ; NO - pressure has changed
491 ; movff amb_pressure+0,last_pressure+0 ; copy for compare 443 bra isr_sensor_state2_5 ; YES - no change
492 ; movff amb_pressure+1,last_pressure+1 444
493 445 isr_sensor_state2_4:
494 clrf sensor_state_counter ; reset state counter 446 MOVII pressure_abs,pressure_abs_last ; store current pressure as last pressure for next round
495 banksel common ; flag2 is in bank 1 447 bsf trigger_pres_cur_changed ; signal a pressure change
496 btfss simulatormode_active ; are we in simulator mode? 448
497 bra comp_air_pressure ; NO 449 isr_sensor_state2_5:
498 bsf pressure_refresh ; always set pressure_refresh flag in simulator mode 450 ; compute relative pressure
499 banksel isr_backup ; back to bank 0 ISR data 451 movf pressure_surf+0,W ; get surface pressure, low byte
500 movlw LOW d'1000' ; simulate 1000 mbar surface pressure 452 subwf pressure_abs+0,W ; WREG = pressure_abs - pressure_surf (low byte)
501 movwf last_surfpressure+0 453 movwf pressure_rel_cur+0 ; store relative pressure, low byte
502 movlw HIGH d'1000' 454 movf pressure_surf+1,W ; get surface pressure, high byte
503 movwf last_surfpressure+1 455 subwfb pressure_abs+1,W ; WREG = pressure_abs - pressure_surf (high byte)
504 456 movwf pressure_rel_cur+1 ; store relative pressure, high byte
505 comp_air_pressure: 457 btfss STATUS,N ; relative pressure < 0 ?
506 banksel isr_backup ; back to bank 0 ISR data 458 bra isr_sensor_state2_6 ; NO - OK, keep result
507 movf last_surfpressure+0,W ; compensate air pressure 459 CLRI pressure_rel_cur ; YES - set relative pressure to zero
508 subwf amb_pressure+0,W 460
509 movwf rel_pressure+0 ; rel_pressure stores depth 461 isr_sensor_state2_6:
510 462 ; check for new max relative pressure
511 movf last_surfpressure+1,W 463 movf pressure_rel_cur+0,W ; get current relative pressure, low byte
512 subwfb amb_pressure+1,W 464 subwf pressure_rel_max+0,W ; WREG = pressure_rel_max - pressure_rel_cur (low byte)
513 movwf rel_pressure+1 465 movf pressure_rel_cur+1,W ; get current relative pressure, high byte
514 btfss STATUS,N ; is result below zero? 466 subwfb pressure_rel_max+1,W ; WREG = pressure_rel_max - pressure_rel_cur (high byte)
515 bra sensor_int_state_exit ; NO 467 btfss STATUS,N ; result < 0, i.e. new max rel pressure?
516 clrf rel_pressure+0 ; YES - do not display negative depths 468 bra isr_sensor_state2_6a ; NO
517 clrf rel_pressure+1 ; e.g. when surface air pressure dropped during the dive 469 MOVII pressure_rel_cur,pressure_rel_max ; YES - set new max rel pressure
518 bra sensor_int_state_exit 470 bsf trigger_pres_max_changed ; - signal a pressure max change
471
472 isr_sensor_state2_6a:
473
474 IFDEF _min_depth_option
475 ; check if min/max pressures shall be reset
476 btfss reset_trip_pressure ; shall reset the resettable min/max pressures?
477 bra isr_sensor_state2_6b ; NO
478 bcf reset_trip_pressure ; YES - clear request flag
479 CLRI pressure_rel_max_trip ; - set max pressure to zero
480 SETI pressure_rel_min_trip ; - set min pressure to biggest value possible
481 isr_sensor_state2_6b:
482 ; check for new resettable max relative pressure
483 movf pressure_rel_cur+0,W ; get current relative pressure, low byte
484 subwf pressure_rel_max_trip+0,W ; WREG = pressure_rel_max - pressure_rel_cur (low byte)
485 movf pressure_rel_cur+1,W ; get current relative pressure, high byte
486 subwfb pressure_rel_max_trip+1,W ; WREG = pressure_rel_max - pressure_rel_cur (high byte)
487 btfss STATUS,N ; result < 0, i.e. new max rel pressure?
488 bra isr_sensor_state2_6c ; NO - continue checking for min depth
489 MOVII pressure_rel_cur,pressure_rel_max_trip ; YES - set new max rel pressure
490 isr_sensor_state2_6c:
491 ; check for new resettable min relative pressure
492 movf pressure_rel_cur+0,W ; get current relative pressure, low byte
493 subwf pressure_rel_min_trip+0,W ; WREG = pressure_rel_min - pressure_rel_cur (low byte)
494 movf pressure_rel_cur+1,W ; get current relative pressure, high byte
495 subwfb pressure_rel_min_trip+1,W ; WREG = pressure_rel_min - pressure_rel_cur (high byte)
496 btfss STATUS,C ; result > 0, i.e. new min rel pressure?
497 bra sensor_int_state_exit ; NO - done
498 MOVII pressure_rel_cur,pressure_rel_min_trip ; YES - set new min rel pressure
499 ENDIF ; _min_depth_option
500 bra sensor_int_state_exit ; done
501
519 502
520 sensor_int_state1_plus_restart: 503 sensor_int_state1_plus_restart:
521 clrf amb_pressure_avg+0 ; pressure average registers 504 ; clear average registers
522 clrf amb_pressure_avg+1 505 CLRI pressure_abs_avg
523 clrf temperature_avg+0 506 CLRI temperature_avg
524 clrf temperature_avg+1
525 507
526 sensor_int_state1: 508 sensor_int_state1:
527 call get_temperature_value ; state 1: get temperature... 509 call get_temperature_value ; state 1: get temperature...
528 call get_pressure_start ; ...and start pressure integration 510 call get_pressure_start ; ...and start pressure integration
529 bra sensor_int_state_exit 511 bra sensor_int_state_exit
530 512
531 sensor_int_state2: 513 sensor_int_state2:
532 call get_pressure_value ; state 2: get pressure (51 us)... 514 call get_pressure_value ; state 2: get pressure (51 us)...
533 call get_temperature_start ; ...and start temperature integration (73.5 us) 515 call get_temperature_start ; ...and start temperature integration (73.5 us)
534 call calculate_compensation ; .. and calculate temperature compensated pressure (233 us) 516 call calculate_compensation ; .. and calculate temperature compensated pressure (233 us)
535 ;bra sensor_int_state_exit
536 517
537 sensor_int_state_exit: 518 sensor_int_state_exit:
538 rcall isr_restore_clock ; restore clock 519 bra isr_adjust_speed ; set/restore CPU speed and return
539 return 520
540 521
541 ;============================================================================= 522 isr_dimm_tft: ; adjust until max_CCPR1L = CCPR1L
542 523 btfsc tft_is_dimming ; is the display dimming?
543 isr_rtcc: ; each second 524 return ; YES - ignore
525 movf max_CCPR1L,W ; NO - proceed
526 cpfsgt CCPR1L ; CCPR1L > max_CCPR1L ?
527 bra isr_dimm_tft2 ; NO - dim up
528 decf CCPR1L,F ; YES - dim down
529 return ; - done
530 isr_dimm_tft2:
531 movf max_CCPR1L,W
532 sublw ambient_light_min_eco
533 cpfsgt CCPR1L ; CCPR1L > max_CCPR1L - ambient_light_min_eco ?
534 bra isr_dimm_tft3 ; NO - dim up slow
535 movlw .10 ; YES - dim up faster
536 addwf CCPR1L,F
537 isr_dimm_tft3:
538 incf CCPR1L,F ; +1
539 return ; done
540
541
542 ;=============================================================================
543 ; RTC interrupt on every 1/2 second
544 ;
545 isr_rtcc:
544 bcf PIR3,RTCCIF ; clear flag 546 bcf PIR3,RTCCIF ; clear flag
545 banksel 0xF16 ; addresses, F16h through F5Fh, are also used by SFRs, but are not part of the access RAM 547 bsf trigger_half_second ; set flag for a new 1/2 second has begun
548 btfsc reset_timebase ; shall reset the timebase?
549 bra isr_rtcc_1 ; YES - warp to new full second
550 btg timebase_0sec ; NO - toggle the 1/2 second timebase
551 btfsc timebase_0sec ; - did it toggled 1 -> 0 ?
552 return ; NO - on half second, done
553
554 isr_rtcc_1:
555 ; new full second
556 bsf trigger_full_second ; set flag for a new 1/1 second has begun
557
558 btfsc block_rtc_access ; ISR suspended from accessing the RTC?
559 bra isr_rtcc_2 ; YES
560
561 banksel RTCCFG ; RTC registers are outside access RAM
546 bsf RTCCFG,RTCPTR1 562 bsf RTCCFG,RTCPTR1
547 bsf RTCCFG,RTCPTR0 ; year 563 bsf RTCCFG,RTCPTR0
548 movff RTCVALL,year ; format is BCD 564 banksel isr_backup ; back to ISR default bank
549 movff RTCVALH,day ; dummy read 565
550 movff RTCVALL,day ; format is BCD 566 movff RTCVALL,rtc_year ; read year in BCD
551 movff RTCVALH,month ; format is BCD 567 movff RTCVALH,rtc_day ; dummy read
552 movff RTCVALL,hours ; format is BCD 568 movff RTCVALL,rtc_day ; read day in BCD
553 movff RTCVALH,secs ; format is BCD 569 movff RTCVALH,rtc_month ; read month in BCD
554 movff RTCVALL,secs ; format is BCD 570 movff RTCVALL,rtc_hour ; read hour in BCD
555 movff RTCVALH,mins ; format is BCD 571 movff RTCVALH,rtc_secs ; dummy read
556 banksel isr_backup ; back to bank 0 ISR data 572 movff RTCVALL,rtc_secs ; read seconds in BCD
573 movff RTCVALH,rtc_mins ; read minutes in BCD
557 574
558 ; Convert BCD to DEC and set registers 575 ; Convert BCD to DEC and set registers
559 movff mins, isr1_temp 576 movf rtc_mins,W
560 rcall isr_rtcc_convert ; converts to dec with result in WREG 577 rcall isr_rtcc_convert_BCD_DEC ; convert to decimal with result in WREG
561 movff WREG,mins 578 movwf rtc_mins
562 movff secs, isr1_temp 579 movf rtc_secs,W
563 rcall isr_rtcc_convert ; converts to dec with result in WREG 580 rcall isr_rtcc_convert_BCD_DEC ; convert to decimal with result in WREG
564 movff WREG,secs 581 movwf rtc_secs
565 movff hours, isr1_temp 582 movf rtc_hour,W
566 rcall isr_rtcc_convert ; converts to dec with result in WREG 583 rcall isr_rtcc_convert_BCD_DEC ; convert to decimal with result in WREG
567 movff WREG,hours 584 movwf rtc_hour
568 movff month, isr1_temp 585 movf rtc_month,W
569 rcall isr_rtcc_convert ; converts to dec with result in WREG 586 rcall isr_rtcc_convert_BCD_DEC ; convert to decimal with result in WREG
570 movff WREG,month 587 movwf rtc_month
571 movff day, isr1_temp 588 movf rtc_day,W
572 rcall isr_rtcc_convert ; converts to dec with result in WREG 589 rcall isr_rtcc_convert_BCD_DEC ; convert to decimal with result in WREG
573 movff WREG,day 590 movwf rtc_day
574 movff year, isr1_temp 591 movf rtc_year,W
575 rcall isr_rtcc_convert ; converts to dec with result in WREG 592 rcall isr_rtcc_convert_BCD_DEC ; convert to decimal with result in WREG
576 movff WREG,year 593 movwf rtc_year
577 594
578 ; Place once/second tasks for ISR here (Be sure of the right bank!) 595 isr_rtcc_2:
579 banksel common ; flag1 is in bank 1 596 ; get ambient light level and set max_CCPR1L
580 btfss sleepmode ; in sleepmode? 597 call get_ambient_level ; get ambient light level and set max_CCPR1L
581 call get_ambient_level ; NO - get ambient light level and set max_CCPR1L 598 banksel isr_backup ; back to ISR default bank (for safety only)
582 599
583 rcall isr_battery_gauge ; add amount of battery consumption to battery_gauge:6 600 rcall isr_battery_gauge ; calculate the current charge consumption and add it to the battery gauge
584 601 rcall isr_update_uptime ; increment overall OSTC uptime
585 ; update uptime 602 rcall isr_update_timeout ; process the timeout timer
586 banksel uptime+0 603
604 btfsc divemode ; in dive mode?
605 rcall isr_divemode_1sec ; YES - do the every second dive mode tasks
606
607 btfsc divemode ; in dive mode?
608 btfsc simulatormode ; YES - in simulator mode?
609 rcall isr_update_lastdive_time ; NO - YES - increment the last dive time
610
611 ; reset the timebase if requested
612 btfss reset_timebase ; shall reset the timebase?
613 bra isr_rtcc_3 ; NO
614 bcf reset_timebase ; YES - clear request flag
615 clrf eventbase ; - clear all pending events
616 clrf timebase ; - clear all timebase flags
617 clrf timebase_secs ; - clear seconds timer
618 clrf timebase_mins ; - clear minutes timer
619 clrf simulator_time ; - clear minutes timer of simulator runtime as well
620 bsf trigger_half_second ; - set flag for a new 1/2 second has begun
621 bsf trigger_full_second ; - set flag for a new 1/1 second has begun
622 return ; - done
623
624 isr_rtcc_3:
625 ; count-up the 2 seconds timebase timer
626 btg timebase_1sec ; toggle the 1 second timer bit
627 btfss timebase_1sec ; did it toggled 1 -> 0 ?
628 btg timebase_2sec ; YES - toggle the 2 seconds timer bit
629
630 ; count-up the seconds timer
631 incf timebase_secs,F ; increment seconds timer (may temporary become 60 here)
632 movlw .59 ; max. for seconds timer
633 cpfsgt timebase_secs ; seconds timer > max.?
634 return ; NO - done
635
636 ; new full minute
637 clrf timebase_secs ; YES - reset timer
638 bsf trigger_full_minute ; - set flag for a new minute has begun
639
640 btfsc divemode ; - in dive mode?
641 btfsc simulatormode ; YES - in simulator mode?
642 rcall inc_surface_interval ; NO - YES - increment surface interval
643
644 btfss simulatormode ; - in simulator mode?
645 bra isr_rtcc_4 ; NO
646 infsnz simulator_time,F ; YES - increment real runtime of the simulator, did wrap around (became zero)?
647 setf simulator_time ; YES - disallow wrap-around, keep at 255 instead
648
649 isr_rtcc_4:
650 incf timebase_mins,F ; - increment minutes timer
651 movlw .59 ; - max. for minutes timer
652 cpfsgt timebase_mins ; - minutes timer > max.?
653 return ; NO - done
654
655 ; new full hour
656 clrf timebase_mins ; YES - reset timer
657 bsf trigger_full_hour ; - set flag for a new hour has begun
658 return ; - done
659
660
661 ; increment overall OSTC uptime
662 isr_update_uptime:
587 incf uptime+0,F 663 incf uptime+0,F
588 movlw .0 664 clrf WREG
589 addwfc uptime+1,F 665 addwfc uptime+1,F
590 addwfc uptime+2,F 666 addwfc uptime+2,F
591 addwfc uptime+3,F 667 addwfc uptime+3,F
592 668 return
593 banksel common ; flag1 is in bank 1 669
594 bsf onesecupdate ; a new second has begun 670 ; increment time since last dive, called every second when not in dive mode (or when in simulator mode)
595 btfsc divemode ; in divemode? 671 isr_update_lastdive_time:
596 rcall isr_divemode_1sec ; YES - do some divemode stuff in bank common
597
598 btfss divemode ; in divemode?
599 rcall isr_update_lastdive_time ; NO - update the last dive timer
600
601 tstfsz secs ; secs == 0 ?
602 return ; NO - done
603
604 bsf oneminupdate ; a new minute has begun
605
606 btfss divemode ; in Divemode?
607 rcall check_nofly_desat_time ; NO - so increase interval
608
609 ; Check if a new hour has just begun
610 tstfsz mins ; mins == 0 ?
611 bra isr_rtcc2 ; NP
612 bsf onehourupdate ; YES - set flag
613
614 isr_rtcc2:
615 banksel isr_backup ; back to bank 0 ISR data
616 return ; done
617
618 isr_update_lastdive_time: ; called every second when not in divemode
619 ; update uptime
620 banksel lastdive_time+0
621 incf lastdive_time+0,F 672 incf lastdive_time+0,F
622 movlw .0 673 clrf WREG
623 addwfc lastdive_time+1,F 674 addwfc lastdive_time+1,F
624 addwfc lastdive_time+2,F 675 addwfc lastdive_time+2,F
625 addwfc lastdive_time+3,F 676 addwfc lastdive_time+3,F
626 banksel common 677 return
627 return 678
628 679 ; process the timeout timer
680 isr_update_timeout:
681 btfsc reset_timeout ; shall reset the timeout?
682 bra isr_update_timeout_1 ; YES
683 tstfsz isr_timeout_timer ; NO - timeout timer already at zero?
684 decfsz isr_timeout_timer ; NO - decrement timer, reached zero now?
685 return ; YES / NO - nothing further to do
686 bsf trigger_timeout ; YES - set timeout flag
687 return ; - done
688 isr_update_timeout_1:
689 bcf reset_timeout ; clear request flag
690 bcf trigger_timeout ; clear pending timeout trigger, if any
691 movff isr_timeout_reload,isr_timeout_timer ; reload timer
692 return ; done
693
694 ;=============================================================================
695 ; Calculate charge drawn from the battery
696 ;
629 isr_battery_gauge: 697 isr_battery_gauge:
630 banksel isr_backup ; bank 0 ISR data 698 btfsc block_battery_gauge ; access to battery gauge suspended?
631 movlw current_sleepmode ; 100µA/3600 -> nAs (sleepmode current) 699 return ; YES - done
632 movwf isr1_temp ; store value (low byte) 700 MOVLI current_sleepmode,isr_mpr ; NO - default to sleep mode with 100µA/3600 -> nAs
633 clrf isr2_temp ; high byte 701 btfss sleepmode ; - in sleep mode?
634 702 rcall isr_battery_gauge2 ; NO - compute current consumption value into isr_lo and isr_hi
635 banksel common ; flag1 is in bank 1 703 movf isr_mpr+0,W ; - 48 bit add of isr_mpr:2 with battery_gauge:6
636 btfss sleepmode ; in sleepmode?
637 rcall isr_battery_gauge2 ; NO - compute current consumption value into isr1_temp and isr2_temp
638
639 banksel isr_backup ; bank 0 ISR data
640 movf isr1_temp,W ; 48 Bit add of isr1_temp and isr2_temp into battery_gauge:6
641 addwf battery_gauge+0,F 704 addwf battery_gauge+0,F
642 movf isr2_temp,W 705 movf isr_mpr+1,W
643 addwfc battery_gauge+1,F 706 addwfc battery_gauge+1,F
644 movlw .0 707 clrf WREG
645 addwfc battery_gauge+2,F 708 addwfc battery_gauge+2,F
646 addwfc battery_gauge+3,F 709 addwfc battery_gauge+3,F
647 addwfc battery_gauge+4,F 710 addwfc battery_gauge+4,F
648 addwfc battery_gauge+5,F 711 addwfc battery_gauge+5,F
649 return 712 return
650 713
651 isr_battery_gauge2: 714 isr_battery_gauge2:
652 ; set consumption rate in nAs for an one second interval 715 ; set consumption rate in nAs - nano Ampere per second
653 ; Example: 716 ; Example:
654 ; movlw LOW .55556 ; 0,2A/3600*1e9s = nAs 717 ; MOVLI .55556,isr_mpr ; 0.2 Ah / 3600 seconds per hour * 1e9s = nAs
655 ; movwf isr1_temp ; low byte 718 ;
656 ; movlw HIGH .55556 ; 0,2A/3600*1e9s = nAs 719 ; Remark: although all the constants are named current_xxxx, in reality they mean charge!
657 ; movwf isr2_temp ; high byte 720
658 721 ; Calculate current consumption for LED backlight: 47*CCPR1L+272 (according to values in hwos.inc: 115*CCPR1L+216)
659 ; Current consumption for LED backlight is 47*CCPR1L+272 722 movf CCPR1L,W ; get CCPR1L into WREG
660 movf CCPR1L,W 723 mullw current_backlight_multi ; PRODH:PRODL = k * WREG = current_backlight_multi * CCPR1L
661 mullw current_backlight_multi 724 ADDLI current_backlight_offset,PRODL ; PRODH:PRODL += current_backlight_offset
662 movlw LOW current_backlight_offset 725 MOVII PRODL,isr_mpr ; copy result to isr_mpr
663 addwf PRODL,F 726
664 movlw HIGH current_backlight_offset 727 ; Add current for CPU and GPU
665 addwfc PRODH,F 728 ; cpu_speed_state = ECO 3.10 mA -> 861 nAs
666 movff PRODL,isr1_temp 729 ; = NORMAL 5.50 mA -> 1528 nAs
667 movff PRODH,isr2_temp ; isr1_temp and isr2_temp hold value for backlight 730 ; = FASTEST 8.04 mA -> 2233 nAs
668 731 btfss speed_is_eco ; speed = eco ?
669 ; Add current for CPU and GPU 732 bra isr_battery_gauge3 ; NO
670 ; cpu_speed_state=1: ECO (3.1mA -> 861nAs), =2: NORMAL (5.50mA -> 1528nAs) or =3: FASTEST (8.04mA -> 2233nAs) 733 ADDLI current_speed_eco,isr_mpr ; YES - add current_speed_eco to isr_mpr
671 banksel isr_backup ; Bank0 ISR data
672 movlw .1
673 cpfseq cpu_speed_state
674 bra isr_battery_gauge3
675 movlw LOW current_speed_eco
676 addwf isr1_temp,F
677 movlw HIGH current_speed_eco
678 addwfc isr2_temp,F
679 bra isr_battery_gauge5 734 bra isr_battery_gauge5
680 isr_battery_gauge3: 735 isr_battery_gauge3:
681 movlw .2 736 btfss speed_is_normal ; speed = normal?
682 cpfseq cpu_speed_state 737 bra isr_battery_gauge4 ; NO
683 bra isr_battery_gauge4 738 ADDLI current_speed_normal,isr_mpr ; YES - add current_speed_normal to isr_mpr
684 movlw LOW current_speed_normal
685 addwf isr1_temp,F
686 movlw HIGH current_speed_normal
687 addwfc isr2_temp,F
688 bra isr_battery_gauge5 739 bra isr_battery_gauge5
689 isr_battery_gauge4: 740 isr_battery_gauge4:
690 movlw LOW current_speed_fastest 741 ADDLI current_speed_fastest,isr_mpr ; speed is fastest, add current_speed_fastest to isr_mpr
691 addwf isr1_temp,F
692 movlw HIGH current_speed_fastest
693 addwfc isr2_temp,F
694 isr_battery_gauge5: 742 isr_battery_gauge5:
695 ; Add current if IR receiver is on 743 btfss ir_power ; IR enabled?
696 btfss ir_power ; IR enabled? 744 bra isr_battery_gauge6 ; NO
697 bra isr_battery_gauge6 ; NO 745 ADDLI current_ir_receiver,isr_mpr ; YES - add current_ir_receiver to isr_mpr
698 movlw LOW current_ir_receiver
699 addwf isr1_temp,F
700 movlw HIGH current_ir_receiver
701 addwfc isr2_temp,F
702 isr_battery_gauge6: 746 isr_battery_gauge6:
703 ; Add current for compass/accelerometer 747 btfss compass_enabled ; compass active?
704 btfss compass_enabled ; compass active? 748 bra isr_battery_gauge7 ; NO
705 bra isr_battery_gauge7 ; NO 749 ADDLI current_compass,isr_mpr ; YES - add current_compass to isr_mpr
706 movlw LOW current_compass
707 addwf isr1_temp,F
708 movlw HIGH current_compass
709 addwfc isr2_temp,F
710 isr_battery_gauge7: 750 isr_battery_gauge7:
711 return 751 return
712 752
753
754 ;=============================================================================
755 ; Every second tasks while in dive mode
756 ;
713 isr_divemode_1sec: 757 isr_divemode_1sec:
714 incf samplesecs,F ; "samplingrate" diving seconds done 758 decfsz sampling_timer,F ; decrement sampling timer, became zero?
715 decf samplesecs_value,W ; holds "samplingrate" value (minus 1 into WREG) 759 bra isr_divemode_1sec_1 ; NO
716 cpfsgt samplesecs ; done? 760 bsf trigger_sample_divedata ; YES - set trigger flag for sampling dive data
717 bra isr_divemode_1sec2 ; NO 761 movff sampling_rate,sampling_timer; - reload timer
718 762
719 clrf samplesecs ; clear counter... 763 isr_divemode_1sec_1:
720 bsf store_sample ; ...and set bit for profile storage 764 btfss reset_timebase ; shall reset the timebase? (request flag will be cleared later)
721 isr_divemode_1sec2: 765 bra isr_divemode_1sec_2 ; NO
722 ; increase total divetime (regardless of start_dive_threshold) 766 CLRI total_divetime_secs ; YES - reset total dive time, seconds (2 byte)
723 infsnz total_divetime_seconds+0,F 767 clrf counted_divetime_secs ; - reset counted dive time, seconds (1 byte)
724 incf total_divetime_seconds+1,F ; total dive time (regardless of start_dive_threshold) 768 CLRI counted_divetime_mins ; - reset counted dive time, minutes (2 byte)
725 769 clrf apnoe_dive_secs ; - reset apnoe dive time, seconds (1 byte)
726 btfss divemode2 ; displayed divetime is running? 770 clrf apnoe_dive_mins ; - reset apnoe dive time, minutes (1 byte)
727 return ; NO (e.g. too shallow) 771 bcf apnoe_at_surface ; - apnoe mode starts in submerged state
728 772 return ; - done
729 ; increase divetime registers (displayed dive time) 773
730 incf divesecs,F 774 isr_divemode_1sec_2:
731 movlw d'59' 775 INCI total_divetime_secs ; increase total dive time (regardless of start_dive_threshold)
732 cpfsgt divesecs 776 btfss count_divetime ; shall the dive time be counted (regarding start_dive_threshold)?
733 bra isr_divemode_1sec2a 777 bra isr_divemode_1sec_4 ; NO (too shallow / apnoe at surface)
734 778 incf counted_divetime_secs,F ; YES - increase dive time (displayed dive time)
735 clrf divesecs 779 movlw d'59' ; - 60 seconds make a minute
736 bsf realdive ; this bit is always set (again) if the dive is longer then one minute 780 cpfsgt counted_divetime_secs ; - next full minute reached?
737 infsnz divemins+0,F 781 bra isr_divemode_1sec_3 ; NO - done
738 incf divemins+1,F ; increase divemins 782 clrf counted_divetime_secs ; YES - reset seconds to 0
739 783 INCI counted_divetime_mins ; - increase dive minutes
740 isr_divemode_1sec2a: 784 bsf divetime_longer_1min ; - set flag for dive time exceeding 1 minute
741 btfss FLAG_apnoe_mode ; are we in apnoe mode? 785 ;bra isr_divemode_1sec_3 ; - done
742 return ; NO 786
743 787 isr_divemode_1sec_3: ; submerged
744 incf apnoe_secs,F ; increase descent registers 788 btfss FLAG_apnoe_mode ; in apnoe mode?
745 movlw d'59' 789 return ; NO - done
746 cpfsgt apnoe_secs ; full minute? 790 btfss apnoe_at_surface ; - been at surface before?
747 return ; NO 791 bra isr_divemode_1sec_3a ; NO - increment the dive time
748 clrf apnoe_secs 792 bcf apnoe_at_surface ; YES - a new dive has begun
749 incf apnoe_mins,F ; increase descent mins 793 bsf apnoe_new_dive ; - signal a new dive has begun
750 return 794 clrf apnoe_surface_secs ; - clear surface seconds
751 795 clrf apnoe_surface_mins ; - clear surface minutes
752 ;============================================================================= 796 clrf apnoe_dive_secs ; - clear dive seconds
753 ; BCD to Binary conversion. 797 clrf apnoe_dive_mins ; - clear dive minutes
754 ; Input: isr1_temp = Value in BCD 798 MOVII pressure_rel_cur,pressure_rel_max ; - reset max pressure to current pressure
755 ; Output WREG = value in binary. 799 bsf trigger_pres_max_changed ; - signal a new maximum pressure
756 isr_rtcc_convert: 800 isr_divemode_1sec_3a:
757 swapf isr1_temp, W 801 incf apnoe_dive_secs,F ; increment dive time, seconds
802 movlw d'59' ; 60 seconds make a minute
803 cpfsgt apnoe_dive_secs ; next full minute reached?
804 return ; NO - done
805 clrf apnoe_dive_secs ; YES - reset seconds to 0
806 incf apnoe_dive_mins,F ; - increment dive time, minutes
807 return ; - done
808
809 isr_divemode_1sec_4: ; at surface
810 btfss FLAG_apnoe_mode ; in apnoe mode?
811 return ; NO - done
812 bsf apnoe_at_surface ; YES - memorize been at the surface
813 incf apnoe_surface_secs,F ; - increment surface time, seconds
814 movlw d'59' ; - 60 seconds make a minute
815 cpfsgt apnoe_surface_secs ; - next full minute reached?
816 return ; NO - done
817 clrf apnoe_surface_secs ; YES - reset seconds to 0
818 incf apnoe_surface_mins,F ; - increment surface time, minutes
819 return ; - done
820
821
822
823
824
825
826 isr_divemode_1sec_3_dive:
827
828
829 ;=============================================================================
830 ; BCD to Binary conversion
831 ; Input WREG = value in BCD
832 ; Output WREG = value in binary
833 ;
834 isr_rtcc_convert_BCD_DEC:
835 movwf isr_lo
836 swapf isr_lo, W
758 andlw 0x0F ; W = tens 837 andlw 0x0F ; W = tens
759 rlncf WREG, W ; W = 2*tens 838 rlncf WREG, W ; W = 2 * tens
760 subwf isr1_temp, F ; 16*tens + ones - 2*tens 839 subwf isr_lo, F ; 16 * tens + ones - 2*tens
761 subwf isr1_temp, F ; 14*tens + ones - 2*tens 840 subwf isr_lo, F ; 14 * tens + ones - 2*tens
762 subwf isr1_temp, W ; 12*tens + ones - 2*tens 841 subwf isr_lo, W ; 12 * tens + ones - 2*tens
763 return 842 return
764 843
765 ;============================================================================= 844
766 845 ;=============================================================================
846 ; Check buttons
847 ;
767 isr_switch_right: 848 isr_switch_right:
768 bcf INTCON,INT0IE ; disable INT0 849 bcf INTCON,INT0IE ; disable INT0
769 banksel common ; flag1 is in bank 1
770 btfss flip_screen ; 180° flipped? 850 btfss flip_screen ; 180° flipped?
771 bsf switch_right ; set flag 851 bsf switch_right ; set flag
772 btfsc flip_screen ; 180° flipped? 852 btfsc flip_screen ; 180° flipped?
773 bsf switch_left ; set flag 853 bsf switch_left ; set flag
774 bra isr_switch_common ; continue... 854 bra isr_switch_common ; continue...
775 855
776 isr_switch_left: 856 isr_switch_left:
777 bcf INTCON3,INT1IE ; disable INT1 857 bcf INTCON3,INT1IE ; disable INT1
778 banksel common ; flag1 is in bank 1
779 btfss flip_screen ; 180° flipped? 858 btfss flip_screen ; 180° flipped?
780 bsf switch_left ; set flag 859 bsf switch_left ; set flag
781 btfsc flip_screen ; 180° flipped? 860 btfsc flip_screen ; 180° flipped?
782 bsf switch_right ; set flag 861 bsf switch_right ; set flag
862 ;bra isr_switch_common ; continue...
863
783 isr_switch_common: 864 isr_switch_common:
784 ; load timer1 for first press 865 clrf TMR1L ; load timer1 for first press
785 clrf TMR1L
786 movlw TMR1H_VALUE_FIRST ; in steps of 7.8125 ms 866 movlw TMR1H_VALUE_FIRST ; in steps of 7.8125 ms
787 movwf TMR1H 867 movwf TMR1H ; ...
788 bsf T1CON,TMR1ON ; start timer 1 868 bsf T1CON,TMR1ON ; start timer 1
789 banksel isr_backup ; select bank 0 for ISR data
790 bcf INTCON3,INT1IF ; clear flag 869 bcf INTCON3,INT1IF ; clear flag
791 bcf INTCON,INT0IF ; clear flag 870 bcf INTCON,INT0IF ; clear flag
792 return 871 return
793 872
873
874 ;=============================================================================
875 ; Button hold-down interrupt
876 ;
794 timer1int: 877 timer1int:
795 bcf PIR1,TMR1IF ; clear flag 878 bcf PIR1,TMR1IF ; clear flag
796 banksel common ; flag1 is in bank 1
797 bcf INTCON,INT0IF ; clear flag 879 bcf INTCON,INT0IF ; clear flag
798 bcf INTCON3,INT1IF ; clear flag 880 bcf INTCON3,INT1IF ; clear flag
799 ; digital 881 ; digital
800 btfss switch_left1 ; left button hold-down? 882 btfss switch_left1 ; left button hold-down?
801 bra timer1int_left ; YES 883 bra timer1int_left ; YES
802 btfss switch_right2 ; right button hold-down? 884 btfss switch_right2 ; right button hold-down?
803 bra timer1int_right ; YES 885 bra timer1int_right ; YES
804 886
805 ; Analog 887 ; analog
806 btfsc analog_sw2_pressed ; left button hold-down? 888 btfsc analog_sw2_pressed ; left button hold-down?
807 bra timer1int_left ; YES 889 bra timer1int_left ; YES
808 btfsc analog_sw1_pressed ; right button hold-down? 890 btfsc analog_sw1_pressed ; right button hold-down?
809 bra timer1int_right ; YES 891 bra timer1int_right ; YES
810 892
811 ; No button hold-down, stop Timer 1 893 ; no button hold-down, stop Timer 1
812 bcf T1CON,TMR1ON ; stop timer 1 894 bcf T1CON,TMR1ON ; stop timer 1
813 bsf INTCON,INT0IE ; enable INT0 895 bsf INTCON,INT0IE ; enable INT0
814 bsf INTCON3,INT1IE ; enable INT1 896 bsf INTCON3,INT1IE ; enable INT1
815 return 897 return
816 898
818 btfss flip_screen ; 180° flipped? 900 btfss flip_screen ; 180° flipped?
819 bsf switch_left ; (re-)set flag 901 bsf switch_left ; (re-)set flag
820 btfsc flip_screen ; 180° flipped? 902 btfsc flip_screen ; 180° flipped?
821 bsf switch_right ; (re-)set flag 903 bsf switch_right ; (re-)set flag
822 bra timer1int_common ; continue 904 bra timer1int_common ; continue
905
823 timer1int_right: 906 timer1int_right:
824 btfss flip_screen ; 180° flipped? 907 btfss flip_screen ; 180° flipped?
825 bsf switch_right ; set flag 908 bsf switch_right ; set flag
826 btfsc flip_screen ; 180° flipped? 909 btfsc flip_screen ; 180° flipped?
827 bsf switch_left ; set flag 910 bsf switch_left ; (re-)set flag
828 timer1int_common: 911 ;bra timer1int_common ; continue
829 ; load timer1 for next press 912
830 clrf TMR1L 913 timer1int_common: ; load timer1 for next pressure
831 movlw TMR1H_VALUE_CONT ; surface mode 914 clrf TMR1L ; clear timer, low byte
832 btfsc divemode 915 movlw TMR1H_VALUE_CONT ; default to surface mode value
833 movlw TMR1H_VALUE_CONT_DIVE ; sive mode 916 btfsc divemode ; in dive mode?
834 movwf TMR1H 917 movlw TMR1H_VALUE_CONT_DIVE ; YES - overwrite with dive mode value
918 movwf TMR1H ; write value to timer, high byte
835 return ; return from timer1int with timer1 kept running 919 return ; return from timer1int with timer1 kept running
836 920
837 ;============================================================================= 921
838 922 ;=============================================================================
839 check_nofly_desat_time: ; called every minute when not in divemode 923 ; Increment surface interval (counted in minutes) while still in desaturation
840 banksel int_O_desaturation_time 924 ;
841 movf int_O_desaturation_time+0,W ; is Desat null ? 925 ; int_O_desaturation_time is only computed while in start, surface mode,
842 iorwf int_O_desaturation_time+1,W 926 ; menue_tree or ghostwriter. So the ISR may clock surface_interval past
843 bz check_nofly_desat_time_1 ; YES 927 ; the actual surface interval time. But TFT_surface_lastdive will check
844 928 ; int_O_desaturation_time and in case int_O_desaturation_time is zero it
845 ; int_O_desaturation_time is only computed while in start, surface mode, menue_tree or ghostwriter. 929 ; will not show surface_interval but lastdive_time instead. Thus the glitch
846 ; So the ISR may clock surface_interval past the actual surface interval time. But TFT_surface_lastdive 930 ; will remain invisible.
847 ; will check int_O_desaturation_time and in case int_O_desaturation_time is zero it will not show 931 ;
848 ; surface_interval but lastdive_time instead. So this glitch remains invisible. 932 inc_surface_interval: ; called every minute when not in dive mode (in banksel common context)
849 933 btfsc reset_surface_interval ; shall reset the surface interval timer?
850 ; Increase surface interval timer 934 bra inc_surface_interval_1 ; YES
851 banksel common 935 movff int_O_desaturation_time+0,isr_lo ; NO - check desaturation time
852 infsnz surface_interval+0,F 936 movff int_O_desaturation_time+1,WREG
853 incf surface_interval+1,F 937 iorwf isr_lo,W ; - inclusive-or low & high byte, desaturation time = 0 ?
854 return ; done 938 bz inc_surface_interval_1 ; YES - reset surface interval timer
855 939 INCI surface_interval ; NO - increment surface interval
856 check_nofly_desat_time_1: 940 return ; - done
857 banksel common 941 inc_surface_interval_1:
858 clrf surface_interval+0 942 bcf reset_surface_interval ; reset request flag
859 clrf surface_interval+1 ; clear surface interval timer 943 CLRI surface_interval ; reset surface interval timer
860 return ; done 944 return ; done
861 945
862 ;============================================================================= 946 ;=============================================================================
863
864 isr_restore_clock:
865 movff cpu_speed_request,cpu_speed_state ; acknowledge CPU speed request
866 banksel isr_backup
867 movlw d'1'
868 cpfseq cpu_speed_request
869 bra isr_restore_speed2
870 ; Reset to eco
871 movlw b'00000000'
872 movwf OSCTUNE ; 4x PLL Ddsable (Bit 6) - only works with 8 or 16MHz (=32 or 64MHz)
873 movlw b'00110010'
874 movwf OSCCON ; 1 MHz INTOSC
875 movlw T2CON_ECO
876 movwf T2CON
877 bra isr_restore_exit
878 isr_restore_speed2:
879 movlw d'2'
880 cpfseq cpu_speed_request
881 bra isr_restore_speed3
882 ; Reset to normal
883 movlw b'01110010'
884 movwf OSCCON ; 16 MHz INTOSC
885 movlw b'00000000'
886 movwf OSCTUNE ; 4x PLL disable (Bit 6) - only works with 8 or 16MHz (=32 or 64MHz)
887 movlw T2CON_NORMAL
888 movwf T2CON
889 bra isr_restore_exit
890 isr_restore_speed3:
891 ; Reset to fastest
892 movlw b'01110010' ; 16 MHz INTOSC
893 movwf OSCCON
894 movlw b'01000000'
895 movwf OSCTUNE ; 4x PLL enable (Bit 6) - only works with 8 or 16MHz (=32 or 64MHz)
896 movlw T2CON_FASTEST
897 movwf T2CON
898 ;bra isr_restore_exit
899 isr_restore_exit:
900 btfss OSCCON,HFIOFS
901 bra isr_restore_exit ; loop until PLL is stable
902 return
903
904
905 restore_flash: ; restore first flash page from EEPROM
906 banksel common
907 ; Start address in internal flash
908 movlw 0x00
909 movwf TBLPTRL
910 movwf TBLPTRH
911 movwf TBLPTRU
912
913 movlw b'10010100' ; setup erase
914 rcall Write ; write
915
916 movlw .128
917 movwf lo ; byte counter
918 clrf EEADR
919 movlw .3
920 movwf EEADRH ; setup backup address
921
922 TBLRD*- ; dummy read to be in 128 byte block
923 restore_flash_loop:
924 call read_eeprom
925 incf EEADR,F
926 movff EEDATA,TABLAT ; put 1 byte
927 tblwt+* ; table write with pre-increment
928 decfsz lo,F ; 128 bytes done?
929 bra restore_flash_loop ; NO - loop
930
931 movlw b'10000100' ; setup writes
932 rcall Write ; write
933
934 reset ; done, reset CPU
935
936 Write:
937 movwf EECON1 ; type of memory to write in
938 movlw 0x55
939 movwf EECON2
940 movlw 0xAA
941 movwf EECON2
942 bsf EECON1,WR ; write
943 nop
944 nop
945 return
946 947
947 END 948 END