In comp.arch.embedded, "Colin MacDougall" <colin |D0T|
> wrote:
>Hello,
>
>I am trying to connect an ALPS two phase mechanical encoder to
>a small micro ( AVR 90s2313 ). I can get it to work pretty much
>how I want - at the moment it causes a row of eight LED's to
>run back and forth. The problem I come across is that turning
>the shaft to the next detent position sometimes causes the LED's
>to double count. Looking at the output from the encoder on a scope
>reveals a bit of noise on the outputs. I have set my micro to
>interrupt on a falling edge of one of the phases and tried using
>a 7 mS software timer but this does not totally cure the problem.
>Does anyone have some tips for using these devices ? Is it usual to
>hook the outputs straight on to a micro or should a Schmitt trigger
>or similar be used first ?
If there's some the micro can't devote a few extra cycles to
debouncing, then you should perhaps add extra hardware (or look into
raising the clock speed!), but a good software-only debounce works
fine for me.
>I have thought about using a 'best of three'
>approach where the phase is sampled three times and a decision made
>if all three results are the same.
I do something similar. I've gotten a mechanical encoder to work
quite reliably, with switches directly into the microcontroller port
pins, with the chip's internal pullups enabled (no external resistors
or capacitors). I read the two lines inside a once-every-1-mS timer
interrupt, and read it each time for ten times through (giving an
'actual switch value' every 10mS). Since the switch goes to ground and
the internal pullup goes to VCC, the switch 'on' is read as a zero. I
initialize a buffer to all 1's and do an 'and' of each reading with
the buffer so that if a bit value is zero (switch on) during any of
those reads, I presume the switch is on (and all the other 1 readings
were due to a scratchy switch - if the switch contacts are nowhere
near each other, then ALL readings of it will be 1, correctly
indicating switch off). At the end of the tenth reading I pass this
off as The Encoder Value, then reinit the buffer and counter.
I had looked at the switch output with a scope, both turning slowly
(to see any noise, as well as the rise time of the pullups) and
quickly (to see how fast the quadrature signals might reasonably
change at maximum hand-turning speed), so this code is the result of
Deliberate Research into Cheap Mechanical Rotary Encoders (these are
made by Panasonic, digikey part# P10859-ND).
It's AVR 90S1200 assembly code, I wrote it on my own time, as
opposed to being paid to do it, so I have full rights to the code. If
someone asks nicely I coould put the whole thing online, I might even
add some [more!] documentation to it.
>TIA
>
>Colin