AnsweredAssumed Answered

ADC EOC Interrupt

Question asked by limmer.b on May 30, 2011
Latest reply on Jun 2, 2011 by limmer.b
Hello everybody,

I'm working with the STM32F103ZGT6 on the STM3210E-Eval-Board with Ride7 by Raisonance. I'd like to test the integrated ADC with its' EOC Interrupt to send the converted values by USART to the Terminal at each interrupt.

Sadly, my whole program stops - as far i can see it - BEFORE entering the EOC ISR.
When I use my online-Hardware-debugger, I can see that he stops right under the following lines in system_stm32f10x.c.:

#ifdef VECT_TAB_SRAM
  SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */
#else
  SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */
#endif

The same happens when the AnalogWatchdog-interrupt shoud be started.
Here's my code:

#include"platform_config.h"
#include<stdio.h>
#include"stm32f10x.h"
#include"stm32f10x_adc.c"
#include"stm32_eval.h"
#include"string.h"
#include"stm3210e_eval_lcd.h"
#include"stm32f10x_tim.c"

NVIC_InitTypeDef NVIC_InitStructure;
USART_InitTypeDef USART_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
TIM_OCInitTypeDef  TIM_OCInitStructure;

void DMA_Configuration(void);
void USARTInit(void);
void send_string(char value[255]);
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void ADC1_Configuration(void);
void ADC2_Configuration(void);
void ADC1_Calibration(void);
void ADC2_Calibration(void);
void Timer_Configuration(void);
void LCDInit(void);

#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
   set to 'Yes') calls __io_putchar() */

#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif/* __GNUC__ */

int main(void)
{
/* Variables ---------------------------------------------------------------*/
char message[40];

  STM_EVAL_LEDInit(LED1);

  RCC_Configuration(); // System clocks configuration
  USARTInit(); // USART Initialization
  Timer_Configuration(); // Timer configuration
  DMA_Configuration(); //DMA configuration
  NVIC_Configuration(); // NVIC configuration
  GPIO_Configuration(); // GPIO configuration
  ADC1_Configuration(); // ADC1 configuration
  ADC2_Configuration(); // ADC2 configuration
  ADC1_Calibration(); // ADC1 calibration
  ADC2_Calibration(); // ADC2_Calibration

  send_string("  \rMCU initialization complete.\n\rPlease press [s] to start conversion...\n\r");
do
  {
  c=USART_ReceiveData(USART1);
  } while(c!='s');

  TIM_Cmd(TIM2, ENABLE); // TIM2 enable counter

  ADC_Cmd(ADC1, ENABLE); // Enable ADC1
  ADC_SoftwareStartConvCmd(ADC1, ENABLE); // Start ADC1 Conversion
  ADC_Cmd(ADC2, ENABLE); // Enable ADC2
  ADC_SoftwareStartConvCmd(ADC2, ENABLE); // Start ADC2 Conversion
}

PUTCHAR_PROTOTYPE
{
  USART_SendData(EVAL_COM1, (uint8_t) ch);

while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TC) == RESET)
  {}

return ch;
}

void USARTInit(void)
{
  USART_InitStructure.USART_BaudRate = 115200;
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  USART_InitStructure.USART_StopBits = USART_StopBits_1;
  USART_InitStructure.USART_Parity = USART_Parity_No;
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

  STM_EVAL_COMInit(COM1, &USART_InitStructure);

  USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE);

  USART_Cmd(USART1, ENABLE);
}

/**
  * @brief  Configures the different system clocks.
  * @param  None
  * @retval None
  */

void RCC_Configuration(void)
{
/* HCLK = SYSCLK = 56 MHz */
  RCC_HCLKConfig(RCC_SYSCLK_Div1);

/* PCLK1 = HCLK/8 = 7 MHz*/
  RCC_PCLK1Config(RCC_HCLK_Div8);

/* PCLK2 = HCLK/2 = 28 MHz*/
  RCC_PCLK2Config(RCC_HCLK_Div2);

/* ADCCLK = PCLK2/2 = 14 MHz --> ADCCLK must not be higher than 14 MHz */
switch(ADC_Clock)
  {
case1: RCC_ADCCLKConfig(RCC_PCLK2_Div2); break;
case2: RCC_ADCCLKConfig(RCC_PCLK2_Div4); break;
case3: RCC_ADCCLKConfig(RCC_PCLK2_Div6); break;
case4: RCC_ADCCLKConfig(RCC_PCLK2_Div8); break;
  }

/* Enable peripheral clocks ------------------------------------------------*/

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 | RCC_AHBPeriph_DMA2, ENABLE); // Enable DMA1 and DMA2 clocks

/* Enable ADC1, ADC2, GPIOA, GPIOC GPIOD, AFIO and USART1 clocks */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA |RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO |
                         RCC_APB2Periph_ADC1 | RCC_APB2Periph_ADC2 | RCC_APB2Periph_GPIOC, ENABLE);

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 | RCC_APB1Periph_TIM2, ENABLE); // Enable USART2 and TIM2 clocks
}

