Docsity

Serial peripheral interface (SPI), Lecture notes of Electrical Engineering

Partial preview of the text

Download Serial peripheral interface (SPI) and more Lecture notes Electrical Engineering in PDF only on Docsity!

SPI Bus

Serial Peripheral Interface

Introduction

  • The SPI bus uses separate lines for sending and receiving data
  • It employs an additional line for selecting which slave device you are talking to
  • This adds extra wires, but also eliminates the problem of needing different slave device addresses.
  • The SPI bus is generally easier to get running than I2C and can run at a faster speed

Data can be shifted into the slave device on either the rising or falling edge of the clock signal (called the clock phase ), and the SCLK default state can be set to either high or low (called the clock polarity ).

SPI Communication Modes

Because there are two options for the clock phase and two options for the clock polarity, you can configure the SPI bus in a total of four ways

SPI Communication Lines

Communication Scheme

  • The SPI communication scheme is synced with the clock signal and depends on the state of the SS line.
  • Because all devices on the bus share the MOSI, MISO, and SCLK lines, all commands sent from the master arrive at each slave.
  • The SS pin tells the slave whether it should ignore this data or respond to it.
  • Importantly, this means that you must make sure to only have one SS pin set low (the active mode) at a time in any program that you write.

Comparing SPI to I2C and UART

Communicating with an SPI Accelerometer

An accelerometer is one of three common positioning sensors that you might find in a modern smartphone (the other two being a gyroscope and a magnetometer).

  • Gyroscopes measure rotational motion,
  • Magnetometers measure magnetic fields (including that of the Earth),
  • Accelerometers measure linear acceleration.

An Arduino acts as the master device and the accelerometer as the slave

  • Accelerating this MEMS accelerometer will push the movable mass in the direction opposite of the acceleration.
  • This, in turn, causes the distance between the immovable plates and the moving plates to increase or decrease.
  • These plates form tiny capacitors, with the capacitance changing as a function of the plates’ distance from one another.
  • As the plates move closer, the increased capacitance can be measured, and this can be correlated to a rate of acceleration.

LIS3DH pin-out and orientation diagram

Wire the SPI pins from the breakout board to the Arduino’s pins that are listed in Table

Then, connect an RGB LED to pins 6, 5, and 3 (all PWM-capable, in case you want to control the brightness of the LED) via 220Ω current-limiting resistors.

Remember, this is a common-anode LED, so the common pin (the longest one) should connect to 5V from the Arduino.

Don’t forget to also connect the breakout board’s 5V and GND lines to the Arduino.

Accelerometer-based orientation sensor—orientation.ino // Uses the Z-Axis of an Accelerometer to detect orientation // Include Libraries // This library will, itself, include the SPI and Universal Sensor Libraries

**#include **

// Define the pins (the SPI hardware pins are used by default) const int RED_PIN = 6; const int GREEN_PIN = 5; const int CS_PIN = 10;

// Set the sensitivity of the accelerometer to +/-2G accel.setRange(LIS3DH_RANGE_2_G); // Set the LED cathode pins as outputs and turn them off // HIGH is off because this is a common anode LED

pinMode(RED_PIN, OUTPUT); digitalWrite(RED_PIN, HIGH); pinMode(GREEN_PIN, OUTPUT); digitalWrite(GREEN_PIN, HIGH); } void loop() { // Get X, Y, and Z accelerations accel.read(); // Print the Raw Z Reading Serial.print("Raw: "); Serial.print(accel.z);

// Map the Raw Z Reading G's based on +/-2G Range Serial.print("\tActual: "); Serial.print((float(accel.z)/32768.0)2.0); Serial.println("G");* // Check if we are upside-down if (accel.z < 0) { digitalWrite(RED_PIN, LOW); digitalWrite(GREEN_PIN, HIGH); } else { digitalWrite(RED_PIN, HIGH); digitalWrite(GREEN_PIN, LOW); } // Get new data every 100ms delay(100); }