1. SPI usage principle
Data transmission is realized by data exchange, the first jump edge realizes data output, and the second jump edge realizes sampling. As shown in the following figure
2. GPIO configuration
GPIO_InitTypeDef GPIO_InitStructure;
//Configure SPI2 pins
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 |GPIO_Pin_14| GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //Multiplex push-pull output
GPIO_Init(GPIOB, &GPIO_InitStructure);
3. SPI configuration
typedef struct
{
uint16_t SPI_Direction; //Set direction (2-wire full-duplex, 2-wire receive only, 1-wire send, 1-wire receive)
uint16_t SPI_Mode; //Mode (slave or master)
uint16_t SPI_DataSize; //width (8 or 16 bits)
uint16_t SPI_CPOL; //Clock polarity (low or high)
uint16_t SPI_CPHA; //Clock phase (first or second transition edge)
uint16_t SPI_NSS; //Chip select mode (hardware or software)
uint16_t SPI_BaudRatePrescaler; //Baud rate prescaler (from 2 to 256)
uint16_t SPI_FirstBit; //The first bit to be sent (lowest bit or highest bit first)
uint16_t SPI_CRCPolynomial; //Set crc polynomial (number) such as 7
}SPI_InitTypeDef;
//SPI2 configuration options
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE);
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_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI2, &SPI_InitStructure);
//Enable SPI2
SPI_Cmd(SPI2, ENABLE);
4. Exchange data
u8 SPI_Exchange(SPI_TypeDef* SPIx,u8 Byte)
{
while((SPIx->SR&SPI_I2S_FLAG_TXE)==RESET); //Wait for the send area to be empty
SPIx->DR=Byte; //Send a byte
while((SPIx->SR&SPI_I2S_FLAG_RXNE)==RESET);//Wait for receiving a byte
return SPIx->DR; //Return received data
}
Previous article:STM32 IIC reads AT24C02
Next article:stm32 serial port dma receives asynchronous data
Recommended ReadingLatest update time:2024-11-23 11:13
- 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?
- 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
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- PHYTEC's imx8mplus development board review
- [RVB2601 Creative Application Development] 4. Use RTC to record time
- ADN8834 does not work or has current overshoot
- [National Technology N32 MCU Development Package] --N32L40x Series
- Hiring embedded software engineer
- Positive and negative 12V power supply, MC340463, simulation is OK, but the actual limit is about -5V
- Guangzhou color TV company is looking for hardware engineers
- [NUCLEO-WL55JC2 Review 4] STM32WL sub-GHZ RF (LoRa\FSK..)
- AVRDUDE released version 6.4 and moved to github
- Several issues on hysteresis compensation