Module-1 (1) Juk
Module-1 (1) Juk
8051 Microcontroller
Microprocessor vs Microcontroller
• General-purpose microprocessors
• Must add RAM, ROM, I/O ports, and timers externally to make them functional
• Have the advantage of versatility on the amount of RAM, ROM, and I/O ports
• Microcontroller
• The fixed amount of on-chip ROM, RAM, and number of I/O ports makes them ideal
for many applications in which cost and space are critical
• In many applications, the space it takes, the power it consumes, and the price per unit
are much more critical considerations than the computing power
Microprocessor Microcontroller
Microprocessor contains ALU, General Microcontroller contains the circuitry of
purpose registers, stack pointer, program microprocessor, and in addition it has built in
counter, clock timing circuit, interrupt circuit ROM, RAM, I/O Devices, Timers/Counters
etc.
It has many instructions to move data between It has few instructions to move data between
memory and CPU memory and CPU
Few bit handling instruction It has many bit handling instructions
Less number of pins are multifunctional More number of pins are multifunctional
Single memory map for data and code Separate memory map for data and code
(program) (program)
Access time for memory and IO are more Less access time for built in memory and IO.
Microprocessor based system requires It requires less additional hardware
additional hardware
More flexible in the design point of view Less flexible since the additional circuits which
is residing inside the microcontroller is fixed for
a particular microcontroller
Large number of instructions with flexible Limited number of instructions with few
addressing modes addressing modes
1
Microcontrollers for Embedded Systems
An embedded product uses a microprocessor (or microcontroller) to do one task and one task
only
o There is only one application software that is typically burned into ROM
A PC, in contrast with the embedded system, can be used for any number of applications
o It has RAM memory and an operating system that loads a variety of applications into
RAM and lets the CPU run them
o A PC contains or is connected to various embedded products
Each one peripheral has a microcontroller inside it that performs only one task
Criteria for Choosing a Microcontroller
Meeting the computing needs of the task at hand efficiently and cost effectively
o Speed
o Packaging
o Power consumption
o The amount of RAM and ROM on chip
o The number of I/O pins and the timer on chip
o How easy to upgrade to higher performance or lower power-consumption versions
o Cost per unit
Availability of software development tools, such as compilers, assemblers, and debuggers
Wide availability and reliable sources of the microcontroller
The 8051 family has the largest number of diversified (multiple source) suppliers
o Intel (original)
o Atmel
o Philips/Signetics
o AMD
o Infineon (formerly Siemens)
o Matra
o Dallas Semiconductor/Maxim
Harvard and Von Neumann Architectures
• Harvard Architecture—a type of computer architecture where the instructions (program
code) and data are stored in separate memory spaces
Example: Intel 8051 architecture
• Von Neumann Architecture—another type of computer architecture where the instructions
and data are stored in the same memory space
Example: Intel x86 architecture (Intel Pentium, AMD Athlon, etc.)
2
8051 Architecture
3
8051 Microcontroller Architecture
8051 has 4 K Bytes of internal ROM. The address space is from 0000 to 0FFFh. If the program
size is more than 4 K Bytes 8051 will fetch the code automatically from external memory.
Accumulator is an 8 bit register widely used for all arithmetic and logical operations. Accumulator
is also used to transfer data between external memory. B register is used along with Accumulator
for multiplication and division. A and B registers together is also called MATH registers.
PSW (Program Status Word). This is an 8 bit register which contains the arithmetic status of ALU
and the bank select bits of register banks.
The heart of the 8051 is the circuitry that generates the clock pulses by which all internal
operations are synchronized.
Pins XTALI and XTAL2 are provided for connecting a resonant network to form an
oscillator.
The 8051 requires an external oscillator circuit. The oscillator circuit usually runs
around 12MHz. the crystal generates 12M pulses in one second. The pulse is used to
synchronize the system operation in a controlled pace.
A machine cycle is minimum amount time a simplest machine instruction must take.
An 8051 machine cycle consists of 12 crystal pulses (ticks).
Instruction with a memory operand needs multiple memory accesses (machine cycles).
4
Typically, a quartz crystal and capacitors are employed, as shown in Figure.
5
• The A (accumulator) register is used for many operations, including addition, subtraction,
integer multiplication and division, and Boolean bit manipulations.
• The A register is also used for all data transfers between the 8051 and any external
memory.
• The B register is used with the A register for multiplication and division operations.
7 6 5 4 3 2 1 0
CY AC F0 RS1 RS0 OV - P
6
The Stack and the Stack Pointer
• The stack refers to an area of internal RAM that is used in conjunction with certain opcodes to
store and retrieve data quickly.
• The 8-bit stack pointer (SP) register is used by the 8051 to hold an internal RAM address
that is called the "top of the stack."
• The top of the stack is the location in internal RAM where the last byte of data was stored
by a stack operation.
• The SP increments before storing data on the stack so that the stack grows up as data is
stored.
• As data is retrieved from the stack, the byte is read from the stack, and then the SP decrements
to point to the next available byte of stored data.
• The SP is set to 07h when the 8051 is reset and can be changed to any internal RAM address
by the programmer.
Pin Diagram
8
Pinout Description
Pins 1-8 PORT 1. Each of these pins can be configured as an input or an output.
Pin 9 RESET. A logic one on this pin disables the microcontroller and clears the contents
of most registers. In other words, the positive voltage on this pin resets the
microcontroller. By applying logic zero to this pin, the program starts execution from
the beginning.
Pins10-17 PORT 3. Similar to port 1, each of these pins can serve as general input or output.
Besides, all of them have alternative functions
Pin 10 RXD. Serial asynchronous communication input or Serial synchronous
communication output.
Pin 11 TXD. Serial asynchronous communication output or Serial synchronous
communication clock output.
Pin 12 INT0.External Interrupt 0 input
Pin 13 INT1. External Interrupt 1 input
Pin 14 T0. Counter 0 clock input
Pin 15 T1. Counter 1 clock input
Pin 16 WR. Write to external (additional) RAM
Pin 17 RD. Read from external RAM
Pin 18, 19 XTAL2, XTAL1. Internal oscillator input and output. A quartz crystal which
specifies operating frequency is usually connected to these pins.
Pin 20 GND. Ground.
Pin 21-28 Port 2. If there is no intention to use external memory then these port pins are
configured as general inputs/outputs. In case external memory is used, the
higher address byte, i.e. addresses A8-A15 will appear on this port. Even
though memory with capacity of 64Kb is not used, which means that not all
eight port bits are used for its addressing, the rest of them are not available as
inputs/outputs.
Pin 29 PSEN. If external ROM is used for storing program then a logic zero (0)
appears on it every time the microcontroller reads a byte from memory.
Pin 30 ALE. Prior to reading from external memory, the microcontroller puts the
lower address byte (A0-A7) on P0 and activates the ALE output. After
receiving signal from the ALE pin, the external latch latches the state of P0
and uses it as a memory chip address. Immediately after that, the ALE pin is
returned its previous logic state and P0 is now used as a Data Bus.
Pin 31 EA. By applying logic zero to this pin, P2 and P3 are used for data and address
transmission with no regard to whether there is internal memory or not. It
means that even there is a program written to the microcontroller, it will not
be executed. Instead, the program written to external ROM will be executed.
By applying logic one to the EA pin, the microcontroller will use both
memories, first internal then external (if exists).
Pin 32-39 PORT 0. Similar to P2, if external memory is not used, these pins can be used
as general inputs/outputs. Otherwise, P0 is configured as address output (A0-
A7) when the ALE pin is driven high (1) or as data output (Data Bus) when
the ALE pin is driven low (0).
Pin 40 VCC. +5V power supply.
9
Internal Memory organization
A functioning computer must have
• MEMORY FOR PROGRAM CODE BYTES, COMMONLY IN ROM
• RAM MEMORY FOR VARIABLE DATA THAT CAN BE ALTERED AS THE
PROGRAM RUNS.
Additional memory can be added externally using suitable circuits.
8051 has a Harvard architecture, which uses the same address, in different memories, for
code and data.
• Internal circuitry accesses the correct memory based upon the nature of the operation in
progress.
Internal RAM
Thirty-two bytes from address 00h to 1Fh that make up 32 working registers organized as four
banks of eight registers each. (Bank0- Bank3; with each bank having registers R0-R7)
Each register can be addressed by name (when its bank is selected) or by its RAM address.
Thus R0 of bank 3 is R0 (if bank 3 is currently selected) or address 18h (whether bank 3 is selected
or not).
Bits RS0 and RS1 in the PSW determine which bank of registers is currently in use.
Register banks not selected can be used as general-purpose RAM.
10
Bank 0 is selected upon reset.
A bit-addressable area of 16 bytes occupies RAM byte addresses 20h to 2Fh, forming a total
of 128 addressable bits.
An addressable bit may be specified by its bit address of 00h to 7Fh, or 8 bits may form any
byte address from 20h to 2Fh.
o Thus, for example, bit address 4Fh is also bit 7 of byte address 29h.
o Addressable bits are useful when the program need only remember a binary event (switch
on, light off, etc.).
A general-purpose RAM area above the bit area, from 30h to 7Fh, addressable as bytes.
Internal ROM
The 8051 is organized so that data memory and program code memory can be in two entirely
different physical memory entities; each has the same address ranges.
A corresponding block of internal program code, contained in an internal ROM, occupies code
address space 0000h to 0FFFh.
The PC is ordinarily used to address program code bytes from addresses 0000h to 0FFFh.
Program addresses higher than 0FFFh, which exceed the internal ROM capacity, will cause the
8051 to automatically fetch code bytes from external program memory.
Code bytes can also be fetched exclusively from an external memory, addresses 0000h to
FFFFh, by connecting the external access pin (EA pin 31 on the DIP) to ground.
The PC does not care where the code is; the circuit designer decides whether the code is found
totally in internal ROM, totally in external ROM, or in a combination of internal and external
ROM.
11
External Memory (ROM & RAM) interfacing
Eg. Interfacing of 16 K Byte of RAM and 32 K Byte of EPROM to 8051
Number of address lines required for 16 Kbyte memory is 14 lines and that of 32Kbytes of memory is
15 lines.
The connections of external memory is shown below.
The lower order address and data bus are multiplexed. De-multiplexing is done by the latch. Initially
the address will appear in the bus and this latched at the output of latch using ALE signal. The output
of the latch is directly connected to the lower byte address lines of the memory. Later data will be
available in this bus. Still the latch output is address itself. The higher byte of address bus is directly
connected to the memory. The number of lines connected depends on the memory size.
The RD and WR (both active low) signals are connected to RAM for reading and writing the data.
PSEN of microcontroller is connected to the output enable of the ROM to read the data from the
memory.
EA (active low) pin is always grounded if we use only external memory. Otherwise, once the program
size exceeds internal memory the microcontroller will automatically switch to external memory.
12
The number of bits that a semiconductor memory chip can store is called chip capacity
o It can be in units of Kbits (kilobits), Mbits (megabits), and so on.
Memory chips are organized into a number of locations within the IC.
o Each location can hold 1 bit, 4 bits, 8 bits, or even 16 bits, depending on how it is
designed internally
The number of locations within a memory IC depends on the address pins
The number of bits that each location can hold is always equal to the number
of data pins
A memory chip contain 2x location, where x is the number of address pins.
Each location contains y bits, where y is the number of data pins on the chip.
The entire chip will contain 2x X y bits
13