introduction
When you connect a microcontroller to a sensor, display, or other module, have you considered how the two devices communicate with each other? What are they talking about? How do they understand each other?
Communication between electronic devices is like communication between humans. Both parties need to speak the same language. In electronics, these languages are called communication protocols. Luckily for us, we only need to know a few communication protocols when building most DIY electronics projects.
First, we'll start with some basic concepts about electronic communications, then detail how SPI works.
SPI, I2C, and UART are much slower than protocols like USB, Ethernet, Bluetooth, and WiFi, but they are simpler and use fewer hardware and system resources. SPI, I2C and UART are ideal for communication between microcontrollers and between microcontrollers and sensors that do not require the transmission of large amounts of high-speed data.
text
Serial and parallel communications
Electronic devices communicate with each other by sending bits of data through wires that are physically connected between the devices. A bit like the letters in a word, except instead of 26 letters (the letters in the English alphabet), the bits are binary and can only be 1 or 0. Bits are transferred from one device to another through rapid changes in voltage. In a system operating at 5 V, bit 0 communicates as a short pulse of 0 V, while bit 1 communicates as a short pulse of 5 V.
Data bits can be transmitted in parallel or serial form. In parallel communication, data bits are sent all at the same time over a single wire. The image below shows the parallel transmission of the letter "C" in binary (01000011):
In serial communication, bits are sent one by one over a single wire. The image below shows the serial transmission of the letter "C" in binary (01000011):
Introduction to SPI communication
SPI is a common communication protocol used by many different devices. For example, SD card modules, RFID card reader modules, and 2.4 GHz wireless transmitter/receivers all use SPI to communicate with microcontrollers.
A unique advantage of SPI is that it can transfer data without interruption. Any number of bits can be sent or received in a continuous stream. Using I2C and UART, data is sent in packets, limited to a specific number of bits. Start and stop conditions define the beginning and end of each packet, so data is interrupted during transmission.
Devices communicating via SPI are in a master-slave relationship. The master device is the controlling device (usually a microcontroller), while the slave devices (usually sensors, displays, or memory chips) get instructions from the master device. The simplest configuration of SPI is a single master, one slave system, but one master can control multiple slaves (more on this below).
MOSI (Master Out/Slave In) – The line on which the master sends data to the slave.
MISO (Master In/Slave Out) – The line on which the slave sends data to the master.
SCLK (clock) – clock signal line.
SS/CS (Slave Select/Chip Select) – The line used by the master to select the slave to which it sends data.
In practice, the number of slaves is limited by the system load capacitance, which reduces the master's ability to accurately switch between voltage levels.
How does SPI work?
clock
The clock signal synchronizes the master's data bit output to the slave's bit sampling. One bit of data is transferred per clock cycle, so the speed of data transfer depends on the frequency of the clock signal. SPI communication is always initiated by the host since the host configures and generates the clock signal.
Any communication protocol in which devices share a clock signal is called synchronization. SPI is a synchronous communication protocol. There are also asynchronous methods that do not use clock signals. For example, in UART communication, both sides are set to a preconfigured baud rate, which determines the speed and timing of data transfer.
Clock signals in SPI can be modified using the properties of clock polarity and clock phase. Together, these two properties define when a bit is output and when it is sampled. The host can set the clock polarity to allow bits to be output and sampled on the rising or falling edge of the clock cycle. The clock phase can be set so that the output and sample occur on the first or second edge of the clock cycle, regardless of whether it is rising or falling.
Slave selection
The master can select a slave to communicate with by setting the slave's CS/SS lines to a low voltage level. In the idle, non-transmitting state, the slave select line remains at a high voltage level. There may be multiple CS/SS pins on the master, which allows multiple slaves to be connected in parallel. If only one CS/SS pin is present, multiple slaves can be daisy chained to the master.
multiple slaves
The SPI can be set up to run with a single master and a single slave, and can be set up with multiple slaves controlled by a single master. There are two ways to connect multiple slaves to the master. If the master has multiple slave select pins, the slaves can be connected in parallel as follows:
If only one slave select pin is available, slaves can be daisy-chained via:
MOSI and MISO
The master sends data to the slave serially over the MOSI line. The slave receives the data sent by the master through the MOSI pin. Data sent from the master to the slave is usually sent most significant bit first.
The slave can also send data back to the master serially over the MISO line. Data sent back to the master from the slave is usually sent least significant bit first.
SPI data transfer steps
Host output clock signal:
The master switches the SS/CS pin to a low voltage state, thus activating the slave:
The master sends data to the slave one at a time along the MOSI line. The slave reads the received bits:
If a response is required, the slave returns data one bit at a time along the MISO line to the master. The host reads the received bits:
Advantages and Disadvantages of SPI
There are some advantages and disadvantages to using SPI, and if you are choosing between different communication protocols, you should know when to use SPI based on your project requirements:
advantage
There are no start and stop bits, so data can stream continuously without interruption
No complex slave addressing system like I2C
Higher data transfer rate than I2C (almost twice as fast)
Separate MISO and MOSI lines so data can be sent and received simultaneously
shortcoming
Use four wires (I2C and UART use two wires)
No acknowledgment that data has been received successfully (I2C has this feature)
Doesn't have any form of error checking like parity bits in UART
Only one host allowed
Previous article:What is a microcontroller? What is a microcontroller?
Next article:How does MCU run programs on extended SDRAM?
Recommended ReadingLatest update time:2024-11-15 07:21
- Popular Resources
- Popular amplifiers
- 100 Examples of Microcontroller C Language Applications (with CD-ROM, 3rd Edition) (Wang Huiliang, Wang Dongfeng, Dong Guanqiang)
- Digilent Vivado library
- Teach you to learn 51 single chip microcomputer-C language version (Second Edition) (Song Xuefeng)
- Raspberry Pi Development in Action (2nd Edition) ([UK] Simon Monk)
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- How are chips made? CCTV's most powerful science popularization!
- Unboxing and verifying BOX function through APP
- Thermostat reference design based on TI SimpleLink wireless MCU
- Pre-registration for the live broadcast with prizes | ST Data Center and Communication Network Power Management Solutions
- MSP430 - Key interrupt control small light flip
- [NXP Rapid IoT Review] Rapid IoT Studio: The First Step in Simple Programming
- Adafruit Funhouse Development Board
- GD32E231 DIY Contest (9) - Rewrite the key processing program
- 【Distributed temperature and humidity acquisition system】+WIFI data receiving module
- Autonomous driving company TuSimple is recruiting for 2022, welcome to apply