LAB: SPI Protocol

จุดประสงค์

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

LAB: Logic Analyzer with SPI Protocol

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

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

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

โปรแกรม

  1. Arduino IDE

  2. โปรแกรม KingstVIS

การตั้งค่า KingstVIS program

  • เชื่อมต่อ logic Analyzer กับ คอมพิวเตอร์

  • เปิดโปรแกรม Kingst VIS

  • ไปที่ Analyzers >> คลิ๊ก icon add(+) >> เลือก SPI

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

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

ตั้งค่า Display Format แสดง ASCII
  • ไปที่ Channel 3 เลือกกด Indicates "rising edge trigger"

Trigger condition setting

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

Code: SPI master (M5 stack)

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

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

Last updated

Was this helpful?