EMBEDDED
C
•Whenever the conventional „C‟ language and
its extensions are used for programming
embedded systems, it is referred to as
“Embedded C” programming.
What is a EMBEDDED C?
C V/S EMBEDDED C
ASSEMBLY V/S EMBEDDED C
The assembly code is 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.
ASSEMBLY
Org 00h
mov r1,#05h
mov r2,#06h
mov a,r1
add a,r2
mov r3,a
end
COMPARISON BETWEEN ASSEMBLY AND EMBEDDED C PROGRAM
EMBEDDED C
main( )
{
unsigned int
a,b,c; a=0x5;
b=0x6;
c=a+b;
Printf (“ %x”,c);
}
DIFFERENCE BETWEEN CONVENTIONAL C AND EMBEDDED C.
 Compliers for conventional C are TC, BC
 Compilers for Embedded C are keil µvision - 2 & 3, PIC C etc.
 Conventional C programs needs complier to compile the
program & run it.
 The embedded C program needs a cross compiler to compile &
generate HEX code.
 The programs in C are basically processor dependent whereas
Embedded C programs are micro controller dependent.
DIFFERENCE BETWEEN CONVENTIONAL C AND EMBEDDED C.
 The C program is used for developing an application and not
suitable for embedded systems.
 The embedded C is an extension of the conventional C. i.e
Embedded C has all the features of normal C, but has some
extra added features which are not available in C.
 Many functions in C do not support Reentrant concept of
functions.
DIFFERENCE BETWEEN CONVENTIONAL C AND EMBEDDED C.
 C is not memory specific. i.e variables cannot be put in the
desired memory location but the location of variable can be
found out.
 In embedded C this can be done using specific inbuilt
instructions.
 C depends on particular processor or application.
 Embedded C is Controller or target specific.
 Embedded C allows direct communication with memory.
Why C for Micro controllers
 Compatibility
 Direct access to hardware address
 Direct connection to interrupts
 Optimization consideration
 Development environment
 Reentrancy
PROGRAM FLOW IN CROSS COMPILERS
EDITOR Notepad
or Dos
.C .H .ASM .A51
COMPLILER ASSEMBLER
.OBJ .OBJ
Linker / Locator
HEX file
To Micro
Controller
BIT LEVEL PROGRAMMING & OPTIMIZATION
 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
directly points the data in a particular segment of memory.
 Structures and Unions are possible in bit operation. The bits of
the variable can be accessed without using previously declared
bit names.
BIT LEVEL PROGRAMMING & OPTIMIZATION
 It 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.
 Limitations of bit level program
 Bit pointer is invalid.
 Array of bits is invalid.
STARTUP CODE
 Startup code is an extra piece of software that executes prior to main().
The startup code is generally written in assembly language and linked
with any executable that you build.
 It prepares the way for the execution of programs written in a high-level
language.
 Each such language has its own set of expectations about the run-time
environment in which programs are executed.
 For example, many languages utilize a stack. Space for the stack must be
allocated and some registers or data structures initialized before
software written in the high-level language can be properly executed.
EMBEDDED SOFTWARE DEVELOPMENT
 The embedded software development tools, cannot make assumption
about the target platform.
 The user has to provide some details of the system to the tools through
explicit statements or instructions.
 Once these data are given to the tools, these tools generate the expected
outputs on the computer system itself so that the programmer can
analyze or make suitable changes in the software to get the desired
output.
REENTRANCY, STATIC, VOLATILE KEYWORDS
 A variable is a named object that resides in the RAM.
 In C programs each function call causes a frame to be pushed on to stack
which contains function parameters and allocation of the locals of the
functions.
 In embedded C there is no stack allocated .
 In a block of memory, a function is given a fixed address space for its local
variables .
 Thus recursive calls to a function will cause the data in the variables to be
corrupted.
REENTRANCY, STATIC, VOLATILE KEYWORDS
 In this, a separate copy of the locals for each function exists.
 Because the stack is simulated reentrant functions are large.
 Care should be taken that these function do not cross the
memory limitations when copies of the functions are created.
 This also requires the elimination of any bit parameters, locals
and return values from reentrant functions.
REENTRANT FUNCTIONS
REENTRANT FUNCTIONS
VOLATILE MODIFIERS
 Volatile is global variable.
 These are specifically used in case of ports or interrupts.
 Features :
 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.
VOLATILE MODIFIERS
Eg:
# include
unsigned volatile char time;
main( )
{
time = 0;
while(time <100){};
Void AutoUpdate(void)
{
time=time+1;
}
VOLATILE MODIFIERS
 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.
CREATING ENVIRONMENT VARIABLES
 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.
COMPILING WITH AN EMBEDDED COMPILER
CREATING ENVIRONMENT VARIABLES
 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.
CREATING EXECUTABLE PROGRAMS, TEMPORARY
FILES,INCLUDE FILES AND LIBRARY FILES.
 The C source code is compiled using c51 compiler by invoking C51.exe.
 The command line is C51 source files [directives….] where Source files: is
the name of the source program to be compiled. Directive: are directives
to the compiler to control the function of the compiler
 The source code is usually developed in C or assembly which are
executable programs.
 C51 compiler generates a series of output files during compilation.
 Basename.lst : (list file) these contain formatted source text with any
errors detected by the compiler .
CREATING EXECUTABLE PROGRAMS, TEMPORARY
FILES,INCLUDE FILES AND LIBRARY FILES.
 Basename.obj: (object code) These contain the relocatable object
code.These are linked to an absolute module by L51 linker/locator.
 Basename.I:contains source text as expanded by the preprocessor.All macros
are expanded and all comments are deleted on this listing.32
 Basename.src: these are assembly source files generated from c source
code.These are assembled with A51 assembler.
 Basename.hex (I): this is a hex file or a binary file used to program the
device.
Include files:
 Embedded c program needs some important include and library files.Include
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 PIC18F458 are
“PIC16Fxx.h”.
 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.
CREATING EXECUTABLE PROGRAMS, TEMPORARY
FILES,INCLUDE FILES AND LIBRARY FILES.
The most commonly used compilers are keil- 2,3 , Pic C and Hitech C. In 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.
CREATING EXECUTABLE PROGRAMS, TEMPORARY
FILES,INCLUDE FILES AND LIBRARY FILES.
CROSS COMPLIERS
 Code Optimization.
 Declare local variables in the inner most scope
 Do not declare all the local variables in the outermost function scope.
 If local variables are declared in the inner most scope.
 If the parameter was declared in the outermost scope, all function calls
would have incurred the overhead of object .
 Place case labels in narrow range
 If the case labels are in a narrow range, the compiler does not generate
a if-else-if cascade for the switch statement.
 This code generated is faster than if-else-if cascade code that is
generated in cases where the case labels are far apart.
RULES FOR DEVELOPING EMBEDDED C PROGRAM
 Reduce the number of parameters Function calls with large number of
parameters may be expensive due to large number of parameter pushes on stack
on each call. For the same reason, avoid passing complete structures as
parameters. Use pointers and references in such cases.
 Use references for parameter passing and return value for types bigger than 4
bytes
 Passing parameters by value results in the complete parameter being copied
on to the stack. This is fine for regular types like integer, pointer etc. These
types are generally restricted to four bytes. When passing bigger types, the
cost of copying the object on the stack can be prohibitive. When the function
exits the destructor will also be invoked.
RULES FOR DEVELOPING EMBEDDED C PROGRAM
 SFRS & Registers
1. Use All the SFR’s in capital letters only.
2. Reduce the warnings in the program.
3. Make use of MACRO definitions in the program.
4. Always define the variables in the code memory by using the keyword
code in declaration.
5. Eg unsigned int code a[] = { };
6. Always define as unsigned type of declaration.
7. Make use of sbit definition for single bit declaration.
8. Eg sbit rs = P3^6;
RULES FOR DEVELOPING EMBEDDED C PROGRAM
 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);
 Infinite loop can be achieved by
while(1)
{
………
}
RULES FOR DEVELOPING EMBEDDED C PROGRAM
THANK YOU

