Embedded System Design Using 8031&8051 Microcontrollers


We are living in the Embedded World. You are surrounded with many embedded products and your
daily life largely depends on the proper functioning of these gadgets. Television, Radio, CD player of
your living room, Washing Machine or Microwave Oven in your kitchen, Card readers, Access
Controllers, Palm devices of your work space enable you to do many of your tasks very effectively.
Apart from all these, many controllers embedded in your car take care of car operations between the
bumpers and most of the times you tend to ignore all these controllers.
In recent days, you are showered with variety of information about these embedded controllers in
many places. All kinds of magazines and journals regularly dish out details about latest technologies,
new devices, fast applications which make you believe that your basic survival is controlled by these
embedded products. Now you can agree to the fact that these embedded products have successfully
invaded into our world. You must be wondering about these embedded controllers or systems. What is
this Embedded System?
The computer you use to compose your mails, or create a document or analyze the database is known
as the standard desktop computer. These desktop computers are manufactured to serve many purposes
and applications.
You need to install the relevant software to get the required processing facility. So, these desktop
computers can do many things. In contrast, embedded controllers carryout a specific work for which
they are designed. Most of the time, engineers design these embedded controllers with a specific goal
in mind. So these controllers cannot be used in any other place.
Theoretically, an embedded controller is a combination of a piece of microprocessor based hardware
and the suitable software to undertake a specific task.
These days designers have many choices in microprocessors/ microcontrollers. Especially, in 8 bit
and 32 bit, the available variety really may overwhelm even an experienced designer. Selecting a right
microprocessor may turn out as a most difficult first step and it is getting complicated as new devices
continue to pop-up very often.
In the 8 bit segment, the most popular and used architecture is 8031. Market acceptance of this
particular family has driven many semiconductor manufacturers to develop something new based on
this particular architecture. You can find this 8031 in a variety of configurations, flavour and definitely
you cannot see this kind of variations with any other architecture. Even after 25 years of existence,
semiconductor manufacturers still come out with some kind of device using this 8031 core.

 8031 Microcontrollers

8031 Architecture

The generic 8031 architecture sports a Harvard architecture, which contains two separate buses for
both program and data. So, it has two distinctive memory spaces of 64K X 8 size for both program and
data. It is based on an 8 bit central processing unit with an 8 bit Accumulator and another 8 bit B
register as main processing blocks. Other portions of the architecture include few 8 bit and 16 bit
registers and 8 bit memory locations.
Each 8031 device has some amount of data RAM built in the device for internal processing. This area
is used for stack operations and temporary storage of data.
This base architecture is supported with onchip peripheral functions like I/O ports, timers/counters,
versatile serial communication port. So it is clear that this 8031 architecture was designed to cater
many real time embedded needs.
The following list gives the features of the 8031 architecture:
 Optimized 8 bit CPU for control applications.
 Extensive Boolean processing capabilities.
  64K Program Memory address space.
  64K Data Memory address space.
  128 bytes of onchip Data Memory.
  32 Bi-directional and individually addressable I/O lines.
  Two 16 bit timer/counters.
  Full Duplex UART. 
  6-source / 5-vector interrupt structure with priority levels.
  Onchip clock oscillator.
Now you may be wondering about the non mentioning of memory space meant for the program storage,
the most important part of any embedded controller. Originally this 8031 architecture was introduced
with onchip, ‘one time programmable’ version of Program Memory of size 4K X 8. Intel delivered all
these microcontrollers (8051) with user’s program fused inside the device. The memory portion was
mapped at the lower end of the Program Memory area. But, after getting devices, customers couldn’t
change any thing in their program code, which was already made available inside during device
fabrication.






So, very soon Intel introduced the 8031 devices (8751) with re-programmable type of Program Memory
using built-in EPROM of size 4K X 8. Like a regular EPROM, this memory can be re-programmed many
times. Later on Intel started manufacturing these 8031 devices without any onchip Program Memory.
Now I go ahead giving more information on the important functional blocks of the 8031.



                                                   8031 Microcomputer Pinout Diagram
