1 | #include "stm32f4xx_adc.h"
|
2 | #include "stm32f4xx_gpio.h"
|
3 | #include "stm32f4xx_rcc.h"
|
4 | #include "stm32f4xx_dma.h"
|
5 | #include "variables.h"
|
6 |
|
7 | void adc_init(void){
|
8 | ADC_InitTypeDef ADC_InitStruct;
|
9 | ADC_CommonInitTypeDef ADC_CommonInitStruct;
|
10 | DMA_InitTypeDef DMA_InitStruct;
|
11 | GPIO_InitTypeDef GPIO_InitStruct;
|
12 | /* Enable ADC3, DMA2 and GPIO clocks ****************************************/
|
13 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2 | RCC_AHB1Periph_GPIOC, ENABLE);
|
14 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC3, ENABLE);//ADC3 is connected to the APB2 peripheral bus
|
15 | /* DMA2 Stream0 channel0 configuration **************************************/
|
16 | DMA_InitStruct.DMA_Channel = DMA_Channel_2;
|
17 | DMA_InitStruct.DMA_PeripheralBaseAddr = (uint32_t)&ADC3->DR;//ADC3's data register
|
18 | DMA_InitStruct.DMA_Memory0BaseAddr = (uint32_t)&ADCConvertedValue;
|
19 | DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralToMemory;
|
20 | DMA_InitStruct.DMA_BufferSize = (uint32_t) 8;
|
21 | DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
|
22 | DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;
|
23 | DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;//Reads 16 bit values
|
24 | DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;//Stores 16 bit values
|
25 | DMA_InitStruct.DMA_Mode = DMA_Mode_Circular;
|
26 | DMA_InitStruct.DMA_Priority = DMA_Priority_High;
|
27 | DMA_InitStruct.DMA_FIFOMode = DMA_FIFOMode_Disable;
|
28 | DMA_InitStruct.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
|
29 | DMA_InitStruct.DMA_MemoryBurst = DMA_MemoryBurst_Single;
|
30 | DMA_InitStruct.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
|
31 | DMA_Init(DMA2_Stream0, &DMA_InitStruct);
|
32 | DMA_Cmd(DMA2_Stream0, ENABLE);
|
33 | /* Configure GPIO pins ******************************************************/
|
34 | GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;// PC0, PC1, PC2, PC3
|
35 | GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AN;//The pins are configured in analog mode
|
36 | GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
|
37 | GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
|
38 | GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL ;//We don't need any pull up or pull down
|
39 | GPIO_Init(GPIOC, &GPIO_InitStruct);//Initialize GPIOC pins with the configuration
|
40 | /* ADC Common Init **********************************************************/
|
41 | ADC_CommonInitStruct.ADC_Mode = ADC_Mode_Independent;
|
42 | ADC_CommonInitStruct.ADC_Prescaler = ADC_Prescaler_Div2;
|
43 | ADC_CommonInitStruct.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
|
44 | ADC_CommonInitStruct.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
|
45 | ADC_CommonInit(&ADC_CommonInitStruct);
|
46 | /* ADC3 Init ****************************************************************/
|
47 | ADC_DeInit();
|
48 | ADC_InitStruct.ADC_Resolution = ADC_Resolution_12b;//Input voltage is converted into a 12bit int (max 4095)
|
49 | ADC_InitStruct.ADC_ScanConvMode = ENABLE;//The scan is configured in multiple channels
|
50 | ADC_InitStruct.ADC_ContinuousConvMode = ENABLE;//Continuous conversion: input signal is sampled more than once
|
51 | ADC_InitStruct.ADC_ExternalTrigConv = 0;
|
52 | ADC_InitStruct.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
|
53 | ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right;//Data converted will be shifted to right
|
54 | ADC_InitStruct.ADC_NbrOfConversion = 4;
|
55 | ADC_Init(ADC3, &ADC_InitStruct);//Initialize ADC with the configuration
|
56 | /* Select the channels to be read from **************************************/
|
57 | ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_144Cycles);//PC0
|
58 | ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 2, ADC_SampleTime_144Cycles);//PC1
|
59 | ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 3, ADC_SampleTime_144Cycles);//PC2
|
60 | ADC_RegularChannelConfig(ADC1, ADC_Channel_13, 4, ADC_SampleTime_144Cycles);//PC3
|
61 | /* Enable DMA request after last transfer (Single-ADC mode) */
|
62 | ADC_DMARequestAfterLastTransferCmd(ADC3, ENABLE);
|
63 | /* Enable ADC3 DMA */
|
64 | ADC_DMACmd(ADC3, ENABLE);
|
65 | /* Enable ADC3 */
|
66 | ADC_Cmd(ADC3, ENABLE);
|
67 | /* Start ADC3 Software Conversion */
|
68 | ADC_SoftwareStartConv(ADC3);
|
69 | }
|