cancel
Showing results for 
Search instead for 
Did you mean: 

Help with simple ADC on STM32F4

willschneider
Associate II
Posted on July 17, 2013 at 16:46

Hi all,

I'm trying to get a simple ADC working on the stm32f4 discoveryboard. At the moment I simply want to obtain an input value(ConvertedValue) as a variable rather than saving to memory inDMA.

I'm struggling with the pins - some of them give me a littlefunctionality and others none at all. I cant get any port to workproperly. PIN0 will work to an extent - it will read a changingvalue, but it is also outputting a voltage of 2 volts which I've nottold it to - and so any readings it gets are somewhat distorted.

I'd really appreciate anyone taking a look at the code im using(its mostly taken from various sources around the web - not really myown)

Sorry for the poor/lack of formatting <code>

<
pre
><
font
size
=
''1''
>#include ''stm32f4xx_adc.h''
#include ''stm32f4xx_gpio.h''
#include ''stm32f4xx_rcc.h''
#include ''led.h''
#include ''stm32f4_discovery.h''
#include ''stm32f4xx_tim.h''
#include ''stm32f4xx_dac.h''
int ConvertedValue = 0; //Converted value read from ADC1
</
font
><
font
size
=
''1''
>
void adc_configure(){
ADC_InitTypeDef ADC_init_structure; //Structure for adc confguration
GPIO_InitTypeDef GPIO_initStructre; //Structure for analog input pin
//Clock configuration
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);//The ADC1 is connected the APB2 peripheral bus thus we will use its clock source
RCC_AHB1PeriphClockCmd(RCC_AHB1ENR_GPIOCEN,ENABLE);//Clock for the ADC port!! Do not forget about this one ;)
//Analog pin configuration
GPIO_StructInit(&GPIO_initStructre);
GPIO_initStructre.GPIO_Pin = GPIO_Pin_0;//The channel 10 is connected to PC0
GPIO_initStructre.GPIO_Mode = GPIO_Mode_AN; //The PC0 pin is configured in analog mode
GPIO_initStructre.GPIO_PuPd = GPIO_PuPd_NOPULL; //We don't need any pull up or pull down
GPIO_Init(GPIOE,&GPIO_initStructre);//Affecting the port with the initialization structure configuration
//ADC structure configuration
ADC_DeInit();
ADC_init_structure.ADC_DataAlign = ADC_DataAlign_Right;//data converted will be shifted to right
ADC_init_structure.ADC_Resolution = ADC_Resolution_12b;//Input voltage is converted into a 12bit number giving a maximum value of 4096
ADC_init_structure.ADC_ContinuousConvMode = ENABLE; //the conversion is continuous, the input data is converted more than once
ADC_init_structure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;// conversion is synchronous with TIM1 and CC1 (actually I'm not sure about this one :/)
ADC_init_structure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;//no trigger for conversion
ADC_init_structure.ADC_NbrOfConversion = 1;//I think this one is clear :p
ADC_init_structure.ADC_ScanConvMode = DISABLE;//The scan is configured in one channel
ADC_Init(ADC1,&ADC_init_structure);//Initialize ADC with the previous configuration
//Enable ADC conversion
ADC_Cmd(ADC1,ENABLE);
//Select the channel to be read from
ADC_RegularChannelConfig(ADC1,ADC_Channel_10,1,ADC_SampleTime_144Cycles);
}
int adc_convert(){
ADC_SoftwareStartConv(ADC1);//Start the conversion
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));//Processing the conversion
return ADC_GetConversionValue(ADC1); //Return the converted data
}
int main(void){
adc_configure();//Start configuration
//dac_configure();
while(1)
{//loop while the board is working
ConvertedValue = adc_convert();//Read the ADC converted value
//DAC_Ch1_WaveConfig();
}
}</
font
>
</
pre
>

</code>
38 REPLIES 38
willschneider
Associate II
Posted on July 23, 2013 at 16:49

I've also included C:\Keil\ARM\CMSIS\Lib\ARM lib's, and pretty much every other half likely path I can find. To no avail

Posted on July 23, 2013 at 16:53

Try adding FOO.LIB to your project tree

Add Files to Group -> STM32F4xx_DSP_StdPeriph_Lib_V1.1.0\Libraries\CMSIS\Lib\ARM\arm_cortexM4lf_math.lib

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
willschneider
Associate II
Posted on July 23, 2013 at 17:07

I think that did it! You are truely the one. If you give me much more help i'm probably going to have to pay off your mortgage or something. ;)

willschneider
Associate II
Posted on July 25, 2013 at 11:48

I'm trying convert an uint16 to float32 inside my interrupt function - but doing so is completely messing with the chip. It seems to take an age - so that the output drops out for a good portion of a second, and it also doesnt seem to actually work. The floating point value doesnt seem to get created. (although i'm having trouble actually discovering this as the debugging mode becomes very tempremental when I'm trying to use floats)

The conversion code i'm using is simply

TestValue = (float)ADCConvertedValues[1];

Could I be missing an import that allows me to deal with floating point numbers?
willschneider
Associate II
Posted on July 25, 2013 at 15:22

Ah, I found it in the end. For anybody else who hits the same problem, it turns out I needed to include the following function in my code -

void cortexm4f_enable_fpu() {
/* set CP10 and CP11 Full Access */
SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2));
}

Posted on July 25, 2013 at 16:45

Should have code like that in SystemInit(), pulled in based on some #defines, in Keil these are usually driven by ''Use FPU'' in the target configuration/options.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
willschneider
Associate II
Posted on July 26, 2013 at 15:39

Concerning using the FFT floating point function in the math lib, it appears that I need to feed it an array of alternate real and imaginary numbers. Is there a way I can set the DMA to save the ADC values to memory like this, or is there a better way of creating such an array?

John F.
Senior
Posted on July 29, 2013 at 09:40

You may find this online FFT IFFT resource useful.

http://www.random-science-tools.com/maths/FFT.htm

You can paste data in and out of it (if you printf your data as floats) and FFT in both directions with graphs. I found it a valuable sanity check when starting out with the DSP FFT functions. At first, maybe just try a small simple set of data such as a sine wave to see what's happening.

ayetlimam
Associate
Posted on May 15, 2014 at 01:25

hi clive , please i need your help in my project !!! just like an oscilloscope i need to convert a signal and have the time value of all the converted value . i work on STM32F407 .

Can you help me please ? 

Posted on May 15, 2014 at 03:58

I'm not really up for doing other peoples projects.

In the example using the timer as a timebase, the position in the array infers the relative time the sample was taken with respect to all the others. The periodicity of the sampling is known, and the time period over which all the samples are taken is also known.

If you were to export the values you could compute a time offset with respect to the the array index.

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