code giao tiep stm 32 với esp8266 điều khiển động cơ
code giao tiep stm 32 với esp8266 điều khiển động cơ
code giao tiep stm 32 với esp8266 điều khiển động cơ
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
Page 5
main.c Friday, January 19, 2024, 5:11 PM
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