原创 stm32 DMA中断范例

2011-5-30 17:03 17825 7 7 分类: MCU/ 嵌入式

zip.gifDMA.zip

要注意DMA使用使能或者失能AHB外设时钟的函数是RCC_AHBPeriphClockCmd(),所以,初始化时要调用它。

比如:RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);

这个程序给我的感觉是:不管程序在哪里,即使在一个错误的地方运行(可能程序已经返回)。当中断发生时,只要没有被屏蔽掉,程序是跳到中断服务程序的地址去执行的,即使此前程序返回。不知道我的判断是不是对的,各位发表一下高见。

 

/* Private variables ---------------------------------------------------------*/
vu16 CurrDataCounterBegin=0,CurrDataCounterEnd=0;
uc32 SRC_Const_Buffer[BufferSize]={0x12345678,0x12345678,0x12345678,0x12345678,
  0x12345678,0x12345678,0x12345678,0x12345678,0x12345678,0x12345678,0x12345678,0x12345678,
  0x12345678,0x12345678,0x12345678,0x12345678,0x12345678,0x12345678,0x12345678,0x12345678,
  0x12345678,0x12345678,0x12345678,0x12345678,0x12345678,0x12345678,0x12345678,0x12345678,
  0x12345678,0x12345678,0x12345678,0x12345678};
u32 DST_Buffer[BufferSize];

/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
void RCC_Configuration()
{
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
}

void GPIO_Configuration()
{
  GPIO_InitTypeDef GPIO_InitStructure;
  GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1;
  GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
  GPIO_Init(GPIOA,&GPIO_InitStructure);
}

void NVIC_Configuration()
{
  NVIC_InitTypeDef NVIC_InitStructure;
 
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
  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 DMA_Configuration()
{
  DMA_InitTypeDef DMA_InitStructure;
 
  DMA_DeInit(DMA1_Channel1);
  DMA_InitStructure.DMA_PeripheralBaseAddr=(u32)SRC_Const_Buffer;
  DMA_InitStructure.DMA_MemoryBaseAddr=(u32)DST_Buffer;
  DMA_InitStructure.DMA_DIR=DMA_DIR_PeripheralSRC;
  DMA_InitStructure.DMA_BufferSize=BufferSize;
  DMA_InitStructure.DMA_PeripheralInc=DMA_PeripheralInc_Enable;
  DMA_InitStructure.DMA_MemoryInc=DMA_MemoryInc_Enable;
  DMA_InitStructure.DMA_PeripheralDataSize=DMA_PeripheralDataSize_Word;
  DMA_InitStructure.DMA_MemoryDataSize=DMA_MemoryDataSize_Word;
  DMA_InitStructure.DMA_Mode=DMA_Mode_Normal;
  DMA_InitStructure.DMA_Priority=DMA_Priority_Low;
  DMA_InitStructure.DMA_M2M=DMA_M2M_Enable;
  DMA_Init(DMA1_Channel1,&DMA_InitStructure);
 
  DMA_ITConfig(DMA1_Channel1,DMA_IT_TC,ENABLE);
 
  DMA_Cmd(DMA1_Channel1,ENABLE);
}

void DMA1_Channel1_IRQHandler()
{
  GPIO_SetBits(GPIOA,GPIO_Pin_1);
  if(DMA_GetITStatus(DMA1_IT_TC1))
  {
    CurrDataCounterEnd=DMA_GetCurrDataCounter(DMA1_Channel1);
    DMA_ClearITPendingBit(DMA1_IT_GL1);
  }
}

/**
  * @brief  Main program.
  * @param  None
  * @retval : None
  */
int main(void)
{
  /* Setup STM32 system (clock, PLL and Flash configuration) */
  SystemInit();

  /* Add your application code here
     */
  RCC_Configuration();
  GPIO_Configuration();
  NVIC_Configuration();
  DMA_Configuration();
 
  while(CurrDataCounterEnd!=0);//其实这句没什么作用

  /* Infinite loop *///程序虽然返回,但中断还是会发生
/*  while (1)
  {
  }*/
}

文章评论0条评论)

登录后参与讨论
我要评论
0
7
关闭 站长推荐上一条 /2 下一条