2. What is Embedded C
programming
Embedded C is one of the most popular and most
commonly used Programming Languages in the
development of Embedded Systems.
Embedded C is perhaps the most popular
languages among Embedded Programmers for
programming Embedded Systems.
There are many popular programming languages
like Assembly, BASIC, C++, Python etc. that are
often used for developing Embedded Systems but
Embedded C remains popular due to its efficiency,
less development time and portability.
3. What is Embedded C
programming
Embedded C is an extension of C language
and it is used to develop micro-controller
based applications.
The extensions in the Embedded C language
from normal C Programming Language is the
I/O Hardware Addressing, fixed-point
arithmetic operations, accessing address
spaces, etc.
Embedded C Program has five layers of Basic
Structures. They are:
4. Basic structure of Embedded C
Comment: These are simple readable text, written in code to
make it more understandable to the user. Usually comments are
written in // or /* */.
Pre-processor directives: The Pre-Processor directives tell the
compiler which files to look in to find the symbols that are not
present in the program.
Global Declaration: The part of the code where global variables
are defined.
Local Declaration: The part of the code where local variables
are defined.
Main function: Every C program has a main function which
drives the whole code. It basically has two parts the declaration
part and the execution part. Where, the declaration part is where
all the variables are declared, and the execution part defines the
whole structure of execution in the program.
5. Differences between C and
Embedded C:
Parameters C Embedded C
GENERAL
•C is a general purpose
programming language,
which can be used to
design any type of desktop
based applications.
•It is a type of high level
language.
•Embedded C is simply an
extension C language and it is
used to develop micro-
controller based applications.
•It is nothing but an extension
of C.
DEPENDEN
CY
•C language is hardware
independent language.
•C compilers are OS
dependent.
•Embedded C is fully hardware
dependent language.
•Embedded C are OS
independent.
6. Differences between C and
Embedded C:
Parameters C Embedded C
COMPILER
•For C language, the
standard compilers can be
used to compile and
execute the program.
•Popular Compiler to
execute a C language
program are:
• GCC (GNU
Compiler collection)
• Borland turbo C,
• Intel C++
•For Embedded C, a
specific compilers that
are able to generate
particular
hardware/micro-
controller based output is
used.
•Popular Compiler to
execute a Embedded C
language program are:
• Keil compiler
• BiPOM
ELECTRONIC
• Green Hill
software
7. Differences between C and
Embedded C:
Parameters C Embedded C
USABILITY AND
APPLICATION
•C language has a free-format of
program coding.
•It is specifically used for desktop
application.
•Optimization is normal.
•It is very easy to read and modify
the C language.
•Bug fixing are very easy in a C
language program.
•It supports other various
programming languages during
application.
•Input can be given to the
program while it is running.
•Applications of C Program:
• Logical programs
• System software
programs
•Formatting depends upon the type
of microprocessor that is used.
•It is used for limited resources like
RAM and ROM.
•High level of optimization.
•It is not easy to read and modify
the Embedded C language.
•Bug fixing is complicated in a
Embedded C language program.
•It supports only required
processor of the application, and
not the programming languages.
•Only the pre-defined input can be
given to the running program.
•Applications of Embedded C
Program:
• DVD
• TV
• Digital camera
8. Keywords in Embedded C
A Keyword is a special word with a special meaning
to the compiler (a C Compiler for example, is a
software that is used to convert program written in C
to Machine Code).
For example, if we take the Keil’s Cx51 Compiler (a
popular C Compiler for 8051 based Microcontrollers)
the following are some of the keywords:
bit
sbit
sfr
small
large
9. Keywords in Embedded C
The following table lists out all the keywords
associated with the Cx51 C Compiler.
_at_ alien bdata
bit code compact
data far idata
interrupt large pdata
_priority_ reentrant sbit
sfr sfr16 small
_task_ using xdata
10. Data Types in Embedded C
Data Types in C Programming Language (or any
programming language for that matter) help us
declaring variables in the program.
There are many data types in C Programming
Language like signed int, unsigned int, signed char,
unsigned char, float, double, etc. In addition to these
there few more data types in Embedded C.
The following are the extra data types in Embedded
C associated with the Keil’s Cx51 Compiler.
bit
sbit
sfr
sfr16
11. Program variables
A variable is an addressable storage location to
information to be used by the program.
Each variable must be declared to indicate size
and type of information to be stored, plus name to
be used to reference the information
intx,y,z;//declares 3 variables of type “int”
char a,b;//declares 2 variables of type “char”
Space for variables may be allocated in registers,
RAM, or ROM/Flash (for constants)
Variables can be automatic or static
12. Variable arrays
An array is a set of data, stored in consecutive
memory locations, beginning at a named
address
Declare array name and number of data
elements, N
Elements are “indexed”, with indices [0 .. N-1]
int n[5]; //declare array of 5 “int” values
n[3] = 5;//set value of 4tharray element
13. C statement types
Simple variable assignments
Includes input/output data transfers
Arithmetic operations
Logical/shift operations
Control structures
IF, WHEN, FOR, SELECT
Function calls
User-defined and/or library functions
14. C control structures
Control order in which instructions are executed
(program flow)
Conditional execution
Execute a set of statements if some condition is
met
Select one set of statements to be executed from
several options, depending on one or more
conditions
Iterative execution
Repeated execution of a set of statement
A specified number of times, or
Until some condition is met, or
16. SWITCH statement
Compact alternative to ELSE-IF structure, for multi- way
decision that tests one variable or expression for a
number of constant values
17. WHILE loop structure
Compact alternative to ELSE-IF structure, for multi- way
decision that tests one variable or expression for a
number of constant values
18. FOR loop structure
Repeat a set of statements (one “loop”) while some
condition is met
Often a given # of iteration
19. C functions
Functions partition large programs into a set of
smaller tasks
Helps manage program complexity
Smaller tasks are easier to design and debug
Functions can often be reused instead of starting
over
Can use of “libraries” of functions developed by
3rd parties, instead of designing your own
A function is “called” by another program to
perform a task
The function may return a result to the caller
One or more arguments may be passed to the
function/procedure
21. Function arguments
Calling program can pass information to a
function in two ways
By value: pass a constant or a variable value
function can use, but not modify the value
By reference: pass the address of the variable
function can both read and update the
variable
Values/addresses are typically passed to the
function by pushing them onto the system stack
Function retrieves the information from the
stack
23. MACROs in C
In C programming macro is a segment of the code
that has some unique name. Whenever in a
program we used the macro name, it is replaced
by the contents of the macro.
Consider the below example,
#define MECHATRONICS 78
In my code where I will use “MECHATRONICS ”,
it is replaced with 78.
These replacements occur before
the compilation of the code which is the reason
there is no type checking occurring with C
macros. Basically, there are two types of
macros, object-like macros, and function-like
macros.
24. Object-like Macros in C
An object-like macro is a simple identifier that will
be replaced by a code segment (a piece of code)
in the program.
It is mainly used to give a symbolic name to a
numeric constant and it is a good habit to give a
meaningful name to a numeric constant.
Like if you are creating a macro to represent the
second, you should write the macro name
SECOND. It increases the code readability.
To define a new macro in C, We can use the
#define. A #define is a pre-processor directive.
25. Object-like Macros in C
Syntax of the object-like macro,
#define MACRO_NAME MACRO_VALUE
For Example,
#define ARRAY_SIZE 1024
Now ARRAY_SIZE is an alias of 1024, whenever
in the program we will use ARRAY_SIZE it
replaces with 1024.
//Declaration of array
int arr[ARRAY_SIZE];
26. Object-like Macros in C
Before the compilation of the code, the C
preprocessor will replace the ARRAY_SIZE
with 1024 and the array will look like the below
expression,
int arr[1024];
Note: Macro definitions need not be
terminated by a semi-colon(;)
27. Example
#include <stdio.h>
#define PI_VALUE 3.1415
int main()
{
float radius, circleArea;
printf("Enter the radius: ");
scanf("%f", &radius);
//We are using PI_VALUE
circleArea = PI_VALUE*radius*radius;
printf("Area=%.2f",circleArea);
return 0;
}
Output:
Enter the radius:
3
Area=28.27
28. Function like Macro in C:
A function-like macro looks like the function is
because it has a pair of parentheses like the
function.
Similar to the object like macro #define use to
create a function-like macro. You need to put a
pair of parentheses immediately after the
macro name.
For example,
#define MAX(A, B) (((A) > (B)) ? (A) : (B))
29. Example
#include <stdio.h>
#define MAX(A, B) (((A) > (B)) ? (A) : (B))
int main()
{
int a = 10;
int b = 20;
//check max value
int ret = MAX(a,b);
printf("Max value %dn", ret);
return 0;
}
Output:
Max value 20
30. Function like Macro in C:
Remember the following points before using
the function-like a macro in c.
Always use proper parentheses in macro.
Use each macro parameter once to avoid the
undesired side effect.
You should never use a function like macro if you
can use the function.
Always remember there is no type checking occur
in macro.
You should remember the following points before
using the function-like a macro in c
31. Some C Predefined Macros
The following is the list of some predefined
Macros which you should know.
Macro Description
_DATE_ current date in "MM DD YYYY" format.
_TIME_ current time in "HH:MM:SS" format.
_FILE_ current file name.
_LINE_ current line number.
32. Memory Map In C
A typical memory representation of C
program consists of following sections.
1. Text segment
2. Initialized data segment
3. Uninitialized data segment
4. Stack
5. Heap
33. Text Segment:
A text segment , also known as a code segment
or simply as text, is one of the sections of a
program in an object file or in memory, which
contains executable instructions.
As a memory region, a text segment may be
placed below the heap or stack in order to prevent
heaps and stack overflows from overwriting it.
Usually, the text segment is sharable so that only
a single copy needs to be in memory for
frequently executed programs, such as text
editors, the C compiler, the shells, and so on.
Also, the text segment is often read-only, to
prevent a program from accidentally modifying its
instructions.
34. Initialized Data Segment:
Initialized data segment, usually called simply
the Data Segment.
A data segment is a portion of virtual address
space of a program, which contains the global
variables and static variables that are
initialized by the programmer.
Note that, data segment is not read-only, since
the values of the variables can be altered at
run time.
This segment can be further classified into
initialized read-only area and initialized read-
write area.
35. Uninitialized Data Segment:
Uninitialized data segment, often called the “bss”
(Block Started by Symbol) segment, named after an
ancient assembler operator that stood for “block started
by symbol.”
Data in this segment is initialized by the kernel to
arithmetic 0 before the program starts executing
uninitialized data starts at the end of the data segment
and contains all global variables and static variables
that are initialized to zero or do not have explicit
initialization in source code.
For instance a variable declared static int i; would be
contained in the BSS segment.
For instance a global variable declared int j; would be
contained in the BSS segment.
36. Stack
The stack area traditionally adjoined the heap area and
grew the opposite direction; when the stack pointer met the
heap pointer, free memory was exhausted. (With modern
large address spaces and virtual memory techniques they
may be placed almost anywhere, but they still typically grow
opposite directions.)
The stack area contains the program stack, a LIFO
structure, typically located in the higher parts of memory.
On the standard PC x86 computer architecture it grows
toward address zero; on some other architectures it grows
the opposite direction.
A “stack pointer” register tracks the top of the stack; it is
adjusted each time a value is “pushed” onto the stack.
The set of values pushed for one function call is termed a
37. Stack
Stack, where automatic variables are stored,
along with information that is saved each time a
function is called. Each time a function is called,
the address of where to return to and certain
information about the caller’s environment, such
as some of the machine registers, are saved on
the stack.
The newly called function then allocates room on
the stack for its automatic and temporary
variables. This is how recursive functions in C can
work.
Each time a recursive function calls itself, a new
stack frame is used, so one set of variables
doesn’t interfere with the variables from another
instance of the function.
38. Heap:
Heap is the segment where dynamic memory
allocation usually takes place.
The heap area begins at the end of the BSS
segment and grows to larger addresses from
there.
The Heap area is managed by malloc, realloc,
and free, which may use the brk and sbrk
system calls to adjust its size (note that the use
of brk/sbrk and a single “heap area” is not
required to fulfill the contract of
malloc/realloc/free).
The Heap area is shared by all shared libraries
39. Example
The size(1) command reports the sizes (in
bytes) of the text, data, and bss segments.
1. Check the following simple C program
#include <stdio.h>int main(void){return 0;}
gcc memory-layout.c -o memory-layout
size memory-layout
text data bss dec hex filenam
e
960 248 8 1216 4c0 memory-
layout
40. Example
2. Let us add one global variable in program,
now check the size of bss (highlighted in red
color).
#include <stdio.h>
int global; /* Uninitialized variable stored in bss*/
int main(void){return 0;}
gcc memory-layout.c -o memory-layout
size memory-layout
text data bss dec hex filename
960 248 12 1220 4c4 memory-
layout
41. Embedded Systems - Registers
Bank/Stack
The 8051 microcontroller has a total of 128
bytes of RAM.
The 128 bytes of RAM inside the 8051 are
assigned the address 00 to 7FH.
They can be accessed directly as memory
locations and are divided into three different
groups as follows
43. SFR’s
The internal RAM or Data Memory of the 8051
Microcontroller is divided in to General Purpose
Registers, Bit Addressable Registers, Register
Banks and Special Function Registers or SFRs.
The 8051 Microcontroller Special Function
Registers are used to program and control
different hardware peripherals like Timers, Serial
Port, I/O Ports etc. In fact, by manipulating the
8051 Microcontroller Special Function Registers
(SFRs), you can assess or change the operating
mode of the 8051 Microcontroller.
The following image shows you the basic
structure of 8051 Microcontroller’s Internal RAM.
44. SFR’s
The 8051 Microcontroller Special Function
Registers act as a control table that monitor and
control the operation of the 8051 Microcontroller.
If you observe in Internal RAM Structure, the
Address Space from 80H to FFH is allocated to
SFRs.
Out of these 128 Memory Locations (80H to FFH),
there are only 21 locations that are actually
assigned to SFRs. Each SFR has one Byte Address
and also a unique name which specifies its purpose.
Since the SFRs are a part of the Internal RAM
Structure, you can access SFRs as if you access
the Internal RAM.
The main difference is the address space: first 128
Bytes (00H to 7FH) is for regular Internal RAM and
45. Embedded Systems - Registers
Bank/Stack
32 bytes from 00H to 1FH locations are
set aside for register banks and the stack.
16 bytes from 20H to 2FH locations are
set aside for bit-addressable
read/write memory.
80 bytes from 30H to 7FH locations are
used for read and write storage;
it is called as scratch pad.
These 80 locations RAM are widely used
for the purpose of storing data and
parameters by 8051 programmers.
46.
47. Concept of Portability and
Scalibility
Scalability and Portability are 02 very different
concepts in computer programming and hence
there is no comparison nor differentiation
possible.
Scalability is a property of a software system,
typically is considered as one of the advantages
of using a parallel computing or distributed
computing system.
However, it also applies to a single computing
system as well.
A system (computer+operating
environment+application software) is considered
scalable if the computing capacity of the system
like CPU, memory, secondary storage, etc. could
be increased in case of high load on the system.
48. Concept of Portability and
Scalibility
Generally, a system could be
Vertically Scalable: It means a particular system
can be upgraded with more compute capacity
like CPU, memory, secondary storage, etc.
There is always an upper limit to which a system
can be scaled up. Once that limit is reached, it
cannot be scaled up.
Scale Up architecture is another term for
referring to a vertically scalable system.
Generally, single computing systems are scaled
in this way.
49. Concept of Portability and
Scalibility
Horizontally Scalable: It is used in some form of
a parallel or distributed computing system like a
cluster where instead of upgrading a particular
machine, a new machine or node as it is referred
in a cluster is added to the cluster providing
more compute capacity to the cluster.
Theoretically speaking, there is no upper level
limit on the number of nodes added to a cluster.
Scale Out architecture is another term for
referring to a horizontally scalable system.
Generally, distributed computing systems like a
50. Concept of Portability and
Scalibility
Portability is a property of a software program or
programming language which states that a program
written and compiled in a particular environment can be
executed on other similar or different environments.
WORA (Write Once Run Anywhere) is used to describe
this aspect of portability.
For example, Java is considered a portable
programming language as a Java program is compiled
to bytecodes and not to a machine specific native code.
This compiled version of the program is portable as it
can be moved to any environment which has a JRE
(Java Runtime Environment) available. The JRE (an
implementation of JVM) is used for executing the
program by converting the bytecode into machine
51. Concept of Portability and
Scalibility
From the mathematical software developer's
point of view, portability may require significant
effort.
Standards permit the effort of developing and
maintaining bodies of mathematical software to
be leveraged over as many different computer
systems as possible.
Given the diversity of parallel architectures,
portability is attainable to only a limited degree,
but machine dependencies can at least be
isolated.
52. Concept of Portability and
Scalibility
Scalability demands that a program be
reasonably effective over a wide range of
numbers of processors.
The scalability of parallel algorithms over a
range of architectures and numbers of
processors requires that the granularity of
computation be adjustable.
To accomplish this, we use block algorithms
with adjustable block sizes.
Eventually, however, poly algorithms (where the
actual algorithm is selected at runtime