Working with rotary encoders w/ quadrature square output

Started by Taylor, March 02, 2011, 02:58:47 PM

Previous topic - Next topic

Taylor

I'm working on an mp3 player project based on this.

In his design, volume is controlled by a momentary for volume up, and one for volume down. I was thinking it would be very slick to use a, ipod-style scroll wheel to control volume, and found some which have tactile switches integrated for the next/last track and pause buttons.

But these rotaries have a 2-channel quadrature square wave output. I understand the basic concept that you can get the direction of movement by comparing the 2 signals - if ch1 is high, then ch2 goes high, you're going clockwise; if ch1 is low when ch2 goes high, it's going CCW. But I'm having a hard time thinking about how to approach this in terms of the logic or code needed to turn that into volume up and down.

Any thoughts on this, conceptually?

Skrogh

Found this:
http://mrgoodbench.com/Delphi/Codewheel.html
The schematic you want is probably:

It's meant for tracking the speed of wheels, but the principle is the same.

Cheers,
-Skrogh   :)

Taylor

Thanks Skrogh (great first post  :) ). I tried it in this simulation. It doesn't seem to work for a quadrature type input - it seems to want a binary input where one channel pulses at half the speed of the other. But that might just be because the flip flop in that sim doesn't have all the pins used here.

In any case, that link is very info-packed so that will get me headed in the right direction.

Skrogh

Thanks! (great work with the Iron Eather pedals btw)
Hmm... For some reason that's the wrong flipflops (there are different types), I did a simulation in LTspice and it worked there.
As far as I remember, CD4013's are quite easy to come by.

Taylor

Yep, for sure, I've got a sleeve of 4013s right next to me in fact. I'll build it and see - I'm just lazy and prefer to simulate things than build them to test them! Are you also "Skrog Productions" on electro-music? No final 'h', I know, but not a common name.  ;)

Skrogh

Hehe I'm mostly too lazy too, that's why I have LTspice (you should give it a try, gives the the option to import and export .wav-files).
Nope, not the "Skrog" from anywhere. I'm the same "Skrogh" as on TalkBass, but I think that's about it. My username is a contraction of my first and middle name.

potul

I would take a pure software approach, no extra hardware:

-Setup an interrupt triggered by one of the 2 signals transition 0 to 1.
-When this interrupt happens, read the other signal. Depending on the state of this, the encoder is going in CCW or CW.
-Increase or decrease volume.

Mat

potul

oh, another possible approach that would use only 1 I/O pin is to add a resistor ladder, and feed it into the ADC of the PIC (if you have one). But then you will need to continuously monitor the ADC and track the state changes.

Mat

Galego

Did you see the code i posted in my tremolo topic? That should help you...

NewA VAR BYTE 'Current state of encoder A bits
DirectionA VAR BIT 'Direction of encoder A travel 1=CCW
zza6 VAR NewA.5   'The first bit
zza15 VAR OldA.6 'The second bit

OldA = NewA
NewA = PortC & %01100000 'input of portC<6:5>
DirectionA = zza6 ^ zza15
if NewA != OldA then
disable
gosub ChangedA
enable
endif

ChangedA:
if (DirectionA = 1) then
if (NewA = 0) then
[code for CCW movement]
endif
else
if (NewA = 0) then
[code for CW movement]
endif
endif
return


I didn't feel the need to use interrupts to read rotary encoders.

potul

yeah, you don't really need to use interrupts, you can continuously poll the inputs.

Taylor

Thanks guys. Looks like I will need to begin diving into PICs. I have been holding out, as my only digital experience is with the FV1, but it's something I need to learn eventually, so may as well start now. I've got a book on it, but never got past programming blinkenlights.  :icon_lol:

PRR

> Looks like I will need to begin diving into PICs

Why? Two flops (maybe less) should do it.

  • SUPPORTER

Taylor

Well, the project already uses a PIC. So as much as I personally would prefer the hardware logic approach because it's easy, the end result would be much cleaner if I just edit the code, since I wouldn't have to add any hardware.

potul


MoltenVoltage

I haven't found any shortcut other than a lot of trial and error and error.

Every encoder will be mechanically different and even the same brand will have different numbers of detents and therefore a different debounce algorithm will be required if you want it to move fluidly (esp. at different speeds), and in the right direction every time, and react instantly.  This is no small task.

On top of that, you will need to figure out how often to allow it to interrupt the rest of your code, and what parts of that code it can interrupt without causing other problems.

Even my Sony car stereo encoder code is crap, which should give you an idea of how challenging it can be.

No substitute for hard work on this one.
MoltenVoltage.com for PedalSync audio control chips - make programmable and MIDI-controlled analog pedals!

Galego

Quote from: MoltenVoltage on March 04, 2011, 12:49:12 PM
I haven't found any shortcut other than a lot of trial and error and error.

Every encoder will be mechanically different and even the same brand will have different numbers of detents and therefore a different debounce algorithm will be required if you want it to move fluidly (esp. at different speeds), and in the right direction every time, and react instantly.  This is no small task.

On top of that, you will need to figure out how often to allow it to interrupt the rest of your code, and what parts of that code it can interrupt without causing other problems.

Even my Sony car stereo encoder code is crap, which should give you an idea of how challenging it can be.

No substitute for hard work on this one.

Well, you made pickup my tremolo to verify that. No issue whatsoever, i don't even use interrupts nor debouncing and it works flawlessly, i tried to spin them as fast as i could by rolling them on the edge of my hand and they still tracked perfectly.
And that's using the code i posted before.

These are the encoders i used: http://pt.mouser.com/Search/ProductDetail.aspx?R=EN11-HSM1BF20virtualkey57700000virtualkey858-EN11-HSM1BF20