2.1 Microcontroller: Embedded Systems
2.1 Microcontroller: Embedded Systems
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:
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
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
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
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
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.
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..
A.A.N.M&V.V.R.S.R.POLYTECHNIC,DEPARTMENT OF ECE
41
EMBEDDED SYSTEMS.IND.TRG
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
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.
In which the interrupt routines will take care of the most urgent operations.
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.
49
50