Thanks to inglewoodpete,
Latest code detects all edges CW and CCW including first edge after reversal.
I still have the LED output in the code, as it's very convenient for quick sanity check, and I still have no counter but I'll fix that now. Next step will be to see if I can shoehorn IR input, SPI output, channel selection and some memory functions into the same PICAXE.
Pops.
Latest code detects all edges CW and CCW including first edge after reversal.
CSS:
'Simple Rotary Encoder Demonstration Programme
'Written by inglewoodpete
'
#PICAXE 40X2
#Terminal 38400 'SerTxd at 32MHz
#No_Data 'Comment this statement out if EEPROM data is to be initialised
'
' Leg
'Symbol iEncoder0 = pinC.6 ' 25 Digital input - bit A of encoder
'Symbol iEncoder1 = pinC.7 ' 26 Digital input - bit B of encoder
'
' **** Variables b0 to b27 or w0 to w13 (Prefix b for byte; w for word)
'
Symbol bByteToShow = b1 'w0 Used to display hexadecimal value (for debugging)
Symbol bCurrentState = b2 'w1 Current copy of encoder output
Symbol bPreviousState = b3 'w1 Previous copy of encoder output
Symbol bLatestTransition= b4 'w2 4-bit value, from Previous and Current states
Symbol bPrevTransition = b5 'w2 Copy of previous transition, in upper 4-bits
Symbol bDirection = b6 'w3 Validated indication of rotation direction
Symbol bTemp = b7 'w3 Used within ShowHex routine
'
' ---------------------------------------------------------------------
Init: SetFreq M16
;PullUp %00011000
Pause 1000 'Allows time for logging terminal to open after download
;b8 = 128
SerTxd (CR, LF, "Rotary Encoder Demo", CR, LF)
;bPreviousState = PinsC And %00011000
bPreviousState = PinsC And %11000000
Do ' **** M a i n L o o p ****
bCurrentState = PinsC And %11000000
low D.2
low D.3
If bCurrentState <> bPreviousState Then 'Has encoder been turned
bByteToShow = bCurrentState
GoSub ShowHex 'Show byte value as hex
SerTxd (" ")
' At this point, bPreviousState = XY000000 and bCurrentState = xy000000
' First, get transition value into lower 4-bits indicating direction (CW or ACW)
' (/ 4) Move previous XY bits right 2 bit-positions: 00XY0000
' (Or) merge current xy bits: xyXY0000
' (/16) Move all 4 bits right by 4 bit positions: 0000xyXY
' This results in previous, current encoder bits appearing in lower nibble
;bLatestTransition = bPreviousState / 4 Or bCurrentState / 16
bLatestTransition = bCurrentState / 4 Or bPreviousState / 16
'
bDirection = bPrevTransition Or bLatestTransition
bByteToShow = bDirection
GoSub ShowHex 'Show byte value as hex
SerTxd(" ")
Select Case bDirection
Case $7E, $E8, $81, $17, $D7, $41, $28, $BE: SerTxd("CW ") high D.2
Case $42, $2B, $BD, $D4, $7D, $EB, $82, $14: SerTxd("CCW ") high D.3
End Select
;debug
;bByteToShow = bLatestTransition
bPreviousState = bCurrentState 'Save for next comparison
bPrevTransition = bLatestTransition * 16 'Copy transition into upper 4-bits
SerTxd(CR, LF)
EndIf
Loop
'
' **** Subroutines *****************************
'
' ---- ShowHex: Send Hexadecimal value to PE Terminal -----------------------------------------
'
' Send a byte as hex to terminal on the programming lead (ShowHex:)
' Send a nibble as hex to terminal on the programming lead (SHexNibl:)
' Alternate "D" entries cause a leading $ sign to be logged
'
' Entry: bByteToShow (byte) data to be transmitted
' Exit: bByteToShow (byte) unchanged
' Used: bTemp (byte)
'
ShowHexD:SerTxd("$") 'Alternate entry
ShowHex: bTemp = bByteToShow / 16 'Get high nibble
GoSub Sh4bits 'Show it
Goto SHexNibl 'Then do low nibble
ShowNibD:SerTxd("$") 'Alternate entry
SHexNibl:bTemp = bByteToShow And %00001111
Sh4bits: bTemp = bTemp + "0" 'Convert to ASCII
If bTemp> "9" Then 'Separate out and
bTemp = bTemp + 7 ' correct over-
EndIf ' decimal part.
SerTxd(bTemp) 'Display it
Return
Pops.