2013-07-17 07:46 AM
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>
2013-07-23 07:49 AM
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
2013-07-23 07:53 AM
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.lib2013-07-23 08:07 AM
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. ;)
2013-07-25 02:48 AM
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 simplyTestValue = (float)ADCConvertedValues[1];
Could I be missing an import that allows me to deal with floating point numbers?
2013-07-25 06:22 AM
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));
}
2013-07-25 07:45 AM
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.
2013-07-26 06:39 AM
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?
2013-07-29 12:40 AM
You may find this online FFT IFFT resource useful.
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.2014-05-14 04:25 PM
2014-05-14 06:58 PM
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.