我在用746 Nucleo板子作串口实验,用下面这个工程:
D:work
STM32CubeF7STM32Cube_FW_F7_V1.7.0ProjectsSTM32F746ZG-NucleoExamplesUARTUART_PrintfEWARM
uart初始化代码改成 ,我用UART3:
void HAL_UART_MspInit(UART_HandleTypeDef *huart)
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_PeriphCLKInitTypeDef RCC_PeriphClkInit;
/*##-1- Enable peripherals and GPIO Clocks #################################*/
/* Enable GPIO TX/RX clock */
USARTx_TX_GPIO_CLK_ENABLE();
//USARTx_RX_GPIO_CLK_ENABLE();
/* Select SysClk as source of USART1 clocks */
RCC_PeriphClkInit.PeriphClockSelec
tion = RCC_PERIPHCLK_USART1;
RCC_PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_SYSCLK;
HAL_RCCEx_PeriphCLKConfig( RCC_PeriphClkInit);
/* Enable USARTx clock */
USARTx_CLK_ENABLE();
/*##-2- Configure peripheral GPIO ##########################################*/
/* UART TX GPIO pin configuration */
GPIO_InitStruct.Pin = USARTx_TX_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = USARTx_TX_AF;
HAL_GPIO_Init(USARTx_TX_GPIO_PORT, GPIO_InitStruct);
/* UART RX GPIO pin configuration */
GPIO_InitStruct.Pin = USARTx_RX_PIN;
GPIO_InitStruct.Alternate = USARTx_RX_AF;
HAL_GPIO_Init(USARTx_RX_GPIO_PORT, GPIO_InitStruct);
HAL_NVIC_SetPriority(USARTx_IRQn,0,1);
HAL_NVIC_EnableIRQ(USARTx_IRQn);
}
在main文件中加了回调函数:
/**
* @brief Rx Transfer completed callbacks
* @param huart: uart handle
* @retval None
*/
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
UartReady = SET;
}
main() 函数里增加了 接收判断函数;
UartHandle.Instance = USARTx;
UartHandle.Init.BaudRate = 9600;
UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
UartHandle.Init.StopBits = UART_STOPBITS_1;
UartHandle.Init.Parity = UART_PARITY_ODD;
UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
UartHandle.Init.Mode = UART_MODE_TX_RX;
UartHandle.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init( UartHandle) != HAL_OK)
{
/* Initialization Error */
Error_Handler();
}
printf("nr UART Printf Example: retarget the C library printf function to the UARTnr");
printf("** Test finished successfully. ** nr");
UartReady = RESET;
if( HAL_UART_Receive_IT( UartHandle , aRxBuffer, 30) != HAL_OK)
{
Error_Handler();
}
UartReady = RESET;
while (UartReady != SET)
{
BSP_LED_On(LED2);
HAL_Delay(100);
BSP_LED_Off(LED2);
HAL_Delay(100);
BSP_LED_On(LED2);
HAL_Delay(100);
BSP_LED_Off(LED2);
HAL_Delay(500);
}
UartReady = RESET;
BSP_LED_Off(LED2);
启动程序后, 断点放在最后两行, 无论用超级终端怎么发数据, 都无法跳出上面的while 循环。
如果谁有 746Nucleo板子的中断方式发送和接收的工程, 共享出来学习一下。 谢谢!