Home - Search - Members
Full Version: Shaft Encoder code needed
nicholastyc
May 7 2008, 10:43 PM
Hi,
I think of it ...but could not figure out a right way, please help.
I need a shaft encoder code on direction.

Need help...

thanks

Hi,
i found a code bbut i don really understand about the last 2 line,
--------------------------------------------------------------------------------------------------
extern signed short angle; /* rotation since reset */
static unsigned char ab = 0; /* the old value of the sensor ports */
const signed short table[] = {0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0};
/* increment of angle for the 16 possible bit codes */
ab = ab << 2; /* move the old data left two places */
ab |= (port & 0x3); /* OR in the two new bits */
angle += table[(ab & 0xf)]; /* get the change from the 16 entry table */

-------------------------------------------------------------------------------------------------
http://www.mkesc.co.uk/ise.pdf

details on the document...

Thanks...
what i want to know is ... wat is the port stated here and why 0&3 ?
then... angle += table[(ab & 0xf)]; then wat is this means?
Ajay
May 8 2008, 4:53 PM
There can be three cases:
first shaft rotated clockwise: +1
shaft rotated anti-clockwise: -1
shaft did not rotate: 0

as per the datasheet of encoder, there are four cases for +1 and four for -1 rest all are 0.
CODE:
//By default or at starting ab is 0
//means your shaft did not move or still
ab = ab << 2;
// Now you shifted the already read value of ab by 2 bits
// so that you can accommodate new two bits from encoder
// there new two bits give the current movement of shaft
ab |= (port & 0x3);
// now you read the two bits from port and appended it with old bits
// and you have a 4bit number now
// see table on page 2 of datasheet, every number tells you whether your shaft moved clockwise or anti-clockwise.
// these values are stored in array and read with current value of ab
 

This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Rickey's World © 2003 - 2007