0

I'm using stm32f411e discovery board. I'm trying to use ADC1 Channel 3 (PA3) to read the potentiometer. When I put the function config for ADC is ADC_SingleChannel_Config() in #if (ADC_SINGLE_DEF), it is not working and the value return is out of 12 bit resolution. But when I put the ADC_SingleChannel_Config() after the Common_SetUp() in main() it works correctly. I dont know what is happening here. Please help me for this problem. Thanks very much!!!
Ps: All of functions is configurated and worked correctly. This is just my main.c file
My code

#include <stdio.h>
#include <stdint.h>
#include <string.h>

#include "stm32f4xx.h"
#include "stm32f4xx_it.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"

#include "main.h"
#include "macro.h"
#include "hw_cfg.h"
#include "systick_timer_cfg.h"
#include "usart_cfg.h"
#include "watchdog_cfg.h"
#include "rtc_config.h"
#include "adc_config.h"
#include "dma_config.h"

#include "xprintf.h"
#include "ring_buffer.h"

/* Marcro define common config ******************/
#define LED_DEF                 1
#define BUTTON_DEF              1
#define SYSTICK_DEF             1

#define IWDG_DEF                1
#define ADC_SINGLE_DEF          1


#define TIME_INTERVAL           1000 // ms

/* Global variables ******************************/
uint32_t now = 0;

/* Static prototype functions ***********************/
static void Common_SetUp(void);
static void Loop_Handler(void);


/**
 * *********************************************
 *               Main function
 */
int main(void) {
    Common_SetUp ();
//    ADC_SingleChannel_Config(); /* Put this function here is worked */
    /* Loop endless */
    while (1) {
        if (millis() - now > TIME_INTERVAL) {
            now = millis();
            /* TO DO */
            #if (ADC_SINGLE_DEF) /* Read potentiometer */
            uint16_t adc_ret = ADC_Read_SingleChannel (ADC1, ADC_Channel_3, 1);
            double potentiometer_val = (double)(adc_ret * 3) / 4095;
            PRINTLN("[PONTENTIONMETER READ]");
            PRINTLN("ADC = %1d", adc_ret);
            PRINTLN("VOL = %0.2f", potentiometer_val);
#endif
        }
    }
}


/**
 * *********************************************
 *          Common Set Up Peripherals
 */
void Common_SetUp() {
#if (LED_DEF)
    LEDs_Config();
    LEDs_Off (O_R_G_B);
#endif

#if (BUTTON_DEF)
    Button_Config();
#endif

#if (SYSTICK_DEF)
    SYSTICK_Config();
#endif

#if (IWDG_DEF)
    IWDG_Config();
#endif

#if (ADC_SINGLE_DEF)
    ADC_SingleChannel_Config(); /* Put this function here is not worked */
#endif
}
***My ADC_SingleChannel_Config()*** <br>

void ADC_SingleChannel_Config() {

GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;

/* Open Clock Gate *****/
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);

/* IO Configuration ******/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);

/* ADC Configuration ******/
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = 1;
ADC_Init(ADC1, &ADC_InitStructure);
/* Enable ADC */
ADC_Cmd(ADC1, ENABLE);

}

3
  • Please confirm int is 32-bit, else adc_ret * 3 may overflow. Commented Feb 12, 2022 at 20:52
  • "and the value return is out of 12 bit resolution." --> What values are you seeing? (Best in hexadecimal) Is the 12-bit value right or left justified? Commented Feb 12, 2022 at 20:54
  • Aside: Why / 4095 and not / 4096? I suspect the / 4096 is the correct. It depends on if the A/D represents a rounded measurement or truncated one. Commented Feb 12, 2022 at 20:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.