From cf35546265c53f8a29a67a7879061b9da7ef60a0 Mon Sep 17 00:00:00 2001 From: FluffyCube9343 Date: Sun, 27 Feb 2022 18:48:54 -0500 Subject: [PATCH] Spi successful with start and end bytes --- communication/SPI/notes.txt | 10 +++ .../SPI/v0.2_M_SPI/{ => M_SPI}/M_SPI.ino | 2 +- .../SPI/v0.2_S_SPI/{ => S_SPI}/S_SPI.ino | 6 +- .../M_SPI}/M_SPI.ino | 8 +- .../S_SPI}/S_SPI.ino | 53 ++++++----- communication/SPI/v0.5_M_SPI/M_SPI/M_SPI.ino | 39 ++++++++ communication/SPI/v0.5_S_SPI/S_SPI/S_SPI.ino | 90 +++++++++++++++++++ 7 files changed, 174 insertions(+), 34 deletions(-) create mode 100644 communication/SPI/notes.txt rename communication/SPI/v0.2_M_SPI/{ => M_SPI}/M_SPI.ino (96%) rename communication/SPI/v0.2_S_SPI/{ => S_SPI}/S_SPI.ino (96%) rename communication/SPI/{v0.3_M_SPI => v0.4_M_SPI/M_SPI}/M_SPI.ino (90%) rename communication/SPI/{v0.3_S_SPI => v0.4_S_SPI/S_SPI}/S_SPI.ino (63%) create mode 100644 communication/SPI/v0.5_M_SPI/M_SPI/M_SPI.ino create mode 100644 communication/SPI/v0.5_S_SPI/S_SPI/S_SPI.ino diff --git a/communication/SPI/notes.txt b/communication/SPI/notes.txt new file mode 100644 index 0000000..ab289be --- /dev/null +++ b/communication/SPI/notes.txt @@ -0,0 +1,10 @@ +0.2 is spi with random data (incrementing characters) +0.4 is spi with a seperator bytes +0.5 is spi with both start and end bytes, sensing whether there is data avilable before putting a start byte + + +future +------ + +0.6 will make it into a string for useful inputs, that can then be converted into float arrays. +1.0 is full integration with MPU6050 \ No newline at end of file diff --git a/communication/SPI/v0.2_M_SPI/M_SPI.ino b/communication/SPI/v0.2_M_SPI/M_SPI/M_SPI.ino similarity index 96% rename from communication/SPI/v0.2_M_SPI/M_SPI.ino rename to communication/SPI/v0.2_M_SPI/M_SPI/M_SPI.ino index d6e6a2b..b5d3455 100644 --- a/communication/SPI/v0.2_M_SPI/M_SPI.ino +++ b/communication/SPI/v0.2_M_SPI/M_SPI/M_SPI.ino @@ -32,6 +32,6 @@ void loop(void) Mastersend = buttonvalue; Mastereceive=SPI.transfer(Mastersend); //Send the mastersend value to slave also receives value from slave - Serial.println(Mastereceive); + Serial.println((char) Mastereceive); delay(50); } diff --git a/communication/SPI/v0.2_S_SPI/S_SPI.ino b/communication/SPI/v0.2_S_SPI/S_SPI/S_SPI.ino similarity index 96% rename from communication/SPI/v0.2_S_SPI/S_SPI.ino rename to communication/SPI/v0.2_S_SPI/S_SPI/S_SPI.ino index 87f5a9f..cd59170 100644 --- a/communication/SPI/v0.2_S_SPI/S_SPI.ino +++ b/communication/SPI/v0.2_S_SPI/S_SPI/S_SPI.ino @@ -11,7 +11,7 @@ volatile boolean received; volatile byte Slavereceived,Slavesend; int buttonvalue; -int x = 5; +int x = 65; void setup() { @@ -62,7 +62,7 @@ void loop() x=0;*/ } - if(x == 10){x = 0;} + if(x == 122){x = 65;} Slavesend=x; @@ -71,7 +71,7 @@ void loop() SPDR = x;//Sends the x value to master via SPDR Serial.println(SPDR); Serial.println(Slavesend); - delay(500); + delay(50); } diff --git a/communication/SPI/v0.3_M_SPI/M_SPI.ino b/communication/SPI/v0.4_M_SPI/M_SPI/M_SPI.ino similarity index 90% rename from communication/SPI/v0.3_M_SPI/M_SPI.ino rename to communication/SPI/v0.4_M_SPI/M_SPI/M_SPI.ino index d6e6a2b..a3c7183 100644 --- a/communication/SPI/v0.3_M_SPI/M_SPI.ino +++ b/communication/SPI/v0.4_M_SPI/M_SPI/M_SPI.ino @@ -7,7 +7,7 @@ #define LED 7 #define ipbutton 2 int buttonvalue; -int x; +int x; void setup (void) { @@ -31,7 +31,9 @@ void loop(void) Mastersend = buttonvalue; Mastereceive=SPI.transfer(Mastersend); //Send the mastersend value to slave also receives value from slave + Serial.print((int) Mastereceive); + Serial.print("\t"); - Serial.println(Mastereceive); - delay(50); + Serial.println((char) Mastereceive); + delay(250); } diff --git a/communication/SPI/v0.3_S_SPI/S_SPI.ino b/communication/SPI/v0.4_S_SPI/S_SPI/S_SPI.ino similarity index 63% rename from communication/SPI/v0.3_S_SPI/S_SPI.ino rename to communication/SPI/v0.4_S_SPI/S_SPI/S_SPI.ino index 87f5a9f..06e4438 100644 --- a/communication/SPI/v0.3_S_SPI/S_SPI.ino +++ b/communication/SPI/v0.4_S_SPI/S_SPI/S_SPI.ino @@ -11,7 +11,10 @@ volatile boolean received; volatile byte Slavereceived,Slavesend; int buttonvalue; -int x = 5; +int x = 65; +int wai = 0; +String t1 = "1.00 0.00 0.00 0.00"; +char Buf[32]; void setup() { @@ -35,44 +38,40 @@ ISR (SPI_STC_vect) //Inerrrput routine function } void loop() -{ if(received) //Logic to SET LED ON OR OFF depending upon the value recerived from master +{ + if(received) //Logic to SET LED ON OR OFF depending upon the value recerived from master { if (Slavereceived==8) { - x+=1; + if(wai==0){ + t1.toCharArray(Buf,t1.length()); + } - /* - digitalWrite(LEDpin,HIGH); //Sets pin 7 as HIGH LED ON - Serial.println("Slave LED ON"); - }else - { - digitalWrite(LEDpin,LOW); //Sets pin 7 as LOW LED OFF - Serial.println("Slave LED OFF"); - } - - buttonvalue = digitalRead(buttonpin); // Reads the status of the pin 2 - - if (buttonvalue == HIGH) //Logic to set the value of x to send to master - { - x=1; - }else - { - x=0;*/ + + - } - if(x == 10){x = 0;} - + + Serial.print(wai); + if(wai==t1.length()){x = 's';} + else{ + if(wai==t1.length()+1){ + wai=0; + } + + x = t1[wai]; + } + Slavesend=x; SPDR = Slavesend; SPDR = x;//Sends the x value to master via SPDR - Serial.println(SPDR); - Serial.println(Slavesend); - delay(500); + wai++; + delay(250); + } -} +}} diff --git a/communication/SPI/v0.5_M_SPI/M_SPI/M_SPI.ino b/communication/SPI/v0.5_M_SPI/M_SPI/M_SPI.ino new file mode 100644 index 0000000..a3c7183 --- /dev/null +++ b/communication/SPI/v0.5_M_SPI/M_SPI/M_SPI.ino @@ -0,0 +1,39 @@ +//Master Arduino Code: +//SPI MASTER (ARDUINO) +//SPI COMMUNICATION BETWEEN TWO ARDUINO +//CIRCUIT DIGEST + +#include //Library for SPI +#define LED 7 +#define ipbutton 2 +int buttonvalue; +int x; +void setup (void) + +{ + Serial.begin(115200); //Starts Serial Communication at Baud Rate 115200 + + pinMode(ipbutton,INPUT); //Sets pin 2 as input + pinMode(LED,OUTPUT); //Sets pin 7 as Output + + SPI.begin(); //Begins the SPI commnuication + SPI.setClockDivider(SPI_CLOCK_DIV8); //Sets clock for SPI communication at 8 (16/8=2Mhz) + digitalWrite(SS,HIGH); // Setting SlaveSelect as HIGH (So master doesnt connnect with slave) +} + +void loop(void) +{ + byte Mastersend,Mastereceive; + + buttonvalue = 8; + + digitalWrite(SS, LOW); //Starts communication with Slave connected to master + + Mastersend = buttonvalue; + Mastereceive=SPI.transfer(Mastersend); //Send the mastersend value to slave also receives value from slave + Serial.print((int) Mastereceive); + Serial.print("\t"); + + Serial.println((char) Mastereceive); + delay(250); +} diff --git a/communication/SPI/v0.5_S_SPI/S_SPI/S_SPI.ino b/communication/SPI/v0.5_S_SPI/S_SPI/S_SPI.ino new file mode 100644 index 0000000..94a6f24 --- /dev/null +++ b/communication/SPI/v0.5_S_SPI/S_SPI/S_SPI.ino @@ -0,0 +1,90 @@ + +//Slave Arduino Code: +//SPI SLAVE (ARDUINO) +//SPI COMMUNICATION BETWEEN TWO ARDUINO +//CIRCUIT DIGEST +//Pramoth.T +int laste = 0; +#include +#define LEDpin 7 +#define buttonpin 2 +volatile boolean received; +volatile byte Slavereceived,Slavesend; +int buttonvalue; +int x = 65; +int wai = -1; +String t1 = "1.00 0.00 0.00 0.00"; +char Buf[32]; +int onset = 1; +void setup() + +{ + Serial.begin(115200); + + pinMode(buttonpin,INPUT); // Setting pin 2 as INPUT + pinMode(LEDpin,OUTPUT); // Setting pin 7 as OUTPUT + pinMode(MISO,OUTPUT); //Sets MISO as OUTPUT (Have to Send data to Master IN + + SPCR |= _BV(SPE); //Turn on SPI in Slave Mode + received = false; + + SPI.attachInterrupt(); //Interuupt ON is set for SPI commnucation + +} + +ISR (SPI_STC_vect) //Inerrrput routine function +{ + Slavereceived = SPDR; // Value received from master if store in variable slavereceived + received = true; //Sets received as True +} + +void loop() +{ + if(received) //Logic to SET LED ON OR OFF depending upon the value recerived from master + { + if (Slavereceived==8) + { + if(wai==-1 && t1.length()>18 && t1.length()<33){ + t1.toCharArray(Buf,t1.length()); + } + if(t1.length()>18 && t1.length()<33){ + + + + + + + + Serial.print(wai); + if(wai==t1.length()){x = 'e'; wai = -2; t1 = "wqieuroqwueyroqwueyroiweuqyrioqwueyrioqweuryoiqweuryoiqweurywq";} + else{ + if(wai == -1){x = 's';} + + else{ + x = t1[wai];} + }} + + Slavesend=x; + if(x==laste && x == (int) 'e'){ + laste = x; + + x = 'b'; + } + if(onset==1){ + x = 'o'; + wai--; + onset = 0; + } + laste = x; + + SPDR = Slavesend; + SPDR = x;//Sends the x value to master via SPDR + wai++; + + } + delay(250); + + + +} +}