#include "stm32c0xx.h"
static void sysClockConfig(void){
FLASH->ACR &= ~FLASH_ACR_LATENCY;
FLASH->ACR |= FLASH_ACR_LATENCY_0; /* 001: 1 wait state */
while ((FLASH->ACR & FLASH_ACR_LATENCY) != FLASH_ACR_LATENCY_0) {
/* Wait until flash latency is committed */
}
/* 2. Ensure HSI48 is ON and stable */
RCC->CR |= RCC_CR_HSION;
while (!(RCC->CR & RCC_CR_HSIRDY)) {
/* Wait for HSI48 oscillator to stabilize */
}
/* 3. Clear HSIDIV to select division factor 1 (48 MHz) */
/* HSIDIV field is 3 bits wide (bits [13:11]) */
RCC->CR &= ~RCC_CR_HSIDIV;
while (!(RCC->CR & RCC_CR_HSIRDY)) {
/* Wait until clock divider switch settles */
}
/* 4. Ensure AHB and APB prescalers are set to div 1 */
RCC->CFGR &= ~(RCC_CFGR_HPRE | RCC_CFGR_PPRE);
}
static void gpio_init(void){
/* Enable GPIOA peripheral clock */
RCC->IOPENR |= RCC_IOPENR_GPIOAEN;
RCC->IOPENR |= RCC_IOPENR_GPIOCEN;
/* Configure PA5: Output mode (01b), Push-Pull (0b), Low Speed (00b), No pull-up/down (00b) */
GPIOA->MODER &= ~(3U << (5 * 2));
GPIOA->MODER |= (1U << (5 * 2));
GPIOC->MODER &= ~(3U << (13 * 2));
GPIOA->OTYPER &= ~(1U << 5); // Push-pull
GPIOA->OSPEEDR &= ~(3U << (5 * 2)); // Low speed
GPIOA->PUPDR &= ~(3U << (5 * 2)); // No pull
GPIOC->PUPDR &= ~(3U << (13 * 2)); // 00: Floating (onboard hardware pull-up handles this)
GPIOA->BSRR = (1U << (5 + 16));
}
void delay_ms(volatile uint32_t ms){
while(ms--){
for(volatile uint32_t i = 0; i<12000; i++){
__NOP();
}
}
}
int main(void){
sysClockConfig();
gpio_init();
bool state=false;
while (1)
{
if((GPIOC->IDR & (1U << 13)) == 0){
delay_ms(25);
if((GPIOC->IDR & (1U << 13)) == 0){
state = !state;
if (state)
{
/* Turn LED ON: write 1 to BS5 (bit 5) */
GPIOA->BSRR = (1U << 5);
}
else
{
/* Turn LED OFF: write 1 to BR5 (bit 21 = 5 + 16) */
GPIOA->BSRR = (1U << (5 + 16));
}
while ((GPIOC->IDR & (1U << 13)) == 0);
delay_ms(25);
}
}
}
}Loading
st-nucleo-c031c6
st-nucleo-c031c6