Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

rotary encoder interface

148 views
Skip to first unread message

Gary M. Belcaster

unread,
Aug 9, 1998, 3:00:00 AM8/9/98
to
need to interface a rotary encoder to a 8051
anyone have a subroutine that does this ?
thanks in advance

gary

Jack Peacock

unread,
Aug 9, 1998, 3:00:00 AM8/9/98
to
Gary M. Belcaster wrote in message ...
+AD4-need to interface a rotary encoder to a 8051
+AD4-anyone have a subroutine that does this ?


What are you trying to do? As it happens I'm working on the
same thing right now,a two phase encoder with index on a
shaft, belt driven by a variable speed motor (over 1HP). I
connect each phase input to one of the 8051 interrupts (I
use the Atmel 89C51 variant) and the index to an extra P3
pin.

As long as you aren't using this for any type of gaming
device (slot machine, paycheck wheel, big 6, etc.),
convention exhibit, or animated sign I can supply some
source code for you. It's all in 8051 assembly...after all,
real embedded programmers work at the bit level, none of
that high level C language stuff.
Jack Peacock

r.sko...@fz-juelich.de

unread,
Aug 10, 1998, 3:00:00 AM8/10/98
to
Jack Peacock <pea...@simconv.com> wrote:
: Gary M. Belcaster wrote in message ...

: +AD4-need to interface a rotary encoder to a 8051
: +AD4-anyone have a subroutine that does this ?


: What are you trying to do? As it happens I'm working on the
: same thing right now,a two phase encoder with index on a
: shaft, belt driven by a variable speed motor (over 1HP). I
: connect each phase input to one of the 8051 interrupts (I
: use the Atmel 89C51 variant) and the index to an extra P3
: pin.

If though what you want is a routine to read out an absolute encoded angular
position encoder, we are using a couple of Haidenhain ROC 417. Those are
angular encoders with 17 bits resolution (of which we only use 16) and they
shift out one bit for each clock pulse. We use them to position several
detectors with stepping motors. To read out one 16 bit word between any two
motorsteps is easily done.

Rolf

--
r.sko...@fz-juelich.de

Tap

unread,
Aug 10, 1998, 3:00:00 AM8/10/98
to
On Sun, 09 Aug 1998 14:54:20 +0000, gbe...@pressenter.com (Gary M.
Belcaster) wrote:

>need to interface a rotary encoder to a 8051

>anyone have a subroutine that does this ?

>thanks in advance
>
>gary
Take one of the quad outputs and connect it to an edge interrupt input
the other to any normal input pin

During your interrupt routine...
Read the normal pin first.. if hi, inc your counters+1, if low dec
your counters -1
That it

If the direction of count is wrong just swap the quad outputs or in
the software

If you have a speed problem ..ie reading wrong direction
put one of the quad outputs into the D input of a D type flipflop and
the other into the clock of the same chip .The Q output of the
flipflop will latch the direction bit for you, till your routine can
get to it

Tap

Werner Huerttlen

unread,
Aug 10, 1998, 3:00:00 AM8/10/98
to
ts...@spuzzum.bc.ca (Tap ) wrote:


>Tap

Hi Tap,
do you have any great idea, how to increase resolution in a similar
way ? idea is to increase resolution by 4, like it is possible with
hardware.

If one has not enough interrupts, it is possible to poll A and B in
your main timer-isr and do direction detection and step detection in
software, because it is gray-code.

already done with the Atmel AvR and 4 encoders.

Werner

Best regards,
Werner
remove .XXX in e-mail-adress

* Opinions expressed herein are my own and may
* not represent those of my employer

Tap

unread,
Aug 10, 1998, 3:00:00 AM8/10/98
to

Yes... you can count all edges to increase the count per spin from
both quad outputs
You will need a Pic 84 or any other chip that has Int on change
.....ie Rb <7:4> Hook up one quad output to.. say rb7
the other to rb6.and rb0 .Set up isr's on rb0, rb6 and rb7
and make rb0 isr a rising edge type

