#include "stm32c0xx.h"
void peripheral_init(void) {
// 1. Enable peripheral clocks for GPIOA (DIP switches) and GPIOB (Bar Graph)
RCC->IOPENR |= RCC_IOPENR_GPIOAEN | RCC_IOPENR_GPIOBEN;
// 2. Configure PORTA (PA0 to PA3) as Inputs (00 = Input Mode)
GPIOA->MODER &= ~(GPIO_MODER_MODE0 | GPIO_MODER_MODE1 | GPIO_MODER_MODE2 | GPIO_MODER_MODE3);
// 3. Configure PORTA (PA0 to PA3) with Internal Pull-Up Resistors (01 = Pull-up)
GPIOA->PUPDR &= ~(GPIO_PUPDR_PUPD0 | GPIO_PUPDR_PUPD1 | GPIO_PUPDR_PUPD2 | GPIO_PUPDR_PUPD3);
GPIOA->PUPDR |= (GPIO_PUPDR_PUPD0_0 | GPIO_PUPDR_PUPD1_0 | GPIO_PUPDR_PUPD2_0 | GPIO_PUPDR_PUPD3_0);
// 4. Configure PORTB (PB0 to PB3) as General Purpose Outputs (01 = Output Mode)
GPIOB->MODER &= ~(GPIO_MODER_MODE0 | GPIO_MODER_MODE1 | GPIO_MODER_MODE2 | GPIO_MODER_MODE3);
GPIOB->MODER |= (GPIO_MODER_MODE0_0 | GPIO_MODER_MODE1_0 | GPIO_MODER_MODE2_0 | GPIO_MODER_MODE3_0);
}
int main(void) {
peripheral_init();
while (1) {
// Read PORTA, isolate pins 0-3, and invert the bits in one line.
// Inversion (~) ensures the LED turns ON when the Wokwi DIP switch pulls the pin to GND.
uint32_t direct_output = (~GPIOA->IDR) & 0x0F;
// Clear the lowest 4 bits of PORTB and apply the new bar graph state
GPIOB->ODR = (GPIOB->ODR & ~0x0F) | direct_output;
}
}