à
1. Operating Systems vs. Embedded Systems.
2. Usage of High Level Languages such as C.
3. Assembly vs. C.
4. Difference between Conventional C and Embedded C.
5. Advantages Of Embedded C .
6. Why C Language for Embedded Systems.
7. Cross compilers.
8. Bit wise operations and start up code.
9. Reentrant functions , Volatile modifiers.
10. Rules for embedded C programming.
@
Ö n OS or most of the non-embedded
concepts the processing is of main concern
and the nput / output .
Ö n Embedded , it is the input / output that is
important and processing serves only to
connect inputs with outputs.
Ö OS are not closely tied to the hardware,
and, it manipulates hardware registers for
memory management,task process
switching,interrupt request etc.
Ö Embedded Systems are closely tied to the
hardware throughout the system. The
system consists of manipulating the
hardware registers.
Ö Developing a program in C involves large
memory caches, virtual memory,register
sets.
Ö But Embedded systems have limited
memory space and registers.
Ö The development of OS does not have the
restrictions of the size of the overall
systems, power consumption to a larger
extent.
Ö The above parameter have to be considered
in designing an Embedded Systems.
= .
Ö C being a high level language and a
familiar language, among the
programmers it has become the first
choice.
Ö Languages such as C are easy in
coding.Because they are more reliable
programs and they have increased
programmer productivity and portability
across hardware.
Ö C has its reputation increased and it is
also implemented in operating systems.
Ä.
Ö The assembly code are difficult to read and
maintain.
Ö The amount of code reusable from assembly
code is very low.
Ö C programs are easy to
read,understand,maintain, because it
possesses greater structure.
Ö With C the programmer need not know the
architecture of the processor.
Ö Code developed in C will be more portable
to other systems rather than in assembly.
Comparison between assembly
and Embedded C program
Ö Assembly Ö Embedded C
Org 00h main( )
mov r1,#05h { unsigned int a,b,c;
mov r2,#06h a=0x5;
mov a,r1 b=0x6;
add a,r2 c=a+b;
mov r3,a Printf (³ %x´,c);
end }
?
Ä Ä
@ Ä
Ä
Ä
@ ! @ !
"
#
à
$@
%
Embedded C offers a unique concept of bit level
programming.
This concept is mainly used to declare a variable that
will be stored in the bit addressable area of data
memory.
This is very useful because the variable declared in
this fashion
a particular
.
Structures and Unions are possible in bit operation.
The bits of the variable can be accessed without using
previously declared bit names.
t can be defined in a simple way as
Ex:
Unsigned char bdata a=10;
bit =b;
b=a^3;
(a=(10)d => 0a¶ =0000 1010)
b = 1;
After the final execution , the value of b is 1.
Bit pointer is invalid.
Array of bits is invalid.
O
ë
`
`
ë
`
ë
`
ë
`
`
ë
`
ë
`
`
ë
K
`
`
ë !!
"
#`
$`
%&' %('
)`
*` (
+`
,`
-!! .
/`
0`
-.
?
ö
Ä
ö
à=à
ö
Ä
(
'
"
'
= )
$ )
'
ö
ö
àÄ*
(
(
'@Äà@?ö
Ö Volatile is global variable.
Ö These are specifically used in case of ports or
interrupts.
ö
Ö Volatile takes 1 byte instruction.
Ö Permanent memory location is allotted.
Ö Type casting is not possible.
Ö No optimization.
Ö Volatile can modify the value dynamically.
Ö Volatile modifier can change the value outside the
scope of function.
Eg:
# include<reg51.h>
unsigned volatile char time;
main( )
{
time = 0;
while(time<100) {}; // waits for 100 counts
}
void AutoUpdate(void)
{
time = time + 1;
}
Ö Volatile modifier can change the value
outside the scope of the function.
Ö Usually the value of global variable changes
only as a result of explicit statements in C
functions i.e. currently executing.
Ö Without volatile modifier the compiler may
look at the two statements in main and
conclude that since the while loop does not
modify time it could never reach 100.
Ö Volatile modifier disables the optimization
and forces the prg to fetch a new value from
that variable each time the variable is
accessed.
'
Ö Declaring a variable involves 2 actions. First action
is declaring the type and second action is defining it
in memory.
Ö E.g.
1. Unsigned char a; 8 bit unsigned number.
2. Char c1; 8 bit unsigned numbers.
3. Unsigned int a; 16 bit unsigned number.
4. int i; 16 bit signed number.
5. Short s; 16 bit signed number.
6. Long l1; 4 signed 32 bit integer.
7. Float & double are not used/ preferred in embedded
C programs.
Ä
' .
These contents are allowed to change, all the
variables must be located in the RA and not
RO.
Automatic variables contains temporary information
used only by one software module.These variables
are allocated,used and then de-allocated from the
stack.These variables provide protection limiting the
scope of access in such a way that only the program
that created the local variable can access it.The
information stored in this are not permanent.
Static variable is information shared by more than
one program module. Static variables are not de-
allocated. The memory is permanent.
ã
Ä
'' !"#
@=+,
@
@-
ö
ö +
,
+ - , "
Äö
+ ,
Ä
@-
ö
@
@-
(
)
?
)
#ö+(, ?
?
(
&
&
nclude files:
Embedded c program needs some important include
and library files. nclude files includes mainly the
header files which are required to convert the
source code suitable for application or device.
Ö Some of the most common header files used in the
AR LPC 2129 are ³LPC21xx.h´.
Ö Apart from these, common header used in
conventional C i.e <stdio.h> can be used.
Ö User can create own header files and reduce the
code size.
Ö Some of the important library functions are
inbuilt features that help the user to access the
internal memory location as well as external
hardware pins of the device.
Ö Some of these header files as well as the library
functions are not available in conventional C.
The most commonly used compilers are keil- 2,3
, Pic C and Hitech C.
n Keil micro-vision 2 or 3,
A new project has to be created in the start.
For that project, a target, usually a controller which is
used for the application is selected .
For that project a source group is added.Source group
usually contains the source code files.
Once the source code is developed ,this code is
compiled by the cross compiler.
This compilation is usually called as ³Build´.
During this stage, object files are created by the
compiler.
After compilation, these are linked to the linker
which produces a .hex code.These code
formats are suitable for the micro controller.
dentifying Error Levels During compilation:
There are three different categories in identifying
the error levels during compilation. These errors
are caused during
1.assembling
2.compiling and
3.Run time.
The assembling errors are caused during the
assembling of the source code.The compilation
errors are generated during compilation.
The above two errors are caused due to syntax
errors in the program . These errors are generated
by the compiler for the users reference. The errors
and the reason for these are provided as help to the
user in Keil library functions.
These are described under the heading as
A51(assembling errors), C51(compile time
errors) and Linking errors.
The runtime errors are caused during the linking
of the source code. The runtime errors are generated
during runtime by loader/linker. These errors are not
highlighted during the compile time. Because these
errors does not affect the compiler while generating
object code.
These errors are caused when the processor cannot
execute any instruction which are used in the program.
The errors and the reason for these are provided as help
to the user in Keil library functions.
A compiler mainly translates program written in a high
level language into an equivalent set of opcodes for a
particular processor.Each processor has its own unique
machine language .
Ä
. /.
A compiler produces a series of files called as object
files.
CROSS COPL ERS
"
The different object files generated by the
compiler are linked together to form a new object
file. This file contains all the code and data from
the input object files and are in the same object
file format.
AP files generated by the linker gives
information of absolute object file/absolute
location of program,which includes both data and
code. Once the linking is finished, all the machine
language codes from all the input object files will
be in the text section of the new data.
While the linker is in the process of merging the
contents it also looks for unresolved symbols.
After merging all the code & data sections &
resolving all the symbol references, the linker
produces a special relocatable copy of the
program. i.e. The program is complete except that
no memory address is allotted for data & code
sections.
Ö The tool that performs the conversion from
relocatable program to executable binary images
is called locater.
The input to the locater is the information about
the memory on the target address .
Ö The locater uses the information to assign physical
memory to each code & data sections within the
locatable program.
Ö t will then produce an output file that contains a
binary memory image that can be loaded into the
target RO.
Ö n 8051/ AR the locater is built in the linker
itself.
Ö The memory information required by the linker is
passed in the form of a linker script.
]
1
1 `
'
11
1 1 11 11
1 11 1 11 1 111
11 1`
1 1 1 1
1
1 1 11
1 `13 1
11 1
11
=
1 11 1 1
1 1 1111
1
1
111 1`1
1
1
11 1 1
1
1
1 `1
1 1 1 1
111 `1
4 1
1
1 1 111
1 12
1 111 1
`1
4 1 1
1
1 1 1
11 1
`
1
1
1
111 11 `1
1
11 11 1 111 12 1
1
11
`1
1
11
1 1
1
1121
11 1 11
111 1 111 `
-1
1
1111 11111
1
1 1
1 11 1 11 11 1
`
1
12 11 1 111 1 1
1
`1(1 1 12 1
1 1
1
1 1`
ÖUse All the SFR¶s in capital letters only.
ÖReduce the warnings in the program.
Öake use of ACRO definitions in the program.
ÖAlways define the variables in the code memory by using the
keyword code in declaration.
ÖEg unsigned int code a[] = { };
ÖAlways define as unsigned type of declaration.
Öake use of sbit definition for single bit declaration.
ÖEg sbit rs = P3^6;
ÖSince these are not floating point co-processor, no decimal
values can be given as input to them.
Ö So we cannot define the above declaration as sbit rs = P3.6.
Ö The declaration like this below are invalid.
P3^6 = 0;
Ö P3^6 is bit addressable type & 0 is a 8 bit data which cannot be
stored in single bit.
Ö Permanent termination of the program is got by using while(1);
Ö nfinite loop can be achieved by
while(1)
{
«««
}