code giao tiep stm 32 với esp8266 điều khiển động cơ

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

main.

c Friday, January 19, 2024, 5:11 PM

1 /* USER CODE BEGIN Header */


2 /**
3 ******************************************************************************
4 * @file : main.c
5 * @brief : Main program body
6 ******************************************************************************
7 * @attention
8 *
9 * Copyright (c) 2024 STMicroelectronics.
10 * All rights reserved.
11 *
12 * This software is licensed under terms that can be found in the LICENSE file
13 * in the root directory of this software component.
14 * If no LICENSE file comes with this software, it is provided AS-IS.
15 *
16 ******************************************************************************
17 */
18 /* USER CODE END Header */
19 /* Includes ------------------------------------------------------------------*/
20 #include "main.h"
21 #include "stdio.h"
22 #include "string.h"
23 char buffer[100];
24 int i = 20;
25 #define RS_Port GPIOD
26 #define EN_Port GPIOB
27 #define RW_Port GPIOB
28
29 #define LCD_RS_Pin GPIO_PIN_2
30 #define LCD_EN_Pin GPIO_PIN_3
31 #define LCD_RW_Pin GPIO_PIN_3
32
33 #define D4_Port GPIOD
34 #define D5_Port GPIOD
35 #define D6_Port GPIOD
36 #define D7_Port GPIOD
37 #define B4_Port GPIOD
38
39 #define DATA_D4 GPIO_PIN_4
40 #define DATA_D5 GPIO_PIN_5
41 #define DATA_D6 GPIO_PIN_6
42 #define DATA_D7 GPIO_PIN_7
43 #define DATA_B4 GPIO_PIN_4
44
45 #include "D:\THU VIEN\lcd.c"
46 ADC_HandleTypeDef hadc1;
47
48 UART_HandleTypeDef huart3;
49
50
51 void SystemClock_Config(void);
52 static void MX_GPIO_Init(void);
53 static void MX_ADC1_Init(void);
54 static void MX_USART3_UART_Init(void);
55
56 volatile uint16_t value =0;
57 int nhietdo=0;
58 char Rx_data[7];
59

Page 1
main.c Friday, January 19, 2024, 5:11 PM

60
61 int main(void)
62 {
63 HAL_Init();
64
65 SystemClock_Config();
66 MX_GPIO_Init();
67 MX_ADC1_Init();
68 MX_USART3_UART_Init();
69
70
71 LCD_Init();
72 LCD_Gotoxy(1,0);
73 LCD_Puts("vinh hau");
74
75 while (1)
76 {
77
78 HAL_ADC_Start(&hadc1);
79 HAL_ADC_PollForConversion(&hadc1, 100);
80 value = HAL_ADC_GetValue(&hadc1);
81 nhietdo = value*(3.3 / 4096)*1000.0/10.0;
82 LCD_Gotoxy(1,1);
83 LCD_PutChar(nhietdo /10 +48);
84 LCD_Gotoxy(2,1);
85 LCD_PutChar(nhietdo % 10 +48);
86
87 HAL_UART_Transmit(&huart3, (uint8_t*)buffer, sprintf(buffer, "NHIET DO LA %d",
nhietdo), 500);
88 HAL_Delay(5);
89 HAL_UART_Transmit(&huart3,(uint8_t*)"\r\n",2,1000);
90 HAL_Delay(5);
91
92 if((HAL_UART_Receive(&huart3,(uint8_t*)& Rx_data,1,1000))==HAL_OK)
93 {
94 if(Rx_data[0]=='B')
95 {
96
97 HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0,0);
98 HAL_GPIO_WritePin(GPIOC, GPIO_PIN_1,1);
99 HAL_GPIO_WritePin(GPIOC, GPIO_PIN_2,0);
100 HAL_GPIO_WritePin(GPIOC, GPIO_PIN_3,1);
101 HAL_GPIO_WritePin(GPIOE, GPIO_PIN_12,0);
102 HAL_GPIO_WritePin(GPIOE, GPIO_PIN_13,1);
103
104
105 }
106 if(Rx_data[0]=='T')
107 {
108
109 HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0,1);
110 HAL_GPIO_WritePin(GPIOC, GPIO_PIN_1,0);
111 HAL_GPIO_WritePin(GPIOC, GPIO_PIN_2,1);
112 HAL_GPIO_WritePin(GPIOC, GPIO_PIN_3,0);
113 HAL_GPIO_WritePin(GPIOE, GPIO_PIN_12,1);
114 HAL_GPIO_WritePin(GPIOE, GPIO_PIN_13,0);
115
116 }
117 }

