本帖最后由 吹水唔抹嘴 于 2017-10-17 16:42 编辑
ADC3的通道3即PA3使用的DMA是DMA2的通道5,只采集PA3一路ADC,2ms采一次,以下是初始化,却一直进入不了DMA2_Channel4_5_IRQHandler中断。求帮忙看下[mw_shl_code=c,true]void ADC_tog_Config(void)
{
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable ADC1, DMA2 and GPIO clocks ****************************************/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC3| RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO , ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
/* Configure PA.01 (ADC Channel1) as analog input -------------------------*/
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_3 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC3, ENABLE);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC3, DISABLE);
RCC_ADCCLKConfig(RCC_PCLK2_Div6); //设置ADC分频因子6 72M/6=12,ADC最大时间不能超过14M
RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC3, ENABLE);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC3, DISABLE);
ADC_DeInit(ADC3); //复位ADC1,将外设 ADC1 的全部寄存器重设为缺省值
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //ADC工作模式:ADC1和ADC2工作在独立模式
ADC_InitStructure.ADC_ScanConvMode = ENABLE; //多通道扫描模式
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //模数转换工作在单次转换模式
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC3; //配置定时器1第3通道CC3作为ADC的转换触发源
// ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //ADC数据右对齐
ADC_InitStructure.ADC_NbrOfChannel = 1; //顺序进行规则转换的ADC通道的数目
ADC_Init(ADC3, &ADC_InitStructure); //根据ADC_InitStruct中指定的参数初始化外设ADCx的寄存器
ADC_DMACmd(ADC3, ENABLE);
ADC_ExternalTrigConvCmd(ADC3, ENABLE);
ADC_RegularChannelConfig(ADC3, ADC_Channel_3, 1, ADC_SampleTime_239Cycles5);//Ib
ADC_Cmd(ADC3, ENABLE); //使能指定的ADC1
ADC_ResetCalibration(ADC3); //使能复位校准
while(ADC_GetResetCalibrationStatus(ADC3)); //等待复位校准结束
ADC_StartCalibration(ADC3); //开启AD校准
while(ADC_GetCalibrationStatus(ADC3)); //等待校准结束
/* Enable ADC1 DMA */
// ADC_DMACmd(ADC1, ENABLE);
/* Enable ADC1 */
}
//DMA配置
void DMA_Config(void)
{
DMA_InitTypeDef DMA_InitStructure;
/* Enable ADC1, DMA2 and GPIO clocks ****************************************/
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2 , ENABLE);
DMA_DeInit(DMA2_Channel5); //将DMA的通道1寄存器重设为缺省值
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC3_DR_ADDRESS;//ADC1_DR_Address; //DMA外设ADC基地址
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)ADC1ConvertedValue; //DMA内存基地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //数据传输方向,从内存读取发送到外设
DMA_InitStructure.DMA_BufferSize = 500; //DMA通道的DMA缓存的大小
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_InitStructure.DMA_Mode = DMA_Mode_Normal;//;
DMA_InitStructure.DMA_Priority = DMA_Priority_High; //DMA通道 x拥有高优先级
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //DMA通道x没有设置为内存到内存传输
DMA_Init(DMA2_Channel5, &DMA_InitStructure); //根据DMA_InitStruct中指定的参数初始化DMA的通道USART1_Tx_DMA_Channel所标识的寄存器
DMA_Cmd(DMA2_Channel5, ENABLE);//使能DMA
DMA_ITConfig(DMA2_Channel5,DMA_IT_TC,ENABLE); //开DMA2_Stream0传输完成中断
}
//ADC中断配置
void DMA_NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel =DMA2_Channel4_5_IRQn;// DMA1_Channel1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // Enable the DMA Interrupt
NVIC_Init(&NVIC_InitStructure);
}
//TIM1做PWM输出
//--@nPrescaler 分频参数,以系统频率72M为主频。72M/nPrescaler
//--@nTimerPeriod 计数计满重装值
//--例如TIM1_Config(7200,60000 );则计数时间片为0.1毫秒,每6秒溢出一次
void TIM1_Config(u16 nPrescaler,u16 nTimerPeriod)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
uint32_t ccr1;
ccr1 = nTimerPeriod / 2;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
/* Time Base configuration */
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period = nTimerPeriod-1;
TIM_TimeBaseStructure.TIM_Prescaler = nPrescaler-1;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
/* TIM1 channel1 configuration in PWM mode */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = ccr1;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_Cmd(TIM1,ENABLE); //使能TIMx外设
TIM_CtrlPWMOutputs(TIM1,ENABLE); //Enables the TIM peripheral Main Outputs.
}
void CurrrentAndVoltageTest_init(void)
{
ADC_tog_Config();
TIM1_Config(7200,20);
DMA_Config();
DMA_NVIC_Config();
}
u8 dma_okflag=0;
void DMA1_Channel1_IRQHandler(void)
{
// OSIntEnter();
DMA_ClearFlag(DMA1_FLAG_TC1);
// if(dma_okflag==0)
dma_okflag=1;
DMA_ITConfig(DMA1_Channel1,DMA_IT_TC,DISABLE); //开DMA2_Stream0传输完成中断
// OSSemPost(ADC_resultSem);
// OSIntExit();
}
void DMA2_Channel4_5_IRQHandler(void)
{
// OSIntEnter();
static int str=0;
str++;
// DMA_ClearFlag(DMA2_FLAG_TC1);
DMA_ClearFlag(DMA2_FLAG_GL5 );
// if(dma_okflag==0)
dma_okflag=1;
// DMA_ITConfig(DMA2_Channel5,DMA_IT_TC,DISABLE); //开DMA2_Stream0传输完成中断
USART1_SendPack(&str,1);
// OSSemPost(ADC_resultSem);
// OSIntExit();
}[/mw_shl_code]
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>