大家好,在使用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;
}
谢谢
|