On Wed, Dec 09, 2009 at 01:57:36PM -0000, Donald H wrote: > > --- In AVR-Chat@yahoogroups.com, "Jeff Blaine AC0C" <keepwalking188@...> wrote: > > > > I have seen a lot of code on handling the rotary encoder. But it > > seems there are a thousand approaches spreading over the years on > > the web. > > > > Given the popularity of this as an input device now, I hoped the > > board had a c-code snip that covered handling the rotary encoder in > > an IRQ context. > > "The Board", which board ? > > Have you tried any of the "thousand approaches" you have seen ? > > If so, You should be done by now. Ditto. There are 1001 different expectations for use of an optical encoder, that is why there are 1000 different implementations. As Don said, if you know what you want it to do and you code for it, you'd be done by now. Generally there are 3 outputs from the encoder, A, B, and Index. Could interrupt only on A. Ideally one would IRQ on any edge but not all AVR IRQ inputs can do that so start with one edge or the other. If A rises and B is also high decrement your position counter. If B is low then increment your counter. Then reconfigure the IRQ for falling edge. When A falls if B is high then increment the counter, if B low then decrement. And reconfigure for rising edge. Don't want to spoil your fun but there are ways to implement the above with math rather than if-then-else. You could use the index to reset (or modulo wrap) your counter but you have to be careful as the index has a width and you always want to reset on the same edge. For example if incrementing always reset on the rising edge, if decrementing always reset on the falling edge. Index doesn't need an interrupt input of its own, you can easily poll it from the other encoder interrupt same as suggested above for B. -- David Kelly N4HHE, dkelly@HiWAAY.net ======================================================================== Whom computers would destroy, they must first drive mad.
Message
Re: [AVR-Chat] Re: Rotary encoder
2009-12-09 by David Kelly
Attachments
- No local attachments were found for this message.