Professional Documents
Culture Documents
Unit 1
Unit 1
Introduction
1 Concept of microcontroller
2 Microprocessor and Microcontroller
3 Comparison of all 8- bit microcontrollers
4 8051 architecture
5 Pin Diagram
6 Memory Organization
7 Special Function Registers
8 Internal Structure of I/O ports
9 Interfacing of 8051 with external memory
1. Concept of Microcontroller
An embedded computer is a computer that is implemented for a
particular purpose. But in comparison PC computer serves a number of
purposes like checking mails , surfing the internet, checking email ,
listening to music , word processing etc.
The MCU contains a flash memory where it stores its program . The
flash memory is easily erased and a new program can burned. This
makes it very flexible. MCUs can be programmed few thousand times
before they die.
The features of 8051 microcontroller are :
ALU
Microprocessor Microcontroller
Single memory map for data and Separate memory map for data
code(program) and code (program)
SWAP F REGHI,W 1 1
IORWF REG LO 1 1
0.4µs
ST 62
LD A,REGHI 2 4
RLC A 1 4
RLC A 1 4
RLC A 1 4
RLC A 1 4
ADD A,REGLO 1 4
LD REGLO,A 1 4
10 28
10 28
45.5µs
Z86CXX
SWAP REGHI 2 8
OR REGHI,REGLO 2 6
10 28
5.33 µs
COP 800
SWAP REGHI 1 2
OR REGHI,REGLO 1 1
OR A,[B] 1 1
X A,[B] 1 1
4 5
5 µs
MC68HC05
LDA REG HI 2 3
ROLA 1 3
ROLA 1 3
ROLA 1 3
ROLA 1 3
ADD REGLO 2 3
STA REGLO 2 4
10 22
10.5µsec
8051
MOV A,Rx 1 1
SWAP 1 1
ORL A,Ry 1 1
MOV Ry,A 1 1
4 4
2.4µs
LOOP CONTROL
This example is one of simple loop control where the register containing a
loop count is decremented, tested for zero, and if not zero, then branched
back to the beginning of the loop.
Byte/Words Byte/Words
Cycles Cycles
2 2
3/2 6
0.6 µs 6 µs
ST 62 MC68HC05
Byte/Words Bytes/Words
Cycles Cycles
DEC X 1 DEC X 1
4 3
2 3
6 6
2.86µs
9.75 µs
Z86CXX 8051
1.2µs
1.67 µs
This example tests a single bit in a register or a RAM location and makes a
conditional branch. We assume that the MSb is tested and a branch is to be
taken if the bit is set
Byte/Words Byte/Words
Cycles Cycles
2 2
3/2 6
0.6 µs 4 µs
ST 62 MC68HC05
Byte/Words Bytes/Words
Cycles Cycles
8.125 2.38µs
µs
Z86CXX 8051
Byte/Words Bytes/Words
Cycles Cycles
BTJRT NEWADD,REG,7 3
16/18
4µs
0.6 µs
MOV A,@Rx 1 1
JB A.7 , NEWADD 3 2
SOFTWARE TIMER
PIC 16C5X/XX
Byte/Word Cycles
MOVWF COUNT 2 1 1
CLRF COUNT1 1 1
GOTO LOOP 1 2
GOTO LOOP 1 1
RET 1 1
COP 800
Byte/Word Cycles
LD B, #0EH 1 1
DRSZ B 1 1
GOTO LOOP 1 2
GOTO LOOP 1 1
RET 1 1
ST62
Byte/Word Cycles
LDI A, #FF 2 4
LD X,A 1 4
LDI A,#04 2 4
LD Y,A 1 4
JRNZ LOOP 1 4
DEC Y 1 2
JRNZ LOOP 1 4
RET 1 2
10
4. 8051 architecture
Figure 1 8051 architecture.
It is an 8-bit microcontroller.
It is built with 40 pins DIP (dual inline package), 4kb of ROM storage and
128 bytes of RAM storage, 2 16-bit timers.
It consists of are four parallel 8-bit ports, which are programmable as well
as addressable depending on the requirement.
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
CY AC F0 RS1 RS0 OV - P
CY - carry flag
OV – overflow
P - parity
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.
8051 Microcontroller has both Internal ROM and Internal RAM. If the internal
memory is inadequate, you can add external memory using suitable circuits.
o For External Access Pin (EA Pin) must be pulled HIGH i.e.
when the EA Pin is high, the CPU first fetches instructions
from the Internal Program Memory in the address range of
0000H to 0FFFFH and if the memory addresses exceed the
limit, then the instructions are fetched from the external
ROM in the address range of 1000H to FFFFH.
There is also an alternative method to fetch the instructions that is ignore the
Internal ROM and fetch all the instructions only from External Program
Memory(External ROM).
For this purpose EA pin must be connected to GND . In this case the memory
addresses of external ROM will be from 0000H to FFFFH.
Data Memory (RAM) of 8051 Microcontroller
The 4 banks are named as Bank0, Bank1, Bank2 and Bank3. Each Bank
consists of 8 registers named as R0 – R7. Each Register can be addressed in
two ways: by name or by address.
To address the register by name, first the corresponding Bank has to be
selected. In order to select the bank, use RS0 and RS1 bits of the Program
Status Word (PSW) Register (RS0 and RS1 are 3 rd and 4th bits in the PSW
Register).
When addressing the Register using its address say 12H for example, the
corresponding Bank may or may not be selected. (12H corresponds to R2 in
Bank2).
The next 16B of the RAM i.e. from 20H to 2FH are Bit – Addressable memory
locations. There are totally 128 bits that can be addressed individually using
00H to 7FH or the entire byte can be addressed as 20H to 2FH.
The final 80B of the internal RAM addresses from 30H to 7FH, is the general
purpose RAM area which are byte addressable.
These lower 128B of RAM can be addressed directly or indirectly.
SRFs Memory addresses are only direct addressable. Even though some of
the addresses between 80H and FFH are not assigned to any SFR, they
cannot be used as additional RAM area.
In some microcontrollers, there is an additional 128B of RAM, which share the
memory address with SFRs that is 80H to FFH. But, this additional RAM block
is only accessed by indirect addressing.
They are:
A or Accumulator (ACC)
The Accumulator or Register A is the most important and most used 8051
Microcontroller SFRs. The Register A is located at the address E0H in the SFR
memory space. The Accumulator is used to hold data for almost all ALU Operations.
0 0 0 0 0 0 0
0
MSB LSB
B (Register B)
The B Register is used along with the accumulator in Multiplication and Division
operations. These two operations are performed on data that are stored only in
Registers A and B. During Multiplication Operation, one of the operand either the
multiplier or multiplicand is stores in B Register and also the higher byte of the
result.
In case of Division Operation, the B Register holds the divisor and also the
remainder of the result. It can be used as a General Purpose Register for normal
operations and is often used as an Auxiliary Register by Programmers to store
temporary results.
0 0 0 0 0 0 0 0
The PSW or Program Status Word Register is called as Flag Register is one of the
important SFRs. The PSW Register consists of Flag Bits, which help the programmer
in checking the condition of the result and make decisions.
Flags are 1-bit storage elements that store and indicate the nature of the result that
is generated by execution of certain instructions. The following image shows the
contents of the PSW Register.
0 0 0 0 0 0 0
0
RS1 RS0
Bank
0 0
Bank 0
0 1
Bank1
1 0
Bank2
1 1
Bank3
The Data Pointer is a 16-bit Register and is physically the combination of DPL (Data
Pointer Low) and DPH (Data Pointer High) SFRs. The Data Pointer can be used as a
single 16-bit register (as DPTR) or two 8-bit registers (as DPL and DPH).
DPTR doesn’t have a physical Memory Address but the DPL (Lower Byte of DPTR)
and DPH (Higher Byte of DPTR) have separate addresses in the SFR Memory Space.
DPL = 82H and DPH = 83H.
DPH DPL
DPTR
DPL
DPH
Stack Pointer (SP)
SP or Stack Pointer points to the top of the Stack and it indicates the next data to
be accessed. Stack Pointer can be accessed using PUSH, POP, CALL and RET
Instructions. The Stack Pointer is an 8-bit register and upon reset, the Stack Pointer
is initialized with 07H.
When writing a new data byte into the stack, the SP (Stack Pointer) is automatically
incremented by 1 and the new data is written at an address SP+1. When reading
data from stack, the data is retrieved from the Address in SP and after that the SP is
decremented by 1 (SP-1).
0 0 0 0 0 0 0 0
The 8051 Microcontroller four Ports which can be used as Input and/or Output.
These four ports are P0, P1, P2 and P3. Each Port has a corresponding register with
same names (the Port Registers are also P0, P1, P2 and P3). The addresses of the
Port Registers are as follows: P0 – 80H, P1 – 90H, P2 – A0H and P2 – B0H.
Each bit in these SFRs corresponds to one physical Pin in the 8051 Microcontroller.
All these Port Registers are both Bit Addressable and Byte Addressable. Writing 1 or
0 on a Port Register Bit will reflect as an appropriate voltage (5V and 0V) on the
corresponding Pin.
If a Port Bit is SET (declared as 1), the corresponding Port Pin will be configured as
Input and similarly if a Port Bit is CLEARED (declared as 0), the corresponding Port
Pin is configured as Output. Upon reset, all the Port Bits are SET (1) and hence, all
the Port Pins are configured as Inputs.
1 1 1 1 1 1 1
1
P0
1 1 1 1 1 1 1
1
P1
1 1 1 1 1 1 1
1
P2
1 1 1 1 1 1 1
1
The PCON or Power Control register,is used to control the 8051 Microcontroller’s
Power Modes and is located at 87H of the SFR Memory Space. Using two bits in the
PCON Register, the microcontroller can be set to Idle Mode and Power Down Mode.
During Idle Mode, the Microcontroller will stop the Clock Signal to the ALU (CPU) but
it is given to other peripherals like Timer, Serial, Interrupts, etc. In order to
terminate the Idle Mode, use an Interrupt or Hardware Reset.
In the Power Down Mode, the oscillator will be stopped and the power will be
reduced to 2V. To terminate the Power Down Mode, use the Hardware Reset.
Apart from these two, the PCON Register can also be used for few additional
purposes. The SMOD Bit in the PCON Register is used to control the Baud Rate of
the Serial Port.
There are two general purpose Flag Bits in the PCON Register, which can be used by
the programmer during execution.
0 0 0 0 0 0 0
0
The Serial Control or SCON SFR is used to control the 8051 Microcontroller’s Serial
Port. It is located at an address of 98H. Using SCON, helps in the control the
Operation Modes of the Serial Port, Baud Rate of the Serial Port and Send or Receive
Data using Serial Port.
SCON Register also consists of bits that are automatically SET when a byte of data
is transmitted or received.
0 0 0 0 0 0 0
0
MSB LSB
SCON
Timer Control or TCON Register is used to start or stop the Timers of 8051
Microcontroller. It also contains bits to indicate if the Timers has overflowed. The
TCON SFR also consists of Interrupt related bits.
0 0 0 0 0 0 0
0
MSB LSB
TMOD (Timer Mode)
The TMOD or Timer Mode register or SFR is used to set the Operating Modes of the
Timers T0 and T1. The lower four bits are used to configure Timer0 and the higher
four bits are used to configure Timer1.
0 0 0 0 0 0 0
0
The Gatex bit is used to operate the Timerx with respect to the INTx pin or
regardless of the INTx pin.
The C/Tx bit is used selects the source of pulses for the Timer to count.
C/T1 = 1 ==> Timer1 counts pulses from Pin T1 (P3.5) (Counter Mode)
C/T1 = 0 ==> Timer1 counts pulses from internal oscillator (Timer Mode)
C/T0 = 1 ==> Timer0 counts pulses from Pin T0 (P3.4) (Counter Mode)
C/T0 = 0 ==> Timer0 counts pulses from internal oscillator (Timer Mode)
0 0 0 0 0 0 0 0
MSB LSB
IP (Interrupt Priority)
The IP or Interrupt Priority Register is used to set the priority of the interrupt as High
or Low. If a bit is CLEARED, the corresponding interrupt is assigned low priority and
if the bit is SET, the interrupt is assigned high priority.
0 0 0 0 0 0 0 0
MSB
LSB
The Serial Buffer or SBUF register is used to hold the serial data while transmission
or reception.
MSB LSB
TL0/TH0 (Timer 0 Low/High)
The Timer 0 consists of two SFRs: TL0 and TH0. The TL0 is the lower byte and the
TH0 is the higher byte and together they form a 16-bit Timer0 Register.
TH0 TL0
TIMER 0
TL0
TH0
The TL1 and TH1 are the lower and higher bytes of the Timer 0.
TH1 TL1
TIMER 1
TL1
TH1
All 8051 microcontrollers have 4 I/O ports each comprising 8 bits which can be
configured as inputs or outputs. Accordingly, in total of 32 input/output pins
enabling the microcontroller to be connected to peripheral devices are available for
use.
Output pin A logic zero (0) is applied to a bit of the P register. The output FE
transistor is turned on, thus connecting the appropriate pin to ground.
Input pin A logic one (1) is applied to a bit of the P register. The output FE transistor
is turned off and the appropriate pin remains connected to the power supply voltage
over a pull-up resistor of high resistance.
Logic state (voltage) of any pin can be changed or read at any moment. A logic zero
(0) and logic one (1) are not equal. A logic one (0) represents a short circuit to
ground. Such a pin acts as an output. A logic one (1) is “loosely” connected to the
power supply voltage over a resistor of high resistance. Since this voltage can be
easily “reduced” by an external signal, such a pin acts as an input.
Port 0
The P0 port is characterized by two functions. If external memory is used then the
lower address byte (addresses A0-A7) is applied on it. Otherwise, all bits of this port
are configured as inputs/outputs.
If any pin of this port is configured as an input then it acts as if it “floats”. Such an
input has unlimited input resistance and in determined potential.
When the pin is configured as an output, it acts as an “open drain”. By applying
logic 0 to a port bit, the appropriate pin will be connected to ground (0V). By
applying logic 1, the external output will keep on “floating”. In order to apply logic 1
(5V) on this output pin, it is necessary to built in an external pull-up resistor.
Port 1
P1 is a true I/O port, because it doesn't have any alternative functions as is the case
with P0, but can be configured as general I/O only. It has a pull-up resistor built-in
and is completely compatible with TTL circuits.
Port 2
P2 acts similar to P0 when external memory is used. Pins of this port occupy
addresses intended for external memory chip. This time it is about the higher
address byte with addresses A8-A15. When no memory is added, this port can be
used as a general input/output port showing features similar to P1.
Port 3
All port pins can be used as general I/O, but they also have an alternative function.
In order to use these alternative functions, a logic one (1) must be applied to
appropriate bit of the P3 register. In terms of hardware, this port is similar to P0,
with the difference that its pins have a pull-up resistor built-in.
The design for 8051 Microcontroller based system is not limited to the internal
RAM and ROM present in the 8051 Microcontroller. There is also 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, do not have any internal
ROM but have to be interfaced with external ROM.
A maximum of 64B of Program Memory (ROM) and Data Memory (RAM) each can be
interfaced with the 8051 Microcontroller.
Consider the following block diagram of interfacing 64KB of External RAM and 64KB
of External ROM with the 8051 Microcontroller.
References: