Skip to main content

Check Your Understanding 8.1.5 Check Your Understanding

1.

In your first blinky you produced a delay with a busy-wait loop, tuned so that it takes approximately 10 ms with the 12 MHz clock. The clock is later changed to 48 MHz. What happens to the delay?
  • It shrinks to approximately 2.5 ms, four times shorter, because the same number of iterations now executes four times faster.
  • Correct. A busy-wait delay is tied to the clock speed. A hardware timer’s period comes from a programmed prescaler and count, so it is set deliberately rather than inherited from however fast the loop happens to run.
  • It stays at 10 ms because the compiler adjusts the loop count for the new clock.
  • The compiler has no idea the loop is meant to be a delay. It just generates instructions. Nothing adjusts the iteration count when the clock changes.
  • It grows to approximately 40 ms because a faster clock consumes more power and slows execution.
  • A faster clock makes the CPU run faster, not slower. The same iterations complete in less time, so the delay gets shorter.
  • It is unchanged because loops take the same wall-clock time regardless of clock speed.
  • Loop iterations are made of instructions, and instructions execute at the clock rate. Quadruple the clock and the same loop finishes in a quarter of the time.

2.

A timer’s input clock runs at 1 MHz after prescaling. The auto-reload register (ARR) is set to 499. How often does the timer generate an update event?
  • Every 500 µs: the counter increments every 1 µs and wraps after 500 ticks (0 through 499).
  • Correct. The counter counts 0, 1, 2, … 499, then wraps: 500 ticks at 1 µs each, so 500 µs per period. The written value is always the desired count minus one.
  • Every 499 µs: the counter counts from 1 to 499.
  • The counter starts at 0 and counts to ARR inclusive: that is ARR + 1 = 500 ticks per period, not 499.
  • Every 1 ms: the timer always generates events at 1 kHz regardless of ARR.
  • The event rate depends on both the prescaled clock and ARR. Here 500 ticks at 1 MHz give 500 µs.
  • Every 1 µs: the timer fires on every clock tick.
  • The update event fires only when the counter wraps at ARR. With ARR = 499 that is once per 500 ticks: 500 µs.

3.

A program must keep a display updating smoothly and also react to a sensor event that occurs at unpredictable times. Compared with polling the sensor’s status flag in the main loop, what does using an interrupt change?
  • The hardware notifies the CPU when the event occurs, so the main loop spends no time checking for it; the ISR runs briefly, then the main program resumes where it left off.
  • Correct. The responsibility to notify moves from the software (asking repeatedly) to the hardware (announcing once). The saved cycles can go to real work, or the CPU can sleep, which is how battery-powered devices live for months.
  • The ISR runs at the same time as the main loop, on separate hardware, so the program does two things at once.
  • The STM32C031C6 has one Cortex-M0+ core. An ISR does not run beside the main program. It suspends it, runs, and returns. The gain is that the suspension happens only when the event actually occurs.
  • The sensor event itself is detected by the hardware sooner, so the event happens earlier.
  • An interrupt changes how the CPU finds out about the event, not when the event occurs. What improves is the response: the CPU no longer waits until its next trip around the polling loop.
  • The event flag no longer needs to be cleared, because the interrupt consumes it automatically.
  • The flag still must be cleared, in the ISR itself, or the interrupt fires again the moment the ISR returns. Interrupts change who notices the flag, not whose job it is to lower it.

4.

Why should an Interrupt Service Routine be kept as short as possible?
  • While the ISR runs, the main program is suspended and other interrupts can be kept waiting; a slow ISR delays everything else the system must do.
  • Correct. The standard pattern is to set a volatile flag in the ISR and let the main loop do the time-consuming work when it sees the flag.
  • ISRs have a hardware-enforced maximum execution time and are terminated if they exceed it.
  • The Cortex-M0+ imposes no time limit on an ISR. An ISR that never returns simply hangs the rest of the program. Nothing terminates it.
  • Long ISRs overflow the CPU’s register save area and corrupt the main program’s state.
  • The state saved at interrupt entry is restored at exit regardless of how long the ISR ran. The cost of a long ISR is blocked responsiveness, not corruption.
  • The compiler cannot optimize code inside an ISR, so long ISRs waste memory.
  • The compiler optimizes ISR code like any other (subject to volatile on shared variables). The reason to keep ISRs short is responsiveness.

5.

A global variable uint8_t count is incremented in an ISR and printed in main(). The program prints 0 forever even though the ISR really does run every second. What is the most likely cause?
  • count is not declared volatile, so the compiler assumes it never changes inside main()’s loop and does not re-read it from memory.
  • Correct. The ISR’s write happens outside the compiler’s view of the loop. volatile tells the compiler to fetch the value fresh at every use.
  • The ISR’s name is misspelled.
  • A misspelled handler name would mean your function is never wired into the vector table and so never runs at all, but the problem states the ISR runs every second.
  • The interrupt was never enabled in the NVIC.
  • With the NVIC line disabled the ISR would never run, but the problem states it runs.
  • The timer’s prescaler is set incorrectly.
  • A wrong prescaler changes how often the ISR runs, not whether main() can see the variable it updates.