* UART, I2C and SPI are the three most commonly used communication protocols in microcontroller systems. *
1. Introduction to SPI:
SPI is a high-speed, full-duplex, synchronous communication bus. The standard SPI uses only 4 pins and is often used for communication between microcontrollers and EEPROM, FLASH, real-time clocks, digital signal processors and other devices. The SPI communication principle is simpler than I2C. It is mainly a master-slave communication mode. This mode usually has only one host and one or more slaves. The standard SPI has 4 wires, namely SSEL (chip select, also written as SCS), SCLK (clock, also written as SCK), MOSI (Master Output/Slave Input) and MISO (Master Input/Slave Output).
SSEL: Slave chip select enable signal. If the slave device is low level enabled, when this pin is pulled low, the slave device will be selected, and the host will communicate with the selected slave.
SCLK: Clock signal, generated by the host, somewhat similar to the SCL of I2C communication.
MOSI: The channel through which the host sends instructions or data to the slave.
MISO: The channel through which the host reads the status or data of the slave.
2. Programming
Send a byte
void SPISendByte(unsigned char ch)
{
unsigned char idata n = 8; //Send one data byte to SDA, a total of eight bits
SCLK = 1; // Set the clock high
SSEL = 0; //Select slave
while(n--)
{
delayNOP();
SCLK = 0; //Clock low
if((ch & 0x80) == 0x80) //If the highest bit of the data to be sent is 1, send bit 1
{
MOSI = 1; //Transmit bit 1
}
else
{
MOSI = 0; //Otherwise transmit bit 0
}
delayNOP();
ch = ch << 1; // data shifted one bit to the left
SCLK = 1; // Set the clock high
}
}
Receive a byte
unsigned char SPIreceiveByte()
{
unsigned char idata n = 8; // Read a data byte from the MISO line, a total of eight bits
unsigned char tdata;
SCLK = 1; //Clock is high
SSEL = 0; //Select slave
while(n--)
{
delayNOP();
SCLK = 0; //Clock is low
delayNOP();
tdata = tdata << 1; // Shift left by one bit, or _crol_(temp,1)
if(MISO == 1)
{
tdata = tdata | 0x01;
}// If the received bit is 1, the last bit of the data is set to 1
else
{
tdata = tdata & 0xfe;
}// Otherwise the last position of the data is 0
SCLK = 1;
}
return(tdata);
}
At the beginning of each time, the clock line SCLK is at a high level, MISO is the data receiving bit, the highest bit is received first, and the data is obtained by shifting left 8 times. MOSI is the data sending bit, and the highest bit of the data is sent first.
The sending and receiving of data are executed between the statements SCLK=0; and SCLK=1;, that is:
SCLK = 0;
Data transmission, reception and shifting
SCLK = 1;
That is, data transmission and reception start with the falling edge of SCLK and end with the rising edge.
---------------------
Author: Turbidity and Clearness
Source: CSDN
Original text: https://blog.csdn.net/qq_34706280/article/details/78702106
Copyright Statement: This article is an original article by the blogger. Please attach the blog link when reprinting!
Previous article:8051 MCU baud rate calculation formula (with C language routine)
Next article:Two infrared decoding methods (RC-5)
- Popular Resources
- Popular amplifiers
- 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
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Brief Analysis of Automotive Ethernet Test Content and Test Methods
- How haptic technology can enhance driving safety
- Let’s talk about the “Three Musketeers” of radar in autonomous driving
- Why software-defined vehicles transform cars from tools into living spaces
- What are the applications of wiring harness testers in the automotive field?
- ESP32-S3 is now available
- DC grounding detection system based on MSP430
- Beautiful Smart NeoPixel LED Cube
- What electrical and electronic engineers must know
- Exposing a black-hearted supplier
- About AD17 export step file SOLIDWORKS
- [ESP32-Audio-Kit Audio Development Board Review]——(1): Selecting a development environment based on vs code
- I don't understand the concept of divergence. Can anyone explain this concept in a vivid way and its relationship with inflow and outflow?
- Why does the MOS tube burn when LTC4359 is used as a load switch solution?