Hi, A couple of weeks ago, I posted some questions that arose in my attempt to use the PortB IOC feature interfacing with a quadrature rotary encoder. Several of you responded with helpful comments -- thank you all for taking the time to respond. Among the responses, Brian Harris pointed me to AN696, which contains the description of a perfect implementation for my application. It uses a pair of D-flipflops to generate 2 separate pulse streams, Up and Down. These 2 streams are used as the clock inputs for TMR0 and TMR1. So, neither sampling nor interrupts are required. I've studied the document, and simple as it is, I don't understand part of it. Here are some relevant excerpts from AN696: "... Timer0 and Timer1 are used to accumulate the up and down pulses that are derived from the encoder output signals. The counters are never cleared to avoid the possibility of losing count information. Instead, the values of the Timer0 and Timer1 registers saved during the previous sample period are subtracted from the present timer values, using two's-complement arithmetic. This caclulation provides us with the total number of up and down pulses accumulated during the servo update period. The use of two's-complement arithmetic also accounts for a timer overflow that may have occurred since the last read." And, here are the relevant C instructions to implement the scheme (to save space: int mvelocity, Dncount, Upcount; long mposition; void UpdPos(void) { mvelocity = Dncount; /* the previously-saved pulse counts */ mvelocity -= Upcount; Upcount = ReadTimer0(); /* the current counts */ Dncount = ReadTimer1(); mvelocity += Upcount; mvelocity -= Dncount; mposition += (long)mvelocity << 8; } Finally, my questions: What in this code accounts for the description's use of the phrase, "using 2's-complement arithmetic"? And, how does this, "account for a timer overflow that may have occurred ..."? Stepping through the code, using decimal arithmetic, I don't get the expected results. Obviously, I'm missing some very important concept. Can someone please explain how this works? I'm baffled. Al Young Phoenix -- http://www.piclist.com hint: The list server can filter out subtopics (like ads or off topics) for you. See http://www.piclist.com/#topics