An embedded device

Dependencies:   Crypto

Files at this revision

API Documentation at this revision

Comitter:
tanyuzhuo
Date:
Thu Feb 28 11:52:05 2019 +0000
Parent:
11:7286f0c07086
Child:
13:c51d828531d5
Commit message:
interrupt function

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
motor-mining.lib Show diff for this revision Revisions of this file
--- a/main.cpp	Thu Feb 28 10:44:25 2019 +0000
+++ b/main.cpp	Thu Feb 28 11:52:05 2019 +0000
@@ -23,6 +23,9 @@
 #define MCSPpin   A1
 #define MCSNpin   A0
 
+int8_t motorHome();
+inline int8_t readRotorState();
+void motorOut(int8_t driveState);
 //Mapping from sequential drive states to motor phase outputs
 /*
 State   L1  L2  L3
@@ -49,9 +52,11 @@
 DigitalOut led1(LED1);
 
 //Photointerrupter inputs
-DigitalIn I1(I1pin);
-DigitalIn I2(I2pin);
-DigitalIn I3(I3pin);
+InterruptIn I1(I1pin);
+InterruptIn I2(I2pin);
+InterruptIn I3(I3pin);
+
+
 
 //Motor Drive outputs
 DigitalOut L1L(L1Lpin);
@@ -61,6 +66,21 @@
 DigitalOut L3L(L3Lpin);
 DigitalOut L3H(L3Hpin);
 
+int8_t orState = 0;    //Rotot offset at motor state 0
+int8_t intState = 0;
+int8_t intStateOld = 0;
+    
+//interrupt function
+void push(){
+    //Poll the rotor state and set the motor outputs accordingly to spin the motor
+    intState = readRotorState();
+    if (intState != intStateOld) {
+        intStateOld = intState;
+        motorOut((intState-orState+lead+6)%6); //+6 to make sure the remainder is positive
+        //pc.printf("%d\n\r",intState);
+    }
+}
+
 //Set a given drive state
 void motorOut(int8_t driveState){
     
@@ -101,10 +121,6 @@
     
 //Main
 int main() {
-    int8_t orState = 0;    //Rotot offset at motor state 0
-    int8_t intState = 0;
-    int8_t intStateOld = 0;
-    
     //Initialise the serial port
     Serial pc(SERIAL_TX, SERIAL_RX);
     pc.printf("Hello\n\r");
@@ -114,14 +130,8 @@
     pc.printf("Rotor origin: %x\n\r",orState);
     //orState is subtracted from future rotor state inputs to align rotor and motor states
     
-    //Poll the rotor state and set the motor outputs accordingly to spin the motor
-    while (1) {
-        intState = readRotorState();
-        if (intState != intStateOld) {
-            intStateOld = intState;
-            motorOut((intState-orState+lead+6)%6); //+6 to make sure the remainder is positive
-            //pc.printf("%d\n\r",intState);
-        }
-    }
+    I1.rise(&push);
+    I2.rise(&push);
+    I3.rise(&push);
 }
 
--- a/motor-mining.lib	Thu Feb 28 10:44:25 2019 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-https://os.mbed.com/teams/Internet-of-Tings/code/motor-mining/#000000000000