STM32F407使用标准库实现USART2使用DMA进行数据循环接收实例
时间: 2023-09-06 17:11:06 浏览: 56
下面是一个使用STM32F407标准库和DMA的USART2循环接收示例代码。该代码使用DMA接收USART2传输的数据,并将其存储在一个缓冲区中,然后在接收到特定字符时将缓冲区中的数据打印出来。
```c
#include "stm32f4xx.h"
#include <stdio.h>
#include <string.h>
#define BUFFER_SIZE 50
#define RX_COMPLETE_FLAG (1 << 6)
volatile uint8_t buffer[BUFFER_SIZE];
volatile uint8_t rx_index = 0;
volatile uint8_t rx_complete = 0;
void USART2_Init(void);
void DMA1_Stream5_IRQHandler(void);
int main(void)
{
USART2_Init();
while (1)
{
if (rx_complete)
{
printf("Received: %s\n", buffer);
rx_index = 0;
rx_complete = 0;
memset(buffer, 0, sizeof(buffer));
}
}
}
void USART2_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
USART_InitTypeDef USART_InitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
DMA_InitTypeDef DMA_InitStruct;
// Enable clock for GPIOA and USART2
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
// Configure GPIO pins for USART2 transmit and receive
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStruct);
// Connect GPIO pins to USART2 alternate function
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);
// Configure USART2
USART_InitStruct.USART_BaudRate = 115200;
USART_InitStruct.USART_WordLength = USART_WordLength_8b;
USART_InitStruct.USART_StopBits = USART_StopBits_1;
USART_InitStruct.USART_Parity = USART_Parity_No;
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART2, &USART_InitStruct);
// Enable USART2 receive interrupt
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
// Configure NVIC for USART2
NVIC_InitStruct.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);
// Configure DMA1 Channel 4 Stream 5 for USART2 receive
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);
DMA_InitStruct.DMA_Channel = DMA_Channel_4;
DMA_InitStruct.DMA_PeripheralBaseAddr = (uint32_t)&USART2->DR;
DMA_InitStruct.DMA_Memory0BaseAddr = (uint32_t)buffer;
DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStruct.DMA_BufferSize = BUFFER_SIZE;
DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStruct.DMA_Mode = DMA_Mode_Circular;
DMA_InitStruct.DMA_Priority = DMA_Priority_High;
DMA_InitStruct.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStruct.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
DMA_InitStruct.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStruct.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA1_Stream5, &DMA_InitStruct);
// Enable DMA1 Stream 5 transfer complete interrupt
DMA_ITConfig(DMA1_Stream5, DMA_IT_TC, ENABLE);
// Configure NVIC for DMA1 Stream 5
NVIC_InitStruct.NVIC_IRQChannel = DMA1_Stream5_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);
// Enable USART2 and DMA1 Channel 4 Stream 5
USART_Cmd(USART2, ENABLE);
DMA_Cmd(DMA1_Stream5, ENABLE);
}
void USART2_IRQHandler(void)
{
if (USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
USART_ClearITPendingBit(USART2, USART_IT_RXNE);
}
}
void DMA1_Stream5_IRQHandler(void)
{
if (DMA_GetFlagStatus(DMA1_Stream5, RX_COMPLETE_FLAG) != RESET)
{
DMA_ClearFlag(DMA1_Stream5, RX_COMPLETE_FLAG);
// Copy the contents of the buffer up to the delimiter to a local buffer
uint8_t temp_buffer[BUFFER_SIZE];
uint8_t delimiter = '\n';
for (uint8_t i = 0; i < BUFFER_SIZE; i++)
{
temp_buffer[i] = buffer[i];
if (buffer[i] == delimiter)
{
temp_buffer[i] = '\0';
rx_complete = 1;
break;
}
}
}
}
```
在此示例中,我们使用DMA1的通道4和流5来接收USART2传输的数据。使用DMA实现循环接收的好处是,可以避免在中断中处理大量数据,从而提高系统的响应能力。在DMA配置中,我们将USART2的数据寄存器作为DMA的外设地址,并将缓冲区作为DMA的内存地址。我们还启用了循环模式,以便在接收缓冲区溢出时重新开始接收。
在DMA传输完成时,我们将使用DMA传输完成中断处理程序来检查接收到的数据。由于我们在接收到特定字符时才打印缓冲区中的数据,因此我们需要在此处检查缓冲区中是否存在特定字符。如果找到特定字符,则将缓冲区中的数据复制到临时缓冲区中,并将rx_complete标志设置为1,以便在主循环中打印数据。在打印缓冲区中的数据之后,我们需要清空缓冲区以便接收新的数据。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)