Test program for quadrature encoder interface

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002  
00003 DigitalIn phaseA( p5 ); // phase a of the quadrature encoder
00004 DigitalIn phaseB( p6 ); // phase b of the quadrature encoder
00005  
00006 int encoderClickCount    = 0; // hold the signed value corresponding to the number of clicks left or right since last sample
00007 int previousEncoderState = 0; // keep a record of the last actioned sample state of the Qb+Qa outputs for comparison on each interrupt
00008  
00009 void quadratureDecoder( void )
00010 {
00011     int currentEncoderState = (phaseB.read() << 1) + phaseA.read(); // create a two bit value out of phaseB and phaseA
00012     
00013     if( currentEncoderState == previousEncoderState )
00014     {
00015         return;
00016     }
00017     
00018     switch( previousEncoderState )
00019     {
00020         case 0:
00021             if( currentEncoderState == 1 )
00022             {
00023                 encoderClickCount--;
00024             }
00025             else if( currentEncoderState == 2 )
00026             {
00027                 encoderClickCount++;
00028             }
00029             break;
00030             
00031         case 1:
00032             if( currentEncoderState == 3 )
00033             {        
00034                 encoderClickCount--;
00035             }
00036             else if( currentEncoderState == 0 )
00037             {
00038                 encoderClickCount++;
00039             }
00040             break;
00041             
00042         case 2:
00043             if( currentEncoderState == 0 )
00044             {
00045                 encoderClickCount--;
00046             }
00047             else if( currentEncoderState == 3 )
00048             {
00049                 encoderClickCount++;
00050             }
00051             break;
00052             
00053         case 3:
00054             if( currentEncoderState == 2 )
00055             {
00056                 encoderClickCount--;
00057             }
00058             else if( currentEncoderState == 1 )
00059             {
00060                 encoderClickCount++;
00061             }
00062             break;
00063 
00064         default:
00065             break;
00066     }
00067     previousEncoderState = currentEncoderState;
00068 }
00069  
00070  // read the rotation accumulator, and once read, reset it to zero
00071 int getClicks( void )
00072 {
00073    int res = encoderClickCount; // this allows the knob to be rotated "while im not looking at it' and still return the 
00074     
00075     encoderClickCount = 0;      // actual number of clicks that have been rotated since last time I was checking it.
00076 
00077     return res;
00078 }
00079  
00080 int main()
00081 {
00082     Serial pc( USBTX, USBRX );
00083     Ticker sampleTicker;                                // create a timer to sample the encoder
00084     int    currentClicks;
00085 
00086     phaseA.mode( PullUp );                              // phaseA is set with a built in pull-up resistor
00087     phaseB.mode( PullUp );                              // phaseB is set with a built in pull-up resistor    
00088     sampleTicker.attach_us( &quadratureDecoder, 1000 ); // make the quadrature decoder function check the knob once every 1000us = 1ms
00089 
00090     pc.printf( "Encoder test started\r\n" );
00091     while( 1 )
00092     {
00093         currentClicks = getClicks();
00094         if( currentClicks != 0 )
00095         {
00096             pc.printf( "click count = %d,%d\r\n", currentClicks, previousEncoderState );
00097         }
00098         wait_ms( 200 );
00099     }
00100 }