Skip to main content

Subsection 8.2.7 Part 7: Writing blinkyTimerInt.c

Now assemble blinkyTimerInt.c. The skeleton below is the polled program from Subsectionย 8.2.4 with its core moved: the five init lines you already ran are given, and the four // TODO clusters are the interrupt machinery from Part 6. Note what is gone: the main loop no longer touches the timer at all.
/*
 * blinkyTimerInt.c -- blink the on-board LED from a TIM14 update interrupt
 *
 * Four blanks to fill in.  Everything you need was in Parts 5 and 6:
 *
 *   TODO 1   raise an interrupt per update      TIM14->DIER
 *   TODO 2   let it through the controller      NVIC_EnableIRQ( ... )
 *   TODO 3   the handler itself                 exact name from the startup file
 *   TODO 4   the shared flag                    one keyword matters
 */
#include <stdio.h>
#include "ES28.h"
#define LED (1U<<5)        // on-board LED
#define PSC_FACTOR 12000   // 12 MHz / 12000 = 1 kHz
#define ARR_FACTOR 500     // 1 kHz / 500 = 2 Hz

void tim14_500ms_interrupt_init();

// TODO 4a -- declare the flag the ISR will share with main.
//            Which keyword must the declaration carry, and why?


int main(void) {
    RCC->IOPENR |= RCC_IOPENR_GPIOAEN;
    GPIOA->MODER = (GPIOA->MODER & ~GPIO_MODER_MODE5_Msk)
                 | (GPIO_OUTPUT << GPIO_MODER_MODE5_Pos);
    GPIOA->ODR &= ~(LED);          // LED starts off

    // TODO 4b -- initialize the flag ...

    tim14_500ms_interrupt_init();

    while (1) {
        // TODO 4c -- when the flag is set: clear it, toggle the LED.
        //            The timer registers appear NOWHERE in this loop.

    }
    return 0;
}

void tim14_500ms_interrupt_init() {
    __disable_irq();               // given: no interrupts during setup
    RCC->APBENR2 |= RCC_APBENR2_TIM14EN;
    TIM14->PSC = PSC_FACTOR - 1;
    TIM14->ARR = ARR_FACTOR - 1;
    TIM14->CNT = 0;

    // TODO 1 -- enable the update interrupt in the timer itself


    // TODO 2 -- enable TIM14's line in the NVIC


    TIM14->CR1 |= TIM_CR1_CEN;
    __enable_irq();                // given: setup done, interrupts on
}

// TODO 3 -- write the ISR.  Its name must be EXACTLY the handler name from
//           the vector table lookup -- copy it from the startup file, don't
//           type it from memory.  No arguments, no return value.  Inside,
//           bracketed by __disable_irq(); ... __enable_irq(); do only two
//           things: clear UIF the Part 5 way (TIM14->SR = ~TIM_SR_UIF;),
//           and set the flag.

Activity 8.2.6. Make the Timer Do the Calling.

The skeleton is blinkyTimerInt.c, in the bookโ€™s starter files.

(b)

Swap the build: right-click blinkyTimerPolled.c and choose Resource Configurations โ†’ Exclude from Build, confirm both Debug and Release are checked, then OK, and make sure blinkyTimerInt.c is not excluded (Figureย 1.3.1). Two main()s cannot coexist in one build.

(f)

Fill TODO 4: the volatile flag, declared, initialized, and consumed in the loop.

(g)

Build, run, and watch the LED blink at the same steady rate as before, now with the main loop only checking your volatile flag. Done early? Make it toggle every 250 ms without touching PSC; then go back to 500 ms and make it toggle every 250 ms without touching ARR. Now imagine your clock was running at 48 MHz (which our chip can do): how could you make the 250 ms or 500 ms blink rate work?
LED not blinking? First, one triage check: is the board completely dead: no LED, no serial output at all? Jump straight to step 3. Otherwise walk through the steps in order. This will help you isolate the fault:
  1. Does the project build cleanly, with blinkyTimerInt.c included and blinkyTimerPolled.c excluded via Resource Configurations โ†’ Exclude from Build?
  2. Swap back for a moment: does the polled version still blink? If yes, your timer configuration is fine: the problem is in the interrupt path, which narrows it to the next two steps.
  3. Is the ISRโ€™s name exactly TIM14_IRQHandler, copied from the startup file, not typed? A near-miss name builds and links without a single warning; your function is never in the table, so it never runs, and the board may look completely dead rather than merely dark.
  4. Did you enable both halves: UIE in TIM14_DIER and the NVIC line? Either one alone produces silence.
The complete file is at the end of this part. If you did not finish the four blanks in class, copy it, compare it against your own version to find what was different, and note how you would fix yours.
The link below opens the same exercise in the simulator, for working on it away from your Nucleo. The LED is PA5 and it is already on the Nucleo board, so nothing is wired today. The register panel shows all six TIM14 registers this chapter uses, CR1, DIER, SR, CNT, PSC and ARR, as soon as your program touches the timer, so you can watch the counter run from 0 to 499 and wrap, and the update interrupt flag UIF come up when it does. The counter runs on real time here, so a 500 ms period really does take 500 ms. A count too large for the 16 bits of TIM14->ARR is truncated here exactly as it is on the board, and the blink comes out at the wrong rate.
One word of caution: this simulator does not optimize your code, so a flag shared between the interrupt handler and main() works here whether or not its declaration carries volatile. TODO 4 asks which keyword that declaration must carry, and a blink in the simulator is not evidence that you have it right. The prescaler is one more place the simulator is simpler than the chip: a new value takes effect here as soon as it is written, rather than waiting for the next update event the way Figureย 8.2.4 shows.
The simulator does not save your code, so use the download button (the downward arrow) to keep a copy of your work.