Test program for quadrature encoder interface

Dependencies:   mbed

Committer:
zoot661
Date:
Mon Dec 31 15:22:03 2012 +0000
Revision:
0:75e3cf27c60b
First version of quadrature encoder test program

Who changed what in which revision?

UserRevisionLine numberNew contents of line
zoot661 0:75e3cf27c60b 1 #include "mbed.h"
zoot661 0:75e3cf27c60b 2
zoot661 0:75e3cf27c60b 3 DigitalIn phaseA( p5 ); // phase a of the quadrature encoder
zoot661 0:75e3cf27c60b 4 DigitalIn phaseB( p6 ); // phase b of the quadrature encoder
zoot661 0:75e3cf27c60b 5
zoot661 0:75e3cf27c60b 6 int encoderClickCount = 0; // hold the signed value corresponding to the number of clicks left or right since last sample
zoot661 0:75e3cf27c60b 7 int previousEncoderState = 0; // keep a record of the last actioned sample state of the Qb+Qa outputs for comparison on each interrupt
zoot661 0:75e3cf27c60b 8
zoot661 0:75e3cf27c60b 9 void quadratureDecoder( void )
zoot661 0:75e3cf27c60b 10 {
zoot661 0:75e3cf27c60b 11 int currentEncoderState = (phaseB.read() << 1) + phaseA.read(); // create a two bit value out of phaseB and phaseA
zoot661 0:75e3cf27c60b 12
zoot661 0:75e3cf27c60b 13 if( currentEncoderState == previousEncoderState )
zoot661 0:75e3cf27c60b 14 {
zoot661 0:75e3cf27c60b 15 return;
zoot661 0:75e3cf27c60b 16 }
zoot661 0:75e3cf27c60b 17
zoot661 0:75e3cf27c60b 18 switch( previousEncoderState )
zoot661 0:75e3cf27c60b 19 {
zoot661 0:75e3cf27c60b 20 case 0:
zoot661 0:75e3cf27c60b 21 if( currentEncoderState == 1 )
zoot661 0:75e3cf27c60b 22 {
zoot661 0:75e3cf27c60b 23 encoderClickCount--;
zoot661 0:75e3cf27c60b 24 }
zoot661 0:75e3cf27c60b 25 else if( currentEncoderState == 2 )
zoot661 0:75e3cf27c60b 26 {
zoot661 0:75e3cf27c60b 27 encoderClickCount++;
zoot661 0:75e3cf27c60b 28 }
zoot661 0:75e3cf27c60b 29 break;
zoot661 0:75e3cf27c60b 30
zoot661 0:75e3cf27c60b 31 case 1:
zoot661 0:75e3cf27c60b 32 if( currentEncoderState == 3 )
zoot661 0:75e3cf27c60b 33 {
zoot661 0:75e3cf27c60b 34 encoderClickCount--;
zoot661 0:75e3cf27c60b 35 }
zoot661 0:75e3cf27c60b 36 else if( currentEncoderState == 0 )
zoot661 0:75e3cf27c60b 37 {
zoot661 0:75e3cf27c60b 38 encoderClickCount++;
zoot661 0:75e3cf27c60b 39 }
zoot661 0:75e3cf27c60b 40 break;
zoot661 0:75e3cf27c60b 41
zoot661 0:75e3cf27c60b 42 case 2:
zoot661 0:75e3cf27c60b 43 if( currentEncoderState == 0 )
zoot661 0:75e3cf27c60b 44 {
zoot661 0:75e3cf27c60b 45 encoderClickCount--;
zoot661 0:75e3cf27c60b 46 }
zoot661 0:75e3cf27c60b 47 else if( currentEncoderState == 3 )
zoot661 0:75e3cf27c60b 48 {
zoot661 0:75e3cf27c60b 49 encoderClickCount++;
zoot661 0:75e3cf27c60b 50 }
zoot661 0:75e3cf27c60b 51 break;
zoot661 0:75e3cf27c60b 52
zoot661 0:75e3cf27c60b 53 case 3:
zoot661 0:75e3cf27c60b 54 if( currentEncoderState == 2 )
zoot661 0:75e3cf27c60b 55 {
zoot661 0:75e3cf27c60b 56 encoderClickCount--;
zoot661 0:75e3cf27c60b 57 }
zoot661 0:75e3cf27c60b 58 else if( currentEncoderState == 1 )
zoot661 0:75e3cf27c60b 59 {
zoot661 0:75e3cf27c60b 60 encoderClickCount++;
zoot661 0:75e3cf27c60b 61 }
zoot661 0:75e3cf27c60b 62 break;
zoot661 0:75e3cf27c60b 63
zoot661 0:75e3cf27c60b 64 default:
zoot661 0:75e3cf27c60b 65 break;
zoot661 0:75e3cf27c60b 66 }
zoot661 0:75e3cf27c60b 67 previousEncoderState = currentEncoderState;
zoot661 0:75e3cf27c60b 68 }
zoot661 0:75e3cf27c60b 69
zoot661 0:75e3cf27c60b 70 // read the rotation accumulator, and once read, reset it to zero
zoot661 0:75e3cf27c60b 71 int getClicks( void )
zoot661 0:75e3cf27c60b 72 {
zoot661 0:75e3cf27c60b 73 int res = encoderClickCount; // this allows the knob to be rotated "while im not looking at it' and still return the
zoot661 0:75e3cf27c60b 74
zoot661 0:75e3cf27c60b 75 encoderClickCount = 0; // actual number of clicks that have been rotated since last time I was checking it.
zoot661 0:75e3cf27c60b 76
zoot661 0:75e3cf27c60b 77 return res;
zoot661 0:75e3cf27c60b 78 }
zoot661 0:75e3cf27c60b 79
zoot661 0:75e3cf27c60b 80 int main()
zoot661 0:75e3cf27c60b 81 {
zoot661 0:75e3cf27c60b 82 Serial pc( USBTX, USBRX );
zoot661 0:75e3cf27c60b 83 Ticker sampleTicker; // create a timer to sample the encoder
zoot661 0:75e3cf27c60b 84 int currentClicks;
zoot661 0:75e3cf27c60b 85
zoot661 0:75e3cf27c60b 86 phaseA.mode( PullUp ); // phaseA is set with a built in pull-up resistor
zoot661 0:75e3cf27c60b 87 phaseB.mode( PullUp ); // phaseB is set with a built in pull-up resistor
zoot661 0:75e3cf27c60b 88 sampleTicker.attach_us( &quadratureDecoder, 1000 ); // make the quadrature decoder function check the knob once every 1000us = 1ms
zoot661 0:75e3cf27c60b 89
zoot661 0:75e3cf27c60b 90 pc.printf( "Encoder test started\r\n" );
zoot661 0:75e3cf27c60b 91 while( 1 )
zoot661 0:75e3cf27c60b 92 {
zoot661 0:75e3cf27c60b 93 currentClicks = getClicks();
zoot661 0:75e3cf27c60b 94 if( currentClicks != 0 )
zoot661 0:75e3cf27c60b 95 {
zoot661 0:75e3cf27c60b 96 pc.printf( "click count = %d,%d\r\n", currentClicks, previousEncoderState );
zoot661 0:75e3cf27c60b 97 }
zoot661 0:75e3cf27c60b 98 wait_ms( 200 );
zoot661 0:75e3cf27c60b 99 }
zoot661 0:75e3cf27c60b 100 }