QEIx4 Example

Dependencies:   QEIx4 mbed

Committer:
jocis
Date:
Wed Oct 01 10:23:33 2014 +0000
Revision:
3:922c100de8cd
Parent:
2:7c787d83331e
replaced virtual functions by attached functions; added documentation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jocis 0:bbf48777e304 1 #include "mbed.h"
jocis 0:bbf48777e304 2 #include "QEIx4.h"
jocis 0:bbf48777e304 3
jocis 2:7c787d83331e 4 DigitalOut LEDalive(LED1);
jocis 3:922c100de8cd 5 DigitalOut LEDzero(LED2);
jocis 2:7c787d83331e 6 DigitalOut LEDup(LED4);
jocis 2:7c787d83331e 7 DigitalOut LEDdown(LED3);
jocis 2:7c787d83331e 8
jocis 3:922c100de8cd 9 Timer t; // timer for polling
jocis 0:bbf48777e304 10
jocis 0:bbf48777e304 11 // ports for nxp LPC 1768
jocis 3:922c100de8cd 12 QEIx4 qei1(p30, p29, p28, (QEIx4::EMODE)(QEIx4::IRQ | QEIx4::SPEED)); // QEI with index signal for zeroing
jocis 3:922c100de8cd 13 QEIx4 qei2(p21, p22, NC, QEIx4::IRQ_NO_JAMMING); // QEI with AB signals only
jocis 3:922c100de8cd 14 QEIx4 qei3(p25, p24, NC, QEIx4::POLLING); // QEI without interrups in polling mode
jocis 2:7c787d83331e 15
jocis 3:922c100de8cd 16 // The callback functions
jocis 3:922c100de8cd 17 void myCounterChangeCallback(int value)
jocis 2:7c787d83331e 18 {
jocis 2:7c787d83331e 19 static int valueLast=-1;
jocis 0:bbf48777e304 20
jocis 2:7c787d83331e 21 if ( value > valueLast ) {
jocis 2:7c787d83331e 22 LEDup = !LEDup;
jocis 2:7c787d83331e 23 LEDdown = 0;
jocis 2:7c787d83331e 24 } else {
jocis 2:7c787d83331e 25 LEDdown = !LEDdown;
jocis 2:7c787d83331e 26 LEDup = 0;
jocis 2:7c787d83331e 27 }
jocis 3:922c100de8cd 28 valueLast = value;
jocis 3:922c100de8cd 29 }
jocis 3:922c100de8cd 30
jocis 3:922c100de8cd 31 void myIndexTriggerCallback(int value)
jocis 3:922c100de8cd 32 {
jocis 3:922c100de8cd 33 qei1 = 0; // reset counter
jocis 3:922c100de8cd 34 LEDzero = 1;
jocis 2:7c787d83331e 35 }
jocis 2:7c787d83331e 36
jocis 2:7c787d83331e 37 int main()
jocis 2:7c787d83331e 38 {
jocis 0:bbf48777e304 39 t.start();
jocis 0:bbf48777e304 40
jocis 3:922c100de8cd 41 qei1.setIndexTrigger(true); // set the flag to zero counter on next index signal rises
jocis 2:7c787d83331e 42 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, ...)
jocis 3:922c100de8cd 43 qei3.attachIndexTrigger(myIndexTriggerCallback);
jocis 2:7c787d83331e 44
jocis 3:922c100de8cd 45 qei3.attachCounterChange(myCounterChangeCallback);
jocis 0:bbf48777e304 46
jocis 2:7c787d83331e 47 while(1) {
jocis 0:bbf48777e304 48 qei3.poll(); // poll manually without interrupt - sampling in this loop with about 2kHz
jocis 2:7c787d83331e 49
jocis 2:7c787d83331e 50 if ( t.read_ms() > 250 ) { // every quater second (4 Hz)
jocis 0:bbf48777e304 51 t.reset();
jocis 0:bbf48777e304 52 t.start();
jocis 2:7c787d83331e 53 LEDalive = !LEDalive;
jocis 2:7c787d83331e 54
jocis 2:7c787d83331e 55 printf ( "\r\n%6d %6d %6d %10.3f", (int)qei1, (int)qei2, (int)qei3, (float)qei1.getSpeed() ); // print counter values
jocis 0:bbf48777e304 56 }
jocis 0:bbf48777e304 57
jocis 2:7c787d83331e 58 wait_us(20); // for about 50kHz polling
jocis 0:bbf48777e304 59 }
jocis 0:bbf48777e304 60 }