main.c
UART_HandleTypeDef huart1;
uint8_t USART1_RX_BUF[200]; //接收缓冲,最大USART_REC_LEN个字节.
uint16_t USART1_RX_STA = 0; //接收状态标记
uint8_t USART1_Count = 0;
int main(void)
{
while (1)
{
if(USART1_RX_STA == 1)
{
HAL_UART_Transmit(&huart1, (uint8_t*)USART1_RX_BUF, USART1_Count, 1000); //发送接收到的数据
while(__HAL_UART_GET_FLAG(&huart1, UART_FLAG_TC) != SET); //等待发送结束
USART1_RX_STA = 0;
USART1_Count = 0;
memset(USART1_RX_BUF, 0, sizeof(USART1_RX_BUF));
}
}
}
static void MX_USART1_UART_Init(void)
{
huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
__HAL_UART_ENABLE_IT(&huart1, UART_IT_RXNE); //开启接收中断
HAL_NVIC_EnableIRQ(USART1_IRQn); //使能USART1中断通道
HAL_NVIC_SetPriority(USART1_IRQn, 3, 3); //抢占优先级3,子优先级3
__HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);
}
stm32f1xx_it.c
extern uint8_t USART5_RX_BUF[200];
extern uint16_t USART5_RX_STA;
extern uint8_t USART5_Count;
void USART1_IRQHandler(void)
{
/* USER CODE BEGIN USART1_IRQn 0 */
uint8_t clear = 0, Res;
if((__HAL_UART_GET_FLAG(&huart1, UART_FLAG_RXNE) != RESET))
{
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET);
HAL_UART_Receive(&huart1, &Res, 1, 1000);
USART1_RX_BUF[USART1_Count++] = Res;
}
else if((__HAL_UART_GET_FLAG(&huart1, UART_FLAG_IDLE) != RESET))
{
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_2, GPIO_PIN_RESET);
clear = huart1.Instance->SR;
clear = huart1.Instance->DR;
USART1_RX_STA = 1;
}
/* USER CODE END USART1_IRQn 0 */
HAL_UART_IRQHandler(&huart1);
/* USER CODE BEGIN USART1_IRQn 1 */
/* USER CODE END USART1_IRQn 1 */
}
注意!!!
stm32f103串口4收发数据要改变DIR引脚电平:高电平打开发送TX,低电平打开接收RX
若需要将printf()函数转向串口输出,可在main.c文件添加如下代码
#ifdef __GNUC__
/* With GCC, small printf (option LD Linker->Libraries->Small printf
set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
PUTCHAR_PROTOTYPE
{
HAL_UART_Transmit(&huart1 , (uint8_t *)&ch, 1, 0xFFFF);
return ch;
}
若要计算输出浮点型,则在project properties > C/C++ Build > Settings > Tool Settings (TAB) > MCU GCC Linker > Miscellaneous > other flag中添加 -u_printf_float
main.c
UART_HandleTypeDef huart1;
uint8_t USART1_RX_BUF[200]; //接收缓冲,最大USART_REC_LEN个字节.
uint16_t USART1_RX_STA = 0; //接收状态标记
uint8_t USART1_Count = 0;
int main(void)
{
while (1)
{
if(USART1_RX_STA == 1)
{
HAL_UART_Transmit(&huart1, (uint8_t*)USART1_RX_BUF, USART1_Count, 1000); //发送接收到的数据
while(__HAL_UART_GET_FLAG(&huart1, UART_FLAG_TC) != SET); //等待发送结束
USART1_RX_STA = 0;
USART1_Count = 0;
memset(USART1_RX_BUF, 0, sizeof(USART1_RX_BUF));
}
}
}
static void MX_USART1_UART_Init(void)
{
huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
__HAL_UART_ENABLE_IT(&huart1, UART_IT_RXNE); //开启接收中断
HAL_NVIC_EnableIRQ(USART1_IRQn); //使能USART1中断通道
HAL_NVIC_SetPriority(USART1_IRQn, 3, 3); //抢占优先级3,子优先级3
__HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);
}
stm32f1xx_it.c
extern uint8_t USART5_RX_BUF[200];
extern uint16_t USART5_RX_STA;
extern uint8_t USART5_Count;
void USART1_IRQHandler(void)
{
/* USER CODE BEGIN USART1_IRQn 0 */
uint8_t clear = 0, Res;
if((__HAL_UART_GET_FLAG(&huart1, UART_FLAG_RXNE) != RESET))
{
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET);
HAL_UART_Receive(&huart1, &Res, 1, 1000);
USART1_RX_BUF[USART1_Count++] = Res;
}
else if((__HAL_UART_GET_FLAG(&huart1, UART_FLAG_IDLE) != RESET))
{
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_2, GPIO_PIN_RESET);
clear = huart1.Instance->SR;
clear = huart1.Instance->DR;
USART1_RX_STA = 1;
}
/* USER CODE END USART1_IRQn 0 */
HAL_UART_IRQHandler(&huart1);
/* USER CODE BEGIN USART1_IRQn 1 */
/* USER CODE END USART1_IRQn 1 */
}
注意!!!
stm32f103串口4收发数据要改变DIR引脚电平:高电平打开发送TX,低电平打开接收RX
若需要将printf()函数转向串口输出,可在main.c文件添加如下代码
#ifdef __GNUC__
/* With GCC, small printf (option LD Linker->Libraries->Small printf
set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
PUTCHAR_PROTOTYPE
{
HAL_UART_Transmit(&huart1 , (uint8_t *)&ch, 1, 0xFFFF);
return ch;
}
若要计算输出浮点型,则在project properties > C/C++ Build > Settings > Tool Settings (TAB) > MCU GCC Linker > Miscellaneous > other flag中添加 -u_printf_float
举报