AnsweredAssumed Answered

Problem with EOC interrupt for ADC : STM32F3

Question asked by regnier.pierre on Jun 19, 2014
Latest reply on Jun 20, 2014 by baird.hal.001
Hello,
i am using a STM32F3Discovery and i want to use the ADC.
When the ADC finishes a conversion, i want to use EOC interupt to get the value.
Thanks for help! :)

 

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#define BSRR_VAL 0xC000
uint16_t interrupt = 0;
/** @addtogroup STM32F3_Discovery_Peripheral_Examples
  * @{
  */

/** @addtogroup ADC_Example
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
__IO uint16_t  ADC1ConvertedVoltage = 0, calibration_value = 0;
__IO uint32_t TimingDelay = 0;
 __IO uint16_t  ADC1ConvertedValue = 0;
__IO uint32_t compteur = 0;
uint16_t TimerPeriod = 0;

 

ADC_InitTypeDef       ADC_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
GPIO_InitTypeDef      GPIO_InitStructure;
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

#ifdef __c
extern "C" {
#endif 
  void ADC1_IRQHandler();
 
#ifdef __c
}
#endif

/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */

int main(void){
 

ADC1_config();

  do{
 
  ADC_StartConversion(ADC1);
  while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
  /*
  ADC1ConvertedValue= ADC_GetConversionValue(ADC1);
  printf(" %d\n",ADC1ConvertedValue);
  */
 /* if(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == SET){
    printf("drapeau levé\n");
  }*/
  }while(1);
}

void ADC1_config(){
  ADC_ITConfig(ADC1, ADC_IT_EOC, ENABLE);
  NVIC_InitTypeDef NVIC_InitStructure;
 
  NVIC_InitStructure.NVIC_IRQChannel = ADC1_2_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
  /* Configure the ADC clock */
  RCC_ADCCLKConfig(RCC_ADC12PLLCLK_Div128);
 
  /* Enable ADC1 clock */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_ADC12, ENABLE);
     
 
  /* Setup SysTick Timer for 1 usec interrupts  */
  if (SysTick_Config(SystemCoreClock / 1000000))
  {
   
    while (1)
    {
    }
  }
 
  /* ADC Channel configuration */
  /* GPIOC Periph clock enable */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);

  /* Configure ADC Channel7 as analog input */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
  GPIO_Init(GPIOC, &GPIO_InitStructure);
  ADC_StructInit(&ADC_InitStructure);

    /* GPIOC Periph clock enable */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOE, ENABLE);

  /* Configure ADC Channel7 as analog input */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOE, &GPIO_InitStructure);
 
 
 
  /* Calibration procedure */ 
  ADC_VoltageRegulatorCmd(ADC1, ENABLE);
 
  /* Insert delay equal to 10 µs */
  Delay(100);
 
  ADC_SelectCalibrationMode(ADC1, ADC_CalibrationMode_Single);
  ADC_StartCalibration(ADC1);
 
  while(ADC_GetCalibrationStatus(ADC1) != RESET );
  calibration_value = ADC_GetCalibrationValue(ADC1);
  Delay(100);
 
  ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;                                                                   
  ADC_CommonInitStructure.ADC_Clock = ADC_Clock_AsynClkMode;                   
  ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;            
  ADC_CommonInitStructure.ADC_DMAMode = ADC_DMAMode_OneShot;                 
  ADC_CommonInitStructure.ADC_TwoSamplingDelay = 0;         
  ADC_CommonInit(ADC1, &ADC_CommonInitStructure);
 
  ADC_InitStructure.ADC_ContinuousConvMode = ADC_ContinuousConvMode_Enable;
  ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
  ADC_InitStructure.ADC_ExternalTrigConvEvent = ADC_ExternalTrigConvEvent_0;        
  ADC_InitStructure.ADC_ExternalTrigEventEdge = ADC_ExternalTrigEventEdge_None;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_OverrunMode = ADC_OverrunMode_Disable;  
  ADC_InitStructure.ADC_AutoInjMode = ADC_AutoInjec_Disable; 
  ADC_InitStructure.ADC_NbrOfRegChannel = 1;
 
  ADC_Init(ADC1, &ADC_InitStructure);
 
  /* ADC1 regular channel7 configuration */
  ADC_RegularChannelConfig(ADC1, ADC_Channel_7, 1, ADC_SampleTime_19Cycles5);
  
  /* Enable ADC1 */
  ADC_Cmd(ADC1, ENABLE);
 
  /* wait for ADRDY */
  while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_RDY));
}

void Delay(__IO uint32_t nTime)
{
  TimingDelay = nTime;

  while(TimingDelay != 0);
}

#ifdef  USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif

 

 

/**************************************************************************************/
 

 

 

 

 

void ADC1_2_IRQHandler(void)
{
    GPIOE->ODR |= 0x100; //PE08 up
    ADC1ConvertedValue= ADC_GetConversionValue(ADC1);
    printf("value: %d\n",ADC1ConvertedValue);
    ADC_ClearITPendingBit(ADC1, ADC_IT_EOC);
    GPIOE->ODR &= ~0x0100; //PE8 down
 }

 

 

 

 

 

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __MAIN_H
#define __MAIN_H

/* Includes ------------------------------------------------------------------*/
#include "stm32f30x.h"
#include "stm32f3_discovery.h"
#include "stm32f30x_it.h"
#include <stdio.h>

/* Exported types ------------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/
/* Exported macro ------------------------------------------------------------*/
/* Exported functions ------------------------------------------------------- */
//void EXTIOInit(void);
void ADC1_config(void);
void Delay(__IO uint32_t nTime);

#endif /* __MAIN_H */



Outcomes