SPI And Arduino Interface

Table of contents:

SPI And Arduino Interface
SPI And Arduino Interface

Video: SPI And Arduino Interface

Video: SPI And Arduino Interface
Video: Видеоуроки по Arduino. Интерфейсы SPI (8-я серия, ч1) 2024, December
Anonim

We study the SPI interface and connect a shift register to the Arduino, which we will access using this protocol to control the LEDs.

SPI interface
SPI interface

Necessary

  • - Arduino;
  • - shift register 74HC595;
  • - 8 LEDs;
  • - 8 resistors of 220 Ohm.

Instructions

Step 1

SPI - Serial Peripheral Interface or "Serial Peripheral Interface" is a synchronous data transfer protocol for interfacing a master device with peripheral devices (slave). The master is often a microcontroller. Communication between devices is carried out over four wires, which is why SPI is sometimes referred to as a "four-wire interface". These tires are:

MOSI (Master Out Slave In) - data transmission line from the master to the slave devices;

MISO (Master In Slave Out) - transmission line from the slave to the master;

SCLK (Serial Clock) - synchronization clock pulses generated by the master;

SS (Slave Select) - slave device selection line; when on line "0", the slave "understands" that it is being accessed.

There are four modes of data transfer (SPI_MODE0, SPI_MODE1, SPI_MODE2, SPI_MODE3), due to the combination of clock pulse polarity (we work at the HIGH or LOW level), Clock Polarity, CPOL, and the phase of the clock pulses (synchronization on the rising or falling edge of the clock pulse), Clock Phase, CPHA.

The figure shows two options for connecting devices using the SPI protocol: independent and cascading. When independently connected to the SPI bus, the master communicates with each slave individually. With a cascade - the slave devices are triggered alternately, in a cascade.

Types of SPI connections
Types of SPI connections

Step 2

In the Arduino, the SPI buses are on specific ports. Each board has its own pin assignment. For convenience, the pins are duplicated and placed on a separate ICSP (In Circuit Serial Programming) connector. Please note that there is no slave select pin on the ICSP connector - SS, because it is assumed that the Arduino will be used as the master on the network. But if necessary, you can assign any digital pin of the Arduino as SS.

The figure shows the standard assignment of the pins to the SPI buses for the Arduino UNO and Nano.

SPI implementation in Arduino
SPI implementation in Arduino

Step 3

A special library has been written for Arduino that implements the SPI protocol. It is connected like this: at the beginning of the program, add #include SPI.h

To start working with the SPI protocol, you need to set the settings and then initialize the protocol using the SPI.beginTransaction () procedure. You can do this with one instruction: SPI.beginTransaction (SPISettings (14000000, MSBFIRST, SPI_MODE0)).

This means that we initialize the SPI protocol at a frequency of 14 MHz, data transfer goes, starting from MSB (most significant bit), in the "0" mode.

After initialization, we select the slave device by putting the corresponding SS pin in the LOW state.

Then we transfer the data to the slave device with the SPI.transfer () command.

After transmission, return SS to the HIGH state.

Work with the protocol ends with the SPI.endTransaction () command. It is desirable to minimize the execution time of the transfer between the SPI.beginTransaction () and SPI.endTransaction () instructions so that there is no overlap if another device tries to initialize data transfer using different settings.

SPI transmission
SPI transmission

Step 4

Let's consider the practical application of the SPI interface. We will light the LEDs by controlling the 8-bit shift register via the SPI bus. Let's connect the 74HC595 shift register to the Arduino. We connect to each of the 8 outputs via an LED (through a limiting resistor). The diagram is shown in the figure.

Connecting shift register 74HC595 to Arduino
Connecting shift register 74HC595 to Arduino

Step 5

Let's write such a sketch.

First, let's connect the SPI library and initialize the SPI interface. Let's define pin 8 as the slave selection pin. Let's clear the shift register by sending the value "0" to it. We initialize the serial port.

To light a specific LED using a shift register, you need to apply an 8-bit number to its input. For example, in order for the first LED to light up, we feed the binary number 00000001, for the second one - 00000010, for the third one - 00000100, etc. These binary numbers in decimal notation form the following sequence: 1, 2, 4, 8, 16, 32, 64, 128 and are powers of two from 0 to 7.

Accordingly, in the loop (), we recalculate from 0 to 7 by the number of LEDs. The pow (base, power) function raises 2 to the power of the loop counter. Microcontrollers do not work very accurately with numbers of the "double" type, so to convert the result to an integer, we use the round () function. And we transfer the resulting number to the shift register. For clarity, the serial port monitor displays the values that are obtained during this operation: one runs through the digits - the LEDs light up in a wave.

Sketch for controlling the shift register via the SPI bus
Sketch for controlling the shift register via the SPI bus

Step 6

The LEDs light up in turn, and we observe a traveling "wave" of lights. The LEDs are controlled using a shift register, to which we connected via the SPI interface. As a result, only 3 Arduino pins are used to drive 8 LEDs.

We have studied the simplest example of working with an Arduino SPI bus. We will consider the connection of shift registers in more detail in a separate article.

Recommended: