Searching \ for 'UP/DOWN counter using a PIC?' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: massmind.org/techref/microchip/time.htm?key=count
Search entire site for: 'UP/DOWN counter using a PIC?'.

Truncated match.
PICList Thread
'UP/DOWN counter using a PIC?'
1997\07\02@134022 by david

flavicon
face
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?

I was thinking along the lines of using one externally clocked counter to
count all pulses, and another to count only those in one direction.
Subtracting these two give a relative number of steps in a particular
direction.

Any comments?

David

                        \|||/
                         o o
David Darknell
-------+--+--oOO-(_)-OOo--+--+-----------------------------------------
             |--|__|--|___|--|__|--|   spam_OUTdavidTakeThisOuTspamxia.com .....davidKILLspamspam@spam@papdev.com

"In a world without need of fences, who needs Gates."
                                                                 Author
unknown
_______________________________________________________________________

1997\07\02@152707 by smegtra

picon face
david@PAPDEV.COM 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?
>
> I was thinking along the lines of using one externally clocked counter to
> count all pulses, and another to count only those in one direction.
> Subtracting these two give a relative number of steps in a particular
> direction.

I threw together a few latches and XOR gates to convert from quadrature
to tachometer and direction.  The tach input drove one of my interrupt
on change pins.  Whenever I got that interrupt, I'd just poll the
direction pin.  I needed to use 16 bit maths, but that wasn't too
horrible.  The only problem (?) with it was that when the direction
changed, my circuit produced a one cycle latency.  Not too tough to fix
in software.  I can't remember the circuit offhand, but I seem to recall
that it used two XORs, two latches, and nothing else.  I'll dig it up if
you'd like.

Jason ( # -e^(pi*i) ) <g>

1997\07\03@074223 by Gary Sutcliffe

flavicon
face
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

------------------------------------------------------------------------
Gary Sutcliffe,  W9XT          Unified Microsystems
ppvvppspamKILLspammixcom.com              PO Box 133 Slinger, WI 53086
http://www.qth.com/w9xt        414-644-9036


'UP/DOWN counter using a PIC?'
1997\11\26@000703 by Peter Homann
picon face
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.
{Quote hidden}

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: .....peterhKILLspamspam.....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

1997\11\26@080128 by Alvaro Deibe Diaz

flavicon
face
  I know it's late, but here is what I've done to read an encoder with a
16C73. This routin runs on an interrupt, about 100 times in a second:
;
; Rutina para la lectura del encoder
;
; Se utiliza un unico byte local, ENCODER, de la forma:
;
; ENCODER: '00000000'
;           |||||||+> Lectura anterior de 'Y'
;           ||||||+-> Lectura anterior de 'X'
;           |||||+--> '1' = Avanzar hacia adelante
;           ||||+---> '1' = Avanzar hacia atras
;           ++++----> Bits de avance anteriores
;
                           ; En esta posicion los bits del
                           ; encoder estan en PORTA:
                           ; '00000000' <-- PORTA
                           ;        |+--> 'X'
                           ;        +---> 'Y'
;
       movf    PORTA,W
       movwf   AUX         ;  Aqui comienzan las cuentas:
       xorf    ENCODER,F   ; ENCODER <- ENCODER xor AUX
       rrf     AUX,F       ;  Ahora se guardan, al reves,
       rlf     ENCODER,F   ; los dos bits del encoder en
       rrf     AUX,F       ; AUX, para la proxima cuenta.
       rlf     ENCODER,F
;
       btfsc   ENCODER,2   ; Por ultimo, se actualiza el
       incf    COUNTER,F   ; contador.
       btfsc   ENCODER,3
       decf    COUNTER,F

  Sorry for the Spanish comments. I think the code is self explaining (and
my english is not so good :-)

  First you get the encoder bits in AUX (0 and 1). Then, xor AUX with
ENCODER, where are the previous encoder bits (1 and 0), but in inverse
order. Finally, you get two bits (2 and 3) in ENCODER to change the
position counter: if bit 2 =1 then inc(counter). If bit 3 =1 then
dec(counter). Caution, it is posible to get bits 2 and 3 both set, so you
inc and dec counter, but who cares?. If you care, simply add 'movlw
B'00001100; xorwf ENCODER,F' before 'btfsc ENCODER,2', and you're done.

-----Mensaje original-----
De:     Peter Homann [SMTP:EraseMEpeterhspam_OUTspamTakeThisOuTADACEL.COM.AU]
Enviado el:     miercoles 26 de noviembre de 1997 6:04
Para:   PICLISTspamspam_OUTMITVMA.MIT.EDU
Asunto: Re: UP/DOWN counter using a PIC?

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.
{Quote hidden}

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: @spam@peterhKILLspamspamadacel.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

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