here is a bit of code ive used many times, its written for picbasic pro,,it expects a 2 bit gray code input..hope its what your after
edit:::: in response to a couple of queries...the code is set up for 2 encoders...the OUTPUTS from each encoders is tied to +5v via 2x 10k resistors
the COMMON terminal of the encoder is tied to 0v
the encoder outputs are connected to portB 4 & 5 and the other encoder to portB 2 & 3
the code outputs a SERIAL interface to a lcd, and simply presents a 'count' of the position of the encoder,, this is easily converted to a standard parallel LCD in the usual PBP way
ive not simulated this with proteus, although i have downloaded the 'ROTENC' model elsewhere on this site...and after a few false starts it seems to work ok..if anyone gets it working pls post the project!!!!
here is an example of a cheap encoder
http://cgi.ebay.co.uk/Rotary-Encoder-push-button-switch-Ideal-for-Data-Entry_W0QQitemZ270130801383QQihZ017QQcategoryZ58169QQrdZ1QQssPageNameZWD1VQQcmdZViewItemQQ_trksidZp1638Q2em118Q2el1247end edit::::
include "modedefs.bas"
zFlags var byte
Changed var zflags.0
OldA var byte ' Previous state of encoder A bits
NewA var byte ' Current state of encoder A bits
DirectionA var bit ' Direction of encoder A travel; 1=CCW.
zza6 var NewA.0
zza15 var OldA.1
PositionA var byte
OldB var byte ' Previous state of encoder B bits
NewB var byte ' Current state of encoder B bits
DirectionB var bit ' Direction of encoder B travel; 1=CCW.
zzb6 var NewB.0
zzb15 var OldB.1
PositionB var byte
true con 1
false con 0
PositionA = 0
PositionB = 0
' Rotary encoders connected as follows:
'RenA1 var PortB.5
'RenA2 var PortB.4
'RenB1 var PortB.3
'RenB2 var PortB.2
NewA = PortB & %00110000
NewB = PortB & %00001100
NewA = NewA >> 4
NewB = NewB >> 2
pause 500
'LCDOut LCDCmd, LCDClear
pause 200
MainLoop:
OldA = newA
OldB = newB
Changed = false
Again:
NewA = PortB & %00110000
NewB = PortB & %00001100
NewA = NewA >> 4
NewB = NewB >> 2
IF NewA <> OldA Then Gosub ChangedA
IF NewB <> OldB Then Gosub ChangedB
if Changed = false then Again
'serout 7,t2400,$fe[#PositionA]
serout 7,t2400,[$fe,1]
serout 7,t2400,[#PositionB]
Goto MainLoop
ChangedA:
DirectionA = zza6 ^ zza15 ' XOR right bit of new w/ left bit of old.
IF DirectionA = 1 Then
PositionA = PositionA - 1
else
PositionA = PositionA + 1
pause 10
endif
Changed = true
Return
ChangedB:
DirectionB = zzb6 ^ zzb15 ' XOR right bit of new w/ left bit of old.
IF DirectionB = 1 Then
PositionB = PositionB - 1
pause 10
else
PositionB = PositionB + 1
pause 10
endif
Changed = true
Return