|
[DPRG] Quadrature encoder C function
Subject: [DPRG] Quadrature encoder C function
From: Rick Bickle
rbickle at intconsys.com
Date: Wed Aug 13 15:32:13 CDT 2008
Scott,
I guess I'm a little late with the response, but here's my 2 cents
worth. I do a lot of encoders...
I generally trigger a hardware interrupt with one or both channels of
the quadrature sensor. To determine direction, simply check the state of
the other channel when one is triggered. You don't need to store the
previous states. For debouncing, I would look at the duration of the
contact bounce on the scope, and characterize the length of time the
bouncing lasts; Then put a slightly longer delay in the code just after
the trigger occurred to prevent re-triggering of the same encoder
transistion. Of course, this is also going to limit your maximum
speed...
Good luck,
Rick
-----Original Message-----
From: dprglist-bounces at dprg.org [mailto:dprglist-bounces at dprg.org] On
Behalf Of scott at lighthouse21.com
Sent: Thursday, August 07, 2008 2:12 AM
To: dprglist at dprg.org
Subject: [DPRG] Quadrature encoder C function
Hello all,
I'm trying to read a quadrature encoder that's part of a
front-panel.
It's turned by a human so changes are intermittent and not terribly
fast (relatively speaking). Here's the function I'm currently using
unsigned long
encoder()
{
static unsigned char ucOldValue;
static int encoderValue = 1;
_delay_ms(1);
if ( ( ( PINB >> PB4) & 1) == 0 ) ucOldValue = 0;
if ( ( ( PINB >> PB4) & 1 ) == 1 && ucOldValue == 0 )
{
if ( ( ( PINB >> PB3) & 1 ) == 1 ) encoderValue ++;
else encoderValue --;
ucOldValue = 1;
}
return encoderValue;
}
The _delay_ms(1) is my attempt to debounce the switch (its a physical
contact encoder) but the output is sporadic. About half the time it
works
as expected and half the time it randomly increments and decrements the
return value even though I'm only turning the encoder in a single
direction. Any thoughts as to how to clean up the output?
Thanks,
Scott
_______________________________________________
DPRGlist mailing list
DPRGlist at dprg.org
http://list.dprg.org/mailman/listinfo/dprglist
More information about the DPRG mailing list
|