1
\$\begingroup\$

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

\$\endgroup\$
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – Chris Stratton Feb 24 at 18:30
  • \$\begingroup\$ 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. \$\endgroup\$ – Nathaniel Brochu Feb 24 at 19:25
  • \$\begingroup\$ 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). \$\endgroup\$ – Chris Stratton Feb 24 at 19:57
  • 1
    \$\begingroup\$ 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! \$\endgroup\$ – Nathaniel Brochu Feb 25 at 3:43
  • \$\begingroup\$ +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. \$\endgroup\$ – Chris Stratton Feb 25 at 4:52
0
\$\begingroup\$

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));
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.