STM32 HAL库怎么使用SPI的发送和接收函数?

2019-03-23 17:42发布

请讲讲怎么使用HAL_SPI_Transmit()​;HAL_SPI_Receive();HAL_SPI_TransmitReceive()​;发送和接收一个Byte吗?​
用的CUBEMX生成的SPI3的配置void MX_SPI3_Init(void)
{

  hspi3.Instance = SPI3;
  hspi3.Init.Mode = SPI_MODE_MASTER;
  hspi3.Init.Direction = SPI_DIRECTION_2LINES;
  hspi3.Init.DataSize = SPI_DATASIZE_8BIT;
  hspi3.Init.CLKPolarity = SPI_POLARITY_LOW;
  hspi3.Init.CLKPhase = SPI_PHASE_1EDGE;
  hspi3.Init.NSS = SPI_NSS_SOFT;
  hspi3.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
  hspi3.Init.FirstBit = SPI_FIRSTBIT_MSB;
  hspi3.Init.TIMode = SPI_TIMODE_DISABLE;
  hspi3.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  hspi3.Init.CRCPolynomial = 7;
  hspi3.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
  hspi3.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
  if (HAL_SPI_Init(&hspi3) != HAL_OK)
  {
    Error_Handler();
  }

}

我下面这样写对吗?
void SPI_WriteByte(uint8_t TxData){         
    HAL_SPI_Transmit(&hspi3,&TxData,1,0xffff);
}uint8_t SPI_ReadByte(uint8_t RxData)
{         
    HAL_SPI_Receive(&hspi3,RxData, 1, 0xffff);
}

此帖出自小平头技术问答
0条回答

一周热门 更多>