Library for manual Encoders as used in user interfaces. Very simple, reduced and rock solid encoder library. Counts full pulses only. Inherent debouncing through state machine. Running on a regular timer IRQ. No IRQ jamming through bouncing. Immune to false edges giving unwanted counts when moving partial steps. Not depending on PinDetect or anything else. May be enhanced by adding acceleration and push button debouncing in the future.

Library for manual Encoders as used in user interfaces. Very simple, reduced and rock solid. Counts full pulses/steps only. Inherent debouncing through state machine. No time dependency for debouncing. Very tight code running in a regular timer IRQ. No IRQ jamming through bouncing edges. Immune to false edges giving unwanted counts when moving partial steps back and forth. Not depending on PinDetect or any other debouncing library. May be enhanced in the near future by adding acceleration and push button debouncing.

Committer:
eduardoG26
Date:
Tue Nov 25 09:33:56 2014 +0000
Revision:
2:603b47d3755e
Parent:
1:158a3886aced
Child:
3:e8e1481ac042
Formal changes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
eduardoG26 2:603b47d3755e 1 // Encoder lib for user interfaces
eduardoG26 1:158a3886aced 2 // Extremely simple time driven state machin with inherent debouncing.
eduardoG26 1:158a3886aced 3 // No irq-jamming because of switch bouncing
eduardoG26 1:158a3886aced 4 // No counting on first edges when encoder leaves idle state (both switches open).
eduardoG26 1:158a3886aced 5 // Rock solid
eduardoG26 2:603b47d3755e 6 /*
eduardoG26 2:603b47d3755e 7 The MIT License (MIT)
eduardoG26 2:603b47d3755e 8
eduardoG26 2:603b47d3755e 9 Copyright (c) 2014 calima engineering
eduardoG26 2:603b47d3755e 10
eduardoG26 2:603b47d3755e 11 Permission is hereby granted, free of charge, to any person obtaining a copy
eduardoG26 2:603b47d3755e 12 of this software and associated documentation files (the "Software"), to deal
eduardoG26 2:603b47d3755e 13 in the Software without restriction, including without limitation the rights
eduardoG26 2:603b47d3755e 14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
eduardoG26 2:603b47d3755e 15 copies of the Software, and to permit persons to whom the Software is
eduardoG26 2:603b47d3755e 16 furnished to do so, subject to the following conditions:
eduardoG26 2:603b47d3755e 17
eduardoG26 2:603b47d3755e 18 The above copyright notice and this permission notice shall be included in
eduardoG26 2:603b47d3755e 19 all copies or substantial portions of the Software.
eduardoG26 2:603b47d3755e 20
eduardoG26 2:603b47d3755e 21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
eduardoG26 2:603b47d3755e 22 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
eduardoG26 2:603b47d3755e 23 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
eduardoG26 2:603b47d3755e 24 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
eduardoG26 2:603b47d3755e 25 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
eduardoG26 2:603b47d3755e 26 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
eduardoG26 2:603b47d3755e 27 THE SOFTWARE.
eduardoG26 2:603b47d3755e 28 */
eduardoG26 1:158a3886aced 29
eduardoG26 0:fc87dcec05fd 30 #include "mbed.h"
eduardoG26 1:158a3886aced 31 #include "Encoder.h"
eduardoG26 2:603b47d3755e 32 #include "EncoderConfig.h"
eduardoG26 1:158a3886aced 33
eduardoG26 1:158a3886aced 34 // Chose encoder pins, default, nucleo-XXX
eduardoG26 1:158a3886aced 35 #ifndef ENCODER_PIN_A
eduardoG26 1:158a3886aced 36 #define ENCODER_PIN_A D6
eduardoG26 1:158a3886aced 37 #endif
eduardoG26 1:158a3886aced 38 #ifndef ENCODER_PIN_B
eduardoG26 1:158a3886aced 39 #define ENCODER_PIN_B D7
eduardoG26 1:158a3886aced 40 #endif
eduardoG26 1:158a3886aced 41
eduardoG26 1:158a3886aced 42 // Sample every ... microseconds, default
eduardoG26 1:158a3886aced 43 // 250µs is a good value for cheap 24 ppr encoders (ALPS EC12E24...) with strong bouncing.
eduardoG26 1:158a3886aced 44 // 1000µs is a good value for quality 24 ppr encoders (ALPS EC12E24...) with low bouncing.
eduardoG26 1:158a3886aced 45 // 250µs is a good value for fast 24+ ppr encoders (Sparkfun, Qubig Panel1 type...).
eduardoG26 1:158a3886aced 46 #ifndef ENCODER_SAMPLE_PERIOD_US
eduardoG26 1:158a3886aced 47 #define ENCODER_SAMPLE_PERIOD_US (250)
eduardoG26 1:158a3886aced 48 #endif
eduardoG26 0:fc87dcec05fd 49
eduardoG26 0:fc87dcec05fd 50 static volatile uint16_t Pulses;
eduardoG26 0:fc87dcec05fd 51 static bool UpDown, Ready;
eduardoG26 0:fc87dcec05fd 52
eduardoG26 0:fc87dcec05fd 53 DigitalIn PinA(ENCODER_PIN_A), PinB(ENCODER_PIN_B);
eduardoG26 0:fc87dcec05fd 54
eduardoG26 0:fc87dcec05fd 55 static Ticker EncoderTick;
eduardoG26 1:158a3886aced 56 static void EncoderTickIRQ();
eduardoG26 0:fc87dcec05fd 57
eduardoG26 1:158a3886aced 58 // Call once in main.
eduardoG26 1:158a3886aced 59 void EncoderStart ()
eduardoG26 1:158a3886aced 60 {
eduardoG26 1:158a3886aced 61 PinA.mode(PullUp);
eduardoG26 1:158a3886aced 62 PinB.mode(PullUp);
eduardoG26 1:158a3886aced 63 EncoderTick.attach_us (&EncoderTickIRQ, ENCODER_SAMPLE_PERIOD_US);
eduardoG26 1:158a3886aced 64 }
eduardoG26 1:158a3886aced 65
eduardoG26 1:158a3886aced 66 // Switch off Pullups and IRQ to save power f. e.
eduardoG26 1:158a3886aced 67 void EncoderStop ()
eduardoG26 1:158a3886aced 68 {
eduardoG26 1:158a3886aced 69 PinA.mode(PullNone);
eduardoG26 1:158a3886aced 70 PinB.mode(PullNone);
eduardoG26 1:158a3886aced 71 EncoderTick.detach();
eduardoG26 1:158a3886aced 72 }
eduardoG26 1:158a3886aced 73 // Get counting variable.
eduardoG26 1:158a3886aced 74 uint16_t EncoderGet ()
eduardoG26 1:158a3886aced 75 {
eduardoG26 1:158a3886aced 76 return Pulses;
eduardoG26 1:158a3886aced 77 }
eduardoG26 1:158a3886aced 78
eduardoG26 1:158a3886aced 79 // State machine. Runs in IRQ. Static.
eduardoG26 0:fc87dcec05fd 80 static void EncoderTickIRQ()
eduardoG26 0:fc87dcec05fd 81 {
eduardoG26 1:158a3886aced 82 // If-then-else structure is good because every pin is read max. once.
eduardoG26 0:fc87dcec05fd 83 if(PinB) {
eduardoG26 0:fc87dcec05fd 84 if(PinA) { // clockwise?
eduardoG26 0:fc87dcec05fd 85 Ready = true;
eduardoG26 0:fc87dcec05fd 86 } else {
eduardoG26 0:fc87dcec05fd 87 UpDown = true;
eduardoG26 0:fc87dcec05fd 88 }
eduardoG26 0:fc87dcec05fd 89 } else {
eduardoG26 0:fc87dcec05fd 90 if(PinA) { // counterclockwise?
eduardoG26 0:fc87dcec05fd 91 UpDown = false;
eduardoG26 0:fc87dcec05fd 92 } else {
eduardoG26 0:fc87dcec05fd 93 if(Ready) {
eduardoG26 0:fc87dcec05fd 94 if (UpDown) {
eduardoG26 0:fc87dcec05fd 95 Pulses++; //moving forward
eduardoG26 0:fc87dcec05fd 96 } else {
eduardoG26 0:fc87dcec05fd 97 Pulses--; //moving reverse
eduardoG26 0:fc87dcec05fd 98 }
eduardoG26 0:fc87dcec05fd 99 Ready = false;
eduardoG26 0:fc87dcec05fd 100 }
eduardoG26 0:fc87dcec05fd 101 }
eduardoG26 0:fc87dcec05fd 102 }
eduardoG26 0:fc87dcec05fd 103 }
eduardoG26 0:fc87dcec05fd 104
eduardoG26 0:fc87dcec05fd 105 // End of file