My rotary test

/media/uploads/weeliat/rotarytest1.txt

Trying out how to increase and decrease number with a rotary encoder and print it on a lcd screen. I'm using a sparkfun rotary encoder: http://www.sparkfun.com/products/9117

I'm pretty stuck though..


6 comments on My rotary test:

15 Nov 2011

An rotary encoder is simply two switches with an offset angle. You can see in the datasheet how they are working. perhaps this small piece of (old 8051 sdcc) code will give a hint of how the encoder works: You need to keep the former encoder state in memory and compare it to the current state. With two bits you have 4 possibilities. The encoder_up and encoder_down functions are called depending on the direction of the turn. The fucntion is called with a unsigned char containing the state of the two input pins.

// rotary encoder routine
// the encoder has 4 states
// 00 01 11 10
do_encoder(unsigned char c)
{
	switch (OLD_ENCODER)
	{
		case 0x00:  // both inputs low 
			if (c == 1) encoder_up();
			if (c == 2) encoder_down();
			break;	
				
		case 0x01: 
			if (c == 3) encoder_up();
                        if (c == 1) encoder_down();
                        break;
	
		case 0x02:
			if (c == 0) encoder_up();
                        if (c == 3) encoder_down();
                        break;
		case 0x03:  // both inputs high
			if (c == 2) encoder_up();
                        if (c == 1) encoder_down();
                        break;
	}
	OLD_ENCODER=c;   // save current state in OLD_ENCODER

15 Nov 2011

Hello there. I'm pretty bad at programming, so thanks for taking time to reply me. I'm quite sure my rotary encoder is a 3bit type. That means 8possibilities..

By encoder_up(); and encoder_down(); , it refers to clockwise turning and anti-clockwise turning respectively right? Also by switch (OLD_ENCODER), does it mean that I have to define it/ create a function for it?

Once again thanks!

15 Nov 2011

Well from the datasheet I see only three pins for the encoder and two for the switch. The remaing two are just mounting studs. So with three pins this means there is one common 'C' and the two switches A and B You connect the A and B connections of the encoder to two input pins of the mbed and connect the common to ground.

The order in which these switches close and open is:

AB AB AB AB AB AB AB AB AB

00 01 11 10 00 01 11 10 00 .... and so on and so on (note this is not binary but gray coded meaning only 1 bit will change at a time)

By storing the former state of the two bits you can determine which direction the encoder is turned:

when 'old_encoder' equals 00 and the new value is 01 it is rotating clockwise and when you read 10 you know it is rotating anticlockwise (or vice versa) For all 4 possible states you need to make some test functions.

15 Nov 2011

Have a look at Quadratur Encoder This does all the work for you ! :)

16 Nov 2011

if you want to learn how things work then simply cutting and pasting is not the answer :-)

17 Nov 2011

Gert van der Knokke wrote:

Well from the datasheet I see only three pins for the encoder and two for the switch. The remaing two are just mounting studs. So with three pins this means there is one common 'C' and the two switches A and B You connect the A and B connections of the encoder to two input pins of the mbed and connect the common to ground.

The order in which these switches close and open is:

AB AB AB AB AB AB AB AB AB

00 01 11 10 00 01 11 10 00 .... and so on and so on (note this is not binary but gray coded meaning only 1 bit will change at a time)

By storing the former state of the two bits you can determine which direction the encoder is turned:

when 'old_encoder' equals 00 and the new value is 01 it is rotating clockwise and when you read 10 you know it is rotating anticlockwise (or vice versa) For all 4 possible states you need to make some test functions.

Hello Gert,

Yeap. Now I understand. Thanks alot for the help! :D

Please log in to post comments.