PDA

View Full Version : Farnell's Rotary Encoder 733-726



crematory
- 7th September 2005, 21:44
Hello

I was looking for a rotary encoder and I found this 733-726, you can download its datasheets from here

www.farnell.com/datasheets/48122.pdf

I thought I can post the code I wrote for this encoder here before ordering...

Please if any body got this kind of rotary encoders, then try this code and tell me if it works or not, also if any one has a suggestion to improve the code, don't hesitate to share....

@ DEVICE PIC16F84A, HS_OSC, WDT_ON, PWRT_ON, PROTECT_OFF

DATA $C0,$F9,$A4,$B0,$99,$92,$82,$F8,$80,$90

Define Osc 4

TRISA = %00011
TRISB = 0
PORTA = 0
PORTB = 0
INTCON = 0

Index Var Byte : Index = 0

A var PortA.0
B vAR PortA.1


ATemp var bit
BTemp var bit


Main:

atemp = a : btemp = b

if atemp == btemp then
while a == b : wend
else
goto main
ENDIF


PAUSE 1
if atemp <> a AND BteMP == B then VOL_UP ' CW Rotation Checking
IF ATEMP == A AND BTEMP <> B THEN VOL_DOWN ' CCW Rotation Checking


' Indicate the incoming pulses and rotation direction
' with a 7-segment display.... Very Nice indeed....
VOL_UP:
read Index, PortB
If index == 10 then
index = 0
else
Index = Index + 1
endif
GOTO MAIN

VOL_DOWN:
read index, PortB
If Index == 0 then
Index = 9
else
index = index - 1
endif
Goto main

END

A seven segmet display is used to allow visual indication of response, I tested this code with a wheel from an old mouse, it has three wires, black, red, and yellow, but results were annoying because of the bad wheel signals, but in essense the code is working fine...as I tried to pulse the inputs with a wire and three fingers...when I squeeze the wheel a little bit, it works very nice :)

mister_e
- 8th September 2005, 14:49
i don't know if will be handy but here's a snip of my encoder routine


GetEncoder:
'
OldPos = (PORTB & %11000000)>>6 ' get only PORTB<7:6> bits
NewPos = OldPos

' see if encoder has move
'
while ((NewPos==0) or (NewPos==3))
NewPos = (PORTB & %11000000)>>6 ' get only PORTB<7:6> bits
wend
pause 20 ' debounce delay

' Get encoder movement
' --------------------
'
' Rotary encoder output table (clockWise rotation):
' -------------------------------------------------
' 00
' 01 between move (half detent)
' 11
' 10 between move (half detent)
'
' Using XOR
'
' OldPOS : 0 0 3 3
' NewPos : 1 2 1 2
' movement : CW CCW CCW CW
' XOR -------------------------------
' 1 2 2 1
'
' So, if NewPos XOR OldPos = 1 => increment
' Newpos XOR OldPos = 2 => Decrement
'
Newpos=newpos ^ oldpos
select case newpos

case 1
Menupointer=menupointer+1

case 2
Menupointer=menupointer-1

end select

Yeah i know, more comments than code but.. i like it ;)

wallaby
- 8th September 2005, 23:12
I have used these encoders and still have problems, I tried code supplied on this forum and it still does the same thing, it jumps up by 2 values instead of one ( rotate right one click generates 2 counts ). I do have the board set up still so I will try both code examples and advise.

Nigel, Queensland, Australia

crematory
- 9th September 2005, 20:40
Hello mister_e

Thanks for the nice code.... and comments also :)

I think I found my way of how to use these evil rotary encoders...

Now, I don't know how to explain this very well, but I think the code will do..

Code:


@ DEVICE PIC16F84A, XT_OSC, PWRT_ON, PROTECT_OFF, WDT_ON

TRISA = 3
TRISB = 0

PORTA = 0
PORTB = 0

NEW VAR BYTE
OLD VAR BYTE
TEMP VAR BYTE
DIRECTION VAR BIT

INIT:
TEMP = PORTA
NEW = TEMP & 3 ' PortA<1,0> only required as inputs<B,A>
START:
OLD = NEW
BEGIN:
TEMP = PORTA ' Read the new value to see if changed
NEW = TEMP & 3
IF NEW == OLD THEN BEGIN ' If not changed, keep reading
DIRECTION = NEW.1 ^ OLD.0 ' XOR left bit of new and right bit of old
IF DIRECTION = 1 THEN CLOCKWISE

COUNTERCLOCKWISE:
'
'
GOTO START


CLOCKWISE:
'
'
GOTO START



I tested this code with a mouse wheel, and it worked perfectly without any problems, I used 7 segment display to monitor the results. Tomorrow I will order the rotary encoder, and see if it also works as I intended...

This method prevents double stepping each time the nob is rotated, try it guys, and tell me what is your experience like...

keithdoxey
- 11th October 2005, 18:20
I have used these encoders and still have problems, I tried code supplied on this forum and it still does the same thing, it jumps up by 2 values instead of one ( rotate right one click generates 2 counts ). I do have the board set up still so I will try both code examples and advise.

Nigel, Queensland, Australia

Hi Nigel,

I am about to use a rotary encoder for the first time and whilst trying to confirm the pinout with a multimeter I couldnt get any readings across any pins. I then went and found the datasheet and discovered I have bought a high resolution encoder.

My encoder has 24 detents in common with many others, but many of the encoders have only 6 cycles per revolution so 4 clicks to go through 00,01,11,10

Mine apparently has 24 cycles per revolution so a single click travels through 00,01,11,10 and back to 00. Basically whenever the encoder is resting on a detent BOTH switches are open. I confirmed this by using LED's and you can see the 4 steps during each click.

Given that the code above will generate an output count whenever 00 or 11 are presented I guess that using the type of encoder I have will give a count of two for each click.

Just need to code for that now :)

Regards

Dave
- 12th October 2005, 12:51
keithdoxey, I am the one that supplied Nigel with the code and it works perfectly with a standard encoder, however the encoder you have selected as well as Nigel are ment to be used in low power situations. That is why the encoder seems to be open circuited at rest or in the detented position. As I told Nigel, take the encoder count and divide it by 2 is the easiest way to use these type of "low power" encoders.

Dave Purola,
N8NTA

keithdoxey
- 12th October 2005, 13:51
As I told Nigel, take the encoder count and divide it by 2 is the easiest way to use these type of "low power" encoders.


Hi Dave,

Excellent idea. Just because I need a result of 0-63 doesnt meant I cant use an encoder value of 0-126 in steps of two.

Will give it a try later.

Thanks