Embedded Systems Programming Lesson 5: Preprocessor and volatile

Embedded Systems Programming Lesson-5 shows how to make the code more readable by using the C preprocessor. You also learn about the volatile keyword and how to use it to write programs that run correctly at any level of optimization.

The lesson starts with showing you how to define macros and explains how the C pre-processor substitutes the macros before the actual compilation.

Next, you define macros for all registers used in the “blinky” program and you test the code.

Next, you learn about the vendor-provided header file that conveniently defines all the registers for you.

You notice that the header file defines the registers as “volatile” and you learn what that means.

You also learn that “volatile” can be useful not just for the registers, but also for the “normal” variables like the delay-loop counter.

You increase the level of optimization to “high” and see for yourself that the compiler optimizes the delay loops “away”.

You fix the problem by making the loop counter “volatile”.

Finally, in the last step of the lesson, you learn how to actually include the “lm4f120h5qr.h” header file into your code and how to recognize the interesting registers in the header file.

In the next lesson you will learn how to use the bitwise OR and AND operators to blink the other colors of the composite LED and you will also learn about more advanced features of the GPIO registers. Stay tuned…

Miro Samek
state-machine.com


Resources:

Article: “Place volatile accurately” by Dan Saks:
http://www.embedded.com/electronics-blogs/programming-pointers/4025609/Place-volatile-accurately

Article: “Volatile as a promise” by Dan Saks:
http://www.embedded.com/electronics-blogs/programming-pointers/4025624/Volatile-as-a-promise

YouTube playlist of the course:

Course web-page:
http://www.state-machine.com/quickstart

Leave a Reply