ADC mit STM32 F3 Discovery

OP #3085462
Lesenswert?

Hallo liebe Community!

Seit langem versuchen wir nun unseren Infrarot Analogsensor auszulesen 
und zumindest den Wert, den wir einlesen sollten, auf die OnBoard LED 
Reihe zu schreiben - ohne Erfolg!
Nun wollten wir hier mal fragen, ob jemand eine Lösung für uns hat, im 
Internet sind wir auf nichts gestoßen. Auch ziemlich merkwürdig ist, 
dass das mitgelieferte Beispielprojekt - wir haben es fast 1:1 
übernommen - auch nicht zum Erfolg geführt hat...

Schaut euch diesen Code einmal an, vielleicht könnt ihr uns helfen.

ADC.h:
1
 #ifndef ADC_H_
2
#define ADC_H_
3

4
void ADC1_Init();
5

6
#endif /* ADC_H_ */

ADC.c:
1
 #include "stm32F30x.h"
2
#include "stm32F30x_gpio.h"
3
#include "stm32F30x_adc.h"
4
#include "stm32f30x_rcc.h"
5

6
GPIO_InitTypeDef GPIO_InitStruct;
7
ADC_InitTypeDef ADC_InitStruct;
8

9
void ADC1_Init()
10
{
11
  RCC_ADCCLKConfig(RCC_ADC12PLLCLK_Div2);
12
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_ADC12, ENABLE);
13
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
14

15
  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AN;
16
  GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
17
  GPIO_InitStruct.GPIO_Pin = GPIO_Pin_1;
18
  GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
19
  GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
20
  GPIO_Init(GPIOA, &GPIO_InitStruct);
21

22
  ADC_InitStruct.ADC_AutoInjMode = DISABLE;
23
  ADC_InitStruct.ADC_ContinuousConvMode = ENABLE;
24
  ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Left;
25
  ADC_InitStruct.ADC_ExternalTrigConvEvent = ADC_ExternalTrigConvEvent_0;
26
  ADC_InitStruct.ADC_ExternalTrigEventEdge = ADC_ExternalTrigEventEdge_None;
27
  ADC_InitStruct.ADC_NbrOfRegChannel = 1;
28
  ADC_InitStruct.ADC_OverrunMode = DISABLE;
29
  ADC_InitStruct.ADC_Resolution = ADC_Resolution_12b;
30
  ADC_Init(ADC1, ADC_InitStruct);
31

32
  ADC_RegularChannelConfig();
33

34
  ADC_Cmd(ADC1, ENABLE);
35

36
  ADC_StartConversion(ADC1);
37
}


Danke im Voraus!
MfG,
Phil
Gast #3089883
Lesenswert?

Tag,
Ich arbeite mit Phil am gleichen Projekt und der Code kompiliert nicht, 
das ist war. Folgender Code compiliert, funktioniert aber trotzdem 
nicht.

Wir verwenden hier ADC1, Channel 7 auf Pin C1:

[c]
#include "stm32f30x.h"
#include "stm32f30x_rcc.h"
#include "stm32f30x_gpio.h"
#include "stm32f30x_adc.h"

#include "LED.h"

unsigned long i;

int main(void)
{
  LED_Init();

  RCC_ADCCLKConfig(RCC_ADC12PLLCLK_Div8);
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_ADC12, ENABLE);

  GPIO_InitTypeDef GPIO_InitStruct;
  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AN;
  GPIO_InitStruct.GPIO_Pin = GPIO_Pin_1;
  GPIO_Init(GPIOC, &GPIO_InitStruct);

  ADC_InitTypeDef ADC_InitStruct;
  ADC_InitStruct.ADC_AutoInjMode = ADC_AutoInjec_Disable;
  ADC_InitStruct.ADC_ContinuousConvMode = 
ADC_ContinuousConvMode_Disable;
  ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStruct.ADC_ExternalTrigConvEvent = 
ADC_ExternalTrigConvEvent_0;
  ADC_InitStruct.ADC_ExternalTrigEventEdge = 
ADC_ExternalTrigInjecEventEdge_None;
  ADC_InitStruct.ADC_NbrOfRegChannel = 1;
  ADC_InitStruct.ADC_OverrunMode = ADC_OverrunMode_Disable;
  ADC_InitStruct.ADC_Resolution = ADC_Resolution_8b;
  ADC_Init(ADC1, &ADC_InitStruct);

  ADC_RegularChannelConfig(ADC1, ADC_Channel_7, 1, 
ADC_SampleTime_1Cycles5);

  ADC_Cmd(ADC1, ENABLE);

  for(i = 0; i < 100000; i++);

  LED_Write(0xAA);

  while(1)
  {
    ADC_StartConversion(ADC1);
    while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
    LED_Write(ADC_GetConversionValue(ADC1));
  }

  return 0;
}
[/c)

