本章我们将深入学习 SPI 在 Arduino 硬件和开发环境中是如何工作和应用的,将使用 Arduino Uno 作为标准开发板,你可以使用其他型号的 Arduino 开发板。、

SPI 被用来从一个处理器发送、接收串行数据到另一个处理器,或者另一个外设,如 LCD 液晶屏,温度传感器,SD记忆卡等等。

更多关于 SPI 信息,请查看 Wiki

http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus

如需了解更多 Arduino 官方对 SPI 描述,请查看 Arduino 官方网站

http://arduino.cc/en/Reference/SPI

原文

http://www.gammon.com.au/forum/?id=10892

 

第一节:发送数据

让我们从一张图片开始,下图为逻辑分析仪截取数据的截图,这张图中表述的是如何从 Arduino 通过 SPI 发送 “Fab” 这3个字符。

SPI_logical_analyzer_1

 

我们通过设置 SS (CS)下降沿触发,来告诉分析仪何时开始捕捉信号。

当SS (CS) 变为高电平,表示该串数据已发送完毕,通过上图,我们可以获得如下信息:

  • A – 没有数据,SS 为高电平,时钟信号(SCK)为低电平。
  • B – SS taken low to enable the slave (peripheral). At this point the slave should prepare to transfer data by setting the MOSI (master out, slave in) line, and the SCK (serial clock) as inputs, and the MISO (master in, slave out) as an output. The slave can now prepare to notice clock pulses on the SCK line.
  • C – First character arrives (the letter “F” or 0x46 or 0b01000110). For each of the 8 bits the SCK (clock) line is briefly brought high, and then low again. This tells the slave to read the data on the MOSI line. Also the slave can place data on the MISO line for the master to simultaneously read in.
  • D – The letter “a” arrives
  • E – The letter “b” arrives
  • F – “No data” after “Fab” – however the SS is still enabled.
  • G – SS taken high to indicate end of the sequence of data. At this stage the slave should release the MISO line (configure it as an input, or “high impedance”). Also the slave should ignore any clock pulses now (they may be for a different peripheral).
#include <SPI.h>
#include "pins_arduino.h"

void setup (void)
{
}


void loop (void)
{

  
  digitalWrite(SS, HIGH);  // ensure SS stays high

  // Put SCK, MOSI, SS pins into output mode
  // also put SCK, MOSI into LOW state, and SS into HIGH state.
  // Then put SPI hardware into Master mode and turn SPI on
  SPI.begin ();

  delay (5000);  // 5 seconds delay to start logic analyser.

  char c;
  
  // enable Slave Select
  digitalWrite(SS, LOW);    // SS is pin 10
  
  // send test string
  for (const char * p = "Fab" ; c = *p; p++)
    SPI.transfer (c);

 // disable Slave Select
 digitalWrite(SS, HIGH);

 // turn SPI hardware off
 SPI.end ();
 
 while (1);  //loop
}

 

发送和接收数据

SPI_logical_analyzer_3

SPI 工作模式

For maximum flexibility with various slaves, the SPI protocol allows for variations on the polarity of the clock pulses. CPOL is clock polarity, and CPHA is clock phase.

  • Mode 0 (默认模式)- 时钟线空闲时为低 (CPOL = 0), and the data is sampled on the transition from low to high (leading edge) (CPHA = 0)
  • Mode 1 – 时钟线空闲时为低 (CPOL = 0), and the data is sampled on the transition from high to low (trailing edge) (CPHA = 1)
  • Mode 2 – 时钟线空闲时为高 (CPOL = 1), and the data is sampled on the transition from high to low (leading edge) (CPHA = 0)
  • Mode 3 – 时钟线空闲时为高(CPOL = 1), and the data is sampled on the transition from low to high (trailing edge) (CPHA = 1)

Mode 0

SPI_logical_analyzer_mode0

Mode 1

SPI_logical_analyzer_mode1

Mode 2

SPI_logical_analyzer_mode2

Mode 3

SPI_logical_analyzer_mode3