Motor control on mbed

Dependencies:   mbed

Revision:
1:f47edcd44466
Parent:
0:f76be6916639
--- a/main.cpp	Sat Jul 23 13:47:47 2016 +0000
+++ b/main.cpp	Sat Jul 23 16:00:05 2016 +0000
@@ -8,10 +8,12 @@
 void init_CN(void);
 void CN_interrupt(void);
 
-int8_t stateA=0, stateB=0, stateC=0, stateB_2=0;
+int8_t stateA=0, stateB=0, stateC=0;
 int8_t state_1 = 0, state_1_old = 0;
+int vCount;
 
 int main() {
+    init_CN();
     
     while(1)
     {
@@ -19,6 +21,66 @@
     }     
 }
 
+void CN_interrupt(void)
+{
+    //Motor 1
+    stateA = HallA.read();
+    stateB = HallB.read();
+    stateC = HallC.read();
+    
+    ///code for state determination///
+    if (stateA == 1)
+    {
+        if (stateB == 1)
+            if(stateC == 0)
+            {
+                state_1 = 2;               
+            }
+            else
+            {
+                ;
+            }
+        else
+            if(stateC == 1)
+            {
+                state_1 = 6;              
+            }
+            else
+            {
+                state_1 = 1;
+            }
+    }
+    else
+    {
+        if (stateB == 1)
+            if(stateC == 1)
+            {
+               state_1 = 4;                
+            }
+            else
+            {
+                state_1 = 3;
+            }
+        else
+            if(stateC == 1)
+            {
+                state_1 = 5;               
+            }
+            else
+            {
+                ;
+            }
+    }
+    
+    //Forward: vCount +1
+    //Inverse: vCount -1
+    if ( (state_1 == (state_1_old + 1)) || (state_1 == 1 && state_1_old == 6) )
+        vCount++;
+    else if ( (state_1 == (state_1_old - 1)) || (state_1 == 6 && state_1_old == 1))
+        vCount--;
+            
+    state_1_old = state_1;
+}
 
 void init_CN(void)
 {
@@ -31,38 +93,6 @@
        
     stateA = HallA.read();
     stateB = HallB.read();
-    stateC = HallC.read();    
-}
-
-void CN_interrupt(void)
-{
-    //Motor 1
-    stateA = HallA.read();
-    stateB = HallB.read();
     stateC = HallC.read();
     
-    ///code for state determination///
-    if (stateA == 0)
-    {
-        if (stateB == 0)
-            state_1 = 0;
-        else
-            state_1 = 1;
-    }
-    else
-    {
-        if (stateB_1 == 1)
-            state_1 = 2;
-        else
-            state_1 = 3;
-    }
-    
-    //Forward: v1Count +1
-    //Inverse: v1Count -1
-    if ( (state_1 == (state_1_old + 1)) || (state_1 == 0 && state_1_old == 3) )
-        v1Count++;
-    else if ( (state_1 == (state_1_old - 1)) || (state_1 == 3 && state_1_old == 0))
-        v1Count--;
-            
-    state_1_old = state_1;
 }
\ No newline at end of file