Searching \ for 'Encoders (again?)' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/io/sensors.htm?key=encoder
Search entire site for: 'Encoders (again?)'.

Truncated match.
PICList Thread
'Encoders (again?)'
1997\02\06@212036 by Steve Hardy

flavicon
face
> From: "Cesarz, Peter J. (GEA, 068974 )" <spam_OUTPETER.CESARZTakeThisOuTspamAPPL.GE.COM>
>
> I know that there was some traffic concerning encoders earlier....but at
> the time I wasn't interested in them.  Now I am, and I feel a bit guilty
> because this may be old news to others.
>
> The encoder I've got has an A pin and a B pin.  I've noticed on the
> scope that one pulse train will lead the other depending on the
> direction of rotation (CW vs. CCW).  During a power up state there could
> be one of four conditions (two bits, A and B, thus four possible
> states).  I'm beginning to write my block diagram for the code (intended
> for a PIC16C84) and it's getting pretty messy with all the decision
> loops. [cut]

You have a quadrature encoder (lucky you).  The programming isn't all that
complicated.  Look for transitions of both A and B inputs and execute the
following algorithm.  Unless you have an index pulse ('I'), there is no way
of knowing the initial count therefore you may as well set it to zero and
go from there.

       A     B     Counter
       ----  ----  -------
       0-1   1     +1
       0-1   0     -1
       1-0   1     -1
       1-0   0     +1
       1     0-1   -1
       0     0-1   +1
       1     1-0   +1
       0     1-0   -1

which can also be expressed as the following pseudocode:

       wait for transition of A or B
       if transition on A
         if A xor B == 0
           increment counter
         else
           decrement counter
       else
         if A xor B == 0
           decrement counter
         else
           increment counter


and, to get really specific, let's write some PIC 16CXX code to implement an
8-bit counter (called POS) assuming the A and B inputs come from port B,
bits 0 and 1 respectively.


       clrf    POS             ; zero counter
       movf    PORTB,w
       movwf   OLD             ; save current (old) input state
loop    movf    PORTB,w         ; get new state
       movwf   NEW             ; and save it
       xorwf   OLD,w           ; compare w/- old state
       andlw   3               ; mask out bits of interest

; At this point W countains a number from 0 to 3 interpreted as:
;    00   No change
;    01   A changed
;    10   B chaged
;    11   Both changed - shouldn't occur unless overspeeding, in which case
;         a count has been lost and the position is indeterminate.  We don't
;         check for this in the following - you can add it if problematic.

       btfsc   STATUS,Z        ; test for no change
       goto    loop            ; loop if no change

       xorwf   NEW,w           ; XOR in new B state
       rlf     NEW,f           ; move bit A to position B
       xorwf   NEW,w           ; XOR in new A state

; Now bit 1 of W contains (whether B changed) xor (new B) xor (new A).

       andlw   2               ; Mask out bit of interest
       btfsc   STATUS,Z        ; skip if non-zero
       goto    increment       ; was zero.
       decf    POS,f           ; decrement counter
       goto    continue
increment incf  POS,f
continue rrf    NEW,w           ; fix up and get new state
       movwf   OLD             ; and save as old state
       goto    loop


That should just about do it, although one could shave a few cycles here and
there.

Regards,
SJH
Canberra, Australia

More... (looser matching)
- Last day of these posts
- In 1997 , 1998 only
- Today
- New search...