400-035-6699
当前位置: 首页 » 技术支持 » 博文资讯 »

STM32串口中断编程详解与实现步骤

配置STM32的串口中断并不复杂,下面将详细介绍如何进行GPIO口配置、串口参数设置、中断设置以及如何实现数据的发送和接收。
首先,我们需要配置uart的GPIO口。这一步是确保UART能够正常工作的基础。我们可以通过以下代码来完成:
```c void UART1_GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; // 将USART1的发送端配置为复用推挽输出 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure);
// 将USART1的接收端配置为浮空输入 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); } ```
接下来,是串口参数的配置。这一步决定了串口的工作方式和通信参数。以下代码展示了如何设置串口的波特率、数据位、停止位等参数:
```c void USART_Configuration(void) { USART_InitTypeDef USART_InitStructure; USART_ClockInitTypeDef USART_ClockInitStructure;
Uart1_GPIO_Configuration(); // 同步参数配置 USART_ClockInitStructure.USART_Clock = USART_Clock_Disable; USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low; USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge; USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable; USART_ClockInit(USART1, &USART_ClockInitStructure);
// 基本参数配置 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(USART1, &USART_InitStructure);
// 启用接收中断 USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); // 启用串口 USART_Cmd(USART1, ENABLE); } ```
为了使串口中断能够正常工作,还需要对中断控制器NVIC进行配置,并修改串口中断处理函数。以下是配置NVIC的示例代码:
```c void NVIC_Configuration(void) { NVIC_InitTypeDef NVIC_InitStructure;
#ifdef VECT_TAB_RAM NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); #else NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); #endif
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } ```
在串口中断处理函数中,我们需要根据不同的情况处理接收到的数据或者发送数据:
```c void USART1_IRQHandler(void) { // 处理接收到的数据 if(USART_GetITStatus(USART1, USART_IT_RXNE) != reset) { USART_ClearITPendingBit(USART1, USART_IT_RXNE); }
// 发送中断 if (USART_GetITStatus(USART1, USART_IT_TXE) != RESET) { USART_SendData(USART1, Send_Data[Send_Length++]); if (Send_Length == SEND_LENGTH) { USART_ClearITPendingBit(USART1,USART_IT_TXE); USART_ITConfig(USART1, USART_IT_TXE, DISABLE); USART_ITConfig(USART1, USART_IT_TC, ENABLE); } }
// 发送完成 if (USART_GetITStatus(USART1, USART_IT_TC) != RESET) { USART_ClearITPendingBit(USART1,USART_IT_TC); USART_ITConfig(USART1, USART_IT_TC, DISABLE); } } ```
最后,在需要发送数据的地方,只需设置好数据数组`Send_Data`和发送长度,然后调用以下函数即可启动发送:
```c void Send_to_PC(void) { USART_ITConfig(USART1, USART_IT_TXE, ENABLE); } ```
通过以上步骤,STM32的串口就可以顺利地工作起来,实现数据的接收和发送。在实际应用中,根据具体的通信需求,可能还需要对代码进行适当的调整和优化。

STM32的串口中断配置,也是很简单的.

STM32串口中断编程详解与实现步骤

首先是配置UART的GPIO口

首先是配置UART的GPIO口

/**********************************************

* Name : UART1_GPIO_Configuration

* Deion : Configures the uart1 GPIO ports.

* Input : None

* Output : None

* Return : None

**********************************************************/

void UART1_GPIO_Configuration(void)

{

GPIO_InitTypeDef GPIO_InitStructure;

// Configure USART1_Tx as alterNATe push-pull

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

GPIO_Init(GPIOA, &GPIO_InitStructure);

// Configure USART1_Rx as input floating

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

GPIO_Init(GPIOA, &GPIO_InitStructure);

}

然后是配置串口参数

/*******************************************************

* Name : UART1_Configuration

* Deion : Configures the uart1

* Input : None

* Output : None

* Return : None

*********************************************/

void USART_Configuration(void)

{

USART_InitTypeDef USART_InitStructure;

USART_ClockInitTypeDef USART_ClockInitStructure;

Uart1_GPIO_Configuration();

USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;

USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;

USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;

USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;

/* Configure the USART1 synchronous paramters */

USART_ClockInit(USART1, &USART_ClockInitStructure);

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;

/* Configure USART1 bASIC and asynchronous paramters */

USART_Init(USART1, &USART_InitStructure);

/* Enable USART1 Receive interrupts */

USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

/* Enable USART1 */

USART_Cmd(USART1, ENABLE);

}

然后是在中断设置,需要修改stm32f10x_it.c 中的串口中断函数 并且需要修改void NVIC_Configuration(void)函数

修改NVIC_Configuration函数

/***********************************************************

* Name : NVIC_Configuration

* Deion : Configures NVIC and Vector Table base location.

* Input : None

* Output : None

* Return : None

***************************************************/

void NVIC_Configuration(void)

{

NVIC_InitTypeDef NVIC_InitStructure;

#ifdef VECT_TAB_RAM

/* Set the Vector Table base location at 0x20000000 */

NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);

#else /* VECT_TAB_FLASH */

/* Set the Vector Table base location at 0x08000000 */

NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);

#endif

/* Configure the NVIC Preemption Priority Bits */

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);

/* Enable the USART1 Interrupt */

NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel;

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init(&NVIC_InitStructure);

}

//串口中断

void USART1_IRQHandler(void)

{

//处理接收到的数据

if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)

{

/* Clear the USART1 Receive interrupt */

USART_ClearITPendingBit(USART1, USART_IT_RXNE);

}

//发送中断

if (USART_GetITStatus(USART1, USART_IT_TXE) != RESET)

{

USART_SendData(USART1, Send_Data[Send_Length++]);

if (Send_Length==SEND_LENGTH)

{

//发送字节结束

USART_ClearITPendingBit(USART1,USART_IT_TXE);

USART_ITConfig(USART1, USART_IT_TXE, DISABLE);

USART_ITConfig(USART1, USART_IT_TC, ENABLE);

}

}

//发送完成

if (USART_GetITStatus(USART1, USART_IT_TC) != RESET)

{

USART_ClearITPendingBit(USART1,USART_IT_TC);

USART_ITConfig(USART1, USART_IT_TC, DISABLE);

}

}

在需要发送的程序里Send_Data[SEND_LENGTH]和发送长度设置好,

void Send_to_PC(void)

{

//设置好Send_Data[SEND_LENGTH]数组

//打开发送中断

USART_ITConfig(USART1, USART_IT_TXE, ENABLE);

}

至此 串口就可以工作起来了!~

【限时免费】一键获取网络规划系统模板+传输架构设计+连通性评估方案

STM32相关文章

服务电话:
400-035-6699
企服商城