Page 2
main.c Friday, January 19, 2024, 5:11 PM

118
119 }
120 /* USER CODE END 3 */
121 }
122
123 /**
124 * @brief System Clock Configuration
125 * @retval None
126 */
127 void SystemClock_Config(void)
128 {
129 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
130 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
131
132 /** Configure the main internal regulator output voltage
133 */
134 __HAL_RCC_PWR_CLK_ENABLE();
135 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
136
137 /** Initializes the RCC Oscillators according to the specified parameters
138 * in the RCC_OscInitTypeDef structure.
139 */
140 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
141 RCC_OscInitStruct.HSIState = RCC_HSI_ON;
142 RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
143 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
144 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
145 {
146 Error_Handler();
147 }
148
149 /** Initializes the CPU, AHB and APB buses clocks
150 */
151 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
152 |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
153 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
154 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
155 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
156 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
157
158 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
159 {
160 Error_Handler();
161 }
162 }
163
164 /**
165 * @brief ADC1 Initialization Function
166 * @param None
167 * @retval None
168 */
169 static void MX_ADC1_Init(void)
170 {
171
172 /* USER CODE BEGIN ADC1_Init 0 */
173
174 /* USER CODE END ADC1_Init 0 */
175
176 ADC_ChannelConfTypeDef sConfig = {0};

Page 3
main.c Friday, January 19, 2024, 5:11 PM

177
178 /* USER CODE BEGIN ADC1_Init 1 */
179
180 /* USER CODE END ADC1_Init 1 */
181
182 /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and
number of conversion)
183 */
184 hadc1.Instance = ADC1;
185 hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
186 hadc1.Init.Resolution = ADC_RESOLUTION_12B;
187 hadc1.Init.ScanConvMode = DISABLE;
188 hadc1.Init.ContinuousConvMode = ENABLE;
189 hadc1.Init.DiscontinuousConvMode = DISABLE;
190 hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
191 hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
192 hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
193 hadc1.Init.NbrOfConversion = 1;
194 hadc1.Init.DMAContinuousRequests = DISABLE;
195 hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
196 if (HAL_ADC_Init(&hadc1) != HAL_OK)
197 {
198 Error_Handler();
199 }
200
201 /** Configure for the selected ADC regular channel its corresponding rank in the
sequencer and its sample time.
202 */
203 sConfig.Channel = ADC_CHANNEL_0;
204 sConfig.Rank = 1;
205 sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
206 if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
207 {
208 Error_Handler();
209 }
210 /* USER CODE BEGIN ADC1_Init 2 */
211
212 /* USER CODE END ADC1_Init 2 */
213
214 }
215
216 /**
217 * @brief USART3 Initialization Function
218 * @param None
219 * @retval None
220 */
221 static void MX_USART3_UART_Init(void)
222 {
223
224 /* USER CODE BEGIN USART3_Init 0 */
225
226 /* USER CODE END USART3_Init 0 */
227
228 /* USER CODE BEGIN USART3_Init 1 */
229
230 /* USER CODE END USART3_Init 1 */
231 huart3.Instance = USART3;
232 huart3.Init.BaudRate = 9600;
233 huart3.Init.WordLength = UART_WORDLENGTH_8B;

Page 4
main.c Friday, January 19, 2024, 5:11 PM

234 huart3.Init.StopBits = UART_STOPBITS_1;


235 huart3.Init.Parity = UART_PARITY_NONE;
236 huart3.Init.Mode = UART_MODE_TX_RX;
237 huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
238 huart3.Init.OverSampling = UART_OVERSAMPLING_16;
239 if (HAL_UART_Init(&huart3) != HAL_OK)
240 {
241 Error_Handler();
242 }
243 /* USER CODE BEGIN USART3_Init 2 */
244
245 /* USER CODE END USART3_Init 2 */
246
247 }
248
249 /**
250 * @brief GPIO Initialization Function
251 * @param None
252 * @retval None
253 */
254 static void MX_GPIO_Init(void)
255 {
256 GPIO_InitTypeDef GPIO_InitStruct = {0};
257 /* USER CODE BEGIN MX_GPIO_Init_1 */
258 /* USER CODE END MX_GPIO_Init_1 */
259
260 /* GPIO Ports Clock Enable */
261 __HAL_RCC_GPIOH_CLK_ENABLE();
262 __HAL_RCC_GPIOC_CLK_ENABLE();
263 __HAL_RCC_GPIOA_CLK_ENABLE();
264 __HAL_RCC_GPIOE_CLK_ENABLE();
265 __HAL_RCC_GPIOD_CLK_ENABLE();
266 __HAL_RCC_GPIOB_CLK_ENABLE();
267
268 /*Configure GPIO pin Output Level */
269 HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3,
GPIO_PIN_RESET);
270
271 /*Configure GPIO pin Output Level */
272 HAL_GPIO_WritePin(GPIOE, GPIO_PIN_12|GPIO_PIN_13, GPIO_PIN_RESET);
273
274 /*Configure GPIO pin Output Level */
275 HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5
276 |GPIO_PIN_6|GPIO_PIN_7, GPIO_PIN_RESET);
277
278 /*Configure GPIO pin Output Level */
279 HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3|GPIO_PIN_4, GPIO_PIN_RESET);
280
281 /*Configure GPIO pins : PC0 PC1 PC2 PC3 */
282 GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3;
283 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
284 GPIO_InitStruct.Pull = GPIO_NOPULL;
285 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
286 HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
287
288 /*Configure GPIO pins : PE12 PE13 */
289 GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_13;
290 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
291 GPIO_InitStruct.Pull = GPIO_NOPULL;

