# LAB: SPI Protocol

## จุดประสงค์

เพื่อให้เข้าใจการสื่อสารระหว่าง MCU กับ MCU ผ่าน โปรโตคอลSPI

## LAB: Logic Analyzer with SPI Protocol

การทดลอง: ใช้ Logic Analyzer แสดงข้อความการสื่อสารของ SPI Protocol&#x20;

### อุปกรณ์ทดลอง

<table><thead><tr><th width="374">Components</th><th>Quantity</th></tr></thead><tbody><tr><td><a href="https://docs.m5stack.com/#/en/app/demo-board">M5Stack</a> </td><td>1</td></tr><tr><td><a href="http://www.qdkingst.com/en">Logic Analyzer (รุ่น LA1016 or LA2016) </a></td><td>1</td></tr><tr><td>Arduino UNO</td><td>1</td></tr></tbody></table>

![อุกปรณ์การทดลอง](https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MClo3nC-1US0rbK8Qau%2F-MI8-eb5REqAm51sjsmJ%2F-MI84l_k-9aPl5JlxpG8%2Fimage.png?alt=media\&token=d006bd0a-60c9-4627-9e73-51ed60ba7a2e)

### โปรแกรม

1. Arduino IDE
2. โปรแกรม KingstVIS

### การตั้งค่า KingstVIS program&#x20;

* เชื่อมต่อ logic Analyzer กับ คอมพิวเตอร์&#x20;
* เปิดโปรแกรม Kingst VIS&#x20;
* ไปที่ Analyzers >> คลิ๊ก icon add(+) >> เลือก SPI&#x20;

![](https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MClo3nC-1US0rbK8Qau%2F-MI7zO1ZmP3t_6Rkr68f%2F-MI7zuz4iyaxcpgE7PTj%2Fimage.png?alt=media\&token=9c0acc56-38b4-4cf4-a5ff-9723ffec55cb)

* ไปที่ Analyzers >> คลิ๊ก icon setting >> Edit  ตั้งค่าตามรูปด้านล่าง >> OK

![](https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MClo3nC-1US0rbK8Qau%2F-MI7zO1ZmP3t_6Rkr68f%2F-MI8-7C36rXCsWjuMEiv%2Fimage.png?alt=media\&token=39aaaf42-e7ac-4f2b-801f-5194ae77659c)

* ไปที่ Analyzers อีกครั้ง >> คลิ๊ก icon setting >> Display Format >> ASCII  เพื่อให้โปรแกรมแสดงผลเป็นข้อความ

![ตั้งค่า Display Format แสดง ASCII  ](https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MClo3nC-1US0rbK8Qau%2F-MI35iTX6rYT_LpZqH8g%2F-MI3PZLezuIVwcXf2Usg%2Fimage.png?alt=media\&token=c20e01d2-5145-4f4b-be0e-796293e4ddcb)

* ไปที่ Channel 3  เลือกกด Indicates "rising edge trigger"

![Trigger condition setting](https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MClo3nC-1US0rbK8Qau%2F-MHtzSbjPY-RhFrcHm7K%2F-MHu3jGoXJ81ffmTz-JM%2Fimage.png?alt=media\&token=c8efa3cf-63d5-49df-824d-1382f79fa25c)

### Pin Connect

รายชื่อ PIN ต่อเชื่อมต่อกัน

| M5Stack | Arduino UNO | Logic Analyzer |
| :-----: | :---------: | :------------: |
|  pin 18 |    pin 10   |    Channel 3   |
|    MO   |    pin 11   |    Channel 0   |
|    MI   |    pin 12   |    Channel 1   |
|   SCK   |    pin 13   |    Channel 2   |
|   GND   |     GND     |       GND      |

ตำแหน่ง SPI port ที่ใช้ในการทดลองนี้แสดงดังภาพด้านล่าง

![](https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MClo3nC-1US0rbK8Qau%2F-MI8-eb5REqAm51sjsmJ%2F-MI87hF9vIwUncAxchzH%2Fimage.png?alt=media\&token=ecd16d3a-a535-4739-8491-9bd0522ddccd)

### Code: SPI master (M5 stack)

```arduino
// SPI master (M5 stack)
#include <SPI.h>
int ss = 18;
void setup (void) {
   Serial.begin(115200); //set baud rate to 115200 for usart
   pinMode(ss,OUTPUT);
   digitalWrite(ss, HIGH); // disable Slave Select
   SPI.begin ();
   SPI.setClockDivider(SPI_CLOCK_DIV8);//divide the clock by 8
}

void loop (void) {
   char c;
   digitalWrite(ss, LOW); // enable Slave Select
   // send test string
   for (const char * p = "Hello, world!\r" ; c = *p; p++) 
   {
      SPI.transfer (c);
      Serial.print(c);
   }
   digitalWrite(ss, HIGH); // disable Slave Select
   delay(2000);
}
```

### Code: SPI slave Arduino UNO

```arduino
// SPI slave Arduino UNO
#include <SPI.h>
char buff [50];
volatile byte indx;
volatile boolean process;


void setup (void) {
  Serial.begin (115200);
  pinMode(MISO, OUTPUT); // have to send on master in so it set as output
  SPCR |= _BV(SPE); // turn on SPI in slave mode
  indx = 0; // buffer empty
  process = false;
  SPI.attachInterrupt(); // turn on interrupt
}

ISR (SPI_STC_vect) // SPI interrupt routine
{
  byte c = SPDR; // read byte from SPI Data Register
  if (indx < sizeof buff) {
    buff [indx++] = c; // save data in the next index in the array buff
    if (c == '\r') //check for the end of the word
      process = true;
  }
}

void loop (void) {
  if (process) {
    process = false; //reset the process
    Serial.println (buff); //print the array on serial monitor
    indx = 0; //reset button to zero
  }
}
```

### บันทึกผลการทดลอง

1. บันทึกผลการทดลองที่ logic analyzer แสดง
