在STM32F4程序设计中我想用串口DMA加空闲中断的方式进行定长数据的收发,请问要怎么编程,请详细指出
时间: 2024-02-11 17:05:44 浏览: 54
好的,下面是详细的程序设计步骤:
1. 首先,你需要启用串口和DMA的时钟。
```c
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1 | RCC_AHB1Periph_DMA2, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
```
2. 配置串口的GPIO引脚。
```c
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
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);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);
```
3. 配置USART2的参数,包括波特率、数据位、停止位、校验位等。
```c
USART_InitTypeDef USART_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(USART2, &USART_InitStructure);
```
4. 配置DMA的参数,包括数据传输方向、数据宽度、传输大小、传输模式等。
```c
DMA_InitTypeDef DMA_InitStructure;
DMA_InitStructure.DMA_Channel = DMA_Channel_4;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) & (USART2->DR);
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t) & receive_buffer;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = BUFFER_SIZE;
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_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(DMA1_Stream5, &DMA_InitStructure);
```
5. 配置空闲中断,用于通知主程序收到数据。
```c
USART_ITConfig(USART2, USART_IT_IDLE, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
```
6. 启动DMA传输,并且开启USART2的接收中断。
```c
DMA_Cmd(DMA1_Stream5, ENABLE);
USART_DMACmd(USART2, USART_DMAReq_Rx, ENABLE);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
```
7. 在USART2的中断服务函数USART2_IRQHandler()中处理接收到的数据。
```c
void USART2_IRQHandler(void)
{
if (USART_GetITStatus(USART2, USART_IT_IDLE) != RESET)
{
DMA_Cmd(DMA1_Stream5, DISABLE);
uint16_t len = BUFFER_SIZE - DMA_GetCurrDataCounter(DMA1_Stream5);
process_received_data(receive_buffer, len); // 处理接收到的数据
DMA_SetCurrDataCounter(DMA1_Stream5, BUFFER_SIZE);
DMA_Cmd(DMA1_Stream5, ENABLE);
USART_ClearITPendingBit(USART2, USART_IT_IDLE);
}
}
```
其中,process_received_data()函数是用来处理接收到的数据的,你需要自己实现。
另外,上面的代码是用USART2和DMA1_Stream5进行数据收发的,如果你要用其他的串口和DMA,需要根据实际情况进行修改。
阅读全文