STM32F103 串口1通过DMA发送数据HAL库实现
时间: 2023-11-30 07:05:08 浏览: 92
STM32F103串口DMA发送
要实现STM32F103串口1通过DMA发送数据,可以使用HAL库提供的函数进行配置。以下是基本的步骤:
1. 开启DMA时钟和串口时钟:
```
__HAL_RCC_DMA1_CLK_ENABLE();
__HAL_RCC_USART1_CLK_ENABLE();
```
2. 配置串口:
```
huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
HAL_UART_Init(&huart1);
```
3. 配置DMA传输通道:
```
hdma_usart1_tx.Instance = DMA1_Channel4;
hdma_usart1_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
hdma_usart1_tx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_usart1_tx.Init.MemInc = DMA_MINC_ENABLE;
hdma_usart1_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hdma_usart1_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hdma_usart1_tx.Init.Mode = DMA_NORMAL;
hdma_usart1_tx.Init.Priority = DMA_PRIORITY_HIGH;
HAL_DMA_Init(&hdma_usart1_tx);
__HAL_LINKDMA(&huart1,hdmatx,hdma_usart1_tx);
```
4. 编写发送函数:
```
void UART_DMA_SendData(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
{
HAL_UART_Transmit_DMA(huart, pData, Size);
}
```
5. 调用发送函数:
```
uint8_t send_buf[10] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A};
UART_DMA_SendData(&huart1, send_buf, 10);
```
以上就是通过HAL库实现STM32F103串口1通过DMA发送数据的基本步骤。需要注意的是,具体的配置参数需要根据情况进行调整。
阅读全文