21ic问答首页 - HC32F460 SPI通信问题
HC32F460 SPI通信问题
HelloOK1232021-12-05
大家好,在使用HC32F460 的SPI时,碰到这个问题:
1 / HC32F460 可以正常发送数据, 外部的设备也正常回复数据,但是HC32F460 确收不到数据(读取DR寄存器得到的是刚才发送的数据)
2/ 已经使用示波器观测波形,确认发送是正常的(外部设备也只有在收到数据后才回复) 。示波器上的波形也显示外部设备返回了一个数据。
以下是代码
//SPI的配置
void Spi_Config(void)
{
stc_spi_init_t stcSpiInit;
/* configuration structure initialization */
MEM_ZERO_STRUCT(stcSpiInit);
/* Configuration peripheral clock */
PWC_Fcg1PeriphClockCmd(SPI_UNIT_CLOCK, Enable);
/* Configuration SPI pin */
PORT_SetFunc(SPI_SCK_PORT, SPI_SCK_PIN, SPI_SCK_FUNC, Disable);
PORT_SetFunc(SPI_MOSI_PORT, SPI_MOSI_PIN, SPI_MOSI_FUNC, Disable);
PORT_SetFunc(SPI_MISO_PORT, SPI_MISO_PIN, SPI_MISO_FUNC, Disable);
// PORT_SetFunc(SPI_CS_PORT, SPI_CS_PIN, SPI_NSS_FUNC, Disable); //CS 单独控制
/* Configuration SPI structure */
stcSpiInit.enClkDiv = SpiClkDiv8; //SpiClkDiv32 SpiClkDiv64
stcSpiInit.enFrameNumber = SpiFrameNumber1;
stcSpiInit.enDataLength = SpiDataLengthBit8;//SpiDataLengthBit32 SpiDataLengthBit16 SpiDataLengthBit8
stcSpiInit.enFirstBitPosition = SpiFirstBitPositionMSB;
stcSpiInit.enSckPolarity = SpiSckIdleLevelLow;
stcSpiInit.enSckPhase = SpiSckOddChangeEvenSample; // SpiSckOddSampleEvenChange old--SpiSckOddChangeEvenSample
stcSpiInit.enReadBufferObject = SpiReadReceiverBuffer; //SpiReadSendBuffer old--SpiReadReceiverBuffer
stcSpiInit.enWorkMode = SpiWorkMode3Line;
stcSpiInit.enTransMode = SpiTransFullDuplex;
stcSpiInit.enCommAutoSuspendEn = Disable; //Disable
stcSpiInit.enModeFaultErrorDetectEn = Disable;
stcSpiInit.enParitySelfDetectEn = Disable;
stcSpiInit.enParityEn = Disable;
stcSpiInit.enParity = SpiParityEven;
stcSpiInit.enMasterSlaveMode = SpiModeMaster;
stcSpiInit.stcDelayConfig.enSsSetupDelayOption = SpiSsSetupDelayTypicalSck1;
stcSpiInit.stcDelayConfig.enSsSetupDelayTime = SpiSsSetupDelaySck1;
stcSpiInit.stcDelayConfig.enSsHoldDelayOption = SpiSsHoldDelayCustomValue;
stcSpiInit.stcDelayConfig.enSsHoldDelayTime = SpiSsHoldDelaySck1;
stcSpiInit.stcDelayConfig.enSsIntervalTimeOption = SpiSsIntervalCustomValue;
stcSpiInit.stcDelayConfig.enSsIntervalTime = SpiSsIntervalSck6PlusPck2;
SPI_Init(SPI_UNIT, &stcSpiInit);
SPI_Cmd(SPI_UNIT, Enable);
}
//SPI读取函数
UINT8 SPI_Read( void ) /* 从CH376读数据 */
{
UINT16 spi_u8Tmp=0;
SPI_CS_OnOff(1);
SPI_CS_OnOff(0); //使能CS端口
Ddl_Delay1us(100);
SPI_SendData8(SPI_UNIT, 0x55);
/* Wait rx buffer full */
while (Reset == SPI_GetFlag(SPI_UNIT, SpiFlagReceiveBufferFull))
{
}
spi_u8Tmp=SPI_ReceiveData16(SPI_UNIT); //此处得到的数据,是0x55,也就是跟发送的一样,外部设备返回的是0xAA(示波器上也是显示0xAA)
Ddl_Delay1us(10);
Ddl_Delay1us(1000);
SPI_CS_OnOff(1);
printf("spi_u8Tmp==%hx",spi_u8Tmp);
return (UINT8)spi_u8Tmp;
}
谢谢
1 / HC32F460 可以正常发送数据, 外部的设备也正常回复数据,但是HC32F460 确收不到数据(读取DR寄存器得到的是刚才发送的数据)
2/ 已经使用示波器观测波形,确认发送是正常的(外部设备也只有在收到数据后才回复) 。示波器上的波形也显示外部设备返回了一个数据。
以下是代码
//SPI的配置
void Spi_Config(void)
{
stc_spi_init_t stcSpiInit;
/* configuration structure initialization */
MEM_ZERO_STRUCT(stcSpiInit);
/* Configuration peripheral clock */
PWC_Fcg1PeriphClockCmd(SPI_UNIT_CLOCK, Enable);
/* Configuration SPI pin */
PORT_SetFunc(SPI_SCK_PORT, SPI_SCK_PIN, SPI_SCK_FUNC, Disable);
PORT_SetFunc(SPI_MOSI_PORT, SPI_MOSI_PIN, SPI_MOSI_FUNC, Disable);
PORT_SetFunc(SPI_MISO_PORT, SPI_MISO_PIN, SPI_MISO_FUNC, Disable);
// PORT_SetFunc(SPI_CS_PORT, SPI_CS_PIN, SPI_NSS_FUNC, Disable); //CS 单独控制
/* Configuration SPI structure */
stcSpiInit.enClkDiv = SpiClkDiv8; //SpiClkDiv32 SpiClkDiv64
stcSpiInit.enFrameNumber = SpiFrameNumber1;
stcSpiInit.enDataLength = SpiDataLengthBit8;//SpiDataLengthBit32 SpiDataLengthBit16 SpiDataLengthBit8
stcSpiInit.enFirstBitPosition = SpiFirstBitPositionMSB;
stcSpiInit.enSckPolarity = SpiSckIdleLevelLow;
stcSpiInit.enSckPhase = SpiSckOddChangeEvenSample; // SpiSckOddSampleEvenChange old--SpiSckOddChangeEvenSample
stcSpiInit.enReadBufferObject = SpiReadReceiverBuffer; //SpiReadSendBuffer old--SpiReadReceiverBuffer
stcSpiInit.enWorkMode = SpiWorkMode3Line;
stcSpiInit.enTransMode = SpiTransFullDuplex;
stcSpiInit.enCommAutoSuspendEn = Disable; //Disable
stcSpiInit.enModeFaultErrorDetectEn = Disable;
stcSpiInit.enParitySelfDetectEn = Disable;
stcSpiInit.enParityEn = Disable;
stcSpiInit.enParity = SpiParityEven;
stcSpiInit.enMasterSlaveMode = SpiModeMaster;
stcSpiInit.stcDelayConfig.enSsSetupDelayOption = SpiSsSetupDelayTypicalSck1;
stcSpiInit.stcDelayConfig.enSsSetupDelayTime = SpiSsSetupDelaySck1;
stcSpiInit.stcDelayConfig.enSsHoldDelayOption = SpiSsHoldDelayCustomValue;
stcSpiInit.stcDelayConfig.enSsHoldDelayTime = SpiSsHoldDelaySck1;
stcSpiInit.stcDelayConfig.enSsIntervalTimeOption = SpiSsIntervalCustomValue;
stcSpiInit.stcDelayConfig.enSsIntervalTime = SpiSsIntervalSck6PlusPck2;
SPI_Init(SPI_UNIT, &stcSpiInit);
SPI_Cmd(SPI_UNIT, Enable);
}
//SPI读取函数
UINT8 SPI_Read( void ) /* 从CH376读数据 */
{
UINT16 spi_u8Tmp=0;
SPI_CS_OnOff(1);
SPI_CS_OnOff(0); //使能CS端口
Ddl_Delay1us(100);
SPI_SendData8(SPI_UNIT, 0x55);
/* Wait rx buffer full */
while (Reset == SPI_GetFlag(SPI_UNIT, SpiFlagReceiveBufferFull))
{
}
spi_u8Tmp=SPI_ReceiveData16(SPI_UNIT); //此处得到的数据,是0x55,也就是跟发送的一样,外部设备返回的是0xAA(示波器上也是显示0xAA)
Ddl_Delay1us(10);
Ddl_Delay1us(1000);
SPI_CS_OnOff(1);
printf("spi_u8Tmp==%hx",spi_u8Tmp);
return (UINT8)spi_u8Tmp;
}
谢谢
赞0
评论
2021-12-06
赞0
您好,我现在发送后,加了判断和延迟,现象还是一样,以下是代码,谢谢
uint8_t spi_ReadWrite(uint8_t wData)
{
UINT8 spi_u8Tmp=0;
// SPI_CS_OnOff(1);
SPI_CS_OnOff(0);
// Ddl_Delay1us(100);
while (Reset == SPI_GetFlag(SPI_UNIT, SpiFlagSendBufferEmpty))
{
}
SPI_SendData8(SPI_UNIT, wData);
while (Reset == SPI_GetFlag(SPI_UNIT, SpiFlagSendBufferEmpty))
{
}
Ddl_Delay1ms(10);
Ddl_Delay1us(200);
/* Wait rx buffer full */
while (Reset == SPI_GetFlag(SPI_UNIT, SpiFlagReceiveBufferFull))
{
}
Ddl_Delay1ms(100);
spi_u8Tmp=SPI_ReceiveData8(SPI_UNIT);
Ddl_Delay1us(1000);
SPI_CS_OnOff(1);
// printf("spi_u8Tmp==%hx \n",spi_u8Tmp);
spiRc=spi_u8Tmp;
return (UINT8)spi_u8Tmp;
}
评论
2021-12-06
赞0
评论
2021-12-06
赞0
整容二极管
晶振的噩梦
谁杀死了LED
保险丝的葬礼
妄自尊大的发光二极管
————
电子的奇妙世界,视频公开课
https://open.21ic.com/open/lesson/5766
评论
2021-12-05
赞0
评论
2021-12-05
您需要登录后才可以回复 登录 | 注册