AnsweredAssumed Answered

EOC flag clears when entering ISR

Question asked by ionut.fasola on Nov 4, 2016
Latest reply on Nov 4, 2016 by obid.matic
When I start the ADC conversion the code jumps to ADC1_COMP_IRQHandler
In ADC isr the EOC flag clears after first assembly instruction of isr code. So when I check for EOC flag if is set...the flag is already zero. 

A workaround would be to not check that flag and read the ADC anyway, if I have just the EOC event as IRQ source enabled. Is EOC flag clearing by hardware? How to prevent it?

I checked the code w/o 
      ADC_ContinuousModeCmd(ADC1,DISABLE);
      ADC_OverrunModeCmd(ADC1,ENABLE);
      ADC_WaitModeCmd(ADC1,ENABLE);
      ADC_DiscModeCmd(ADC1,ENABLE);
      ADC_AutoPowerOffCmd(ADC1,ENABLE);



Code:
#define D1_PORT GPIOA
#define D2_PORT GPIOB
#define D3_PORT GPIOB
#define D4_PORT GPIOB
#define DP_PORT GPIOB
 
 
#define D1_Pin GPIO_Pin_12
#define D2_Pin GPIO_Pin_4
#define D3_Pin GPIO_Pin_3
#define D4_Pin GPIO_Pin_12
 
 
#define  SegB_Port GPIOA
#define  SegD_Port GPIOA
#define  SegE_Port GPIOA
 
 
#define  SegC_Port GPIOB
#define  SegG_Port GPIOB
 
 
#define  SegA_Port GPIOF
#define  SegF_Port GPIOF
 
 
#define SegA_Pin GPIO_Pin_6
#define SegB_Pin GPIO_Pin_15
#define SegC_Pin GPIO_Pin_14
#define SegD_Pin GPIO_Pin_8
#define SegE_Pin GPIO_Pin_11
#define SegF_Pin GPIO_Pin_7
#define SegG_Pin GPIO_Pin_13
#define DP_Pin GPIO_Pin_15
 
 
#define RelayPort GPIOB
#define K1Pin GPIO_Pin_2
#define K2Pin GPIO_Pin_10
 
 
#define SW1Pin GPIO_Pin_7
#define SW2Pin GPIO_Pin_6
#define SW3Pin GPIO_Pin_4
#define SWPort GPIOB
 
 
#define UserbtnPort GPIOA
#define UserbtnPin GPIO_Pin_0
 
 
int main(void)
{
    gpio_config();
    adc_config();
 
    ADC_StartOfConversion(ADC1);
  while (1)
  {
  }
}
 
void ADC1_COMP_IRQHandler(void)
{
    if(ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC)==SET)
        {
            DCVoltageValues[DCVoltageValuesIdx]=ADC_GetConversionValue(ADC1);
            DCVoltageValuesIdx++;
        }
 
 
    ADC_ClearFlag(ADC1,ADC_FLAG_EOC|ADC_FLAG_ADRDY|ADC_FLAG_EOSMP|ADC_FLAG_EOSEQ|ADC_FLAG_OVR);
    ADC_ClearITPendingBit(ADC1,ADC_IT_EOC);
    ADC_ClearITPendingBit(ADC1,ADC_IT_EOSMP);
    ADC_ClearITPendingBit(ADC1,ADC_IT_EOSEQ);
    ADC_ClearITPendingBit(ADC1,ADC_IT_OVR);
    ADC_ClearITPendingBit(ADC1,ADC_IT_AWD);
 
 
}
 
 
 
 
 
void gpio_config(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    EXTI_InitTypeDef EXTI_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
 
 
    GPIO_StructInit(&GPIO_InitStructure);
 
 
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA|RCC_AHBPeriph_GPIOB|RCC_AHBPeriph_GPIOF,ENABLE);
 
 
    //RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC,ENABLE);
 
 
    /* Enable SYSCFG clock for EXTI controller */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
 
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
      GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
      GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_DOWN;
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3;//50MHz
 
     GPIO_InitStructure.GPIO_Pin = D1_Pin|SegB_Pin|SegD_Pin|SegE_Pin;
     GPIO_Init(GPIOA, &GPIO_InitStructure);
 
     GPIO_InitStructure.GPIO_Pin = D2_Pin|D3_Pin|D4_Pin|SegC_Pin|SegG_Pin;
     GPIO_Init(GPIOB, &GPIO_InitStructure);
 
     GPIO_InitStructure.GPIO_Pin = SegA_Pin|SegF_Pin;
     GPIO_Init(GPIOF, &GPIO_InitStructure);
 
     //led 072
    // GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
    // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
    // GPIO_Init(GPIOC, &GPIO_InitStructure);
 
          //K1 , K2
     GPIO_InitStructure.GPIO_Pin = K1Pin|K2Pin;
     GPIO_Init(RelayPort, &GPIO_InitStructure);
 
     GPIO_InitStructure.GPIO_Pin= SW1Pin|SW2Pin|SW3Pin;
     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
     GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
     GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3;//50MHz
 
     GPIO_Init(SWPort, &GPIO_InitStructure);
 
     //buton 072
    // GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;//are pull up pe placa
    // GPIO_InitStructure.GPIO_Pin= UserbtnPin;
    // GPIO_Init(UserbtnPort, &GPIO_InitStructure);
 
     /* Configure EXTI7 line */
    EXTI_InitStructure.EXTI_Line = EXTI_Line7;
    EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
    EXTI_InitStructure.EXTI_LineCmd = ENABLE;
    EXTI_Init(&EXTI_InitStructure);
 
          /* Configure EXTI6 line */
    EXTI_InitStructure.EXTI_Line = EXTI_Line6;
    EXTI_Init(&EXTI_InitStructure);
 
          /* Configure EXTI5 line */
    EXTI_InitStructure.EXTI_Line = EXTI_Line5;
    EXTI_Init(&EXTI_InitStructure);
 
    // buton 072
    //EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
    //EXTI_InitStructure.EXTI_Line = EXTI_Line0;
    //EXTI_Init(&EXTI_InitStructure);
 
     /* Enable and set EXTI4..15 Interrupt to the lowest priority */
     NVIC_InitStructure.NVIC_IRQChannel = EXTI4_15_IRQn;
     NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
     //NVIC_Init(&NVIC_InitStructure);
 
     //buton 072
    // NVIC_InitStructure.NVIC_IRQChannel = EXTI0_1_IRQn;
    // NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
    // NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    // NVIC_Init(&NVIC_InitStructure);
}
 
void adc_config(void)
{
      ADC_InitTypeDef     ADC_InitStructure;
      GPIO_InitTypeDef    GPIO_InitStructure;
      NVIC_InitTypeDef NVIC_InitStructure;
 
 
      /* GPIOA Periph clock enable */
      RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
 
 
      //ADC Clock Source Selection : 6MHz
      RCC_ADCCLKConfig(RCC_ADCCLK_PCLK_Div2);
 
 
      /* ADC1 Periph clock enable */
      RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
 
 
      /* Configure ADC Channel1 1..4 as analog input */
 
 
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3 ;
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
      GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
      GPIO_Init(GPIOA, &GPIO_InitStructure);
 
 
      /* ADCs DeInit */
      ADC_DeInit(ADC1);
 
 
      /* Initialize ADC structure */
      ADC_StructInit(&ADC_InitStructure);
 
 
      /* Configure the ADC1 in continuous mode with a resolution equal to 12 bits  */
      ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
      ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
     ADC_InitStructure.ADC_ExternalTrigConv=ADC_ExternalTrigConv_T3_TRGO;//T3 trgo=update event
      ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
      ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
      ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward;
      ADC_Init(ADC1, &ADC_InitStructure);
 
      ADC_ChannelConfig(ADC1, ADC_Channel_1 , ADC_SampleTime_239_5Cycles);
      /* ADC Calibration */
      ADC_GetCalibrationFactor(ADC1);
 
      /* Enable the ADC peripheral */
      ADC_Cmd(ADC1, ENABLE);
 
 
      /* Wait the ADRDY flag */
      while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADRDY));
 
 
      ADC_TempSensorCmd(ENABLE);
      ADC_VrefintCmd(ENABLE);
      ADC_VbatCmd(ENABLE);
 
 
      ADC_ContinuousModeCmd(ADC1,DISABLE);
      ADC_OverrunModeCmd(ADC1,ENABLE);
      ADC_WaitModeCmd(ADC1,ENABLE);
      ADC_DiscModeCmd(ADC1,ENABLE);
      ADC_AutoPowerOffCmd(ADC1,ENABLE);
 
 
      ADC_ITConfig(ADC1,ADC_IT_EOC,ENABLE);
 
 
      /* Enable the ADC gloabal Interrupt */
      NVIC_InitStructure.NVIC_IRQChannel = ADC1_IRQn;
      NVIC_InitStructure.NVIC_IRQChannelPriority = 5;
      NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
      NVIC_Init(&NVIC_InitStructure);
}

Outcomes