Percobaan 2
Percobaan 2 Komunikasi SPI Menggunakan Arduino
1. Prosedur [Kembali]
- Rangkai semua komponen
- Buat program di aplikasi arduino IDE
- Setelah selesai masukkan program ke arduino
- Jalankan program pada simulasi dan cobakan dengan modul
- Lihat hasil LCD apakah sesuai dengan warna switch yang ditekan
4. Prinsip Kerja Rangkaian [Kembali]
- Prinsip Kerja
Rangkaian percobaan ini merupakan rangkaian 8x Dip Switch dan Dual Seven Segment dengan komunikasi SPI. Dimana Dip Switch akan mengatur keluaran pada Dual Seven Segment, sehingga setiap dipswitch mewakili angka-angka secara berurutan. dari 1-9.
- Flowchart
MASTER
- Listing Program
- //Master Arduino#include<SPI.h> //Library for SPIint dip[] = {2,3,4,5,6,7,8,9};int dipvalue[] = {};void setup (){Serial.begin(9600); //Starts Serial Communication at Baud Rate 115200for(int i = 0; i < 8; i++){pinMode(dip[i], INPUT_PULLUP);}SPI.begin(); //Begins the SPI commnuicationSPI.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;int x = 1;for(int i = 0; i < 8; i++){dipvalue[i] = digitalRead(dip[i]);if(dipvalue[i] == LOW){x = dip[i];}}digitalWrite(SS, LOW); //Starts communication with Slave connected to masterMastersend = x;Serial.println(Mastersend);SPI.transfer(Mastersend); //Send the mastersend value to slave also receives value from slavedelay(1000);}
- //Slave Arduino:#include<SPI.h>const int segmentPins[] = {9, 8, 7, 6, 5, 4, 3, 2};volatile boolean received = false;volatile byte Slavereceived;int index;void setup(){Serial.begin(9600);for (int i = 0; i < 8; i++) {pinMode(segmentPins[i], OUTPUT);}SPCR |= _BV(SPE); //Turn on SPI in Slave ModeSPI.attachInterrupt(); //Interuupt ON is set for SPI commnucation}ISR (SPI_STC_vect){ //Inerrrput routine functionSlavereceived = SPDR; // Value received from master if store in variable slavereceivedreceived = true; //Sets received as True}void loop(){Serial.println(Slavereceived);if(received){//Logic to SET LED ON OR OFF depending upon the value recerived from masterdisplayCharacter(Slavereceived);delay(1000);}}void displayCharacter(int ch) {byte patterns[10][7] = {{0, 0, 0, 0, 0, 0, 1}, // 0{1, 0, 0, 1, 1, 1, 1}, // 1{0, 0, 1, 0, 0, 1, 0}, // 2{0, 0, 0, 0, 1, 1, 0}, // 3{1, 0, 0, 1, 1, 0, 0}, // 4{0, 1, 0, 0, 1, 0, 0}, // 5{0, 1, 0, 0, 0, 0, 0}, // 6{0, 0, 0, 1, 1, 1, 1}, // 7{0, 0, 0, 0, 0, 0, 0}, // 8{0, 0, 0, 0, 1, 0, 0} // 9};if ((ch >= 0 && ch <= 9)) {// Get the digit index (0-9) from the characterint index = ch;// Write the pattern to the segment pinsfor (int i = 0; i < 7; i++) {digitalWrite(segmentPins[i], patterns[index][i]);}}}
6. Kondisi dan Analisa [Kembali]
Tidak ada komentar:
Posting Komentar