1 | #include "stm32f4xx.h"
|
2 | #include "stm32f4xx_gpio.h"
|
3 | #include "stm32f4xx_rcc.h"
|
4 | #include "stm32f4xx_adc.h"
|
5 | #include "stm32f4xx_usart.h"
|
6 | #include "misc.h"
|
7 |
|
8 |
|
9 | void interruptConfig();
|
10 | void adcConfig();
|
11 | void usartConfig();
|
12 | void ledConfig();
|
13 |
|
14 |
|
15 | typedef struct{
|
16 | uint16_t buffer;
|
17 | int lock;
|
18 | }bufferStruct;
|
19 |
|
20 | bufferStruct bufferOne;
|
21 | bufferStruct bufferTwo;
|
22 |
|
23 |
|
24 | int main(void)
|
25 | {
|
26 | ledConfig();
|
27 | usartConfig();
|
28 | adcConfig();
|
29 |
|
30 | ADC_SoftwareStartConv(ADC3);
|
31 |
|
32 |
|
33 | ADC_ITConfig(ADC3, ADC_IT_EOC, ENABLE);
|
34 | USART_ITConfig(USART3, USART_IT_TXE, ENABLE);
|
35 |
|
36 |
|
37 | while(1)
|
38 | {
|
39 | }
|
40 | }
|
41 |
|
42 | void ADC_IRQHandler()
|
43 | {
|
44 | GPIO_ToggleBits(GPIOD, GPIO_Pin_12);
|
45 |
|
46 | if(bufferOne.lock== 0)
|
47 | {
|
48 | bufferOne.buffer =ADC_GetConversionValue(ADC3);
|
49 | bufferOne.lock = 1;
|
50 | }
|
51 | else if(bufferTwo.lock == 0)
|
52 | {
|
53 | bufferTwo.buffer = ADC_GetConversionValue(ADC3);
|
54 | bufferTwo.lock = 1;
|
55 | }
|
56 | ADC_ClearFlag(ADC3, ADC_FLAG_EOC);
|
57 | }
|
58 |
|
59 |
|
60 | void USART3_IRQHandler()
|
61 | {
|
62 | GPIO_ToggleBits(GPIOD, GPIO_Pin_13);
|
63 |
|
64 | if(bufferOne.lock== 1)
|
65 | {
|
66 | USART_SendData(USART3, (uint8_t) bufferOne.buffer );
|
67 | bufferOne.lock = 0;
|
68 | }
|
69 | else if(bufferTwo.lock == 1)
|
70 | {
|
71 | USART_SendData(USART3, (uint8_t) bufferTwo.buffer );
|
72 | bufferTwo.lock = 0;
|
73 | }
|
74 | USART_ClearFlag(USART3, USART_IT_TXE);
|
75 | }
|
76 |
|
77 |
|
78 |
|
79 |
|
80 | void ledConfig()
|
81 | {
|
82 | GPIO_InitTypeDef led_gpio;
|
83 |
|
84 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
|
85 |
|
86 | led_gpio.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13;
|
87 | led_gpio.GPIO_Mode = GPIO_Mode_OUT;
|
88 | led_gpio.GPIO_OType = GPIO_OType_PP;
|
89 | led_gpio.GPIO_Speed = GPIO_Speed_50MHz;
|
90 | led_gpio.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
91 | GPIO_Init(GPIOD, &led_gpio);
|
92 | }
|
93 |
|
94 | void adcConfig()
|
95 | {
|
96 | ADC_InitTypeDef ADC_InitStructure;
|
97 | ADC_CommonInitTypeDef ADC_CommonInitStructure;
|
98 | GPIO_InitTypeDef GPIO_InitStructure;
|
99 | NVIC_InitTypeDef NVIC_adc;
|
100 |
|
101 |
|
102 | RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOC, ENABLE);
|
103 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC3, ENABLE);
|
104 |
|
105 |
|
106 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
|
107 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
|
108 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
|
109 | GPIO_Init(GPIOC, &GPIO_InitStructure);
|
110 |
|
111 |
|
112 | ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
|
113 | ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
|
114 | ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
|
115 | ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
|
116 | ADC_CommonInit(&ADC_CommonInitStructure);
|
117 |
|
118 |
|
119 | ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
|
120 | ADC_InitStructure.ADC_ScanConvMode = DISABLE;
|
121 | ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
|
122 | ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
|
123 | ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
|
124 | ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
|
125 | ADC_InitStructure.ADC_NbrOfConversion = 1;
|
126 | ADC_Init(ADC3, &ADC_InitStructure);
|
127 |
|
128 |
|
129 | ADC_RegularChannelConfig(ADC3, ADC_Channel_12, 1, ADC_SampleTime_3Cycles);
|
130 |
|
131 | ADC_Cmd(ADC3, ENABLE);
|
132 |
|
133 | NVIC_adc.NVIC_IRQChannel = ADC_IRQn;
|
134 | NVIC_adc.NVIC_IRQChannelCmd = ENABLE;
|
135 | NVIC_adc.NVIC_IRQChannelPreemptionPriority = 0x0F;
|
136 | NVIC_adc.NVIC_IRQChannelSubPriority = 0x01;
|
137 | NVIC_Init(&NVIC_adc);
|
138 |
|
139 | }
|
140 |
|
141 | void usartConfig()
|
142 | {
|
143 | GPIO_InitTypeDef GPIO_InitStructure;
|
144 | USART_InitTypeDef USART_InitStructure;
|
145 | NVIC_InitTypeDef NVIC_usart;
|
146 |
|
147 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
|
148 |
|
149 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
|
150 |
|
151 | GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_USART3);
|
152 |
|
153 | GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_USART3);
|
154 |
|
155 | GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
|
156 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
|
157 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
|
158 |
|
159 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
|
160 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
161 | GPIO_Init(GPIOC, &GPIO_InitStructure);
|
162 |
|
163 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
|
164 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
|
165 | GPIO_Init(GPIOC, &GPIO_InitStructure);
|
166 |
|
167 | USART_InitStructure.USART_BaudRate = 11500;
|
168 | USART_InitStructure.USART_WordLength = USART_WordLength_8b;
|
169 | USART_InitStructure.USART_StopBits = USART_StopBits_1;
|
170 | USART_InitStructure.USART_Parity = USART_Parity_No;
|
171 | USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
|
172 | USART_InitStructure.USART_Mode = USART_Mode_Tx;
|
173 |
|
174 |
|
175 | USART_Init(USART3, &USART_InitStructure);
|
176 |
|
177 |
|
178 | USART_Cmd(USART3, ENABLE);
|
179 |
|
180 | NVIC_usart.NVIC_IRQChannel = USART3_IRQn;
|
181 | NVIC_usart.NVIC_IRQChannelCmd= ENABLE;
|
182 | NVIC_usart.NVIC_IRQChannelPreemptionPriority = 0x01;
|
183 | NVIC_usart.NVIC_IRQChannelSubPriority = 0x01;
|
184 | NVIC_Init(&NVIC_usart);
|
185 | }
|