for
loop delays
The first challenge is to implement the delay
function without using any peripheral and theobvious solution is to implement it as a for
loop delay:
#[inline(never)]
fn delay(tim6: &tim6::RegisterBlock, ms: u16) {
for _ in 0..1_000 {}
}
Of course, the above implementation is wrong because it always generates the same delay for anyvalue of ms
.
In this section, you’ll have to:
- Fix the
delay
function to generate delays proportional to its inputms
. - Tweak the
delay
function to make the LED roulette spin at a rate of approximately 5 cycles in 4seconds (800 milliseconds period). - The processor inside the microcontroller is clocked at 8 MHz and executes most instructions in one“tick”, a cycle of its clock. How many (
for
) loops do you think thedelay
function must doto generate a delay of 1 second? - How many
for
loops doesdelay(1000)
actually do? - What happens if compile your program in release mode and run it?