How to wire encoder to microcontroller?

Thread Starter

Temeraire

Joined Feb 26, 2019
23
I have this incremental encoder (picture is attached, i think it is EC11) and i want to use it with a PIC16F887 microcontroller.

The encoder has 5 labelled wires (VCC, B, A, BUTTON, GND) and i dont use the buttton.I plugged the 'A' to the PIC's RB3 pin and the 'B' to the PIC's RB4 pin (set up as input pins) and I plugged the 'Vcc' to an arduino uno's 5v and the Ground to the arduino uno's Ground.

In Debugging Mode the PIC can notice the change in the used input pins properly if i rotate the encoder, but the program itself doesn't work properly (Interrupt-On-Change is set up to the encoder's pins, and the program doesnt step in to the Interrupt if i rotate the encoder).

I know my problem is starting to seem like a programming problem and not a wiring one, but i have a strong suspicion that i missed something in the wiring (or done something wrong).
 

Attachments

SamR

Joined Mar 19, 2019
5,486
It is a programming problem. The encoder is basically an A B switch. Depending on CW or CCW A makes first or B makes first and the program increments or decrements. The button is an on-off switch activated when the encoder shaft is pressed in.
 

JohnInTX

Joined Jun 26, 2012
4,787
That is a mechanical encoder which connects the two phases A and B between ground and open circuit in the typical quadrature pattern. The button is the 4th wire which connects to ground when it's pushed. You need pullup resistors to generate the logic '1'. @ericgibbs diagram shows RC networks to 'debounce' the mechanical contacts but no pullups. Those are likely provided by the uC. The PIC16F887 has the ability to turn on 'WEAK PULL UPS' by setting bits in the WPUB register AND enabling global pullups by clearing RBPU/ in the OPTION register.

Assuming you get all of that working, you enable interrupt on change by setting bits in the IOCB register. Don't forget to read PORTB to clear the mismatch condition.

The way I do encoder code is to record the current 2 bits then on the interrupt read the new 2 bits. Combine old and new to a 4 bit code something like this:
xx 00 = old
xx 01 = new
Shift old left 2 bits and combine with new
00 01
Treat this an an index into a table of actions:
0001 = index 1 means one click CW (was 00, now 01)
If you turn CCW:
New = 10, combine with old:
00 10 = index 2 means one click CCW (was 00 now 10)

Fill out the rest of the combinations. There will be 16 entries in the table. In each group of 4 entries you will have one combination for each of 4 possible actions. For the group starting with an old value of 00:
00 00 NONE : old and new are the same.
00 01 CW: one click clockwise
00 10 CCW: one click counterclockwise
00 11 ERROR: the new reading differs in more than one bit, save the new reading and look for the next one.

In assembler, you can decode the table using the standard lookup by adding W to PCL into a 16 entry table of goto's that do the indicated action.

In C use the 4 bit old <<2 + new number as the control in a switch statement or an index into a table of pointers to functions that do one of the 4 possible actions.

Good luck!
 
Last edited:
Top