0% found this document useful (0 votes)
136 views23 pages

2.1 Microcontroller: Embedded Systems

The document discusses the 8051 microcontroller family. It describes the original 8051 microcontroller, which had 128 bytes of RAM, 4K bytes of ROM, two timers, one serial port, and four 8-bit I/O ports. It provides a block diagram of the 8051 and describes its ports, oscillators, and programming. The 8051 is programmed using embedded C to perform specific tasks by downloading the hex file produced by the compiler into the microcontroller's ROM.

Uploaded by

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

2.1 Microcontroller: Embedded Systems

The document discusses the 8051 microcontroller family. It describes the original 8051 microcontroller, which had 128 bytes of RAM, 4K bytes of ROM, two timers, one serial port, and four 8-bit I/O ports. It provides a block diagram of the 8051 and describes its ports, oscillators, and programming. The 8051 is programmed using embedded C to perform specific tasks by downloading the hex file produced by the compiler into the microcontroller's ROM.

Uploaded by

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

EMBEDDED SYSTEMS.IND.

TRG

CHAPTER 2
EMBEDDED SYSTEMS
2.1 MICROCONTROLLER
 GENERAL
In this chapter, the 8051 family, 8051 assembly language programming,
loop and I/O port programming, 8051 addressing modes, arithmetic instructions,
8051 hardware connection and Intel hex file have been discussed.
 THE 8051 FAMILY:
In 1981, Intel Corporation introduced an 8-bit microcontroller called the
8051. This microcontroller had 128 bytes of RAM, 4K bytes of on-chip ROM, two
timers, one serial port, and four ports (each 8-bits wide) all on a single chip. The
8051 is an 8-bit processor, meaning that the CPU can work on only 8 bits of data at
a time. Data larger than 8 bits has to broken into 8-bit pieces to be processed by the
CPU. The 8051 has a total of four I/O ports, each 8 bits wide. Although the 8051
can have a maximum of 64K bytes of on-chip ROM, many manufacturers have put
only 4K bytes on the chip. There are different flavors of the 8051 in terms of speed
and amount of on-chip ROM, but they are all compatible with the original 8051 as
far as the instructions are concerned. The various members of the 8051 family are
8051 microcontroller, 8052 microcontroller and 8031 microcontroller.

A.A.N.M&V.V.R.S.R.POLYTECHNIC,DEPARTMENT OF ECE

29
EMBEDDED SYSTEMS.IND.TRG
 8051 Block Diagram:

30
Figure 1:Block diagram of inside the microcontroller 8051

A.A.N.M&V.V.R.S.R.POLYTECHNIC,DEPARTMENT OF ECE

EMBEDDED SYSTEMS.IND.TRG

 8051 Microcontroller
31
The 8051 is the original member of the 8051 family. Figure 2.1 shows the
block diagram of the 8051 microcontroller. The AT89C51 is a low-power, high-
performance CMOS 8-bit microcomputer with 4K bytes of Flash programmable
and erasable read only memory (PEROM). The device is manufactured using
Atmel’s high-density nonvolatile memory technology and is compatible with the
industry-standard MCS-51 instruction set and pin out. The on-chip Flash the
program memory to be reprogrammed in-system or by a conventional nonvolatile
memory programmer. By combining a versatile 8-bit CPU with Flash on a
monolithic chip, the Atmel AT89C51 is a powerful microcomputer which provides
a highly-flexible and cost-effective solution to many embedded control
applications. The AT89C51 provides the following standard features: 4Kbytes of
Flash, 128 bytes of RAM, 32 I/O lines, two 16-bittimer/counters, five vector two-
level interrupt architecture, a full duplex serial port, and on-chip oscillator and
clock circuitry. In addition, the AT89C51 is designed with static logic for
operation down to zero frequency and supports two software selectable power
saving modes. The Idle Mode stops the CPU while allowing the RAM,
timer/counters, serial port and interrupt system to continue functioning. The
Power-down Mode saves the RAM contents but freezes the oscillator disabling all
other chip functions until the next hardware reset.
Pin Description of 8051:
VCC
Supply voltage.
GND
Ground.