/**
  * @brief  Configures the different GPIO ports.
  * @param  None
  * @retval None
  */

void GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;

/* Configure PC.01, PC.02, PC.03 and PC.04 (ADC Channel11, ADC Channel12, ADC Channel13 and
     ADC Channel14) as analog inputs */

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  GPIO_Init(GPIOC, &GPIO_InitStructure);

/* Configure USART1 RTS and USART2 Tx as alternate function push-pull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
}

/**
  * @brief  Configures Vector Table base location.
  * @param  None
  * @retval None
  */

void NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;

/* Configure and enable ADC interrupt */
#if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL)
  NVIC_InitStructure.NVIC_IRQChannel = ADC1_IRQn;
#else
  NVIC_InitStructure.NVIC_IRQChannel = ADC1_2_IRQn;
#endif
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

/* Enable the TIM2 gloabal Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}

void send_string(char value[255])
{
int n;

for(n=0; n<strlen(value);n++)
  {
    USART_SendData(USART1, value[n]); //USART transmission
while(!USART_GetFlagStatus(USART1, USART_FLAG_TC)); // Wait until transmission complete
  }
}

void ADC1_Configuration(void)
{
/* ADC1 configuration ------------------------------------------------------*/
  ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
  ADC_InitStructure.ADC_ScanConvMode = DISABLE;
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_NbrOfChannel = 1;
  ADC_Init(ADC1, &ADC_InitStructure);

/* ADC1 regular channels configuration */
switch(ADC_Cycles)
  {
case1: ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 1, ADC_SampleTime_1Cycles5); break;
case2: ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 1, ADC_SampleTime_7Cycles5); break;
case3: ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 1, ADC_SampleTime_13Cycles5); break;
case4: ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 1, ADC_SampleTime_28Cycles5); break;
case5: ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 1, ADC_SampleTime_41Cycles5); break;
case6: ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 1, ADC_SampleTime_55Cycles5); break;
case7: ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 1, ADC_SampleTime_71Cycles5); break;
case8: ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 1, ADC_SampleTime_239Cycles5); break;
  }

  ADC_ITConfig(ADC1, ADC_IT_EOC, ENABLE); // Enable ADC1 EOC interrupt
  ADC_ITConfig(ADC1, ADC_IT_AWD, ENABLE); // Enable ADC1 AWD interrupt

  ADC_AnalogWatchdogCmd(ADC1, ADC_AnalogWatchdog_SingleRegEnable);
  ADC_AnalogWatchdogThresholdsConfig(ADC1, 0x6A0, 0x000);
  ADC_AnalogWatchdogSingleChannelConfig(ADC1, ADC_Channel_11);

  ADC_DMACmd(ADC1, ENABLE); // Enable ADC1 DMA


void ADC2_Configuration(void)
{
/* ADC2 configuration ------------------------------------------------------*/
  ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
  ADC_InitStructure.ADC_ScanConvMode = DISABLE;
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  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_14, 1, ADC_SampleTime_28Cycles5); // ADC2 regular channels configuration
  ADC_ITConfig(ADC2, ADC_IT_EOC, DISABLE); // Enable ADC2 EOC interupt
  ADC_DMACmd(ADC2, ENABLE); // Enable ADC2 DMA
}

void ADC1_Calibration(void)
{
  ADC_ResetCalibration(ADC1); // Enabe ADC1 reset calibration register
while(!ADC_GetResetCalibrationStatus(ADC1)); // Check the end of ADC1 reset calibration register
  ADC_StartCalibration(ADC1); // Start ADC1 calibration
while(ADC_GetCalibrationStatus(ADC1)); // Check the end of ADC1 calibration
}       

void ADC2_Calibration(void)
{
  ADC_ResetCalibration(ADC2); // Enable ADC2 reset calibaration register
while(!ADC_GetResetCalibrationStatus(ADC2)); // Check the end of ADC2 reset calibration register
  ADC_StartCalibration(ADC2); // Start ADC2 calibaration
while(ADC_GetCalibrationStatus(ADC2)); // Check the end of ADC2 calibration
}


#if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL)
void ADC1_IRQHandler(void)
#else
void ADC1_2_IRQHandler(void)
#endif
{
  STM_EVAL_LEDOn(LED1);
if(ADC_GetITStatus(ADC1, ADC_IT_EOC) != RESET)
  {
    sprintf(buf, "%d\t%d ms\n\r", ADC1ConvertedValue, time);
    send_string(buf);

    ADC_ClearITPendingBit(ADC1, ADC_IT_EOC);
    ADC_ClearITPendingBit(ADC1, ADC_IT_AWD);
  }
}
I'd appreciate every helping hint.
Thanks,

B.L. 

Outcomes