Sunteți pe pagina 1din 42

AMC ENGINEERING COLLEGE

18 KM, BANNERGHATTA ROAD, BANGALORE-560083

DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING

CERTIFICATE

This is to certify that the internship report entitled Embedded Systems carried out by
Greeshma Gopal A, USN:1AM15EC043, at Tekatron IT Solutions, a bonafide student of AMC
Engineering College, Bangalore, in partial fulfillment for the award of degree of Bachelor of
Engineering in Electronics and Communication Engineering, of the Visvesvaraya
Technological University, Belgavi during the Year 2018-19. It is certified that all the
corrections/suggestions indicated for internal assessment have been incorporated in the report. The
internship report has been approved as it satisfies the academic requirements in respect of
prescribed for the said degree.

Signature of Internal Guide Signature of the HOD Signature of the Principal


(Mrs. Aruna Ramalingam) (Dr. N.V. Uma Reddy) (Dr. A G Nataraj)

External Viva Voce

Name of the Examiners Signature with Date


1.
2.
PROFILE OF THE COMPANY

Tekatron IT Solutions in Uttarahalli, Bangalore is a top company in the category Computer


Training Institutes.

Tekatron It Solutions Llp is a Limited Liability Partnership firm incorporated on 09


September 2016. It is registered at Registrar of Companies, Bangalore.

Designated Partners of Tekatron It Solutions Llp are Chethan Chikbilthi Revaiah and
Hamsa Ashwathnarayana.

The name is an amalgamation of TECHNOLOGY & ELECTRONICS and is an exclusive


end to end solution in hardware and software fields.

Contact number: +91 9740214947

Email address : chethanrev@gmail.com

Registered address:
# 209, 4th Cross Road Near Raghavendra Temple,
Srinivasa Colony,
Bangalore- 560061.
ACKNOWLEDGEMENT

I would like to thank our chairman Dr. K . R. Paramahamsa and CEO,


Dr. T. N. Sreenivasa, AMC Engineering College, Bangalore, for providing the necessary
infrastructure.

I sincerely thank Dr. N.V. Uma Reddy, HOD, Dept. of Electronics and Communication
Engineering, AMC Engineering College, Bangalore, from the bottom of my heart for her support
and understanding.

I would like to thank our Principal, Dr. A.G.Nataraj, AMC Engineering College,
Bangalore, for his kind cooperation.

I consider it my privilege to express gratitude to my guide, Mrs. Aruna Ramalingam,


Associate Professor, Dept. Of Electronics and Communication Engineering, AMC Engineering
College, Bangalore, without whose support and guidance, this Internship report would not have
been a success.

I wish to express my profound thanks to Mrs. Ashajyothi Mallya, Assistant Professor,


Dept. Of Electronics and Communication Engineering, AMC Engineering College, Bangalore, for
her constant support and valuable suggestions during the course of the Internship.

My deepest gratitude goes to my teachers, who showed infinite patience and understanding
till the completion of my Internship.

Last, but not the least, my sincere credit to my parents, my friends and to one and all who
have directly or indirectly helped me in the successful completion of the Internship.

GREESHMA GOPAL A
(1AM15EC043)
DECLARATION

I hereby declare that the Internship report entitled “EMBEDDED SYSTEMS” has been
carried out by me under the guidance of my guide, Mrs. Aruna Ramalingam, Associate Professor,
Dept. Of Electronics and Communication Engineering, AMC Engineering College, Bangalore and
submitted in partial fulfillment of the course requirements for the award of the degree in Bachelor
of Electronics and Communication Engineering during the year 2018-19.

The matter embodied in this report has not been submitted to any other university or institution
for the award of any other degree or diploma.

GREESHMA GOPAL A
(1AM15EC043)
EMBEDDED SYSTEMS

LIST OF CONTENTS
CHAPTER CHAPTER NAME PAGE
No. NO.
1. INTRODUCTION TO EMBEDDED SYSTEMS
1.1 SYSTEM 1
1.2 EMBEDDED SYSTEM 1
2. CHARACTERISTICS OF AN EMBEDDED SYSTEM, ITS
ADVANTAGES AND DISADVANTAGES
2.1 CHARACTERISTICS OF AN EMBEDDED SYSTEM 3
2.2 ADVANTAGES AND DISADVANTAGES OF AN EMBEDDED 4
SYSTEM
3. BASIC STRUCTURE OF AN EMBEDDED SYSTEM 5
4. PROCESSORS
4.1 PROCESSORS IN A SYSTEM 6
4.2 TYPES OF PROCESSORS 6
4.3 MICROPROCESSOR VS MICRCONTROLLER 8
4.4 VON NEUMANN ARCHITECTURE 9
4.5 HARVARD ARCHITECTURE 10
4.6 VON-NEUMAN VS HARVARD ARCHITECTURE 11
4.7 CISC AND RISC 11
5. CLASSIFICATIONS OF EMBEDDED SYSTEM
5.1 SMALL SCALE EMBEDDED SYSTEMS 13
5.2 MEDIUM SCALE EMBEDDED SYSTEMS 13
5.3 SOPHISTICATED EMBEDDED SYSTEMS 14
6. MOORE’S LAW 15
7. 7.1 PROGRAM
7.2 MACHINE LANGUAGE 16
7.3 ASSEMBLY LANGUAGE 16
8. C PROGRAM 18
8.1 C PROGRAM STRUCTURE 19
8.2 C PROGRAM DIRECTIVES 20
8.3 IDENTIFIERS 20
8.4 KEYWORDS 21
8.5 STATEMENTS 22
8.6 PRE-PROCESSOR DIRECTIVES 23
8.7 C FUNCTION 25
9. COMPILERS
9.1 THE PREPROCESSOR 30
9.2 THE COMPILER 30
9.3 THE LINKER 31
10. CROSS DEVELOPMENT
10.1 CROSS COMPILER 32
10.2 CROSS DEVELOPMENT TOOLS 33
11. PROBLEMS FACED 34
12. TROUBLE SHOOTING OF PROBLEMS 35
13. SUMMARY 36

