0% found this document useful (0 votes)
16 views6 pages

Embedded C - Lecture 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views6 pages

Embedded C - Lecture 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Embedded C Programming Concepts

Lecture 2: Programming in Embedded C

2.1 Introduction: Whenever the conventional „C‟ language and its extensions are used for
programming embedded systems, it is referred as „Embedded C‟ programming.

2.2 ‘C’ v/s ‘Embedded C’


C Embedded C
Well structured, well defined and standardized Embedded „C‟ can be considered as a subset
general purpose programming language conventional „C‟
A platform specific application, known as, A software program called cross-compiler is
compiler is used for the conversion of the used for the conversion of program written in
programs written in C to the target processor Embedded C to the processor/controller target
specific binary files. processor specific instructions (machine
language).
C compiler generates operating system Embedded C compilers generate hardware-
dependent executable files. dependent files. They can be uploaded into the
microcontroller to perform the task.
Hardware Independent Hardware dependent
Network drivers, interpreters, compilers, Embedded C helps to develop embedded
operating systems, and, text editors are some systems such as robots, vehicle tracking
example for C applications. systems, smart monitoring systems, etc.
Dennis M. Ritchie developed C language the C Standards Committee developed
Embedded C.

2.3 Compiler vs. Cross-compiler


Compiler Cross-compiler
A compiler is software that translates the A cross compiler is a software that can create
computer code written in high-level executable code for platforms other than the
programming language to machine language. one on which the compiler is running
Here the OS, the compiler program and the Here the embedded firmware is developed on a
application making use of the code run on the machine with Intel/AMD or any other
same target processor. processor and the same is converted into
Example: C machine code for any other target processor
architecture.
Example: Keil C51 is a cross0compiler

2.4 Using ‘C’ in ‘Embedded C’


NOTE: C language is well known to you. Now you know that „Embedded C‟ is a subset of „C‟. So
here only the basic points of „C‟ which are extensively used are mentioned, not discussed.
i) Keywords and identifiers:
 32 keywords(lowercase only)
 Identifiers are user defined names and labels (can contain letters, numbers and only
special character („_‟).Identifier should start with letter.

ii) Data Types: The following table is given for 32 bit platform

For embedded application memory is a big constraint. So optimum data type for a variable is
selected. For example if the variable is expected to be in the range 0 to 255 then „unsigned char‟
or „unsigned short int‟ data types are used instead of „int‟ or „unsigned int‟.
iii) Storage class:

Apart from the above four storage classes, „C‟ supports storage class „global‟. There is no explicit
storage class for „global‟. The way of declaration of a variable determines whether it is global or not.

iv) Arithmetic Operations

v) Logical Operations
vi) Relational Operations

vii) Branching Instructions

Conditional branching instructions Unconditional branching instructions


Depends on certain conditions and if the Divert program execution
conditions are met, the program execution unconditionally
is diverted accordingly.
Example Example
1. If statement goto lable
2. If-else statement
3. Switch case statement

viii) Looping instructions:


 Used for executing a particular block of code repeatedly till a condition is met or wait till
an event is fired
 Used to check the status of certain I/O ports, registers, etc. and also for producing delays
 Certain devices allow write/read operations to and from some registers of the device only
when the device is ready and the device ready is normally indicated by a status register or
by setting/clearing certain bits of status registers. Hence the program should keep on
reading the status register till the device ready indication comes
 The reading operation forms a loop
 Looping instructions in C are i) while ii) do while iii) for iv)break v)continue

ix) Array and pointer


 Array is a collection of related data types (elements).Arrays are usually declared
with data type of array, name of the array and the number of related elements to be
placed in the array.
 Pointer is a memory pointing based technique for variable access and modification.
 Pointers are very helpful in
1. Accessing and modifying variables
2. 2.Increasing speed of execution
3. Accessing contents within a block of memory
4. Passing variables to functions by eliminating the use of local copy of variables
5. Dynamic memory allocations
x) Characters and Strings
 Character is a one byte data type and it can hold values ranging from 0 to 255
(unsigned character) or -128 to +127(signed character.
 String is an array of characters
 'H‟ is a character where as “Hello” is a string
xi) Functions
 Basic building blocks of modular programs
 Self-contained and re-usable code snippet intended to perform a particular task.
 Embedded C supports two different types of functions namely, library functions
and user defined functions.
 Library functions are the built-in functions which is either part of the standard
“Embedded C” library or user created library files. For example
1. I/O library functions (printf(),scanf())
2. String operations library functions(strcpy(),strcmp())
3. Memory allocation library function(malloc(),calloc())

A corresponding header file (.h) provides information for library function.

 User defined functions are programmer created functions for various reasons like
modularity, easy understanding of code, code reusability etc.
xii) Structures and Unions
 „structure‟ is a variable holding a collection of data types ( int, float, char, long
etc.).The data types can be either unique or distinct.
 Union is a concept derived from structure and union declaration follows the same
syntax.
 Union differs from structure in the memory allocation technique for the member
variables. Whenever union variable is created, memory is allocated only to member
variable of union requiring the maximum storage size. But for structure memory is
allocated to each member variables.
 Because of the above reason, in embedded C applications , union may be used for
fast accessing of individual bytes of „long‟ or „int‟ variables, eliminating the need
for masking the other bytes of „long‟ or „int‟ variables which are of no interest, for
checking some conditions.

--------------------------------------------------END OF LECTURE 2---------------------------------------------

1. Introduction to Embedded Systems – Shibu K.V Mc Graw Hill

-----------------------------------STAY HOME,BE PREPARED----------------------------------------

You might also like