Getting the Encoder working with Interrupts

9 Mar

IThis is working nicely now, but it was a bit tricky.

When he designed the uBITx, Farhan decided not to use the Arduino pins dedicated for devices that can externally interrupt the micro controller (D2 & D3). Instead, he used inputs A0 and A1, which are general purpose analog/digital pins and then he polled the inputs in software to determine the encoder state. This works well enough for the primitive low-resolution encoder supplied with the uBITx, but it can’t keep up with higher resolution encoders that have more than 10x as many pulses per revolution of the shaft. This is compounded by the fact that the TFT color display I’m using ‘steals’ a lot of processor throughput every time it updates, causing missed states when the encoder is turned.

 

 

Fortunately, Farhan chose to use analog-capable pins, and with a little trickery, these can generate interrupts by using the Arduino’s built-in analog comparator, which can generate interrupts to detect state changes. To accomplish this, you have to set up the comparator to generate an interrupt on a specified edge of one of the encoder inputs and then immediately poll the other encoder input to compare state combinations. This is now working pretty well with no missed states or ‘stuttering’, no matter how fast you spin the encoder shaft.

This weekend, I’ll hopefully connect the new display and encoder to the uBITx, connect an antenna, and give it a whirl.

 

UPDATE: I had a lot of problems with the comparator bouncing on encoder transitions because it has no hysterisis. I rewired the Raduino bard to use the D2 and D3 lines for the encoder and A0 & A1 replacing what D2 and D3 were doing. This works much better.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: