Microcontroller 1
Microcontroller 1
Syllabus:
Microcontroller:
Microprocessor Vs Microcontroller, Microcontroller & Embedded Processors, Processor Architectures-
Harvard Vs Princeton & RISC Vs CISC.
8051 Architecture- Registers, Pin diagram, I/O ports functions, Internal Memory organization. External
Memory (ROM & RAM) interfacing.
Refer: Textbook 1-1.1, Textbook 2-1.0,1.1,3.0,3.1,3.2,3.3 Textbook 3-Pg 5-9
Page 1 of 24
data bus. The address bus is a unidirectional bus, which means that the CPU uses the address bus only to
send out addresses.
Control bus: The control buses are used to provide read or write signals to the device to indicate if the CPU
is asking for Information or sending it information.
Of the three buses, the address bus and data bus determine the capability of a given CPU.
CPU and its relation to RAM and ROM: For the CPU to process information, the data must be stored in
RAM or ROM. The function of ROM in computers is to provide information that is fixed and permanent. In
contrast, RAM is used to store information that is not permanent and can change with time. The CPU cannot
get the information directly from the disk since the disk is too slow. In other words, the CPU first seeks the
information to be processed from RAM (or ROM). Only if it is not there does the CPU seek it from a mass
storage device such as a disk, and then it transfers the information to RAM. For this reason, RAM and ROM
are sometimes referred to as primary memory and disks are called secondary memory.
Inside CPUs: A program stored in memory provides instructions to the CPU to perform an action. It is the
function of the CPU to fetch these instructions from memory and execute them. To perform the actions of
fetch and execute, all CPUs are equipped with resources such as the following:
Page 2 of 24
1.1 Microcontrollers and Embedded Processors:
9. GPP are more flexible in design point of view. MC are less flexible in design point of view.
10. GPP has single memory map for data & code MC has separate memory map for data & code.
11. GPP has less number of multifunctional pins. MC has more number of multifunctional pins.
Intel’s x86 family (8086, 80286, 80386, 80486, and
8051 family, PIC 16F8X, Hitachi H8, 68HC11xx,
12. the Pentium) or Motorola’s 680×0 family (68000,
etc.
68010, 68020, 68030, 68040, etc.)
Page 3 of 24
1.1.2 Microcontrollers for embedded systems:
An embedded product uses a microprocessor (or microcontroller) to do one task and one task
only. In an embedded system, there is only one application software that is typically burned into ROM.
Ex: A printer is an example of embedded system since the processor inside it performs only one task;
namely, getting the data and printing it.
In Contrast, a Personal Computer can be used for any number of applications such as word
processor, print server, bank teller terminal, video game player, network server, or internet terminal.
The reason a PC can perform myriad tasks is that it has RAM memory and an operating system that
loads the application software into RAM and lets the CPU run it.
An x86 PC contains or is connected to various embedded products such as the keyboard, printer,
modem, disk controller, sound card, CD-ROM drive, mouse, and so on. Each one of these peripherals
has a microcontroller inside it that performs only one task. For example, inside every mouse there is a
microcontroller that performs the task of finding the mouse position and sending it to the Pc. Table-2
below lists some embedded products.
One of the most critical needs of an embedded system is to decrease power consumption and
space. This can be achieved by integrating more functions into the CPU chip.
Choosing a microcontroller:
Three criteria in choosing microcontrollers are as follows:
1) Meeting the computing needs of the task at hand efficiently and cost effectively:
a) Data handling size: an 8-bit, 16-bit, or 32-bit microcontroller can best handle the computing
needs of the task most effectively
b) Speed: highest speed that the microcontroller supports?
c) Packaging: Does it come in a 40-pin DIP (dual inline package) or a QFP (quad flat package), or
some other packaging format? This is important in terms of space, assembling, and prototyping
the end product.
Page 4 of 24
d) Power consumption: This is especially critical for battery-powered products.
e) Memory: The amount of RAM and ROM on chip.
f) Peripheral needed: The number of I/O pins and the timer on the chip.
g) How easy it is to upgrade to higher-performance or lower power-consumption versions.
h) Cost per unit. This is important in terms of the final cost of the product in which a
microcontroller is used. For example, there are microcontrollers that cost 50 cents per unit when
purchased 100,000 units at a time.
2) Availability of software development tools such as compilers, assemblers, and debuggers:
Key considerations include the availability of an assembler, debugger, a code-efficient C
language compiler, emulator, technical support, and both in-house and outside expertise. In many
cases, third-party vendor (that is, a supplier other than the chip manufacturer) support for the chip
is as good as, if not better than, support from the chip manufacturer.
3) Wide availability and reliable sources of the microcontroller:
Ready availability in needed quantities both now and in the future. The 8051 family has the
largest number of diversified (multiple source) suppliers. By supplier is meant a producer besides
the originator of the microcontroller. In the case of the 8051, which was originated by Intel,
several companies also currently produce (or have produced in the past) the 8051. These
companies include: Intel, Atmel, Philips/Signetics, AMD, Infineon (formerly Siemens), Matra, and
Dallas Semiconductor.
• The code storage is not optimal and requires multiple fetches to form the instruction.
• Program and data fetches are done using TDM which affects the performance.
• Ex: Motorola 68HC11, 8086, etc.
2) RISC Vs CISC:
2. Instructions are executed in single cycles Instructions are executed in multiple cycles
3. Low hardware complexity High hardware complexity
4. Hardware based decoder and control unit Microprogrammed decoder and control unit
Instructions are of variable length &
5. Instructions are of fixed length & format
different format
6. Program consists of large code size Program consists of small code size.
Pipelining: different parts of an instruction Execution time for each instruction may be
7.
are executed simultaneously different.
8. Less number of addressing modes More number of addressing modes
CISC approach attempts to minimize the RISC approach to reduce the cycles per
9. no. of instructions per program sacrificing instruction at the cost of the number of
the no. of cycles per instruction. instructions per program.
10. Ex.: ARM (advanced RISC), etc. Ex.: Z80, 68000, 80286, 80386, etc.
Page 6 of 24
Fig.6: Block diagram of 8051
Features of 8051:
1) An 8-bit ALU with A & B Registers, 8-bit PSW.
2) 16-bit address and 8-bit data bus.
3) 16-bit Program Counter (PC) & 16-bit Data Pointer (DPTR).
4) 8-bit Stack Pointer (SP), initial default value is 07h.
5) SFR: TCON, TMOD, SCON, PCON, SBUF, IP & IE, etc.
6) Two 16-bit timers/counters: T0 & T1
7) Two external interrupts INT0 & INT1 and three internal interrupts TO, T1 & SI.
8) Full duplex UART Serial interface.
9) 32 I/O pins arranged as 04 8-bit ports: P0 - P3.
10) Special bit manipulation instructions.
11) Internal ROM of 4 KB, 8751 - EPROM; 8951 - EEPROM 8031 – 0 bytes. Extendable up to 64 KB.
12) Internal RAM of 128 bytes, Extendable up to 64KB.
✓ Four register banks, each containing 8 registers (32 bytes)
✓ 16 bytes bit addressable memory
✓ 80 bytes of general-purpose data memory
13) Harvard memory architecture. The program memory and data memory have separate address spaces from
0000h and separate control signals.
14) CISC (Complex Instruction Set Computer) architecture.
Page 7 of 24
15) Internal clock and oscillator circuit.
Classic version has no DAC, modem, watchdog timer, ADC, floating-point processor, cache, memory
management unit, DMA, pipelining.
CY AC F0 RS1 RS0 OV -- P
Page 8 of 24
CY, the carry flag: This flag is set whenever there is a carry out from the D7 bit. This flag bit is
affected after an 8-bit addition or subtraction. It can also be set to 1 or 0 directly by an instruction such
as "SETB c" and "CLR C" where "SETB" stands for "set bit carry" and "CLR C" for "clear carry".
AC, the auxiliary carry flag: If there is a carry from D3 to D4 during an ADD or SUB operation, this
bit is set; otherwise, it is cleared. This flag is used by instructions that perform BCD (binary coded
decimal) arithmetic.
P, the parity flag: The parity flag reflects the number of 1s in the A (accumulator) register only. If the
A register contains an odd number of 1s, then P = 1. Therefore, P = 0 if A has an even number of 1s.
OV, the overflow flag: This flag is set whenever the result of a signed number operation is too large,
causing the high-order bit to overflow into the sign bit. In general, the carry flag is used to detect errors
in unsigned arithmetic operations. The overflow flag is only used to detect errors in signed arithmetic.
Program Counter (PC):
Program counter is a 16-bit register which addresses the instruction bytes that are to be fetched from
locations in program memory.
Program ROM may be on the chip at addresses 0000h to 0FFFh, external to the chip for addresses that
exceed 0FFFh, or totally external for all addresses from 0000h to FFFFh.
The PC is automatically incremented after every instruction byte is fetched and may also be altered by
certain instructions. The PC is the only register that doesn’t have an internal address.
Data Pointer (DPTR):
The data pointer (DPTR) is a 16-bit register made up of two 8-bit registers, named DPH & DPL, which are
used to furnish memory addresses for internal & external code access & data access.
The DPTR is under the control of program instructions and can be specified by its 16-bit name DPTR or by
each individual byte name DPH & DPL.
DPTR doesn’t have a single internal address; DPH & DPL are each assigned an address.
Page 9 of 24
microcontroller such as fetching an opcode byte, decoding an opcode, executing an opcode, or writing a data
byte.
Program instructions may require one, two, or four machine cycles to be executed, depending on the type of
instruction. Instructions are fetched and executed by the microcontroller automatically, beginning with the
instruction located at ROM memory address 0000h at the time the microcontroller is first reset.
Ex.: A 12 MHz crystal yields the convenient time of 1 µs per cycle. An 11.0592 MHz crystal yields a cycle
frequency of 921.6 KHz, which can be divided evenly by the standard communication baud rates of 19200,
9600, 4800, 2400, 1200 and 300.
Page 11 of 24
Fig. 9: Internal RAM organization
2. Bit-addressable read/write memory:
A bit addressable area of 16 bytes occupies RAM byte address 20h to 2Fh, forming a total of 128
addressable bits. An addressable bit may be specified by its address of 00h to 7Fh or 8-bits may form
any byte address from 20h to 2Fh. Addressable bits are useful when the program need only remember
a binary event (ON, OFF, etc.)
3. General purpose area or scratch pad area:
A byte-addressable area from 30h to 7Fh is used as general-purpose RAM for data storage.
Page 12 of 24
and then contents of the register are saved on the stack and. To PUSH the registers onto the stack we must
use their RAM addresses.
Popping from the stack:
Popping the contents of the stack back into a given register is the opposite process of pushing. With
every pop, the top byte of the stack is copied to the register specified by the instruction and the stack pointer
is decremented once.
The upper limit of the stack:
➢ Locations 08h to 1Fh and 30h to 7Fh in the 8051 RAM can be used for the stack.
➢ Locations 20h-2Fh of RAM are reserved for bit-addressable memory and must not be used by the stack.
➢ Stack location can be changed to a desired location by changing the address in stack pointer (SP).
There are many special function registers and they are widely used. The SFR can be accessed by their
names (which is much easier) or by their addresses.
Table-4 lists the 8051 special function registers (SFR) and their addresses. The following two points should
be noted about the SFR addresses. Fig 10 represents the SFRs as programming model.
1. The special function registers have addresses between 80h and FFh.
2. Not all the address space of 80h to FFh is used by the SFR. The unused locations 80h to FFh are reserved
and must not be used by the,8051 programmer.
Page 14 of 24
1.3 Pin Diagram of 8051 microcontroller:
Page 15 of 24
1.4 IO ports fucntions:
I/0 port pins and their functions:
The four ports P0, P1, P2, and P3 each has 8 pins, making them 8-bit ports. All the ports upon RESET are
configured as output, ready to be used as output ports. To use any of these ports as an input port, it must be
programmed. To configure as an input, a 1 must be sent to the port.
i) Port 0:
Port 0 occupies a total of 8 pins (pins 32 - 39). It can be used for input or output. To use the pins of port 0 as
both input and output ports, each pin must be connected externally to a 10K-ohm pull-up resistor. This is due
to the fact that P0 is an open drain, unlike P1, P2, and P3.
Ports 0 as input: With resistors connected to port 0, in order to make it an input, the port must be
programmed by writing 1 to all the bits.
Dual role of port 0: Port 0 is also designated as AD0 - AD7, allowing it to be used for both address and
data. When connecting an 8051/31 to an external memory, port 0 provides both address and data. The 8051
multiplexes address and data through port 0 to save pins.
ii) Port 1:
Port 1 occupies a total of 8 pins (pins 1 to 8). It can be used as input or output. In contrast to port 0, this port
does not need any pull-up resistors since it already has pull-up resistors internally. Upon reset, port 1 is
configured as an input port.
Port 1 as input: If port 1 has been configured as an output port, to make it an input port, it must be
programmed by writing 1 to all its bits.
iii) Port 2:
Port 2 occupies a total of 8 pins (pins 21 to 28). It can be used as input or output. Just like P1, port 2 does not
need any pull-up resistors since it already has pull-up resistors internally. On reset, port 2 is configured as an
input port.
Port 2 as input: To make port 2 an input, it must be programmed by writing 1 to all its bits.
Dual role of port 2: Port 2 is also designated as A8 - A15, indicating its dual function. When the 8051 is
connected to external memory, P2 is used for the upper 8 bits of the 16-bit address, and it cannot be used for
I/O.
Page 16 of 24
iv) Port 3:
Port 3 occupies a total of 8 pins, pins 10 through 17. It can be used as input or output. P3 does not need any
pull- up resistors, just as P1 and P2 did not. Port 3 has the additional function of providing some extremely
important signals such as interrupts. Table-6 provides these alternate functions of P3.
Fig. 13: Data, Address, and Control buses for the 8031/51
EA pin:
➢ The EA pin is connected to Vcc to indicate that the program code is stored in the microcontroller's on-chip
ROM.
➢ To indicate that the program code is stored in external ROM, this pin must be connected to GND.
P0 and P2 role in providing addresses:
➢ In the 8031/51, port 0 and port 2 provide the 16-bit address to access external memory.
➢ P0 provides the lower 8-bit addresses A0- A7, and P2 provides the upper 8-bit addresses A8 - A15.
➢ P0 is used for both the address and data paths. This is called address/ data multiplexing in chip design.
➢ The ALE signal is used to demultiplex the P0 into data path and address path. when ALE = 0 the 8031
uses P0 for the data path, and when ALE = 1, it uses it for the address path.
➢ To extract the addresses from the P0 pins, 74LS373 latch is used and the connection of P0 is as shown in
fig. 13.
Page 17 of 24
PSEN:
➢ PSEN is an output signal for the 8031/51 microcontroller uses to access external ROM containing
program code and must be connected to the OE pin of a ROM containing the program code.
➢ The role of EA and PSEN is significant when connecting the 8031/51 to external ROM.
✓ When the EA pin is connected to GND, the 8031/51 fetches opcode from external ROM by using
PSEN.
✓ When EA is connected to VCC, these chips do not activate the PSEN pin. This indicates that the on-
chip ROM contains program code.
Ex.:
Fig.14 shows the connections between an 8031 and an external memory configuration consisting of 16K of
EPROM and 8K of static RAM. The 8051 accesses external RAM whenever certain program instructions are
executed. External ROM is accessed whenever the /EA is connected to ground or when the PC contains an
address higher than the last address in the internal 4K ROM (0FFFh). 8051 designs can thus use internal and
external ROM automatically; the 8031, having no internal ROM, must have /EA grounded.
Fig.15 shows the timing associated with an external memory access cycle. During any memory access cycle,
port 0 is time multiplexed. That is, it provides the lower byte of the 16-bit memory address, then acts as bi-
directional data bus to write or read a byte of memory data. Port 2 proivdes the high byte of the memory
address during the entire memory read/write cycle.
The lower address byte from port 0 must be latched into an external register to save the byte. Address byte
save is accompished by the ALE clock pulse that provides the correct timing for the ‘373 type data latch. The
port 0 pins then become free to serve as a data bus.
Page 18 of 24
Fig.15: External memory timing
If the memory access is for a byte of program code in the ROM, the /PSEN pin will go low to enable the
ROM to place a byte of program code on the data bus. If the access is for a RAM byte, the /WR (write) or
/RD (read) pins will go low, enabling data to flow between the RAM and the data bus.
Note that the /WR & /RD signals are alternate uses for port 3 pins 16 and 17. Also, port 0 is used for the
lower address byte and data; port 2 is used for upper address bits. The use of external memory consumes
many of the port pins, leaving only port 1 and parts of port 3 for general I/O.
Page 19 of 24
Fig.16: CPU connecting to memory chip
1. The data bus of the CPU is connected directly to the data pins of the memory chip.
2. Control signals RD (read) & WR (write) from the CPU are connected to the OE (output enable) & WE
(write enable) pins of the memory chip respectively.
3. The address bus of CPU is connected to address lines of the memory chip and also used to decode the
memory chip selection through CS pin of memory chip.
Chip select of a memory chip is normally active low and is activated by the output of the memory decoder.
Normally memories are divided into blocks, and the output of the decoder selects a given memory block.
There are three ways to generate a memory block selector:
Solution:
(a) When EA = 0, the EA pin is strapped to GND, and all program fetches are directed to external memory
regardless of whether or not the 8751 has some on-chip ROM for program code. This external ROM can be as
high as 64K bytes with address space of 0000 - FFFFH. In this case an 8751 (89C51) is the same as the
8031system.
(b) With the 8751 (89C51) system where EA = Vcc the microcontroller fetches the program code of addresses 0000 -
0FFFH from on-chip ROM since it has 4K bytes of on-chip program ROM and any fetches from addresses
1000H - FFFFH are directed to external ROM.
(c) With the 8752 (89C52) system where EA = V cc- themicrocontroller fetches the program code of addresses
0000 - 1FFFH from on-chip ROM since it has 8K bytes of on-chip program ROM and any, fetches from
addresses 2000H - FFFFH are directed to external ROM.
Ex:
Discuss the role of the PSEN pin in accessing on-chip and off-chip program codes.
Solution:
In the process of fetching the internal on-chip program code the PSEN pin is not used and is never activated.
However, PSEN is used for all external program fetches.
External ROM for data:
➢ To connect the 8031/51 to external ROM containing data, we use RD (pin P3.7).
➢ PSEN and RD signals play an important role in accessing ROM for code & data respectively. For the ROM
containing the program code, PSEN is used to fetch the code. For the ROM containing data, the RD signal is used
to fetch the data.
➢ MOVC instruction is used to access the data from ROM along with PC or DPTR.
Page 22 of 24
8051 Data memory space:
➢ In addition to its code space, the 8051 family also has 64K bytes of data memory space.
➢ The data memory space is accessed using the DPTR register and an instruction called MOVX, where X stands for
external (meaning that the memory space must be implemented externally).
External memory interfacing example:
Fig. 21: 8031 connection to External Program ROM, Data RAM, and Data ROM
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0000h
16K Data
ROM ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 3FFFh
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 8000h
16K Data
RAM ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 BFFFh
16K x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0000h
Program ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
ROM x x 1 1 1 1 1 1 1 1 1 1 1 1 1 1 3FFFh
Page 23 of 24
Review Questions for module – 1:
Page 24 of 24