AnsweredAssumed Answered

STM32F1 ADC - Only works the first 2/3 times

Question asked by xavier.jose on Sep 12, 2013
Latest reply on Sep 13, 2013 by Clive One
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

Outcomes