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