A.A.N.M&V.V.R.S.R.POLYTECHNIC,DEPARTMENT OF ECE

32
EMBEDDED SYSTEMS.IND.TRG

A.A.N.M&V.V.R.S.R.POLYTECHNIC,DEPARTMENT OF ECE

32
EMBEDDED SYSTEMS.IND.TRG

Port 0
Port 0 is an 8-bit open-drain bi-directional I/O port. As an output port, each pin
can sink eight TTL inputs. When 1s are written to port 0 pins, the pins can be used as
high-impedance inputs. Port 0 may also be configured to be the multiplexed low- order
address/data bus during accesses to external program and data memory. In this mode P0
has internal pull-ups. Port 0 also receives the code bytes during Flash programming, and
outputs the code bytes during program verification. External pull-ups are required during
program verification.

Port 1
Port 1 is an 8-bit bi-directional I/O port with internal pull-ups. The Port 1 output
buffers can sink/source four TTL inputs. When 1s are written to Port 1 pins they are
pulled high by the internal pull-ups and can be used as inputs. As inputs, Port 1 pins that
are externally being pulled low will source current (IIL) because of the internal pull-ups.
Port 1 also receives the low-order address bytes during Flash programming and
verification.
Port 2
Port 2 is an 8-bit bi-directional I/O port with internal pull-ups. The Port 2 output
buffers can sink/source four TTL inputs. When 1s are written to Port 2 pins they are
pulled high by the internal pull-ups and can be used as inputs. As inputs, Port 2 pins that
are externally being pulled low will source current (IIL) because of the internal pull-ups.
Port 2 emits the high-order address byte during fetches from external program memory
and during accesses to external data memory that uses 16-bit addresses (MOVX
@DPTR). In this application, it uses strong internal pull-ups when emitting 1s. During
accesses to external data memory that uses 8-bit addresses (MOVX @ RI), Port 2 emits
the contents of the P2 Special Function Register. Port 2 also receives the high-order
address bits and some control signals during Flash programming and verification.

A.A.N.M&V.V.R.S.R.POLYTECHNIC,DEPARTMENT OF ECE

33
EMBEDDED SYSTEMS.IND.TRG

Port 3
Port 3 is an 8-bit bi-directional I/O port with internal pull-ups. The Port 3 output
buffer scan sink/source four TTL inputs. When 1s are written to Port 3 pins they are
pulled high by the internal pull-ups and can be used as inputs. As inputs, Port 3 pins that
are externally being pulled low will source current (IIL) because of the pull-ups. Port 3
also serves the functions of various special features of the AT89C51 as listed below:

Table 1 function of port 3


ALE/PROG
Address Latch Enable output pulse for latching the low byte of the address during
accesses to external memory. This pin is also the program pulse input (PROG) during
Flash programming. In normal operation ALE is emitted at a constant rate of 1/6 the
oscillator frequency, and may be used for external timing or clocking purposes. Note,
however, that one ALE pulse is skipped during each access to external Data Memory. If
desired, ALE operation can be disabled by setting bit 0 of SFR location 8EH. With the bit
set, ALE is active only during a MOVX or MOVC instruction. Otherwise, the pin is
weakly pulled high. Setting the ALE-disable bit has no effect if the microcontroller is in
external execution mode.
,

A.A.N.M&V.V.R.S.R.POLYTECHNIC,DEPARTMENT OF ECE

34
EMBEDDED SYSTEMS.IND.TRG

PSEN

Program Store Enable is the read strobe to external program memory. When the
AT89C51 is executing code from external program memory, PSEN is activated twice
each machine cycle,except that two PSEN activations are skipped during each access to
external data memory.

EA/VPP

External Access Enable. EA must be strapped to GND in order to enable the


device to fetch code from external program memory locations starting at 0000H up to
FFFFH. Note, however, that if lock bit 1 is programmed, EA will be internally latched on
reset. EA should be strapped to VCC for internal program executions. This pin also
receives the 12-volt programming enable voltage (VPP) during Flash programming, for
parts that require 12-volt VPP.

XTAL1

Input to the inverting oscillator amplifier and input to the internal clock operating
circuit. Port 3 also receives some control signals for Flash programming and verification.

RST

Reset input. A high on this pin for two machine cycles while the oscillator is running
resets the device.

XTAL2
Output from the inverting oscillator amplifier. Oscillator Characteristics XTAL1
and XTAL2 are the input and output, respectively, of an inverting amplifier which can be
configured for use as an on-chip oscillator, as shown in Figure 1. Either a quartz crystal or
ceramic resonator may be used. To drive the device from an external clock source,
XTAL2 should be left unconnected while XTAL1 is driven as shown.

A.A.N.M&V.V.R.S.R.POLYTECHNIC,DEPARTMENT OF ECE

35
EMBEDDED SYSTEMS.IND.TRG

Figure 3: Crystal Oscillator Connections

There are no requirements on the duty cycle of the external clock signal, since the
input to the internal clocking circuitry is through a divide-by-two flip-flop, but minimum
and maximum voltage high and low time specifications must be observed.
 Programming of Microcontroller 8051
We are using embedded C programming language to program the central unit i.e.
microcontroller 8051, so that it performs the specific task according to the requirement.
Need of C:
Compiler produces hex file that we download into ROM of microcontroller. The size of
hex file produced by compiler is one of the main concerns of microcontroller
programmers for two reasons:
1. Microcontroller has limited on -chip ROM
2. The code space for 8051 is limited to 64 KB
Programming in assembly language is tedious and time consuming. C is a high level
programming language that is portable across many hardware architectures.

A.A.N.M&V.V.R.S.R.POLYTECHNIC,DEPARTMENT OF ECE

36
EMBEDDED SYSTEMS.IND.TRG

So for following reasons we use C:


1. It is easier and less time consuming to write in C than assembly.
2. C is easier to modify and update.
3. You can use code available in function libraries.
4. C code is portable to other microcontrollers with little or no modification.
We use reg51.h as a header file as “#include <reg51.h>”. These files contain all the
definitions of the 80C51 registers. This file is included in your project and will be
assembled together with the compiled output of your C program.
C data types for 8051:
1. Unsigned char is 8-bit data type ranging 0-255 (0-FFH)
2. Signed char is 8-bit data type that uses most significant bit to represent the – or +
value. We have only 7-bits for the magnitude of the signed numbers giving us values
from -128 to +127.
1. Unsigned int is 16-bit data type ranging 0-65535(0-FFFFH).
2. Signed int is 16-bit data type that uses most significant bit to represent the – or +
value. We have only 15-bits for the magnitude of the signed numbers giving us values
from -32768 to +32767.
Sbit is a keyword designed to access single bit addressable registers. It allows to the
single bits of the SFR registers. We can use sbit to access the individual bits of the ports
as “Sbit mybit=P1^0”. This controls the D0 of port P1. Bit data type allows access to the
single bits of bit - addressable memory spaces 20-2FH. Sfr, the bit data type is used for
the bit addressable section of RAM space 20-2FH. Bitwise operators are AND (&), OR
(|), EX-OR (^), Inverter (~), Shift Right (>>) and Shift left (<<).

TIME DELAY PROGRAMS:


The basic principle of delay generation by software means is to load some
appropriate value in a register or register pair. Then a program loop is setup to
decrement the register or register pair in each pass through the loop. The looping
continues until the contents of the register or register pair are zero. The delay
obtained by this process depends on the initial value of the register and the time
needed for executing the instructions within the loop.

A.A.N.M&V.V.R.S.R.POLYTECHNIC,DEPARTMENT OF ECE
37
EMBEDDED SYSTEMS.IND.TRG
To obtain the delay between the track of the events, this process needs two
important software elements Le, counter and time delays.
(a) Counter:
It is defined to provide the track of events. It is designed by loading an
appropriate number into one of the register and using INR or DCR instructions.
To update the count and to check the final count, a loop has to be established. This
is shown in Fig.
In this, the counting is performed at high speed, hence only the last count can be
observed.
To observe each track of the counting, there must be a time delay between the
counts.
(b) Time - delay:
It is a specific procedure defined by a loaded register with an appropriate number
depending upon the time - delay required. Then the register is decremented until it
reaches zero. It is shown in the Fig. Note that the loop causes the delays,
depending upon the c1ockperiod of the system and time required to execute the
instructions in the loop.
2.2 Introduction to Micro vision Keil (IDE)
 INTRODUCTION
Keil is a cross compiler. So first we have to understand the concept of compilers and
cross compilers. After then we shall learn how to work with keil.
Concept of compiler: -
Compilers are programs used to convert a High Level Language to object code.
Desktop compilers produce an output object code for the underlying microprocessor, but
not for other microprocessors. I.E the programs written in one of the HLL like ‘C’ will
compile the code to run on the system for a particular processor like x86 (underlying
microprocessor in the computer). For example compilers for Dos platform is different
from the Compilers for Unix platform

A.A.N.M&V.V.R.S.R.POLYTECHNIC,DEPARTMENT OF ECE

38
EMBEDDED SYSTEMS.IND.TRG

So if one wants to define a compiler then compiler is a program that translates


source code into object code. The compiler derives its name from the way it works,
looking at the entire piece of source code and collecting and reorganizing the
instruction. See there is a bit little difference between compiler and an interpreter.
Interpreter just interprets whole program at a time while compiler analyzes and
execute each line of source code in succession, without looking at the entire program.
The advantage of interpreters is that they can execute a program immediately.
Secondly programs produced by compilers run much faster than the same programs
executed by an interpreter. However compilers require some time before an executable
program emerges. Now as compilers translate source code into object code, which is
unique for each type of computer, many compilers are available for the same
language.
Concept of cross compiler: -
A cross compiler is similar to the compilers but we write a program for the target
processor (like 8051 and its derivatives) on the host processors (like computer of x86)
It means being in one environment you are writing a code for another
environment is called cross development. And the compiler used for cross development is
called cross compiler
So the definition of cross compiler is a compiler that runs on one computer but
produces object code for a different type of computer. Cross compilers are used to
generate software that can run on computers with a new architecture or on special-
purpose devices that cannot host their own compilers. Cross compilers are very popular
for embedded development, where the target probably couldn't run a compiler. Typically
an embedded platform has restricted RAM, no hard disk, and limited I/O capability. Code
can be edited and compiled on a fast host machine (such as a PC or Unix workstation)
and the resulting executable code can then be downloaded to the target to be tested. Cross
compilers are beneficial whenever the host machine has more resources (memory, disk,
I/O etc) than the target. Keil C Compiler is one such compiler that supports a huge
number of host and target combinations. It supports as a target to 8 bit microcontrollers
like Atmel and Motorola etc.
Why do we need cross compiler?
There are several advantages of using cross compiler. Some of them are
described as follows

A.A.N.M&V.V.R.S.R.POLYTECHNIC,DEPARTMENT OF ECE

39
EMBEDDED SYSTEMS.IND.TRG

 By using this compilers not only can development of complex embedded systems be
completed in a fraction of the time, but reliability is improved, and maintenance is easy.
• Knowledge of the processor instruction set is not required.
• A rudimentary knowledge of the 8051’s memory architecture is desirable but not
necessary.
• Register allocation and addressing mode details are managed by the compiler.
• The ability to combine variable selection with specific operations improves program
readability.
• Keywords and operational functions that more nearly resemble the human thought
process can be used.
• Program development and debugging times are dramatically reduced when compared to
assembly language programming.
• The library files that are supplied provide many standard routines (such as formatted
output, data conversions, and floating-point arithmetic) that may be incorporated into
your application.
• Existing routine can be reused in new programs by utilizing the modular programming
techniques available with C.
• The C language is very portable and very popular. C compilers are available for almost
all target systems. Existing software investments can be quickly and easily converted
from or adapted to other processors or environments.

Now after going through the concept of compiler and cross compilers lets we start
with Keil C cross compiler.

 Keil C cross compiler: -


Keil is a German based Software development company. It provides several
development tools like
• IDE (Integrated Development environment)
• Project Manager
• Simulator
• Debug
• C Cross Compiler ,

A.A.N.M&V.V.R.S.R.POLYTECHNIC,DEPARTMENT OF ECE
40
EMBEDDED SYSTEMS.IND.TRG

Cross Assembler, Locator/Linker Keil Software provides you with software development
tools for the 8051 family of microcontrollers. With these tools, you can generate
embedded applications for the multitude of 8051 derivatives. Keil provides following
tools for 8051 development
1. C51 Optimizing C Cross Compiler,
2. A51 Macro Assembler,
3. 8051 Utilities (linker, object file converter, library manager),
4. Source-Level Debugger/Simulator,
5. µVision for Windows Integrated Development Environment.
The keil 8051 tool kit includes three main tools, assembler, compiler and linker.
An assembler is used to assemble your 8051 assembly program
A compiler is used to compile your C source code into an object file
A linker is used to create an absolute object module suitable for your in-circuit emulator.

8051 project development cycle: - these are the steps to develop 8051 project using keil
1. Create source files in C or assembly.
2. Compile or assemble source files.
3. Correct errors in source files.
4. Link object files from compiler and assembler..

 Now let us start how to work with keil.


 Working with keil: -
To open keil software click on start menu then program and then select keil2 (or
any other version keil3 etc. here the discussion is on keil2 only). Following window will
appear on your screen

A.A.N.M&V.V.R.S.R.POLYTECHNIC,DEPARTMENT OF ECE

41
EMBEDDED SYSTEMS.IND.TRG

you can see three different windows in this screen.


1) project work space window
2) editing window
3) output window.
Project workspace window is for showing all the related files connected with your
project.
Editing window is the place where you will edit the code
Output window will show the output when you compile or build or run your project
 Now to start with new project follow the steps
 click on project menu and select new project
 you will be asked to create new project in specific directory
just move to your desired directory and there create a new folder for your project named
"first". Here I am creating new project in d:\keil2\myprojects\first as shown in figure

A.A.N.M&V.V.R.S.R.POLYTECHNIC,DEPARTMENT OF ECE

42
EMBEDDED SYSTEMS.IND.TRG
 give the name of project as "test". By default it will be saved as *.v2 extension.

 now you will be asked to chose your target device for which you want to write the
program.
 scroll down the cursor and select generic from list. expand the list and select 8051 (all
variants)
 when you click OK, you will be asked to add startup code and file to your project folder.

A.A.N.M&V.V.R.S.R.POLYTECHNIC,DEPARTMENT OF ECE

43
EMBEDDEDSYSTEMS.IND.TRG

Now click on file menu and select new file. editor window will open. Now you can start
writing your code.
 As you start writing program in C, same way here also you have to first include the
header file. Because our target is 8051 our header file will be "reg51.h"
 After including this file. just right click on the file and select open document <reg51.h>.
The following window will appear

44
A.A.N.M&V.V.R.S.R.POLYTECHNIC,DEPARTMENT OF ECE

