#include "stm32f4xx_hal.
h"
// Define GPIO pins for LEDs
#define LED_PIN_0 GPIO_PIN_0
#define LED_PIN_1 GPIO_PIN_1
#define LED_PIN_2 GPIO_PIN_2
#define LED_PIN_3 GPIO_PIN_3
#define LED_PIN_4 GPIO_PIN_4
#define LED_PIN_5 GPIO_PIN_5
#define LED_PIN_6 GPIO_PIN_6
#define LED_PIN_7 GPIO_PIN_7
#define LED_PORT GPIOA
// Define GPIO pins for push buttons
#define BUTTON_0_PIN GPIO_PIN_0
#define BUTTON_1_PIN GPIO_PIN_1
#define BUTTON_2_PIN GPIO_PIN_2
#define BUTTON_3_PIN GPIO_PIN_3
#define BUTTON_PORT GPIOB
// Function prototypes
void SystemClock_Config(void);
void Error_Handler(void);
void initializeIO(void);
// Main function
int main(void) {
// Initialize the HAL Library
HAL_Init();
// Configure the system clock
SystemClock_Config();
// Initialize GPIO for LEDs and push buttons
initializeIO();
// Main loop
while (1) {
// Read the state of the push buttons
uint16_t buttonState = HAL_GPIO_ReadPin(BUTTON_PORT, BUTTON_0_PIN) |
(HAL_GPIO_ReadPin(BUTTON_PORT, BUTTON_1_PIN) << 1) |
(HAL_GPIO_ReadPin(BUTTON_PORT, BUTTON_2_PIN) << 2) |
(HAL_GPIO_ReadPin(BUTTON_PORT, BUTTON_3_PIN) << 3);
// Control the LEDs based on the button state
HAL_GPIO_WritePin(LED_PORT, LED_PIN_0, (buttonState & 0x01) ? GPIO_PIN_SET :
GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_PORT, LED_PIN_1, (buttonState & 0x02) ? GPIO_PIN_SET :
GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_PORT, LED_PIN_2, (buttonState & 0x04) ? GPIO_PIN_SET :
GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_PORT, LED_PIN_3, (buttonState & 0x08) ? GPIO_PIN_SET :
GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_PORT, LED_PIN_4, (buttonState & 0x10) ? GPIO_PIN_SET :
GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_PORT, LED_PIN_5, (buttonState & 0x20) ? GPIO_PIN_SET :
GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_PORT, LED_PIN_6, (buttonState & 0x40) ? GPIO_PIN_SET :
GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_PORT, LED_PIN_7, (buttonState & 0x80) ? GPIO_PIN_SET :
GPIO_PIN_RESET);
}
}
// System Clock Configuration
void SystemClock_Config(void) {
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
// Configure the main PLL
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 7;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
Error_Handler();
}
// Select PLL as the system clock source and configure the HCLK, PCLK1, and PCLK2
clocks
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK |
RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK) {
Error_Handler();
}
}
// Error Handler
void Error_Handler(void) {
// User can add their own error handling here
while (1) {
// Infinite loop
}
}
// Initialize GPIO for LEDs and push buttons
void initializeIO(void) {
// Enable the GPIO clock for GPIOA and GPIOB
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
// Initialize GPIO for LEDs
GPIO_InitTypeDef ledGPIO;
ledGPIO.Mode = GPIO_MODE_OUTPUT_PP;
ledGPIO.Pull = GPIO_NOPULL;
ledGPIO.Speed = GPIO_SPEED_FREQ_LOW;
ledGPIO.Pin = LED_PIN_0 | LED_PIN_1 | LED_PIN_2 | LED_PIN_3 | LED_PIN_4 |
LED_PIN_5 | LED_PIN_6 | LED_PIN_7;
HAL_GPIO_Init(LED_PORT, &ledGPIO);
// Initialize GPIO for push buttons
GPIO_InitTypeDef buttonGPIO;
buttonGPIO.Mode = GPIO_MODE_INPUT;
buttonGPIO.Pull = GPIO_PULLUP;
buttonGPIO.Speed = GPIO_SPEED_FREQ_LOW;
buttonGPIO.Pin = BUTTON_0_PIN | BUTTON_1_PIN | BUTTON_2_PIN | BUTTON_3_PIN;
HAL_GPIO_Init(BUTTON_PORT, &buttonGPIO);
}