Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

rotary encoder

Status
Not open for further replies.

adrianvon

Member
Hi all,

I wish to use a rotary encoder instead of two buttons to scroll a menu on the LCD. Is it hard to interface a rotary encoder with the PIC ? and does anyone know of a good tutorial to get me started?

Thanks in advance.
 
Piclist is always a good start for code.

I found this there:-

http://www.piclist.org/techref/microchip/qenc.htm
The problem with these is that the programmers have optimized (or tried to optimize) the code to such a point that it can be a bit difficult to follow what is happening.

Less optimum, but easier to understand, is to treat one of the encoder inputs as a clock and the other input as data. It doesn't matter which is which. Whenever the clock goes from zero to one, look at the data input and if it's 0, then the direction is CCW. If the data input is 1, then the direction is CW. If this seems to work backwards, then just reverse the sense of either the data input or the clock input.

Depending on how you arrange your hardware, you can do a few things that will allow you to optimize the code significantly, and of course, make it harder to understand. :D

I've used the following code in my projects. It appears to use fewer instructions than any of the examples in the referenced link. The only requirement is that the inputs must appear in adjacent bits in the input register.
:
Code:
;	Check for an input change of state
	movf	gpio,w	; read inputs
	xorwf	LastEnc,w	; compare to previously saved encoder status
	andlw	030h	; mask off all except bits 4 and 5
	btfsc	status,z	; zero flag is clear if encoder status has changed
	goto	EncEnd	; Skip around next section if no status change
;
;	Encoder inputs have changed, so determine direction and update Counter
	bcf	STATUS,RP0	; Sel Bank 0
	rrf	LastEnc,w	; get previous bit 5 value as bit 4
	xorwf	gpio,w	; and xor it with current bit 4
	andlw	010h	; clear all but bit 4
	btfss	status,z	; increment (+1)
	movlw	0feh	; decrement (-1)
	addlw	1
	addwf	counter,f	; add to counter
	movf	gpio,w	; get current encoder status again
	movwf	LastEnc	; and save value for next time
EncEnd
;	End of Encoder service section

In this example, the encoder inputs were bits 4 and 5 of GPIO. The first 5 instructions test to see if a change has occurred in either of the inputs. If this routine is called only after a change has been detected, then these instructions can be deleted reducing the code to just 10 instructions. The result is used to to either increment or decrement (depending on direction of encoder rotation) a counter register "Counter."
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top