
18 Chapter 2: Your First Embedded Program
The hardware-specific constant CYCLES_PER_MS represents the number of decre-
ment-and-test cycles (nCycles-- != 0) that the processor can perform in a single
millisecond. To determine this number I used trial and error. I made an approxi-
mate calculation (I think it came out to around 200), then wrote the remainder of
the program, compiled it, and ran it. The LED was indeed blinking but at a rate
faster than 1 Hz. So I used my trusty stopwatch to make a series of small changes
to CYCLES_PER_MS until the rate of blink was as close to 1 Hz as I cared to test.
That’s it! That’s all there is to the Blinking LED program. The three functions main,
toggleLed, and delay do the whole job. If you want to port this program to some
other embedded system, you should read the documentation that came with your
hardware, rewrite toggleLed as necessary, and change the value of CYCLES_PER_MS.
Of course, we do still need to talk about how to build and execute this program.
We’ll examine those topics in the next two chapters. But first, I have a little some-
thing to say about infinite loops and their role in embedded systems.
The Role of the Infinite Loop
One of the most fundamental differences between programs developed for
embedded systems and those written for other computer platforms is that the
embedded programs almost always end with an infinite loop. Typically, this ...