STM32F401用配置串口2的dma,并用串口dma发送数据给上位机,请只用c语言详细编写相关库函数代码
时间: 2023-11-22 11:56:58 浏览: 76
STM32 自定义HID 设备, 通过快传输接收上位机发来的数据。上位机使用Libusb库函数实现,接口简单
5星 · 资源好评率100%
以下是STM32F401串口2 DMA发送数据的相关库函数代码,仅供参考:
1. 配置串口2:
```c
void UART2_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
// Enable the GPIOA clock
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
// Enable the USART2 clock
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
// Connect PA2 to USART2_Tx
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
// Configure USART Tx as alternate function push-pull
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
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 configuration
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_Tx;
USART_Init(USART2, &USART_InitStructure);
USART_Cmd(USART2, ENABLE);
}
```
2. 配置DMA:
```c
void DMA_Config(void)
{
DMA_InitTypeDef DMA_InitStructure;
// Enable the DMA1 clock
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
// DMA configuration
DMA_DeInit(DMA1_Stream6);
DMA_InitStructure.DMA_Channel = DMA_Channel_4;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&USART2->DR;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)TxBuffer;
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
DMA_InitStructure.DMA_BufferSize = TxBufferSize;
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_HalfFull;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA1_Stream6, &DMA_InitStructure);
// Enable the USART2 DMA requests
USART_DMACmd(USART2, USART_DMAReq_Tx, ENABLE);
// Enable the DMA1 Stream6
DMA_Cmd(DMA1_Stream6, ENABLE);
}
```
3. 发送数据:
```c
void UART2_DMA_SendData(uint8_t* data, uint16_t length)
{
// Wait for the DMA1 Stream6 to be ready
while (DMA_GetFlagStatus(DMA1_Stream6, DMA_FLAG_TCIF6) == RESET);
// Clear the DMA1 Stream6 transfer complete flag
DMA_ClearFlag(DMA1_Stream6, DMA_FLAG_TCIF6);
// Copy the data to the TxBuffer
for (int i = 0; i < length; i++) {
TxBuffer[i] = data[i];
}
// Set the DMA1 Stream6 transfer length
DMA_SetCurrDataCounter(DMA1_Stream6, length);
// Enable the DMA1 Stream6
DMA_Cmd(DMA1_Stream6, ENABLE);
}
```
在上述代码中,我们使用DMA1的Stream6配置串口2发送数据。首先,我们需要调用UART2_Config()函数来配置串口2,然后调用DMA_Config()函数来配置DMA。在发送数据时,我们需要将数据复制到TxBuffer中,并调用UART2_DMA_SendData()函数来启动DMA传输。DMA传输完成后,我们需要等待DMA传输完成,并清除DMA传输完成标志。
阅读全文