On isr rb0 read rb7's input and use its state to set /clear a
direction bit in ram.. then exit to rb6 isr and +1 or -1 the counters
according to the direction bit

On any rb6 or rb7 irs... +1or -1 counters according to the direction
bit..then exit ..

rb0 isr must have priorty over rb6,rb7

You can also make a both edge circut with a Xor gate ..
quad output to one input and an r-c feed back on the other input
this will make a pulse on every edge

Tap


Werner Huerttlen

unread,
Aug 11, 1998, 3:00:00 AM8/11/98
to
ts...@spuzzum.bc.ca (Tap ) wrote:

...


>You can also make a both edge circut with a Xor gate ..
>quad output to one input and an r-c feed back on the other input
>this will make a pulse on every edge
>
>Tap
>

Tap, thanks. I am impressed.

Chris Wright

unread,
Aug 11, 1998, 3:00:00 AM8/11/98
to
In article <35d13c39.78669557@news>, Tap <ts...@spuzzum.bc.ca> writes

>
>You can also make a both edge circut with a Xor gate ..
>quad output to one input and an r-c feed back on the other input
>this will make a pulse on every edge
>
Unless I've missed something, this will give a pulse on every change-of-
state of the encoder signal, but it will not give any indication of
direction, without some additional work to decode what state it has gone
from/to. This could be done most easily with software, but you may need
to latch the two inputs.

I have done stuff like this, and if you don't take account of direction
you will get cumulative errors, because even in a system which you think
is uni-directional, you will get some jitter, or uncertainty in the
position. If direction is taken into account then the count will just
flick up and back down by one count occasionally - no problem, but if
you are just counting changes-of-state the count will go on creeping up
whilst the encoder stays still.

--
Chris Wright
Colt International Licensing Ltd

Rick Sharpe

unread,
Aug 11, 1998, 3:00:00 AM8/11/98
to
The two signals are like a sine and cosine wave, 90 degrees
out of sync.

if A is hi, then falling B is a + count, rising B is a - count.
if A is lo, then the counts are reversed

and

if B is hi then rising A is a + count, falling A is a - count
if B is lo, then the counts are reversed

so, to implement, build edge detector pulse generators for A and
B, route the pulses to + or - based on the logic above, and use
+ and - to step a counter up and down.

make the pulse width much smaller than the max speed.
use hysteresis/Schmidt trigger on the A and B signals (very important)

Rick Sharpe


Chris Wright wrote in message ...

Tap

unread,
Aug 11, 1998, 3:00:00 AM8/11/98
to
My remark about the xor gate was intended to provide a means to
generate interrupt pulses for a mcu chip without interrupt on change
inputs. What Chris Wright says about the direction stuff is
correct.but I though I covered that in the software description before
the remark...obviously not well enough

Rick Sharpe's comments remind me of the days when ,that if I had two
nickels to rub together only one of them was mine. I was making my own
optical rotary encoders with PCB layout tape for use in Disco lighting
systems . The edges from the phototransistors were way too soft and
noisy and I used 555 timers in some oddball way to get a schmidt
trigger effect to clean up the signal .From there one "quad"(well sort
of anyway) would connect to the direction pins on a cmos up/dn
counters and the other quad straight into the clock inputs. I had
magnitude comparitors tied between the counters and shift register
outputs which provided the data for the stop /reverse points. Up at
other end of serial data lines was a joystick and a whole raft of
analog and digital chips that I somehow converted audio into a sway
distance and allowed the user to point an array of spotlights.
In the end and after a lot of work..it all worked..I got paid
Tap

Adrian Jansen

unread,
Aug 12, 1998, 3:00:00 AM8/12/98
to
I also tried the trick of using xor gates to detect edges, then latched the
other phase on each pulse. Works well except that when going from forward
to reverse, you miss one pulse - ie the forward/reverse switch happens on
the *second* pulse after reversing direction. This may be ok in some
sitiations, but is highly annoying when using an encoder for manual input -
remembering to go 2 steps back to get one decrement is very non-intuitive.
Still looking for a way around this one.

--
--
Regards,

