#include <Arduino.h>

#define LED PA2
#define BUTTON PB0

void SystemClock_Config(void);

boolean buttonState = HIGH;

void handleButtonPress() {
  buttonState = digitalRead(BUTTON);
}

void setup() {
  SystemClock_Config();
  Serial.begin(115200);
  pinMode(LED, OUTPUT);
  pinMode(BUTTON, INPUT);
  attachInterrupt(digitalPinToInterrupt(BUTTON), handleButtonPress, CHANGE);
}

void loop() {
  if (buttonState == LOW) {
    digitalWrite(LED, LOW);
  } else {
    digitalWrite(LED, HIGH);
  }
}

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_OFF;

  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_DIV4; // 8 MHz / 4 = 2 MHz
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
}
Loading
stm32-bluepill