Board2

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
pbdt1997
Date:
Sat Apr 20 07:03:02 2019 +0000
Parent:
0:a2b61e577da7
Commit message:
Slave Board;

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r a2b61e577da7 -r 3817c83cef51 main.cpp
--- a/main.cpp	Wed Mar 27 14:11:01 2019 +0000
+++ b/main.cpp	Sat Apr 20 07:03:02 2019 +0000
@@ -1,43 +1,40 @@
 #include "mbed.h"
 #include <SPISlave.h>
 
-#define LOW = 0;
-#define HIGH = 1;
+#define LOW 0;
+#define HIGH 1;
 Serial pc(USBTX, USBRX);
 
 SPISlave slave(PA_7, PA_6, PA_5, PA_15); //MOSI MISO CLK CS
 
 DigitalIn prox1(PA_14);
 
-struct stepper1{
-    DigitalOut PUL(PC_1); 
-    DigitalOut DR(PC_0); 
-}
-
-//DigitalOut PUL(PC_1); 
-//DigitalOut DR(PC_0);    //LOW=up, HIGH=down 
+DigitalOut PUL_1(D3);
+DigitalOut DR_1(PC_0);
 
 bool start = false;
 int position = 0;
 char inputPos = 0;
+char check =0;
 
+//drive stepper motor
 void drvStepper1(int step){
     if(step >= 0){
-        for(char i=0; i<step; i++){
-            DR.stepper1 = 1;
-            PUL.stepper1 = 1;
+        for(int i=0; i<step; i++){
+            DR_1 = 1;
+            PUL_1 = 1;
             wait_ms(1);
-            PUL.stepper1 = 0;
+            PUL_1 = 0;
             wait_ms(1);
         }
     }
     else if(step < 0){
-        step = (-1)*step;
-        for(char i=0; i<step; i++){
-            DR.stepper1 = 0;
-            PUL.stepper1 = 1;
+        step = -step;
+        for(int i=0; i<step; i++){
+            DR_1 = 0;
+            PUL_1 = 1;
             wait_ms(1);
-            PUL.stepper1 = 0;
+            PUL_1 = 0;
             wait_ms(1);
         }
     }
@@ -48,22 +45,35 @@
       slave.format(8,3);
       slave.frequency(1000000);
       slave.reply(0x00);              // Prime SPI with first reply
-    while(1) {
+    while(1) {\
+        //check data from master
         if(slave.receive()){
             int data = slave.read();
             if(data == 123){
-                slave1.reply(0x42); //send 'B'
+                slave.reply(0x42); //send 'B'
                 start = true;
             }
         }
         if(start == true){
-            if(prox1 == 0){
-                PUL.stepper1 = LOW;
+        //drive stepper motor
+        while(prox1 == 1){
+            for(int i=0; i<200; i++){
+                DR_1 = 1; //1=down
+                PUL_1 = 1;
+                wait_ms(1);
+                PUL_1 = 0;
                 wait_ms(1);
-            }
-            else{
-                drvStepper1(100);
-            }
+                printf("1\n");
+            }   
         }
-   }
+        wait(1);
+        printf("0\n");
+        for(int i=0; i<1700; i++){
+            DR_1 = 0; //1=down
+            PUL_1 = 1;
+            wait_ms(1);
+            PUL_1 = 0;
+            wait_ms(1);
+            printf("1\n");
+        }
 }