Tugas Pendahuluan Modul 1 (Percobaan 2 Kondisi 5)
#include "main.h"
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
int main(void)
{
HAL_Init();
MX_GPIO_Init();
while (1)
{
uint8_t ir_status = HAL_GPIO_ReadPin(GPIOB, IR_Pin); // Membaca IR sensor (PB10)
uint8_t touch_status = HAL_GPIO_ReadPin(GPIOB, TOUCH_Pin); // Membaca Touch Sensor (PB7)
// LED ungu (merah + biru) menyala jika IR aktif
if (ir_status == GPIO_PIN_SET)
{
HAL_GPIO_WritePin(GPIOA, Red_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOB, Blue_Pin, GPIO_PIN_SET);
}
else
{
HAL_GPIO_WritePin(GPIOA, Red_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOB, Blue_Pin, GPIO_PIN_RESET);
}
// LED Hijau menyala jika Touch aktif
HAL_GPIO_WritePin(GPIOA, Green_Pin, touch_status);
if (touch_status == GPIO_PIN_RESET) {
HAL_GPIO_WritePin(GPIOA, Red_Pin, GPIO_PIN_SET); // Nyalakan LED RED
} else {
HAL_GPIO_WritePin(GPIOA, Red_Pin, GPIO_PIN_RESET); // Matikan LED RED
}
HAL_Delay(10); // Delay kecil untuk stabilisasi pembacaan sensor
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
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();
}
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOA, Red_Pin|Green_Pin, GPIO_PIN_RESET);
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(Blue_GPIO_Port, Blue_Pin, GPIO_PIN_RESET);
/*Configure GPIO pins : Red_Pin Green_Pin */
GPIO_InitStruct.Pin = Red_Pin|Green_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);
/*Configure GPIO pin : Blue_Pin */
GPIO_InitStruct.Pin = Blue_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(Blue_GPIO_Port, &GPIO_InitStruct);
/*Configure GPIO pins : IR_Pin TOUCH_Pin */
GPIO_InitStruct.Pin = IR_Pin|TOUCH_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
void Error_Handler(void)
{
__disable_irq();
while (1)
{
}
}
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t *file, uint32_t line)
{
}
#endif /* USE_FULL_ASSERT */
Komentar
Posting Komentar