Forum: Mikrocontroller und Digitale Elektronik STM32F4 Discovery ADC


von Daniela (Gast)


Lesenswert?

Hallo,

besitze das STM32F4 Discovery Board und möchte einen ADC Port 
initialisieren. In der STM Library ist nur ein Beispiel eines
ADC mit DMA dabei. Diesen benötige ich nicht. Möchte einfach
Werte aus dem ADC lesen. Benötige eine Samplingrate von etwa 500khz
bis 1Mhz.

Könnte mir jemand beim Initialisierungscode helfen?

Liebe Grüße
Daniela

von Moritz M. (Gast)


Lesenswert?

Hallo,

in der Std-Lib von ST steht oben in der "ADC.c" Datei eine komplette 
Erklärung.

Moritz

von Stefan W. (ahabakuk)


Lesenswert?

Hallo Daniela

Du benötigst eine Clock für den ADC
1
// enable Clock for ADC1
2
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
3
4
GPIO_InitTypeDef gpioInit; // create new datatype

Zum Beispiel Port B Pin 0 als Analog Eingang setzen
1
  
2
/*--- PORT B -------------------------------*/
3
  
4
/* Configure PB0 (ADC Channel 8) as analog input */
5
gpioInit.GPIO_Pin = GPIO_Pin_0;
6
gpioInit.GPIO_Mode = GPIO_Mode_AIN;
7
GPIO_Init(GPIOB, &gpioInit);

ADC Initialisieren
1
ADC_InitTypeDef ADC_InitStructure;
2
3
/* ADC1 Configuration */
4
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
5
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
6
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
7
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
8
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
9
ADC_InitStructure.ADC_NbrOfChannel = 1;
10
ADC_Init(ADC1, &ADC_InitStructure);
11
  
12
// ADC1 regular channel 8 configuration: witch adc, witch channel, groupe sequencer, sample time
13
//(ADC_TypeDef* ADCx, uint8_t ADC_Channel, uint8_t Rank, uint8_t ADC_SampleTime)
14
ADC_RegularChannelConfig(ADC1,ADC_Channel_8,1,ADC_SampleTime_13Cycles5);
15
  
16
ADC_Cmd(ADC1, ENABLE);   // Enable ADC1
17
  
18
ADC_SoftwareStartConvCmd(ADC1, ENABLE); //Start ADC1 Conversion

Das Resultat kannst du so in eine Variabel schreiben:
1
ADC_result=ADC_GetConversionValue(ADC1); //get ADC result from register

von Daniela (Gast)


Angehängte Dateien:

Lesenswert?

Hey danke für die schnelle Hilfe ;-)

es kommen ein paar compile Fehler. Weist du warum?

lg

von Daniela (Gast)


Lesenswert?

bei mir hat nur der struct ADC_CommonInitTypeDef die Variable ADC_Mode?
Ich verwende die std lib vom Discovery board.

von Daniela (Gast)


Lesenswert?

Das ist nun meine Init ADC funktion
1
void InitAdc()
2
{
3
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
4
  GPIO_InitTypeDef gpioInit;
5
  
6
  /*--- PORT B -------------------------------*/
7
  /* Configure PB0 (ADC Channel 8) as analog input */
8
  gpioInit.GPIO_Pin = GPIO_Pin_0;
9
  gpioInit.GPIO_Mode = GPIO_Mode_AIN;
10
  GPIO_Init(GPIOB, &gpioInit);
11
  
12
  ADC_InitTypeDef ADC_InitStructure;
13
  ADC_CommonInitTypeDef ADC_CommonInitStructure;
14
15
  ADC_StructInit(&ADC_InitStructure);
16
  ADC_CommonInit(&ADC_CommonInitStructure);
17
18
  /* ADC1 Configuration */
19
  ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
20
  ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
21
  ADC_InitStructure.ADC_ScanConvMode = DISABLE;
22
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
23
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConvEdge_None;
24
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
25
  ADC_InitStructure.ADC_NbrOfConversion = 1;
26
  ADC_Init(ADC1, &ADC_InitStructure);
27
    
28
  /* ADC Common configuration *************************************************/
29
  ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
30
  ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
31
  ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_2;  
32
  ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2; 
33
  ADC_CommonInit(&ADC_CommonInitStructure);
34
35
  // ADC1 regular channel 8 configuration: witch adc, witch channel, groupe sequencer, sample time
36
  //(ADC_TypeDef* ADCx, uint8_t ADC_Channel, uint8_t Rank, uint8_t ADC_SampleTime)
37
  ADC_RegularChannelConfig(ADC1,ADC_Channel_8,1,ADC_SampleTime_15Cycles);
38
  ADC_Cmd(ADC1, ENABLE);   // Enable ADC1
39
  ADC_SoftwareStartConv(ADC1); //Start ADC1 Conversion
40
}