DEPT. OF ECE, AMCEC i


EMBEDDED SYSTEMS

LIST OF FIGURES
FIGURE FIGURE NAME PAGE
NO. NO.

1.1 BLOCK DIAGRAM OF EMBEDDED SYSTEMS 2


2.1 BASIC STRUCTURE OF AN EMBEDDED SYSTEM 5
4.1 A SIMPLE BLOCK DIAGRAM OF A 7
MICROPROCESSOR
4.2 VON-NEUMANN ARCHITECTURE 9
4.3 HARVARD ARCHITECTURE 10
5.1 EXAMPLE FOR SMALL SCALE EMBEDDED 13
SYSTEM
5.2 EXAMPLE FOR MEDIUM SCALE EMBEDDED 14
SYSTEM
5.3 EXAMPLE FOR SOPHISTICATED EMBEDDED 14
SYSTEM

6.1 GRAPH PROVING MOORE’S LAW 15


6.2 DEVELOPMENT OF SYSTEM-IN-CHIP 15
ARCHITECTURE
8.1 A TYPICAL MICROCONTROLLER PROGRAM 19

LIST OF TABLES
TABLE TABLE NAME PAGE
NO. NO.

4.1 COMPONENTS OF A MICROCONTROLLER 8

4.2 MICROPROCESSOR VS MICROCONTROLLER 8


4.1 VON-NEUMANN VS HARVARD ARCHITECTURE 11
4.4 CISC VS RISC 12
7.1 INSTRUCTIONS FROM THE NATIONAL 17
SEMICONDUCTOR COP888 ASSEMBLY
LANGUAGE
8.1 LIST OF RESERVED KEYWORDS IN C 21

DEPT. OF ECE, AMCEC ii


EMBEDDED SYSTEMS

CHAPTER 1
INTRODUCTION TO EMBEDDED SYSTEMS

1.1 System

A system is an arrangement in which all its unit assemble work together according to a set
of rules. It can also be defined as a way of working, organizing or doing one or many tasks
according to a fixed plan. For example, a watch is a time displaying system. Its components follow
a set of rules to show time. If one of its parts fails, the watch will stop working. So we can say, in
a system, all its subcomponents depend on each other.

1.2 Embedded System

As its name suggests, Embedded means something that is attached to another thing. An
embedded system can be thought of as a computer hardware system having software embedded in
it. An embedded system can be an independent system or it can be a part of a large system. The
block diagram of an embedded system is given in Fig.1.1. An embedded system is a
microcontroller or microprocessor based system which is designed to perform a specific task. For
example, a fire alarm is an embedded system; it will sense only smoke.

An embedded system has three components:

• It has hardware.
• It has application software.
• It has Real Time Operating system (RTOS) that supervises the application software
and provide mechanism to let the processor run a process as per scheduling by
following a plan to control the latencies. RTOS defines the way the system works. It

DEPT. OF ECE, AMCEC 1


EMBEDDED SYSTEMS

sets the rules during the execution of application program. A small scale embedded
system may not have RTOS.
So we can define an embedded system as a Microcontroller based, software driven,
reliable, real-time control system.

Fig.1.1 Block Diagram of Embedded Systems

DEPT. OF ECE, AMCEC 2


EMBEDDED SYSTEMS

CHAPTER 2
CHARACTERISTICS OF AN EMBEDDED SYSTEM, ITS
ADVANTAGES AND DISADVANTAGES

2.1 CHARACTERISTICS OF AN EMBEDDED SYSTEM

2.1.1 Single-functioned – An embedded system usually performs a specialized


operation and does the same repeatedly. For example: A pager always functions as a pager.

2.1.2 Tightly constrained – All computing systems have constraints on design metrics,
but those on an embedded system can be especially tight. Design metrics is a measure of an
implementation's features such as its cost, size, power, and performance. It must be of a
size to fit on a single chip, must perform fast enough to process data in real time and
consume minimum power to extend battery life.

2.1.3 Reactive and Real time – Many embedded systems must continually react to
changes in the system's environment and must compute certain results in real time without
any delay. Consider an example of a car cruise controller; it continually monitors and reacts
to speed and brake sensors. It must compute acceleration or de- accelerations repeatedly
within a limited time; a delayed computation can result in failure to control of the car.

2.1.4 Microprocessors based – It must be microprocessor or microcontroller based.

2.1.5 Memory – It must have a memory, as its software usually embeds in ROM. It does
not need any secondary memories in the computer.

DEPT. OF ECE, AMCEC 3


EMBEDDED SYSTEMS

2.1.6 Connected – It must have connected peripherals to connect input and output
devices.

2.1.7 HW-SW systems – Software is used for more features and flexibility. Hardware
is used for performance and security.

2.2 ADVANTAGES AND DISADVANTAGES OF AN EMBEDDED SYSTEM

2.2.1 Advantages
• Easily Customizable
• Low power consumption
• Low cost
• Enhanced performance

2.2.2 Disadvantages