EMBEDDED SYSTEMS.IND.TRG
 If you scroll down cursor you will see that all the SFRs like P0-P3, TCON, TMOD,
ACC, bit registers and byte registers are already defined in this header file. so one can
directly use these register names in coding
 now you can write your program same as c language starting with void main()
 After completing the code save the file in project folder with ".c" extension.
 Now right click on "source group 1" in project workspace window. select "add files to
source group 1"
 select the C file you have created and click add button

45
 you will see that the c file has been added in source group
 Now to compile the program from project menu select "build target". In the output
window you will see the progress
 If there is any compilation error then target will not be created. Remove all the errors
and again build the target till you find "0 Error(s)"
 Now you are ready to run your program. from debug menu select "start/stop debug
session"
 You will see your project workspace window now shows most of the SFRs as well as
GPRs r0-r7. Also one more window is now opened named "watches". in this window
you can see different variable values.

A.A.N.M&V.V.R.S.R.POLYTECHNIC,DEPARTMENT OF ECE

46
EMBEDDED SYSTEMS.IND.TRG

 to add variable in watch window go to "watch#1" tab. then type F2 to edit and type the
name of your variable
 If you want to see the output on ports go to peripheral menu and select I/O ports. Select
the desire port. you can give input to port pins by checking or unchecking any check
box. here the check mark means digit 1 and no check mark means 0. the output on the
pin will be shown in same manner
 to run the program you can use any of the option provided "go", "step by step", "step
forward", "step ove" etc.
 now after testing your program you need to down load this program on your target
board that is 8051. for this you have to create hax file
 to create hex file first stop debug session. Again you will be diverted to project
workspace window.
 right click on "target 1" and select "option for target 1". Following window will appear

A.A.N.M&V.V.R.S.R.POLYTECHNIC,DEPARTMENT OF ECE

.
47
EMBEDDEDSYSTEMS.IND.TRG

 select output tag and check "create hex file" box


 now when you again build your program you will see the message in output window
"hex file is created".
 in your project folder you can see the hex file with same name of your project as
"test.hex".
 this file you can directly load in 8051 target board and run the application on actual
environment.
So here I have described the procedure to create a project in keil for 8051 micro
controller. To see some sample programs for 8051 in keil just go through the link "sample
programs in keil" so that you can get the idea how to write a program for 8051 in keil C.
2.3 BASICS OF EMBEDDED SYSTEMS:
 An embedded system has a computer hardware with software embedded in it has
one of its important components.
 Embedded systems are used generally to perform a single function.

 They are tightly constrained i.e., they work rigid conditions.

 Embedded systems generally require small amounts of memory.


A.A.N.M&V.V.R.S.R.POLYTECHNIC,DEPARTMENT OF ECE

48
EMBEDDEDSYSTEMS.IND.TRG

Applications:
1. Consumer electronic products i.e., is in remote controls, Washing machines etc.
2. In the Automobile field, they are used in fuel injection systems, automatic breaking
system
3. Defence applications: They are used in smart weapons, guided missiles, global
positioning ,TY systems.
4. Medical field: In Cardioc monitors, Pacemakers.

REAL TIME OPERATING SYSTEMS:

 In which the interrupt routines will take care of the most urgent operations.

 The signaling between the interrupt routine is handled by the RTOS

 RTOS can activate task code subroutines according to their priorities.

 RTOS can suspend one task code subroutine in middle and begin the processing
another task code.
 Each task in an RTOS is always in one of the three states known as Task states.
Ready: This task is ready -to go to the processor for processing
Running: The processor is currently is executing this task.
Blocked: At present this task is inactive even though the processor is free
Mail Box:
These are like quies which can be read by RTOS either to create the function or to
check whether mail box contains any messages.
Pipes:
These are also like quies in which RTOS can write or read and also RTOS create
the pipes.

A.A.N.M&V.V.R.S.R POLYTECHNIC,DEPARTMENT OF ECE

49
50

You might also like