Central Processing Unit

The CPU is the brain of the microcontrollers reading user’s programs and executing the expected task
as per instructions stored there in. Its primary elements are an 8 bit Arithmetic Logic Unit (ALU),
Accumulator (Acc), few more 8 bit registers, B register, Stack Pointer (SP), Program Status Word
(PSW) and 16 bit registers, Program Counter (PC) and Data Pointer Register (DPTR).
The ALU (Acc) performs arithmetic and logic functions on 8 bit input variables. Arithmetic operations
include basic addition, subtraction, multiplication and division. Logical operations are AND, OR,
Exclusive OR as well as rotate, clear, complement and etc. Apart from all the above, ALU is responsible
in conditional branching decisions, and provides a temporary place in data transfer operations within
the device.
B register is mainly used in multiply and divide operations. During execution, B register either keeps
one of the two inputs and then retains a portion of the result. For other instructions, it can be used as
another general purpose register.
Program Status Word keeps the current status of the ALU in different bits.
Stack Pointer (SP) is an 8 bit register. This pointer keeps track of memory space where the important
register information are stored when the program flow gets into executing a subroutine. The stack
portion may be placed in any where in the onchip RAM. But normally SP is initialized to 07H after a
device reset and grows up from the location 08H. The Stack Pointer is automatically incremented or
decremented for all PUSH or POP instructions and for all subroutine calls and returns.
Program Counter (PC) is the 16 bit register giving address of next instruction to be executed during
program execution and it always points to the Program Memory space.
Data Pointer (DPTR) is another 16 bit addressing register that can be used to fetch any 8 bit data from
the data memory space. When it is not being used for this purpose, it can be used as two eight bit
registers.

Input / Output Ports

The 8031’s I/O port structure is extremely versatile and flexible. The device has 32 I/O pins configured
as four eight bit parallel ports (P0, P1, P2 and P3). Each pin can be used as an input or as an output
under the software control. These I/O pins can be accessed directly by memory instructions during
program execution to get required flexibility.
These port lines can be operated in different modes and all the pins can be made to do many different
tasks apart from their regular I/O function executions. Instructions, which access external memory,
use port P0 as a multiplexed address/data bus. At the beginning of an external memory cycle, low
order 8 bits of the address bus are output on P0. The same pins transfer data byte at the later stage
of the instruction execution.
Also, any instruction that accesses external Program Memory will output the higher order byte on P2
during read cycle. Remaining ports, P1 and P3 are available for standard I/O functions. But all the 8
lines of P3 support special functions: Two external interrupt lines, two counter inputs, serial port’s two
data lines and two timing control strobe lines are designed to use P3 port lines. When you don’t use
these special functions, you can use corresponding port lines as a standard I/O.
Even within a single port, I/O operations may be combined in many ways. Different pins can be configured
as input or outputs independent of each other or the same pin can be used as an input or as output at
different times. You can comfortably combine I/O operations and special operations for Port 3 lines.

Timers / Counters

8031 has two 16 bit Timers/Counters capable of working in different modes. Each consists of a ‘High’
byte and a ‘Low’ byte which can be accessed under software. There is a mode control register and a
control register to configure these timers/counters in number of ways.
These timers can be used to measure time intervals, determine pulse widths or initiate events with
one microsecond resolution upto a maximum of 65 millisecond (corresponding to 65, 536 counts). Use
software to get longer delays. Working as counter, they can accumulate occurrences of external events
(from DC to 500KHz) with 16 bit precision

Serial Port

