I am currently trying to make the ADC driver code for the STM32F3 version of the colorchord https://github.com/cnlohr/colorchord code work on an STM32F4, as my F4 does not have an on board microphone. Also, I still want to use direct audio-line-in anyway since the microcontroller won't be especially close to the speakers anyway.

Here is the ADC.c code. The rest of the project is pretty much the same, in how the data is processed, just, one has audio read over the PDM microphone, while the other by ADC, but both convert it into the same format.

    //Mostly from: http://www.pezzino.ch/stm32-adc-voltage-monitor/
//Also: http://www.micromouseonline.com/2009/05/26/simple-adc-use-on-the-stm32/

#include "adc.h"
#include <stm32f30x_rcc.h>
#include <stm32f30x_gpio.h>
#include <stm32f30x_adc.h>
#include <stm32f30x_tim.h>
#include <stm32f30x_misc.h>

static int calibration_value;
extern RCC_ClocksTypeDef RCC_Clocks;

void InitADC()

    ADC_InitTypeDef       ADC_InitStructure;
    ADC_CommonInitTypeDef ADC_CommonInitStructure;
    GPIO_InitTypeDef      GPIO_InitStructure;
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

    // Configure the ADC clock

    //Enable ADC1 clock //
    RCC_AHBPeriphClockCmd( RCC_AHBPeriph_ADC34, ENABLE );

    // ADC Channel configuration
    // GPIOC Periph clock enable
    RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOB, ENABLE );

    // Configure ADC Channel7 as analog input
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
    GPIO_Init( GPIOB, &GPIO_InitStructure );

    ADC_StructInit( &ADC_InitStructure );

    // Calibration procedure
    ADC_VoltageRegulatorCmd( ADC4, ENABLE );

    // Insert delay equal to 10 µs
    _delay_us( 10 );

    ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
    ADC_CommonInitStructure.ADC_Clock = ADC_Clock_AsynClkMode;
    ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
    ADC_CommonInitStructure.ADC_DMAMode = ADC_DMAMode_OneShot;
    ADC_CommonInitStructure.ADC_TwoSamplingDelay = 0;
    ADC_CommonInit( ADC4, &ADC_CommonInitStructure );

    ADC_InitStructure.ADC_ContinuousConvMode = ADC_ContinuousConvMode_Enable;
    ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
    ADC_InitStructure.ADC_ExternalTrigConvEvent = ADC_ExternalTrigConvEvent_0;
    ADC_InitStructure.ADC_ExternalTrigEventEdge = ADC_ExternalTrigEventEdge_None;
    ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
    ADC_InitStructure.ADC_OverrunMode = ADC_OverrunMode_Disable;
    ADC_InitStructure.ADC_AutoInjMode = ADC_AutoInjec_Disable;
    ADC_InitStructure.ADC_NbrOfRegChannel = 1;
    ADC_Init( ADC4, &ADC_InitStructure );

    // ADC4 regular channel3 configuration
    ADC_RegularChannelConfig( ADC4, ADC_Channel_3, 1, ADC_SampleTime_181Cycles5 );

    // Enable ADC4
    ADC_Cmd( ADC4, ENABLE );

    // wait for ADRDY
    while( !ADC_GetFlagStatus( ADC4, ADC_FLAG_RDY ) );

    NVIC_InitTypeDef NVIC_InitStructure;
    // Enable the TIM2 gloabal Interrupt
    NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init (&NVIC_InitStructure);

    // TIM2 clock enable
    RCC_APB1PeriphClockCmd (RCC_APB1Periph_TIM2, ENABLE);
    // Time base configuration
    RCC->CFGR |= 0X1400;
    TIM_TimeBaseStructure.TIM_Period = (RCC_Clocks.HCLK_Frequency/(ADCFS*ADCOVERSAMP)) - 1;
    TIM_TimeBaseStructure.TIM_Prescaler = 1 - 1; // Operate at clock frequency
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit (TIM2, &TIM_TimeBaseStructure);
    // TIM IT enable
    TIM_ITConfig (TIM2, TIM_IT_Update, ENABLE);
    /// TIM2 enable counter
    TIM_Cmd (TIM2, ENABLE);

    /// Start ADC4 Software Conversion
    ADC_StartConversion( ADC4 );


void TIM2_IRQHandler (void)
    static uint8_t oversamp = 0;
    static int32_t average = 0;
    static int32_t oversampout = 0;


    if (TIM_GetITStatus (TIM2, TIM_IT_Update) != RESET) {
        TIM_ClearITPendingBit (TIM2, TIM_IT_Update);
        int16_t value = ADC_GetConversionValue(ADC4);
        ADC_StartConversion( ADC4 );

        oversampout += value;

        if( oversamp >= ADCOVERSAMP )
            value = oversampout / ADCOVERSAMP;
            average = ((average*1023) + (value*1024))/1024;
            value = value-(average/1024);
            oversamp = 0;
            ADCCallback( value );
            oversampout = 0;



Problem is, the library for the STM32F3 and F4 are completely different so, it's not just a matter of copypasting, and/or changing pin names and numbers, the actual function set required is completely different.

Could someone please help me figure out how to set up/configure the ADC on the F4 the same way the F3 code above would?

I'm sort of on a time limit now, now and then I've been poking at the problem in my free time between study, but now its suddenly mid October and I wanted this done and working on the uC I have (stm32f407vet6) before my friend's birthday since, an RBG visualizer using this was sort of my present to them, or, going to be.

Anyway, could someone please help me out with the ADC initialization code? I have no idea what half these configurations/functions do so, it's proving extremely difficult to comb through the ST libraries to find equivalent ones, so far I have a few down, but, many lines of code don't seem to have an equivalent.

At least for the config/setup, this code should be identical to how it would be initialized on the arduino IDE. Or in short, I need the F4 equivalent for the contents of initadc().

  • \$\begingroup\$ Stop using prehistoric dead SPL. Use HAL instead. \$\endgroup\$ – P__J__ Oct 19 at 10:02
  • \$\begingroup\$ Use CMSIS and do it right. \$\endgroup\$ – Elliot Alderson Oct 19 at 12:13
  • \$\begingroup\$ Find an example that does something close to what you need, and change it into what you need. \$\endgroup\$ – Chris Stratton Oct 19 at 16:30

Your Answer

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

Browse other questions tagged or ask your own question.