asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
SIMPLE QEI relative && absolute encoder driver NOINT |
Posted: Fri Mar 11, 2016 1:54 pm |
|
|
SUPER SIMPLE rotary encoder: Gray and '2bit' relative in software
QEI without hardware.......
Suitable only for hand operated encoders at low speeds
Non interrupt code for simple handling of both 2 bit incremental encoders
and grey code absolute position encoders
incremental connection: A>-pin_B0 B>-pin_B1
Position is set from +/- 32767 counting UP CW and down CCW
rollover not handled in the driver
Because we use gray code and not straight hex - there is no need to
de-bounce the absolute encoder - and direction can be determined for it
by comparing new count to previous count.
Code: |
// lookup table for Gray_number to linear ordinal position
const unsigned int8 GCD[16]={0,1,3,2,7,6,4,5,15,14,12,13,8,9,11,10 }
unsigned int8 old,new,direction=1;
signed int16 clock=0,index=0;
/////////////////////////////////////////////////////////////////
// incremental 2 bit
/////////////////////////////////////////////////////////////////
// iniitialize code for incremental
// usually done in main()
old=input_B()&3;
// counts using Spectrol DE2 - series
// 128 clocks per rev with 32 indexes
// call in a tight loop in main() when input is expected
#INLINE
void handle_incremental(void){
new=input_B()&3;
if(new==old) return;
IF(!NEW){ // at zero index position set direction
if(2==old) { direction=1; ++index; } // fix index count
else { direction=0; --index; }
} // end if (!new)
// adjust total clock tix of encoder
// 4 per index count
if (direction) ++clock;
else --clock;
old=new; // old is the new , new
}
/////////////////////////////////////////////////////////////////
// 4 bit connections to B0-B3 pulled UP to +5 -common is ground
// return sequential ABSOLUTE position from 0-15
// from 16 position absolute detented gray code encoder
// like Greyhill 25LBG units
/////////////////////////////////////////////////////////////////
// no init for absolute
#inline
unsigned int8 get_absolute(void){
return(GCD[(~input_B())&15]);
}
|
|
|