Adrian Jansen aja...@ozemail.com.au
Design Engineer J & K Micro Systems
Ph (07) 4639 4676 Microcomputer solutions for industrial control


Rick Sharpe wrote in message <35d06...@aedes.isd.net>...

Tap

unread,
Aug 12, 1998, 3:00:00 AM8/12/98
to
You can fix this in software but if this is an high accuracy item your
doing, don't use this to double edges, because you are swapping time for
an extra optical slot ..During a reversal the xor pulse can mask the
correct data if it happens at just the right spot. For anywhere it
really counts just get right resolution for the job and be done with it.
I almost always works out cheaper in both the long and short run.
Tap

Stefan Wimmer

unread,
Aug 12, 1998, 3:00:00 AM8/12/98
to
In article <35d06...@aedes.isd.net>, "Rick Sharpe" <rsh...@beckman.com> wrote:
>The two signals are like a sine and cosine wave, 90 degrees
>out of sync.

.. connect the sine signal to the D-input of a D-FF and the cosine to its
clock and to the clock of an up/down counter. The output of the D-FF goes to
the U/D input of the counter.

--
Stefan Wimmer Cellware Broadband
Email s...@cellware.de Rudower Chaussee 5
WWW http://www.cellware.de/ 12489 Berlin, Germany

Visit my private Homepage: Love, Electronics, Rockets, Fireworks!
http://www.geocities.com/CapeCanaveral/6368/

Rick Sharpe

unread,
Aug 12, 1998, 3:00:00 AM8/12/98
to

I remember rolling my own schmidt triggers out of a noninverting
CMOS buffer and a pair resistors. You get 6 or 8 per IC that
way. Not quite as good as the "store bought", though.

Maybe this will turn into a war stories thread!

Rick

Tap wrote in message <35d08aa1.164289780@news>...


>My remark about the xor gate was intended to provide a means to
>generate interrupt pulses for a mcu chip without interrupt on change
>inputs. What Chris Wright says about the direction stuff is
>correct.but I though I covered that in the software description before
>the remark...obviously not well enough
>
>Rick Sharpe's comments remind me of the days when ,that if I had two
>nickels to rub together only one of them was mine. I was making my own
>optical rotary encoders with PCB layout tape for use in Disco lighting
>systems . The edges from the phototransistors were way too soft and
>noisy and I used 555 timers in some oddball way to get a schmidt
>trigger effect to clean up the signal .From there one "quad"(well sort
>of anyway) would connect to the direction pins on a cmos up/dn
>counters and the other quad straight into the clock inputs. I had
>magnitude comparitors tied between the counters and shift register
>outputs which provided the data for the stop /reverse points. Up at
>other end of serial data lines was a joystick and a whole raft of
>analog and digital chips that I somehow converted audio into a sway
>distance and allowed the user to point an array of spotlights.
> In the end and after a lot of work..it all worked..I got paid
>Tap
>
>
>
>
>
>
>

>On Tue, 11 Aug 1998 10:22:37 -0500, "Rick Sharpe"


><rsh...@beckman.com> wrote:
>
>>The two signals are like a sine and cosine wave, 90 degrees
>>out of sync.
>>

>>if A is hi, then falling B is a + count, rising B is a - count.
>>if A is lo, then the counts are reversed
>>
>>and
>>
>>if B is hi then rising A is a + count, falling A is a - count
>>if B is lo, then the counts are reversed
>>
>>so, to implement, build edge detector pulse generators for A and
>>B, route the pulses to + or - based on the logic above, and use
>>+ and - to step a counter up and down.
>>
>>make the pulse width much smaller than the max speed.
>>use hysteresis/Schmidt trigger on the A and B signals (very important)
>>
>>Rick Sharpe
>>
>>

Rick Sharpe

unread,
Aug 12, 1998, 3:00:00 AM8/12/98
to

pm

unread,
Aug 13, 1998, 3:00:00 AM8/13/98
to
I am using a coldfire chip to do the same thing.. for the 8051s I add the HP
chip HCTL2016 Quad Encoder.

pc


Gary M. Belcaster wrote in message ...

0 new messages