cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F1 ADC - Only works the first 2/3 times

ee06091
Associate III
Posted on September 12, 2013 at 19:27

Hi,

I'm trying the ADC on STM32VLDiscovery.

#include <stm32f10x.h>
#include <stm32f10x_gpio.h>
#include <stm32f10x_rcc.h>
#include <stm32f10x_tim.h>
#include <stm32f10x_adc.h>
#include <misc.h>
int
main(
void
)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* TIM3 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
/* GPIOA/C/ADC1/AFIO clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC | RCC_APB2Periph_ADC1 | RCC_APB2Periph_AFIO, ENABLE);
/* GPIO PA1-AF */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* GPIO PC9 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* ADC */
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T3_TRGO;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 1, ADC_SampleTime_55Cycles5);
ADC_ITConfig(ADC1, ADC_IT_EOC, ENABLE);
ADC_ExternalTrigConvCmd(ADC1, ENABLE);
ADC_Cmd(ADC1, ENABLE);
//configure NVIC
NVIC_InitStructure.NVIC_IRQChannel = ADC1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/*NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);*/
//configure timer
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_Period = SystemCoreClock/1000 - 1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/* TIM3 TRGO selection */
TIM_ITConfig(TIM3, TIM_IT_Trigger, ENABLE);
TIM_Cmd(TIM3, ENABLE);
GPIO_ResetBits(GPIOC, GPIO_Pin_9);
GPIO_SetBits(GPIOC, GPIO_Pin_9);
while
(1)
{}
}
void
ADC1_IRQHandler(
void
)
{
uint16_t ain = ADC_GetConversionValue (ADC1);
ADC_ClearITPendingBit(ADC1 ,ADC_IT_EOC );
}
#ifdef USE_FULL_ASSERT
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) */
while
(1)
{}
}
#endif

The first 2/3 times the TIM3 trigger the ADC converter but after that it doesn't convert anymore :s

(gdb) r
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /home/ze/Development/STM32F1Projects/analogin/analogin.elf 
Breakpoint 6, ADC1_IRQHandler () at main.c:86
86 ADC_ClearITPendingBit(ADC1 ,ADC_IT_EOC );
(gdb) p ain
$4 = 1952
(gdb) c
Continuing.
Breakpoint 6, ADC1_IRQHandler () at main.c:86
86 ADC_ClearITPendingBit(ADC1 ,ADC_IT_EOC );
(gdb) p ain
$5 = 4041
(gdb) c
Continuing.

and the adc IT never triggers again :s Thanks #adc
3 REPLIES 3
Posted on September 12, 2013 at 20:25

You don't select a trigger output, but rather a TIM3 interrupt you don't service.

You don't calibrate the ADC Reading the ADC value clears EOC Exiting the IRQ too quickly after clearing a peripheral has the potenital to reenter due to NVIC/Pipeline/Tailchain hazard. The following is a working example

/******************************************************************************/
// VL Discovery ADC1 1 KHz Sample PA.6 via TIM3, and EOC interrupt - sourcer32@gmail.com
#include ''STM32vldiscovery.h''
/******************************************************************************/
#define SAMPLES 100
vu16 ADC_RegularConvertedValueTab[SAMPLES];
/******************************************************************************/
void ADC1_IRQHandler(void) // ADC1_2_IRQHandler() for non F100
{
static int i = 0;
if (ADC_GetITStatus(ADC1, ADC_IT_EOC) != RESET) // Enters at 1 KHZ
{
ADC_RegularConvertedValueTab[i] = ADC_GetConversionValue(ADC1); // Implicitly clears EOC
i = (i + 1) % SAMPLES;
STM32vldiscovery_LEDToggle(LED3); // Toggles at 500 Hz
if (i == 0) STM32vldiscovery_LEDToggle(LED4); // Toggles at 5 Hz for 100 samples
}
}
/******************************************************************************/
int main(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructInit;
uint16_t Period;
/* Initialise LEDs LD3&LD4, both off */
STM32vldiscovery_LEDInit(LED3);
STM32vldiscovery_LEDInit(LED4);
STM32vldiscovery_LEDOff(LED3);
STM32vldiscovery_LEDOff(LED4);
/* Enable ADC1, TIM3 and GPIOA clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
/* Configure PA.06 (ADC Channel6) as analog input -------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure the NVIC */
NVIC_InitStructure.NVIC_IRQChannel = ADC1_IRQn; // ADC1_2_IRQn for non F100
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* ADC1 configuration ------------------------------------------------------*/
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = DISABLE; // Single Channel
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; // Scan on Demand (Trigger)
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T3_TRGO;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
/* ADC1 regular channel6 configuration */
ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 1, ADC_SampleTime_55Cycles5);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
/* Enable ADC1 reset calibaration register */
ADC_ResetCalibration(ADC1);
/* Check the end of ADC1 reset calibration register */
while(ADC_GetResetCalibrationStatus(ADC1));
/* Start ADC1 calibaration */
ADC_StartCalibration(ADC1);
/* Check the end of ADC1 calibration */
while(ADC_GetCalibrationStatus(ADC1));
/* Enable ADC1 EOC interrupt */
ADC_ITConfig(ADC1, ADC_IT_EOC, ENABLE); /* EOC = End Of Conversion */
Period = SystemCoreClock / 1000; // 1 KHz from a 24 MHz system clock, watch 16-bit @ 72 MHz
TIM_TimeBaseStructInit.TIM_Period = Period - 1;
TIM_TimeBaseStructInit.TIM_Prescaler = 0;
TIM_TimeBaseStructInit.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructInit.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructInit.TIM_RepetitionCounter = 0x00;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructInit);
/* Select Update as Trigger */
TIM_SelectOutputTrigger(TIM3, TIM_TRGOSource_Update);
/* Enables the TIM3 counter */
TIM_Cmd(TIM3, ENABLE);
/* Enable ADC1 external trigger conversion */
ADC_ExternalTrigConvCmd(ADC1, ENABLE);
while(1); /* does not exit - kind of important */
}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
ee06091
Associate III
Posted on September 13, 2013 at 18:55

Thanks, understood the problem :)

PS: Can you explain me the vu16 variable?

Posted on September 13, 2013 at 20:25

vu16

volatile unsigned int 16-bit

volatile because we change the content under interrupt, and might want to access it via foreground code.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..