新手入门
- 积分
- 11
- 金钱
- 11
- 注册时间
- 2022-1-25
- 在线时间
- 4 小时
|
需要做一两路独立adc采样,第一路用adc和dma1没有问题,第二路用adc3+dma2就出了问题,进不去中断。后来根据adc1代码改写了一个单独adc3+dma2的代码单独拿来用,还是进不去DMA2_Channel4_5_IRQHandler,网上找了可能的几个问题,都没解决,没有办法只能求助各位大佬能否帮我看看是配置的哪个步骤出了问题。
- #include "adc.h"
- u16 ADC_Value[NPT];
- //cndtr:数据传输量
- void MYDMA_Config(u16 cndtr)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- DMA_InitTypeDef DMA_InitStructure;
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE); //使能DMA时钟
-
- DMA_DeInit(DMA2_Channel5); //重设DMA为缺省值
-
- DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC3->DR; //外设地址
- DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)ADC_Value; //存储器地址
- DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //外设到存储器的传输模式
- DMA_InitStructure.DMA_BufferSize = cndtr; //数据量为1(应该为cndtr参数)
- DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //
- DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //存储器地址自增
- DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord ; //16位!!!特别注意
- DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord ; //16位!!!特别注意
- DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //循环模式DMA_Mode_Circular
- DMA_InitStructure.DMA_Priority = DMA_Priority_High; //优先级高
- DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //(内存到内存禁止)
-
- DMA_ITConfig(DMA2_Channel5,DMA_IT_TC, ENABLE); //使能传输完成中断
-
- //NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);设置在主函数
- NVIC_InitStructure.NVIC_IRQChannel = DMA2_Channel4_5_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- DMA_Init(DMA2_Channel5,&DMA_InitStructure); //初始化
- DMA_SetCurrDataCounter(DMA2_Channel5,cndtr);//设置数据量(应该为cndtr参数,若有其它需要改变即
- //可,我这里是为了直观)
- DMA_Cmd(DMA2_Channel5, ENABLE);
- }
- //ADC初始化
- void Adc_Init(void)
- {
- ADC_InitTypeDef ADC_InitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |RCC_APB2Periph_ADC3 , ENABLE ); //相关时钟使能
-
-
- RCC_ADCCLKConfig(RCC_PCLK2_Div6); //设置分频因子,ADC时间最大不过14M
-
- //PA1寄存器初始化
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //模拟输入模式
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
-
- ADC_DeInit(ADC3);
-
- ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //独立模式,不与其它ADC协作
- ADC_InitStructure.ADC_ScanConvMode = DISABLE; //关闭多通道模式
- ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //关闭连续转换模式
- ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T2_CC3;//使用外部触发模式
- ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //数据右对齐
- ADC_InitStructure.ADC_NbrOfChannel = 1; //
- ADC_Init(ADC3, &ADC_InitStructure); //
-
- ADC_RegularChannelConfig(ADC3, ADC_Channel_1, 1, ADC_SampleTime_239Cycles5 );
- //配置采样时间
- ADC_Cmd(ADC3, ENABLE); //使能指定ADC
- ADC_DMACmd(ADC3, ENABLE);//使能ADC - DMA
-
- ADC_ResetCalibration(ADC3); //复位校准
- while(ADC_GetResetCalibrationStatus(ADC3)); //等待复位校准
- ADC_StartCalibration(ADC3); //开启AD校准
- while(ADC_GetCalibrationStatus(ADC3)); //等待结束
-
- ADC_ExternalTrigConvCmd(ADC3, ENABLE); //设置外部触发模式使能
- }
- void TIM2_Init( u32 Fre )
- {
-
- u32 MaxData;
- u16 div=1;
-
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //时钟使能
- while( (SystemCoreClock/2/Fre/div)>65535 )
- {
- div++;
- }
- MaxData = SystemCoreClock/2/Fre/div - 1;
- // printf("%d,%d\r\n",MaxData,div);
- //定时器TIM2初始化
- TIM_TimeBaseStructure.TIM_Period = MaxData; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
- TIM_TimeBaseStructure.TIM_Prescaler =div; //设置用来作为TIMx时钟频率除数的预分频值
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
- TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx的时间基数单位
-
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择定时器模式:TIM脉冲宽度调制模式1
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
- TIM_OCInitStructure.TIM_Pulse = MaxData/2;
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //输出极性:TIM输出比较极性低
- TIM_OC2Init(TIM2, & TIM_OCInitStructure); //初始化外设TIM2_CH2
-
- TIM_Cmd(TIM2, ENABLE); //使能TIMx
- TIM_CtrlPWMOutputs(TIM2, ENABLE);
- }
- void all_init(){
- TIM2_Init(100000);
- Adc_Init();
- MYDMA_Config(NPT);
- }
- void DMA2_Channel4_5_IRQHandler(void)
- {
-
- u16 i=0;
- for(i=0;i<NPT;i++){
- printf("%d\r\n",ADC_Value[i]);
- }
- if(DMA_GetITStatus(DMA2_IT_TC1)!=RESET){
-
- // for(i=0;i<NPT;i++){
- // printf("%d\r\n",ADC_Value[i]);
- // }
-
- DMA_ClearITPendingBit(DMA2_IT_TC1);
- }
- }
复制代码
|
|