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.