• High development effort


• Larger time to market

DEPT. OF ECE, AMCEC 4


EMBEDDED SYSTEMS

CHAPTER 3
BASIC STRUCTURE OF AN EMBEDDED SYSTEM

The illustration of the basic structure of an embedded system is given in Fig.3.1.

Fig. 3.1 Basic structure of an Embedded System

3.1 Sensor – It measures the physical quantity and converts it to an electrical signal which
can be read by an observer or by any electronic instrument like an A2D converter. A sensor
stores the measured quantity to the memory.
3.2 A-D Converter – An analog-to-digital converter converts the analog signal sent by the
sensor into a digital signal.
3.3 Processor & ASICs – Processors process the data to measure the output and store it to
the memory.
3.4 D-A Converter – A digital-to-analog converter converts the digital data fed by the
processor to analog data.
3.5 Actuator – An actuator compares the output given by the D-A Converter to the actual
(expected) output stored in it and stores the approved output.

DEPT. OF ECE, AMCEC 5


EMBEDDED SYSTEMS

CHAPTER 4
PROCESSORS

Processor is the heart of an embedded system. It is the basic unit that takes inputs and
produces an output after processing the data. For an embedded system designer, it is necessary
to have the knowledge of both microprocessors and microcontrollers.

4.1 Processors in a System

A processor has two essential units:

• Program Flow Control Unit (CU)


• Execution Unit (EU)

The CU includes a fetch unit for fetching instructions from the memory. The EU has
circuits that implement the instructions pertaining to data transfer operation and data
conversion from one form to another.

The EU includes the Arithmetic and Logical Unit (ALU) and also the circuits that
execute instructions for a program control task such as interrupt, or jump to another set of
instructions.

A processor runs the cycles of fetch and executes the instructions in the same sequence
as they are fetched from memory.

4.2 Types of Processors

Processors can be of the following categories:

• General Purpose Processor (GPP)


• Microprocessor
• Microcontroller

DEPT. OF ECE, AMCEC 6


EMBEDDED SYSTEMS

• Embedded Processor
• Digital Signal Processor
• Media Processor
• Application Specific System Processor (ASSP)
• Application Specific Instruction Processors (ASIPs)
• GPP core(s) or ASIP core(s) on either an Application Specific Integrated Circuit
(ASIC) or a Very Large Scale Integration (VLSI) circuit.

4.2.1 Microprocessor

A microprocessor is a single VLSI chip having a CPU. In addition, it may also have
other units such as coaches, floating point processing arithmetic unit, and pipelining units that
help in faster processing of instructions. Fig. 4.1 shows a simple block diagram of a
Microprocessor.
Earlier generation microprocessors’ fetch-and-execute cycle was guided by a clock
frequency of order of ~1 MHz. Processors now operate at a clock frequency of 2GHz.

Fig.4.1 A simple block diagram of a Microprocessor

4.2.2 Microcontroller

A microcontroller is a single-chip VLSI unit (also called microcomputer) which,


although having limited computational capabilities, possesses enhanced input/output capability
and a number of on-chip functional units. Table 4.1 gives the components of a Microcontroller.

DEPT. OF ECE, AMCEC 7


EMBEDDED SYSTEMS

Table 4.1 Components of a Microcontroller

CPU RAM ROM

I/O Port Timer Serial COM Port

4.2.2.1 Microcontroller Chip


Microcontrollers are particularly used in embedded systems for real-time control
applications with on-chip program memory and devices.

4.3 Microprocessor vs Microcontroller


Let us now take a look at the most notable differences between a microprocessor and a
microcontroller given in Table 4.2.
Table 4.2 Microprocessor Vs Microcontroller
Microprocessor Microcontroller
Microprocessors are multitasking in nature. Single task oriented. For example, a washing
Can perform multiple tasks at a time. For machine is designed for washing clothes only.
example, on computer we can play music while
writing text in text editor.
RAM, ROM, I/O Ports, and Timers can be RAM, ROM, I/O Ports, and Timers cannot be
added externally and can vary in numbers. added externally. These components are to be
embedded together on a chip and are fixed in
numbers.
Designers can decide the number of memory or Fixed number for memory or I/O makes a
I/O ports needed. microcontroller ideal for a limited but specific task.

External support of external memory and I/O Microcontrollers are lightweight and cheaper than
ports makes a microprocessor-based system a microprocessor.
heavier and costlier.
External devices require more space and their A microcontroller-based system consumes less
power consumption is higher. power and takes less space.

DEPT. OF ECE, AMCEC 8


EMBEDDED SYSTEMS

The 8051 microcontrollers work with 8-bit data bus. So they can support external data
memory up to 64K and external program memory of 64k at best. Collectively, 8051
microcontrollers can address 128k of external memory.
When data and code lie in different memory blocks, then the architecture is referred as
Harvard architecture. In case data and code lie in the same memory block, then the architecture
is referred as Von Neumann architecture.

4.4 Von Neumann Architecture


The Von Neumann architecture was first proposed by a computer scientist John von
Neumann. In this architecture, one data path or bus exists for both instruction and data. As a
result, the CPU does one operation at a time. It either fetches an instruction from memory, or
performs read/write operation on data. So an instruction fetch and a data operation cannot occur
simultaneously, sharing a common bus. Refer Fig. 4.2.

Fig. 4.2 Von-Neumann Architecture

Von-Neumann architecture supports simple hardware. It allows the use of a single,

DEPT. OF ECE, AMCEC 9


EMBEDDED SYSTEMS

