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 | }
|