Anda di halaman 1dari 57

| || 

à 
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 }
? 
  
  


Ö Compliers for conventional C are TC, BC


Ö Compilers for Embedded C are keil µvision - 2 &
3, P C 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.
Ö The C program are 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.
Ö any functions in C do not support
Reentrant concept of functions.
Ö C are not memory specific. i.e variables
cannot be put in the desired memory
location but the location of variable can be
found out.
Ö n embedded C this can be done using
specific inbuilt instructions.
Ö C depends on particular processor or
application.
Ö Embedded C are Controller or target
specific.
Ö Embedded C allows direct communication
with memory.
ã  
 
Ö Compatibility
Ö Direct access to hardware address
Ö Direct connection to interrupts
Ö Optimization consideration
Ö Development environment
Ö Reentrancy
•      
ED TOR
Notepad or Dos

  Ä  Ä 

@• Ä  

Ä
Ä

@ ! @ !

 " 


#

à  
 

•   $@
%

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`  
-.
 
 ? 

Ö 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.

 &

&' 
" 
Ö A variable is a named object that resides in the
RA.
Ö n 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.
Ö n embedded C there is no stack allocated .
Ö n 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.
Ö n 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.
' 

ö 
     Äà






ö 
 Ä

ö 
 à=•à ö 

 


Ä 
(

     ' 
  
 "  
 ' 
    
  
    


  
=  )
 
   


 $ )

 

   
 

 
 
' 

ö  

ö 
  

 àÄ*
 
 
  (
  
  

 
   
    



 

  

   
    



 

  
   
    
(


 
'@Äà@?ö
Ö 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.
  ã
Ä  

•'' !"#

 @=+,

  
@
@-
 
 ö
ö   +
,
+ - ,     "
ĕö
+ ,
Ä 
@-

ö
@ @-
(
 )

 

? ) 
 

#ö+(, ? 

•  ?

 (
•  &
  
&   

Ö 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 .
Ö Basename.obj: (object code) These contain the
relocatable object code.These are linked to an
absolute module by L51 linker/locator.
Ö Basename. :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 ( ): this is a hex file or a binary file
used to program the device.

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

Compiler Cross Compiler


Uses OS as
Platform
Uses OS as
Platform 1. Edit the program
1. Edit the program
2. Compilation
2. Compilation

3. Running / Uses 2nd platform


Execution
ie hardware
4. Output
Execution and output
@ !
After the C file is compiled object files (.Obj) are
generated.These files are used to link with other
.obj , library (.Lib) files to generate an absolute
object file.To invoke the linker L51 has to be used.
The structure of these object files is defined by a
standard format like COFF (³Common Object File
Format´) or ELF(Extended Linker Format).
  
Assembler is also a compiler . t is also called as
assembly language compiler.The assembler compiles
and generates object files for an assembly program
only.These assemblers produce the same object file
as C compiler.
? 
Are used for the step by step execution of the
program. n this for each execution in steps, the
debugger operates on the object code generated
during compile time.

 "
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.

Ö This script informs the linker¶s built in locator


about the memory on the target board and instructs
it to locate the data and BSS sections in the RA.

Ö The result of this final step of build process is an


absolutely located binary image that can be
downloaded to an embedded system or
programmed in to a read ±memory device.


Emulators are    which duplicate the
functions of one system as that of the other system, so
that second system appears to behave as the first
system.
Emulators are basically the processor modules which
are custom programmed to behave as the actual target
system as it was a real target.
These are based on single processor. The processor are
preprogrammed for the target requirement.
n the actual circuit all the devices are physically
present on the system so that they provide the desired
signals to the processor or controller to do its job.
Ö n this, since the processor are preprogrammed, the
signals generated by the actual devices on the target
are particularly generated by the processor and the
processor generates corresponding required output
which should be actually generated by the target itself.


Simulators are software, present in the DE
that simulates the hardware conditions which
would have been generated by the actual
hardware components in the target.
These help the programmer to debug the
program step by step and check the proper
flow of the program. This works on the
concept of virtual circuit. We can check the
output by simulating the required condition.
8051 Simulator simplifies code development
with icro C and icro- DE.
Errors in user programs can be found and fixed
quickly in simulation mode by avoiding time
consuming downloads to the target board. Support
for hardware ports in simulation. Use real /O
ports from your target boards while simulating the
program on your PC. ntegrated Development
Environment to edit, build, download, simulate
and debug within the same program. Simulation of
8051 programs in C, Assembly or mixed level.
Simulated program can be simultaneously viewed
in C and Assembly.
Variable window to watch C variable names,
values and addresses.
Register window to watch the simulated special function
registers including all ports.
emory window to watch and modify up to 4GB of
simulated program memory, 256kB.
Call stack window to view list of function calls that lead to
current program line (traces all jumps and calls )
Terminal window to simulate the serial port ( both receive
and transmit are simulated )
Output Window Debug Tab to watch debug messages
Fully customizable window layout with dockable or
floating debug windows. Stop Debugging button to stop
simulation at any point Go button to start execution Step
nto and Step Over buttons to single-step through the
source code at C or Assembly level Unlimited number of
breakpoints to stop execution at any C or Assembly source
line.
   
•  
Ö Code Optimization.
2  



]  

 
     
 

 

 
         

         



 

         
   

       
 



 
       
          
 ] 

 
          

 

         


      
2  



  
ë 1 1  11 11
 1
1 1
  1 
 1  `1
ë 11
 1 1  1
1 1
1 1
 `1
ë 1 1  1 1  1
1 1  1
 11 
 1 11 1
 1 1
  112 1`
Ö •





ë1 1 1  1 1
11 1  1 1
 1
 1 1  11
  
1  11 1 
1
  `
ë   1
1  1121 11 1  1 1

1

1 1 11 1 
11
 1 1
 `1
ë 
1 1  1
1  1 1
  
1  1
 11
1  1
1 1  1 1 1  1
 11`1
ë( 1   11121 1 1 
1
  1
1
   11 1  11 1 
1

1 
1   `
'      
 
 1 1
1 1  11   11

 1 11 1  11  1  1 11
1 1 1`
1 1  1   1
1
1  1  11
 1   `13 1
  1 1    1
1 1 

=   
      
     
•
1   11 1  1
1 1  1111
  1
1
1 11 1 `1

1
1
11 1 1
 1
  1
 1 `1
 1 1 1 1 
 111 `1
4 1
1
 1 1 1 11
1 12 
 1 1 1 1 1


 `1
4 1 1 
 1
 1 1 1
1 1 1
 `
 1
1
1 

11 1    1 1   `1 
1
11  1 1 1  111 12 1
 
 1
1 1 
 `1 
1


 1 1
 1  1 
1
1121
11 1 11
 
1 11 1   11 1    `
-1
1
 11 1 1    1 1111
1
 1 1 
 1 1 1  1 11 11 1
  `
•
1
 12  1 1  1 1 1 1 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)
{
«««
}

Anda mungkin juga menyukai