sequential memory. Today's processing speeds vastly outpace memory access times, and we
employ a very fast but small amount of memory (cache) local to the processor.

4.5 Harvard Architecture


The Harvard architecture offers separate storage and signal buses for instructions and
data. This architecture has data storage entirely contained within the CPU, and there is no
access to the instruction storage as data. Computers have separate memory areas for program
instructions and data using internal data buses, allowing simultaneous access to both
instructions and data.
Programs needed to be loaded by an operator; the processor could not boot itself. In a
Harvard architecture, there is no need to make the two memories share properties. Refer Fig.
4.3.

Fig. 4.3 Harvard Architecture

DEPT. OF ECE, AMCEC 10


EMBEDDED SYSTEMS

4.6 Von-Neumann Architecture vs Harvard Architecture

The following points distinguish the Von Neumann Architecture from the Harvard
Architecture.
Table 4.3 Von- Neumann Vs Harvard Architecture

Von-Neumann Architecture Harvard Architecture

Single memory to be shared by both code and data. Separate memories for code and data.
Processor needs to fetch code in a separate clock cycle and Single clock cycle is sufficient, as separate
data in another clock cycle. So it requires two clock buses are used to access code and data.
cycles.
Higher speed, thus less time consuming. Slower in speed, thus more time-consuming.
Simple in design. Complex in design.

4.7 CISC and RISC

CISC is a Complex Instruction Set Computer. It is a computer that can address a large
number of instructions.
In the early 1980s, computer designers recommended that computers should use fewer
instructions with simple constructs so that they can be executed much faster within the CPU
without having to use memory. Such computers are classified as Reduced Instruction Set
Computer or RISC.

4.7.1 CISC vs RISC


Table 4.4 gives the difference between CISC and RISC.

DEPT. OF ECE, AMCEC 11


EMBEDDED SYSTEMS

Table 4.4 CISC Vs RISC

CISC RISC

Larger set of instructions. Easy to program. Smaller set of Instructions. Difficult to


program.
Simpler design of compiler, considering Complex design of compiler.
larger set of instructions.
Many addressing modes causing complex instruction Few addressing modes, fix instruction
formats. format.
Instruction length is variable. Instruction length varies.
Higher clock cycles per second. Low clock cycle per second.
Emphasis is on hardware. Emphasis is on software.
Control unit implements large instruction set using Each instruction is to be executed by
micro-program unit. hardware.
Slower execution, as instructions are to be read from Faster execution, as each instruction is to
memory and decoded by the be executed by hardware.
decoder unit.
Pipelining is not possible. Pipelining of instructions is possible,
considering single clock cycle.

DEPT. OF ECE, AMCEC 12


EMBEDDED SYSTEMS

CHAPTER 5
CLASSIFICATIONS OF EMBEDDED SYSTEM
• Small Scale Embedded System
• Medium Scale Embedded System
• Sophisticated Embedded System
5.1 Small Scale Embedded Systems
These systems are designed with a single 8- or 15-bit microcontroller; they have little
hardware and software complexities and involve board level design. Example for small
scale embedded system is given in Fig. 5.1.They may even be battery operated. When
developing embedded software for these, an editor, assembler and cross assembler,
specific to the microcontroller or processor used, are the main programming tools.
Usually, ‗C‘ is used for developing these systems. ‗C‘ program compilation is done
into the assembly, and executable codes are then appropriately located in the system
memory. The software has to fit within the memory available and keep in view the need
to limit power dissipation when system is running continuously.

Figure 5.1 Example for Small Scale Embedded System

5.2 Medium Scale Embedded Systems

These systems are usually designed with a single or few 16- or 32-bit
microcontrollers or DSPs or Reduced Instruction Set Computers (RISCs). These have
both hardware and software complexities. Example for small scale embedded system is
given in Fig. 5.2. For complex software design, there are the following programming
tools: RTOS, Source code engineering tool, Simulator, Debugger and Integrated
Development Environment (IDE). Software tools also provide the solutions to the
hardware complexities. An assembler is of little use as a programming tool. These
systems may also employ the readily available ASSPs and IPs (explained later) for the

DEPT. OF ECE, AMCEC 13


EMBEDDED SYSTEMS

various functions—for example, for the bus interfacing, encrypting, deciphering,


discrete cosine transformation and inverse transformation, TCP/IP protocol stacking
and network connecting functions.

Figure 5.2 Example for Medium Scale Embedded System

5.3 Sophisticated Embedded Systems

Sophisticated embedded systems have enormous hardware and software complexities


and may need scalable processors or configurable processors and programmable logic
arrays. They are used for cutting edge applications that need hardware and software co-
design and integration in the final system; however, they are constrained by the
processing speeds available in their hardware units. Example for small scale embedded
system is given in Fig. 5.3. Certain software functions such as encryption and
deciphering algorithms, discrete cosine transformation and inverse transformation
algorithms, TCP/IP protocol stacking and network driver functions are implemented in
the hardware to obtain additional speeds by saving time. Some of the functions of the
hardware resources in the system are also implemented by the software. Development
tools for these systems may not be readily available at a reasonable cost or may not be
available at all. In some cases, a compiler or retarget able compiler might have to be
developed for these.

Figure 5.3 Example for Sophisticated Embedded System

DEPT. OF ECE, AMCEC 14


EMBEDDED SYSTEMS

