Mercurial > public > mk2
annotate code_part1/OSTC_code_asm_part1/altimeter.asm @ 132:49bb155ddfbf
Fix altimeter after simulator (zero divide -> 32767m)
author | JeanDo |
---|---|
date | Wed, 05 Jan 2011 00:14:44 +0100 |
parents | 2907b42c195b |
children | 48f12271eb18 |
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 | |
32 movf pressureRef+0,W ; Already initialized ? | |
33 iorwf pressureRef+1,W | |
34 bnz altimeter_1 ; Yes... | |
35 | |
36 movlw LOW(4*.1013+1) ; Init see level at 1013,25 mbar. | |
37 movwf pressureRef+0 | |
38 movlw HIGH(4*.1013+1) | |
39 movwf pressureRef+1 | |
40 | |
41 ; Reset computation. Eg. after a sleep, enables to faster restart with correct | |
42 ; values... | |
43 altimeter_reset: | |
44 movlb HIGH(pressureAvg) | |
45 clrf pressureSum+0 ; Init averaging area | |
46 clrf pressureSum+1 | |
47 clrf pressureCount | |
48 | |
49 movff amb_pressure+0,pressureAvg+0 ; And init first average. | |
50 movff amb_pressure+1,pressureAvg+1 | |
51 | |
52 movlw 4 ; And multiply AVG by 16 to be coherent. | |
53 altimeter_reset_1: | |
54 bcf STATUS,C | |
55 rlcf pressureAvg+0 | |
56 rlcf pressureAvg+1 | |
57 decfsz WREG | |
58 bra altimeter_reset_1 | |
132
49bb155ddfbf
Fix altimeter after simulator (zero divide -> 32767m)
JeanDo
parents:
125
diff
changeset
|
59 |
49bb155ddfbf
Fix altimeter after simulator (zero divide -> 32767m)
JeanDo
parents:
125
diff
changeset
|
60 rcall compute_altitude |
125 | 61 |
62 movlb 1 ; Back to normal bank1. | |
63 return | |
64 | |
65 altimeter_1: | |
66 ;---- Do a bank-safe 16bit summing ----------------------------------- | |
67 movff amb_pressure+0,WREG | |
68 addwf pressureSum+0,F | |
69 movff amb_pressure+1,WREG | |
70 addwfc pressureSum+1,F | |
71 | |
72 incf pressureCount ; Increment count too. | |
73 movlw .32 ; Already 32 done ? | |
74 subwf pressureCount,W | |
75 bnz altimeter_4 ; NO: skip update. | |
76 | |
77 ;---- Update altitude every 32 pressure measures -------------------- | |
78 bcf STATUS,C ; Divide by 2, to store pressure x16 | |
79 rrcf pressureSum+1 | |
80 rrcf pressureSum+0 | |
81 | |
82 movff pressureSum+0,pressureAvg+0 | |
83 movff pressureSum+1,pressureAvg+1 | |
84 | |
85 rcall compute_altitude ; Compute from the averaged value... | |
86 | |
87 clrf pressureSum+0 ; And reset sum zone for next averaging. | |
88 clrf pressureSum+1 | |
89 clrf pressureCount | |
90 | |
91 altimeter_4: | |
92 movlb 1 ; make sure to be in normal bank1 | |
93 return | |
94 | |
95 ;---- Display result ------------------------------------------------- | |
96 altimeter_display: | |
97 GETCUSTOM8 .49 ; Check CF#49 | |
98 btfss WREG,0 ; Enabled ? | |
99 return ; NO: return | |
100 | |
101 WIN_TOP .35 ; Custom view drawing zone... | |
102 WIN_LEFT .90 | |
103 WIN_INVERT .0 | |
104 WIN_FONT .0 | |
105 call PLED_standard_color | |
106 | |
107 STRCPY "Alt:" | |
108 | |
109 movff altitude+0,lo ; BANK-SAFE read altitude | |
110 movff altitude+1,hi | |
111 movf lo,W ; Is it zero (not computed yet) ? | |
112 iorwf hi,W | |
113 bz altimeter_2 | |
114 | |
115 bsf leftbind | |
116 output_16 | |
117 bcf leftbind | |
118 bra altimeter_3 | |
119 | |
120 altimeter_2: | |
121 STRCAT "****" | |
122 | |
123 altimeter_3: | |
124 STRCAT_PRINT "m " | |
125 return | |
126 | |
127 ;============================================================================= | |
128 ; Compute altitude, using the formula: | |
129 ; H(P) = 18.787 log10(P0/P) (Log base 10) | |
130 | |
131 ;---- Interface the the Mayh library ----------------------------------------- | |
132 extern __AARGB2 ; A float in fA2, fA1, fA0, fAExo | |
133 extern __BARGB2 ; B float in fB2, fB1, fB0, fBExo | |
134 extern FLO1632U ; convert uint16 fA+1 --> fp32 fA | |
135 extern FPD32 ; fp32 divide fA/fB --> fA | |
136 extern FPM32 ; fp32 multiply fA*fB --> fA | |
137 extern INT3216 ; convert fp32 fA --> int16 fA+1 | |
138 #define fA __AARGB2 | |
139 #define fB __BARGB2 | |
140 | |
141 ;---- Interface to the C library --------------------------------------------- | |
142 extern __AARGB3 | |
143 extern log10 ; float32 log(auto float32) | |
144 #define fRet __AARGB3 | |
145 | |
146 compute_altitude: | |
147 ; Setup C-code stack, to enable calling the log() function. | |
148 lfsr FSR1, c_code_data_stack | |
149 lfsr FSR2, c_code_data_stack | |
150 | |
151 ; Convert pressure to float, --> fB | |
152 movff pressureAvg+0, fA+1 | |
153 movff pressureAvg+1, fA+2 | |
154 call FLO1632U ; u16 fA[1:2] --> fp32 fA | |
155 movff fA+0, fB+0 | |
156 movff fA+1, fB+1 | |
157 movff fA+2, fB+2 | |
158 movff fA+3, fB+3 | |
159 | |
160 ; Convert sea-level reference pressure to float, --> fB | |
161 movff pressureRef+0, fA+1 ; Get sea level pressure. | |
162 movff pressureRef+1, fA+2 | |
163 bcf STATUS,C ; Multiply by 4. | |
164 rlcf fA+1 | |
165 rlcf fA+2 | |
166 bcf STATUS,C | |
167 rlcf fA+1 | |
168 rlcf fA+2 | |
169 call FLO1632U ; to float: u16 fA[1:2] --> fp32 fA | |
170 | |
171 ; Divide | |
172 call FPD32 ; fp32 X/Y --> X | |
173 | |
174 ; log10() | |
175 movff fA+0, POSTINC1 ; Push X to stack | |
176 movff fA+1, POSTINC1 | |
177 movff fA+2, POSTINC1 | |
178 movff fA+3, POSTINC1 | |
179 call log10 ; log(P0/P) | |
180 | |
181 movf POSTDEC1,F,ACCESS ; pop argument | |
182 movf POSTDEC1,F,ACCESS | |
183 movf POSTDEC1,F,ACCESS | |
184 movf POSTDEC1,F,ACCESS | |
185 | |
186 ; Move log10(P0/P) to fB | |
187 movff fRet+0,fB+0 ; move result to fB | |
188 movff fRet+1,fB+1 | |
189 movff fRet+2,fB+2 | |
190 movff fRet+3,fB+3 | |
191 | |
192 ; Multiply by scaling factor for meters, and standatd atmosphere. | |
193 movlw LOW(.18787) | |
194 movff WREG, fA+1 | |
195 movlw HIGH(.18787) | |
196 movff WREG, fA+2 | |
197 call FLO1632U ; u16 fA[1:2] --> fp32 fA | |
198 call FPM32 ; altitute --> fp32 fA | |
199 | |
200 ; Convert result to int16 --> altitude. | |
201 call INT3216 ; fp32 fA --> int16 fA+1 | |
202 movff fA+1, altitude+0 | |
203 movff fA+2, altitude+1 | |
204 | |
205 return | |
206 | |
207 ;============================================================================= | |
208 ; Altimeter menu | |
209 ; | |
210 ; Edit reference (where altitude = 0) pressure, while displaying corresponding | |
211 ; altitude. | |
212 ; | |
213 altimeter_menu: | |
132
49bb155ddfbf
Fix altimeter after simulator (zero divide -> 32767m)
JeanDo
parents:
125
diff
changeset
|
214 movff pressureRef+0,WREG ; Make sure it is initialized... |
49bb155ddfbf
Fix altimeter after simulator (zero divide -> 32767m)
JeanDo
parents:
125
diff
changeset
|
215 movff pressureRef+1,fA |
49bb155ddfbf
Fix altimeter after simulator (zero divide -> 32767m)
JeanDo
parents:
125
diff
changeset
|
216 iorwf fA |
49bb155ddfbf
Fix altimeter after simulator (zero divide -> 32767m)
JeanDo
parents:
125
diff
changeset
|
217 bnz altimeter_menu_1 ; Yes: skip reset... |
49bb155ddfbf
Fix altimeter after simulator (zero divide -> 32767m)
JeanDo
parents:
125
diff
changeset
|
218 rcall altimeter_reset |
49bb155ddfbf
Fix altimeter after simulator (zero divide -> 32767m)
JeanDo
parents:
125
diff
changeset
|
219 |
49bb155ddfbf
Fix altimeter after simulator (zero divide -> 32767m)
JeanDo
parents:
125
diff
changeset
|
220 altimeter_menu_1: |
125 | 221 call PLED_ClearScreen ; Menu header. |
222 call PLED_standard_color | |
223 call PLED_topline_box | |
224 WIN_INVERT .1 ; Init new Wordprocessor | |
225 WIN_FONT .0 | |
226 WIN_LEFT .80-7*7 | |
227 WIN_TOP .0 | |
228 STRCPY_PRINT "Set Altimeter:" | |
229 | |
230 movlw 3 ; Start menu on line 3. | |
231 movwf menupos | |
232 | |
233 altimeter_menu_2: | |
234 WIN_FONT .0 ; Reset, because compute erase that... | |
235 WIN_INVERT .0 | |
236 WIN_LEFT .20 ; First line: | |
237 WIN_TOP .35 | |
238 STRCPY "Sea ref:" | |
239 | |
240 movff pressureRef+0, lo | |
241 movff pressureRef+1, hi | |
242 bcf STATUS,C ; Divide ref pressure by 4 | |
243 rrcf hi ; to get the integer part of it: | |
244 rrcf lo | |
245 bcf STATUS,C | |
246 rrcf hi | |
247 rrcf lo | |
248 bsf leftbind | |
249 output_16 | |
250 | |
251 PUTC '.' | |
252 movff pressureRef+0, hi ; Decimal part is constructed | |
253 clrf WREG ; from the 2 lower bits. | |
254 btfsc hi,0 | |
255 addlw .25 | |
256 btfsc hi,1 | |
257 addlw .50 | |
258 movwf lo | |
259 output_99x | |
260 | |
261 STRCAT_PRINT "mbar " | |
262 | |
263 WIN_TOP .65 ; Second line: | |
264 STRCPY "Alt:" | |
265 movff altitude+0, lo | |
266 movff altitude+1, hi | |
267 bcf leftbind | |
268 output_16 | |
269 STRCAT_PRINT "m " | |
270 | |
271 WIN_TOP .95 ; Action enable | |
272 STRCPY "Enabled: " | |
273 GETCUSTOM8 .49 | |
274 btfss WREG,0 | |
275 bra alt_menu_1 | |
276 STRCAT_PRINT "ON " | |
277 bra alt_menu_2 | |
278 alt_menu_1: | |
279 STRCAT_PRINT "OFF" | |
280 alt_menu_2: | |
281 | |
282 WIN_TOP .125 ; Action add | |
283 STRCPY_PRINT "+0.25 mbar" | |
284 WIN_TOP .155 ; Action sub | |
285 STRCPY_PRINT "-0.25 mbar" | |
286 WIN_TOP .185 ; Action exit | |
287 STRCPY_PRINT "Exit" | |
288 | |
289 alt_menu_loop: | |
290 call PLED_menu_cursor ; Display cursor | |
291 bcf switch_left ; reset buttons state | |
292 bcf switch_right | |
293 | |
294 alt_menu_loop1: ; Wait for button. | |
295 btfsc switch_right ; [[MENU]] button | |
296 bra alt_menu_next | |
297 | |
298 btfsc switch_left ;[[ENTER]] button | |
299 bra alt_menu_do_it | |
300 | |
301 btfsc divemode ; Diving stared ? | |
302 goto restart ; YES: quit this menu ! | |
303 | |
304 btfsc onesecupdate ; Check what should be every 1sec. | |
305 call timeout_surfmode | |
306 | |
307 btfsc onesecupdate | |
308 call set_dive_modes | |
309 | |
310 bcf onesecupdate ; end of 1sek. tasks | |
311 | |
312 btfsc sleepmode ; Sleep mode entered ? | |
313 bra alt_menu_exit | |
314 | |
315 bra alt_menu_loop1 | |
316 | |
317 ;---- Move to next line ------------------------------------------------------ | |
318 | |
319 alt_menu_next: | |
320 incf menupos ; next line. | |
321 movlw .7 | |
322 cpfseq menupos ; Below last line ? | |
323 bra alt_menu_loop | |
324 movlw .3 ; Yes: back to line no 3. | |
325 movwf menupos | |
326 bra alt_menu_loop | |
327 | |
328 ;----- Execute menu line ----------------------------------------------------- | |
329 | |
330 alt_menu_do_it: | |
331 movf menupos,W ; test line value | |
332 addlw -3 | |
333 bz alt_menu_enable | |
334 dcfsnz WREG | |
335 bra alt_menu_plus1 ; 4 --> +1 | |
336 dcfsnz WREG | |
337 bra alt_menu_minus1 ; 5 --> -1 | |
338 bra alt_menu_exit ; else --> exit | |
339 | |
340 ;---- Toggle altimeter (CF#49) ----------------------------------------------- | |
341 alt_menu_enable: | |
342 GETCUSTOM8 .49 ; Read CF#49 | |
343 btg WREG,0 ; Toggle boolean value | |
344 movwf EEDATA | |
345 movlw d'1' ; Upper EEPROM Bank | |
346 movwf EEADRH | |
347 movlw 4*(.49-.32) + 0x82 ; CF#49 low byte address in EEPROM | |
348 movwf EEADR | |
349 call write_eeprom | |
350 bra altimeter_menu_2 | |
351 | |
352 ;---- Increment sea level pressure ------------------------------------------- | |
353 | |
354 alt_menu_plus1: | |
355 movlb HIGH(pressureRef) ; Setup our own ram bank | |
356 infsnz pressureRef+0 ; 16bit inc. | |
357 incf pressureRef+1 | |
358 bra alt_menu_recompute ; then recompute altitude. | |
359 | |
360 ;---- Decrement sea level pressure ------------------------------------------- | |
361 | |
362 alt_menu_minus1: | |
363 movlb HIGH(pressureRef) ; Setup our own ram bank | |
364 decf pressureRef+0 ; 16bit decrement | |
365 movlw 0 | |
366 subwfb pressureRef+1 | |
367 | |
368 alt_menu_recompute: | |
369 rcall compute_altitude ; Recompute altitude | |
370 movlb 1 ; Go back to normal bank1 | |
371 bra altimeter_menu_2 | |
372 | |
373 ;---- Exit altimeter menu ---------------------------------------------------- | |
374 alt_menu_exit: | |
375 movlw .5 ; reset position to Altimeter line. | |
376 movwf menupos ; | |
377 goto more_menu2 ; in the More... menu. |