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