CHAPTER 6
MOORE’S LAW
Moore's law describes a long-term trend in the history of computing hardware. Since
the invention of the integrated circuit in 1958, the number of transistors that can be placed
inexpensively on an integrated circuit has increased exponentially, doubling approximately
every two years. Refer Fig. 6.1 and Fig.6.2. The trend was first observed by Intel co-founder
Gordon E. Moore in 1965. Almost every measure of the capabilities of digital electronic
devices is linked to Moore's law: processing speed, memory capacity, etc.

Figure 6.1 Graph proving Moore’s Law

Figure 6.2 Development of System-In-Chip architecture

DEPT. OF ECE, AMCEC 15


EMBEDDED SYSTEMS

CHAPTER 7

PROGRAM
The most important thing to remember about computers is that they can do only what they
are instructed to do. To accomplish a meaningful task on a computer, someone must give it
exhaustive and very explicit instructions. A collection of such instructions is a called a program
and the person who writes and revises these instructions is known as a programmer or
developer.

7.1 Machine Language

Computers only understand one language: machine language. Each family of


computers has its own machine language which can not be understood by any other family of
computers. Any particular computer within a family may also use a slightly different dialect of
the family language and may incorporate features not available to other members of the family.
Any instructions for a specific computer must be given in its individual machine language.
Machine language is a collection of binary numbers such as:
00000000000001010101011110100110100010101010011010001111101
11100000010000000000011111111100011101010110000000000000010 10

The hexadecimal equivalent is: 000557A68AA68FBC0800FF8EAC000A

7.2 Assembly Language

Each microcontroller has its own assembly language or assembly language variation.
Assembly language consists of mnemonic instructions and addressing modes where the
instruction describes what to do and the addressing mode describes where to do it. Table 7.1
gives the Instructions from the National Semiconductor COP888 assembly language.

DEPT. OF ECE, AMCEC 16


EMBEDDED SYSTEMS

Table 7.1 Instructions from the National Semiconductor COP888 assembly language

8 bit microcontrollers usually use byte size instruction codes. Each instruction
has two possible components: an opcode and an operand. The opcode is the function
that the instruction performs while the operand is data used by the opcode. Neither
opcodes nor operands are restricted to 1 byte. There are several different types of
addressing modes. An addressing mode is simply the means by which an address is
determined. Some common modes are immediate data, direct address, and indirect or
indexed address.

DEPT. OF ECE, AMCEC 17


EMBEDDED SYSTEMS

CHAPTER 9

C PROGRAM
A C program can vary from 3 lines to millions of lines and it should be written into one
or more text files with extension ".c"; for example, hello.c. You can use "vi", "vim" or
any other text editor to write your C program into a file.
Traditionally, the first program a developer writes in the C language is one which
displays the message Hello World! on the computer screen. This is a sensible beginning for
traditional C platforms where conventional input and output are important and fundamental
concepts.
Let us look at a simple code that would print the words "Hello World":

#include <stdio.h>
int main()

/* my first program in C */
printf("Hello, World! \n");

return 0;

}
Let us take a look at the various parts of the above program:

The first line of the program #include <stdio.h> is a preprocessor command, which tells a
C compiler to include stdio.h file before going to actual compilation.
The next line int main() is the main function where the program execution begins.
The next line /*...*/ will be ignored by the compiler and it has been put to add additional
comments in the program. So such lines are called comments in the program.
The next line printf(...) is another function available in C which causes the message "Hello,
World!" to be displayed on the screen.
The next line return 0; terminates the main() function and returns the value 0.

DEPT. OF ECE, AMCEC 18


EMBEDDED SYSTEMS

In the world of 8 bit microcontrollers device input and output play radically different roles.
Programs rarely have access to keyboard input or screen output devices which are common in
traditional C programming.

The following introductory program shown in Fig.8.1 is representative of microcontroller


programming. The program tests to see if a button attached to a controller port has been pushed.
If the button has been pushed, the program turns on an LED attached to the port, waits, and
then turns it back off.

Figure 8.1 A typical microcontroller program

In Example shown in Figure 9.1, the compiler is informed that two ports are available.
The name PortA refers to physical port A’s data register, which is available for reading and
writing and is located at address 0x0A. The name PortADir refers to physical port A’s data
direction register, which is available for writing only and is located at address 0x8A.

The condition is the integer 1b (binary), which is interpreted as true. Therefore, once
the computer begins to execute statements inside the braces of the while loop, it will not
terminate until the computer malfunctions or is turned off. This kind of loop is often called an
infinite loop. In traditional C programming, an infinite loop is usually avoided. However, it is
often used in embedded systems programming. An embedded controller typically executes a
single program “infinitely”. Only when the controller is reset or turned off will the loop
terminate.

8.1 C Program Structure

A C program is built from three components:

• Directives are directives handled directly by the preprocessor

DEPT. OF ECE, AMCEC 19


EMBEDDED SYSTEMS

• 2. Declarations are instructions for the compiler to record the type and
associated memory locations of symbols
• 3. Statements are the executable instructions in a program.

8.2 C Preprocessor Directives

The simple C program shown in Example 6 in the previous section introduced several
preprocessor directives: #include directives include the contents of another file, #define
directives define symbolic constants, #pragma directives describe details of the target
hardware.

8.3 Identifiers

8.3.1 Declaration

Declarations define the name and type of identifiers used in your


program. One benefit of programming in a high level language is the ability to
construct generic groups of instructions, called functions, to perform tasks
whose steps are not dependent upon specific values.

For example, you can write instructions to add together two numbers
without knowing the values of the numbers. This can be done through the use
of identifiers.

An identifier can either represent a value, called a variable, or a group


