Skip to content

Commit b72f934

Browse files
author
SimonePDA
authored
Merge pull request #303 from hydrocontrol/patch-1
Append volatile.adoc with info about atomic read
2 parents 3af0aa0 + 6e515bc commit b72f934

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

Language/Variables/Variable Scope & Qualifiers/volatile.adoc

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,21 @@ Declaring a variable volatile is a directive to the compiler. The compiler is so
2424
Specifically, it directs the compiler to load the variable from RAM and not from a storage register, which is a temporary memory location where program variables are stored and manipulated. Under certain conditions, the value for a variable stored in registers can be inaccurate.
2525

2626
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 a concurrently executing thread. In the Arduino, the only place that this is likely to occur is in sections of code associated with interrupts, called an interrupt service routine.
27+
28+
[float]
29+
=== int or long volatiles
30+
If the volatile variable is bigger than a byte (e.g. a 16 bit int or a 32 bit long), then the microcontroller can not read it in one step, because it is an 8 bit microcontroller. This means that while your main code section (e.g. your loop) reads the first 8 bits of the variable, the interrupt might already change the second 8 bits. This will produce random values for the variable.
31+
32+
Remedy:
33+
34+
While the variable is read, interrupts need to be disabled, so they can't mess with the bits, while they are read.
35+
There are several ways to do this:
36+
37+
1. #LANGUAGE# link:../../../functions/interrupts/nointerrupts[noInterrupts]
38+
39+
2. use the ATOMIC_BLOCK macro. Atomic operations are single MCU operations - the smallest possible unit.
40+
41+
2742
[%hardbreaks]
2843

2944
--
@@ -46,7 +61,7 @@ A variable should be declared volatile whenever its value can be changed by some
4661
// toggles LED when interrupt pin changes state
4762
4863
int pin = 13;
49-
volatile int state = LOW;
64+
volatile byte state = LOW;
5065
5166
void setup()
5267
{
@@ -66,6 +81,21 @@ void blink()
6681
6782
----
6883

84+
85+
[source,arduino]
86+
----
87+
#include <util/atomic.h> // this library includes the ATOMIC_BLOCK macro.
88+
volatile int input_from_interrupt;
89+
90+
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
91+
// code with interrupts blocked (consecutive atomic operations will not get interrupted)
92+
int result = input_from_interrupt;
93+
}
94+
95+
----
96+
97+
98+
6999
--
70100
// HOW TO USE SECTION ENDS
71101

@@ -81,4 +111,4 @@ void blink()
81111
* #LANGUAGE# link:../../../functions/external-interrupts/attachinterrupt[attachInterrupt]
82112

83113
--
84-
// SEE ALSO SECTION ENDS
114+
// SEE ALSO SECTION ENDS

0 commit comments

Comments
 (0)