void SPI1_Configuration(void) { SPI_InitTypeDef SPI_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); 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); //SPI1 NSS GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_SetBits(GPIOA, GPIO_Pin_4); SPI_Cmd(SPI1, DISABLE); //必须先禁能,才能改变MODE SPI_InitStructure.SPI_Direction =SPI_Direction_2Lines_FullDuplex; //两线全双工 SPI_InitStructure.SPI_Mode =SPI_Mode_Master; //主 SPI_InitStructure.SPI_DataSize =SPI_DataSize_8b; //8位 SPI_InitStructure.SPI_CPOL =SPI_CPOL_High; //CPOL=1时钟悬空高 SPI_InitStructure.SPI_CPHA =SPI_CPHA_1Edge; //CPHA=1 数据捕获第2个 SPI_InitStructure.SPI_NSS =SPI_NSS_Soft; //软件NSS SPI_InitStructure.SPI_BaudRatePrescaler =SPI_BaudRatePrescaler_4; //2分频 SPI_InitStructure.SPI_FirstBit =SPI_FirstBit_MSB; //高位在前 SPI_InitStructure.SPI_CRCPolynomial =7; //CRC7 SPI_Init(SPI1,&SPI_InitStructure); SPI_Cmd(SPI1, ENABLE); //spi的配置结束了可以使用了。 }u8 SPI1_ReadWriteByte(u8 val) { /* Wait until the transmit buffer is empty */ while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); /* Send the byte */ SPI_I2S_SendData(SPI1, val); /* Wait until a data is received */ while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET); /* Get the received data */ return SPI_I2S_ReceiveData(SPI1); } |
RE:stm32 之 SPI初始化 仿真的时候显示NSS管脚未使能?
RE:stm32 之 SPI初始化 仿真的时候显示NSS管脚未使能?