下面我的一些代码 SPI初始化 void MX_SPI1_Init(void) {
hspi1.Instance = SPI1; hspi1.Init.Mode = SPI_MODE_MASTER; hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32; hspi1.Init.Direction = SPI_DIRECTION_2LINES; hspi1.Init.CLKPhase = SPI_PHASE_2EDGE; hspi1.Init.CLKPolarity = SPI_POLARITY_LOW; hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED; hspi1.Init.CRCPolynomial = 7; hspi1.Init.DataSize = SPI_DATASIZE_8BIT; hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB; hspi1.Init.NSS = SPI_NSS_SOFT; hspi1.Init.TIMode = SPI_TIMODE_DISABLED HAL_SPI_MspInit(&hspi1); __HAL_SPI_ENABLE(&hspi1); if(HAL_SPI_Init(&hspi1) != HAL_OK) { /* Initialization Error */ } /* SPI */
} IO口初始化
void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi) {
GPIO_InitTypeDef GPIO_InitStruct = {0}; if(hspi->Instance==SPI1) { /* USER CODE BEGIN SPI1_MspInit 0 */
/* USER CODE END SPI1_MspInit 0 */ /* Peripheral clock enable */ __HAL_RCC_SPI1_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE(); /**SPI1 GPIO Configuration PA5 ------> SPI1_SCK PA6 ------> SPI1_MISO PA7 ------> SPI1_MOSI */ GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; GPIO_InitStruct.Alternate = GPIO_AF0_SPI1; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); CE_OFF; HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7,GPIO_PIN_SET); /* USER CODE BEGIN SPI1_MspInit 1 */
/* USER CODE END SPI1_MspInit 1 */ }
}
|