2

I've been reading a lot online to figure what is not working in my code, but it seems like the ADC conversion in my code never starts and I'm getting really desperate... No matter what I do, the EOC Flag always remain to 0 and I am currently out of ideas. Making the ADC work should be simple, but for some reason, I am completly unable to make it work.

I'd be very grateful if someone could show me where my mistakes seems to be. I am using the STM32L100CR-Discovery.

/* Standard includes. */
#include "stm32l1xx.h"
#include "stm32l1xx_rcc.h"
#include "stm32l1xx_gpio.h"
#include "stm32l1xx_exti.h"
#include "stm32l1xx_syscfg.h"
#include "stm32l1xx_spi.h"
#include "stm32l1xx_adc.h"
#include "stdio.h"
#include "misc.h"



void ADC_Initialization (void)
{
    ADC_DeInit( ADC1);
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
    RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

    ADC_InitTypeDef ADC_InitStruct;
    ADC_InitStruct.ADC_ContinuousConvMode = DISABLE;
    ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right;
    ADC_InitStruct.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T2_CC2;
    ADC_InitStruct.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
    ADC_InitStruct.ADC_NbrOfConversion = 1;
    ADC_InitStruct.ADC_Resolution = ADC_Resolution_12b;
    ADC_InitStruct.ADC_ScanConvMode = DISABLE;
    ADC_Init(ADC1, &ADC_InitStruct);


    ADC_CommonInitTypeDef ADC_CommonInitStruct;
    ADC_CommonInitStruct.ADC_Prescaler = ADC_Prescaler_Div1;
    ADC_CommonInit(&ADC_CommonInitStruct);

    ADC_Cmd(ADC1, ENABLE);

    ADC_BankSelection(ADC1, ADC_Bank_A);

    ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 1, ADC_SampleTime_4Cycles);
    ADC_TempSensorVrefintCmd (ENABLE);

}


int Read_ADC(void)
{
    ADC_SoftwareStartConv(ADC1);
    int statut = ADC_GetSoftwareStartConvStatus(ADC1);
    printf("%d", statut);

    ADC_EOCOnEachRegularChannelCmd(ADC1, ENABLE);

    while (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);

    int valeur = ADC_GetConversionValue(ADC1);

    return valeur;

}

int main(void)
{
    ADC_Initialization();
    int ADC_Valeur = 0;

    while(1)
    {
        ADC_Valeur = Read_ADC();
        printf("%d", ADC_Valeur);
    }
}

Thanks in advance!

stm32 stm

  • 2
    It's probably worth starting with some example code, and then perhaps gradually migrating that towards your need. If the chip is not on an eval board, also make sure you connected *all* the power and ground pairs, not just some of them. – Chris Stratton Feb 24 '19 at 18:30
  • I actually did start with few example codes, I managed to make my SPI works and I even managed to make the ADC work using the HAL library. Over, I still do not get why it is not working using the basics library using a very similar code. Also, it is an eval board, so there should be no connection problems. – Nathaniel Brochu Feb 24 '19 at 19:25
  • Start over from the working example and migrate it step by step towards your need, testing at each change (setting up version control like git would be great for this). – Chris Stratton Feb 24 '19 at 19:57
  • 1
    I mean, I was already doing that, but it gave me the idea to remove one per one the line in the HAL library to see only what was necessary. In the end, I did manage to find my problem! If anyone ever happens to search for the solution here it is: First, I did mess up this line : RCC_APB2PeriphCLOCKCmd(RCC_APB2Periph_ADC1, ENABLE); But it is also necessary on the STM32L100C to activate the HSI oscillator clock, which is not done in the library. Adding this line with the RCC should work RCC -> CR |= (0x1U << (0U)); So thank you very much! – Nathaniel Brochu Feb 25 '19 at 03:43
  • +1 Sounds like you have a solution. To resolve your question you should write that up more clearly in the "answer" form below and then accept it once the self-answer timer expires. – Chris Stratton Feb 25 '19 at 04:52

1 Answers1

1

In the end, I did manage to find my problem! If anyone ever happens to search for the solution here it is: First, I did mess up this line :

RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1, ENABLE);

It should read :

RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);

But it is also necessary on the STM32L100C to activate the HSI oscillator clock, which is not done in the library. Adding this line somewhere in the initialization should work:

RCC -> CR |= (0x1U << (0U));