Author Topic: Quadrature encoder decoder chip.  (Read 9310 times)

0 Members and 1 Guest are viewing this topic.

Offline firewalker

  • Super Contributor
  • ***
  • Posts: 1931
  • Country: gr
Quadrature encoder decoder chip.
« on: October 06, 2011, 06:43:28 PM »
Is there any chip for decoding quadrature encoders?

E.g.



When encoder at rest: Out-1, Out-2 to LOW.
When encoder CW: Out-1 pulsing according to the speed of turning the encoder.
When encoder CCW: Out-2 pulsing according to the speed of turning the encoder.


I know it can be done with an mcu (I have already implement it). I was wondering for a specific ic for the job.
Become a realist, stay a dreamer.

 

Offline buxtronix

  • Contributor
  • Posts: 25
  • Country: 00
Re: Quadrature encoder decoder chip.
« Reply #1 on: October 06, 2011, 10:11:50 PM »
The MAX7360 has a rotary decoder, but wont give the output you want (it is i2c).

You're probably better off implementing it in a small MCU, its easy to get it to run with
one chip and no external components. And probably cheaper. Unlikely that there is
anything that gives exactly what you want.
 

Offline FreeThinker

  • Frequent Contributor
  • **
  • Posts: 791
  • Country: england
  • Truth through Thought
Re: Quadrature encoder decoder chip.
« Reply #2 on: October 07, 2011, 01:11:24 AM »
Not sure I understand what you are after but the system you describe made in think 'mouse' as a first thought. A single axis of the mouse would give you what you need, or if you need two discrete channels then use both axis. Any good for you?
Machines were mice and Men were lions once upon a time, but now that it's the opposite it's twice upon a time.
MOONDOG
 

Offline Zad

  • Frequent Contributor
  • **
  • Posts: 998
  • Country: gb
    • Digital Wizardry, Analogue Alchemy, Software Sorcery
Re: Quadrature encoder decoder chip.
« Reply #3 on: October 07, 2011, 05:11:17 AM »
Should be a pretty simple state machine. If you can't find a custom chip for the job then a PIC10 or 12 should be easy to configure.

Offline firewalker

  • Super Contributor
  • ***
  • Posts: 1931
  • Country: gr
Re: Quadrature encoder decoder chip.
« Reply #4 on: October 07, 2011, 06:23:42 AM »
I have already made what I want with an AVR.

I was wondering if there was a chip just for the job that would be potentially cheaper.
Become a realist, stay a dreamer.

 

Offline deephaven

  • Frequent Contributor
  • **
  • Posts: 743
  • Country: gb
  • Civilization is just one big bootstrap
    • Deephaven Ltd
Re: Quadrature encoder decoder chip.
« Reply #5 on: October 07, 2011, 08:45:32 AM »
I have already made what I want with an AVR.

I was wondering if there was a chip just for the job that would be potentially cheaper.

You can get a PIC10F204T-I/OT for £0.38 from Farnell (1 off), I doubt if you'll find a dedicated chip cheaper than that.
 

Offline BravoV

  • Super Contributor
  • ***
  • Posts: 3545
  • Country: 00
  • An EEE (Eternal Electronics Enthusiast)
Re: Quadrature encoder decoder chip.
« Reply #6 on: October 07, 2011, 05:22:28 PM »
I have already made what I want with an AVR.

I was wondering if there was a chip just for the job that would be potentially cheaper.

I don't think it will be cheaper, thats why it doesn't exist and they don't make it, its too expensive to design and produce such chip and probably will not sell either, this from chip maker's perspective.

Offline Hero999

  • Super Contributor
  • ***
  • Posts: 5956
  • Country: 00
Re: Quadrature encoder decoder chip.
« Reply #7 on: October 08, 2011, 04:10:28 AM »
The cheapest PIC is just over half the price of the cheapest AVR in RS Components: ATTINY4-TSHR costs £0.64 and the PIC10F200 costs £0.34.

The MCU you've used is probably overkill, use a cheaper one.
 

Offline Mechatrommer

  • Super Contributor
  • ***
  • Posts: 6872
  • Country: my
  • reassessing directives...
