A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://docs.leaflabs.com/static.leaflabs.com/pub/leaflabs/maple-docs/latest/lang/api/volatile.html below:

volatile — Maple v0.0.12 Documentation

volatile¶

The volatile keyword known is a variable qualifier. It is usually used before the datatype of a variable, to modify the way in which the compiler treats the variable.

Declaring a variable volatile is a directive to the compiler. The compiler is software which translates your C++ code into the machine code, which are the real instructions for the STM32 chip in the Maple. (The particular compiler we provide for use with the Maple is a version of GCC).

Specifically, it directs the compiler to read the variable’s value fresh every time it is used, rather than “backing up” the value and reading from its backup copy. (Compilers often “back up” a variable’s value in RAM into a storage location called a register; this is done for efficiency).

A variable should be declared volatile whenever its value can be changed by something beyond the control of the code section in which it appears, such as an external interrupt. (The only place that this is likely to occur in most programs is inside of code called by interrupts).

Example¶
// toggles LED when interrupt pin changes state

int pin = 13;
volatile int state = LOW;

void setup() {
  pinMode(pin, OUTPUT);
  attachInterrupt(0, blink, CHANGE);
}

void loop() {
  digitalWrite(pin, state);
}

void blink() {
  if (state == HIGH) {
    state = LOW;
  } else {
    // state must be HIGH
    state = HIGH;
  }
}

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4