Mercurial > public > mk2
annotate code_part1/OSTC_code_asm_part1/altimeter.asm @ 213:d19445e0cb2f
BUGFIX Altimeter reset when exiting sleep mode (bug#&6).
author | JeanDo |
---|---|
date | Wed, 23 Feb 2011 00:18:41 +0100 |
parents | f5e9db793dd3 |
children | 8a0bbe43df65 |
rev | line source |
---|---|
125 | 1 ;============================================================================= |
2 ; | |
3 ; File altimeter.asm | |
4 ; | |
5 ; Altimeter function prototype. | |
6 ; | |
7 ; This program is free software: you can redistribute it and/or modify | |
8 ; it under the terms of the GNU General Public License as published by | |
9 ; the Free Software Foundation, either version 3 of the License, or | |
10 ; (at your option) any later version. | |
11 ; | |
12 ; This program is distributed in the hope that it will be useful, | |
13 ; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 ; GNU General Public License for more details. | |
16 ; | |
17 ; You should have received a copy of the GNU General Public License | |
18 ; along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 ; | |
20 ; Copyright (c) 2010, JD Gascuel. | |
21 ;============================================================================= | |
22 ; HISTORY | |
23 ; 2010-12-15 : [jDG] First prototype with quadratic polynomial ant tp°. | |
24 ; 2010-12-28 : [jDG] Use MPLAB Math and C libraries for FP32 computations. | |
25 ; 2011-01-02 : [jDG] Edit reference pressure by 0.25 mbar. | |
26 ; | |
132
49bb155ddfbf
Fix altimeter after simulator (zero divide -> 32767m)
JeanDo
parents:
125
diff
changeset
|
27 ; Known bug: Simulator reset altitude and reference... |
49bb155ddfbf
Fix altimeter after simulator (zero divide -> 32767m)
JeanDo
parents:
125
diff
changeset
|
28 |
125 | 29 altimeter_calc: |
30 movlb HIGH(pressureAvg) | |
31 | |
170 | 32 movlw HIGH(4*.900) ; Is presure ref bigger than 900mbar |
33 cpfsgt pressureRef+1 | |
213
d19445e0cb2f
BUGFIX Altimeter reset when exiting sleep mode (bug#&6).
JeanDo
parents:
199
diff
changeset
|
34 bra altimeter_restart ; No: Should do a reset now. |
162 | 35 |
36 movlw HIGH(4*.1100) ; Is ref pressure bigger than 1100mbar ? | |
37 cpfsgt pressureRef+1 | |
38 bra altimeter_1 ; No: ok it is valid... | |
39 | |
213
d19445e0cb2f
BUGFIX Altimeter reset when exiting sleep mode (bug#&6).
JeanDo
parents:
199
diff
changeset
|
40 ; Reset calibration value to default. |
170 | 41 altimeter_reset: |
42 movlb HIGH(pressureAvg) | |
125 | 43 movlw LOW(4*.1013+1) ; Init see level at 1013,25 mbar. |
44 movwf pressureRef+0 | |
45 movlw HIGH(4*.1013+1) | |
46 movwf pressureRef+1 | |
47 | |
213
d19445e0cb2f
BUGFIX Altimeter reset when exiting sleep mode (bug#&6).
JeanDo
parents:
199
diff
changeset
|
48 ; Restart averaging. Eg. after a sleep, enables to faster restart with correct |
d19445e0cb2f
BUGFIX Altimeter reset when exiting sleep mode (bug#&6).
JeanDo
parents:
199
diff
changeset
|
49 ; values... |
d19445e0cb2f
BUGFIX Altimeter reset when exiting sleep mode (bug#&6).
JeanDo
parents:
199
diff
changeset
|
50 altimeter_restart: |
125 | 51 clrf pressureSum+0 ; Init averaging area |
52 clrf pressureSum+1 | |
53 clrf pressureCount | |
54 | |
55 movff amb_pressure+0,pressureAvg+0 ; And init first average. | |
56 movff amb_pressure+1,pressureAvg+1 | |
57 | |
58 movlw 4 ; And multiply AVG by 16 to be coherent. | |
59 altimeter_reset_1: | |
60 bcf STATUS,C | |
61 rlcf pressureAvg+0 | |
62 rlcf pressureAvg+1 | |
63 decfsz WREG | |
64 bra altimeter_reset_1 | |
132
49bb155ddfbf
Fix altimeter after simulator (zero divide -> 32767m)
JeanDo
parents:
125
diff
changeset
|
65 |
49bb155ddfbf
Fix altimeter after simulator (zero divide -> 32767m)
JeanDo
parents:
125
diff
changeset
|
66 rcall compute_altitude |
125 | 67 |
68 movlb 1 ; Back to normal bank1. | |
69 return | |
70 | |
71 altimeter_1: | |
72 ;---- Do a bank-safe 16bit summing ----------------------------------- | |
73 movff amb_pressure+0,WREG | |
74 addwf pressureSum+0,F | |
75 movff amb_pressure+1,WREG | |
76 addwfc pressureSum+1,F | |
77 | |
78 incf pressureCount ; Increment count too. | |
79 movlw .32 ; Already 32 done ? | |
80 subwf pressureCount,W | |
81 bnz altimeter_4 ; NO: skip update. | |
82 | |
83 ;---- Update altitude every 32 pressure measures -------------------- | |
84 bcf STATUS,C ; Divide by 2, to store pressure x16 | |
85 rrcf pressureSum+1 | |
86 rrcf pressureSum+0 | |
87 | |
88 movff pressureSum+0,pressureAvg+0 | |
89 movff pressureSum+1,pressureAvg+1 | |
90 | |
91 rcall compute_altitude ; Compute from the averaged value... | |
92 | |
93 clrf pressureSum+0 ; And reset sum zone for next averaging. | |
94 clrf pressureSum+1 | |
95 clrf pressureCount | |
96 | |
97 altimeter_4: | |
98 movlb 1 ; make sure to be in normal bank1 | |
99 return | |
100 | |
101 ;---- Display result ------------------------------------------------- | |
102 altimeter_display: | |
103 GETCUSTOM8 .49 ; Check CF#49 | |
104 btfss WREG,0 ; Enabled ? | |
105 return ; NO: return | |
106 | |
107 WIN_TOP .35 ; Custom view drawing zone... | |
108 WIN_LEFT .90 | |
109 WIN_INVERT .0 | |
110 WIN_FONT .0 | |
111 call PLED_standard_color | |
112 | |
166 | 113 STRCPY "Alt: " |
125 | 114 |
115 movff altitude+0,lo ; BANK-SAFE read altitude | |
116 movff altitude+1,hi | |
166 | 117 btfss hi,7 ; Is altitude negativ ? |
118 bra altimeter_2 ; No: just print it | |
125 | 119 |
166 | 120 PUTC '-' ; Yes: print the minus sign |
121 comf hi ; And do a 16bit 2-complement. | |
122 comf lo | |
123 infsnz lo | |
124 incf hi | |
125 | |
126 altimeter_2: | |
125 | 127 bsf leftbind |
128 output_16 | |
129 bcf leftbind | |
130 | |
166 | 131 STRCAT_PRINT "m " |
125 | 132 return |
133 | |
134 ;============================================================================= | |
135 ; Compute altitude, using the formula: | |
136 ; H(P) = 18.787 log10(P0/P) (Log base 10) | |
137 | |
138 ;---- Interface the the Mayh library ----------------------------------------- | |
139 extern __AARGB2 ; A float in fA2, fA1, fA0, fAExo | |
140 extern __BARGB2 ; B float in fB2, fB1, fB0, fBExo | |
141 extern FLO1632U ; convert uint16 fA+1 --> fp32 fA | |
142 extern FPD32 ; fp32 divide fA/fB --> fA | |
143 extern FPM32 ; fp32 multiply fA*fB --> fA | |
144 extern INT3216 ; convert fp32 fA --> int16 fA+1 | |
145 #define fA __AARGB2 | |
146 #define fB __BARGB2 | |
147 | |
148 ;---- Interface to the C library --------------------------------------------- | |
149 extern __AARGB3 | |
150 extern log10 ; float32 log(auto float32) | |
151 #define fRet __AARGB3 | |
152 | |
153 compute_altitude: | |
154 ; Setup C-code stack, to enable calling the log() function. | |
155 lfsr FSR1, c_code_data_stack | |
156 lfsr FSR2, c_code_data_stack | |
157 | |
158 ; Convert pressure to float, --> fB | |
159 movff pressureAvg+0, fA+1 | |
160 movff pressureAvg+1, fA+2 | |
161 call FLO1632U ; u16 fA[1:2] --> fp32 fA | |
162 movff fA+0, fB+0 | |
163 movff fA+1, fB+1 | |
164 movff fA+2, fB+2 | |
165 movff fA+3, fB+3 | |
166 | |
167 ; Convert sea-level reference pressure to float, --> fB | |
168 movff pressureRef+0, fA+1 ; Get sea level pressure. | |
169 movff pressureRef+1, fA+2 | |
170 bcf STATUS,C ; Multiply by 4. | |
171 rlcf fA+1 | |
172 rlcf fA+2 | |
173 bcf STATUS,C | |
174 rlcf fA+1 | |
175 rlcf fA+2 | |
176 call FLO1632U ; to float: u16 fA[1:2] --> fp32 fA | |
177 | |
178 ; Divide | |
179 call FPD32 ; fp32 X/Y --> X | |
180 | |
181 ; log10() | |
182 movff fA+0, POSTINC1 ; Push X to stack | |
183 movff fA+1, POSTINC1 | |
184 movff fA+2, POSTINC1 | |
185 movff fA+3, POSTINC1 | |
186 call log10 ; log(P0/P) | |
187 | |
188 movf POSTDEC1,F,ACCESS ; pop argument | |
189 movf POSTDEC1,F,ACCESS | |
190 movf POSTDEC1,F,ACCESS | |
191 movf POSTDEC1,F,ACCESS | |
192 | |
193 ; Move log10(P0/P) to fB | |
194 movff fRet+0,fB+0 ; move result to fB | |
195 movff fRet+1,fB+1 | |
196 movff fRet+2,fB+2 | |
197 movff fRet+3,fB+3 | |
198 | |
199 ; Multiply by scaling factor for meters, and standatd atmosphere. | |
200 movlw LOW(.18787) | |
201 movff WREG, fA+1 | |
202 movlw HIGH(.18787) | |
203 movff WREG, fA+2 | |
204 call FLO1632U ; u16 fA[1:2] --> fp32 fA | |
205 call FPM32 ; altitute --> fp32 fA | |
206 | |
207 ; Convert result to int16 --> altitude. | |
208 call INT3216 ; fp32 fA --> int16 fA+1 | |
209 movff fA+1, altitude+0 | |
210 movff fA+2, altitude+1 | |
211 | |
212 return | |
213 | |
214 ;============================================================================= | |
215 ; Altimeter menu | |
216 ; | |
217 ; Edit reference (where altitude = 0) pressure, while displaying corresponding | |
218 ; altitude. | |
219 ; | |
220 altimeter_menu: | |
132
49bb155ddfbf
Fix altimeter after simulator (zero divide -> 32767m)
JeanDo
parents:
125
diff
changeset
|
221 movff pressureRef+0,WREG ; Make sure it is initialized... |
49bb155ddfbf
Fix altimeter after simulator (zero divide -> 32767m)
JeanDo
parents:
125
diff
changeset
|
222 movff pressureRef+1,fA |
49bb155ddfbf
Fix altimeter after simulator (zero divide -> 32767m)
JeanDo
parents:
125
diff
changeset
|
223 iorwf fA |
49bb155ddfbf
Fix altimeter after simulator (zero divide -> 32767m)
JeanDo
parents:
125
diff
changeset
|
224 bnz altimeter_menu_1 ; Yes: skip reset... |
49bb155ddfbf
Fix altimeter after simulator (zero divide -> 32767m)
JeanDo
parents:
125
diff
changeset
|
225 rcall altimeter_reset |
49bb155ddfbf
Fix altimeter after simulator (zero divide -> 32767m)
JeanDo
parents:
125
diff
changeset
|
226 |
49bb155ddfbf
Fix altimeter after simulator (zero divide -> 32767m)
JeanDo
parents:
125
diff
changeset
|
227 altimeter_menu_1: |
125 | 228 call PLED_ClearScreen ; Menu header. |
229 call PLED_standard_color | |
230 call PLED_topline_box | |
231 WIN_INVERT .1 ; Init new Wordprocessor | |
174 | 232 DISPLAYTEXTH .288 ; Title bar |
125 | 233 |
170 | 234 movlw 2 ; Start menu on line 2. |
125 | 235 movwf menupos |
236 | |
237 altimeter_menu_2: | |
174 | 238 WIN_INVERT 0 |
239 WIN_FONT 0 | |
125 | 240 WIN_LEFT .20 ; First line: |
241 WIN_TOP .35 | |
174 | 242 lfsr FSR2,letter |
243 OUTPUTTEXTH .289 ; Sea ref: | |
125 | 244 |
245 movff pressureRef+0, lo | |
246 movff pressureRef+1, hi | |
247 bcf STATUS,C ; Divide ref pressure by 4 | |
248 rrcf hi ; to get the integer part of it: | |
249 rrcf lo | |
250 bcf STATUS,C | |
251 rrcf hi | |
252 rrcf lo | |
253 bsf leftbind | |
254 output_16 | |
255 | |
170 | 256 STRCAT_PRINT " mbar " |
125 | 257 |
170 | 258 WIN_TOP .65 ; Action enable |
174 | 259 lfsr FSR2, letter |
260 OUTPUTTEXTH .290 | |
125 | 261 GETCUSTOM8 .49 |
262 btfss WREG,0 | |
263 bra alt_menu_1 | |
199 | 264 OUTPUTTEXT .130 ; ON |
125 | 265 bra alt_menu_2 |
266 alt_menu_1: | |
199 | 267 OUTPUTTEXT .131 ; OFF |
125 | 268 alt_menu_2: |
199 | 269 call word_processor |
125 | 270 |
174 | 271 DISPLAYTEXTH .291 ; Action reset |
272 DISPLAYTEXTH .292 ; Action add | |
273 DISPLAYTEXTH .293 ; Action sub | |
274 DISPLAYTEXT .011 ; Action exit | |
125 | 275 |
174 | 276 WIN_LEFT .85 ; Bottom right. |
277 lfsr FSR2, letter | |
278 OUTPUTTEXTH .294 ; "Alt: " | |
279 | |
170 | 280 movff altitude+0, lo |
281 movff altitude+1, hi | |
282 btfss hi,7 ; Is altitude negativ ? | |
283 bra altimeter_menu_3 ; No: just print it | |
284 | |
285 PUTC '-' ; Yes: print the minus sign | |
286 comf hi ; And do a 16bit 2-complement. | |
287 comf lo | |
288 infsnz lo | |
289 incf hi | |
290 | |
291 altimeter_menu_3: | |
292 bsf leftbind | |
293 output_16 | |
294 bcf leftbind | |
295 STRCAT_PRINT "m " | |
296 | |
125 | 297 alt_menu_loop: |
298 call PLED_menu_cursor ; Display cursor | |
299 bcf switch_left ; reset buttons state | |
300 bcf switch_right | |
301 | |
302 alt_menu_loop1: ; Wait for button. | |
303 btfsc switch_right ; [[MENU]] button | |
304 bra alt_menu_next | |
305 | |
306 btfsc switch_left ;[[ENTER]] button | |
307 bra alt_menu_do_it | |
308 | |
309 btfsc divemode ; Diving stared ? | |
310 goto restart ; YES: quit this menu ! | |
311 | |
312 btfsc onesecupdate ; Check what should be every 1sec. | |
313 call timeout_surfmode | |
314 | |
315 btfsc onesecupdate | |
316 call set_dive_modes | |
317 | |
318 bcf onesecupdate ; end of 1sek. tasks | |
319 | |
320 btfsc sleepmode ; Sleep mode entered ? | |
321 bra alt_menu_exit | |
322 | |
323 bra alt_menu_loop1 | |
324 | |
325 ;---- Move to next line ------------------------------------------------------ | |
326 | |
327 alt_menu_next: | |
328 incf menupos ; next line. | |
329 movlw .7 | |
330 cpfseq menupos ; Below last line ? | |
331 bra alt_menu_loop | |
170 | 332 movlw .2 ; Yes: back to line no 2. |
125 | 333 movwf menupos |
334 bra alt_menu_loop | |
335 | |
336 ;----- Execute menu line ----------------------------------------------------- | |
337 | |
338 alt_menu_do_it: | |
339 movf menupos,W ; test line value | |
170 | 340 addlw -2 |
341 bz alt_menu_enable ; 2 --> reset | |
342 dcfsnz WREG | |
343 bra alt_menu_reset ; 3 --> +1 | |
125 | 344 dcfsnz WREG |
345 bra alt_menu_plus1 ; 4 --> +1 | |
346 dcfsnz WREG | |
347 bra alt_menu_minus1 ; 5 --> -1 | |
348 bra alt_menu_exit ; else --> exit | |
349 | |
350 ;---- Toggle altimeter (CF#49) ----------------------------------------------- | |
351 alt_menu_enable: | |
352 GETCUSTOM8 .49 ; Read CF#49 | |
353 btg WREG,0 ; Toggle boolean value | |
354 movwf EEDATA | |
355 movlw d'1' ; Upper EEPROM Bank | |
356 movwf EEADRH | |
357 movlw 4*(.49-.32) + 0x82 ; CF#49 low byte address in EEPROM | |
358 movwf EEADR | |
359 call write_eeprom | |
360 bra altimeter_menu_2 | |
361 | |
170 | 362 ;---- Reset sea level pressure to reference ---------------------------------- |
363 alt_menu_reset: | |
364 rcall altimeter_reset | |
365 bra altimeter_menu_2 | |
366 | |
125 | 367 ;---- Increment sea level pressure ------------------------------------------- |
368 alt_menu_plus1: | |
369 movlb HIGH(pressureRef) ; Setup our own ram bank | |
170 | 370 movlw 4 |
371 addwf pressureRef+0,F ; 16bit inc. | |
372 movlw 0 | |
373 addwfc pressureRef+1,F | |
125 | 374 bra alt_menu_recompute ; then recompute altitude. |
375 | |
376 ;---- Decrement sea level pressure ------------------------------------------- | |
377 alt_menu_minus1: | |
378 movlb HIGH(pressureRef) ; Setup our own ram bank | |
170 | 379 movlw -4 |
380 addwf pressureRef+0,F ; 16bit decrement | |
381 movlw -1 | |
382 addwfc pressureRef+1,F | |
125 | 383 |
384 alt_menu_recompute: | |
385 rcall compute_altitude ; Recompute altitude | |
386 movlb 1 ; Go back to normal bank1 | |
387 bra altimeter_menu_2 | |
388 | |
389 ;---- Exit altimeter menu ---------------------------------------------------- | |
390 alt_menu_exit: | |
391 movlw .5 ; reset position to Altimeter line. | |
392 movwf menupos ; | |
393 goto more_menu2 ; in the More... menu. |