Forum: Mikrocontroller und Digitale Elektronik STM32 ADC liefert Wert der Vorherigen Wandlung statt Aktuellen


von STM32 Einarbeiter (Gast)


Lesenswert?

Hallo,

ich hab ein kleines Problem mit dem ADC des STM32F407VG  (Discovery 
Board).
Und zwar klappt es zwar die Wandlungen des ADC anzustoßen und ich 
bekommen auch ein Ergebnis, aber den Wert den ich erhalte ist immer von 
der Wandlung zuvor, also veraltet.
Es wäre schön, wenn jemand kurz über den Code schauen könnte, damit ich 
weißt was ich falsch mach.

Viele Grüße
1
#include <stm32f4xx.h>
2
#include <stm32f4xx_gpio.h>
3
#include <stm32f4xx_adc.h>
4
#include <stm32f4xx_conf.h>
5
6
7
uint16_t ADC_Val;                         //Stores the calculated ADC value
8
ADC_InitTypeDef       ADC_InitStructure;
9
ADC_CommonInitTypeDef ADC_CommonInitStructure;
10
GPIO_InitTypeDef      GPIO_InitStructure;
11
 
12
void ADC_Configuration(void)
13
{
14
/******************************************************************************/
15
/*               ADCs interface clock, pin and DMA configuration              */
16
/******************************************************************************/
17
        
18
  /* Enable peripheral clocks */
19
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_ADC2 | RCC_APB2Periph_ADC3 , ENABLE);
20
  
21
  RCC_AHB1PeriphClockCmd(RCC_AHB1ENR_GPIOCEN,ENABLE);//Clock for the ADC port!! Do not forget about this one ;)
22
 
23
  /* Configure ADC Channel 12 pin as analog input */
24
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2;
25
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
26
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
27
  GPIO_Init(GPIOC, &GPIO_InitStructure);
28
 
29
  /* ADC Common configuration *************************************************/
30
  ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
31
  ADC_CommonInit(&ADC_CommonInitStructure);
32
 
33
  /* ADC1 regular channel 10 to 15 configuration ************************************/
34
  ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
35
  ADC_InitStructure.ADC_ScanConvMode = DISABLE;
36
  ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
37
  
38
//  ADC_InitStructure.ADC_DiscModeChannelCountConfig(1);
39
  ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
40
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
41
  ADC_Init(ADC1, &ADC_InitStructure);
42
  ADC_Init(ADC2, &ADC_InitStructure);
43
  ADC_Init(ADC3, &ADC_InitStructure);
44
  
45
46
 
47
  ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_480Cycles);
48
  ADC_RegularChannelConfig(ADC2, ADC_Channel_11, 1, ADC_SampleTime_480Cycles);
49
 ADC_RegularChannelConfig(ADC3, ADC_Channel_12, 1, ADC_SampleTime_480Cycles);
50
  
51
  
52
//    ADC_DiscModeChannelCountConfig(ADC3, 1);
53
//   ADC_DiscModeCmd(ADC3, ENABLE);
54
  
55
 
56
/* Enable ADC1 to ADC3*/
57
  ADC_Cmd(ADC1, ENABLE);
58
  ADC_Cmd(ADC2, ENABLE);
59
  ADC_Cmd(ADC3, ENABLE);
60
 
61
}//end ADC_Configuration
62
 
63
uint16_t Get_ADC_Value(uint8_t Channel)
64
{
65
    switch(Channel)
66
    {
67
        case 10:
68
            ADC_SoftwareStartConv(ADC1);
69
            while(ADC_GetSoftwareStartConvStatus(ADC1) != RESET){ADC_Val = 0;}
70
            ADC_Val = ADC_GetConversionValue(ADC1);
71
            break;
72
        case 11:
73
            ADC_SoftwareStartConv(ADC2);
74
            while(ADC_GetSoftwareStartConvStatus(ADC2) != RESET){ADC_Val = 0;}
75
            ADC_Val = ADC_GetConversionValue(ADC2);
76
            break;
77
        case 12:
78
            ADC_SoftwareStartConv(ADC3);
79
            while(ADC_GetSoftwareStartConvStatus(ADC3) != RESET){ADC_Val = 0;}
80
            ADC_Val = ADC_GetConversionValue(ADC3);
81
            ADC_Val = ADC_GetConversionValue(ADC3);
82
            break;
83
        default:
84
            ADC_Val = 0;
85
            break;
86
    }
87
    return ADC_Val;
88
}
89
90
int main(void){
91
  volatile  uint16_t aaa=0;
92
  ADC_Configuration();
93
94
95
  
96
  while(1==1){
97
  aaa=Get_ADC_Value(12);
98
    
99
    
100
    
101
    
102
  }
103
}

: Bearbeitet durch User
von Stefan (Gast)


Lesenswert?

Wo ist der Code von ADC_GetConversionValue()?

Ansonsten würde ich sagen: it is not a bug, it is a feature ...:

Beim Aufruf von ADC_GetConversionValue() liest Du wahrscheinlich erst 
den letzten Wert aus dem Wandler aus und startest ihn dann neu. Das 
hätte den Vorteil, daß Du nicht auf den ADC warten musst. In dem Fall 
wäre der richtige Name nicht
   ADC_GetConversionValue()
sondern
   ADC_GetLastConversionValueAndStartNextConversion()


Gruß Stefan

von Dennis X. (Gast)


Lesenswert?

Stefan schrieb:
> ADC_GetLastConversionValueAndStartNextConversion()

:D Sehr gut!

von STM32 Einarbeiter (Gast)


Lesenswert?

Hallo,

ADC_GetConversionValue() kommt aus der Standard Peripheral Library...

Fehler war, man muss scheinbar

auf while (ADC_GetFlagStatus(ADC3, ADC_FLAG_EOC) == RESET) abfragen,

statt auf

while(ADC_GetSoftwareStartConvStatus(ADC3) != RESET)

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.