MPMC Notes 1
MPMC Notes 1
UNIT – 5: Intel 8051 MICROCONTROLLER: Architecture, hardware concepts, input/output ports and
circuits, external memory, counters/timers, serial data input/output, interrupts. Assembly language
programming: Instructions, addressing modes, simple programs. Interfacing: keyboard, displays (LED, 7-
segment display unit), A/D and D/A converters.
5.0 MICROPROCESSORS AND MICROCONTROLLERS
Microprocessor Microcontroller
Program Counter
Block diagram of microprocessor Block diagram of microcontroller
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.
1|Page
Microcontrollers GOGA SUBRAMANYAM
RISC CISC
Only load/store instructions are used to access In additions to load and store instructions,
memory memory access is possible with other
instructions also.
Instructions executed by hardware Instructions executed by the micro program
It uses single memory space for both It has separate program memory and data
instructions and data. memory
It is not possible to fetch instruction code and Instruction code and data can be fetched
data simultaneously
2|Page
Microcontrollers GOGA SUBRAMANYAM
Execution of instruction takes more machine Execution of instruction takes less machine
cycle cycle
Also known as control flow or control driven Also known as data flow or data driven
computers computers
Simplifies the chip design because of single Chip design is complex due to separate memory
memory space space
Interrupts: Interrupts is a sub-routine call that given by the microcontroller when some other program
with high priority is request for acquiring the system buses the n interrupts occur in current running
program.
Interrupts provide a method to postpone or delay the current process, performs a sub-routine task and then
restart the standard program again.
4|Page
Microcontrollers GOGA SUBRAMANYAM
5|Page
Microcontrollers GOGA SUBRAMANYAM
Port-0 can be configured as a normal bidirectional I/O port or it can be used for address/data interfacing for
accessing external memory. When control is '1', the port is used for address/data interfacing. When the control
is '0', the port can be used as a normal bidirectional I/O port.
PORT-1
Port-1 Pin Structure Port-1 has 8 pins (P1.1-P1.7) .The structure of a port-1 pin is shown in fig below.
Port-1 does not have any alternate function i.e. it is dedicated solely for I/O interfacing. When used as
output port, the pin is pulled up or down through internal pull-up. To use port-1 as input port, '1' has to be
written to the latch. In this input mode when '1' is written to the pin by the external device then it read fine.
But when '0' is written to the pin by the external device then the external source must sink current due to
internal pull-up. If the external device is not able to sink the current the pin voltage may rise, leading to a
possible wrong reading.
6|Page
Microcontrollers GOGA SUBRAMANYAM
PORT-2
PORT 2 Pin Structure Port-2 has 8-pins (P2.0-P2.7) . The structure of a port-2 pin is shown in figure below:
Port-2 is used for higher external address byte or a normal input/output port. The I/O operation is similar to
Port-1. Port-2 latch remains stable when Port-2 pin are used for external memory access. Here again due to
internal pull-up there is limited current driving capability.
ü PORT 3 Pin Structure
PORT-3
Port-3 has 8 pin (P3.0-P3.7). Port-3 pins have alternate functions. The structure of a port-3 pin is shown in
figure
Each pin of Port-3 can be individually programmed for I/O operation or for alternate function. The alternate
function can be activated only if the corresponding latch has been written to '1'. To use the port as input port, '1'
should be written to the latch. This port also has internal pull-up and limited current driving capability.
7|Page
Microcontrollers GOGA SUBRAMANYAM
5.6PIN DIAGRAM
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
8|Page
Microcontrollers GOGA SUBRAMANYAM
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.
=========================================================================
5.7MEMORY ORGANIZATION
Internal RAM organization
BANK 3
2
F
2
E
2
D
2
C
9|Page
Microcontrollers GOGA SUBRAMANYAM
Working Registers
Register Banks: 00h to 1Fh. The 8051 uses 8 general-purpose registers R0 through R7
(R0, R1, R2, R3, R4, R5, R6, and R7). There are four such register banks. Selection of
register bank can be done through RS1,RS0 bits of PSW. On reset, the default Register
Bank 0 will be selected.
Bit Addressable RAM: 20h to 2Fh . The 8051 supports a special feature which allows
access to bit variables. This is where individual memory bits in Internal RAM can be set or
cleared. In all there are 128 bits numbered 00h to 7Fh. Being bit variables any one variable
can have a value 0 or 1. A bit variable can be set with a command such as SETB and
cleared with a command such as CLR. Example instructions are:
SETB 25h ; sets the bit
25h (becomes 1) CLR
25h ; clears bit 25h
(becomes 0)
Note, bit 25h is actually bit 5 of Internal RAM location 24h.
The Bit Addressable area of the RAM is just 16 bytes of Internal RAM located between 20h and
2Fh.
General Purpose RAM: 30h to 7Fh. Even if 80 bytes of Internal RAM memory are
available for general-purpose data storage, user should take care while using the memory
location from 00 -2Fh
since these locations are also the default register space, stack space, and bit addressable
space. It is a good practice to use general purpose memory from 30 – 7Fh. The general
purpose RAM can be accessed using direct or indirect addressing modes.
================================================================
5.8Interfacing External Memory
We have seen that a typical 8051 Microcontroller has 4KB of ROM and 128B of RAM (most
modern. The designer of an 8051 Microcontroller based system is not limited to the internal RAM and
ROM present in the 8051 Microcontroller. There is a provision of connecting both external RAM and
ROM i.e. Data Memory and Program. The reason for interfacing external Program Memory or ROM is
that complex programs written in high – level languages often tend to be larger and occupy more
memory.
Another important reason is that chips like 8031 or 8032, which doesn‘t have any internal ROM,
have to be interfaced with external ROM.A maximum of 64B of Program Memory (ROM) and Data
Memory (RAM) each can be interface with the 8051 Microcontroller. The following image shows the
block diagram of interfacing 64KB of External RAM and 64KB of External ROM with the 8051
Microcontroller.
10 | P a g e
Microcontrollers GOGA SUBRAMANYAM
5.9ADDRESSING MODES
Various methods of accessing the data are called
addressing modes. 8051 addressing modes are
classified as follows.
1. Immediate addressing.
2. Register addressing.
3. Direct addressing.
4. Indirect addressing.
5. Index addressing
1. Immediate addressing.
In this addressing mode the data is provided as a part of instruction itself. In
other words data immediately follows the instruction.
Eg. MOV A,#30H
ADD A, #83 # Symbol indicates the data is immediate.
2. Register addressing.
In this addressing mode the register will hold the data. One of the eight
general registers (R0 to R7) can be used and specified as the operand.
Eg. MOV A,R0 ADD A,R6
R0 – R7 will be selected from the current selection of register bank. The default register bank will
be bank 0.
3. Direct addressing
There are two ways to access the internal memory. Using direct address and
indirect address. Using direct addressing mode we can not only address the internal
memory but SFRs also. In direct addressing, an 8 bit internal data memory address is
specified as part of the instruction and hence, it can specify the address only in the
range of 00H to FFH. In this addressing mode, data is obtained directly from the
memory. Eg. MOV A,30H
4. Indirect addressing
The indirect addressing mode uses a register to hold the actual address that
will be used in data movement. Registers R0 and R1 and DPTR are the only
registers that can be used as data pointers. Indirect addressing cannot be used to refer
to SFR registers. Both R0 and R1 can hold 8 bit address and DPTR can hold 16 bit
address.
Eg. MOV A,@R0
MOV A,@R1
MOV A,@DPTR
5. Indexed addressing.
In indexed addressing, either the program counter (PC), or the data pointer
(DTPR)—is used to hold the base address, and the A is used to hold the offset
address. Adding the value of the base address to the value of the offset address forms
the effective address. Indexed addressing is used with JMP or MOVC instructions.
Look up tables are easily implemented with the help of index addressing.
Eg. MOVC A, @A+DPTR // copies the contents of memory location pointed by
the sum of the accumulator A and the DPTR into accumulator A.
MOVC A, @A+PC // copies the contents of memory location
pointed by the sum of the accumulator A and the program counter
into accumulator A.
11 | P a g e
Microcontrollers GOGA SUBRAMANYAM
1. IE Register
This is an 8 bit register used for enabling or disabling the interrupts. The
structure of IE register is shown below.
2. IP Register.
This is an 8 bit register used for setting the priority of the interrupts.
12 | P a g e
Microcontrollers GOGA SUBRAMANYAM
Timer in 8051 is used as timer, counter and baud rate generator. Timer always
counts up irrespective of whether it is used as timer, counter, or baud rate generator:
Timer is always incremented by the microcontroller. The time taken to count one
digit up is based on master clock frequency.
If Master CLK=12 MHz,
Timer Clock frequency =
Master CLK/12 = 1 MHz
Timer Clock Period =
1micro second
This indicates that one increment in count will take 1 micro second.
The two timers in 8051 share two SFRs (TMOD and TCON) which control the
timers, and each timer also has two SFRs dedicated solely to itself (TH0/TL0 and
TH1/TL1).
13 | P a g e
Microcontrollers GOGA SUBRAMANYAM
TMOD Register
TCON Register
14 | P a g e
Microcontrollers GOGA SUBRAMANYAM
TIMER MODES
Timer Mode-1: This mode is similar to mode-0 except for the fact that the Timer
operates in 16-bit mode.
15 | P a g e
Microcontrollers GOGA SUBRAMANYAM
setting TR1=0. Timer0 in mode-3 establishes TL0 and TH0 as two separate
counters.
Fig: Operation of Timer in Mode 3
Control bits TR1 and TF1 are used by Timer-0 (higher 8 bits) (TH0) in Mode-3
while TR0 and TF0 are available to Timer-0 lower 8 bits(TL0).
============================================================
=======
5.12PROGRAMMING 8051 TIMERS IN ASSEMBLY
In order to program 8051 timers, it is important to know the calculation of initial
count value to be stored in the timer register. The calculations are as follows.
In any mode, Timer Clock period = 1/Timer Clock Frequency.
= 1/(Master Clock Frequency/12)
a. Mode 1 (16 bit timer/counter)
Value to be loaded in decimal = 65536 – (Delay Required/Timer
clock period) Convert the answer into hexadecimal and load
onto THx and TLx register. (65536D = FFFFH+1)
b. Mode 0 (13 bit timer/counter)
Value to be loaded in decimal = 8192 – (Delay
Required/Timer clock period) Convert the answer into
hexadecimal and load onto THx and TLx register. (8192D =
1FFFH+1)
c. Mode 2 (8 bit auto reload)
Value to be loaded in decimal = 256 – (Delay Required/Timer clock
period) Convert the answer into hexadecimal and load onto THx
register. Upon starting the timer this value from THx will be
reloaded to TLx register.
(256D = FFH+1)
Transmitter Receiver
2. Half duplex communication link: In half duplex, the communication link can be
used for either transmission or
reception. Data is transmitted in only one direction at a time.
Transmitter Receiver
Receiver Transmitter
3. Full duplex communication link: If the data is transmitted in both ways at the
same time, it is a full duplex i.e. transmission and reception can proceed
simultaneously. This communication link requires two wires for data, one for
Transmitter Receiver
Receiver Transmitter
transmission and one for reception.
17 | P a g e
Microcontrollers GOGA SUBRAMANYAM
Clock
2. Asynchronous Serial data transmission: In this, different clock sources are used for transmitter
and receiver. In this mode, data is transmitted with start and stop bits. A transmission begins with
start bit, followed by data and then stop bit. For error checking purpose parity bit is included just
prior to stop bit. In Asynchronous serial data communication a single byte is transmitted at a time.
18 | P a g e
Microcontrollers GOGA SUBRAMANYAM
3. PCON register: The SMOD bit (bit 7) of PCON register controls the baud
rate in asynchronous mode transmission.
4. Mode 3
This is similar to mode 2 except baud rate is calculated as in mode 1
--
==========================================================================
19 | P a g e
Microcontrollers GOGA SUBRAMANYAM
Arithmetic Instructions
Using Arithmetic Instructions, you can perform addition, subtraction, multiplication and division. The
arithmetic instructions also include increment by one, decrement by one and a special instruction called
Decimal Adjust Accumulator.
The Mnemonics associated with the Arithmetic Instructions of the 8051 Microcontroller Instruction Set
are:
ADD
ADDC
SUBB
INC
DEC
20 | P a g e
Microcontrollers GOGA SUBRAMANYAM
MUL
DIV
DA A
Logical Instructions
The next group of instructions are the Logical Instructions, which perform logical operations like AND,
OR, XOR, NOT, Rotate, Clear and Swap. Logical Instruction are performed on Bytes of data on a bit-by-
bit basis.
Mnemonics associated with Logical Instructions are as follows:
ANL
ORL
XRL
CLR
CPL
RL
RLC
RR
RRC
SWAP
Boolean or Bit Manipulation Instructions
As the name suggests, Boolean or Bit Manipulation Instructions will deal with bit variables. We know
that there is a special bit-addressable area in the RAM and some of the Special Function Registers (SFRs)
are also bit addressable.
The Mnemonics corresponding to the Boolean or Bit Manipulation instructions are:
CLR
SETB
MOV
JC
JNC
JB
JNB
JBC
ANL
ORL
CPL
NOP
LCALL
ACALL
RET
RETI
JMP
======================================================================
5.16MATRIX KEYPAD
Matrix Keypads are commonly used in calculators, telephones etc where a number of input switches are
required. We know that matrix keypad is made by arranging push button switches in row and columns. In
the straight forward way to connect a 4×4 keypad (16 switches) to a microcontroller we need 16 inputs
pins. But by connecting switches in the following way we can read the status of each switch using 8 pins
of the microcontroller.
4×4-Matrix-Keypad
The status of each keys can be determined by a process called Scanning. For the sake of explanation lets
assume that all the column pins (Col1 – Col4) are connected to the inputs pins and all the row pins are
connected to the output pins of the microcontroller. In the normal case all the column pins are pulled up
(HIGH state) by internal or external pull up resistors. Now we can read the status of each switch through
scanning.
1. A logic LOW is given to Row1 and others (Row2 – Row-4) HIGH
2. Now each Column is scanned. If any switch belongs to 1st row is pressed corresponding column
will pulled down (logic LOW) and we can detect the pressed key.
3. This process is repeated for all rows.
If you need to save more pins of your microcontroller then you can interface keypad using the ADC
module of your microcontroller.
Interfacing with 8051 Microcontroller
Circuit diagram
Interfacing Keypad with 8051 Microcontroller using Keil C
10KΩ resistor and 10μF will provide the required Power On Reset (POR) signal to the 8051
microcontroller. 12MHz crystal is used to provide required clock for the microcontroller and 22pF
capacitors will stabilize the oscillations of the crystal. AT89C51 can works upto 24MHz. We can choose
the required frequency by changing the crystal and clock frequency in the project settings of Keil C.
Keypad is connected to the Port P1 and column inputs pins are pulled up internally. 16×2 LCD is
22 | P a g e
Microcontrollers GOGA SUBRAMANYAM
connected to Port P2 and P0. P0.0 and P0.1 pins are pulled up externally using 10KΩ resistors since Port
P0 has no internal pull up.
=========================================================================
5.17ADC (ANALOG TO DIGITAL CONVERTER INTERFACING TO 8051 )
ADC is the Analog to Digital converter, which converts analog data into digital format; usually it is
used to convert analog voltage into digital format. Analog signal has infinite no of values like a sine
wave or our speech, ADC converts them into particular levels or states, which can be measured in
numbers as a physical quantity. Instead of continuous conversion, ADC converts data periodically, which
is usually known as sampling rate. Telephone modem is one of the examples of ADC, which is used for
internet, it converts analog data into digital data, so that computer can understand, because computer can
only understand Digital data. The major advantage, of using ADC is that, we noise can be efficiently
eliminated from the original signal and digital signal can travel more efficiently than analog one. That‘s
the reason that digital audio is very clear, while listening.
ADC 0808/0809 pins
IN0-IN7: Analog Input channels
D0-D7: Data Lines
A, B, C: Analog Channel select lines; A is LSB and C is MSB
OE: Output enable signal
ALE: Address Latch Enable
EOC: End of Conversion signal
Vref+/Vref-: Differential Reference voltage input
Clock: External ADC clock input
23 | P a g e
Microcontrollers GOGA SUBRAMANYAM
ALGORITHM
1. Start.
2. Select the channel.
3. A Low – High transition on ALE to latch in the address.
4. A Low – High transition on Start to reset the ADC‘s SAR.
5. A High – Low transition on ALE.
6. A High – Low transition on start to start the conversion.
7. Wait for End of cycle (EOC) pin to become high.
8. Make Output Enable pin High.
9. Take Data from the ADC‘s output
10. Make Output Enable pin Low.
11. Stop
The total numbers of lines required are:
========================================================================
5.18DAC INTERFACING TO 8051
requires analog signal as they don't accept digital data thus making it necessary to use DAC which
converts digital data into equivalent analog voltage.
-bit DAC 0808. This IC converts digital data into equivalent analog
Current. Hence we require an I to V converter to convert this current into equivalent voltage.
24 | P a g e
Microcontrollers GOGA SUBRAMANYAM
Ex:
1. IF data =00H [00000000], Vref=
Different Analog output voltages for different Digital signal is given as:
Interfacing Diagram
25 | P a g e