在线时间2 小时
UID348530
ST金币0
蝴蝶豆0
注册时间2012-3-20
新手上路
- 最后登录
- 2019-5-21
|
a0a.1 0b0c
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);
} |
-
|