Unit 2 Embedded C
Unit 2 Embedded C
UNIT-2
(Part-1)
Practices that characterize embedded C development:
• in-line assembly language
• device knowledge
• mechanical knowledge.
Inline Assembly
• The labels and variables used in C are available within included
assembly,
• Compiler will not attempt to optimize such code.
• The compiler assumes that the user has a good reason to avoid
the compiler's code generation and optimization.
• The microcontroller's manufacturer should provide assistance in
hand-crafting assembly language programming.
• Required to flip opcodes out of order to accommodate a
pipeline, something the compiler will do transparently.
C functions with inline Assembly language
Device Knowledge
• The resources needed to store and manipulate floating point numbers can place
overwhelming demands on an 8-bit computer.
• The fundamental data type for representing real numbers in C is the float type.
• The maximum value for the target computer is defined in a C header file called
values.h as a symbolic constant called MAXFLOAT.
• C compilers generally allocate four bytes for a float variable, which provides
approximately six digits of precision to the right of the decimal.
• You can have greater precision with the double and long double data types.
• Compilers typically allocate eight bytes for a double variable and more for a long
double.
• You can assign an integer value to a floating point data type, but you must
include a decimal and a 0 to the right of the decimal.
myFloatVariable = 2.0;
Pointers
struct asByte {
int TMR1H; /* high byte */
int TMR1L; /* low byte */
}
union TIMER1_tag {
long TMR1_word; /* access as 16 bit register */
struct asByte halves;
} TMR1;
/* ... */
seed = TMR1.halves.TMR1L;
Since the compiler uses a single block of memory for the entire union, it
allocates a block large enough for the largest element in the union.
The compiler will align the first bits of each element in the lowest address
in the memory block.
If you assign a 16-bit value to scratchPad and then read it as an 8-bit value,
the compiler will return the first 8 bits of the data stored.
typedef
• The typedef keyword defines a new variable type in terms of existing types.
The compiler cares most about the size of the new type, to determine the
amount of RAM or ROM to reserve.
Because of the scarcity of registers on 8-bit machines and the desire for
size optimization rather than speed, the register keyword is not very
useful for embedded system programmers.
The example does two things: it places the register declaration and the
while loop close together and inside a statement block.
This minimizes the cost of potentially dedicating a register to a specific
variable.
It also forces the compiler to reallocate storage for myCounter as soon as
the loop is finished:
• Using the auto data modifier