我用开发板的SPI1和SPI2通信,结果SPI1为主模式时,SPI2可以接收到,但SPI1接收不到,
我把代码中的1该成2,2改成1,结果就正常了!不知到怎么回事,下面是代码
RCC_APB2PeriphClockCmd( RCC_APB2Periph_SPI1|RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO, ENABLE );
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
/* GPIO */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7 ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_Init(GPIOB,&GPIO_InitStructure);
/* SPI配置 */
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; /* 双线_全双工 模式 */
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; /* 作为主机 */
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; /* 数据位是8bits */
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; /* SPI时钟空闲时,保持高电平 */
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;/* 数据采样从第二个时钟边沿开始 */
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; /* 软件NSS管理 */
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;/* 波特率 = APB2/4 */
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; /* 字节顺序是 the Most Significant Bit 在前(最高位在前)*/
SPI_InitStructure.SPI_CRCPolynomial = 7; /* 校验多项式值,开机默认0x07 */
SPI_Init(SPI2, &SPI_InitStructure);
SPI_Cmd(SPI2, ENABLE);
SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE);
SPI_I2S_ClearFlag(SPI2,SPI_I2S_FLAG_RXNE);
SPI_I2S_ClearFlag(SPI1,SPI_I2S_FLAG_RXNE);
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI2, 0x11);
SPI1->DR = 0x22;
// SPI_I2S_SendData(SPI2,0x22);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
data = SPI_I2S_ReceiveData(SPI1);
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET);
data = SPI_I2S_ReceiveData(SPI2); |