Each 8031 microcomputer contains a high speed full duplex (means you can simultaneously use the
same port for both transmitting and receiving purposes) serial port which is software configurable in 4
basic modes: 8 bit UART; 9 bit UART; Interprocessor Communications link or as shift register I/O
expander.
For the standard serial communication facility, 8031 can be programmed for UART operations and can
be connected with regular personal computers, teletype writers, modem at data rates between 122
bauds and 31 kilobauds. Getting this facility is made very simple using simple routines with option to
select even or odd parity. You can also establish a kind of Interprocessor communication facility among
many microcomputers in a distributed environment with automatic recognition of address/data.
Apart from all above, you can also get super fast I/O lines using low cost simple TTL or CMOS shift
registers.

Memory Organization

The 8031 architecture provides both onchip memory as well as off chip memory expansion capabilities.
It supports several distinctive ‘physical’ address spaces, functionally separated at the hardware level
by different addressing mechanisms, read and write controls signals or both:
l On chip Program Memory
l On chip Data Memory
l Off chip Program Memory
l Off chip Data Memory
l On chip Special Function Registers
The Program Memory area (EPROM incase of external memory or Flash/EPROM incase of internal
one) is extremely large and never lose information when the power is removed. Program Memory is
used for information needed each time power is applied: Initialization values, Calibration data, Keyboard
lookup tables etc along with the program itself. The Program Memory has a 16 bit address and any
particular memory location is addressed using the 16 bit Program Counter and instructions which
generate a 16 bit address.
Onchip Data memory is smaller and therefore quicker than Program Memory and it goes into a random
state when power is removed. Onchip RAM is used for variables which are calculated when the program
is executed.
In contrast to the Program Memory, On chip Data Memory accesses need a single 8 bit value (may be
a constant or another variable) to specify a unique location. Since 8 bits are more than sufficient to
address 128 RAM locations, the onchip RAM address generating register is single byte wide.
Different addressing mechanisms are used to access these different memory spaces and this greatly
contributes to microcomputer’s operating efficiency.
The 64K byte Program Memory space consists of an internal and an external memory portion. If the EA
pin is held high, the 8051 executes out of internal Program Memory unless the address exceeds 0FFFH
and locations 1000H through FFFFH are then fetched from external Program Memory. If the EA pin is
held low, the 8031 fetches all instructions from the external Program Memory. In either case, the 16 bit
Program Counter is the addressing mechanism.
The Data Memory address space consists of an internal and an external memory space. External Data
Memory is accessed when a MOVX instruction is executed.
Apart from onchip Data Memory of size 128/256 bytes, total size of Data Memory can be expanded
upto 64K using external RAM devices.
Total internal Data Memory is divided into three blocks:
Lower 128 bytes.
Higher 128 bytes
Special Function Register space.
Higher 128 bytes are available only in 8032/8052 devices.
Even though the upper RAM area and SFR area share same address locations, they are accessed
through different addressing modes. Direct addresses higher than 7FH access SFR memory space
and indirect addressing above 7FH access higher 128 bytes (in 8032/8052).

Common Memory Space

The 8031’s Data Memory may not be used for program storage. So it means you can’t execute
instructions out of this Data Memory.
But, there is a way to have a single block of offchip memory acting as both Program and Data Memory.
By gating together both memory read controls (RD and PSEN) using an AND gate, a common memory
read control signal can be generated.
In this arrangement, both memory spaces are tied together and total accessible memory is reduced
from 128 Kbytes to 64 Kbytes.
The 8031 can read and write into this common memory block and it can be used as Program and Data
Memory.
You can use this arrangement during program development and debugging phase. Without taking
Microcontroller off the socket to program its internal ROM (EPROM/Flash ROM), you can use this
common memory for frequent program storage and code modifications.

Interrupts

The 8031 has five interrupt sources: one from the serial port when a transmission or reception operation
is executed; two from the timers when overflow occurs and two come from the two input pins INT0,
INT1. Each interrupt may be independently enabled or disabled to allow polling on same sources and
each may be classified as high or low priority.
A high priority source can override a low priority service routine. These options are selected by interrupt
enable and priority control registers, IE and IP.
When an interrupt is activated, then the program flow completes the execution of the current instruction
and jumps to a particular program location where it finds the interrupt service routine. After finishing
the interrupt service routine, the program flows return to back to the original place.
The Program Memory address, 0003H is allotted to the first interrupt and next seven bytes can be
used to do any task associated with that interrupt.

