cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F407VG ADC Inputs give non null values when not connected

jlidiborhan
Associate
Posted on April 22, 2015 at 00:04

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 :


#include ''stm32f4xx.h''

#include ''stm32f4xx_rcc.h''

#include ''stm32f4xx_gpio.h''

#include ''stm32f4xx_tim.h''

#include ''stm32f4xx_adc.h''

#include ''stm32f4xx_dma.h''

#include ''delay.h''

#include ''STM32F4-Discovery/stm32f4_discovery.h''




int
i=0;

uint16_t ADC_Val;

uint16_t ADCConvertedValue[6] = {0,0,0,0,0,0};


int
main(
void
) {

/* Initialize system */


SysTick_Init();

SystemInit();



ADC_DMA_Configuration();

while
(1) {



}

}

void
SysTick_Handler(
void
) {

TimeTick_Decrement();

}


#define ADC1_DR_Address ((uint32_t)0x4001244C)


void
ADC_DMA_Configuration(
void
) {

GPIO_InitTypeDef GPIO_InitStructure; 
//Variable used to setup the GPIO pins

DMA_InitTypeDef DMA_InitStructure; 
//Variable used to setup the DMA

ADC_InitTypeDef ADC_InitStructure; 
//Variable used to setup the ADC

ADC_CommonInitTypeDef ADC_CommonInitStructure;


GPIO_StructInit(&GPIO_InitStructure);

ADC_StructInit(&ADC_InitStructure);

ADC_CommonStructInit(&ADC_CommonInitStructure);

DMA_StructInit(&DMA_InitStructure);


RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);


//--Enable ADC1 and GPIOC--

RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 , ENABLE);

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC|RCC_AHB1Periph_GPIOA, ENABLE);



GPIO_StructInit(&GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

GPIO_Init(GPIOC, &GPIO_InitStructure);

GPIO_StructInit(&GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

GPIO_Init(GPIOA, &GPIO_InitStructure);



DMA_DeInit(DMA2_Stream4); 
//Set DMA registers to default values

DMA_InitStructure.DMA_Channel = DMA_Channel_0;

DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR; 
//Source address

DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADCConvertedValue[0]; 
//Destination address

DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;

DMA_InitStructure.DMA_BufferSize = 6; 
//Buffer size

DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;

DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;

DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; 
//source size - 16bit

DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; 
// destination size = 16b

DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;

DMA_InitStructure.DMA_Priority = DMA_Priority_High;

DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;

DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;

DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;

DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;

DMA_Init(DMA2_Stream4, &DMA_InitStructure); 
//Initialize the DMA

DMA_Cmd(DMA2_Stream4, ENABLE);



/**

Config the ADC1

*/

ADC_DeInit();

ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;

ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;

ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; 
//continuous conversion

ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConvEdge_None;

ADC_InitStructure.ADC_NbrOfConversion = 6;

ADC_InitStructure.ADC_ScanConvMode = ENABLE; 
// 1=scan more that one channel in group

ADC_Init(ADC1,&ADC_InitStructure);


ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;

ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;

ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;

ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;

ADC_CommonInit(&ADC_CommonInitStructure);


//Setup order in which the Channels are sampled....

ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 1, ADC_SampleTime_56Cycles);

ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 2, ADC_SampleTime_56Cycles);

ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 3, ADC_SampleTime_56Cycles);

ADC_RegularChannelConfig(ADC1, ADC_Channel_7, 4, ADC_SampleTime_56Cycles);

ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 5, ADC_SampleTime_56Cycles);

ADC_RegularChannelConfig(ADC1, ADC_Channel_15, 6, ADC_SampleTime_56Cycles);


ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);

ADC_DMACmd(ADC1, ENABLE); 
//Enable ADC1 DMA

ADC_Cmd(ADC1, ENABLE); 
//Enable ADC1

ADC_SoftwareStartConv(ADC1); 
// Start ADC1 conversion


}
//end ADC_Configuration

#adc #stm32f4 #dma
1 REPLY 1
Posted on April 22, 2015 at 02:28

And this surprises you?

There isn't any source of electrons to charge/discharge the sampling capacitor

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..