In the STM32F10x series chips, although the number of SPI synchronous serial ports is different, the initialization method is the same. When using SPI, we recall the use of GPIO and pay attention to 6 points. We will mainly explain one point here.
According to the system's requirements for different functions, it will be more complicated to initialize different functional peripherals, initialize GPIO, and initialize SPI synchronous serial port devices. All SIP synchronous serial ports of STM32F10x series chips are shared with GPIO. The initialization of SPI is divided into two major parts, namely the initialization of the I/O port used by SPI and the initialization of SPI functions. For the pins used by the SPI synchronous serial port, according to the direction of the data, set GPIO_Mode_IN_FLOATING or GPIO_Mode_AF_PP multiplexing push-free output. The other settings are the same as the GPIO pin settings.
The initialization method of the SPI synchronous serial port parameters will be shown in a later example.
When we use SPI, we must turn on the clock of the SPI synchronous serial port, so that the system can send and receive data from this opened synchronous serial port.
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE);
Sending data in SPI is very simple. When the data byte to be sent is written into the transmit buffer, the transmission process begins. However, before trying to write to the transmit buffer, it is necessary to confirm whether the TXE flag is 1.
Similarly for the receiver, when the data transmission is completed, the data is also completed after the end. At the last sampling clock edge, the RXNE flag is set. The data word received in the shift register is transferred to the receive buffer. The RXNE bit is cleared when the SPI_DR register is set.
Therefore: when sending data, it is necessary to determine whether the previous data has been sent. Only when the data is sent completely can new data be sent.
For example: PA7 is used as the MOSI pin of SPI1 synchronous serial port, PA6 is used as the MISO pin of SPI1 synchronous serial port, PA5 is used as the SCK pin, and PA4 is used as the NSS chip select pin. Define the synchronous serial port SPI1 as the master device, send 8 data bits each time, the high bit is first, the SPI clock frequency is 9MHz, the data is captured at the second clock edge, the clock is low when idle, and the NSS pin is managed by software.
The complete code is as follows:
#include "stm32f10x_lib.h"
void SPI_Configuration(void)
{
// SPI_1 serial port parameter definition, SPI_1 parameters are clock is usually high, rising edge sampling
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI2,ENABLE); //Enable SPI_2 clock
//Configure SPI1's MISO (PA6) as a floating input
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_MODE = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStructure);
//Configure SPI1's MOSI (PA7) and SPI1's CLK (PA5) as multiplexed push-to-disconnect outputs
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_7;
GPIO_InitSturcture.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
//Configure SPI1's NSS (PA4) as push output
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitSturcture.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA,&GPIO_InitSturcture);
//SPI1 synchronization parameter initialization definition
SPI_InitTypeDef SPI_InitStructure;
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_Fullduplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI1,&SPI_InitStructure);
SPI_Cmd(SPI1,ENABLE);
}
//Send a data through SPI1 port and receive a data at the same time
unsigned char SPI_SendByte(unsigned char Byte)
{
//If the register data is not sent, wait in a loop
while(SPI_T1S_GetFlagStatus(SPI1,SPI_T1S_FLAG_TXE) == RESET);
//Write the data to be sent to the send register
SPI_T1S_SendData(SPI2,Byte);
//If the receiving register does not receive data, loop
while(SPI_T1S_GetFlagStatus(SPI1,SPI_T1S_FLAG_RXNE) == RESET);
return SPI_T1S_ReceiveData(SPI1);
}
Previous article:ARM9 2440 Hardware SPI Driver - NRF24L01
Next article:Basic introduction and points to note about bit operations in STM32
Recommended ReadingLatest update time:2024-11-25 05:16
- Popular Resources
- Popular amplifiers
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- CATL releases October battle report
- Battery industry in October 2024: growth momentum remains unabated!
- Mercedes-Benz will launch the eCitaro equipped with NMC4 batteries to provide high energy density and long life
- Many companies have announced progress on solid-state batteries. When will solid-state batteries go into mass production?
- Xsens Sirius Series Inertial Sensors Enable 3D Inertial Navigation in Harsh Environments
- Infineon's Automotive Landscape: From Hardware to Systems
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- How to use the standard library time_t time(time_t *seconds)? Is there a difference between the value obtained by the parameter and the return value?
- I'm bored, so I'll post a tutorial on LNA
- TI's 430 routines have various modules, so read them carefully.
- Techniques for PCB board array vias
- EEWORLD University ---- Razavi Analog Integrated Circuit Tutorial (English)
- How to interconnect DSP5509A and ARM
- 2006 Germany World Cup Group Stage Schedule
- 【Industrial Production Process PDA】Material Unboxing Post- DISCOVERY KIT WITH STM32F7508-DK
- An MSP430F schematic diagram, if you don't believe it, you should collect it!
- Will 3D Touch be popular in the future?