1 | void Init_LDR()
|
2 | {
|
3 | GPIO_InitTypeDef GPIO_InitStructure;
|
4 | ADC_InitTypeDef ADC_InitStructure;
|
5 |
|
6 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); // GPIOA Clock enable
|
7 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); // ADC Clock enable
|
8 |
|
9 | // ADC Clock auf unter 14 Mhz bringen
|
10 | RCC_ADCCLKConfig(RCC_PCLK2_Div8);
|
11 |
|
12 | /* ADC Pins konfigurieren A0 und B0----------------------------------------*/
|
13 | GPIO_InitStructure.GPIO_Pin = LDR_Pin; // AN1 Pin (PA0) aktivieren
|
14 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; // Analog Input
|
15 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
16 | GPIO_Init(GPIOA, &GPIO_InitStructure);
|
17 |
|
18 | GPIO_InitStructure.GPIO_Pin = VOLT_Pin; // AN1 Pin (PA0) aktivieren
|
19 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; // Analog Input
|
20 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
21 | GPIO_Init(GPIOB, &GPIO_InitStructure);
|
22 |
|
23 | /* ADC1 configuration ------------------------------------------------------*/
|
24 | ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; // ADC1 werkelt allein
|
25 | ADC_InitStructure.ADC_ScanConvMode = DISABLE; // Nur 1 Kanal, keine Scans
|
26 | ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; // ständige Wandlungen aus
|
27 | ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; // Start mit Software, nicht durch Hardware
|
28 | ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; // Bits rechts ausrichten im Half-Word
|
29 | ADC_InitStructure.ADC_NbrOfChannel = 1; // 1 Kanal sampeln
|
30 | ADC_Init(ADC1, &ADC_InitStructure);
|
31 |
|
32 | ADC_Cmd(ADC1, ENABLE);
|
33 |
|
34 | ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_41Cycles5); // PA0
|
35 | /* Enable ADC1 reset calibaration register */
|
36 | ADC_ResetCalibration(ADC1);
|
37 | /* Check the end of ADC1 reset calibration register */
|
38 | while(ADC_GetResetCalibrationStatus(ADC1));
|
39 | /* Start ADC1 calibaration */
|
40 | ADC_StartCalibration(ADC1);
|
41 | /* Check the end of ADC1 calibration */
|
42 | while(ADC_GetCalibrationStatus(ADC1));
|
43 | /* Keine Triggerung durch Events etc */
|
44 | ADC_ExternalTrigConvCmd(ADC1, DISABLE);
|
45 |
|
46 | ADC_SoftwareStartConvCmd(ADC1, ENABLE);
|
47 | }
|
48 |
|
49 | /* Holt einen AD Wert aus dem LDR und triggert neue Wandlung an */
|
50 | uint16_t GetLDRValue()
|
51 | {
|
52 | while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
|
53 | uint16_t val = ADC_GetConversionValue(ADC1);
|
54 | ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_55Cycles5); // PB0
|
55 | /* Nächste Wandlung starten */
|
56 | ADC_SoftwareStartConvCmd(ADC1, ENABLE);
|
57 | return(val);
|
58 | }
|
59 |
|
60 | /* Liest die Batteriespannung aus */
|
61 | uint16_t GetBattValue()
|
62 | {
|
63 | while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
|
64 | uint16_t val = ADC_GetConversionValue(ADC1);
|
65 | ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_55Cycles5); // PB0
|
66 | ADC_SoftwareStartConvCmd(ADC1, ENABLE);
|
67 | return(val);
|
68 | }
|