Programming Arduino Getting Started with Sketches



Yüklə 5,42 Kb.
Pdf görüntüsü
səhifə61/78
tarix22.12.2023
ölçüsü5,42 Kb.
#189286
1   ...   57   58   59   60   61   62   63   64   ...   78
Arduino SimonMonk 2011

highByte
and 
lowByte
are useful for separating an 
int
into two bytes.
Figure 8-1
 shows how this 
int
is actually stored in the EEPROM.
Figure 8-1
Storing a 16-bit integer in EEPROM
To read the 
int
back out of EEPROM, you need to read the two bytes from the
EEPROM and reconstruct the 
int
, as follows:
The 
<<
operator is a bit shift operator that moves the eight high bytes to the top of the
int
and then adds in the low byte.
Storing a 
float
 in EEPROM (Unions)
Storing a 
float
in EEPROM is a little more tricky. To do this, you can use a feature of C
called 
union
s. These data structures are interesting in that they can be thought of as a way
to make the same area of memory accessible to more than one variable. What is more,
these variables are allowed to be of different types as long as they are the same size in
bytes.
The following 
union
definition allows both a 
float
and an 
int
to refer to the same two
bytes of memory:


You can then put a 
float
into the 
union
as follows:
Then you can separate an integer into its two bytes for storage in EEPROM as follows:
Reading the 
float
back out again requires you to do the reverse. First you assemble the
two bytes into a single 
int
, then you put the 
int
into the 
union
and pull it out again as a
float
.
Storing a String in EEPROM
Writing and reading character strings into the EEPROM are pretty straightforward; you
just have to write each character at a time, as in the following example:
To read the string back into a character array, you can do something like this:

Yüklə 5,42 Kb.

Dostları ilə paylaş:
1   ...   57   58   59   60   61   62   63   64   ...   78




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2025
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin