# PIC18F45K22 and a EP50S 10-bit Gray Code Rotary Encoder

#### GettinBetter

Joined Jun 22, 2018
33
Hi peeps, I've got myself a EP50S 10 bit gray code encoder and was expecting it to only have two pulse connections, ignoring supplies (something like PH1 & PH2) however it has a cable for each of all 10 bits (I obviously downloaded the wrong data sheet when researching, but the resolution is correct)... so I was wondering if it is at all possible to poll the 8 pins of Port B and say 2 pins of Port A to create my 10 bit code and then convert to Binary or HEX (not sure yet). I should mention that some of Port C is being used for Terminal over EUSART, and PWM and hopefully Port D for reading a gyro (gulp).
Is the encoder suggestion doable and an acceptable approach, or am I barking up the wrong tree? Any comments gratefully received.
Regards
Les

#### shteii01

Joined Feb 19, 2010
4,644
It sounds like it uses parallel connection. What you propose is doable.

#### joeyd999

Joined Jun 6, 2011
4,477
Here's some PIC18F 16 bit gray to bin code I wrote a long time ago in a galaxy far, far away. Maybe it'll help:

Code:
;**********************************************************
;** GRAY2BIN -- Convert ordered gray code to binary data **
;**********************************************************
;**   Input data: GRAY1:0                                **
;**   Output data: BIN1:0                 **
;**********************************************************

gray2bin

movff    gray1,temp1        ;don't destroy gray
movff    gray0,temp0

clrf    bin1            ;clear binary
clrf    bin0

movlf    bitcnt,16        ;sixteen bits to convert

gloop    rlcf    temp0,f            ;rotate a bit out
rlcf    temp1,f            ;into carry

rlcf    temp2,w            ;roll bit into w.0
xorwf    bin0,w            ;calculate new 1 or 0 in w.0
movwf    temp2            ;save result
rrcf    temp2,f            ;rotate new bit out

rlcf    bin0,f            ;and into result
rlcf    bin1,f

djnz    bitcnt,gloop        ;do for all bits

return                ;and get out

#### WBahn

Joined Mar 31, 2012
26,398
Hi peeps, I've got myself a EP50S 10 bit gray code encoder and was expecting it to only have two pulse connections, ignoring supplies (something like PH1 & PH2) however it has a cable for each of all 10 bits (I obviously downloaded the wrong data sheet when researching, but the resolution is correct)... so I was wondering if it is at all possible to poll the 8 pins of Port B and say 2 pins of Port A to create my 10 bit code and then convert to Binary or HEX (not sure yet). I should mention that some of Port C is being used for Terminal over EUSART, and PWM and hopefully Port D for reading a gyro (gulp).
Is the encoder suggestion doable and an acceptable approach, or am I barking up the wrong tree? Any comments gratefully received.
Regards
Les
What you were expecting (with the two phase outputs) is an incremental quadrature encoder. What you got appears to be an absolute position encoder that outputs the position in Gray code.

Assuming that it is a reflected Gray code (which it almost certainly is) then converting Gray to binary is very straight forward.

One way is the following (store the graycode in a 16-bit unsigned integer named v).

v ^= v >> 1;
v ^= v >> 2;
v ^= v >> 4;
v ^= v >> 8;