STM32 F4串口接收中断

2020-08-13 15:05:27 浏览数 (2)

.c文件中:

代码语言:javascript复制
void NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	
  NVIC_InitStructure.NVIC_IRQChannel = DEBUG_USART_IRQ;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
	
	NVIC_InitStructure.NVIC_IRQChannel = DEBUG_USART2_IRQ;  /* 配置USART为中断源 */  
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;     /* 抢断优先级为1 */  
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;            /* 子优先级为1 */  
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;               /* 使能中断 */  
  NVIC_Init(&NVIC_InitStructure);                               /* 初始化配置NVIC */

}
代码语言:javascript复制
void Debug_USART_Config(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  USART_InitTypeDef USART_InitStructure;
		
  RCC_AHB1PeriphClockCmd(DEBUG_USART_RX_GPIO_CLK|DEBUG_USART_TX_GPIO_CLK,ENABLE);
  RCC_APB2PeriphClockCmd(DEBUG_USART_CLK, ENABLE);
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;  
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Pin = DEBUG_USART_TX_PIN  ;  
  GPIO_Init(DEBUG_USART_TX_GPIO_PORT, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Pin = DEBUG_USART_RX_PIN;
  GPIO_Init(DEBUG_USART_RX_GPIO_PORT, &GPIO_InitStructure);
  
  GPIO_PinAFConfig(DEBUG_USART_RX_GPIO_PORT,DEBUG_USART_RX_SOURCE,DEBUG_USART_RX_AF);
  GPIO_PinAFConfig(DEBUG_USART_TX_GPIO_PORT,DEBUG_USART_TX_SOURCE,DEBUG_USART_TX_AF);
  
  USART_InitStructure.USART_BaudRate = DEBUG_USART_BAUDRATE;
  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(DEBUG_USART, &USART_InitStructure); 
	
	USART_ITConfig(DEBUG_USART, USART_IT_RXNE, ENABLE);
  USART_Cmd(DEBUG_USART, ENABLE);
}

//****************串口2****************************
void DEBUG_USART2_Config(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  USART_InitTypeDef USART_InitStructure;
  RCC_AHB1PeriphClockCmd(DEBUG_USART2_RX_GPIO_CLK|DEBUG_USART2_TX_GPIO_CLK,ENABLE);
  RCC_APB1PeriphClockCmd(DEBUG_USART2_CLK, ENABLE);
  
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;  
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Pin = DEBUG_USART2_TX_PIN  ;  
  GPIO_Init(DEBUG_USART2_TX_GPIO_PORT, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Pin = DEBUG_USART2_RX_PIN;
  GPIO_Init(DEBUG_USART2_RX_GPIO_PORT, &GPIO_InitStructure);
   
  GPIO_PinAFConfig(DEBUG_USART2_RX_GPIO_PORT,DEBUG_USART2_RX_SOURCE,DEBUG_USART2_RX_AF);	/* 连接 PXx 到 USARTx_Tx*/ 
  GPIO_PinAFConfig(DEBUG_USART2_TX_GPIO_PORT,DEBUG_USART2_TX_SOURCE,DEBUG_USART2_TX_AF);	/* 连接 PXx 到 USARTx__Rx*/
  
  USART_InitStructure.USART_BaudRate = DEBUG_USART2_BAUDRATE;		/* 波特率设置:DEBUG_USART_BAUDRATE */  
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;		/* 字长(数据位 校验位):8 */  
  USART_InitStructure.USART_StopBits = USART_StopBits_1;				/* 停止位: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模式控制:同时使能接收和发送 */  
  USART_Init(DEBUG_USART2, &USART_InitStructure); 																/* 完成USART初始化配置 */
	
	USART_ITConfig(DEBUG_USART2, USART_IT_RXNE, ENABLE);	/* 使能串口接收中断 */	
	USART_ITConfig(DEBUG_USART2, USART_IT_IDLE, ENABLE);/* 使能串口帧检测中断 */
  USART_Cmd(DEBUG_USART2, ENABLE);											/* 使能串口 */
}

//****************串口3****************************
void DEBUG_USART3_Config(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  USART_InitTypeDef USART_InitStructure;
  RCC_AHB1PeriphClockCmd(DEBUG_USART3_RX_GPIO_CLK|DEBUG_USART3_TX_GPIO_CLK,ENABLE);
  RCC_APB1PeriphClockCmd(DEBUG_USART3_CLK, ENABLE);
  
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;  
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Pin = DEBUG_USART3_TX_PIN  ;  
  GPIO_Init(DEBUG_USART3_TX_GPIO_PORT, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Pin = DEBUG_USART3_RX_PIN;
  GPIO_Init(DEBUG_USART3_RX_GPIO_PORT, &GPIO_InitStructure);
   
  GPIO_PinAFConfig(DEBUG_USART3_RX_GPIO_PORT,DEBUG_USART3_RX_SOURCE,DEBUG_USART3_RX_AF);	/* 连接 PXx 到 USARTx_Tx*/ 
  GPIO_PinAFConfig(DEBUG_USART3_TX_GPIO_PORT,DEBUG_USART3_TX_SOURCE,DEBUG_USART3_TX_AF);	/* 连接 PXx 到 USARTx__Rx*/
  
  USART_InitStructure.USART_BaudRate = DEBUG_USART3_BAUDRATE;		/* 波特率设置:DEBUG_USART_BAUDRATE */  
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;		/* 字长(数据位 校验位):8 */  
  USART_InitStructure.USART_StopBits = USART_StopBits_1;				/* 停止位: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模式控制:同时使能接收和发送 */  
  USART_Init(DEBUG_USART3, &USART_InitStructure); 																/* 完成USART初始化配置 */
	
	USART_ITConfig(DEBUG_USART3, USART_IT_RXNE, ENABLE);	/* 使能串口接收中断 */	
	USART_ITConfig(DEBUG_USART3, USART_IT_IDLE, ENABLE);/* 使能串口帧检测中断 */
  USART_Cmd(DEBUG_USART3, ENABLE);											/* 使能串口 */
}

.h文件中

代码语言:javascript复制
#include "stm32f4xx.h"
#include <stdio.h>


//引脚定义
/*******************************************************/
#define DEBUG_USART                             USART1
#define DEBUG_USART_CLK                         RCC_APB2Periph_USART1
#define DEBUG_USART_BAUDRATE                    115200  //串口波特率

#define DEBUG_USART_RX_GPIO_PORT                GPIOA
#define DEBUG_USART_RX_GPIO_CLK                 RCC_AHB1Periph_GPIOA
#define DEBUG_USART_RX_PIN                      GPIO_Pin_10
#define DEBUG_USART_RX_AF                       GPIO_AF_USART1
#define DEBUG_USART_RX_SOURCE                   GPIO_PinSource10

#define DEBUG_USART_TX_GPIO_PORT                GPIOA
#define DEBUG_USART_TX_GPIO_CLK                 RCC_AHB1Periph_GPIOA
#define DEBUG_USART_TX_PIN                      GPIO_Pin_9
#define DEBUG_USART_TX_AF                       GPIO_AF_USART1
#define DEBUG_USART_TX_SOURCE                   GPIO_PinSource9

#define DEBUG_USART_IRQHandler                  USART1_IRQHandler
#define DEBUG_USART_IRQ                 				USART1_IRQn
/************************************************************/

//串口2引脚定义
/*******************************************************/
#define DEBUG_USART2                             USART2
#define DEBUG_USART2_CLK                         RCC_APB1Periph_USART2
#define DEBUG_USART2_BAUDRATE                    9600  //串口波特率

#define DEBUG_USART2_RX_GPIO_PORT                GPIOD
#define DEBUG_USART2_RX_GPIO_CLK                 RCC_AHB1Periph_GPIOD
#define DEBUG_USART2_RX_PIN                      GPIO_Pin_6
#define DEBUG_USART2_RX_AF                       GPIO_AF_USART2
#define DEBUG_USART2_RX_SOURCE                   GPIO_PinSource6

#define DEBUG_USART2_TX_GPIO_PORT                GPIOD
#define DEBUG_USART2_TX_GPIO_CLK                 RCC_AHB1Periph_GPIOD
#define DEBUG_USART2_TX_PIN                      GPIO_Pin_5
#define DEBUG_USART2_TX_AF                       GPIO_AF_USART2
#define DEBUG_USART2_TX_SOURCE                   GPIO_PinSource5

#define DEBUG_USART2_IRQHandler                  USART2_IRQHandler
#define DEBUG_USART2_IRQ                 				 USART2_IRQn
/************************************************************/

//串口3引脚定义
/*******************************************************/
#define DEBUG_USART3                             USART3
#define DEBUG_USART3_CLK                         RCC_APB1Periph_USART3
#define DEBUG_USART3_BAUDRATE                    9600  //串口波特率

#define DEBUG_USART3_RX_GPIO_PORT                GPIOB
#define DEBUG_USART3_RX_GPIO_CLK                 RCC_AHB1Periph_GPIOB
#define DEBUG_USART3_RX_PIN                      GPIO_Pin_11
#define DEBUG_USART3_RX_AF                       GPIO_AF_USART3
#define DEBUG_USART3_RX_SOURCE                   GPIO_PinSource11

#define DEBUG_USART3_TX_GPIO_PORT                GPIOB
#define DEBUG_USART3_TX_GPIO_CLK                 RCC_AHB1Periph_GPIOB
#define DEBUG_USART3_TX_PIN                      GPIO_Pin_10
#define DEBUG_USART3_TX_AF                       GPIO_AF_USART3
#define DEBUG_USART3_TX_SOURCE                   GPIO_PinSource10

#define DEBUG_USART3_IRQHandler                  USART3_IRQHandler
#define DEBUG_USART3_IRQ                 				 USART3_IRQn
/************************************************************/

void NVIC_Configuration(void);
void Debug_USART_Config(void);
void DEBUG_USART2_Config(void);
void DEBUG_USART3_Config(void);

0 人点赞