Ich habe auch bereits versucht folgenden Teil einzufügen (vor 
RegularChannelConfig):
[c]
  ADC_CommonInitTypeDef ADC_CommonInitStruct;
  ADC_CommonInitStruct.ADC_Clock = ADC_Clock_AsynClkMode;
  ADC_CommonInitStruct.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
  ADC_CommonInitStruct.ADC_DMAMode = ADC_DMAMode_OneShot;
  ADC_CommonInitStruct.ADC_Mode = ADC_Mode_Independent;
  ADC_CommonInitStruct.ADC_TwoSamplingDelay = 0;
  ADC_CommonInit(ADC1, &ADC_CommonInitStruct);[\c]

Hoffe irgendwer kann helfen...

µC: STM32F303VC auf einem STM32F3discovery-board, programmiert mit 
eclipse, compiliert mit gcc

mfg
Florian
Gast #3090279
Lesenswert?

Ich habe den Code jetzt genauso eingebunden. Das einzige was ich 
geändert habe ist, den delay mit einer einfachen for-schleife statt per 
systemclock zu realisieren.
Trotzdem funktioniert es nicht, d.h. es wird nichts auf den LEDs 
angezeigt (die LED Funktionen funktionieren brigens Einwand frei).
1
int main(void)
2
{
3
  /*!< At this stage the microcontroller clock setting is already configured,
4
       this is done through SystemInit() function which is called from startup
5
       file (startup_stm32f30x.s) before to branch to application main.
6
       To reconfigure the default setting of SystemInit() function, refer to
7
       system_stm32f30x.c file
8
     */
9

10
  LED_Init();
11
  
12
  LED_Write(0x01);
13

14
  /* Configure the ADC clock */
15
  RCC_ADCCLKConfig(RCC_ADC12PLLCLK_Div2);
16

17
  /* Enable ADC1 clock */
18
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_ADC12, ENABLE);
19

20
  /* ADC Channel configuration */
21
  /* GPIOC Periph clock enable */
22
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
23

24
  /* Configure ADC Channel7 as analog input */
25
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 ;
26
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
27
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
28
  GPIO_Init(GPIOC, &GPIO_InitStructure);
29

30
  ADC_StructInit(&ADC_InitStructure);
31

32
  /* Calibration procedure */
33
  ADC_VoltageRegulatorCmd(ADC2, ENABLE);
34

35
  for(i = 0; i < 100000; i++);
36

37
  ADC_SelectCalibrationMode(ADC2, ADC_CalibrationMode_Single);
38
  ADC_StartCalibration(ADC2);
39

40
  while(ADC_GetCalibrationStatus(ADC2) != RESET );
41
  calibration_value = ADC_GetCalibrationValue(ADC2);
42

43
  ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
44
  ADC_CommonInitStructure.ADC_Clock = ADC_Clock_AsynClkMode;
45
  ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
46
  ADC_CommonInitStructure.ADC_DMAMode = ADC_DMAMode_OneShot;
47
  ADC_CommonInitStructure.ADC_TwoSamplingDelay = 0;
48
  ADC_CommonInit(ADC2, &ADC_CommonInitStructure);
49

50
  ADC_InitStructure.ADC_ContinuousConvMode = ADC_ContinuousConvMode_Enable;
51
  ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
52
  ADC_InitStructure.ADC_ExternalTrigConvEvent = ADC_ExternalTrigConvEvent_0;
53
  ADC_InitStructure.ADC_ExternalTrigEventEdge = ADC_ExternalTrigEventEdge_None;
54
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
55
  ADC_InitStructure.ADC_OverrunMode = ADC_OverrunMode_Disable;
56
  ADC_InitStructure.ADC_AutoInjMode = ADC_AutoInjec_Disable;
57
  ADC_InitStructure.ADC_NbrOfRegChannel = 1;
58
  ADC_Init(ADC2, &ADC_InitStructure);
59

60
  /* ADC1 regular channel7 configuration */
61
  ADC_RegularChannelConfig(ADC2, ADC_Channel_7, 1, ADC_SampleTime_7Cycles5);
62

63
  /* Enable ADC1 */
64
  ADC_Cmd(ADC2, ENABLE);
65

66
  /* wait for ADRDY */
67
  while(!ADC_GetFlagStatus(ADC2, ADC_FLAG_RDY));
68

69
  /* Start ADC1 Software Conversion */
70
  ADC_StartConversion(ADC2);
71

72
  /* Infinite loop */
73
  while (1)
74
  {
75
    /* Test EOC flag */
76
    while(ADC_GetFlagStatus(ADC2, ADC_FLAG_EOC) == RESET);
77

78
    /* Get ADC1 converted data */
79
    ADC1ConvertedValue = ADC_GetConversionValue(ADC2);
80

81
    LED_Write(ADC1ConvertedValue);
82

83
    /* Compute the voltage */
84
    ADC1ConvertedVoltage = (ADC1ConvertedValue *3300)/0xFFF;
85

86
  }
87
}

mfg Flo

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren