用STM32F4的库函数写通过DMA_GetCurrentMemoryTarget函数判断DMA中断里面需要处理双缓冲哪一组数据
时间: 2024-04-30 08:22:40 浏览: 128
STM32F4串口DMA双缓冲模式DEMO
以下是用STM32F4的库函数写通过DMA_GetCurrentMemoryTarget函数判断DMA中断里面需要处理双缓冲哪一组数据的示例代码:
```c
#include "stm32f4xx.h"
#define BUFFER_SIZE 256
uint16_t buffer1[BUFFER_SIZE];
uint16_t buffer2[BUFFER_SIZE];
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
void init_DMA(void)
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);
DMA_InitStructure.DMA_Channel = DMA_Channel_0;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)buffer1;
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_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
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_HalfFull;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA1_Stream0, &DMA_InitStructure);
DMA_ITConfig(DMA1_Stream0, DMA_IT_TC, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
DMA_Cmd(DMA1_Stream0, ENABLE);
}
void DMA1_Stream0_IRQHandler(void)
{
if (DMA_GetITStatus(DMA1_Stream0, DMA_IT_TCIF0))
{
DMA_ClearITPendingBit(DMA1_Stream0, DMA_IT_TCIF0);
if (DMA_GetCurrentMemoryTarget(DMA1_Stream0) == 0)
{
// Process buffer1
}
else
{
// Process buffer2
}
}
}
int main(void)
{
init_DMA();
while (1)
{
// Main program loop
}
}
```
在这个示例代码中,首先定义了两个缓冲区`buffer1`和`buffer2`,并且使用DMA将ADC1的数据传输到这两个缓冲区中。在DMA中断里面,使用`DMA_GetCurrentMemoryTarget`函数判断当前需要处理的是哪一个缓冲区,然后进行相应的处理。
需要注意的是,在初始化DMA的时候,要将`DMA_Mode`设置为`DMA_Mode_Circular`,这样才能实现双缓冲的功能。另外,在使用`DMA_GetCurrentMemoryTarget`函数之前,要确保DMA已经传输了至少一次数据,否则该函数会返回错误的结果。
阅读全文