I agree with the other comments. Here's an outline my son sent to me a couple of years ago. Maybe it will help you write your own code. I was able to very easily just on what he says below. We were using an Atmel 644P at the time. Steve -------------------------------------------- Hi, Rudder encoder is two data wires plus 5V power and ground. We'll need at least one other wire either for an index on the encoder or some other absolute positioning signal. Encoders only change 1 of the two bits at a time - they count in "gray code" or "in quadrature" rather than normal binary. So iterating through steps in one direction, the bits will go through this sequence: 00 01 11 10 00 01 11 00 ... Contrast this with normal binary counting which would be: 00 01 10 11 00 01 10 11 ... So what I did was make a lookup table of previous state + current state. The result is either: -1 (to the left) 0 (same) +1 (to the right) 2 (invalid: the encoder jumped two steps which should never happen) We are working with 4 bits so the table has 16 possible combinations which represent the following state changes: 00<-00 00<-01 00<-10 00<-11 01<-00 01<-01 01<-10 01<-11 10<-00 10<-01 10<-10 10<-11 11<-00 11<-01 11<-10 11<-11 The values of the table will be: 0 -1 +1 2 +1 0 2 -1 -1 2 0 +1 2 +1 -1 0 Say register R0 contains last reading in bits 2 and 3. The algorithm looks like this: Shift R0 two bits to the right so that the last reading moves into bits 0 and 1. Read current reading into bits 2 and 3 Do the lookup of the memory at the table start address + R0 If result is 2, signal an error. Else add result (-1, 0, or +1) to position counter. Does that make sense? Matt From: AVR-Chat@yahoogroups.com [mailto:AVR-Chat@yahoogroups.com] On Behalf Of Jeff Blaine AC0C Sent: Wednesday, December 09, 2009 1:16 AM To: AVR-Chat@yahoogroups.com Subject: [AVR-Chat] Rotary encoder 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. Many thanks. 73/jeff/ac0c [Non-text portions of this message have been removed] __________ Information from ESET Smart Security, version of virus signature database 4672 (20091209) __________ The message was checked by ESET Smart Security. http://www.eset.com [Non-text portions of this message have been removed]
Message
RE: [AVR-Chat] Rotary encoder
2009-12-09 by Steve Hodge
Attachments
- No local attachments were found for this message.