1 | /* Configure the ADC clock */
|
2 | RCC_ADCCLKConfig(RCC_ADC12PLLCLK_Div2);
|
3 |
|
4 | /* Enable ADC1 clock */
|
5 | RCC_AHBPeriphClockCmd(RCC_AHBPeriph_ADC12, ENABLE);
|
6 |
|
7 |
|
8 |
|
9 | /* Setup SysTick Timer for 1 µsec interrupts */
|
10 | if (SysTick_Config(SystemCoreClock / 10000))
|
11 | {
|
12 | /* Capture error */
|
13 | while (1)
|
14 | {}
|
15 | }
|
16 |
|
17 | /* ADC Channel configuration */
|
18 | /* GPIOC Periph clock enable */
|
19 | RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
|
20 |
|
21 | /* Configure ADC Channel7 as analog input */
|
22 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 ;
|
23 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
|
24 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
|
25 | GPIO_Init(GPIOC, &GPIO_InitStructure);
|
26 |
|
27 |
|
28 | ADC_StructInit(&ADC_InitStructure);
|
29 |
|
30 | /* Calibration procedure */
|
31 | ADC_VoltageRegulatorCmd(ADC1, ENABLE);
|
32 |
|
33 | /* Insert delay equal to 10 µs */
|
34 | // Delay(10);
|
35 |
|
36 | ADC_SelectCalibrationMode(ADC1, ADC_CalibrationMode_Single);
|
37 | ADC_StartCalibration(ADC1);
|
38 |
|
39 | while(ADC_GetCalibrationStatus(ADC1) != RESET );
|
40 | calibration_value = ADC_GetCalibrationValue(ADC1);
|
41 |
|
42 | ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
|
43 | /* ADC_CommonInitStructure.ADC_Clock = ADC_Clock_AsynClkMode;
|
44 | ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
|
45 | ADC_CommonInitStructure.ADC_DMAMode = ADC_DMAMode_OneShot;
|
46 | ADC_CommonInitStructure.ADC_TwoSamplingDelay = 9;*/
|
47 |
|
48 | ADC_CommonInit(ADC1, &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 = 2;
|
58 | ADC_Init(ADC1, &ADC_InitStructure);
|
59 |
|
60 | /* ADC1 regular channel6 configuration */
|
61 | ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 1, ADC_SampleTime_19Cycles5);
|
62 | ADC_RegularChannelConfig(ADC1, ADC_Channel_7, 2, ADC_SampleTime_19Cycles5);
|
63 |
|
64 | /* Enable ADC1 */
|
65 | ADC_Cmd(ADC1, ENABLE);
|
66 |
|
67 | /* wait for ADRDY */
|
68 | while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_RDY));
|
69 |
|
70 | /* Start ADC1 Software Conversion */
|
71 | ADC_StartConversion(ADC1);
|
72 |
|
73 | /* Infinite loop */
|
74 | while (1)
|
75 | {
|
76 | /* Test EOC flag */
|
77 | while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
|
78 |
|
79 | /* Get ADC1 converted data */
|
80 | ADC1ConvertedValue =ADC_GetConversionValue(ADC1);
|
81 |
|
82 | /* Compute the voltage */
|
83 | ADC1ConvertedVoltage = (ADC1ConvertedValue *3000)/0xFFF;
|
84 |
|
85 |
|
86 |
|
87 |
|
88 | }
|
89 | }
|