1 | #include <stdio.h>
|
2 | #include "diag/Trace.h"
|
3 | #include "stm32f0xx.h"
|
4 | #include "stm32f0xx_conf.h"
|
5 |
|
6 |
|
7 | int main(int argc, char* argv[])
|
8 | {
|
9 |
|
10 | //(#) ADC pins (pA1) configuration --------------------------------------------------------------
|
11 |
|
12 | GPIO_InitTypeDef GPIO_InitStructure;
|
13 | RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
|
14 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
|
15 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
|
16 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
|
17 | GPIO_Init(GPIOA, &GPIO_InitStructure);
|
18 |
|
19 | //leds (pB0... pB3) configuration--------------------------------------------------------------
|
20 |
|
21 |
|
22 | GPIO_InitTypeDef GPIO_LEDs;
|
23 | RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
|
24 | GPIO_LEDs.GPIO_Pin = GPIO_Pin_0 |GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
|
25 | GPIO_LEDs.GPIO_Mode = GPIO_Mode_OUT;
|
26 | GPIO_LEDs.GPIO_OType = GPIO_OType_PP;
|
27 | GPIO_LEDs.GPIO_Speed = GPIO_Speed_50MHz;
|
28 | GPIO_LEDs.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
29 | GPIO_Init(GPIOB, &GPIO_LEDs);
|
30 | //ADC-------------------------------------------------------------------------------------------
|
31 | ADC_InitTypeDef ADC_InitStructure;
|
32 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
|
33 | ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
|
34 | ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
|
35 | ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
|
36 | ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
|
37 | ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward;
|
38 | ADC_Init(ADC1, &ADC_InitStructure);
|
39 |
|
40 | ADC_GetCalibrationFactor(ADC1);
|
41 | ADC_Cmd(ADC1, ENABLE);
|
42 | while(ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN) == RESET);
|
43 | ADC_StartOfConversion(ADC1);
|
44 | uint16_t valueADC = ADC_GetConversionValue(ADC1);
|
45 | ADC_ChannelConfig(ADC1, ADC_Channel_1, ADC_SampleTime_1_5Cycles);
|
46 |
|
47 | //dma--------------------------------------------------------------------------------------
|
48 | DMA_InitTypeDef DMA_InitStructure;
|
49 | RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 , ENABLE);
|
50 | DMA_DeInit(DMA1_Channel1);
|
51 | DMA_InitStructure.DMA_PeripheralBaseAddr= (uint32_t)&ADC1->DR;
|
52 | DMA_InitStructure.DMA_MemoryBaseAddr=(uint32_t)&valueADC;
|
53 | DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
|
54 | DMA_InitStructure.DMA_BufferSize = 2;
|
55 | DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
|
56 | DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
|
57 | DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
|
58 | DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
|
59 | DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
|
60 | DMA_InitStructure.DMA_Priority = DMA_Priority_High;
|
61 | DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
|
62 | DMA_Init(DMA1_Channel1, &DMA_InitStructure);
|
63 | DMA_Cmd(DMA1_Channel1, ENABLE);
|
64 | ADC_DMARequestModeConfig(ADC1, ADC_DMAMode_Circular);
|
65 | ADC_DMACmd(ADC1, ENABLE);
|
66 |
|
67 |
|
68 | while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));//Processing the conversion
|
69 |
|
70 | return ADC_GetConversionValue(ADC1); //Return the converted data
|
71 | ADC_StartOfConversion(ADC1);
|
72 |
|
73 | while (1)
|
74 | if(valueADC<2000){
|
75 | GPIO_SetBits(GPIOB, GPIO_Pin_0);
|
76 | }
|
77 | else if(valueADC>2000){
|
78 | GPIO_SetBits(GPIOB, GPIO_Pin_1);
|
79 | }
|
80 | }
|