MC 18ec46 Mod1
MC 18ec46 Mod1
MC 18ec46 Mod1
Institute of Technology
Approved by AICTE, Affiliated to VTU, Accredited as Grade A Institution by NAAC.
All UG branches – CSE, ECE, EEE, ISE &Mech.E Accredited by NBA for academic years 2018-19 to 2020-21 & valid upto 30.06.2021
Post box no. 7087, 27th cross, 12th Main, Banashankari 2nd Stage, Bengaluru- 560070, INDIA
Ph: 91-80- 26711780/81/82 Email: [email protected], www. bnmit.org
Microprocessor contains ALU, General purpose Microcontroller contains the circuitry of microprocessor,
registers, stack pointer, program counter, clock and in addition it has built in ROM, RAM, I/O Devices,
timing circuit and interrupt circuit Timers/Counters etc.
It has many instructions to move data between It has few instructions to move data between memory and
memory and CPU CPU
Less number of pins are multifunctional More number of pins are multifunctional
Large number of instructions with flexible Limited number of instructions with few
addressing modes addressing modes
In an embedded system, there is only one application software that is typically burned into ROM.
A PC contains or is connected to various embedded products such as keyboard, printer, modem,
disk controller, sound card, CD-ROM driver, mouse and so on. Each one of these peripheral has a
microcontroller inside it that performs only one task. For example inside every mouse the
microcontroller that performs the task of finding the mouse position and sending it to PC.
Some of Embedded Products using Microcontroller are:
Home
Appliances, intercom, telephones, security systems, garage door openers, answering machines, fax
machines, home computers, TVs, cable TV tuner, VCR, camcorder, remote controls, video games,
cellular phones, musical instruments, sewing machines, lighting control, paging, camera, pinball
machines, toys and exercise equipment.
Office
Telephones, computers, security systems, fax, machines, microwave, copier, laser printer, color,
printer and paging
Auto
Trip computer, engine control, air bag, ABS, instrumentation, security system, transmission,
control, entertainment, climate control, cellular, phone and keyless entry.
Q3. Discuss on types of microcontrollers.
Microcontrollers can be classified on the basis of internal bus width, architecture, memory and
instruction set as 4-bit, 8-bit,16-bit and 32-bit microcontrollers.
4-bit Microcontrollers:
These 4-bit microcontrollers are small size, minimum pin count and low cost controllers which are
widely used for low end applications like LED & LCD display drivers, portable battery chargers
etc.. Their power consumption is also low. The popular 4-bit controllers are Renasa M34501 which
is a 20 pin DIP chip with 4kB of ROM, 256 Bytes of RAM,2-Counters and 14 I/O Pins. Similarly
ATAM862 series from ATMEL.
8-bit Microcontrollers:
These are the most popular and widely used microcontrollers .About 55% of all CPUs sold in the
world are 8-bit microcontrollers only. The 8-bit microcontroller has 8-bitinternal bus and the ALU
performs all the arithmetic and logical operations on a byte instruction. The well known 8-bit
microcontroller is 8051 which was designed by Intel in the year 1980 for the use in embedded
systems. Other 8-bit microcontrollers are Intel 8031/8052 and Motorola MC68HC11 and AVR
Microcontrollers, Microchip’s PIC Microcontrollers 12C5XX ,16C5X and 16C505 etc...
16-bit Microcontrollers:
When the microcontroller performs 16-bit arithmetic and logical operations at an instruction, the
microcontroller is said to be a 16-bit microcontroller. The internal bus width of 16-bit
microcontroller is of 16-bit. These microcontrollers are having increased memory size and speed
of operation when compared to 8-bit microcontrollers. These are most suitable for Department of
ECE Unit 3 - RGCET ECT53 - Microprocessors and Microcontrollers 6 programming in High
level languages like C or C++ .They find applications in disk drivers, modems, printers, scanners
and servomotor control. Examples of 16-bit microcontrollers are Intel 8096 family and Motorola
MC68HC12 and MC68332 families, the performance and computing capability of 16 bit
microcontrollers are enhanced with greater precision as compared to the 8-bit microcontrollers.
32-Bit Microcontrollers:
These microcontrollers used in high end applications like Automotive control, Communication
networks, Robotics, Cell phones ,GPRS & PDAs etc..For EX: PIC32, ARM 7, ARM9, SHARP
LH79520, ATMEL 32 (AVR), Texas Instrument’s –. TMS320F2802x/2803x etc.. are some of the
popular 32-bit microcontrollers.
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.
CY AC F0 RS1 RS0 OV - P
CY - Carry flag
AC - Auxiliary carry flag
F0 - Available to the user for general purpose
RS1, RS0 -Register bank select bits
OV - Overflow
P - Parity
Stack Pointer (SP) – it contains the address of the data item on the top of the
stack. Stack may reside anywhere on the internal RAM. On reset, SP is
initialized to 07 so that the default stack will start from address 08 onwards.
Data Pointer (DPTR) – DPH (Data pointer higher byte), DPL (Data pointer
lower byte). This is a 16 bit register which is used to furnish address
information for internal and external program memory and for external data
memory.
Q6. Draw the pin diagram of 8051 Microcontroller and explain the operation of pins.
Pin 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 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).
Q7. Explain 8051 Flag Bits and PSW Register with example.
Flag register in 8051 is called as program status word (PSW) used to indicate the Arithmetic
condition of ACC. This special function register PSW is also bit addressable and 8 bit wide means
each bit can be set or reset independently.
The meaning of various bits of PSW register is shown below.
CY PSW0.7 Carry Flag
AC PSW0.6 Auxiliary Carry Flag
F0 PSW0.5 Flag 0 available for general purpose.
RS1 PSW0.4 Register Bank select bit 1
RS0 PSW0.3 Register bank select bit 0
OV PSW0.2 Overflow flag
PSW0.1 User definable flag
P PSW0.0 Parity flag .set/cleared by hardware.
P → Parity flag → PSW 0.0
Odd number of 1 in ACC, P=1
Even number of 1 in ACC, P=0
• OV (PSW 0.2) → overflow flag → this is used to detect error in signed arithmetic operation.
The bits PSW3 and PSW4 are denoted as RS0 and RS1 and these bits are used to select the bank
registers of the RAM location.
The selection of the register Banks and their addresses are given below.
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.
Internal RAM organization
R7 1F
R6 1E
R5 1D
BANK 3
7F 78 7F
R4 1C
R3 1B
2F
77 70 7E
2E
R2 1A
6F 68 .
R1 19 2D
R0 18 67 60 .
.
2C
R7 17 5F 58
R6 16 2B
57 50
.
R5 15
BANK 2
R4 14
2A
4F 48
.
R3 13
29
47 40
.
R2 12
28
3F 38
.
R1 11 27
.
R0 10 26
37 30
R7 0F 2F 28 32
R6 0E 25
R5 0D 24
27 20 31
BANK 1
R4 0C 23 1F 18 30
R3 0B 17 10
R2 0A 22
R1 09 21 0F 08
R0 08 20 07 00 General purpose memory
R7 07
R6 06
R5 05
BANK 0
R4 04
R3 03 Bit addressable memory
R2 02
R1 01
R0 00
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 -2F. 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.
Q9. 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.
PSEN PSEN
A14 A14
A13 A13
A13
A12 A12
A12
… .. 32
A9 .
A8
A8
. Kbyte
WE
WR
OE
16 .
RAM
RD
AL A7 Kbyte A3
LE A2
8051 A0-A7
.. RAM A0-A7 A1
A1
A0
A0
LOWER BYTE
EA ADDRESS
GND [AD0 – AD7] DAT DAT
A A
O/P O/P
AD0
- DATA BUS [AD0 –
AD7 AD7]
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.
A stack is a last in first out memory. In 8051 internal RAM space can be used as stack. The
address of the stack is contained in a register called stack pointer. Instructions PUSH and
POP are used for stack operations. When a data is to be placed on the stack, the stack
pointer increments before storing the data on the stack so that the stack grows up as data is
stored (pre-increment). As the data is retrieved from the stack the byte is read from the
stack, and then SP decrements to point the next available byte of stored data (post
decrement). The stack pointer is set to 07 when the 8051 resets. So that default stack
memory starts from address location 08 onwards (to avoid overwriting the default register
bank ie., bank 0).
Each port of 8051 has bidirectional capability. Port 0 is called 'true bidirectional port' as it floats
(tristated) when configured as input. Port -1, 2, 3 are called 'quasi bidirectional port'.
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.
Let us assume that control is '0'. When the port is used as an input port, '1' is written to the latch. In
this situation both the output MOSFETs are 'off'. Hence the output pin floats. This high impedance
pin can be pulled up or low by an external source. When the port is used as an output port, a '1'
written to the latch again turns 'off' both the output MOSFETs and causes the output pin to float. An
external pull-up is required to output a '1'. But when '0' is written to the latch, the pin is pulled down
by the lower MOSFET. Hence the output becomes zero.
When the control is '1', address/data bus controls the output driver MOSFETs. If the address/data bus
(internal) is '0', the upper MOSFET is 'off' and the lower MOSFET is 'on'. The output becomes '0'. If
the address/data bus is '1', the upper transistor is 'on' and the lower transistor is 'off'. Hence the
output is '1'. Hence for normal address/data interfacing (for external memory access) no pull-up
resistors are required.
Port-0 latch is written to with 1's when used for external memory access.
Q12. Explain Port-1 of 8051 Microcontroller.
Port-1 Pin Structure
Port-1 has 8 pins (P1.1-P1.7) .The structure of a port-1 pin is shown in figure 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.
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.
P3.0 RxD
P3.1 TxD
P3.2
P3.3
P3.4 T0
P3.5 T1
P3.6
P3.7
Port-0 can drive 8 LS TTL inputs in address /data mode. For digital output port, it needs external
pull-up resistors.
Each Port 3 bit can be configured either as a normal I/O or as a special function bit.
The status of the output port pin is sometimes dependant on the connected load. For instance if a
port is configured as an output port and a '1' is written to the latch, the output pin should also show
'1'. If the output is used to drive the base of a transistor, the transistor turns 'on'.
If the port pin is read, the value will be '0' which is corresponding to the base-emitter voltage of
the transistor.
Reading a latch: Usually the instructions that read the latch, read a value, possibly change it, and
then rewrite it to the latch. These are called "read-modify-write" instructions. Examples of a few
instructions are-
ORL P2, A; P2 <-- P2 or A
In 8051 microcontroller there certain registers which uses the RAM addresses from 80h to FFh and
they are meant for certain specific operations .These registers are called Special function registers
(SFRs).Some of these registers are bit addressable also. The list of SFRs and their functional names
are given below. In these SFRs some of them are related to I/O ports (P0,P1,P2 and P3) and some of
them are meant for control operations (TCON,SCON, PCON..) and remaining are the auxiliary
SFRs, in the sense that they don't directly configure the 8051.
Q. 17 Write a note on ports of 8051 Microcontroller.
The 8051 microcontroller has four parallel I/O ports , each of 8-bits .So, it provides the user 32 I/O
lines for connecting the microcontroller to the peripherals. The four ports are P0 (Port 0), P1(Port1)
,P2(Port 2) and P3 (Port3). Upon reset all the ports are output ports. In order to make them input, all
the ports must be set i.e a high bit must be sent to all the port pins. This is normally done by the
instruction “SETB”.
PORT 0:
Port 0 is an 8-bit I/O port with dual purpose. If external memory is used, these port pins are used for
the lower address byte address/data (AD0-AD7), otherwise all bits of the port are either input or
output. Unlike other ports, Port 0 is not provided with pull-up resistors internally ,so for PORT0
pull-up resistors of nearly 10k are to be connected externally as shown in the fig.2.
Dual role of port 0:
Port 0 can also be used as address/data bus (AD0-AD7), allowing it to be used for both address and
data. When connecting the 8051 to an external memory, port 0 provides both address and data. The
8051 multiplexes address and data through port 0 to save the pins. ALE indicates whether P0 has
address or data. When ALE = 0, it provides data D0-D7, and when ALE =1 it provides address and
data with the help of a 74LS373 latch.
Port 1:
Port 1 occupies a total of 8 pins (pins 1 through 8). It has no dual application and acts only as input
or output port. In contrast to port 0, this port does not need any pull-up resistors since pull-up
resistors connected internally. Upon reset, Port 1 is configured as an output port. To configure it as
an input port, port bits must be set i.e a high bit must be sent to all the port pins. This is normally
done by the instruction “SETB”.
For Ex: MOV A, #0FFH ; A=FF HEX
MOV P1, A ; make P1 an input port by writing 1’s to all of its pins
Port 2 :
Port 2 is also an eight bit parallel port. (pins 21- 28). It can be used as input or output port. As this
port is provided with internal pull-up resistors it does not need any external pull-up resistors. Upon
reset, Port 2 is configured as an output port. If the port is to be used as input port, all the port bits
must be made high by sending FF to the port. For ex, MOV A, #0FFH ; A=FF hex MOV P2, A ;
make P2 an input port by writing all 1’s to it .
PORT 3:
Port3 is also an 8-bit parallel port with dual function.( pins 10 to 17). The port pins can be used for
I/O operations as well as for control operations. The details of these additional operations are given
below in the table. Port 3 also do not need any external pull-up resistors as they are provided
internally similar to the case of Port2 & Port 1. Upon reset port 3 is configured as an output port . If
the port is to be used as input port, all the port bits must be made high by sending FF to the port.
For ex, MOV A, #0FFH ; A= FF hex MOV P3, A ; make P3 an input port by writing all 1’s to it
Alternate Functions of Port 3: