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

![อุกปรณ์การทดลอง](/files/-MI84l_k-9aPl5JlxpG8)

### โปรแกรม

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

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

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

![](/files/-MI7zuz4iyaxcpgE7PTj)

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

![](/files/-MI8-7C36rXCsWjuMEiv)

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

![ตั้งค่า Display Format แสดง ASCII  ](/files/-MI3PZLezuIVwcXf2Usg)

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

![Trigger condition setting](/files/-MHu3jGoXJ81ffmTz-JM)

### 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 ที่ใช้ในการทดลองนี้แสดงดังภาพด้านล่าง

![](/files/-MI87hF9vIwUncAxchzH)

### 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 แสดง


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.aic-eec.com/embedded-systems/resources/basic-hardware-and-firmware/interfacing-and-communication/lab-spi-protocol.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
