Rotary Encoder debounce and signal processor

matherp

Senior Member
The attached code uses a 08M2 to interface a mechanical 2 bit gray code rotary encoder with an additional switch input.

These are very useful input devices but difficult to debounce to get clean inputs. The code uses a dedicated 08M2 to clean up the signals as follows:

Pushing on the switch gives a 10ms clean output pulse
Turning the encoder through one detent gives a 10ms clean output pulse. The direction is indicated by the state of the direction pin which is set before the leading edge of the pulse.

The code uses all six data lines on a 08M2 so a hardware reset is needed for each programming cycle

These signals should then be very easy to interface to any other circuit

Hope this is useful

Peter
Code:
#picaxe 08M2
symbol switched = b0
'Connections
' Encoder switch pulled high and connected to C.3 (pin4)
' Encoder output1 pulled high and connected to C.4 (pin5)
' Encoder output2 pulled high and connected to C.5 (pin6), must be disconnected for programming
'
' Switch pushed pulse out on C.1 (pin6)
' Direction output on C.0 (pin7), must be disconnected for programming
' Encoder turned pulse on pin C.2 (pin5)
'
symbol encodermask = %00111000 'ports c.3, c.4, c.5 pulled high
symbol interruptmask = %00011000 'allow interrupts on c.3, c.4
symbol rotateleft  = %00001000 ' c.3 high, c.4 and c.5 low
symbol rotateright = %00101000 ' c.3 high, c.4 low c.5 high
symbol clicked     = %00110000 ' c.3 low, c.4 and c.5 high



setint OR 0,interruptmask
low c.0
disconnect
do
loop


interrupt:
	switched=pinsc & encodermask
	if switched = clicked then 
		pulsout c.1,1000
	endif
	if switched = rotateright then
		low c.0
		pauseus 10
		pulsout c.2,1000
	endif
	if switched = rotateleft then
		high c.0
		pauseus 10
		pulsout c.2,1000
	endif
	do
		switched=pinsc & encodermask
	loop until switched=encodermask ' all inputs back to high state
	pause 3 'debounce timer
	setint OR 0,interruptmask
return
 
Last edited:
Top