代码如下:
..............
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
MX_USART2_UART_Init();
/* USER CODE BEGIN 2 */
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1|GPIO_PIN_0, GPIO_PIN_RESET);
HAL_Delay(1000);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1|GPIO_PIN_0, GPIO_PIN_SET);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
while(1)
{
huart1ToHuart2_up();
huart2ToHuart1_up();
}
}
............
void huart1ToHuart2_up(void)
{
unsigned char temp;
if(RESET != __HAL_UART_GET_FLAG(&huart1,UART_FLAG_ORE))
{
HAL_UART_Receive(&huart1,&temp,1,0x0);
__HAL_UART_CLEAR_FLAG(&huart1,UART_FLAG_ORE);
}
if(RESET != __HAL_UART_GET_FLAG(&huart1,UART_FLAG_RXNE))
{
HAL_UART_Receive(&huart1,&temp,1,0x0);
HAL_UART_Transmit(&huart2,&temp,1,0x0);
__HAL_UART_CLEAR_FLAG(&huart2,UART_FLAG_TC);
}
}
void huart2ToHuart1_up(void)
{
unsigned char temp;
if(RESET != __HAL_UART_GET_FLAG(&huart2,UART_FLAG_ORE))
{
HAL_UART_Receive(&huart2,&temp,1,0x0);
__HAL_UART_CLEAR_FLAG(&huart2,UART_FLAG_ORE);
}
if(RESET != __HAL_UART_GET_FLAG(&huart2,UART_FLAG_RXNE))
{
HAL_UART_Receive(&huart2,&temp,1,0);
HAL_UART_Transmit(&huart1,&temp,1,0x0);
__HAL_UART_CLEAR_FLAG(&huart1,UART_FLAG_TC);
}
}
|