/* p6_5.c Timer TIM14 interrupt * * Timer TIM14 is configured as an up-counter. By default, the system clock is * running at 8 MHz. The prescaler is set to divide by 8,000 that gives a * 1 kHz clock to the counter. The counter auto-reload is set to 999. When * the counter counts to 999, it updates the counter to zero and sets the * update interrupt flag (UIF). The UIE bit of TIM14->DIER is set so that * the UIF triggers a timer interrupt. The interrupt frequency is 1 Hz. * In the timer interrupt handler, the green LED (PA5) is toggled and * the UIF is cleared. * * This program was tested with Keil uVision v5.23 with DFP v2.0.0 */ #include "stm32f0xx.h" void delayMs(int n); int main(void) { __disable_irq(); /* global disable IRQs */ // 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 = 8000 - 1; /* divided by 8000 */ TIM14->ARR = 1000 - 1; /* divided by 1000 */ TIM14->CNT = 0; /* clear timer counter */ TIM14->CR1 = 1; /* enable TIM14 */ TIM14->DIER |= 1; /* enable UIE */ NVIC_EnableIRQ(TIM14_IRQn); /* enable interrupt in NVIC */ __enable_irq(); /* global enable IRQs */ while(1) { } } void TIM14_IRQHandler(void) { TIM14->SR = 0; /* clear UIF */ GPIOA->ODR ^= 0x20; /* toggle LED */ }