/**
******************************************************************************
* @file : main.c
* @author : Auto-generated by STM32CubeIDE
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 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.
*
******************************************************************************
*/
#include <stdint.h>
/* Reset and Clock Control registers */
typedef struct {
volatile uint32_t CR;
volatile uint32_t CFGR;
volatile uint32_t CIR;
volatile uint32_t APB2RSTR;
volatile uint32_t APB1RSTR;
volatile uint32_t AHBENR;
volatile uint32_t APB2ENR;
volatile uint32_t APB1ENR;
volatile uint32_t BDCR;
volatile uint32_t CSR;
} RCC_TypeDef;
/* General Purpose I/O registers */
typedef struct {
volatile uint32_t CRL;
volatile uint32_t CRH;
volatile uint32_t IDR;
volatile uint32_t ODR;
volatile uint32_t BSRR;
volatile uint32_t BRR;
volatile uint32_t LCKR;
} GPIO_TypeDef;
#define RCC_BASE 0x40021000UL // RCC base address
#define GPIOA_BASE 0x40010800UL // GPIO Port A base address
#define RCC ((RCC_TypeDef *)RCC_BASE)
#define GPIOA ((GPIO_TypeDef *)GPIOA_BASE)
#define BUTTON_PIN 10 // PA10
#define LED_PIN 5 // PA5
void USER_RCC_ClockEnable(void);
void USER_GPIO_Init(void);
void delay(uint32_t milliseconds);
int main(void) {
/* Declarations and Initializations */
USER_RCC_ClockEnable();
USER_GPIO_Init();
uint32_t button_pressed = 0;
while (1) {
// Check if the button is pressed
if (!(GPIOA->IDR & (1 << BUTTON_PIN))) {
// Wait for 10ms to avoid noise
delay(10);
// Double verification
if (!(GPIOA->IDR & (1 << BUTTON_PIN))) {
// Wait until the button is released
while (!(GPIOA->IDR & (1 << BUTTON_PIN)));
// Wait for another 10ms
delay(10);
if (!button_pressed) {
GPIOA->ODR ^= (1 << LED_PIN); // Toggle the LED
button_pressed = 1;
}
}
} else {
button_pressed = 0;
}
}
}
void USER_RCC_ClockEnable(void) {
// RCC_APB2ENR modified to IO port A clock enable
RCC->APB2ENR = RCC->APB2ENR | (0x1UL << 2U); // Set IOPAEN bit
}
void USER_GPIO_Init(void) {
// GPIOx_BSRR modified to reset pin 5 of port A (LD2 is connected to PA5)
GPIOA->BSRR = (0x1UL << 21U); // Reset pin 5
// GPIOx_CRL modified to configure pin 5 as output
GPIOA->CRL = (GPIOA->CRL & ~(0x3UL << 22U)) & ~(0x2UL << 20U); // Clear CNF5[1:0] and MODE5_1 bits
// GPIOx_CRL modified to select pin 5 max speed of 10MHz
GPIOA->CRL = GPIOA->CRL | (0x1UL << 20U); // Set MODE5_0 bit
// GPIOx_CRH modified to configure pin 10 as input floating (push-button)
GPIOA->CRH = (GPIOA->CRH & ~(0x3UL << 8U)) | (0x4UL << 8U); // Clear CNF10[1:0] and set MODE10_0 bit
}
void delay(uint32_t milliseconds) {
for (volatile uint32_t i = 0; i < (milliseconds * 800); i++) {
// Adjust this loop for your clock speed and desired delay length
}
}