【STM32】串口中断接收函数

型号
        stm32f103
使用串口
        USART2
涉及中断
        USART_IT_RXNE:接收到1个字节产生该中断
        USART_IT_IDLE:接收到一帧数据产生该中断

串口配置函数

注意要打开对应的中断!

/* 使能串口2接收中断 */
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_ITConfig(USART2, USART_IT_IDLE, ENABLE);

/// 配置USART接收中断
static void NVIC_Configuration(void)
{
    NVIC_InitTypeDef NVIC_InitStructure;
    /* Configure the NVIC Preemption Priority Bits */
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);

    /* Enable the USARTy Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =1;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
}

/*
 * 函数名:USARTx_Config
 * 描述  :USART GPIO 配置,工作模式配置
 * 输入  :无
 * 输出  : 无
 * 调用  :外部调用
 */
void USART_Config(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;

	/* config USART2 clock */
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); 
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

	/* USART2 GPIO config */
	/* Configure USART2 Tx (PA.02) as alternate function push-pull */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	    
    /* Configure USART2 Rx (PA.03) as input floating */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
	  
	/* USART2 mode config */
	USART_InitStructure.USART_BaudRate = 9600;
	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_Configuration();
	/* 使能串口2接收中断 */
	USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
	USART_ITConfig(USART2, USART_IT_IDLE, ENABLE);

	NVIC_EnableIRQ( USART2_IRQn );//使能NVIC响应UART2的中断请求
	USART_Cmd(USART2, ENABLE);
	USART_ClearFlag(USART2, USART_FLAG_TC);
}

中断服务函数

uint8_t str[256] = {0};
uint8_t finish_tar = 0;

void USART2_IRQHandler(void)
{
	u8 dat;
	static u8 count = 0;
	
	if(USART_GetITStatus(USART2, USART_IT_RXNE) == SET)  //接收中断
	{
		USART_ClearFlag(USART2, USART_IT_RXNE);   //清除接收中断标志
		str[count] = USART2->DR;
		count++;
	}
	if(USART_GetITStatus(USART2, USART_IT_IDLE) == SET)  //空闲中断
	{
		dat = USART2->DR;	//清空闲标志
		str[count] = '\0';
		count = 0;
        finish_tar = 1;
    }
}

主函数

extern uint8_t str[256];
extern uint8_t finish_tar;

int main(void)
{
    // ......
    while(1)
    {
        if(finish_tar == 1)
        {
            printf("%s\n", str);
            memset(str, 0, 256);
            finish_tar = 0;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_48896613/article/details/124993414