CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug Reports on this forum. Send them to support@ccsinfo.com

SIMPLE QEI relative && absolute encoder driver NOINT

 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
asmboy



Joined: 20 Nov 2007
Posts: 2116
Location: albany ny

View user's profile Send private message AIM Address

SIMPLE QEI relative && absolute encoder driver NOINT
PostPosted: Fri Mar 11, 2016 1:54 pm     Reply with quote

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]);

}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group