Laporan Akhir 2
a. Prosedur
b. Hardware dan Diagram Blok
c. Rangkaian Simulasi dan Prinsip Kerja
d. Flowchart dan Listing Program
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.h
* @brief : Header for main.c file.
* This file contains the common defines of the application.
******************************************************************************
* @attention
*
* Copyright (c) 2026 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 */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __MAIN_H
#define __MAIN_H
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "stm32g4xx_hal.h"
#include "stm32g4xx_nucleo.h"
#include <stdio.h>
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Exported types ------------------------------------------------------------*/
/* USER CODE BEGIN ET */
/* USER CODE END ET */
/* Exported constants --------------------------------------------------------*/
/* USER CODE BEGIN EC */
/* USER CODE END EC */
/* Exported macro ------------------------------------------------------------*/
/* USER CODE BEGIN EM */
/* USER CODE END EM */
/* Exported functions prototypes ---------------------------------------------*/
void Error_Handler(void);
/* USER CODE BEGIN EFP */
/* USER CODE END EFP */
/* Private defines -----------------------------------------------------------*/
/* ====== INPUT ====== */
#define FLAME_PIN GPIO_PIN_0
#define FLAME_PORT GPIOA
#define FLOAT_PIN GPIO_PIN_1
#define FLOAT_PORT GPIOA
/* ====== OUTPUT ====== */
#define LED_PIN GPIO_PIN_5
#define LED_PORT GPIOA
#define BUZZER_PIN GPIO_PIN_6
#define BUZZER_PORT GPIOA
#define RELAY_PIN GPIO_PIN_7
#define RELAY_PORT GPIOA
/* USER CODE BEGIN Private defines */
/* USER CODE END Private defines */
#ifdef __cplusplus
}
#endif
#endif /* __MAIN_H */
main.c:
#include "main.h"
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
while (1)
{
GPIO_PinState flame_state;
GPIO_PinState float_state;
flame_state = HAL_GPIO_ReadPin(FLAME_PORT, FLAME_PIN);
float_state = HAL_GPIO_ReadPin(FLOAT_PORT, FLOAT_PIN);
/* ===== FLAME SENSOR ===== */
if (flame_state == GPIO_PIN_RESET)
{
/* Api terdeteksi */
HAL_GPIO_WritePin(LED_PORT, LED_PIN, GPIO_PIN_SET);
HAL_GPIO_WritePin(BUZZER_PORT, BUZZER_PIN, GPIO_PIN_SET);
}
else
{
HAL_GPIO_WritePin(LED_PORT, LED_PIN, GPIO_PIN_RESET);
HAL_GPIO_WritePin(BUZZER_PORT, BUZZER_PIN, GPIO_PIN_RESET);
}
/* ===== RELAY / POMPA ===== */
if ((flame_state == GPIO_PIN_SET) || (float_state == GPIO_PIN_SET))
{
/* Api ATAU tangki penuh → pompa MATI */
HAL_GPIO_WritePin(RELAY_PORT, RELAY_PIN, GPIO_PIN_RESET);
}
else
{
/* Aman & tangki belum penuh → pompa HIDUP */
HAL_GPIO_WritePin(RELAY_PORT, RELAY_PIN, GPIO_PIN_SET);
}
HAL_Delay(100);
}
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOA_CLK_ENABLE();
/* INPUT */
GPIO_InitStruct.Pin = FLAME_PIN | FLOAT_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* OUTPUT */
GPIO_InitStruct.Pin = LED_PIN | BUZZER_PIN | RELAY_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);
/* Relay default ON */
HAL_GPIO_WritePin(RELAY_PORT, RELAY_PIN, GPIO_PIN_SET);
}
void SystemClock_Config(void)
{
/* Clock default CubeIDE */
}
void Error_Handler(void)
{
while (1) {}
}



Komentar
Posting Komentar