PDA

View Full Version : Encoder pin out question



Johan
- 25th July 2007, 09:19
Hi,

I just got an incremental encoder, Bourns type PEC 11 ( 2 bit gray code ) and I'd like to experiment with it , but I never see an encoder before :)

It has 3 legs marked A,C,B ( on datasheet ) ,could anyone kindly tell me how to connect it ?

Thank you,

Johan

HenrikOlsson
- 25th July 2007, 15:50
Hi,
Connect A & B to your PIC inputs and pull them low with, say between 1k & 10k. Connect C to Vdd and you should be in good shape. Here's a datasheet (http://www.bourns.com/pdfs/PEC11.pdf) for it.

/Henrik Olsson.

Johan
- 25th July 2007, 16:01
Thank you Hendrik,

I have done as you suggest, but I could not get result with my code below.

I found the code somewhere , and I tried to port to PBP, I am not sure if it is correct
I use PIC 18F4431



DEFINE OSC 16 ' Oscilator HS
clear
TRISA = %00000000 ' Set PORTA to all output
TRISB = %00000011 ' Set PORTB.2 -7 to output
TRISC = %00000000 ' Set PORTC to all input
TRISD = %00000000 ' Set PORTD to all output
TRISE = %00000000 ' Set PORTE to all output

ANSEL0= %00000000 ' Analog channel AN0-AN7 off
ANSEL1= %00000000 ' Analog channel AN8 off

'PROGRAM TO DETERMINE DIRECTION OF QUADRATURE ENCODER

portb_old var BYTE

'RULE FOR A 2-BIT (QUADRATURE SYSTEM):
'CLOCKWISE = LOW BIT OF OLD VALUE IS ALWAYS EQUAL TO HIGH BIT OF NEW VALUE
'COUNTERCLOCKWISE = LOW BIT OF OLD VALUE DOES NOT EQUAL HIGH BIT OF NEW VALUE

main:



PORTA = 0 'RA0 LED will indicate CW; RA1 LED will indicate CCW
PORTB = 0 'RB0 and RB1 as inputs for encoder A & B

portb_old = 0

my_direction:

IF PORTB XOR portb_old <> 0 THEN
PORTA = ((PORTB.1 XOR (portb_old AND 1)) + 1) 'cw = 1; ccw = 2
ENDIF

portb_old = PORTB

GOTO my_direction

end