Skip to content
0:00 / 0:00

#volatile#cpreprocessor#compileroptimization

#5 Preprocessor and the "volatile" keyword in C

Quantum Leaps, LLC

74K subscribers • 20 videos on Hallway

YouTube
  • Recommended by 2 people: 2 in engineering communities.
  • A classic. People kept recommending it from 2019 to 2023.
  • Presented by Miro Samek, founder of Quantum Leaps

Beginner. No background in the topic needed.

157K views13 years ago2 recommendations

Programming languagesSystems programmingCEmbedded systemsCompilers and interpreters

Miro Samek explains how to use the C preprocessor for readability and the volatile keyword to prevent incorrect compiler optimizations.

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... --- Resources: Article: "Place volatile accurately" by Dan Saks: embedded.com/electronics-blogs/programming-pointers/4025609/… Article: "Volatile as a promise" by Dan Saks: embedded.com/electronics-blogs/programming-pointers/4025624/… YouTube playlist of the course: youtube.com/playlist?list=PLPW8O6W-1chwyTzI3BHwBLbGQoPFxPAPM Course web-page: state-machine.com/quickstart

Why it's here

2 people recommend this

What people say. Recommended as an essential lesson on how the volatile keyword works in embedded C programming.

u/headkazein r/embedded • Recommends

On “Help with embedded/firmware interview questions.”

[video

u/I_forgot_my_main_5in r/embedded • Recommends

On “Transitioning from Test Engineer to Embedded”

…ainly because it's a project that will require dedicated HW as opposed to my other projects where I create a system and hook it up to a computer that I wrote some code in, it's just finding a cost effective MCU that will survive the hot temperature that I will have my system testing in. At the current moment I'm also studying this video series: [video…

u/DoctorKokktorin r/embedded • Mentions

Why isn't my code for turning on an LED for the TIVA C TM4C123 not working? Hello all, I am new to embedded programming and I wanted to start by turning on the LED in the TIVA C TM4C123 development board. Here is my code: int main() { /* Step 1: Enable clock to port F using the RCGCPIO register. Base address: 0x400FE000 Offset: 0x608 Full address: Base address + Offset = 0x400FE608 This register should be set to 0x20 */ **((volatile unsigned int*)(0x400FE608)) = 0x20; /* Step 2: Set pins to be digital using the GPIODEN register. Base address: 0x40025000 Offset: 0x51C Full address: 0x4002551C Set to 0x0E */ **((volatil…