of instructions, called a function. C identifiers represent addresses in computer
group of program instructions.

8.3.1.1 Identifiers in Memory

The compiler allocates memory for all identifiers. As the


compiler reads a program, it records all identifier names in a symbol
table. The compiler uses the symbol table internally as a reference to
keep track of the identifiers: their name, type and the location in memory
which they represent. When the compiler finishes translating a program
into machine language, it will have replaced all the identifier names used
in the program with instructions that refer to the memory addresses
associated with these identifiers.

DEPT. OF ECE, AMCEC 20


EMBEDDED SYSTEMS

8.3.1.2 Identifier names

An identifier name can be any word beginning with a letter or


underscore character. The rules for naming identifiers are quite straightforward.
An identifier can be almost any word that begins with a letter or underscore
character, followed by 0 or more letters, numbers or underscore characters. An
identifier cannot be a C keyword.

The C language has keywords which the compiler reserves because they
have special meaning in the language. For example, the word “ if” is used to
signify the beginning of a decision block.

8.4 Keywords

A keyword may not be used as an identifier name. Table 8.1 gives a list of the reserved words
in C.

Table 8.1 List of the reserved words in C.

auto else Long Switch

break enum Register Typedef

case extern Return Union

char float Short Unsigned

const for Signed Void

continue goto Sizeof Volatile

default if Static While

do int Struct _Packed


double

DEPT. OF ECE, AMCEC 21


EMBEDDED SYSTEMS

8.5 Statements

When a program runs it executes program statements. Declarations describe the


memory locations which statements can use to store and manipulate values. The most
frequently used statement in any programming language is the assignment statement.

C provides many different ways to construct an assignment statement; the following


example shows the simplest way:

currentTemperature = 20;

The compiler will generate an instruction to store the value 20 in the RAM memory
location set aside for the currentTemperature variable.

8.5.1 The Semicolon Statement Terminator

All statements in C must end with a semicolon. C uses the semicolon as a


statement terminator. One of the most common errors in C programming is an extra,
missing or misplaced semicolon. If you leave out a semicolon the C compiler will not
know where a statement should end.

For example, consider the following two statements.

currentTemperature = 20

currentTemperature = 25;

The compiler would produce an error. Forgetting the semicolon at the end of
the first line forces the compiler to read both lines as one statement instead of two.
According to the compiler the written instruction is:

currentTemperature = 20 currentTemperature = 25;

The C compiler does not care about white space between tokens as it reads
through the program. White space includes space, tab and end-of-line characters. On
some computers the end of line will be a single linefeed character, while on others it
will be a linefeed and carriage return together. C compilers ignore both carriage returns
and linefeeds.

DEPT. OF ECE, AMCEC 22


EMBEDDED SYSTEMS

8.5.2 Program Comments

A good programmer includes comments throughout a program. Comments help


to explain what the code is doing at a particular point and often state what specific
symbols or operations represent.

C compilers use slash and asterisk combinations as comment delimiters. When


the compiler encounters a slash immediately followed by an asterisk , /*, it treats every
character following this pair as a comment until an asterisk immediately followed by a
slash, */, is encountered. Most modern C compilers also accept C++ comment syntax.
If the compiler reaches a slash immediately followed by another slash, //, in the source
code it treats the rest of that line as a comment. The C++ convention is more readable
and easier to debug because the effect of the comment syntax does not carry over from
one line to the next as in traditional C. Comments drastically improve code readability.

8.6 Preprocessor directives

Preprocessor directives are specific instructions given to the preprocessor. Preprocessor


directives are always preceded by the # character which is referred to as a hash mark.

These directives are used as follows:

8.6.1 #include

#include is one of the most commonly used preprocessor directives. When the
preprocessor reaches this directive it looks for the file named in the brackets. In the
example above the preprocessor searches for the file hc705c8.h which contains device
specific specifications for the Motorola 68HC705C8. If the file is found the
preprocessor will replace the #include directive with the entire contents of the file. If
the file is not found the preprocessor will halt and give an error. In the example the
#include directive is used to include the contents of a header file. By convention, C
language header files have the .h extension. Header files contain information which is
used by several different sections, or modules, of a C program as they contain
preprocessor directives and predefined values which help to describe the resources and
capabilities of a specific target microcontroller. #define ON 1 #define OFF 0 #define is
another commonly used preprocessor directive which is used to define symbolic
constants. Programs often use a constant number or value many times. Instead of typing

DEPT. OF ECE, AMCEC 23


EMBEDDED SYSTEMS

in the actual number or value throughout the program, one can define a symbol which
represents the value. When the preprocessor reaches a #define directive, it will replace
all the occurrences of the symbol name in your program with the associated constant.

Constants are useful for two specific reasons:

• Increasing program readability: A symbolic name is more descriptive


than a number. For instance, the name ON is easier to understand than the
value 1. Using symbolic constants enhances the readability of your
programs and makes them easier to test, debug and modify.
• Increasing program modifiability: Since the symbolic constant value is
defined in a single place, only one change is necessary if you wish to modify
the value: in the #define statement. Without the #define statement it would
be necessary to search through the entire program for every place the value
is used and change each one individually. In the statements #define ON 1
and #define OFF 0, the symbols ON and OFF are assigned the values 1 and
0 respectively. Everywhere the preprocessor sees the symbol ON it will
replace it with the constant 1; where it sees OFF it will replace it with the
constant 0.

8.6.2 #pragma

#pragma portrw PortA @ 0x0A;

#pragma portw PortADir @ 0x8A;

