/* p2_3.c Turn on or off LED by a switch * * This program turns on the green LED (LD2) by pressing the user * button B1 of the Nucleo board. * The user button is connected to PC13. It has a pull-up resistor * so PC13 stays high when the button is not pressed. * When the button is pressed, PC13 becomes low. * The green LED (LD2) is connected to PA5. * A high on PA5 turns on the LED. * * This program was tested with Keil uVision v5.23 with DFP v2.0.0 */ #include "stm32f0xx.h" void delayMs(int n); int main(void) { RCC->AHBENR |= 0x00080000; /* enable GPIOC clock */ RCC->AHBENR |= 0x00020000; /* enable GPIOA clock */ GPIOA->MODER &= ~0x00000C00; /* clear pin mode */ GPIOA->MODER |= 0x00000400; /* set pin to output mode */ GPIOC->MODER &= ~0x0C000000; /* clear pin mode to input mode */ while(1) { if (GPIOC->IDR & 0x2000) /* if PC13 is high */ GPIOA->BRR = 0x00000020; /* turn off green LED */ else GPIOA->BSRR = 0x00000020; /* turn on green LED */ } } /* 8 MHz SYSCLK */ void delayMs(int n) { int i; for (; n > 0; n--) for (i = 0; i < 1142; i++) ; }