#include "stm32c0xx_hal.h"
#include <stdio.h>
#include <stdbool.h>
#define Green_buttom GPIO_PIN_0
#define Blue_buttom GPIO_PIN_1
#define Red_buttom GPIO_PIN_4
#define Green_LED GPIO_PIN_5
#define Blue_LED GPIO_PIN_6
#define Red_LED GPIO_PIN_7
void SystemClock_Config(void)
{
// 目前先不做設定,後面會補
}
void MX_GPIO_Init(void)
{
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStruct = {0};
// 初始化 LED 腳位
GPIO_InitStruct.Pin = Green_LED | Blue_LED | Red_LED;
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);
// 設定 Buttom
GPIO_InitStruct.Pin = Green_buttom | Blue_buttom | Red_buttom;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
bool KeepGreen = false;
bool KeepBlue = false;
bool KeepRed = false;
while (1)
{
// 讀取按鈕狀態 (按下為低電位)
if (HAL_GPIO_ReadPin(GPIOA, Green_buttom) == GPIO_PIN_RESET)
{
KeepGreen = !KeepGreen; // 切換綠色 LED 狀態
KeepBlue = false;
KeepRed = false;
HAL_Delay(200); // 簡單的消抖
}
if (HAL_GPIO_ReadPin(GPIOA, Blue_buttom) == GPIO_PIN_RESET)
{
KeepBlue = !KeepBlue; // 切換藍色 LED 狀態
HAL_Delay(200); // 簡單的消抖
KeepGreen = false;
KeepRed = false;
}
if (HAL_GPIO_ReadPin(GPIOA, Red_buttom) == GPIO_PIN_RESET)
{
KeepRed = !KeepRed; // 切換紅色 LED 狀態
HAL_Delay(200); // 簡單的消抖
KeepGreen = false;
KeepBlue = false;
}
// 根據 Keep 變數的狀態控制 LED
HAL_GPIO_WritePin(GPIOA, Green_LED, KeepGreen ? GPIO_PIN_SET : GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOA, Blue_LED, KeepBlue ? GPIO_PIN_SET : GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOA, Red_LED, KeepRed ? GPIO_PIN_SET : GPIO_PIN_RESET);
HAL_Delay(10); // 稍微延遲,避免過快的迴圈
}
}