The preprocessor handles #pragma directives in a slightly different fashion than


other preprocessor directives. #pragma directives instruct the compiler to behave in a
certain way based on the description of the hardware resources of the target computer.
#pragma statements are most often used in header files which provide the hardware
specifications for a particular device. #pragma port directives, for example, describe
the ports available on the target computer. The description includes details on port
location, whether they are read, write or read/write and the names the program uses to
access ports.

DEPT. OF ECE, AMCEC 24


EMBEDDED SYSTEMS

8.7 C Functions

C programs are built from functions. Functions typically accept data as input,
manipulate data and return the resulting value. For example, we can write a C function that
would take two numbers, add them together and return the sum as the result.

8.7.1 The main( ) function

All C programs must have one function called main()which is always the first
function executed. A function name is specified by following the name with
parentheses. This is the notation used by the C compiler to determine when it has
encountered a function. As long as the name is not a recognised C command, called a
reserved word, the compiler will assume it is a function if it is immediately followed
by a pair of parentheses. The parentheses may also surround a list of input arguments
for the function.

Syntax for the main( ) function:

void main(void)

{ //function statements

The first use of the term void, prior to the word main, indicates to the compiler
that this main() function does not return any value at all. The second use of the term
void, between the parentheses, indicates to the compiler that this main() function is not
sent any data in the form of arguments. Braces must surround all statements which form
the body of a function. Even functions with no statements in their body require the
braces – the braces after a function header indicate to the compiler that you are
providing the definition of a function.

8.7.2 Calling a Function

The main() function can execute code from other functions. This is referred to
as calling another function. The calling function must know about the called function
in order to execute its code.

A function knows about another function in two ways:

DEPT. OF ECE, AMCEC 25


EMBEDDED SYSTEMS

• The entire definition of the called function is positioned earlier in the


source file than the calling function.
• A function prototype of the called function is included before the calling
function in the same source file. A function prototype describes details
of the requirements of a function so that any program code that calls that
function will know what information the called function requires.

The following is a typical function prototype:

void wait(registera);

The example above is a function prototype for a function called wait(). This
function is preceded by the return value void; therefore, it does not return a value.
Unlike main(), the wait() function does expect to receive an argument, called a
parameter. The type of the parameter (registera) is type registera.

8.7.3 The Function Body

Every function definition has a function header. A function header describes


what type of value the function returns, the name of the function, and what input
arguments it expects. The body of the function follows the function header. The
function body contains a set of statements between braces which are executed when the
function is called. There are several different types of C statements.

8.7.3.1 The Assignment Statement

One of the simplest and most common statements in C is the assignment


statement. An assignment statement takes the value of the expression on the
right of the equal sign and assigns it to the symbol on the left side of the equal
sign.

For example: PortADir.0 = OUTPUT;

PortADir.1 = INPUT;

For Example, Using the C assignment statement In Example 8 the


symbols PortADir.0 and PortADir.1 refer to the first two bits of the port
associated with the name PortADir. The first statement assigns the numeric
value of the expression on the right of the equal sign to bit 0 of PortADir, which

DEPT. OF ECE, AMCEC 26


EMBEDDED SYSTEMS

represents the port A direction register. From the #define directives we know
that OUTPUT is really a symbolic constant associated with the value 0.
Therefore, this assignment statement clears bit 0 of the port A direction register.
By contrast, the second assignment statement sets bit 1 of the port A direction
register as INPUT is a symbolic constant associated with the value 1 in a #define
statement.

8.7.3.2 Control statements

Control statements allow decisions to be made to determine which


statements are executed and how often. In C decisions are made using control
statements. Control statements can select between two or more paths of
execution and repeat a set of statements a given number of times. Some common
control statements are:

8.7.3.2.1 The while statement

while(1)

// statements

The while() control statement instructs the computer to repeat a set of


instructions (loop) as long as a condition is valid. The condition is an expression placed
in the brackets which follow the while statement. C considers any condition which does
not evaluate to 0 to be true and any condition which does evaluate to 0 to be false.

8.7.3.2.2 The if statement

The if() statement provides the ability to make decisions. If the if statement
condition is true then the computer executes the statements in the if body. In Example
9, the value of PortA.1 is compared with the value of PUSHED, if data bit number 1 is
set (has the value 1) then the program will execute any statements in the if body. The
body statement sets port A data bit number 0 by assigning it the value of ON.

while (1)

DEPT. OF ECE, AMCEC 27


EMBEDDED SYSTEMS

if (PortA.1 == PUSHED)

PortA.0 = ON;

Example 10: Nesting if and while statements

When the if decision is placed inside a while loop, the program will test bit 1 in
PortA regularly. Assume a button is attached to pin 1 of port A and an LED to pin 0 of
PortA. We have written a small control program which will continually test the button
attached to pin 1. When the button is pushed, bit 1 of PortA will be set. When bit 1 is
set and the if statement is executed, bit 0 is set. The LED attached to PortA pin 0 will
be set to 1 and will light up.

8.7.3.3 Calling Functions

A program can delegate a task by calling another function. Once the program
turns on the LED in Example 10 it never turns it off. Remember, the while loop is an
infinite loop. How can we solve this problem? One solution is to write a function called
wait()which creates a delay and then turn the LED off.

Consider the following example code fragment:

while (1)

if (PortA.1 = PUSHED)

{ PortA.0 = ON;

wait(10); \\ wait ten seconds

PortA.0 = OFF;

}}

DEPT. OF ECE, AMCEC 28


EMBEDDED SYSTEMS

