Tobie Horswill wrote: > A = current angle (compass heading) > B = desired angle (GPS waypoint heading) > > if A < B then > if ABS(A-B) > 180 then TURN COUNTER CLOCK-WISE else TURN > CLOCK-WISE > else > if A > B then > if ABS(A-B) < 180 then TURN COUNTER CLOCK-WISE else TURN > CLOCK-WISE > else > NO CORRECTION; > > This works fine in a little Windows App I made and I think I could > port it to PIC assembly fairly easily or program it directly in C. > However, I feel it will take a lot of program memory to directly > translate every conditional and math operator (the values are 16 > bits wide since there are 360 degrees) to the PIC. Any suggestions > on how to simplify this procedure in PIC assembly? Tobie: First, you can divide all your angular measurements by 2 to get the numbers into the range [0-179]; that'll remove the need to do 16-bit math without really affecting performance. Next, you could rewrite it as: W = A-B if W = 0 then NO CORRECTION if W < 0 then W = W + 180 if W < 90 then TURN CCW else TURN CW In PIC code, this works out to something like: MOVF B,W ;W = A-B [A and B are in the range 0-179]. SUBWF A,W ; BZ DONE ;If W = 0 then NO CORRECTION. SKPC ;If W >= 0 then skip ahead. ADDLW 180 ;Otherwise, W = 180 + W. SUBLW 89 ;W = 89 - W. BC TURNCCW ;If W >= 0 (in other words, if the old W was ;less than 90), go TURN CCW. TURNCW: .... ;Otherwise, TURN CW. GOTO DONE TURNCCW: .... DONE: -Andy === Andrew Warren --- aiw@cypress.com === Cypress Semiconductor Corporation === Interface Products Division, S.D. === === The opinions expressed above do === not necessarily represent those of === Cypress Semiconductor Corporation. -- http://www.piclist.com hint: PICList Posts must start with ONE topic: "[PIC]:" PIC only "[EE]:" engineering "[OT]:" off topic "[AD]:" ad's