各位大佬帮忙看看是啥问题。
原本SPI寄存器中的数据长度设置的16位,读取从机寄存器8位数据没有问题。因为要读取从机寄存器的24位数据,故将数据长度设置为8位,但读取的8位数据都变成了0或者FF,示波器抓取了MISO上的波形与应该读取到的数值一样,但从SPI数据寄存器中读的值就不对。
原本数据长度设置为16位时的发送函数:
/*********************************************
函数: void SPI_WriteByte(uint16_t data)
描述: SPI写数据.
参数: none
返回: none
版本: V1.0, 2025-1-8
**********************************************/
void SPI_WriteByte(uint16_t data)
{
/* Wait for SPI1 Tx buffer empty */
while (SPI_I2S_GetStatus(SPI1, SPI_I2S_TE_FLAG) == RESET)
;
/* Check the parameters */
assert_param(IS_SPI_PERIPH(SPI1));
/* Write in the DAT register the data to be sent */
SPI1->DAT = data;
/* 等待发送完成 */
while (SPI_I2S_GetStatus(SPI1, SPI_I2S_BUSY_FLAG) == SET)
;
}
设置为8位的发送函数:
/*********************************************
函数: void SPI_WriteByte(uint16_t data)
描述: SPI写数据.
参数: none
返回: none
版本: V1.0, 2025-1-8
**********************************************/
void SPI_WriteByte(uint16_t data)
{
uint8_t byte1,byte2;
byte1 = (data >> 8) & 0xFF;
byte2 = data & 0xFF;
/* Wait for SPI1 Tx buffer empty */
while (SPI_I2S_GetStatus(SPI1, SPI_I2S_TE_FLAG) == RESET)
;
/* Check the parameters */
assert_param(IS_SPI_PERIPH(SPI1));
/* Write in the DAT register the data to be sent */
SPI1->DAT = byte1;
while (SPI_I2S_GetStatus(SPI1, SPI_I2S_TE_FLAG) == RESET)
;
/* Check the parameters */
assert_param(IS_SPI_PERIPH(SPI1));
SPI1->DAT = byte2;
/* 等待发送完成 */
while (SPI_I2S_GetStatus(SPI1, SPI_I2S_BUSY_FLAG) == SET)
;
}
SPI读函数用的是官方例程中的,数据8位或16位都用的这个:
/*********************************************
函数: uint8_t SPI_ReadByte(uint8_t addr)
描述: SPI读寄存器数据.
参数: none
返回: none
版本: V1.0, 2025-1-8
**********************************************/
uint8_t SPI_ReadByte(void)
{
while (SPI_I2S_GetStatus(SPI1, SPI_I2S_RNE_FLAG) == RESET)
;
/* Check the parameters */
assert_param(IS_SPI_PERIPH(SPI1));
/* Return the data in the DAT register */
return SPI1->DAT;
}
从机数据读取函数:
/*********************************************
函数: uint8_t TDC7200_ReadByte(uint8_t addr)
描述: TDC7200读寄存器.
参数: none
返回: none
版本: V1.0, 2025-1-8
**********************************************/
uint8_t TDC7200_ReadByte(uint8_t addr)
{
uint8_t Rd_Data;
uint16_t addr_data;
SPI_CS_L_TDC7200();
addr_data = ((0x80 | addr) << 8) | Data_Buf;
SPI_WriteByte(addr_data);
Rd_Data = SPI_ReadByte();
SPI_CS_H_TDC7200();
return Rd_Data;
} |