Quadrature decoder

Dependents:   Telliskivi2_2014

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers qed.cpp Source File

qed.cpp

00001 #include "qed.h"
00002 
00003 QED::QED(PinName channelA, PinName channelB)
00004     : interruptA(channelA), interruptB(channelB) {
00005  
00006     pulses = 0;
00007     currState = (interruptA.read() << 1) | (interruptB.read());
00008     prevState = currState;
00009  
00010     interruptA.rise(this, &QED::decode);
00011     interruptA.fall(this, &QED::decode);
00012     interruptB.rise(this, &QED::decode);
00013     interruptB.fall(this, &QED::decode); 
00014 }
00015 
00016 int QED::read() {
00017     int pulseCount = pulses;
00018     pulses = 0;
00019     return pulseCount;
00020 }
00021 
00022 void QED::decode() {
00023     currState = (interruptA.read() << 1) | (interruptB.read()); 
00024    
00025     int change = (prevState & PREV_MASK) ^ ((currState & CURR_MASK) >> 1);
00026 
00027     if (change == 0) {
00028         change = -1;
00029     }
00030 
00031     pulses -= change;  
00032     prevState = currState; 
00033 }