Pada prinsip kerja rangkaian diatas dimana merupakan rangkaian berupa SPI yang mana pengiriman datanya secara sinkron dan mode transmisinya berupa full duplex. pada SPI ini terdapat 4 buag pin yaitu pin 13 (SCK) pin 12 (MOSI) pin 11 (MISO) dan pin 10 (SS). yang nantinya pada arduino bagian MASTER, akan memilih slave mana ayang akan dikirm datanya lewat pin SS lalu MASTER akan memberikan output kepada SLAVE (MOSI). setelah SLAVE menerima input maka SLAVE akan juga memberikan output pada MASTER (MISO). dan nantinya akan terus berulang. jika pada rangkaian diatas diaman menggunakan indikator berupa LED yang mana LED pada slave terlebih dahhulu hidup lalu LED pada MASTER yang mana dilakukan secara berulang dengan delay 1 detik.
listining program
//MASTER
#include<SPI.h>//Library for SPI
#defineLED7
#defineipbutton2
int buttonvalue;
int x;
voidsetup()
{
Serial.begin(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);
digitalWrite(SS,HIGH);
}
voidloop()
{
byte Mastersend,Mastereceive;
buttonvalue = digitalRead(ipbutton); //Reads the status of the pin 2
if(buttonvalue == HIGH)
x = 1;
}
else
{
x = 0;
}
digitalWrite(SS, LOW); //Starts communication with Slave
Mastersend = x;
Mastereceive=SPI.transfer(Mastersend);
if(Mastereceive == 1)
{
digitalWrite(LED,HIGH);
delay(500);
digitalWrite(LED,LOW);
//Sets pin 7 HIGH
Serial.println("Master LED ON");
}
else
{
digitalWrite(LED,LOW); //Sets pin 7 LOW
Serial.println("Master LED OFF");
}
delay(1000);
}
//SLAVE
#include<SPI.h>
#defineLEDpin7
#definebuttonpin2
volatile boolean received;
volatile byte Slavereceived,Slavesend;
int buttonvalue;
int x;
voidsetup()
{
Serial.begin(115200);
pinMode(buttonpin,INPUT); // Setting pin 2 as INPUT
pinMode(LEDpin,OUTPUT); // Setting pin 7 as OUTPUT
pinMode(MISO,OUTPUT);
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;
received = true; //Sets received as True
}
voidloop()
{if(received)
{
digitalWrite(LEDpin,HIGH);
delay(1000);
digitalWrite(LEDpin, LOW);//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)
{
x=1;
}else
{
x=0;
}
Slavesend=x;
SPDR = Slavesend; //Sends the x value to master via SPDR
Tidak ada komentar:
Posting Komentar