I prefer the method in the following link. I just works flawlessly. I think it is one of the most elegant way to interface quadrature encoders.
http://www.buxtronix.net/2011/10/rotary-encoders-done-properly.html (http://www.buxtronix.net/2011/10/rotary-encoders-done-properly.html)
The firmware is fairly trivial.
This is what I do on an Atmega
(a) the encoder has two wires A and B connect to two pins, e.g. enc_ra and enc_rb
(b) setup the pin change interrupt on enc_ra , point the vector at Right_pulse_isr
(c) declare a variable right_encoder as a signed integer (or a signed long)
(d) use this code as the Interrupt service routine (This is Bascom , but you get the idea):
Right_pulse_isr:
If Enc_ra = Enc_rb Then Decr right_encoder Else Incr right_encoder
return
(e) Warning: There is an upper speed limit imposed by ISR latency, so be careful with other ISR's running (e.g. buffered serial), but typically with a 16MHz CPU, pulse rates up to 20kHz are possible
(f) If you get erratic counts due to EMI etc, then a RC network (eg 1k / 1nf) will help
This is what I do on an Atmega
if (encoder reading is non-zero) //encoder rotation detected
encoder_flag = (encoder_flag << 1) | 0x01; //add 1 bit
else encoder_flag = 0; //encoder flag reset
if (encoder_flag >= ENC_FAST) encoder reading *= 10; //increment at 10x of the normal speed
...
This will generate miscounts during fairly rare EMI double pulse events, where both A and B change state simultaneously, and the two pulse are seperated by the approximate response time of the ISR.
alright adc and ten turn pot glued to a button it will be I feel like I'm i just released a barrel of snakes.
john kerry is needed in this thread