Gary Sutcliffe wrote: > > At 10:19 AM 7/2/97 -0700, you wrote: > >I have a question for all the PIC experts out there: I am trying to > >interface a quadrature decoder to a PIC which currently drives some > >stepping motors. I can easily figure out how to build an external up/down > >counter and "roll my own" absolute position decoder- but has anybody > >successfully used the PIC itself to do this? > > Well, I did it on an 8051, but the principle would be the same. > > First I save the old values in bits 2 & 3 in a register. Then when the > input changes I put the new values in bits 0 & 1. I use the 4 bit number as > an index to a look up table. > > The table has a return value of either -1 or +1 depending on if the change > from the old value to the new value indicates clockwise rotation (+1) or > counter clockwise (-1). I simply add this value to my counter. Finally I > put the new value in the old value location in preparation for the next change. > > Only 1/2 of the 16 byte table contains valid data. The other half should > never get accessed since the encoder can't genrate those bit patterns (in > theory!). The bottom line is that it is not especially memory efficient, > but is pretty quick. > > Hope this helps. > > - Gary > This reply is a bit late (by about 3 months) but another method is; The encoder reads as follows: Bit 1 0 ---------- ^ 0 0 | CCW | 0 1 | CW | 1 1 v 1 0 To determine the direction and update a counter value; BEGIN . last = encoder . loop forever . . current = encoder . . if last <> current . . . direction = last.bit1 XOR current.bit0 . . . if (direction == 1) . . . . value = value + 1 . . . else . . . . value = value - 1 . . . endif . . . last = current . . endif . endloop END -- Peter Homann email: peterh@adacel.com.au Work : +61 3 9596-2991 Adacel Pty Ltd Fax : +61 3 9596-2960 250 Bay St, Brighton 3186, VIC, AUSTRALIA Mobile : 014 025-925 http://www.adacel.com.au Australian Software Engineering Excellence