STM32F407 串口DMA接收数据的实例
时间: 2023-06-26 08:04:11 浏览: 191
以下是一个基于STM32F407芯片的串口DMA接收数据的实例代码:
```c
#include "stm32f4xx.h"
#include <stdio.h>
#include <string.h>
#define USART1_DR_Address ((uint32_t)0x40011004)
char RxBuffer[64];
uint8_t RxCounter = 0;
uint8_t RxFlag = 0;
void USART_Config(void);
void DMA_Config(void);
int main(void)
{
USART_Config();
DMA_Config();
while(1)
{
if(RxFlag)
{
printf("Received data: %s\r\n", RxBuffer);
RxFlag = 0;
memset(RxBuffer, 0, sizeof(RxBuffer));
RxCounter = 0;
}
}
}
void USART_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 115200;
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_Cmd(USART1, ENABLE);
}
void DMA_Config(void)
{
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
DMA_DeInit(DMA2_Stream2);
DMA_InitStructure.DMA_Channel = DMA_Channel_4;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)USART1_DR_Address;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)RxBuffer;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = sizeof(RxBuffer);
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA2_Stream2, &DMA_InitStructure);
DMA_ITConfig(DMA2_Stream2, DMA_IT_TC, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_DMACmd(USART1, USART_DMAReq_Rx, ENABLE);
DMA_Cmd(DMA2_Stream2, ENABLE);
}
void DMA2_Stream2_IRQHandler(void)
{
if(DMA_GetITStatus(DMA2_Stream2, DMA_IT_TCIF2))
{
DMA_ClearITPendingBit(DMA2_Stream2, DMA_IT_TCIF2);
RxCounter = sizeof(RxBuffer) - DMA_GetCurrDataCounter(DMA2_Stream2);
RxBuffer[RxCounter] = '\0';
RxFlag = 1;
}
}
```
在这个例子中,我们使用USART1和DMA2_Stream2来接收串口数据。首先,在USART_Config函数中,我们进行了USART的配置,将USART1的TX和RX引脚分别配置为PA9和PA10,并设置波特率为115200。
接下来,在DMA_Config函数中,我们对DMA进行了配置。我们将DMA的通道设置为4,将DMA的方向设置为从外设到内存,并将DMA的模式设置为循环模式。我们还启用了DMA的中断,并将DMA2_Stream2_IRQHandler函数作为中断处理函数。
最后,在DMA2_Stream2_IRQHandler函数中,我们检查DMA传输完成中断标志位,并清除该标志位。然后,我们通过DMA_GetCurrDataCounter函数计算已经接收到的数据量,并将RxFlag设置为1,表示已经接收到了一条完整的数据。
在主函数中,我们不断地检查RxFlag标志位,如果该标志位为1,则表示已经接收到了一条完整的数据,我们将其打印出来,并将RxFlag和RxBuffer数组清零。
阅读全文