Embedded C.pptx

  • 1.
  • 2.
    •Whenever the conventional„C‟ language and its extensions are used for programming embedded systems, it is referred to as “Embedded C” programming. What is a EMBEDDED C?
  • 3.
  • 4.
    ASSEMBLY V/S EMBEDDEDC The assembly code is 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.
  • 5.
    ASSEMBLY Org 00h mov r1,#05h movr2,#06h mov a,r1 add a,r2 mov r3,a end COMPARISON BETWEEN ASSEMBLY AND EMBEDDED C PROGRAM EMBEDDED C main( ) { unsigned int a,b,c; a=0x5; b=0x6; c=a+b; Printf (“ %x”,c); }
  • 6.
    DIFFERENCE BETWEEN CONVENTIONALC AND EMBEDDED C.  Compliers for conventional C are TC, BC  Compilers for Embedded C are keil µvision - 2 & 3, PIC C etc.  Conventional C programs needs complier to compile the program & run it.  The embedded C program needs a cross compiler to compile & generate HEX code.  The programs in C are basically processor dependent whereas Embedded C programs are micro controller dependent.
  • 7.
    DIFFERENCE BETWEEN CONVENTIONALC AND EMBEDDED C.  The C program is used for developing an application and not suitable for embedded systems.  The embedded C is an extension of the conventional C. i.e Embedded C has all the features of normal C, but has some extra added features which are not available in C.  Many functions in C do not support Reentrant concept of functions.
  • 8.
    DIFFERENCE BETWEEN CONVENTIONALC AND EMBEDDED C.  C is not memory specific. i.e variables cannot be put in the desired memory location but the location of variable can be found out.  In embedded C this can be done using specific inbuilt instructions.  C depends on particular processor or application.  Embedded C is Controller or target specific.  Embedded C allows direct communication with memory.
  • 9.
    Why C forMicro controllers  Compatibility  Direct access to hardware address  Direct connection to interrupts  Optimization consideration  Development environment  Reentrancy
  • 10.
    PROGRAM FLOW INCROSS COMPILERS EDITOR Notepad or Dos .C .H .ASM .A51 COMPLILER ASSEMBLER .OBJ .OBJ Linker / Locator HEX file To Micro Controller
  • 11.
    BIT LEVEL PROGRAMMING& OPTIMIZATION  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 directly points the data in a particular segment of memory.  Structures and Unions are possible in bit operation. The bits of the variable can be accessed without using previously declared bit names.
  • 12.
    BIT LEVEL PROGRAMMING& OPTIMIZATION  It 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.  Limitations of bit level program  Bit pointer is invalid.  Array of bits is invalid.
  • 13.
    STARTUP CODE  Startupcode is an extra piece of software that executes prior to main(). The startup code is generally written in assembly language and linked with any executable that you build.  It prepares the way for the execution of programs written in a high-level language.  Each such language has its own set of expectations about the run-time environment in which programs are executed.  For example, many languages utilize a stack. Space for the stack must be allocated and some registers or data structures initialized before software written in the high-level language can be properly executed.
  • 14.
    EMBEDDED SOFTWARE DEVELOPMENT The embedded software development tools, cannot make assumption about the target platform.  The user has to provide some details of the system to the tools through explicit statements or instructions.  Once these data are given to the tools, these tools generate the expected outputs on the computer system itself so that the programmer can analyze or make suitable changes in the software to get the desired output.
  • 15.
    REENTRANCY, STATIC, VOLATILEKEYWORDS  A variable is a named object that resides in the RAM.  In C programs each function call causes a frame to be pushed on to stack which contains function parameters and allocation of the locals of the functions.  In embedded C there is no stack allocated .  In a block of memory, a function is given a fixed address space for its local variables .  Thus recursive calls to a function will cause the data in the variables to be corrupted.
  • 16.
    REENTRANCY, STATIC, VOLATILEKEYWORDS  In this, a separate copy of the locals for each function exists.  Because the stack is simulated reentrant functions are large.  Care should be taken that these function do not cross the memory limitations when copies of the functions are created.  This also requires the elimination of any bit parameters, locals and return values from reentrant functions.
  • 17.
  • 18.
  • 19.
    VOLATILE MODIFIERS  Volatileis global variable.  These are specifically used in case of ports or interrupts.  Features :  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.
  • 20.
    VOLATILE MODIFIERS Eg: # include unsignedvolatile char time; main( ) { time = 0; while(time <100){}; Void AutoUpdate(void) { time=time+1; }
  • 21.
    VOLATILE MODIFIERS  Volatilemodifier 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.
  • 22.
    CREATING ENVIRONMENT VARIABLES 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.
  • 23.
    COMPILING WITH ANEMBEDDED COMPILER
  • 24.
    CREATING ENVIRONMENT VARIABLES 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.
  • 25.
    CREATING EXECUTABLE PROGRAMS,TEMPORARY FILES,INCLUDE FILES AND LIBRARY FILES.  The C source code is compiled using c51 compiler by invoking C51.exe.  The command line is C51 source files [directives….] where Source files: is the name of the source program to be compiled. Directive: are directives to the compiler to control the function of the compiler  The source code is usually developed in C or assembly which are executable programs.  C51 compiler generates a series of output files during compilation.  Basename.lst : (list file) these contain formatted source text with any errors detected by the compiler .
  • 26.
    CREATING EXECUTABLE PROGRAMS,TEMPORARY FILES,INCLUDE FILES AND LIBRARY FILES.  Basename.obj: (object code) These contain the relocatable object code.These are linked to an absolute module by L51 linker/locator.  Basename.I:contains source text as expanded by the preprocessor.All macros are expanded and all comments are deleted on this listing.32  Basename.src: these are assembly source files generated from c source code.These are assembled with A51 assembler.  Basename.hex (I): this is a hex file or a binary file used to program the device.
  • 27.
    Include files:  Embeddedc program needs some important include and library files.Include 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 PIC18F458 are “PIC16Fxx.h”.  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. CREATING EXECUTABLE PROGRAMS, TEMPORARY FILES,INCLUDE FILES AND LIBRARY FILES.
  • 28.
    The most commonlyused compilers are keil- 2,3 , Pic C and Hitech C. In 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. CREATING EXECUTABLE PROGRAMS, TEMPORARY FILES,INCLUDE FILES AND LIBRARY FILES.
  • 29.
  • 30.
     Code Optimization. Declare local variables in the inner most scope  Do not declare all the local variables in the outermost function scope.  If local variables are declared in the inner most scope.  If the parameter was declared in the outermost scope, all function calls would have incurred the overhead of object .  Place case labels in narrow range  If the case labels are in a narrow range, the compiler does not generate a if-else-if cascade for the switch statement.  This code generated is faster than if-else-if cascade code that is generated in cases where the case labels are far apart. RULES FOR DEVELOPING EMBEDDED C PROGRAM
  • 31.
     Reduce thenumber of parameters Function calls with large number of parameters may be expensive due to large number of parameter pushes on stack on each call. For the same reason, avoid passing complete structures as parameters. Use pointers and references in such cases.  Use references for parameter passing and return value for types bigger than 4 bytes  Passing parameters by value results in the complete parameter being copied on to the stack. This is fine for regular types like integer, pointer etc. These types are generally restricted to four bytes. When passing bigger types, the cost of copying the object on the stack can be prohibitive. When the function exits the destructor will also be invoked. RULES FOR DEVELOPING EMBEDDED C PROGRAM
  • 32.
     SFRS &Registers 1. Use All the SFR’s in capital letters only. 2. Reduce the warnings in the program. 3. Make use of MACRO definitions in the program. 4. Always define the variables in the code memory by using the keyword code in declaration. 5. Eg unsigned int code a[] = { }; 6. Always define as unsigned type of declaration. 7. Make use of sbit definition for single bit declaration. 8. Eg sbit rs = P3^6; RULES FOR DEVELOPING EMBEDDED C PROGRAM
  • 33.
     So wecannot 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);  Infinite loop can be achieved by while(1) { ……… } RULES FOR DEVELOPING EMBEDDED C PROGRAM
  • 34.