You are on page 1of 150

Embedded C

Programming & Debbuging


Technique
UNIT-3
Embedded C Programming 8051
1. Difference between C & Embedded C
2. Basic structure of Embedded C Programming
3.Keywords
4.Data Types
5.Operators
6.Conditional Branching
7.Loops
8.Macros
9.Functions
Embedded C Programming 8051
10.I/O Programming
11.Timmer Programming
12.Serial Port Programming
Debugging Tools & techniques:
1.Host and Target Machines
2.Linkers/Locators for Embedded Software
3.Getting Embedded s/w into the Target system
4.Testing on Host Machine using Laboratory tools
BASIC ASSEMBLY LANGUAGE
PROGRAMMING CONCEPTS
Introduction to Embedded Programming
General C vs Embedded C
Criteria C
Embedded C

GENERAL • C is a general purpose • Embedded C is simply an


programming language, extension C language and it is
which can be used to design used to develop micro-
any type of desktop based controller based applications.
applications. • It is nothing but an extension
• It is a type of high level of C
language.

DEPEND • C language is hardware • Embedded C is fully hardware


ENCY independent language dependent language
Introduction to Embedded Programming
General C vs Embedded C
Criteria C
Embedded C

DEPEND • C compilers are OS • Embedded C are OS


ENCY dependent. independent.

• For C language, the • For Embedded C, a specific


COMPILE standard compilers can compilers that are able to
R be used to compile and generate particular
execute the program. hardware/micro-controller
based output is used
Introduction to Embedded Programming
General C vs Embedded C
Criteria C Embedded C

• Popular Compiler to execute a


• Popular Compiler to execute a Embedded C language
COMPILE C language program are: program are:
R GCC (GNU Compiler Keil compiler
collection) BiPOM ELECTRONIC
Borland turbo C, Green Hill software
Intel C++

USABILIT • C language has a free- • Formatting depends upon the


format of program coding. type of microprocessor that is
Y AND used.
Introduction to Embedded Programming
General C vs Embedded C
Criteria C Embedded C

USABILIT • C language has a free- • Formatting depends upon the


Y AND format of program coding. type of microprocessor that is
used.

APPLICAT • It is specifically used for • It is used for limited


desktop application. resources like RAM and
ION ROM

• Optimization is normal. • High level of optimization.


• It is not easy to read and
• It is very easy to read and modify the Embedded C
modify the C language. language.
Introduction to Embedded Programming
General C vs Embedded C
Criteria C Embedded C

• Bug fixing are very easy in a C • Bug fixing is complicated


language program. in a Embedded C language
program.
• It supports other various
USABILIT programming languages • It supports only required
Y AND during application. processor of the
Applicati application, and not the
on • Input can be given to the programming languages.
program while it is
running. • Only the pre-defined input
can be given to the
running program.
Introduction to Embedded Programming
General C vs Embedded C
Criteria C Embedded C

• Applications of C • Applications of
Program: Embedded C Program:
USABILIT
Y AND • Logical programs • DVD
Applicati
on • System software • TV
programs
• Digital camera
 C Language:

• C is a general-purpose programming language, which is widely used to design any type

of desktop-based applications.

• It was developed by Dennis Ritchie as a system programming language to develop the

operating system.

.
 C Language:

• The main features of C language include low-level access to memory, a simple set of

keywords, and clean style, these features make C language suitable for system

programmings like OS or compiler development

• In nature it uses a native platform development scheme, ie the development of the

application by it is platform-dependent and can only be used on a single platform


Embedded C:

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 :

1.I/O Hardware Addressing,

2. fixed-point arithmetic operations,

3.accessing address spaces, etc.


Embedded C:

Embedded C Program has five layers of Basic Structures. They are:

• 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.
• the declaration part is where all the variables are declared,

• the execution part defines the whole structure of execution in the program.

• In nature it uses a cross-platform development scheme, i.e., the development of

the application by it is platform-independent and can be used on multiple

platforms.
❖ Machine language

• Native to a processor: executed directly by hardware

• Instructions consist of binary code: 1s and 0s

❖ Assembly language

• A programming language that uses symbolic names to represent operations, registers and

memory locations.

• Slightly higher-level language

• Readability of instructions is better than machine language

• One-to-one correspondence with machine language instructions


❖ Assemblers translate assembly to machine code

❖ Compilers translate high-level programs to machine code

• Either directly, or

• Indirectly via an assembler


Compiler and Assembler
Instructions and Machine Language

❖ Each command of a program is called an instruction (it instructs the

computer what to do).

❖ Computers only deal with binary data, hence the instructions must be in binary

format (0s and 1s) .

❖ The set of all instructions (in binary form) makes up the computer's machine

language. This is also referred to as the instruction set.


❖ Machine language instructions usually are made up of several fields.
Each field specifies different information for the computer.

❖ Major two fields are:


• Opcode
• Operands
Opcode
For example
MOV AL, 34h
The opcode is the MOV instruction.

Operands
MOV AL, 34h
Operands are manipulated by the opcode. In this example, the operands are
the register named AL and the value 34 hex.
Translating Languages
English: D is assigned the sum of A times B plus 10.

High-Level Language: D = A * B + 10
A statement in a high-level language is translated
typically into several machine-level instructions
Intel Assembly Language:
mov eax, A Intel Machine Language:
mul B A1 00404000
addeax, 10 F7 25 00404004
mov D, 83 C0 0A
eax A3 00404008
❖Some representative types of applications:
Keyword
s
• There are certain words that are reserved for doing specific tasks. These

words are known as keywords.

• They are standard and predefined in the Embedded C.

• Keywords are always written in lowercase. These keywords must be

defined before writing the main program.

• The basic keywords of an embedded software are given below:


Keyword
s
Keyword
s
 sbit: This data type is used in case of accessing a single bit of SFR register.

 Syntax: sbit variable name = SFR bit ;

 Ex: sbit a=P2^1;

 Explanation: If we assign p2.1 as ‘a’ variable, then we can use ‘a’ instead of p2.1 anywhere

in the program, which reduces the complexity of the program.


Keyword
s
 Bit: This data type is used for accessing the bit addressable memory of RAM (20h-2fh).

 Syntax: bit variable name;

 Ex: bit c;

 Explanation: It is a bit sequence setting in a small data area that is used by a program to

remember something.
Keyword
s
 SFR: This data type is used for accessing a SFR register by another name. Al

 the SFR registers must be declared with capital letters

 Syntax: SFR variable name = SFR address of SFR register

 Ex: SFR port0=0x80

 Explanation: If we assign 0x80 as ‘port0’, then we can use 0x80 instead of

 port0 anywhere in the program, which reduces the complexity of the


 SFR Register:

 The SFR stands for ‘Special Function Register’. Microcontroller 8051 has 256 bytes of
. memory.
RAM

 This RAM is divided into two parts: the first part of 128 bytes is used for data storage, and

the other of 128 bytes is used for SFR registers.

 All peripheral devices like I/O ports, timers and counters are stored in the SFR register,

and each element has a unique address


Data types
• The data type refers to an extensive system for declaring variables of different types
like integer, character, float, etc.
The embedded C software uses four data types that are used to store data in the
memory.
 The ‘char’ is used to store any single character; ‘int’ is used to store integer value,
and ‘float’ is used to store any precision floating point value.
 The size and range of different data types on a 32-bit machine is given in the
following table. The size and range may vary on machines with different word sizes.
Example of Unsigned bit and sbit
FLOW OF EXECUTION / CONDITIONAL BRANCHING
M
`g h

You might also like