- //------------------------------SPI2------------------------------
-
- SPI_Cmd(SPI2, DISABLE); //必须先禁能,才能改变MODE
- SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //两线全双工
- SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //主
- SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //8bit
- SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //时钟悬空为低
- SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //数据捕获于第1个时钟沿
- SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
- SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;//主频64分步
- SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; ////高位在前
- SPI_InitStructure.SPI_CRCPolynomial = 7;
- SPI_Init(SPI2,&SPI_InitStructure);
- SPI_Cmd(SPI2,ENABLE);
复制代码- unsigned char SPI_WriteByte(unsigned char data)
- {
- unsigned char Data = 0;
-
- //Wait until the transmit buffer is empty
- while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE)==RESET);
- // Send the byte
- SPI_I2S_SendData(SPI2,data);
-
- //Wait until a data is received
- while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_RXNE)==RESET);
- // Get the received data
- Data = SPI_I2S_ReceiveData(SPI2);
-
- // Return the shifted data
- return Data;
- }
复制代码- GPIO_InitStructure.GPIO_Pin = SPI2_CS_PIN | SPI2_SCK_PIN | SPI2_MISO_PIN | SPI2_MOSI_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
复制代码
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>