meine main:
1
int main(void)
2
{
3
  SystemInit();
4
 
5
  InitAdc();
6
  
7
  uint16_t ADC_result = 0;
8
  while(1)
9
  {
10
    ADC_result = ADC_GetConversionValue(ADC1);
11
  }        
12
}

leider bekomme ich im Watch einen Error auf die Variable - was mache ich 
falsch? Habe ich den ADC richtig initialisiert?

lg

von Daniela (Gast)


Lesenswert?

Weis keiner um Rat? In der Variablen steht im watch eine 0 drin. Es 
scheint als läuft der ADC noch nicht. ??

von hilfe (Gast)


Lesenswert?

Hast du überprüft, ob an PB0 auch wirklich dieser ADC Kanal ist?
Was hast du angeschlossen an diesem PIN?

Clock für GPIOA muss noch aktiviert werden!

// enable Clock for GPIOA
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);

ansonsten sieh dir doch mal dieses Tutorial an

http://www.diller-technologies.de/stm32.html#adc

von Daniela (Gast)


Lesenswert?

Das Tutorial leht sich an einen STM32F10X ich habe aber einen F4
beim F10X hab ich es auch schon hingebracht.

wieso muss ich  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);
aktivieren wenn ich doch pin PB0 als ad wandler nutzen möchte.

von pthben (Gast)


Lesenswert?

Stefan!

A tried your code but I have 3 errors

the problems are with the Typedef structures (with GPIO,ADC_COMMON_INIT, 
and ADC_INIT..)

"declaration may no appear after executable statement in block

Here is my adc config:


void adc_configure(){
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
  GPIO_InitTypeDef GpioInit;

  //---- POT B----------------//
  GpioInit.GPIO_Mode =GPIO_Mode_AN; //AIN?
  GpioInit.GPIO_Pin = GPIO_Pin_0;
  GPIO_Init(GPIOB, &GpioInit);

  //ADC
  ADC_InitTypeDef ADC_InitStructure;
  ADC_CommonInitTypeDef ADC_CommonInitStructure;


  ADC_CommonInit(&ADC_CommonInitStructure);

  //ADC1 CONFIG
  ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
  ADC_InitStructure.ADC_ScanConvMode = DISABLE;
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  ADC_InitStructure.ADC_ExternalTrigConv = 
ADC_ExternalTrigConvEdge_None;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_NbrOfConversion = 1;

  ADC_Init(ADC1, &ADC_InitStructure);

  //ADC COmmon CONFIG
  ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
  ADC_CommonInitStructure.ADC_TwoSamplingDelay = 
ADC_TwoSamplingDelay_5Cycles;
  ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_2;
  ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
  ADC_CommonInit(&ADC_CommonInitStructure);

   ADC_RegularChannelConfig(ADC1,ADC_Channel_8,1,ADC_SampleTime_15Cycles);
  ADC_Cmd(ADC1, ENABLE);   // Enable ADC1
  ADC_SoftwareStartConv(ADC1); //Start ADC1 Conversion

  ADC_StructInit(&ADC_InitStructure);
}


I don't know where's the problem, I tried to copy the Struct definitons 
in other lines.. but it doesn't work... :(

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.