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.
Dependents: ESP8266_pid_mtrPos_webserver_SDcard_v2 ESP8266_pid_mtrSpeed_Webserver_SDcard ESP8266_pid_spd_and_pos_webserver_SDcard pid_encoder_speed_demo ... more
Revision 0:50aae578cb89, committed 2015-11-23
- Comitter:
- electromotivated
- Date:
- Mon Nov 23 02:44:15 2015 +0000
- Commit message:
- Basic encoder library upload;
Changed in this revision
| QEI.cpp | Show annotated file Show diff for this revision Revisions of this file |
| QEI.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/QEI.cpp Mon Nov 23 02:44:15 2015 +0000
@@ -0,0 +1,21 @@
+#include "QEI.h"
+
+QEI::QEI(PinName encA, PinName encB): _encA(encA), _encB(encB){
+ _encA.mode(PullUp);
+ _encA.rise(this, &QEI::callback);
+
+ _encB.mode(PullUp);
+}
+
+long QEI::read(){
+ return count;
+}
+
+void QEI::reset(){
+ count = 0;
+}
+
+void QEI::callback(){
+ if(_encB) count--; // CCW count
+ else count++; // CW count
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/QEI.h Mon Nov 23 02:44:15 2015 +0000
@@ -0,0 +1,46 @@
+#ifndef QEI_H
+#define QEI_H
+
+/*
+ Basic Quadrature Encoder Interface (QEI) Class.
+
+ TODO: Expand to allow setting of quad mode, i.e. count on
+ rising of A only, rising and falling of A, rising and falling
+ of both A and B. (Forgot what these modes are called but
+ can be found in literature online.)
+*/
+
+#include "mbed.h"
+
+class QEI{
+ public:
+ /*
+ Constructor for QEI objects
+ @param encA The mbed pin that encoder input A is on
+ @param encB The mbed pin that encoder input B is on
+ */
+ QEI(PinName encA, PinName encB);
+ /*
+ read() returns total number of counts of the encoder.
+ Count can be +/- and indicates the overall direction,
+ (+): CW (-): CCW
+ @return The toltal number of counts of the encoder.
+
+ TODO: Add Conversion Overload for this method
+ */
+ long read();
+ /*
+ reset() clears the counter to 0.
+ */
+ void reset();
+ private:
+ long count; // Total number of counts since start.
+ InterruptIn _encA; // Encoder A interrupt pin
+ DigitalIn _encB; // Encoder B input pin
+ /*
+ Increments/Decrements count on interrrupt.
+ */
+ void callback(); // Interrupt callback function
+};
+
+#endif
\ No newline at end of file