Re: Quadrature encoder decoder chip.
« Reply #8 on: October 08, 2011, 12:04:05 PM »
The cheapest PIC is just over half the price of the cheapest AVR in RS Components: ATTINY4-TSHR costs £0.64 and the PIC10F200 costs £0.34.
The MCU you've used is probably overkill, use a cheaper one.
i dont think attiny is overkill. i've done this exactly encoder with pic10f206, my limitation is it only can encode reliably up to ~100rpm. if i have 4x processing speed, it will be able to do up to 400rpm. this type of state machine is about speed, if your system got high rpm.
if something can select, how cant it be intelligent? if something is intelligent, how cant it exist?
 

Offline NiHaoMike

  • Super Contributor
  • ***
  • Posts: 2887
  • Country: us
  • "Don't turn it on - Take it apart!"
    • Facebook Page
Re: Quadrature encoder decoder chip.
« Reply #9 on: October 08, 2011, 01:15:14 PM »
Have you tried the MSP430? Even the cheapest one can run at up to 16MHz with most instructions only using one cycle.

Offline firewalker

  • Super Contributor
  • ***
  • Posts: 1931
  • Country: gr
Re: Quadrature encoder decoder chip.
« Reply #10 on: October 08, 2011, 10:07:11 PM »
Well, the Attiny2313 was the first chip I found in my drawers with two external interrupts pins (there will be two quadrature encoders, and four outputs two for every encoder).

Alexander.
« Last Edit: October 08, 2011, 10:14:08 PM by firewalker »
Become a realist, stay a dreamer.

 

Offline NiHaoMike

  • Super Contributor
  • ***
  • Posts: 2887
  • Country: us
  • "Don't turn it on - Take it apart!"
    • Facebook Page
Re: Quadrature encoder decoder chip.
« Reply #11 on: October 08, 2011, 11:51:53 PM »
Since its only task is to decode a quadrature encoder, try using polling instead of interrupts. On most processors, interrupts have a significant latency.

Offline buxtronix

  • Contributor
  • Posts: 25
  • Country: 00
Re: Quadrature encoder decoder chip.
« Reply #12 on: October 09, 2011, 12:42:21 AM »
Indeed, if the only thing your MCU is doing is decoding this, then polling is the way to go. Interrupts only make sense if your MCU has to also do something else.

Any MCU should be able to decode these at thousands of RPMs, unless your routine is complex. I have a routine that has 3 lines of logic code (and immune to switch bounce). Thats a pretty tight loop.
 

Offline Mechatrommer

  • Super Contributor
  • ***
  • Posts: 6872
  • Country: my
  • reassessing directives...
Re: Quadrature encoder decoder chip.
« Reply #13 on: October 09, 2011, 01:12:17 AM »
Interrupts only make sense if your MCU has to also do something else.
or you want low powered (sleep), or there's not much rotation going on, like non continuous or only once a while in a day.
« Last Edit: October 09, 2011, 01:15:25 AM by Mechatrommer »
if something can select, how cant it be intelligent? if something is intelligent, how cant it exist?
 

Offline Rufus

  • Super Contributor
  • ***
  • Posts: 2095
Re: Quadrature encoder decoder chip.
« Reply #14 on: October 09, 2011, 02:01:50 AM »
my limitation is it only can encode reliably up to ~100rpm. if i have 4x processing speed, it will be able to do up to 400rpm. this type of state machine is about speed, if your system got high rpm.

Stating encoder rpm without stating encoder lines is completely meaningless. My rough estimate is a polled loop in a PIC10F2xx should be able to track about 1.5 million lines/minute.
 

Offline Mechatrommer

  • Super Contributor
  • ***
  • Posts: 6872
  • Country: my
  • reassessing directives...
Re: Quadrature encoder decoder chip.
« Reply #15 on: October 09, 2011, 06:05:30 AM »
Stating encoder rpm without stating encoder lines is completely meaningless. My rough estimate is a polled loop in a PIC10F2xx should be able to track about 1.5 million lines/minute.
the same with stating encoder lines without stating what it can do or cant. my encoder was to (as usual) encode the rotation (from my old mouse encoder), keep track of previous and current state (4 quadrature state right?) so to be able to decide whether CCW or CW, and then increment or decrement a 32bit value (counter) to be reported back to main mcu when asked through 2pin uart like comm. the idea was to make zero processing in the main mcu (full processing and report mech. in encoder), the main mcu will get a nice 32bit value from pic10f by request. its by polling, and the encoding must not be stopped even while uart reporting. it took me 5 versions to get it speced properly at 100rpm (slightly more). now ask me to do it in 3 opcodes? i surrender.
if something can select, how cant it be intelligent? if something is intelligent, how cant it exist?
 