Interrupt Source               Service routine starting address
External  0                           0003H
Timer/Counter 0                  000BH
External 1                            0013H
Timer/counter 1                   001BH
Serial port                           0023H

Addressing Modes

8031’s assembly language instruction set consists of an operation mnemonic and zero to three operands
separated by commas. In two byte instructions the destination is specified first, and then the source.
Byte wide mnemonics like ADD or MOV use the Accumulator as a source operand and also to receive
the result.
The 8031 supports five types of addressing modes:
* Register Addressing
* Direct Addressing
* Register Indirect Addressing
* Immediate Addressing
* Index Addressing

Register Addressing

Register Addressing accesses the eight working registers (R0-R7) of the selected register bank. The
least significant three bits of the instruction opcode indicate which register is to be used for the
operation. One of the four banks of registers is to be predefined in the PSW before using register
addressing instruction. ACC, B, DPTR and CY, (the Boolean Accumulator) can also be addressed in
this mode.

Direct Addressing

Direct addressing can access any onchip variables or hardware register. To indicate the address of
the location, an additional byte is attached to the opcode. Depending on the highest order bit of the
direct address byte one of two physical memory space is selected.
When the direct address range is between 0 and 127 (00H - 7FH) one of the 128 low order onchip
RAM location is accessed. All I/O ports, special function, control registers are assigned between 128
and 255 (80H - FFH). When direct addressing indicates any location in this range, corresponding
hardware register is accessed.
This is the only method available for accessing I/O ports and special function registers.

Register Indirect Addressing

Register indirect addressing uses the contents of either R0 or R1 (in the pre selected register bank)
as a address pointer to locate in a 256 byte block (the lower 128 bytes of internal RAM in 8031 or 256
bytes in 8032) or the lower 256 bytes of external data memory. Note that the special function registers
are not accessible in this mode. Access to full 64K external data memory address space is indicated
by the 16 bit Data Pointer register, DPTR.
Execution of PUSH and POP instructions also involve indirect register addressing. The Stack Pointer
indicates the correct stack location anywhere in the internal RAM.

 Immediate Addressing

When a source operand is a constant rather than a variable, then the constant can be embedded into
the instruction itself. This kind of instructions take two bytes and first one specifies the opcode and
second byte gives the required constant.

Index Addressing

Only the Program Memory can be accessed by this mode. This mode is intended for reading lookup
tables in the Program Memory. A 16 bit base register (either DPTR or the Program Counter) points to
the base of the lookup tables and the Accumulator carries the constant indicating table entry number.
The address of the exact location of the table is formed by adding the Accumulator data to the base
pointer.

Why Atmel Devices?

In this part of the world (precisely India where I live) Atmel devices are freely available everywhere at
an affordable cost. Since Atmel has very good distribution network in many countries, I am sure you
can also find Atmel devices in your place.
I like Atmel’s flash memory very much. In early days, we used to get EPROM devices from ‘God - only
- knows’ sources and we always worried about the life of our target hardware. Atmel really relieved us
form this. Without caring about EPROM erases, we can program/reprogram the device during prototype
development and most of the time device stays cool even after many hours of running. Also we can go
upto 24MHz with these devices using 3V to 6V supply.
Also Whatever we are going to discuss can be applied to many other 8031 devices without any problem.

Real Life Projects

Having introduced 8031 architecture in your knowledge base, it is time for me to make you understand
what is actually going on in the Embedded World.
Now I am going to discuss about some simple yet useful embedded applications to give you more idea. All
of these projects are modeled from the commercial products. There are many companies manufacturing
these products for the general market. Then I proceed with telling you about various hardware and
software development tools you may need during project implementation.
This flow of discussion should make you comfortable in facing more complex and elaborate designs.

www.electronicsready4u.blogspot.com