0% found this document useful (0 votes)
55 views

#Include stm32f4xx - Hal.h

Uploaded by

meryem.04.sude
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

#Include stm32f4xx - Hal.h

Uploaded by

meryem.04.sude
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include "stm32f4xx_hal.

h"

// Define GPIO pins for button and LED


#define BUTTON_PIN GPIO_PIN_0
#define BUTTON_PORT GPIOA

#define LED_PIN GPIO_PIN_5


#define LED_PORT GPIOA

// Function prototypes
void SystemClock_Config(void);
void Error_Handler(void);

// Main function
int main(void) {
// Initialize the HAL Library
HAL_Init();

// Configure the system clock


SystemClock_Config();

// Initialize GPIO for button


__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitTypeDef buttonGPIO;
buttonGPIO.Pin = BUTTON_PIN;
buttonGPIO.Mode = GPIO_MODE_INPUT;
buttonGPIO.Pull = GPIO_PULLUP;
buttonGPIO.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(BUTTON_PORT, &buttonGPIO);

// Initialize GPIO for LED


__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitTypeDef ledGPIO;
ledGPIO.Pin = LED_PIN;
ledGPIO.Mode = GPIO_MODE_OUTPUT_PP;
ledGPIO.Pull = GPIO_NOPULL;
ledGPIO.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(LED_PORT, &ledGPIO);

// Main loop
while (1) {
// Read the state of the button
if (HAL_GPIO_ReadPin(BUTTON_PORT, BUTTON_PIN) == GPIO_PIN_RESET) {
// Button is pressed, toggle the LED state
HAL_GPIO_TogglePin(LED_PORT, LED_PIN);

// Wait for a short debounce time


HAL_Delay(50);
}
}
}

// 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_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 168;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 4;
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
}
}

You might also like