cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F3 Injected ADC not working on CCR4

FaintSmile
Associate II

Hello

I am trying to trigger adc with injected mode using TIM1 CCR 4 event.When i try to use TIM1 upgrade to generate injected ADC interrupt it worked.I used event 0 for adc injected.But when i try to use TIM1 CCR 4 event to trigger injected ADC it is not working.Here is my code.

void TIM1_Init(void)

{

   TIM_OCInitTypeDef            TIM_OCInitStruct;

   TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;

   NVIC_InitTypeDef            NVICStruct;

   RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);

   TIM_TimeBaseInitStruct.TIM_ClockDivision         = TIM_CKD_DIV1;

   TIM_TimeBaseInitStruct.TIM_CounterMode          = TIM_CounterMode_Up;

   TIM_TimeBaseInitStruct.TIM_Period                     = 63;

   TIM_TimeBaseInitStruct.TIM_Prescaler               = 99;

   TIM_TimeBaseInitStruct.TIM_RepetitionCounter   = 0;

   TIM_TimeBaseInit(TIM1,&TIM_TimeBaseInitStruct);

   TIM_OCInitStruct.TIM_OCMode                              = TIM_OCMode_PWM1;

   TIM_OCInitStruct.TIM_OCIdleState                     = TIM_OCIdleState_Reset;

   TIM_OCInitStruct.TIM_OCPolarity                        = TIM_OCPolarity_High;

   TIM_OCInitStruct.TIM_OCNIdleState                     = TIM_OCNIdleState_Reset;

   TIM_OCInitStruct.TIM_OCNPolarity                     = TIM_OCNPolarity_High;

   TIM_OCInitStruct.TIM_OutputState                    = TIM_OutputState_Enable;

   TIM_OCInitStruct.TIM_OutputNState                     = TIM_OutputNState_Enable;

   TIM_OCInitStruct.TIM_Pulse                              = 20;

   TIM_OC4Init(TIM1,&TIM_OCInitStruct);

   TIM_OC4PreloadConfig(TIM1,TIM_OCPreload_Enable);

   NVICStruct.NVIC_IRQChannel                    = TIM1_UP_TIM16_IRQn;

 NVICStruct.NVIC_IRQChannelPreemptionPriority  = 1;

   NVICStruct.NVIC_IRQChannelSubPriority               = 0;

   NVICStruct.NVIC_IRQChannelCmd                           = ENABLE;

 NVIC_Init(&NVICStruct);

   TIM_SelectMasterSlaveMode(TIM1,TIM_MasterSlaveMode_Enable);

//if i try to use TIM_TRGOSource_Update as trigger it works.

   TIM_SelectOutputTrigger(TIM1,TIM_TRGOSource_OC4Ref);

   TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE);

   TIM_CtrlPWMOutputs(TIM1,ENABLE);

   TIM_Cmd(TIM1,ENABLE);

}

void ADC_Injected_Init(void)

{

   GPIO_InitTypeDef GPIO_InitStructure;

   ADC_InitTypeDef ADC_InitStructure;

   ADC_CommonInitTypeDef ADC_CommonInitStruct;

   NVIC_InitTypeDef NVIC_InitStruct;

   ADC_InjectedInitTypeDef ADC_InjectedInitStruct;

 /* Enable ADC1 clock */

 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_ADC12, ENABLE);

      /* Configure the ADC clock */

 RCC_ADCCLKConfig(RCC_ADC12PLLCLK_Div2);

   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2;

 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;

 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;

 GPIO_Init(GPIOA, &GPIO_InitStructure);

   ADC_CommonInitStruct.ADC_Mode = ADC_Mode_Independent;

   ADC_CommonInitStruct.ADC_TwoSamplingDelay =0x0;

   ADC_CommonInitStruct.ADC_DMAAccessMode =ADC_DMAAccessMode_Disabled;

   ADC_CommonInitStruct.ADC_Clock = ADC_Clock_AsynClkMode;

   ADC_CommonInit(ADC1,&ADC_CommonInitStruct);

   ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;

 ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;

 ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;

 ADC_InitStructure.ADC_NbrOfRegChannel = 1;

   ADC_Init(ADC1, &ADC_InitStructure);

   NVIC_InitStruct.NVIC_IRQChannel = ADC1_2_IRQn;

   NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;

   NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;

   NVIC_InitStruct.NVIC_IRQChannelSubPriority = 2;

   NVIC_Init(&NVIC_InitStruct);

   //if i try event_0 with TIM1 upgrade it works event1 is for TIM1_CC4 event

// table 43 reference manual

   ADC_InjectedInitStruct.ADC_ExternalTrigInjecConvEvent = ADC_ExternalTrigInjecConvEvent_1;

   ADC_InjectedInitStruct.ADC_ExternalTrigInjecEventEdge = ADC_ExternalTrigInjecEventEdge_BothEdge;

   ADC_InjectedInitStruct.ADC_InjecSequence1 = ADC_InjectedChannel_1;

   ADC_InjectedInitStruct.ADC_NbrOfInjecChannel = 1;

   ADC_InjectedInit(ADC1,&ADC_InjectedInitStruct);

 ADC_InjectedChannelSampleTimeConfig(ADC1, ADC_InjectedChannel_1, ADC_SampleTime_1Cycles5);

   ADC_InjectedDiscModeCmd(ADC1,ENABLE);

   ADC_AutoInjectedConvCmd(ADC1,ENABLE);

   ADC_StartInjectedConversion(ADC1);

   ADC_ITConfig(ADC1,ADC_IT_JEOC,ENABLE);

   ADC_Cmd(ADC1,ENABLE);

 /* wait for ADRDY */

 while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_RDY));

   ADC_StartConversion(ADC1);

}

I can see the PWM signal and the upgrade interrupt of TIM1 is working.But i can not enter ADC interrupt if i try to use the code above to trigger ADC Injected interrupt with TIM1 CCR4 event any idea why ?

2 REPLIES 2

Event 0 is for the TRGO setting, not UPDATE, Event 1 for OC4REF

Code doesn't look unreasonable, would take some debugging to understand the issue. Would probably start by pushing CH4 to a pin in toggle mode, and confirm it is ticking.

I probably wouldn't use the both edges mode, nor TRGO and master/slave settings.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
tcanl.1
Associate

Have you slove this?I want to use timer ccr2​ event to tirgger the adc,it is fail too.