DMA1_Stream5_IRQHandler()和DMA1_Channel2_IRQHandler()中断函数有什么区别?
时间: 2024-05-25 10:10:17 浏览: 25
DMA1_Stream5_IRQHandler()和DMA1_Channel2_IRQHandler()都是与DMA相关的中断处理函数,但是它们处理的是不同的DMA通道。
DMA1_Channel2_IRQHandler()是处理DMA1通道2的中断,而DMA1_Stream5_IRQHandler()是处理DMA1流5的中断。
通道是硬件DMA控制器用于传输数据的逻辑通道,而流是在通道内部的物理通道。一个通道可以有多个流,每个流可以执行不同的DMA传输操作。
因此,这两个中断处理函数的区别在于它们处理的是不同的DMA通道和流。具体选择哪个中断处理函数,要根据硬件设计和DMA传输的需要来确定。
相关问题
DMA1_Channel1_IRQn 具体的使用代码
好的,以下是一个使用 DMA1_Channel1_IRQn 中断的 STM32F4 程序的代码示例,主要实现了从 ADC1 数据寄存器中读取采样数据,通过 DMA1 传输到内存中,并在传输完成时通过 DMA1_Channel1_IRQHandler 中断进行处理:
```c
#include "stm32f4xx.h"
#include "stm32f4xx_dma.h"
#include "stm32f4xx_adc.h"
#define ADC1_DR_ADDRESS ((uint32_t)0x4001204C)
#define ADC1_CHANNEL 0
volatile uint16_t ADCValue;
void DMA_Configuration(void)
{
DMA_InitTypeDef DMA_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);
DMA_DeInit(DMA1_Stream0);
DMA_InitStructure.DMA_Channel = DMA_Channel_0;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADCValue;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = 1;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
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_Cmd(DMA1_Stream0, ENABLE);
}
void ADC_Configuration(void)
{
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
ADC_CommonInitTypeDef ADC_CommonInitStructure;
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
ADC_CommonInit(&ADC_CommonInitStructure);
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = 1;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_15Cycles);
ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);
ADC_DMACmd(ADC1, ENABLE);
ADC_Cmd(ADC1, ENABLE);
ADC_SoftwareStartConv(ADC1);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void DMA1_Channel1_IRQHandler(void)
{
if(DMA_GetFlagStatus(DMA1_Stream0, DMA_FLAG_TCIF0) != RESET)
{
DMA_ClearFlag(DMA1_Stream0, DMA_FLAG_TCIF0);
ADC_SoftwareStartConv(ADC1);
// 处理采样数据
}
}
int main(void)
{
DMA_Configuration();
ADC_Configuration();
NVIC_Configuration();
while(1);
}
```
这份代码主要完成了以下工作:
1. 配置 ADC1 和 GPIOA0,设置采样通道和采样时间;
2. 配置 DMA1_Stream0,将 ADC1 的 DR 寄存器的值传输到 ADCValue 变量中;
3. 配置 DMA1_Channel1_IRQn 中断,设置优先级,并在传输完成时进行处理。
在程序中,ADC_SoftwareStartConv 函数用于启动 ADC1 的转换,将采样数据传输到 ADCValue 变量中,当 DMA1_Stream0 传输完成时,会触发 DMA1_Channel1_IRQHandler 中断,我们可以在此中断服务函数中进行采样数据的处理。
用STM32F4的库函数写通过DMA_GetCurrentMemoryTarget函数判断DMA中断里面需要处理双缓冲哪一组数据
以下是用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已经传输了至少一次数据,否则该函数会返回错误的结果。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)