/* p5_4.c Toggle Green LED (LD2) on STM32F030R8 Nucleo64 using TIM14 * * This program configures TIM14 with prescaler divides by 800 * and counter wraps around at 10000. So the 8 MHz system clock * is divided by 800 then divided by 10000 to become 1 Hz. * Every time the counter wraps around, it sets UIF flag * (bit 0 of TIM14_SR register). The program waits for the * UIF to set then toggles the LED (PA5). * * This program was tested with Keil uVision v5.24a with DFP v2.11.0 */ #include "stm32f0xx.h" int main(void) { // configure PA5 as output to drive the LED RCC->AHBENR |= 0X00020000; /* enable GPIOA clock */ GPIOA->MODER &= ~0x00000C00; /* clear pin mode */ GPIOA->MODER |= 0x00000400; /* set pin to output mode */ // configure TIM14 to wrap around at 1 Hz RCC->APB1ENR |= 0x00000100; /* enable TIM14 clock */ TIM14->PSC = 800 - 1; /* divided by 1600 */ TIM14->ARR = 10000 - 1; /* divided by 10000 */ TIM14->CNT = 0; /* clear timer counter */ TIM14->CR1 = 1; /* enable TIM14 */ while (1) { while(!(TIM14->SR & 1)) {} /* wait until UIF set */ TIM14->SR &= ~1; /* clear UIF */ GPIOA->ODR ^= 0x00000020; /* toggle green LED */ } }