AnsweredAssumed Answered

STM32F407VG ADC Inputs give non null values when not connected

Question asked by borhen.jlidi on Apr 22, 2015
Latest reply on Apr 22, 2015 by Clive One
Hi ,
As the title says, my stm32f407 gives non null values when adc pins are not connected .But , when i connect external voltage source they give reasonable values.The pins are : PC4,PC5,PA4,PA5,PA6,PA7 .This is my code :

001.#include "stm32f4xx.h"
002.#include "stm32f4xx_rcc.h"
003.#include "stm32f4xx_gpio.h"
004.#include "stm32f4xx_tim.h"
005.#include "stm32f4xx_adc.h"
006.#include "stm32f4xx_dma.h"
007.#include "delay.h"
008.#include "STM32F4-Discovery/stm32f4_discovery.h"
009. 
010. 
011. 
012.int i=0;
013.uint16_t ADC_Val;
014.uint16_t ADCConvertedValue[6] = {0,0,0,0,0,0};
015. 
016.int main(void) {
017.    /* Initialize system */
018. 
019.    SysTick_Init();
020.    SystemInit();
021. 
022. 
023.    ADC_DMA_Configuration();
024.    while (1) {
025. 
026. 
027.    }
028.}
029.void SysTick_Handler(void) {
030.    TimeTick_Decrement();
031.}
032. 
033.#define ADC1_DR_Address ((uint32_t)0x4001244C)
034. 
035.void ADC_DMA_Configuration(void) {
036.GPIO_InitTypeDef GPIO_InitStructure; //Variable used to setup the GPIO pins
037.DMA_InitTypeDef DMA_InitStructure; //Variable used to setup the DMA
038.ADC_InitTypeDef ADC_InitStructure; //Variable used to setup the ADC
039.ADC_CommonInitTypeDef ADC_CommonInitStructure;
040. 
041.GPIO_StructInit(&GPIO_InitStructure);
042.ADC_StructInit(&ADC_InitStructure);
043.ADC_CommonStructInit(&ADC_CommonInitStructure);
044.DMA_StructInit(&DMA_InitStructure);
045. 
046.RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
047. 
048.//--Enable ADC1 and GPIOC--
049.RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 , ENABLE);
050.RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC|RCC_AHB1Periph_GPIOA, ENABLE);
051. 
052. 
053.GPIO_StructInit(&GPIO_InitStructure);
054.GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
055.GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
056.GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
057.GPIO_Init(GPIOC, &GPIO_InitStructure);
058.GPIO_StructInit(&GPIO_InitStructure);
059.GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
060.GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
061.GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
062.GPIO_Init(GPIOA, &GPIO_InitStructure);
063. 
064. 
065.DMA_DeInit(DMA2_Stream4);  //Set DMA registers to default values
066.  DMA_InitStructure.DMA_Channel = DMA_Channel_0;
067.  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR; //Source address
068.  DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADCConvertedValue[0]; //Destination address
069.  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
070.  DMA_InitStructure.DMA_BufferSize = 6; //Buffer size
071.  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
072.  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
073.  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //source size - 16bit
074.  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; // destination size = 16b
075.  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
076.  DMA_InitStructure.DMA_Priority = DMA_Priority_High;
077.  DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
078.  DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
079.  DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
080.  DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
081.  DMA_Init(DMA2_Stream4, &DMA_InitStructure); //Initialize the DMA
082.  DMA_Cmd(DMA2_Stream4, ENABLE);
083. 
084. 
085.  /**
086.    Config the ADC1
087.  */
088.  ADC_DeInit();
089.  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
090.  ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
091.  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; //continuous conversion
092.  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConvEdge_None;
093.  ADC_InitStructure.ADC_NbrOfConversion = 6;
094.  ADC_InitStructure.ADC_ScanConvMode = ENABLE; // 1=scan more that one channel in group
095.  ADC_Init(ADC1,&ADC_InitStructure);
096. 
097.  ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
098.  ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
099.  ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
100.  ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
101.  ADC_CommonInit(&ADC_CommonInitStructure);
102. 
103.//Setup order in which the Channels are sampled....
104.ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 1, ADC_SampleTime_56Cycles);
105.ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 2, ADC_SampleTime_56Cycles);
106.ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 3, ADC_SampleTime_56Cycles);
107.ADC_RegularChannelConfig(ADC1, ADC_Channel_7, 4, ADC_SampleTime_56Cycles);
108.ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 5, ADC_SampleTime_56Cycles);
109.ADC_RegularChannelConfig(ADC1, ADC_Channel_15, 6, ADC_SampleTime_56Cycles);
110. 
111.ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);
112.ADC_DMACmd(ADC1, ENABLE); //Enable ADC1 DMA
113.ADC_Cmd(ADC1, ENABLE); //Enable ADC1
114.ADC_SoftwareStartConv(ADC1); // Start ADC1 conversion
115. 
116.}//end ADC_Configuration

Outcomes