STM32F4 SPI NSS硬件模式配置及使用(SPI硬件模式和软件模式简单理解)

it2023-02-07  47

        我们知道,SPI_NSS有两种模式,SPI_NSS_Hard和SPI_NSS_Soft。         SPI_NSS_Hard,硬件自动拉高拉低片选,在速率上是远比软件方式控制要高的,缺点是当STM32为主设备时,同一个SPI上面只能接一个从设备。这也就限制了SPI通信设备的数量。         SPI_NSS_Soft,软件控制,GPIO控制片选拉高拉低,优点是一个SPI上面可以挂多个设备。这也是使用较多的方式。所以不再讲述这个,主要讲一下SPI_NSS_Hard下,该如何配置及使用SPI。         话不多说,直接上代码。

void SPI2_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; SPI_InitTypeDef SPI_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE); GPIO_InitStructure.GPIO_Pin = SPI2_NSS_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(SPI2_NSS_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = SPI2_SCK_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(SPI2_SCK_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = SPI2_MOSI_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(SPI2_MOSI_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = SPI2_MISO_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(SPI2_MISO_PORT, &GPIO_InitStructure); GPIO_PinAFConfig(SPI2_NSS_PORT,SPI2_NSS_PIN_SOURCE,GPIO_AF_SPI2); GPIO_PinAFConfig(SPI2_SCK_PORT,SPI2_SCK_PIN_SOURCE,GPIO_AF_SPI2); GPIO_PinAFConfig(SPI2_MISO_PORT,SPI2_MISO_PIN_SOURCE,GPIO_AF_SPI2); GPIO_PinAFConfig(SPI2_MOSI_PORT,SPI2_MOSI_PIN_SOURCE,GPIO_AF_SPI2); SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Hard; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI2, &SPI_InitStructure); SPI_SSOutputCmd(SPI2,ENABLE); SPI_CalculateCRC(SPI2,DISABLE); SPI_ClearFlag(SPI2,SPI_FLAG_TXE); SPI_ClearFlag(SPI2,SPI_FLAG_RXNE); }

        STM32是可以自动拉高拉低的,是通过SPI_Cmd这个函数实现的,使能SPI,硬件自动拉低片选,取消使能SPI,硬件自动拉高片选。

delay_us(28); SPI_Cmd(SPI2,ENABLE); while(SPI_GetFlagStatus(SPI2,SPI_FLAG_TXE) == RESET); SPI_SendData(SPI2,data); while(SPI_GetFlagStatus(SPI2,SPI_FLAG_RXNE) == RESET); SPI_ReceiveData(SPI2); SPI_Cmd(SPI2,DISABLE);

https://blog.csdn.net/qq_43400642/article/details/86526978?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.channel_param

最新回复(0)