stm32 freertos 之串口中断

一、中断处理函数

void USART1_IRQHandler(void)
{
	BaseType_t xHigherPriorityTaskWoken;
   xHigherPriorityTaskWoken = pdFALSE;
	u8 cChar;
	if(USART_GetITStatus (USART1,USART_IT_RXNE)!=RESET)
	{
		cChar=USART_ReceiveData(USART1);
		xQueueSendToBackFromISR (xQueueRx,&cChar,&xHigherPriorityTaskWoken);
		portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
	}
}

二、中断向量表配置

void NVIC_Configuration(void)
{
	NVIC_InitTypeDef  NVIC_InitStructure;
	NVIC_PriorityGroupConfig (NVIC_PriorityGroup_4);
	
	NVIC_InitStructure .NVIC_IRQChannel =USART1_IRQn  ;
	NVIC_InitStructure .NVIC_IRQChannelPreemptionPriority =6;
	NVIC_InitStructure .NVIC_IRQChannelCmd =ENABLE ;
	NVIC_Init(&NVIC_InitStructure );

}

三、串口接收任务创建

xQueueHandle xQueueRx;
void vPC_Communication_Task(void *pvParameters)
{
		BaseType_t xHigherPriorityTaskWoken;
		xHigherPriorityTaskWoken = pdFALSE;
		u8 rx=0;
		xQueueRx=xQueueCreate(20,sizeof(u8));
	
		while(1)
		{
			if(xQueueReceiveFromISR(xQueueRx,&rx,&xHigherPriorityTaskWoken))
			{
					printf("%c\r\n",rx);
			}
			vTaskDelay(1000/portTICK_RATE_MS );
		}

}

四、主函数

int main(void)
{
    BSP_INIT();	

    xTaskCreate(vLED_Task ,"led_task",50,NULL,1,NULL);
    xTaskCreate(vPC_Communication_Task,"vPC_Communication_Task",500,NULL,2,NULL);				
    vTaskStartScheduler();		
}


你可能感兴趣的:(FreeRTOS)