初级会员

- 积分
- 132
- 金钱
- 132
- 注册时间
- 2018-4-3
- 在线时间
- 14 小时
|
3金钱
问题:需要用SPI通信方式读取AD7606测的数据,AD7606是16位,所以SPI工作方式设置成为16位传输。但是,SPI的发送函数HAL_StatusTypeDef HAL_SPI_Transmit(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout)是8bit传输,怎么办呢?
同时,SPI的接受函数HAL_StatusTypeDef HAL_SPI_Receive(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout)也是8bit传输。
应该编写函数呢?忘大神们指点。
下面是我写的代码,但不知道对不对,目前AD还没有调出来。
u16 AD7606_ReadBytes(void)
{
u16 data = 0;
/* Wait until the transmit buffer is empty*/
while (__HAL_SPI_GET_FLAG(&SPI5_Handler,SPI_FLAG_TXE) == RESET)
{
}
/* Send the byte */
u16 TxData[]={0xffff};
HAL_SPI_Transmit(&SPI5_Handler,(u8 *)TxData,sizeof(TxData),1000);
/* Wait until a data is received */
while (__HAL_SPI_GET_FLAG(&SPI5_Handler,SPI_FLAG_RXNE) == RESET)
{
}
/* Get the received data */
u8 RxData[2];
HAL_SPI_Receive(&SPI5_Handler,RxData,2,1000);
data=RxData[0];
data <<= 8;
data +=RxData[1];
/* Return the shifted data */
return data;
}
求教,感谢。
|
最佳答案
查看完整内容[请看2#楼]
SPI配置为8位,16位数据可分为两次8位传输,没问题,SPI直接配置为16位模式,一次性写16位数据到DR就行了.
|