STM32 串口1通过DMA发送数据实现
时间: 2023-11-29 17:05:40 浏览: 110
stm32串口使用DMA发送数据
要实现STM32串口1通过DMA发送数据,需要按照以下步骤进行配置:
1. 选择需要发送的串口,并开启DMA传输功能:
```
USART1->CR3 |= USART_CR3_DMAT;
```
2. 配置DMA传输通道:
```
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
DMA_InitTypeDef DMA_InitStructure;
DMA_DeInit(DMA2_Stream7);
DMA_InitStructure.DMA_Channel = DMA_Channel_4;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&USART1->DR;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)send_buf;
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
DMA_InitStructure.DMA_BufferSize = send_len;
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_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA2_Stream7, &DMA_InitStructure);
```
其中,send_buf为待发送的数据缓存,send_len为待发送的数据长度。
3. 启动DMA传输:
```
DMA_Cmd(DMA2_Stream7, ENABLE);
```
4. 等待DMA传输完成:
```
while(DMA_GetFlagStatus(DMA2_Stream7,DMA_FLAG_TCIF7)==RESET);
DMA_ClearFlag(DMA2_Stream7,DMA_FLAG_TCIF7);
```
5. 关闭DMA传输:
```
DMA_Cmd(DMA2_Stream7, DISABLE);
```
以上就是通过DMA实现STM32串口1发送数据的步骤。需要注意的是,具体的配置参数需要根据情况进行调整。
阅读全文