在做SPI的通讯项目,,,,最近老是出现这么个问题,程序老是卡在红色标记的地方:为什么会出现这种问题呢????大侠们有木有遇到
unsigned char SPI_SendByte(uint8_t Value)
{
while (__HAL_SPI_GET_FLAG(&SPIHandle, SPI_FLAG_TXE) == RESET); 程序老是莫名其妙的卡在这个地方
SPI_SendData(&SPIHandle,Value);
// Wait to receive a byte
while (__HAL_SPI_GET_FLAG(&SPIHandle, SPI_FLAG_RXNE) == RESET);
return SPI_ReceiveData(&SPIHandle);
}
SPI的配置如下
/*-----------------------------1. IO 配置------------------------------------*/
void SPI_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
//使能GPIOA的时钟;MOSI--PA12;MISO--PA11;SPI1SCK--PA5;SPI1NSS--PA4
__GPIOA_CLK_ENABLE();
//使能SPI的时钟
__SPI1_CLK_ENABLE();
// Configure SPI SCK
GPIO_InitStruct.Pin = GPIO_PIN_5; //SPI1_SCK
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
GPIO_InitStruct.Alternate = GPIO_AF0_SPI1; //SPI函数映射,即连接到SPI
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
// Configure SPI MISO and MOSI
GPIO_InitStruct.Pin = GPIO_PIN_12;//MOSI
GPIO_InitStruct.Alternate = GPIO_AF0_SPI1;//SPI函数映射,即连接到SPI1
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_11;//MISO
GPIO_InitStruct.Alternate = GPIO_AF0_SPI1;//SPI函数映射,即连接到SPI
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
// Configure SPI NSS
GPIO_InitStruct.Pin = GPIO_PIN_4;//SPI1_SCK
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;//推挽输出
GPIO_InitStruct.Pull = GPIO_PULLUP;//上拉
GPIO_InitStruct.Speed = GPIO_SPEED_FAST;//高速
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
/*-----------------------------2. 参数配置------------------------------------*/
void SPI_Init(void)
{
SPI_HandleTypeDef SPIHandle;
SPI_GPIO_Config();
/* SPI Config */
SPIHandle.Instance = SPI1;
SPIHandle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64; //
SPIHandle.Init.Direction = SPI_DIRECTION_2LINES; //SPI_Direction_mode
SPIHandle.Init.CLKPhase = SPI_PHASE_2EDGE; //CPHA
SPIHandle.Init.CLKPolarity = SPI_POLARITY_LOW; //CPOL时钟空闲状态极性,SET:空闲时为高电平
SPIHandle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED; //CRC校验 失能
SPIHandle.Init.CRCPolynomial = 7;
SPIHandle.Init.DataSize = SPI_DATASIZE_8BIT; //数据大小8个bit
SPIHandle.Init.FirstBit = SPI_FIRSTBIT_MSB;
SPIHandle.Init.NSS = SPI_NSS_SOFT; //软件管理模式,可以将NSS配置成普通GPIO作为MAX35101的CE端片选信号
SPIHandle.Init.TIMode = SPI_TIMODE_DISABLED; //
SPIHandle.Init.Mode = SPI_MODE_MASTER; //主机模式
HAL_SPI_Init(&SPIHandle);//初始化SPI
__HAL_SPI_ENABLE(&SPIHandle);//使能 SPI
SPI1_NSS_high();//片选信号拉高,低电平有效
}
时钟配置:
void SystemClock_Config(void)
{
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_OscInitTypeDef RCC_OscInitStruct;
// Enable Power Control clock
__PWR_CLK_ENABLE();
// The voltage scaling allows optimizing the power consumption when the device is
// clocked below the maximum system frequency, to update the voltage scaling value
// regarding system frequency refer to product datasheet.
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
// Enable HSI Oscillator and activate PLL with HSI as source
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;//选择HSI作为时钟源
RCC_OscInitStruct.HSEState = RCC_HSE_OFF;//外部时钟 关
RCC_OscInitStruct.HSIState = RCC_HSI_ON;//内部高速时钟打开
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;//PLL时钟开
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;//HSI作为PLL的时钟输入
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_4;//PLLCLK 16 x 4 =64MHZ,PLL先倍频
RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_2;//PLLCLK/2=32MHZ ,PLL分频
RCC_OscInitStruct.HSICalibrationValue = 0x10;//RC校准参数,采取官方提供值
HAL_RCC_OscConfig(&RCC_OscInitStruct);
// Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
// clocks dividers
RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;//PLL作为SYSCLK,即32MHZ
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;//不分频
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1);//闪存1个延时周期
}
|