On Sat, 18 Apr 1998 22:19:07 +0200 marcel writes (many times): >> I still have great difficulty on the folowing >> it seems carry does not affect high byte >> i am also not shure on testing for zero >> i realy need help on this. Subtraction on the PIC tends to confuse me. I generally just use two routines: "add" and "negate" (two's complement). The "add" routine uses the recently-discussed 'add with carry' sequence: ; Add 16-bit numbers A and B together, result in B (B=A+B). movfw al addwf bl,f movfw ah skpnc incfsz ah,w addwf bh,f The two's complement routine is straightforward as well: ; Negate (2's complement) 16-bit number in B (B = - B) comf bl comf bh incf bl,f skpnz incf bh,f [Note that this routine can be extended to more bytes by adding another complement instruction to the top and another incf / skpnz at the bottom. But that isn't important right now.] Using two's complement representation, an ordinary add will combine positive and/or negative numbers correctly. Positive numbers are just binary numbers with the most significant (sign) bit zero. Negative numbers have the sign bit of one. This makes it very easy to tell if a result is positive or negative: btfsc nh,7 ;Test high bit of n (sign bit) goto num_neg ;It's negative goto num_pos ;It's positive The other bits in a negative number are not exactly the value in binary. The "negate" routine will of course turn a negative number into a positive one, also clearing the sign bit. So to implement the "slew rate limiter" logic, do something like this (not optimized at all): A = oldval B = newval call negate (B = - newval) call add (B = oldval - newval) -- Difference in reading C = B (save difference for later) If B positive, call negate (B = -|B|) A = max change (0x80 in this case) call add (B = max change - |difference|) [If B negative now, difference is too large. Setup to alter oldval by either the actual difference (C) or the maximum depending on if the difference is too large] [If B negative] A = oldval B = max change If C positive, call negate (make B = - max change) call add oldval = B [If B positive- difference OK. Note if you still have a copy of newval, it could just be copied.] A = oldval B = C (oldval - newval) call negate (B = newval - oldval) call add (B = oldval + newval - oldval = newval) oldval = B This should work, or at least get close. _____________________________________________________________________ You don't need to buy Internet access to use free Internet e-mail. Get completely free e-mail from Juno at http://www.juno.com Or call Juno at (800) 654-JUNO [654-5866]