STM32F103RC USART2串口中断方式接收数据

现有一个小需求,使用STM32F1系列单片机做串口2的收发数据的功能,通过PC上的串口调试助手给单片机发一串数据,单片机收到数据后再给PC的串口调试助手发回去。

先前有一篇是通过串口查询方式实现的,本次使用串口中断方式实现。

STM32使用USART2,对应单片机的PA1控制方向,PA2发送,PA3接收。

代码如下:

main.c

#include "stm32f10x.h"


u8 chs[128];
u8 flag_recv;
u8 len_recv;
u8 *p_ch;

void init_hardware_usart2_(u32 bound)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
	
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	USART_InitStructure.USART_BaudRate = bound;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_Parity = USART_Parity_No ;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
	USART_Init(USART2, &USART_InitStructure);
	
	NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	
	USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
	USART_ITConfig(USART2, USART_IT_IDLE, ENABLE);
	USART_Cmd(USART2, ENABLE);
	USART_ClearFlag(USART2, USART_FLAG_TC);
	
	GPIO_ResetBits(GPIOA, GPIO_Pin_1);
}

void func_usart2_send_a_byte_(u8 abyte)
{
	while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
	USART_SendData(USART2, abyte);
	while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
}

void func_usart2_send_bytes_(u8 *bytes, u8 bytes_len)
{
	u8 i;
	GPIO_SetBits(GPIOA, GPIO_Pin_1);
	for(i = 0; i < bytes_len; i++)
	{
		func_usart2_send_a_byte_(bytes[i]);
	}
	GPIO_ResetBits(GPIOA, GPIO_Pin_1);
}

int main()
{
	//u8 recv_ch;
	p_ch = chs;
	len_recv = 0;
	init_hardware_usart2_(9600);
	
	while(1)
	{
		
	}
}

void USART2_IRQHandler(void)
{
  u8 rx_dat;
  
  if(USART_GetITStatus(USART2,USART_IT_RXNE) == SET)
  {
		flag_recv = 1;
    rx_dat = USART_ReceiveData(USART2);
		if(p_ch < &chs[127])
		{
			*p_ch ++ = rx_dat;
			len_recv ++;
		}
		
  }
	if(USART_GetITStatus(USART2, USART_IT_IDLE) != RESET)
	{
		if(flag_recv == 1)
		{
			func_usart2_send_bytes_(chs, len_recv);
			len_recv = 0;
			p_ch = chs;
		}
		flag_recv = 0;
	}
}

效果如下:

STM32F103RC USART2串口中断方式接收数据_第1张图片

测试1万多次,STM32都收到了,并成功转发了。

测试代码,仅供参考。

你可能感兴趣的:(串口,C,STM32,语言编程,中断)