wenyangzeng 发表于 2017-1-21 09:50:55

本帖最后由 wenyangzeng 于 2017-1-21 09:53 编辑

本贴可以供你作参考:
https://www.stmcu.org.cn/module/forum/thread-566825-1-1.html
发送方式:
SPI_SendData8(SPI1,data);

接收你可以类推。

Paderboy 发表于 2017-1-21 10:50:13

:loveliness::loveliness:签到

中山无雪 发表于 2017-1-21 10:58:07

zoomdy 发表于 2017-1-21 09:39
16位数的高低字节次序有没有反了?

确实应该注意一下顺序问题

当前显示的数据只有8bit,还缺一半呢,因此显示的结果肯定不正确

zbber 发表于 2017-1-21 14:27:53

学习学习,谢谢楼主,支持分享

cldym 发表于 2017-1-21 15:18:14

学习学习

peter001 发表于 2017-1-21 22:27:04

学习学习

木小犀 发表于 2017-2-9 21:37:07

无薪税绵 发表于 2017-1-21 08:23
HAL_StatusTypeDef HAL_SPI_Receive(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t T ...

HAL_StatusTypeDef HAL_SPI_Receive(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout)
{
__IO uint16_t tmpreg;
uint32_t tmp = 0;

if(hspi->State == HAL_SPI_STATE_READY)
{
    if((pData == NULL ) || (Size == 0))
    {
      returnHAL_ERROR;
    }

    /* Process Locked */
    __HAL_LOCK(hspi);

    /* Configure communication */
    hspi->State       = HAL_SPI_STATE_BUSY_RX;
    hspi->ErrorCode   = HAL_SPI_ERROR_NONE;

    hspi->pRxBuffPtr= pData;
    hspi->RxXferSize= Size;
    hspi->RxXferCount = Size;

    /*Init field not used in handle to zero */
    hspi->RxISR = 0;
    hspi->TxISR = 0;
    hspi->TxXferSize   = 0;
    hspi->TxXferCount= 0;

    /* Configure communication direction : 1Line */
    if(hspi->Init.Direction == SPI_DIRECTION_1LINE)
    {
      SPI_1LINE_RX(hspi);
    }

    /* Reset CRC Calculation */
    if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
    {
      SPI_RESET_CRC(hspi);
    }
   
    if((hspi->Init.Mode == SPI_MODE_MASTER) && (hspi->Init.Direction == SPI_DIRECTION_2LINES))
    {
      /* Process Unlocked */
      __HAL_UNLOCK(hspi);

      /* Call transmit-receive function to send Dummy data on Tx line and generate clock on CLK line */
      return HAL_SPI_TransmitReceive(hspi, pData, pData, Size, Timeout);
    }

    /* Check if the SPI is already enabled */
    if((hspi->Instance->CR1 &SPI_CR1_SPE) != SPI_CR1_SPE)
    {
      /* Enable SPI peripheral */
      __HAL_SPI_ENABLE(hspi);
    }

    /* Receive data in 8 Bit mode */
    if(hspi->Init.DataSize == SPI_DATASIZE_8BIT)
    {
      while(hspi->RxXferCount > 1)
      {
      /* Wait until RXNE flag is set */
      if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_RXNE, RESET, Timeout) != HAL_OK)
      {
          return HAL_TIMEOUT;
      }

      (*hspi->pRxBuffPtr++) = hspi->Instance->DR;
      hspi->RxXferCount--;
      }
      /* Enable CRC Transmission */
      if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
      {
      hspi->Instance->CR1 |= SPI_CR1_CRCNEXT;
      }
    }
    /* Receive data in 16 Bit mode */
    else
    {
      while(hspi->RxXferCount > 1)
      {
      /* Wait until RXNE flag is set to read data */
      if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_RXNE, RESET, Timeout) != HAL_OK)
      {
          return HAL_TIMEOUT;
      }

      *((uint16_t*)hspi->pRxBuffPtr) = hspi->Instance->DR;
      hspi->pRxBuffPtr+=2;
      hspi->RxXferCount--;
      }
      /* Enable CRC Transmission */
      if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
      {
      hspi->Instance->CR1 |= SPI_CR1_CRCNEXT;
      }
    }

    /* Wait until RXNE flag is set */
    if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_RXNE, RESET, Timeout) != HAL_OK)
    {
      return HAL_TIMEOUT;
    }

    /* Receive last data in 8 Bit mode */
    if(hspi->Init.DataSize == SPI_DATASIZE_8BIT)
    {
      (*hspi->pRxBuffPtr++) = hspi->Instance->DR;
    }
    /* Receive last data in 16 Bit mode */
    else
    {
      *((uint16_t*)hspi->pRxBuffPtr) = hspi->Instance->DR;
      hspi->pRxBuffPtr+=2;
    }
    hspi->RxXferCount--;

    /* Wait until RXNE flag is set: CRC Received */
    if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
    {
      if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_RXNE, RESET, Timeout) != HAL_OK)
      {
      hspi->ErrorCode |= HAL_SPI_ERROR_CRC;
      return HAL_TIMEOUT;
      }

      /* Read CRC to Flush RXNE flag */
      tmpreg = hspi->Instance->DR;
      UNUSED(tmpreg);
    }
   
    if((hspi->Init.Mode == SPI_MODE_MASTER)&&((hspi->Init.Direction == SPI_DIRECTION_1LINE)||(hspi->Init.Direction == SPI_DIRECTION_2LINES_RXONLY)))
    {
      /* Disable SPI peripheral */
      __HAL_SPI_DISABLE(hspi);
    }

    hspi->State = HAL_SPI_STATE_READY;

    tmp = __HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR);
    /* Check if CRC error occurred */
    if((hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) && (tmp != RESET))
    {
      hspi->ErrorCode |= HAL_SPI_ERROR_CRC;

      /* Reset CRC Calculation */
      SPI_RESET_CRC(hspi);

      /* Process Unlocked */
      __HAL_UNLOCK(hspi);

      return HAL_ERROR;
    }

    /* Process Unlocked */
    __HAL_UNLOCK(hspi);

    return HAL_OK;
}
else
{
    return HAL_BUSY;
}
}
其中有针对16Bit的传输

木小犀 发表于 2017-2-28 14:33:36

无薪税绵 发表于 2017-1-21 08:23
HAL_StatusTypeDef HAL_SPI_Receive(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t T ...

已经改好了,现在有一个问题,就是数据偶尔会出现问题,温度是实际的2倍,是不是最高位丢失,后面数据都向前进了一位,还有为什么是偶尔出错,大部分的结果都是正确的。

wbaojang 发表于 2020-4-4 09:30:26

:handshake
页: 1 [2]
查看完整版本: STM32F4+MAX6675+K型热电偶测温度