Maximum interrupt frequency? (Motor encoder)


I'm planning to use this kind of rotational encoder with my robot: It's scavenged from an old HP printer and has 18,000 lines. My robot's motors are going to max out at ~2RPM, so I get a maximum of 36K encoder impulses per second (72K since I have two motors). The encoders have two pins, one for the counter and one for direction.

I plan an interrupt routine that triggers on the counter, checks the value on the direction pin and adds or subtracts from a rotation count. Basically,

if(PORTB&DIRMASK) counter++; else counter--;

This routine will be called up to 72,000 times per second.

Can Arduino handle this? My calculation is that for an Arduino running at 16MHz and an ISR called at 72kHz, I have 222 clock cycles per interrupt service routine; if I assume the above computation to use 50 clock cycles, I'd be using 22% of the device, leaving plenty of room for other stuff. But I'd like to hear from someone with more experience on this.

Any advice is much appreciated!

Regards, Björn

Bump... can anyone help here?

OK it is just that despite the maths it sounds too fast for my liking. Remember it is not only what you do in the ISR but also as we are in C the state of the machine has to be preserved and then recovered. If I had the problem I would not even consider writing the ISR in C but would do it directly in assembler.

Well no actually if I was doing it I would off load the keeping track of the count to hardware and design an up / down counter of more bits than I used here:- Rotary Max

That's a great idea, thanks a lot! I think I'll try raw Arduino first, but if that doesn't work, I'll try your design (maybe replacing the 74193 with an 8-bit counter like the 74592, would that work?) and just poll it in a loop.

maybe replacing the 74193 with an 8-bit counter like the 74592, would that work?)

The 592 sounds like it might just be an 8 bit counter and what you need is an up/down counter. You can cascade two 193s together to give you 8 bits if you want.