0% found this document useful (0 votes)
8 views

Bab 5 Program

The document describes code for an Arduino program that uses SPI communication between a master and slave device to control LEDs. The master sends a data value from the serial monitor to the slave. The slave receives this value using an interrupt service routine and uses it to turn on the corresponding LED. Communication is bidirectional as the slave also sends data back to the master.

Uploaded by

Rozin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Bab 5 Program

The document describes code for an Arduino program that uses SPI communication between a master and slave device to control LEDs. The master sends a data value from the serial monitor to the slave. The slave receives this value using an interrupt service routine and uses it to turn on the corresponding LED. Communication is bidirectional as the slave also sends data back to the master.

Uploaded by

Rozin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

//2112024 HILMI DZAKI SETIAWAN

//Device 1

#include <SoftwareSerial.h>

SoftwareSerial SerialExternal(2, 3);

int data = 0;
bool state1 = 0;
bool state2 = 0;

void setup() {
SerialExternal.begin(11200);
Serial.begin(11200);
Serial.println("Welcome to module 4");
Serial.println("device1 is running");
}

void loop() {
//mengambil data dari Serial monitor
if (Serial.available()) {
data = Serial.read(); // baca data (max 1 byte)
data -= 48;//lihat kode ascii, tentang karakter dan desimal
state1 = 1;
}

if (state1) {
SerialExternal.write(data); // kirim data(max 1 byte)
state1 = 0;
}
int d_device2 = 0;
if (SerialExternal.available()) {
d_device2 = SerialExternal.read();
state2 = 1;
}
if (state2) {
Serial.println("data dari device2 = " + String(d_device2));
state2 = 0;
}
delay(250);
}
//2112024 HILMI DZAKI SETIAWAN
//Program Master

#include <SPI.h>
#define SS 10 //Select Slave

int data = 0;
boolean state = false;

void setup (void) {


Serial.begin(9600);
SPI.begin(); //SPI mode Master
pinMode(SS, OUTPUT);
digitalWrite(SS, HIGH);
SPI.setClockDivider(SPI_CLOCK_DIV4); //mengatur clock SPI dari 1/4 frekuensi sistem
(frekuensi sistem 16MHz).
Serial.println("welcome to module 4");
Serial.println("device Master");
}

void loop (void) {


//========= Mengambil nilai dari Serial Monitor ==============
if (Serial.available()) {
data = Serial.read();
data = data - 48;
state = true;
}
//========= Mengirim data ke slave ==============
if (state) {
int dataFromMISO = sendDataSPI(data);//mengirim nilai dari master, dan mengambil nilai
dari slave
if (dataFromMISO) Serial.println("data dari slave = " + String (dataFromMISO));
else Serial.println("slave gagal ngirim");
state = false;
}
delay(250);
}
//======= fungsi write and read data SPI =========
int sendDataSPI(int _val) {
digitalWrite(SS, LOW); //aktifkan pin enable slave
int data_receive = SPI.transfer(_val); // mengirim data ke slave, dan mengambil data dari
slave
digitalWrite(SS, HIGH); // matikan pin enable slave
delay(1);
return data_receive;
}
//2112024 HILMI DZAKI SETIAWAN
//Program Slave

#include <SPI.h>

#define L1 4 //pin arduino


#define L2 5 //pin arduino
#define L3 6 //pin arduino
#define L4 7 //pin arduino
#define NUM_LEDS 4 //jumlah led

int leds[NUM_LEDS] = {L1, L2, L3, L4};

volatile uint8_t data = 0; //variable ini disimpan di RAM

void setup (void) {


Serial.begin (9600);
for (uint8_t indx = 0; indx < NUM_LEDS; indx++) {
pinMode(leds[indx], OUTPUT);
}
pinMode(MISO, OUTPUT); //output untk mengirim data dari slave to master
SPCR |= 0b0000001 << 6; //SPI activated
SPI.attachInterrupt(); //interrupt activated
Serial.println("welcome to module 4");
Serial.println("device SLave");
}
ISR (SPI_STC_vect) //(Interrupt Service Routine) dipanggil ketika ada interrupt dari
SPI
{
data = SPDR; //SPI Data Register
SPDR = data;
}

void loop (void) {


Serial.print("data Input = ");
Serial.println(data);

if (data > 0 && data <= NUM_LEDS) {


uint8_t bin[NUM_LEDS] = {0b0001, 0b0010, 0b0100, 0b1000};
--data;
// ============== KONDISI LED ===============
for (uint8_t j = 0; j < NUM_LEDS; j++) {
uint8_t val = (bin[j] >> data) & 1;
Serial.print("LED" + String(j + 1) + " = " + String(val) + " \t");
digitalWrite(leds[j], val);
}
Serial.println();
data++;
}
else {
for (uint8_t j = 0; j < NUM_LEDS; j++) {
digitalWrite(leds[j], LOW);
}
Serial.println("all leds off");
}
delay(250);
}

You might also like