这几天用STM32F051做ADC采样处理,采用的TIM15触发ADC 12个通道,10ms触发一次。但是在DMA中断中却无法进入中断,望做过的人帮我看看。在此谢过!
void GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);/* GPIOA Clocks enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);/* GPIOB Clocks enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);/* GPIOC Clocks enable */
//ADC
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2| GPIO_Pin_3 | GPIO_Pin_5| GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
void ADC_Config(void)
{
ADC_InitTypeDef ADC_InitStruct;
/* Enable ADC clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
/* Initialize ADC structure */
ADC_StructInit(&ADC_InitStruct);
/* Configure the ADC1 in continous mode withe a resolutuion equal to 12 bits */
ADC_InitStruct.ADC_Resolution = ADC_Resolution_12b; //配置ADC 的转化分辨率
ADC_InitStruct.ADC_ContinuousConvMode = DISABLE; //配置选择连续采样或单次采样
ADC_InitStruct.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising; //上升沿触发
ADC_InitStruct.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T15_TRGO;
ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right; //设置ADC 是左对齐或者右对齐
ADC_InitStruct.ADC_ScanDirection = ADC_ScanDirection_Upward; //设置ADC 扫描方向 AD0--ADC16
ADC_Init(ADC1, &ADC_InitStruct);
/* Convert the ADC1 Vref with 55.5 Cycles as sampling time */
ADC_ChannelConfig(ADC1, ADC_Channel_0 | ADC_Channel_1 | ADC_Channel_2 | ADC_Channel_3, ADC_SampleTime_55_5Cycles);
ADC_ChannelConfig(ADC1, ADC_Channel_5 | ADC_Channel_6 | ADC_Channel_7 | ADC_Channel_8, ADC_SampleTime_55_5Cycles);
ADC_ChannelConfig(ADC1, ADC_Channel_9 | ADC_Channel_13 | ADC_Channel_14 | ADC_Channel_15, ADC_SampleTime_55_5Cycles);
// ADC Calibration
ADC_GetCalibrationFactor(ADC1);
ADC_DMACmd(ADC1, ENABLE);
// Enable ADC1
ADC_Cmd(ADC1, ENABLE);
// Wait the ADCEN falg
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN));
// ADC1 regular Software Start Conv
ADC_StartOfConversion(ADC1);
}
void ADC_DMA_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
//DMA for ADC配置结构
DMA_InitTypeDef DMA_ADC_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
//ADC DMA设置
DMA_DeInit(ADC_DMAn);
DMA_ADC_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR;
DMA_ADC_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&ADC_hex[0][0];
DMA_ADC_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_ADC_InitStructure.DMA_BufferSize = ADC_SIZE; //ADC通道数量
DMA_ADC_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_ADC_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_ADC_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //单ADC
DMA_ADC_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; //单ADC
DMA_ADC_InitStructure.DMA_Mode = DMA_Mode_Circular; //配合ADC连续的循环模式
DMA_ADC_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_ADC_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(ADC_DMAn, &DMA_ADC_InitStructure);
//Enable DMA ADC_DMAn
DMA_Cmd(ADC_DMAn, ENABLE);
//Enable ADC1 DMA
ADC_DMACmd(ADC1, ENABLE);
//DMA for ADC 中断使能
// Enable the DMA gloabal Interrupt
DMA_ITConfig(ADC_DMAn, DMA_IT_TC, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void TIM15_Config(void)
{
uint32_t Period = 479;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM15, ENABLE);
TIM_DeInit(TIM15);//复位TIM15定时器
//X us定时中断
TIM_TimeBaseStructure.TIM_Period = Period; //最大计数值0xffff
TIM_TimeBaseStructure.TIM_Prescaler = 999;//分频 10ms
TIM_TimeBaseStructure.TIM_ClockDivision = 0;//分频因子 //CK_CNT = FCLK /(TIM_Prescaler+1)
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM15, &TIM_TimeBaseStructure);
//PWM1 Mode configuration: Channe15
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = (Period>>1);//(占空)
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM15, &TIM_OCInitStructure);
// TIM_OC1PreloadConfig(TIM15, TIM_OCPreload_Disable);//关闭预装载
// TIM_ARRPreloadConfig(TIM15, ENABLE); //使能自动重装
TIM_SelectOutputTrigger(TIM15, TIM_TRGOSource_Update);
TIM_SelectMasterSlaveMode(TIM15, TIM_MasterSlaveMode_Enable);
/* Main Output Enable */
TIM_CtrlPWMOutputs(TIM15, ENABLE);
}
void ADC_DMA_TIM15Config(void)
{
//TIM15 Configuration
TIM15_Config();
// ADC1 Configuration
ADC_Config();
// ADC_DMA Configuration
ADC_DMA_Config();
//外部时钟触发ADC
TIM_Cmd(TIM15, ENABLE);
}
|