SPI通信的发送接收是同时进行,即主机发送一个字节的同时接收一个字节。
两个STM32主机与从机连接方法
CS—–CS
CLK—CLK
MISO—MISO
MOSI—MOSI
主机程序
STM32F103CBT6
SPI2进行数据发送
- void SPI_Deassert(void)
- {
- GPIO_SetBits(CS_PORT, CS_PIN);
- }
- /*******************************************************************************
- * Function Name : SPI2_Configuration
- * Description : Configures SPI2 as master
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- void SPI2_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- SPI_InitTypeDef SPI_InitStructure;
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
- SPI_Deassert(); // Set pin high before initializing as output pin to not false trigger cs
- RCC_APB2PeriphClockCmd(CS_RCC, ENABLE);
- GPIO_InitStructure.GPIO_Pin = CS_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(CS_PORT, &GPIO_InitStructure);
- SPI_Deassert();
- // Configure SPI2 pins: SCK, MOSI
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_15;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- // Configure SPI2 pins: MISO
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- SPI_Cmd(SPI2, DISABLE);
- SPI_StructInit(&SPI_InitStructure);
- SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
- SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
- SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;//因主从始终不同,这里有区别
- 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_FirstBit = SPI_FirstBit_MSB;
- //SPI_InitStructure.SPI_CRCPolynomial = 7;
- SPI_SSOutputCmd(SPI2, ENABLE);
- SPI_Init(SPI2, &SPI_InitStructure);
- SPI_Cmd(SPI2, ENABLE);
- }
- uint8_t SPI_Sendbyte(uint8_t byte)
- {
- while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET)
- ;
- SPI_I2S_SendData(SPI2, byte);
- while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET)
- ;
- return (SPI_I2S_ReceiveData(SPI2));
- }
从机程序
STM32F103C8T6
SPI1接收
- void SPI_Deassert(void)
- {
- GPIO_SetBits(CS_PORT, CS_PIN);
- }
- /*******************************************************************************
- * Function Name : SPI2_Configuration
- * Description : Configures SPI1 as master
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- void SPI1_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- SPI_InitTypeDef SPI_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_SPI1, ENABLE);
- SPI_Deassert(); // Set pin high before initializing as output pin to not false trigger cs
- RCC_APB2PeriphClockCmd(CS_RCC, ENABLE);
- GPIO_InitStructure.GPIO_Pin = CS_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(CS_PORT, &GPIO_InitStructure);
- SPI_Deassert();
- // Configure SPI1 pins: SCK (pin 5) and MOSI (pin 7)
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- // Configure SPI1 pins: MISO(pin 6)
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- SPI_Cmd(SPI1, DISABLE);
- SPI_StructInit(&SPI_InitStructure);
- SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
- SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
- 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_128;
- SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
- SPI_Init(SPI1, &SPI_InitStructure);
- SPI_Cmd(SPI1, ENABLE);
- }
- uint8_t SPI_Sendbyte(uint8_t byte)
- {
- while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET)
- ;
- SPI_I2S_SendData(SPI1, byte);
- while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET)
- ;
- return (SPI_I2S_ReceiveData(SPI1));
- }
使用时,可以使用中断,也可以直接while循环进行等待。发送直接调用SPI_Sendbyte()函数,接收也可以直接使用SPI_Sendbyte()函数等待。