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