When the wait() function is used and the button is pushed, the program turns
the LED on by setting bit 0 of PortA. The wait() function causes a delay of ten seconds.
After the wait function has finished and ten seconds have passed, the program turns off
the LED by clearing bit 0 of PortA.

DEPT. OF ECE, AMCEC 29


EMBEDDED SYSTEMS

CHAPTER 9

COMPILERS
Compilers translate high level programming language instructions into machine
language. They perform the same task for high level languages that an assembler performs for
assembly language, translating program instructions from a language such as C to an equivalent
set of instructions in machine language.

This translation does not happen in a single step – three different components are
responsible for changing C instructions into their machine language equivalents. These three
components are:

• Preprocessor
• Compiler
• Linker

9.1 The Preprocessor

A program first passes through the C preprocessor. The preprocessor goes


through a program and prepares it to be read by the compiler. The preprocessor includes
the contents of other programmer specified files, manipulates the program text, and
passes on instructions about the particular computer for which the compiler will be
translating.

9.2 The Compiler

The compiler translates a program into an intermediate form containing both


machine code and information about the program’s contents. The compiler is the second
component to handle a program. The compiler has the most important job: digesting
and translating the program into a language readable by the destination computer. Many
compilers operate in different passes through the code. There are often passes
specifically to handle optimizations of code which will reduce the size of the machine
code generated.

DEPT. OF ECE, AMCEC 30


EMBEDDED SYSTEMS

10.3 The Linker

When programs were written in the past often the development computer was
not powerful enough to hold the entire program being developed in memory at one
time. Historically, programs had to be divided into separate modules where each
module would be compiled into object code and a linker would link the and the use of
a linker is no longer absolutely necessary. Many implementations of C provide function
libraries which have been precompiled for a particular computer. These functions serve
common program needs such as serial port support, input/output, and description of the
destination computer. Functions within libraries are usually either linked with modules
which use them or included directly by the compiler if the compiler supports library
function inclusion. When a program has been pre-processed, compiled and linked, the
destination computer will be able to read and execute your program.

DEPT. OF ECE, AMCEC 31


EMBEDDED SYSTEMS

CHAPTER 11

CROSS DEVELOPMENT
A cross compiler runs on one type of computer and produces machine code for a
different type of computer. While many 8 bit embedded microcontrollers can support
sophisticated and extremely useful programs, they are not powerful enough to support the
resource needs of a C development environment. A developer creates and compiles programs
for an 8 bit microcontroller using a cross compiler.

10.1 Cross compiler

An embedded systems developer writes and compiles programs on a larger


computer which can support a C development environment. The compiler used does
not translate to the machine language of the development computer, it produces a
version of the program in the machine language of the 8 bit microcontroller. A compiler
that runs on one type of computer and provides a translation for a different type of
computer is called a cross-platform compiler or cross-compiler. The object code
formats generated by a cross-compiler are based on the target device. For example, a
compiler for the Motorola MC68HC705C8 could generate an S-record file for its object
code.

10.2 Cross development tools

After a program is compiled it must be tested using a simulator or an emulator.


After testing the developer uses a special machine called a ler.

10.2.1 Simulator

A simulator is a software program which allows a developer to run a program


designed for one type of machine (the target machine) on another (the development
machine). The simulator simulates the running conditions of the target machine on the
development machine. Using a simulator one can step through your code while the
program is running. One can change parts of the code in order to experiment with
different solutions to a programming problem. Simulators do not support real interrupts
or devices. An in-circuit simulator includes a hardware device which can be connected
to your development system to behave like the target microcontroller. The in-circuit

DEPT. OF ECE, AMCEC 32


EMBEDDED SYSTEMS

simulator has all the functionality of the software simulator while also supporting the
emulation of the microcontroller’s I/O capabilities.

10.2.2 Emulator

An emulator or in-circuit emulator is a hardware device which behaves


like a target machine. It is often called a real time tool because it can react to
events as the target microcontroller would. Emulators are often packaged with
monitor programs which allow developers to examine registers and memory
locations and set breakpoints.

DEPT. OF ECE, AMCEC 33


EMBEDDED SYSTEMS

CHAPTER 11

PROBLEMS FACED

1. It was the first experience of job and thus, the main problem that I faced was
impunctuality. In a professional workspace, impunctuality isn’t acceptable and hence
managing time was a little difficult for me initially.

2. Communication is one of those internship challenges that could actually hinder my


internship time. Though I landed up at a really good internship but upon starting I found
it difficult to ask questions for the fear of being judged.

DEPT. OF ECE, AMCEC 34


EMBEDDED SYSTEMS

CHAPTER 12

TROUBLESHOOTING OF PROBLEMS
1. Self-management wasn’t something that I could learn overnight. I achieved it only
through habit forming and by adopting a more organized living and started going to my
Internship on time.

2. Keen on learning the new topics, and getting to know about the intern-friendly
environment there, I started asking my queries without the worry of being judged. The
appreciation that I got on clarifying my doubts also motivated me in asking further
queries and getting them clarified.

DEPT. OF ECE, AMCEC 35


EMBEDDED SYSTEMS

CHAPTER 13

SUMMARY
The Internship on Embedded Systems included the following –

• Introduction to Embedded Systems, it characteristics and structure


• Difference between Microprocessors and Microcontrollers, Von Neumann and
Harvard Architectures, RISC and CISC
• Classification of Embedded Systems
• Moore’s Law
• C Programming and Compilers.

DEPT. OF ECE, AMCEC 36

S-ar putea să vă placă și