I'm trying to enable an ADC converter. I'm using STM32F3 Discovery with SPL Library. I measure voltage on PA1 pin. I don't know why it doesn't work. What have I missed? The value of 'value' variable doesn't change and equals 0.

#include "stm32f30x.h"
#include "stm32f30x_rcc.h"
#include <stm32f30x_gpio.h>

{
}

void delay_ms (int time)
{
int i;
for (i = 0; i < time * 4000; i++) {}
}

int main()
{

uint16_t value = 0;

GPIO_InitTypeDef GPIO_InitStructure;

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOE, ENABLE);

GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Pin  = LED;
GPIO_Init( GPIOE, &GPIO_InitStructure);
uint32_t led = 0;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);

while(1)
{
}
}

• Put some dwell in your while loop, so any debugger has time to update. It's probably not your answer, but it's the right thing to do. – Scott Seidman Apr 14 '18 at 19:04
• Previously I had code in the while loop responsible for turning led diodes and It didn't work as well – marek.m371 Apr 14 '18 at 19:12
• Did you checke whether the clock to the SDC is enabled somewhere? It is probably off by default. – Wouter van Ooijen Apr 14 '18 at 19:16
• @WoutervanOoijen -- there are RCC commands in the code (though without looking, I don't know if they're right!) – Scott Seidman Apr 14 '18 at 19:24
• It looks like the GPIO clock is enabled, but not the ADC clock – Scott Seidman Apr 14 '18 at 20:02

You haven't enabled the ADC clock. Try

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_ADC12, ENABLE);


If that doesn't work, use your IDE to find the declaration of RCC_AHBPeriphClockCmd and read the source to see if I've gotten the name of the clock wrong.

The STM30xx library docs will probably be of help. Enabling the ADC clock is step 2 in Section 3.2.1 -- and I can't see anything else you missed. From that doc:

3.2.1 How to use this driver

2. Enable the ADC interface clock using RCC_AHBPeriphClockCmd();

3. ADC pins configuration  Enable the clock for the ADC GPIOs using the following function: RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOx, ENABLE);  Configure these ADC pins in analog mode using GPIO_Init();

4. Configure the ADC conversion resolution, data alignment, external trigger and edge, sequencer lenght and Enable/Disable the continuous mode using the ADC_Init() function.

5. Activate the ADC peripheral using ADC_Cmd() function. FWIW, when I have trouble, I usually go into the library declarations, as these functions tend to be very well self-documented.

FWIW, my SOP for library problems is to find the declaration in the library, as the libraries tend to be fairly well self-documented.

• I have done it according to the manual but still doesn't work. I placed my fixed code above – marek.m371 Apr 15 '18 at 23:23

I got something like this

#include "stm32f30x.h"
#include "stm32f30x_rcc.h"
#include <stm32f30x_gpio.h>

#define LED (GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15)

uint16_t convertedValue= 0;

void delay_ms (int time)
{
int i;
for (i = 0; i < time * 4000; i++) {}
}

{
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

GPIO_InitTypeDef GPIO_InitStructure;
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_1;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA,&GPIO_InitStructure);

}

{
}

int main()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOE, ENABLE);
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Pin  = LED;
GPIO_Init(GPIOE, &GPIO_InitStructure);
uint32_t led = 0;

while(1)
{