Spi successful with start and end bytes

This commit is contained in:
FluffyCube9343 2022-02-27 18:48:54 -05:00
parent a5715d1468
commit cf35546265
7 changed files with 174 additions and 34 deletions

View File

@ -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

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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;
/*
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");
if(wai==0){
t1.toCharArray(Buf,t1.length());
}
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;*/
Serial.print(wai);
if(wai==t1.length()){x = 's';}
else{
if(wai==t1.length()+1){
wai=0;
}
x = t1[wai];
}
if(x == 10){x = 0;}
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);
}
}
}}

View File

@ -0,0 +1,39 @@
//Master Arduino Code:
//SPI MASTER (ARDUINO)
//SPI COMMUNICATION BETWEEN TWO ARDUINO
//CIRCUIT DIGEST
#include<SPI.h> //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);
}

View File

@ -0,0 +1,90 @@
//Slave Arduino Code:
//SPI SLAVE (ARDUINO)
//SPI COMMUNICATION BETWEEN TWO ARDUINO
//CIRCUIT DIGEST
//Pramoth.T
int laste = 0;
#include<SPI.h>
#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);
}
}