Download Embedded Multitasking with small - innovated
Transcript
Basic Embedded Programming Concepts
47
Access to a STRUCTURE from assembly language is simply a matter of using the name of the structure as a label within the assembly.
However, access to the individual bits must be accomplished through
the appropriate assembly language bit manipulation instructions.
Declaration 2.8
UNION union_name {
variable_type variable_name;
variable_type variable_name;
} variable_name;
In some applications, it can be useful to be able to access a given
piece of data not only by different names, but also using different data
structures. To handle this task, the complex data type UNION is used.
What a UNION does is create two definitions for a common word, or
group of words, in data memory. This allows the program to change its
handling of a variable based on its needs at any one time.
For example, the individual groups of bits within the ADCON0 peripheral control register in the previous section were defined to give the
program access to the control bits individually. However, in the initial
configuration of the peripheral, it would be rather cumbersome and inefficient to set each variable one at a time. Defining the STRUCTRUE from
previous example in a UNION allows the designer to not only individually
access the groups of bits within the peripheral control register, but it also
allows the designer to set all of the bits at once via a single 8-bit CHAR.
UNION
Declaration 2.9
UNDEF{
STRUCT REGDEF{
SHORT
ADON;
SHORT
CHS3;
SHORT
GODONE;
UNSIGNED CHAR
CHS:3;
UNSIGNED CHAR
ADCS:2;
} BYBIT;
UNSIGNED CHAR BYBYTE;
} ADCON0 @ 0x1F;
In the example, the original STRUCTURE definition is now included
within the definition of the UNION as one of two possible definitions
for the common data memory. The STRUCTURE portion of the definition has been given the sub-name “BYBIT” and any access to this side of
the definition will require its inclusion in the variable name. The second