Offline buxtronix

  • Contributor
  • Posts: 25
  • Country: 00
Re: Quadrature encoder decoder chip.
« Reply #16 on: October 09, 2011, 06:30:28 PM »

This is my encoder routine for AVR. 2 lines of C code logic (more if you want to make it verbose).

Just add a counter and serial routines.

Code: [Select]
/*
 * Rotary encoder handler.
 *
 * This assumes wiper is ground, and 2 outputs are on PB0 and PB1.
 *
 * More info: http://www.buxtronix.net/2011/10/rotary-encoders-done-properly.html
 */
const char ttable[6][4] = {
  {0x3 , 0x2, 0x1,  0x0}, {0x83, 0x0, 0x1,  0x0},
  {0x43, 0x2, 0x0,  0x0}, {0x3 , 0x5, 0x4,  0x0},
  {0x3 , 0x3, 0x4, 0x40}, {0x3 , 0x5, 0x3, 0x80}
};

volatile char state = 0;

void rotary_init() {
  DIRB = 0x0; // PortB is input
  PORTB = 0x3;  // Weak pullup on PB0/PB1.
}

/* Read input pins and process for events. Call this either from a
 * loop or an interrupt (eg pin change or timer).
 *
 * Returns 0 on no event, otherwise 0x80 or 0x40 depending on the direction.
 */
char rotary_process() {
  state = ttable[state & 0xf][PORTB & 0x3];
  return (state & 0xc0);
}
 

Offline Mechatrommer

  • Super Contributor
  • ***
  • Posts: 6872
  • Country: my
  • reassessing directives...
Re: Quadrature encoder decoder chip.
« Reply #17 on: October 10, 2011, 12:12:43 AM »
nice code bux, using lookup table with some sort of fancy indexing to get current state. with code in C, it could more lines when translated to assembly, not that really important as long as it works. but how do you make sure it is bounce free as you said?
edit i just read your site where you explained state 1,2,3,4. if we are at state 2, the only acceptable next state is either 1 and 3. jump to state 4 will be ignored right? well, in my case its possible. so we are working on different system. on high speed system, if it happens state 2 jumps to state 4, i will count as two click (count) and rotation direction is based on previous rotation. based on momentum law.
« Last Edit: October 10, 2011, 12:22:19 AM by Mechatrommer »
if something can select, how cant it be intelligent? if something is intelligent, how cant it exist?
 

Offline Hero999

  • Super Contributor
  • ***
  • Posts: 5956
  • Country: 00
Re: Quadrature encoder decoder chip.
« Reply #18 on: October 10, 2011, 01:14:15 AM »
What sort of encoder are you using? An optical encoder shouldn't bounce.
 

Offline Mechatrommer

  • Super Contributor
  • ***
  • Posts: 6872
  • Country: my
  • reassessing directives...
Re: Quadrature encoder decoder chip.
« Reply #19 on: October 10, 2011, 01:33:30 AM »
me? yes optical encoder, i dont have to handle bounce, only "jump" state. for lookup table as bux's method, pic10f will not be able to do, it only has 24 registers, no eeprom. it was cranking up every bit of it. so attiny should be more comfortable for my earlier job. but if its only for rotary encoder for user interface, like knob, then pic10f should be just fine, no need lookup table no need fancy codes, just code all you like. ymmv.
edit: btw, the OP method of CW pin1 pulsing and CCW pin2 pulsing is more elegant way i think compared to serial comm like method (if speed is a concern), its just the main mcu needs a little bit of + - algebra.
« Last Edit: October 10, 2011, 01:41:25 AM by Mechatrommer »
if something can select, how cant it be intelligent? if something is intelligent, how cant it exist?
 

Offline baljemmett

  • Supporter
  • ****
  • Posts: 666
  • Country: gb
Re: Quadrature encoder decoder chip.
« Reply #20 on: October 10, 2011, 03:44:14 AM »
for lookup table as bux's method, pic10f will not be able to do, it only has 24 registers, no eeprom.

Huh?  By my reckoning even a naive assembler translation of the guts of the suggested code above would only require two registers (the state variable and a temporary), 25 words of program space for the lookup table, and approximately eight words of program space for the rotary_process routine.  There'd be plenty of space in a 256-word PIC10F device, even after accounting for initialisation code, a main loop to poll rotary_process and pulse outputs as required, and me potentially miscounting lines of code when doing a quick compilation in my head...
 

Offline Mechatrommer

  • Super Contributor
  • ***
  • Posts: 6872
  • Country: my
  • reassessing directives...
Re: Quadrature encoder decoder chip.
« Reply #21 on: October 10, 2011, 05:26:37 AM »
for lookup table as bux's method, pic10f will not be able to do, it only has 24 registers, no eeprom.
only require two registers (the state variable and a temporary), 25 words of program space for the lookup table
i know about two registers. but how do you access program space?
if something can select, how cant it be intelligent? if something is intelligent, how cant it exist?
 

Offline buxtronix

  • Contributor
  • Posts: 25
  • Country: 00
Re: Quadrature encoder decoder chip.
« Reply #22 on: October 10, 2011, 11:09:06 AM »
if we are at state 2, the only acceptable next state is either 1 and 3. jump to state 4 will be ignored right? well, in my case its possible. so we are working on different system. on high speed system, if it happens state 2 jumps to state 4, i will count as two click (count) and rotation direction is based on previous rotation. based on momentum law.

So two things...

1 - If state is jumping from 2 to 4 directly, then something isnt right. It's always bad news if a state from your encoder is going astray. You either have something skipping a contact...or the MCU is not fast enough.

2- If there is a reason that cannot be otherwise fixed, it's not hard at all to modify the state table such that from state 2 to state 4, it emits a CW/CCW code. The Arduino library linked from my blog has the state tables more verbose (properly worded #defines), and emitting a code on a jump from 2 to 4 is possible (including adding a 3rd bit to indicate a double jump). Let me know if you'd like me to show the changes necessary.

 

Offline Mechatrommer

  • Super Contributor
  • ***
  • Posts: 6872
  • Country: my
  • reassessing directives...
Re: Quadrature encoder decoder chip.
« Reply #23 on: October 10, 2011, 03:36:11 PM »
or the MCU is not fast enough... Let me know if you'd like me to show the changes necessary.
yes the problem is if the mcu is not fast enough. its ok i'm not into it anymore maybe next time. at least you showed one way of doing it.

for lookup table as bux's method, pic10f will not be able to do, it only has 24 registers, no eeprom.
only require two registers (the state variable and a temporary), 25 words of program space for the lookup table
i know about two registers. but how do you access program space?
let me answer my own question. even though there's no instruction in pic10f to access the program space directly, but maybe its possible using the program space in form of returning function. by manipulating its address or PC register. i never tried but i think its possible. pseudo code maybe like this...

Code: [Select]

//lookup table in program space
progAddr:
retlw 0x03
retlw 0x02
....
retlw 0x80

// the application
main:

;// do something to get something

PC = progAddr + state * 4 + PORTB ;//change program address to point to the lookuptable (retlw table)
state = W ;// new state

as i said, i never tried and it needs more work rather than this simple pseudo. just maybe.
if something can select, how cant it be intelligent? if something is intelligent, how cant it exist?
 

Offline Rufus

  • Super Contributor
  • ***
  • Posts: 2095
Re: Quadrature encoder decoder chip.
« Reply #24 on: October 10, 2011, 04:44:30 PM »
This is my encoder routine for AVR. 2 lines of C code logic
Code: [Select]
....
 * Revision 1.1  1992/12/03
....
static Byte bdata encstate; // Encoder state
sbit eap = encstate^3;
sbit ebp = encstate^2;   
sbit eac = encstate^1;   
sbit ebc = encstate^0;
....
    // Absolute position change indexed by new encstate
    static char code absdelta[] = {0, 1, -1, 0, -1, 0, 0, 1, 1, 0, 0, -1, 0, -1, 1, 0};

        // Maintain absolute position from encoder inputs
        eap = eac;
        ebp = ebc;
        eac = ENCAI;
        ebc = ENCBI;

        abspos += absdelta[encstate];

One of mine in 8051 C.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf