Henrik, maybe a video would be helpful. 1st half is with a encoder with 30detents/15pulses, 2nd half is 20detents (I don't have datasheet for that one, no idea how many pulses).
That rocker switch selects logic; mine is first in video, yours is 2nd when switch is rocked to the right.
My code with combined logic. I added a switch and moved LCDOUT into a subroutine to keep the code clean:
Code:
goto Start
;--- Subroutines ---------------------------------------------------------------
LCDoutput:
LCDOUT $FE, $D4, "C:", dec5 Enc1_counter,_
" A:", DEC1 Enc1_WiperA,_
" B:", DEC1 Enc1_WiperB,_
" SW:", DEC1 Enc1_SPST : Pauseus 1
Return
Start:
Pause 500 ' Let PIC and LCD stabilize
LCDOUT $FE, 1 : Pauseus 1
LCDOUT $FE, $80, "ROTARY ENCODER TEST" : Pauseus 1
Mainloop:
BlinkLED1 = 1 ' Top of LOOP on Logic 2 probe
if logicselector = 1 then goto MyLogic
' Wiper Chart:
' ============
' A B
' --- ---
' 0 0
' 1 0 /\ CCW
' 1 1
' 0 1 \/ CW
' 0 0
'
' Careful, EC11 30 detents 15 pulses will move from 00 to 11
' EC11 20 detents can move from 00 back to 00 in one click
New_Encoder = PortA & %00000011 ' Read encoder signals on RA0 and RA1
Test = New_Encoder ^ Old_Encoder ' Bitwise XOR current state with previous state to see if any pins changed.
IF Test.0 = 1 THEN ' Edge detected on channel A?
IF Old_Encoder.0 ^ Old_Encoder.1 THEN ' If Old_Encoder is 0 or 3 we count up, otherwise we count down.
Enc1_counter = Enc1_counter + 1
ELSE
Enc1_counter = Enc1_counter - 1
ENDIF
BlinkLED1 = 0 ' Bottom of IFs on Logic 2 probe
GOSUB LCDoutput
GOTO MainLoop
ENDIF
IF Test.1 = 1 THEN ' Edge detected on channel B?
IF Old_Encoder.0 ^/ Old_Encoder.1 THEN ' If Old_Encoder is 1 or 2 we count up, otherwise we count down.
Enc1_counter = Enc1_counter + 1
ELSE
Enc1_counter = Enc1_counter - 1
ENDIF
BlinkLED1 = 0 ' Bottom of IFs on Logic 2 probe
GOSUB LCDoutput
Goto MainLoop
ENDIF
BlinkLED1 = 0 ' Bottom of IFs on Logic 2 probe
GOSUB LCDoutput
goto mainloop
MyLogic:
if Enc1_WiperA = 0 and Enc1_WiperB = 0 then ' See wiper chart above
if Enc1_previous = 01 then ' 2 digits to follow chart better
Enc1_direction = 1 ' 1=CW, 0=CCW
Enc1_rotation = 1 ' Motion occurred
else
if Enc1_previous = 10 then
Enc1_direction = 0 ' 0=CCW
Enc1_rotation = 1 ' Motion occurred
else
Enc1_direction = 0 ' Not relevant without motion
Enc1_rotation = 0 ' No motion occurred
endif
endif
Enc1_previous = 00 ' Save wiper position
endif
if Enc1_WiperA = 1 and Enc1_WiperB = 0 then
if Enc1_previous = 00 then
Enc1_direction = 1
Enc1_rotation = 1
else
if Enc1_previous = 11 then
Enc1_direction = 0
Enc1_rotation = 1
else
Enc1_direction = 0
Enc1_rotation = 0
endif
endif
Enc1_previous = 10
endif
if Enc1_WiperA = 1 and Enc1_WiperB = 1 then
if Enc1_previous = 10 then
Enc1_direction = 1
Enc1_rotation = 1
else
if Enc1_previous = 01 then
Enc1_direction = 0
Enc1_rotation = 1
else
Enc1_direction = 0
Enc1_rotation = 0
endif
endif
Enc1_previous = 11
endif
if Enc1_WiperA = 0 and Enc1_WiperB = 1 then
if Enc1_previous = 11 then
Enc1_direction = 1
Enc1_rotation = 1
else
if Enc1_previous = 00 then
Enc1_direction = 0
Enc1_rotation = 1
else
Enc1_direction = 0
Enc1_rotation = 0
endif
endif
Enc1_previous = 01
endif
if Enc1_rotation = 1 then
if (Enc1_WiperA = 0 and Enc1_WiperB = 0) or _
(Enc1_WiperA = 1 and Enc1_WiperB = 1) then
if Enc1_direction = 1 then
Enc1_counter = Enc1_counter + 1 ' Turned 1 position CW
else
if Enc1_counter > 0 then
Enc1_counter = Enc1_counter - 1 ' Turned 1 position CCW
endif
endif
endif
endif
GOSUB LCDoutput
goto mainloop
Bookmarks