Home > Uncategorized > Rotary Encoder Success!

Rotary Encoder Success!

I’ve been struggling getting a stupid rotary encoder setup as my input for a PIC 18F4580 board , and have finally got it. A while back I had one working, but the code wasn’t very well written. I wanted something more portable and quicker for an ISR than switches, if-else if chains and tables to figure out the direction.  At mbed.org, the cookbook section on QEI had the answer by XORing the right bit of the last state with the left bit of the new bit. This gives 0 when turning CW and 1 for CCW.

So for the hardware, channel A & B are connected to bits 4 & 5 on Port B to take advantage of the interrupt on change feature. A button is available on the encoder, so that is hooked to bit 0, which is an External interrupt pin. Since the encoder and button are seperate interrupt events, the ISR will check which one occurred and then call that function.  The LCD is a Nokia 6100 knock-off from ebay using a driver from Atomsoft. I modified it slightly for my evil purposes, but you should get it from his blog, so I’m not posting it with mine. The other stuff on the board isn’t implemented for this project, but will be soon!

Just the test screen and a homage to the Commodore 64. The two numbers below “Ready” are the number of interrupts for button presses and the encoder, respectively. Last and New are the states of the encoder, and Rotary# is to see if the function actually adds and subtracts by turning CW & CCW.


Advertisements
Categories: Uncategorized
  1. September 19, 2010 at 1:03 pm

    Hey nice work! Glad to see my code was useful. About to browse your site. Thanks for linking back to my site.

    • September 20, 2010 at 3:09 pm

      No prob, it’s definitely a fine piece of code.

      • September 20, 2010 at 3:26 pm

        thanks. i ported it over from arm code. i forgot from who tho.

  1. No trackbacks yet.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: