Void SPI3_Init (void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_AHB1PeriphClockCmd (RCC_AHB1Periph_GPIOC, ENABLE);
RCC_APB1PeriphClockCmd (RCC_APB1Periph_SPI3, ENABLE);
GPIO_InitStructure. GPIO_Pin=GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12;
GPIO_InitStructure. GPIO_Mode=GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
GPIO_Init (GPIOC, & amp; GPIO_InitStructure);
SPI_InitStructure. SPI_Direction=SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure. SPI_Mode=SPI_Mode_Master;
SPI_InitStructure. SPI_DataSize=SPI_DataSize_8b;
SPI_InitStructure. SPI_CPOL=SPI_CPOL_High;
SPI_InitStructure. SPI_CPHA=SPI_CPHA_2Edge;
SPI_InitStructure. SPI_NSS=SPI_NSS_Soft;
SPI_InitStructure. SPI_BaudRatePrescaler=SPI_BaudRatePrescaler_8;
//SPI_InitStructure. SPI_BaudRatePrescaler=SPI_BaudRatePrescaler_32;
SPI_InitStructure. SPI_FirstBit=SPI_FirstBit_MSB;
SPI_InitStructure. SPI_CRCPolynomial=0;
SPI_Init (SPI3, & amp; SPI_InitStructure);
SPI_Cmd (SPI3, ENABLE);
SPI3_ReadWriteByte (0 XFF);
}
Read and write a single byte is as follows:
U8 SPI1_ReadWriteByte (u8 writeData)
{
U8 waitnum=0;
While (SPI_I2S_GetFlagStatus (SPI3, SPI_I2S_FLAG_TXE)==RESET) {}
{
Waitnum++;
If (waitnum> 200) return 0;
}
SPI_I2S_SendData (SPI3, writeData);
While (SPI_I2S_GetFlagStatus (SPI3, SPI_I2S_FLAG_RXNE)==RESET) {}
{
Waitnum++;
If (waitnum> 200) return 0;
}
Return SPI_I2S_ReceiveData (SPI3);
}
CodePudding user response:
TMC5160 adjustable links?