1.官网下载例程
https://www.xhsc.com.cn/Productlist/info.aspx?itemid=1751
2.找到spi例程 开始暴改
改动1.
en_result_t Spi_SendData(uint8_t u8Data)
{
uint32_t u32TimeOut;
u32TimeOut = 1000;
M0P_SPI->DATA = u8Data;
while(u32TimeOut--)
{
if(TRUE == Spi_GetStatus(SpiIf))
{
break;
}
}
if(u32TimeOut == 0)
{
return ErrorTimeout;
}
u8Data = M0P_SPI->DATA;
return Ok;
}
改动为
en_result_t Spi_SendData(uint8_t u8Data)
{
uint32_t u32TimeOut;
u32TimeOut = 1000;
M0P_SPI->DATA = u8Data;
while(--u32TimeOut)
{
if(TRUE == Spi_GetStatus(SpiIf))
{
break;
}
}
if(u32TimeOut == 0)
{
return ErrorTimeout;
}
u8Data = M0P_SPI->DATA;
return Ok;
}
原来的(--u32TimeOut执行后会为0XFFFFFFF,后续条件判断不执行。
3,SPI配置
Gpio_SetFunc_SPI_CS_P02();
Gpio_SetFunc_SPIMISO_P35();
Gpio_SetFunc_SPIMOSI_P36();
Gpio_SetFunc_SPI_SCK_P01();//配置功能脚
stc_spi_config_t SPIConfig;
Spi_SetCS(TRUE);
//配置SPI
SPIConfig.bCPHA = Spicphafirst;
SPIConfig.bCPOL = Spicpollow;
SPIConfig.bIrqEn = FALSE;
SPIConfig.bMasterMode = SpiMaster;
SPIConfig.u8BaudRate = SpiClkDiv2; //时钟
SPIConfig.pfnIrqCb = NULL;
Spi_Init(&SPIConfig); //初始化