- void ADC_DMA_Config(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- //DMA for ADCÅäÖýṹ
- DMA_InitTypeDef DMA_InitStructure;
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_ADC2, ENABLE);
- DMA_DeInit(DMA2_Stream0);
- /* DMA2 Stream0 channe0 configuration **************************************/
- DMA_InitStructure.DMA_Channel = ADC_DMAn;
- DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)0x40012308;
- DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&DMABuf[DMABufFlag][0];
- DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
- DMA_InitStructure.DMA_BufferSize = ADC_REG_SEQ_SIZE;
- DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
- 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_Circular;
- // 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(DMA2_Stream0, &DMA_InitStructure);
-
- /* Enable the DMA gloabal Interrupt */
- DMA_ClearITPendingBit(DMA2_Stream0,DMA_IT_TC);
- DMA_ITConfig(DMA2_Stream0, DMA_IT_TC, ENABLE);
- //Enable DMA ADC_DMAn
- DMA_Cmd(DMA2_Stream0, ENABLE);
- //DMA for ADC ÖжÏʹÄÜ
-
- NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream0_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
复制代码- void ADC_Config(void)
- {
- ADC_InitTypeDef ADC_InitStructure;
- ADC_CommonInitTypeDef ADC_CommonInitStructure;
- /* Enable ADC clock */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_ADC2, ENABLE);
- /* ADC2 regular channel 12 configuration ************************************/
- ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
- ADC_InitStructure.ADC_ScanConvMode = ENABLE;
- ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
- ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
- ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T2_CC4;
- ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
- ADC_InitStructure.ADC_NbrOfConversion = ADC_REG_SEQ_SIZE;
- ADC_Init(ADC2, &ADC_InitStructure);
- /* ADC1 regular channel 12 configuration ************************************/
- ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
- ADC_InitStructure.ADC_ScanConvMode = ENABLE;
- ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //ENABLE
- ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising;
- ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T2_CC4;
- ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
- ADC_InitStructure.ADC_NbrOfConversion = ADC_REG_SEQ_SIZE;
- ADC_Init(ADC1, &ADC_InitStructure);
- /* ADC Common configuration *************************************************/
- ADC_CommonInitStructure.ADC_Mode = ADC_DualMode_RegSimult; //Ë«ÖØģʽ¡¢¹æÔò×é
- ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
- ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_2;
- ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4;
- ADC_CommonInit(&ADC_CommonInitStructure);
- //¸üÐÂɨÃèÐòÁÐ×é
- ReNewADC_RegularSeqConfig();
- //ʹÄÜζȴ«¸ÐÆ÷ºÍÄÚ²¿²Î¿¼µçѹͨµÀ
- ADC_TempSensorVrefintCmd(ENABLE);
- //Enable ADC1 DMA
- ADC_DMACmd(ADC1, ENABLE);
-
- //Enable ADC1 ADC2
- ADC_Cmd(ADC1, ENABLE);
- ADC_Cmd(ADC2, ENABLE); //Ë«ADC
- }
复制代码- void TIM2_Config(void)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
- TIM_DeInit(TIM2);//¸´Î»TIM2¶¨Ê±Æ÷
- //X us¶¨Ê±ÖжÏ
- TIM_TimeBaseStructure.TIM_Period = SamplePeriod; //×î´ó¼ÆÊýÖµ0xffff
- TIM_TimeBaseStructure.TIM_Prescaler = 71;//·ÖƵ 1us
- TIM_TimeBaseStructure.TIM_ClockDivision = 0x00;//·ÖƵÒò×Ó //CK_CNT = FCLK /(TIM_Prescaler+1)
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
- //PWM1 Mode configuration: Channel4
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_Pulse = (SamplePeriod>>1);//(Õ¼¿Õ)
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
- TIM_OC4Init(TIM2, &TIM_OCInitStructure);
- TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Disable);//¹Ø±ÕԤװÔØ
- TIM_ARRPreloadConfig(TIM2, ENABLE); //ʹÄÜ×Ô¶¯ÖØ×°
- }
复制代码- void ADC_DMA_TIM2Config(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- /* Enable GPIOA clock */
- RCC_APB2PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
- /* Configure PA.0-7 pin (ADC Channel0 ~ Channel7) as analog input */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 |
- GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
-
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- /* Configure PC.0-3 pin (ADC Channel10 ~ Channel2) as analog input */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- ADC_DMA_Config();
- ADC_Config();
- TIM2_Config();
- /* Enable DMA request after last transfer (multi-ADC mode) ******************/
- ADC_MultiModeDMARequestAfterLastTransferCmd(ENABLE);
- /* TIM2 main Output Enable */
- TIM_CtrlPWMOutputs(TIM2,ENABLE);
- //ÍⲿʱÖÓ´¥·¢ADC
- TIM_Cmd(TIM2, ENABLE);
- }
复制代码- void DMA2_Stream0_IRQHandler(void)
- {
- INT8U err;
- u8 temp;
- if(DMA_GetITStatus(DMA2_Stream0,DMA_IT_TCIF0))
- {
- if(SampleCounter < HITS) //Öܲ¨²ÉÑùÍê³É
- {
- temp = DMABufFlag; //±£´æÍê³ÉͨµÀµÄ±êÖ¾
-
- DMABufFlag ^= 0x01; //ÂÖתͨµÀ
- DMA2_Stream0->M0AR = (u32)&DMABuf[DMABufFlag][0];//еĽÓÊÕ»º´æÇø
- //...
- SampleCounter++;
- if(SampleCounter >= HITS) //Öܲ¨²ÉÑùÍê³É
- {
- SampleCounter = 0;
- //...
- }
- } //if(SampleCounter < HITS)
- else
- SampleCounter = 0;
- DMA_ClearITPendingBit(DMA2_Stream0,DMA_IT_TCIF0);
- }//end if(DMA_GetITStatus(DMA1_IT_TC1))
- }
复制代码 程序预想的流程是:采用ADC1、ADC2规则组对多个通道进行数据采样,用DMA保存采样数据,规则组中所有通道采样完成后备份数据后续处理。
调用ADC_DMA_TIM2Config函数初始化后,程序运行仿真,只能进入一次中断服务程序DMA2_Stream0_IRQHandler,SR寄存器的OVR位被置位,查不出是什么原因。求指点。。。。。。。。。
|
最新进展,溢出是由DMA2_Stream0->M0AR = (u32)&DMABuf[DMABufFlag][0];引起的,好像不能这样操作,DMA改为双缓存模式后,不会溢出,能连续进入DMA2中断服务程序了,DMA初始化函数增加如下代码:
但是缓冲区接收不到采样数据。缓存区指示标志CT也不会变化。