2013-08-04 09:52 AM
I am trying to get ADC regular dual mode with external trigger on timer 1 to work. I have read a lot of posts on this forum about ADC dual mode and some on external triggering, but I still can't get it to work. When I instead use software to start ADC conversions it all works as it should. So ADC conversions are not getting triggered by the timer interrupt i suspect.
#define ADC_DATA_BUFFER_SIZE 7200
__IO uint32_t ADCdata[ADC_DATA_BUFFER_SIZE+160];
void main(void)
{
ADC_IQSamplingInitialization();
TIMER1_Enable();
while(1)
{
}
}
void ADC_IQSamplingInitialization(void)
{
NVIC_DmaAdcInitialization();
DMA_AdcInitialization();
ADC_Adc1Initialization();
ADC_Adc2Initialization();
//Enable timer 1 compare as external trigger
ADC_ExternalTrigConvCmd(ADC1, ENABLE);
ADC_ExternalTrigConvCmd(ADC2, ENABLE);
//Enable ADC2
ADC_Cmd(ADC2, ENABLE);
//Enable ADC1
ADC_Cmd(ADC1, ENABLE);
}
void NVIC_DmaAdcInitialization(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
//Register with Nested Vectored Interrupt Controller (NVIC).
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0A;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0A;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
//Add DMA1_Channel1_IRQn to NVIC
NVIC_Init(&NVIC_InitStructure);
}
void DMA_AdcInitialization(void)
{
DMA_InitTypeDef DMA_InitStructure;
//Enable DMA1 clock
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
//Configure DMA1 CH1
DMA_Cmd(DMA1_Channel1, DISABLE);
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) ADC_DATA_REGISTER_ADDRESS;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t) &ADCdata[0];
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = ADC_DATA_BUFFER_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_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
//Initialize DMA1 CH1
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
//Enable DMA Transfer Complete interrupt
DMA_ITConfig(DMA1_Channel1, DMA_IT_TC, ENABLE);
//Enable DMA1 CH1
DMA_Cmd(DMA1_Channel1, ENABLE);
}
void ADC_Adc1Initialization(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
//ADC clk = APB2 clk / 6 = 72MHz/6 = 12MHz. MAX 14MHz.
RCC_ADCCLKConfig(RCC_PCLK2_Div6);
RCC_APB2PeriphClockCmd( (RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOC) , ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOC, &GPIO_InitStructure);
ADC_Cmd(ADC1, DISABLE);
ADC_DeInit(ADC1);
ADC_InitStructure.ADC_Mode = ADC_Mode_RegSimult;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_7Cycles5);
ADC_DMACmd(ADC1, ENABLE);
}
void ADC_Adc2Initialization(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
//ADC clk = APB2 clk / 6 = 72MHz/6 = 12MHz. MAX 14MHz.
RCC_ADCCLKConfig(RCC_PCLK2_Div6);
RCC_APB2PeriphClockCmd( (RCC_APB2Periph_ADC2 | RCC_APB2Periph_GPIOA) , ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
ADC_Cmd(ADC2, DISABLE);
ADC_DeInit(ADC2);
ADC_InitStructure.ADC_Mode = ADC_Mode_RegSimult;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC2, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC2, ADC_Channel_3, 1, ADC_SampleTime_7Cycles5);
}
void TIMER1_Init(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
//Enable Timer 1 clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
//Base
TIM_TimeBaseStructInit(&TIM_TimeBaseInitStructure);
TIM_TimeBaseInitStructure.TIM_Prescaler = 0;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_Period = 60167;
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseInitStructure);
TIM_ARRPreloadConfig(TIM1, ENABLE);
//Register with Nested Vectored Interrupt Controller (NVIC).
NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0B;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0B;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//Clear timer
TIM_SetCounter(TIM1, 0);
//Interrupt
TIM_ITConfig(TIM1, TIM_IT_CC1, ENABLE);
}
void TIMER1_Enable(void)
{
//Clear timer
TIM_SetCounter(TIM1, 0);
//Enable timer
TIM_Cmd(TIM1, ENABLE);
}
void TIMER1_Disable(void)
{
//Disable timer
TIM_Cmd(TIM1, DISABLE);
}
void TIM1_CC_IRQHandler(void)
{
//Clear timer 1 compare interrupt flag
TIM_ClearITPendingBit(TIM1, TIM_IT_CC1);
}
void DMA1_Channel1_IRQHandler(void)
{
//Clear DMA Transfer complete interrupt flag
DMA_ClearITPendingBit(DMA1_IT_TC1);
//Light LED2
LED(LED2, ON);
}
I am trying to get ADC regular dual mode with external trigger on timer 1 to work. I have read a lot of posts on this forum about ADC dual mode and some on external triggering, but I still can't get it to work. When I instead use software to start ADC conversions it all works as it should. So ADC conversions are not getting triggered by the timer interrupt i suspect.
#adc-dma-tim-dualmode2013-08-04 06:23 PM
You want to configure channel 1 settings of the timer to use it for generating a trigger.
You don't be interrupting with the high periodicity of CC1, there's no point to doing this. For TIM1 or 8 you're likely to need to enable the PWM output. For non-circular DMA you'll have to repeatedly restart it.2013-08-07 11:40 AM
You are absolutly right! I forgot to configure timer 1 channel 1 and enabling PWM output. Thank you clive1 =)