-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfull Code.c
More file actions
332 lines (285 loc) · 10.1 KB
/
full Code.c
File metadata and controls
332 lines (285 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2025 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include <math.h>
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define MAX_TINGGI_CM 100.0f // Tinggi maksimum air
#define LEVEL_DIFF_OPEN_THRESHOLD 5.0f // Selisih cm untuk membuka gerbang
#define LEVEL_DIFF_EQUAL_THRESHOLD 1.0f // Toleransi dianggap seimbang
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
ADC_HandleTypeDef hadc1;
ADC_HandleTypeDef hadc2;
/* GPIO Pin Definitions */
#define WATER_LEVEL_WADUK_GPIO_PIN GPIO_PIN_1 // PA1
#define WATER_LEVEL_LUAR_GPIO_PIN GPIO_PIN_2 // PA2
#define RELAY_OPEN_GATE_GPIO_PIN GPIO_PIN_4 // PA4: Buka Gerbang
#define RELAY_CLOSE_GATE_GPIO_PIN GPIO_PIN_5 // PA5: Tutup Gerbang
#define RELAY_PUMP_GPIO_PIN GPIO_PIN_6 // PA6: Pompa Air
/* USER CODE BEGIN PV */
static uint8_t mode_pompa_kuras = 0;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
static void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_ADC1_Init(void);
static void MX_ADC2_Init(void);
float Convert_ADC_to_cm(uint32_t adc_value);
float Read_Water_Level_Waduk_cm(void);
float Read_Water_Level_Luar_cm(void);
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
HAL_Init();
/* CERT EXP33-C: Ensure that all system initialization functions succeed before use. */
/* Configure the system clock */
SystemClock_Config();
/* CERT EXP33-C: Validate configuration function success. */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_ADC1_Init();
MX_ADC2_Init();
/* CERT MSC30-C: Avoid using unsafe functions; HAL functions should still be validated. */
/* Infinite loop */
while (1)
{
float tinggiWaduk = Read_Water_Level_Waduk_cm();
float tinggiLuar = Read_Water_Level_Luar_cm();
/* CERT FLP34-C: Validasi nilai floating point terhadap nilai error (< 0.0f) */
if (tinggiWaduk >= 0.0f && tinggiLuar >= 0.0f)
{
/* Logika tambahan: Mode Kuras jika air dalam dan luar sama-sama 50 cm */
if (fabs(tinggiWaduk - 50.0f) < 0.1f && fabs(tinggiLuar - 50.0f) < 0.1f)
{
mode_pompa_kuras = 1;
}
if (mode_pompa_kuras)
{
// Tutup gerbang, nyalakan pompa
HAL_GPIO_WritePin(GPIOA, RELAY_OPEN_GATE_GPIO_PIN, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOA, RELAY_CLOSE_GATE_GPIO_PIN, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOA, RELAY_PUMP_GPIO_PIN, GPIO_PIN_SET);
// Hentikan mode kuras jika air luar sudah turun ke 30 cm
if (tinggiLuar <= 30.0f)
{
mode_pompa_kuras = 0;
}
}
else
{
/* Logika normal saat sensor valid */
if (tinggiLuar > tinggiWaduk + LEVEL_DIFF_OPEN_THRESHOLD)
{
HAL_GPIO_WritePin(GPIOA, RELAY_OPEN_GATE_GPIO_PIN, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOA, RELAY_CLOSE_GATE_GPIO_PIN, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOA, RELAY_PUMP_GPIO_PIN, GPIO_PIN_RESET);
}
else if (fabs(tinggiLuar - tinggiWaduk) < LEVEL_DIFF_EQUAL_THRESHOLD)
{
HAL_GPIO_WritePin(GPIOA, RELAY_OPEN_GATE_GPIO_PIN, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOA, RELAY_CLOSE_GATE_GPIO_PIN, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOA, RELAY_PUMP_GPIO_PIN, GPIO_PIN_SET);
}
else
{
HAL_GPIO_WritePin(GPIOA, RELAY_OPEN_GATE_GPIO_PIN, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOA, RELAY_CLOSE_GATE_GPIO_PIN, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOA, RELAY_PUMP_GPIO_PIN, GPIO_PIN_RESET);
}
}
}
else
{
/* CERT ERR33-C: Tangani kondisi error dari pembacaan sensor dengan fail-safe */
HAL_GPIO_WritePin(GPIOA, RELAY_OPEN_GATE_GPIO_PIN, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOA, RELAY_CLOSE_GATE_GPIO_PIN, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOA, RELAY_PUMP_GPIO_PIN, GPIO_PIN_RESET);
}
/* CERT MSC30-C: Delay blocking perlu diperhitungkan jika sistem real-time */
HAL_Delay(5000); // Delay tetap di akhir loop
}
}
/**
* @brief Convert ADC value to cm for water level calculation
* @param adc_value: ADC value from sensor
* @retval Water level in cm
*/
float Convert_ADC_to_cm(uint32_t adc_value)
{
if (adc_value > 4095U)
{
return -1.0f;
/* CERT INT30-C: Ensure integer values are within valid bounds */
/* CERT FLP30-C: Avoid using floating-point return values as error codes */
}
return (adc_value / 4095.0f) * MAX_TINGGI_CM;
}
/**
* @brief Read water level from waduk (reservoir) using ADC1
* @retval Water level in cm
*/
float Read_Water_Level_Waduk_cm(void)
{
HAL_ADC_Start(&hadc1);
if (HAL_ADC_PollForConversion(&hadc1, 10) != HAL_OK)
{
return -1.0f;
/* CERT ERR33-C: Always check return values of functions and handle errors */
}
uint32_t adc_value = HAL_ADC_GetValue(&hadc1);
return Convert_ADC_to_cm(adc_value);
}
/**
* @brief Read water level from luar (outside) using ADC2
* @retval Water level in cm
*/
float Read_Water_Level_Luar_cm(void)
{
HAL_ADC_Start(&hadc2);
if (HAL_ADC_PollForConversion(&hadc2, 10) != HAL_OK)
{
return -1.0f;
/* CERT ERR33-C: Always handle function failure properly */
}
uint32_t adc_value = HAL_ADC_GetValue(&hadc2);
return Convert_ADC_to_cm(adc_value);
}
/**
* @brief System Clock Configuration
* @retval None
*/
static void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitStruct.Pin = RELAY_OPEN_GATE_GPIO_PIN | RELAY_CLOSE_GATE_GPIO_PIN | RELAY_PUMP_GPIO_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
/**
* @brief ADC1 Initialization Function
* @param None
* @retval None
*/
static void MX_ADC1_Init(void)
{
ADC_ChannelConfTypeDef sConfig = {0};
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
hadc1.Init.ScanConvMode = DISABLE;
hadc1.Init.ContinuousConvMode = DISABLE;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.NbrOfConversion = 1;
hadc1.Init.DMAContinuousRequests = DISABLE;
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
if (HAL_ADC_Init(&hadc1) != HAL_OK)
{
Error_Handler();
}
sConfig.Channel = ADC_CHANNEL_1; // PA1
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_15CYCLES;
HAL_ADC_ConfigChannel(&hadc1, &sConfig);
}
/**
* @brief ADC2 Initialization Function
* @param None
* @retval None
*/
static void MX_ADC2_Init(void)
{
ADC_ChannelConfTypeDef sConfig = {0};
hadc2.Instance = ADC2;
hadc2.Init = hadc1.Init;
if (HAL_ADC_Init(&hadc2) != HAL_OK)
{
Error_Handler();
}
sConfig.Channel = ADC_CHANNEL_2; // PA2
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_15CYCLES;
HAL_ADC_ConfigChannel(&hadc2, &sConfig);
}
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
__disable_irq();
while (1)
{
}
/* CERT MSC32-C: Infinite loop in error handler is valid for safe shutdown. */
}