Board2

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include <SPISlave.h>
00003 
00004 #define LOW 0;
00005 #define HIGH 1;
00006 Serial pc(USBTX, USBRX);
00007 
00008 SPISlave slave(PA_7, PA_6, PA_5, PA_15); //MOSI MISO CLK CS
00009 
00010 DigitalIn prox1(PA_14);
00011 
00012 DigitalOut PUL_1(D3);
00013 DigitalOut DR_1(PC_0);
00014 
00015 bool start = false;
00016 int position = 0;
00017 char inputPos = 0;
00018 char check =0;
00019 
00020 //drive stepper motor
00021 void drvStepper1(int step){
00022     if(step >= 0){
00023         for(int i=0; i<step; i++){
00024             DR_1 = 1;
00025             PUL_1 = 1;
00026             wait_ms(1);
00027             PUL_1 = 0;
00028             wait_ms(1);
00029         }
00030     }
00031     else if(step < 0){
00032         step = -step;
00033         for(int i=0; i<step; i++){
00034             DR_1 = 0;
00035             PUL_1 = 1;
00036             wait_ms(1);
00037             PUL_1 = 0;
00038             wait_ms(1);
00039         }
00040     }
00041 }
00042 
00043 int main() {
00044       pc.baud(9600);
00045       slave.format(8,3);
00046       slave.frequency(1000000);
00047       slave.reply(0x00);              // Prime SPI with first reply
00048     while(1) {\
00049         //check data from master
00050         if(slave.receive()){
00051             int data = slave.read();
00052             if(data == 123){
00053                 slave.reply(0x42); //send 'B'
00054                 start = true;
00055             }
00056         }
00057         if(start == true){
00058         //drive stepper motor
00059         while(prox1 == 1){
00060             for(int i=0; i<200; i++){
00061                 DR_1 = 1; //1=down
00062                 PUL_1 = 1;
00063                 wait_ms(1);
00064                 PUL_1 = 0;
00065                 wait_ms(1);
00066                 printf("1\n");
00067             }   
00068         }
00069         wait(1);
00070         printf("0\n");
00071         for(int i=0; i<1700; i++){
00072             DR_1 = 0; //1=down
00073             PUL_1 = 1;
00074             wait_ms(1);
00075             PUL_1 = 0;
00076             wait_ms(1);
00077             printf("1\n");
00078         }
00079 }