1 | #include "stm32f429i_discovery_lcd2.h"
|
2 | #include "stdlib.h"
|
3 | #include "tm_stm32f4_usart.h"
|
4 |
|
5 |
|
6 | #define Sysreload 3600
|
7 | // 3600/180MHz = 20us--> Interrupt alle 20us
|
8 |
|
9 |
|
10 | #define Buffergroesse 900
|
11 | // Signal hat eine Periodendauer 4,5ms
|
12 | // 225*20us = 4,5ms
|
13 | // --> ADC mit 4 Kanäle--> 225*4 = 900
|
14 |
|
15 | void adc1_init(void);
|
16 | uint16_t ADC1_result[Buffergroesse];
|
17 | u16 Transferwerte[Buffergroesse];
|
18 |
|
19 | int main(void)
|
20 | {
|
21 |
|
22 | SysTick_Config(Sysreload); // IR alle 20us
|
23 | adc1_init();
|
24 | while(1)
|
25 | {
|
26 |
|
27 | }
|
28 |
|
29 |
|
30 | }
|
31 |
|
32 |
|
33 | void DMA2_Stream0_IRQHandler(void)
|
34 | {
|
35 |
|
36 | DMA_ClearITPendingBit(DMA2_Stream0, DMA_IT_TCIF0);
|
37 | }
|
38 |
|
39 | void SysTick_Handler(void)
|
40 | {
|
41 |
|
42 | ADC_SoftwareStartConv(ADC1);
|
43 |
|
44 | }
|
45 |
|
46 | void adc1_init(void)
|
47 | {
|
48 |
|
49 | GPIO_InitTypeDef GPIO_InitStruct;
|
50 | ADC_InitTypeDef ADC_InitStructure;
|
51 | ADC_CommonInitTypeDef ADC_CommonInitStructure;
|
52 | NVIC_InitTypeDef NVIC_InitStructure;
|
53 | DMA_InitTypeDef DMA_InitStructure;
|
54 |
|
55 |
|
56 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); //Takt AHB1 fuer PC freischalten
|
57 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //Takt AHB1 fuer PC freischalten
|
58 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); //Takt APB2 (ADC1) freischalten
|
59 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
|
60 |
|
61 |
|
62 | /*******Konfigurieren des Ports PC1 (ADC1 Channel11) als analog input **********/
|
63 | GPIO_InitStruct.GPIO_Pin = GPIO_Pin_1;
|
64 | GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AN;
|
65 | GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL ;
|
66 | GPIO_Init(GPIOC, &GPIO_InitStruct);
|
67 |
|
68 | //PC2 PC1 PA7 PA5 funktionieren für ADC
|
69 | /*******Konfigurieren des Ports PC2 (ADC1 Channel12) als analog input **********/
|
70 | GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2;
|
71 | GPIO_Init(GPIOC, &GPIO_InitStruct);
|
72 |
|
73 | /*******Konfigurieren des Ports PA7 (ADC1 Channel7) als analog input **********/
|
74 | GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7;
|
75 | GPIO_Init(GPIOA, &GPIO_InitStruct);
|
76 |
|
77 | /*******Konfigurieren des Ports PA5 (ADC1 Channel5) als analog input **********/
|
78 | GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5;
|
79 | GPIO_Init(GPIOA, &GPIO_InitStruct);
|
80 |
|
81 |
|
82 | DMA_StructInit(&DMA_InitStructure);
|
83 | DMA_InitStructure.DMA_Channel = DMA_Channel_0;
|
84 | DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(ADC1->DR);
|
85 | DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADC1_result;
|
86 | DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
|
87 | DMA_InitStructure.DMA_BufferSize = Buffergroesse;
|
88 | DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
|
89 | DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
|
90 | DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
|
91 | DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
|
92 | DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
|
93 | DMA_InitStructure.DMA_Priority = DMA_Priority_High;
|
94 | DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
|
95 | DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
|
96 | DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
|
97 | DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
|
98 | DMA_Init(DMA2_Stream0, &DMA_InitStructure);
|
99 | DMA_Cmd(DMA2_Stream0, ENABLE);
|
100 |
|
101 | DMA_ITConfig(DMA2_Stream0, DMA_IT_TC, ENABLE);
|
102 |
|
103 | // NVIC: IR für ADC einschalten und auf die niedrigste Priorität setzen
|
104 | NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream0_IRQn;
|
105 | NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
|
106 | NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0D;
|
107 | NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
108 | NVIC_Init(&NVIC_InitStructure);
|
109 |
|
110 |
|
111 | /* ----------Allgemeine Initialisierung der ADCs -------------------------------*/
|
112 | ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; //Nur 1 einzelner ADC
|
113 |
|
114 |
|
115 | ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2; //ADC Takt wird auf die hälfte geteilt = * 1/2
|
116 |
|
117 | ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; //kein DMA
|
118 | ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles; //minimale Verzögerung zwischen 2 Abtastungen
|
119 | ADC_CommonInit(&ADC_CommonInitStructure);
|
120 |
|
121 | /*------------------ Initialisierung des ADC1 ---------------------------------*/
|
122 | ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; //12 Bit Auflösung
|
123 | ADC_InitStructure.ADC_ScanConvMode = ENABLE; // nur ein Kanal
|
124 | ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; // single mode conversion
|
125 |
|
126 | //Keine externe Triggerung der Wandlung,
|
127 | ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
|
128 | ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
|
129 |
|
130 | ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //Datenausrichtung
|
131 | ADC_InitStructure.ADC_NbrOfConversion = 4; // nur ein Kanal
|
132 | ADC_Init(ADC1, &ADC_InitStructure);
|
133 |
|
134 |
|
135 | /*----------- ADC1 Konfiguration des Kanals (regular group) --------------------*/
|
136 | ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 1, ADC_SampleTime_15Cycles); //Kanal 13, 1. Kanal, minimale Abtastzeit
|
137 | ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 2, ADC_SampleTime_15Cycles);
|
138 | ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 3, ADC_SampleTime_15Cycles);
|
139 | ADC_RegularChannelConfig(ADC1, ADC_Channel_7, 4, ADC_SampleTime_15Cycles);
|
140 | ADC_DiscModeCmd(ADC1,ENABLE); //Betriebsart Discontinuos Mode, eig. default
|
141 |
|
142 |
|
143 | ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);
|
144 |
|
145 | ADC_DMACmd(ADC1, ENABLE);
|
146 |
|
147 |
|
148 | /* ----------------Enable ADC1 ------------------------------------------------*/
|
149 | ADC_Cmd(ADC1, ENABLE);
|
150 | }
|