øõTRiLOGI Ver 5.0 ~ ~ 0,A 1,B 16,ANext_Dec 17,BNext_Dec 18,ANext_Inc 19,BNext_Inc ~ ~ 8,StateCntr 3 ~ $SM Based Quadradture Decoder V2.pc6 2/19/2020þ Corrected state table. I screwed up 2 of the transition rules.þ $Simple state machine based quadrature decoder. Gary Dickinson 2/18/2020þþþ %LInitDecoder=1st.Scan $The following two rungs compare the current value of theþA and B INPUTs with the values of the next of the previousþstate.þþThe PV of the COUNTER, StateCntr, is the current state. þþThe Custom Functions, NextState and Previous state updateþthe RELAYS ANext_inc, BNext_Inc, ANext_Dec and BNext_Decþfolloiwng a change of state.þ %2StateCntr|%LNextState=(A*ANext_Inc+/A*/ANext_Inc)*(B*BNext_Inc+/B*/BNext_Inc) %0StateCntr|%LPreviousState=(A*ANext_Dec+/A*/ANext_Dec)*(B*BNext_Dec+/B*/BNext_Dec) ~END_CIRCUIT~ È Fn#0,1127 ' InitDecoder - Build State Table in DM and copy current state to RELAY[2] ' /* The state machine based Quadrature Decoder uses a data table that has holds the values of the next and previous states for each of the four possible states. The data table is stored in DM[] and is used to update RELAY[2] after each change in state. RELAY[2] references 16 sequential RELAYS. Current Next Inc Next Dec State B A State B+ A+ State B- A- Binary Hex ------------------------------------------------------------------- 0 0 0 1 0 1 3 1 0 0110 &h0006 1 0 1 2 1 1 0 0 0 1100 &h000C 2 1 1 3 1 0 1 0 1 1001 &h0009 3 1 0 0 0 0 2 1 1 0011 &h0003 RELAY[2] Mapping: Relay Name Relay# Entry Bit# in DM[] ---------------------------------------- ANext_Dec 17 A- 0 BNext_Dec 18 B- 1 ANext_Inc 19 A+ 2 BNext_Inc 20 B+ 3 */ ' Build the state table in DM[] ' DM[StateTableBase + 0] = &h0006 DM[StateTableBase + 1] = &h000C DM[StateTableBase + 2] = &h0009 DM[StateTableBase + 3] = &h0003 ' Set the initial state to 0 ' CtrPV[StateCntr] = 0 RELAY[2] = DM[StateTableBase + CtrPV[StateCntr]] È Fn#1,427 ' NextState - Load RELAY[2] with the next state (Positive Movement) ' RELAY[2] = DM[StateTableBase + CtrPV[StateCntr]] If CtrPV[StateCntr] = 0 ' The state machine just went from 0-->3. ' This is the time to increment the position count. ' ' This is the correct count behavior for a 1x encoder. ' If your encoder has a 2x or a 4x behavior you will ' need to increment on additional state transitions. ' n = n + 1 endif È Fn#2,390 ' NextState - Load RELAY[2] with the next state (Negative Movement) ' RELAY[2] = DM[StateTableBase + CtrPV[StateCntr]] If CtrPV[StateCntr] = 3 ' The state machine just went from 0-->3. ' This is the time to decrement the position count for a 1x ' encoder ' ' If your encoder has a 2x or a 4x behavior you will ' need to decrement on additional state transitions. ' n = n - 1 endif ~END_CUSTFN~ 0,InitDecoder 1,NextState 2,PreviousState ~END_CUSTFNLABEL~ 0,0, 1,0, 2,0, 3,0, 4,0, ~END_QUICKTAGS~ 1,StateTableSize,4 2,StateTableBase,1 ~END_DEFINES~ ~END_BREAKPOINTS~ 192.168.1.16:9080 ~END_LASTIPADDR~