Page 5
main.c Friday, January 19, 2024, 5:11 PM

292 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;


293 HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
294
295 /*Configure GPIO pins : PD10 PD11 */
296 GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11;
297 GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
298 GPIO_InitStruct.Pull = GPIO_NOPULL;
299 HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
300
301 /*Configure GPIO pins : PD2 PD3 PD4 PD5
302 PD6 PD7 */
303 GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5
304 |GPIO_PIN_6|GPIO_PIN_7;
305 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
306 GPIO_InitStruct.Pull = GPIO_NOPULL;
307 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
308 HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
309
310 /*Configure GPIO pins : PB3 PB4 */
311 GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_4;
312 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
313 GPIO_InitStruct.Pull = GPIO_NOPULL;
314 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
315 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
316
317 /* USER CODE BEGIN MX_GPIO_Init_2 */
318 /* USER CODE END MX_GPIO_Init_2 */
319 }
320
321 /* USER CODE BEGIN 4 */
322
323 /* USER CODE END 4 */
324
325 /**
326 * @brief This function is executed in case of error occurrence.
327 * @retval None
328 */
329 void Error_Handler(void)
330 {
331 /* USER CODE BEGIN Error_Handler_Debug */
332 /* User can add his own implementation to report the HAL error return state */
333 __disable_irq();
334 while (1)
335 {
336 }
337 /* USER CODE END Error_Handler_Debug */
338 }
339
340 #ifdef USE_FULL_ASSERT
341 /**
342 * @brief Reports the name of the source file and the source line number
343 * where the assert_param error has occurred.
344 * @param file: pointer to the source file name
345 * @param line: assert_param error line source number
346 * @retval None
347 */
348 void assert_failed(uint8_t *file, uint32_t line)
349 {
350 /* USER CODE BEGIN 6 */

Page 6
main.c Friday, January 19, 2024, 5:11 PM

351 /* User can add his own implementation to report the file name and line number,
352 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
353 /* USER CODE END 6 */
354 }
355 #endif /* USE_FULL_ASSERT */
356

Page 7

You might also like