Back to Rotary Encoder Report


'from http://www.picbasic.co.uk/forum/showthread.php?t=778
'encoder contact pins in portb.0 and b.1
' the encoder flips contacts like this
'01
'00
'10
'11

include "modedefs.bas"
OldPos var byte
NewPos var byte
value var byte
value=127

main: 
OldPos = PORTB & %00000011  'compare the first two bits of portb's current state. Only the high bits will remain

while (NewPos==0) or (NewPos==3)  ' I believe this code is actually skipping 0 and 3 and waiting for a more readable position.
      NewPos = portB & %00000011  ' sample the encoder
wend

select case NewPos  '
       case 1   'if the encoder is 1
            select case OldPos
                   case 0  'and the previous position was 0, then you're going forwards
                        value=value+1
                   case 3 ' or the previous position was 3, then you're going backwards
                        value=value-1
            end select
       case 2  if the current encoder is 2
            select case OldPos
                   case 3  ' and the previous position was 3, going forwared
                        value=value+1
                   case 0  ' or the previous position was 0, going forwared
                        value=value-1
            end select
end select



NewPos=OldPos

serout2 tx, 16468 [DEC value]
goto main