This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

simple ADC on STM32F4

Hi all,

I'm trying to get a simple ADC working on the stm32f4 discovery board. At the moment I simply want to obtain an input value (ConvertedValue) as a variable rather than saving to memory in DMA.
I'm struggling with the pins - some of them give me a little functionality and others none at all. I cant get any port to work properly. PIN0 will work to an extent - it will read a changing value, but it is also outputting a voltage of 2 volts which I've not told 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 my own)

#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


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
}


//void dac_configure(){
            /* Preconfiguration before using DAC----------------------------------------*/
 // GPIO_InitTypeDef GPIO_InitStructure;


  /* DMA1 clock and GPIOA clock enable (to be used with DAC) */
 // RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1 | RCC_AHB1Periph_GPIOA, ENABLE);

  /* DAC Periph clock enable */
//  RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);

  /* DAC channel 1 & 2 (DAC_OUT1 = PA.4)(DAC_OUT2 = PA.5) configuration */
 // 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(GPIOA, &GPIO_InitStructure);

  /* TIM6 Configuration ------------------------------------------------------*/
//  TIM6_Config();  PUT BACK!!!!!!!!!!!!!!!!!!!!

  /* Configures User Button */
 // STM_EVAL_PBInit(BUTTON_USER, BUTTON_MODE_EXTI);
//}

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();
                }


}



//void DAC_Ch1_WaveConfig(void)
//{
                //DAC_DeInit();
  //  DAC_InitTypeDef DAC_InitStructure;

  //  DAC_InitStructure.DAC_Trigger = DAC_Trigger_None;
   // DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
    //DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_LFSRUnmask_Bits11_0;
    //DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;

  //  DAC_Init(DAC_Channel_1, &DAC_InitStructure);

  //  DAC_Cmd(DAC_Channel_1, ENABLE);

    //DAC_SetChannel1Data(DAC_Align_12b_R, ConvertedValue);

//}


0