void SPI1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI1_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
//PB13:SCK PB14:MISO PB15:MOSI
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
//PB10:CE PB12:CS
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_12;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//GPIO重映射
GPIO_PinAFConfig(GPIOB,GPIO_PinSource13 |GPIO_PinSource14 | GPIO_PinSource14,GPIO_AF_0);
SPI1_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
SPI1_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI1_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI1_InitStructure.SPI_CRCPolynomial = 7;
SPI1_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI1_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI1_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI1_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI1_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_Init(SPI1,&SPI1_InitStructure);
SPI_RxFIFOThresholdConfig(SPI1,SPI_RxFIFOThreshold_QF);
SPI_Cmd(SPI1,ENABLE);
// SPI1_ReadWriteByte(0xFF);
}
uint8_t SPI1_ReadWriteByte(uint8_t TxData)
{
uint8_t retry = 0;
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET)
{
retry++;
if(retry > 200)
{
return 0;
}
}
SPI_SendData8(SPI1, TxData);
retry = 0;
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET)
{
retry++;
if(retry > 200)
{
return 0;
}
}
return SPI_ReceiveData8(SPI1);
}
上一篇:STM32F030C6外部中断问题
下一篇:STM32F030 systick滴答定时器
推荐阅读最新更新时间:2024-03-16 16:24