You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Language/Variables/Variable Scope & Qualifiers/volatile.adoc
+21-8Lines changed: 21 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -55,34 +55,47 @@ There are several ways to do this:
55
55
=== Example Code
56
56
// Describe what the example code is all about and add relevant code ►►►►► THIS SECTION IS MANDATORY ◄◄◄◄◄
57
57
58
+
The `volatile` modifier ensures that changes to the `state` variable are immediately visible in `loop()`. Without the `volatile` modifier, the `state` variable may be loaded into a register when entering the function and would not be updated anymore until the function ends.
58
59
59
60
[source,arduino]
60
61
----
61
-
// toggles LED when interrupt pin changes state
62
+
// Flashes the LED for 1 s if the input has changed
To access a variable with size greater than the microcontroller’s 8-bit data bus, use the `ATOMIC_BLOCK` macro. The macro ensures that the variable is read in an atomic operation, i.e. its contents cannot be altered while it is being read.
80
92
81
93
[source,arduino]
82
94
----
83
95
#include <util/atomic.h> // this library includes the ATOMIC_BLOCK macro.
84
96
volatile int input_from_interrupt;
85
97
98
+
// Somewhere in the code, e.g. inside loop()
86
99
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
87
100
// code with interrupts blocked (consecutive atomic operations will not get interrupted)
0 commit comments