STM32 SPI usage

Publisher:AngelicGraceLatest update time:2017-11-07 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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

STM32 SPI usage

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   

}


Keywords:STM32 Reference address:STM32 SPI usage

Previous article:STM32 IIC reads AT24C02
Next article:stm32 serial port dma receives asynchronous data

Recommended ReadingLatest update time:2024-11-23 11:13

Focusing on AI and IoT, the STM32 Summit is coming soon
With the theme of "Gathering Wisdom, Creating the Future", the 2019 STM32 Summit focuses on three major topics: artificial intelligence and computing, industry and security, and cloud technology and connectivity. During the two-day summit, STMicroelectronics and 45 partners will jointly exhibit more than 180 prototype
[Internet of Things]
Understanding of stm32 timer
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; The surface meaning is that the output control polarity is high, but it means that the timer input is 0, not inverted, and the output is 0; The output control polarity is low, the timer input is 0, and the inverted output is 1; CC1E controls the switch. #define
[Microcontroller]
Understanding of stm32 timer
Key points analysis of infrared remote control based on STM32
This article has two contents: 1. Explanation of infrared remote control protocol; 2. Analysis of decoding program (refer to the code of the atom at the right time). I will not tell you about the introduction, advantages and disadvantages of infrared. Let's get to the point. 1. Explanation of infrared remote control p
[Microcontroller]
Key points analysis of infrared remote control based on STM32
Create STM32 project in IAR5.4
1. Download the stm32 peripheral driver library file stm32f10x_stdperiph_lib from www.st.com . The latest one is STM32F10x_StdPeriph_Lib_V3.3.0. 2. Open the stm32f10x_stdperiph_libSTM32F10x_StdPeriph_Lib_V3.3.0ProjectSTM32F10x_StdPeriph_Template folder and copy the EWARMv5 folder to your own project folder (this is th
[Microcontroller]
Some experience in debugging STM32 UDP function
I'm studying STM32F107VC recently. Since a task requires UDP, I plan to use the development board combined with LWip to implement this function. However, I encountered some problems in the process of debugging UDP. After searching on the Internet for a long time, I finally got the basic functions working. I plan to re
[Microcontroller]
STM32 scatter loading file
By using the scatter-loading mechanism, you can specify the memory map of an image to the linker. Scatter-loading gives you complete control over the grouping and location of image components. Scatter-loading can be used for simple images, but it is usually only used for images with complex memory maps, where multiple
[Microcontroller]
STM32 scatter loading file
STM32 serial communication: modify the standard library's printf to print via USART
USART1 needs to be configured in advance, which is quite troublesome. After configuration, I found that even if I use HAL function to send statements, I have to define them in string form in advance, which is really troublesome. Although I wrote a simple library to operate the serial port later, I saw a simpler method
[Microcontroller]
Design of AC permanent magnet synchronous motor driver based on STM32
Introduction In recent years, with the rapid development of microelectronics technology, power electronics technology, modern control technology, material technology and the gradual improvement of motor manufacturing process level, AC permanent magnet synchronous motors have been widely used in industry and agricultu
[Microcontroller]
Design of AC permanent magnet synchronous motor driver based on STM32
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号