用cubemx生成的SPI代码,配置如下:
hspi1.Instance = SPI1;
hspi1.Init.Mode = SPI_MODE_MASTER;
hspi1.Init.Direction = SPI_DIRECTION_2LINES;
hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;
hspi1.Init.NSS = SPI_NSS_SOFT;
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi1.Init.TIMode = SPI_TIMODE_DISABLED;
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_ENABLED;
hspi1.Init.CRCPolynomial = 10;
HAL_SPI_Init(&hspi1);
读和写也直接用的库函数,如下:
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_2,GPIO_PIN_RESET);
// flag=HAL_SPI_Transmit(&hspi1,send,3,1000);
// HAL_SPI_Receive(&hspi1,pdata,3,1000);
flag=HAL_SPI_TransmitReceive(&hspi1,send,pdata,6,10000);
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_2,GPIO_PIN_SET);
片选拉低拉高没有问题,用的点灯来测试发送、接收是否成功,发送的时候返回的是HAL_OK,接收错误,这个程序是我从标准库移植过来的,不知道是什么情况,希望大神给点指导啊、、 |