Jump to content

Quadrature Encoder Interface on Launchpad?

Recommended Posts

I am thinking of using a Stellaris Launchpad (LM4F120XL) as a robot controller. If I had something like this (http://www.pololu.com/catalog/product/1551) how would I go about interfacing with the quadrature encoders? This TI webpage says "Advanced motion control capability, with up to 16 PWM outputs and two quadrature encoder interfaces" but I cannot find the terms "quadrature encoder" or "QEI" in the Launchpad manual. Am I going to need to learn the gritty details of quadrature encoding and write my own C interface?


I am very new to all this, so a gentle shove in the right direction would be awesome.

Link to post
Share on other sites

Haven't tried this on the LM4F120 but you should be able to do a software QEI using GPIO interrupts or perhaps the timer capture pins.  


Reading the state of one pin when the other pin transitions will tell you direction.  For example if pin 1 is high when pin 2 goes from high to low, that means either forward or reverse.  If pin 1 were low during the same pin 2 transition that is the opposite direction.  Time difference between edges on either pin should give you velocity. Integrating that to get position usually involves a third pin as an index to help re-zero the position count approximately once per revolution.


Again, haven't done this lately but here is where i would start.


  • Setup and enable a GPIO interrupt routine for the rising edges of the phase B pin. 
  • if( read(phase A pin) == high )
    • position++
    • direction = forward
    • velocity = some simple math based on ticks/revolution, time between now and last tick and perhaps size of the wheel. Using a TImer capture here helps get the best resolution on this.
  • else
    • position--
    • direction = reverse
    • velocity = same math as before * (-1).
  • end ISR 1


I would also setup a second ISR for the index pin if it existed and inside manually zero the position counter.  Make position variable a volatile as a minimum safe gaurd against race conditions.



Link to post
Share on other sites



My first post here, I've been reading the forum a while but now I decided to show myself.


The LM4F120 indeed does not have the hardware QEI modules available on the higher-end models. So you'll need a pure software solution.


I'm currently working on a final year mechatronics project at Aalto University (in Finland), and the device I'm building has three quadrature encoders. I'm using the SLaunchPad as the controller and am now writing my own support libraries on top of StellarisWare. One such library module is a software QEI, having most of the features that the hardware API has, excluding velocity measurement.


Just a fair warning; you can't run a very fast encoder on a software QEI, let alone multiple encoders. I have a 1000 ppr encoder which I spun at increasing speeds until the software reported an error condition (both channels having an active interrupt at the same time). I got to around 2000-3000 rpm, and the decoding was done in 4x mode, so both edges on both channels were counted. Using an idle-while loop counter, I estimated the CPU usage at that point to be around 50%.


I'm not yet publish-confident with the library, but if you give me your email address, I'll send you the source code. The library supports counting in 1x, 2x and 4x modes, has optional support for Z channel and autoconfiguration for the pins.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...