Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
quadratureEncoder.cpp
00001 /* 00002 name: encoder.h 00003 date: 04-june-2011 00004 header description: contains functions and external variables to interface quadrature encoder 00005 compiler-library: the mbed library interface is used on the whole.. 00006 compatability: can also be compatable with generalised code with diffrerent hardware and software 00007 hardware dependency: works on rover 5 model platform interfaced with mbed controller 00008 00009 indentation: <mbed default intendation - 4 spaces> 00010 resources: http://www.sparkfun.com/products/10336 00011 http://letsmakerobots.com/node/24031 00012 http://www.sparkfun.com/datasheets/Robotics/Rover%205%20Introduction.pdf 00013 */ 00014 #include "mbed.h" 00015 #include "quadratureEncoder.h" 00016 //gets a bit position by shifting 1 00017 #define Abit 0 00018 #define Bbit 1 00019 00020 #define _BV(x) (1 << x) 00021 00022 // sbi sets a particular bit and cbi clears the bit (target byte-word, bit position) 00023 #define sbi(y,x) y|=_BV(x) 00024 #define cbi(y,x) y&=~_BV(x) 00025 00026 quadratureEncoder::quadratureEncoder(PinName pinA, PinName pinB): 00027 _pinA(pinA), _pinB(pinB){ 00028 _pinA.rise(this, &quadratureEncoder::ARise); 00029 _pinA.fall(this, &quadratureEncoder::AFall); 00030 _pinB.rise(this, &quadratureEncoder::BRise); 00031 _pinB.fall(this, &quadratureEncoder::BFall); 00032 00033 _oldState = 0x00; 00034 _nowState = 0x00; 00035 _moved = 0; 00036 _count = 0; 00037 } 00038 00039 //////////////////////////////////////////////////////////////////////////////////////// 00040 int quadratureEncoder::getCount() { 00041 return _count; 00042 } 00043 00044 void quadratureEncoder::resetCount() { 00045 _count = 0; 00046 } 00047 00048 void quadratureEncoder::setCount(int setCounter) { 00049 _count = setCounter; 00050 } 00051 00052 00053 00054 //////////////////////////////////////////////////////////////////////////////////////// 00055 void quadratureEncoder::resetMoved() { 00056 _moved = 0; 00057 } 00058 00059 void quadratureEncoder::setMoved(int setMover) { 00060 _moved = setMover; 00061 } 00062 00063 int quadratureEncoder::getMoved() { 00064 return _moved; 00065 } 00066 00067 //////////////////////////////////////////////////////////////////////////////////////// 00068 char quadratureEncoder::saveState(char nowS) { 00069 return nowS; 00070 } 00071 00072 char quadratureEncoder::getBit(char bitP, char targB) { 00073 targB = targB & _BV(bitP); 00074 if (targB == 0) 00075 return 0; 00076 else 00077 return 1; 00078 } 00079 00080 00081 /* 00082 function: the following 8 functions are ISR called by rise and fall of 4 pins 00083 input: none (interupt called) 00084 output: none 00085 functioning:interrupts are called when the pin change occours 00086 sets or clears the respective bit 00087 checks the previous state and increments respective direction 00088 restores new state value into old state 00089 increments a count value for processing */ 00090 void quadratureEncoder::ARise() { 00091 sbi(_nowState,Abit); 00092 if (getBit(Bbit,_oldState)==0 && getBit(Abit,_oldState)==0) 00093 _moved++; 00094 else if (getBit(Bbit,_oldState)==0 && getBit(Abit,_oldState)==1) 00095 _moved--; 00096 _oldState = saveState(_nowState); 00097 _count++; 00098 } 00099 00100 void quadratureEncoder::AFall() { 00101 cbi(_nowState, Abit); 00102 if (getBit(Bbit,_oldState)==1 && getBit(Abit,_oldState)==1) 00103 _moved++; 00104 else if (getBit(Bbit,_oldState)==1 && getBit(Abit,_oldState)==0) 00105 _moved--; 00106 _oldState = saveState(_nowState); 00107 _count++; 00108 } 00109 00110 void quadratureEncoder::BRise() { 00111 sbi(_nowState,Bbit); 00112 if (getBit(Bbit,_oldState)==1 && getBit(Abit,_oldState)==0) 00113 _moved++; 00114 else if (getBit(Bbit,_oldState)==0 && getBit(Abit,_oldState)==0) 00115 _moved--; 00116 _oldState = saveState(_nowState); 00117 _count++; 00118 } 00119 00120 void quadratureEncoder::BFall() { 00121 cbi(_nowState, Bbit); 00122 if (getBit(Bbit,_oldState)==0 && getBit(Abit,_oldState)==1) 00123 _moved++; 00124 else if (getBit(Bbit,_oldState)==1 && getBit(Abit,_oldState)==1) 00125 _moved--; 00126 _oldState = saveState(_nowState); 00127 _count++; 00128 } 00129
Generated on Tue Jul 12 2022 17:29:59 by
1.7.2