Board2

Dependencies:   mbed

Committer:
pbdt1997
Date:
Sat Apr 20 07:03:02 2019 +0000
Revision:
1:3817c83cef51
Parent:
0:a2b61e577da7
Slave Board;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pbdt1997 0:a2b61e577da7 1 #include "mbed.h"
pbdt1997 0:a2b61e577da7 2 #include <SPISlave.h>
pbdt1997 0:a2b61e577da7 3
pbdt1997 1:3817c83cef51 4 #define LOW 0;
pbdt1997 1:3817c83cef51 5 #define HIGH 1;
pbdt1997 0:a2b61e577da7 6 Serial pc(USBTX, USBRX);
pbdt1997 0:a2b61e577da7 7
pbdt1997 0:a2b61e577da7 8 SPISlave slave(PA_7, PA_6, PA_5, PA_15); //MOSI MISO CLK CS
pbdt1997 0:a2b61e577da7 9
pbdt1997 0:a2b61e577da7 10 DigitalIn prox1(PA_14);
pbdt1997 0:a2b61e577da7 11
pbdt1997 1:3817c83cef51 12 DigitalOut PUL_1(D3);
pbdt1997 1:3817c83cef51 13 DigitalOut DR_1(PC_0);
pbdt1997 0:a2b61e577da7 14
pbdt1997 0:a2b61e577da7 15 bool start = false;
pbdt1997 0:a2b61e577da7 16 int position = 0;
pbdt1997 0:a2b61e577da7 17 char inputPos = 0;
pbdt1997 1:3817c83cef51 18 char check =0;
pbdt1997 0:a2b61e577da7 19
pbdt1997 1:3817c83cef51 20 //drive stepper motor
pbdt1997 0:a2b61e577da7 21 void drvStepper1(int step){
pbdt1997 0:a2b61e577da7 22 if(step >= 0){
pbdt1997 1:3817c83cef51 23 for(int i=0; i<step; i++){
pbdt1997 1:3817c83cef51 24 DR_1 = 1;
pbdt1997 1:3817c83cef51 25 PUL_1 = 1;
pbdt1997 0:a2b61e577da7 26 wait_ms(1);
pbdt1997 1:3817c83cef51 27 PUL_1 = 0;
pbdt1997 0:a2b61e577da7 28 wait_ms(1);
pbdt1997 0:a2b61e577da7 29 }
pbdt1997 0:a2b61e577da7 30 }
pbdt1997 0:a2b61e577da7 31 else if(step < 0){
pbdt1997 1:3817c83cef51 32 step = -step;
pbdt1997 1:3817c83cef51 33 for(int i=0; i<step; i++){
pbdt1997 1:3817c83cef51 34 DR_1 = 0;
pbdt1997 1:3817c83cef51 35 PUL_1 = 1;
pbdt1997 0:a2b61e577da7 36 wait_ms(1);
pbdt1997 1:3817c83cef51 37 PUL_1 = 0;
pbdt1997 0:a2b61e577da7 38 wait_ms(1);
pbdt1997 0:a2b61e577da7 39 }
pbdt1997 0:a2b61e577da7 40 }
pbdt1997 0:a2b61e577da7 41 }
pbdt1997 0:a2b61e577da7 42
pbdt1997 0:a2b61e577da7 43 int main() {
pbdt1997 0:a2b61e577da7 44 pc.baud(9600);
pbdt1997 0:a2b61e577da7 45 slave.format(8,3);
pbdt1997 0:a2b61e577da7 46 slave.frequency(1000000);
pbdt1997 0:a2b61e577da7 47 slave.reply(0x00); // Prime SPI with first reply
pbdt1997 1:3817c83cef51 48 while(1) {\
pbdt1997 1:3817c83cef51 49 //check data from master
pbdt1997 0:a2b61e577da7 50 if(slave.receive()){
pbdt1997 0:a2b61e577da7 51 int data = slave.read();
pbdt1997 0:a2b61e577da7 52 if(data == 123){
pbdt1997 1:3817c83cef51 53 slave.reply(0x42); //send 'B'
pbdt1997 0:a2b61e577da7 54 start = true;
pbdt1997 0:a2b61e577da7 55 }
pbdt1997 0:a2b61e577da7 56 }
pbdt1997 0:a2b61e577da7 57 if(start == true){
pbdt1997 1:3817c83cef51 58 //drive stepper motor
pbdt1997 1:3817c83cef51 59 while(prox1 == 1){
pbdt1997 1:3817c83cef51 60 for(int i=0; i<200; i++){
pbdt1997 1:3817c83cef51 61 DR_1 = 1; //1=down
pbdt1997 1:3817c83cef51 62 PUL_1 = 1;
pbdt1997 1:3817c83cef51 63 wait_ms(1);
pbdt1997 1:3817c83cef51 64 PUL_1 = 0;
pbdt1997 0:a2b61e577da7 65 wait_ms(1);
pbdt1997 1:3817c83cef51 66 printf("1\n");
pbdt1997 1:3817c83cef51 67 }
pbdt1997 0:a2b61e577da7 68 }
pbdt1997 1:3817c83cef51 69 wait(1);
pbdt1997 1:3817c83cef51 70 printf("0\n");
pbdt1997 1:3817c83cef51 71 for(int i=0; i<1700; i++){
pbdt1997 1:3817c83cef51 72 DR_1 = 0; //1=down
pbdt1997 1:3817c83cef51 73 PUL_1 = 1;
pbdt1997 1:3817c83cef51 74 wait_ms(1);
pbdt1997 1:3817c83cef51 75 PUL_1 = 0;
pbdt1997 1:3817c83cef51 76 wait_ms(1);
pbdt1997 1:3817c83cef51 77 printf("1\n");
pbdt1997 1:3817c83cef51 78 }
pbdt1997 0:a2b61e577da7 79 }