QEIx4 Example

Dependencies:   QEIx4 mbed

Files at this revision

API Documentation at this revision

Comitter:
jocis
Date:
Wed Oct 01 10:23:33 2014 +0000
Parent:
2:7c787d83331e
Commit message:
replaced virtual functions by attached functions; added documentation

Changed in this revision

QEIx4.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 7c787d83331e -r 922c100de8cd QEIx4.lib
--- a/QEIx4.lib	Tue Sep 30 12:35:07 2014 +0000
+++ b/QEIx4.lib	Wed Oct 01 10:23:33 2014 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/teams/OpenMoCo/code/QEIx4/#ac6b7b1bf6c5
+http://mbed.org/teams/OpenMoCo/code/QEIx4/#c0b87b11b9cd
diff -r 7c787d83331e -r 922c100de8cd main.cpp
--- a/main.cpp	Tue Sep 30 12:35:07 2014 +0000
+++ b/main.cpp	Wed Oct 01 10:23:33 2014 +0000
@@ -2,18 +2,19 @@
 #include "QEIx4.h"
 
 DigitalOut LEDalive(LED1);
+DigitalOut LEDzero(LED2);
 DigitalOut LEDup(LED4);
 DigitalOut LEDdown(LED3);
 
-Timer t;
+Timer t;   // timer for polling
 
 // ports for nxp LPC 1768
-QEIx4 qei1(p30, p29, p28, (QEIx4::EMODE)(QEIx4::IRQ | QEIx4::SPEED));          // QEI with index signal for zeroing
-QEIx4 qei2(p21, p22, NC,  QEIx4::IRQ_NO_JAMMING);           // QEI with AB signals only
-QEIx4 qei3(p25, p24, NC,  QEIx4::POLLING);    // QEI without interrups in polling mode
+QEIx4 qei1(p30, p29, p28, (QEIx4::EMODE)(QEIx4::IRQ | QEIx4::SPEED));   // QEI with index signal for zeroing
+QEIx4 qei2(p21, p22, NC,  QEIx4::IRQ_NO_JAMMING);                       // QEI with AB signals only
+QEIx4 qei3(p25, p24, NC,  QEIx4::POLLING);                              // QEI without interrups in polling mode
 
-// The callback function.
-void myCallback(int value)
+// The callback functions
+void myCounterChangeCallback(int value)
 {
     static int valueLast=-1;
 
@@ -24,17 +25,24 @@
         LEDdown = !LEDdown;
         LEDup = 0;
     }
-    valueLast=value;
+    valueLast = value;
+}
+
+void myIndexTriggerCallback(int value)
+{
+    qei1 = 0;   // reset counter
+    LEDzero = 1;
 }
 
 int main()
 {
     t.start();
 
-    qei1.setZeroOnIndex(true);      // set the flag to zero counter on next index signal rises
+    qei1.setIndexTrigger(true);     // set the flag to zero counter on next index signal rises
     qei1.setSpeedFactor(1.0f);      // factor to scale from Hz (edges pe second = 4 * CPS) to user units (1.0=Hz, 1/(4*CPR)=rps, 1/(60*4*CPR)=rpm, 360/(4*CPR)=°/s, ...)
+    qei3.attachIndexTrigger(myIndexTriggerCallback);
     
-    qei3.attachCounterChange(myCallback);
+    qei3.attachCounterChange(myCounterChangeCallback);
 
     while(1) {
         qei3.poll();   // poll manually without interrupt - sampling in this loop with about 2kHz