Professional Documents
Culture Documents
Microprocessors/controllers?
Accumulator
I/O port
Register(s)
Interrupt
circuits
Internal RAM Internal ROM
Clock circuit
Stack Pointer
Program Counter
C1
18 XTAL2
19 XTAL1
C2
• Pins XTAL1 and XTAL2 are provided for
connecting a resonant network to form an
oscillator
• Typically quartz crystal and capacitors are
employed(previous figure)
• Crystal frequency is the basic internal clock
frequency of the microcontroller
• Varies from 1MHz to 16 MHz
• Minimum frequencies imply that some internal
memories are dynamic and must always operate
above a minimum frequency or data will be lost
Oscill P2 P1 P2 P1 P2 P1 P2 P1 P2 P1 P2 P1 P2
ator
freq
f
state1 state2 state3 state4 state5 state6
ALE
8051 timing
• Ceramic resonators may be used as a low-cost
alternative to crystal resonators
• Decreases in frequency stability and accuracy
make the ceramic resonator a poor choice if high
speed serial data communication with other
systems, or critical timing, is to be done
• The oscillator formed by the crystal, capacitors
and on-chip inverter generates a pulse train at
the frequency of the crystal is shown in the figure
• The clock frequency, f establishes the smallest
interval of time within the microcontroller, called
the pulse P, time
• The smallest interval of time to accomplish any
simple instruction, or part of a complex
instruction is the machine cycle
• One machine cycle=6 states
• A state is the basic time interval for discrete
operations of the microcontroller such as fetching
an opcode byte, decoding an opcode, executign
an opcode, or writing a data byte
• Two oscillator pulses define each state
• 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
• To calculate the time any particular instruction will
take to be executed, find the number of cycles, C
• The time to execute that instruction is then found
by multiplying C by 12 and dividing the product by
the crystal frequency
• Tinst =CX12d/crystal frequency
• Ex: if the crystal frequency is 16Mhz, then the time
to execute an ADD A,R1 one-cycle instruction is
.75microseconds
• A 12Mhz crystal yields the convenient time of
1microsecond per cycle
• An 11.0592Mhz crystal, yields a cycle frequency of
921.6Khz, which can be divided evenly by the
standard communication baud rates of
19200,9600,4800,2400,1200 and 300hertz
• There are 2 ALE pulses per machine cycle(prev,
fig)
• The ALE pulse, which is primarily used as a
timing pulse for external memory access,
indicates when every instruction byte is fetched
• 2 bytes of a single instruction may thus be
fetched and executed in one machine cycle
• However single byte instructions are not
executed in a half cycle
• Single byte instructions “throw-away” the second
byte(which is the first byte of the next
instruction)
• The next instruction is then fetched in the
following cycle
Program counter and data pointer
• 8051 contains two 16-bit registers: the program
counter(PC) and the data pointer(DPTR)
• Each is used to hold the address of a byte in
memory
• Program instruction bytes are fetched from
locations ain memory that are addressed by the
PC
• 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
• DPTR is made up of two 8 bit registers, DPH and
DPL which are used to furnish memory addresses
for internal and external code access and
external data access
• 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 and DPL
• DPTR doesn't have a single internal address
• DPL and DPH are each assigned an address
A and B CPU registers
• 8051 contains 34 general-purpose, or working
registers
• Two of these, registers A and B hold results of many
instructions, particularly math and logical operations,
of the 8051 CPU
• The other 32 are arranged as part of the internal
RAM in four banks, B0-B3, of 8 registers
• The A(accumulator) register is the more versatile of
the two CPU registers and 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 8051 and an external memory
• The B register is used with the A register for
multiplication and division operations and has no
other function other than as a location where
data may be stored
Flag & the Program status Word(PSW)
• Flags-1 bit registers provided to store the results of
certain program instructions
• Other instructions can test the condition of the flags
and make decisions based on the flag states
• PSW and PCON registers are used to address these
flags
• 8051 has 4 math flags that respond automatically to
the outcomes of math operations and 3 general
purpose user flags that can be set to 1 or cleared to
0 by the programmer as desired
• The math flags include C, AC,OV and P
• User flags are F0,GF0,GF1
• They are general purpose flags that may be used
by the programmer to record some event in the
program
• All of the flags can be set and cleared by the
programmer at will
• The math flags, however, are also affected by
math operations
PSW
7 6 5 4 3 2 1 0
PSW CY AC F0 RS1 RS0 OV - P
18
17
Bank 2
10
0F
Bank 1
08
Bank 0
Bit Addressable Memory
2F 7F 78
20h – 2Fh (16 locations X 8-bits
2E
= 128 bits)
2D
2C Bit addressing:
2B mov C, 1Ah
2A or
29 mov C, 23h.2
28
27
26
25
24
23 1A
22 10
21 0F 08
20 07 06 05 04 03 02 01 00
General Purpose registers
Byte
address
7F
30
The stack and the stack pointer
• 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 to
hold an internal RAM address called the Top of
the Stack
• The address held in the SP register is the location
in internal RAM where the last byte of data was
stored by a stack operation
• When data is to be placed on the stack, 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
Operation of the stack and SP
CPU CPU
action action
Storing data on the stack Internal RAM Getting data from the stack
• The SP is set to 07h when the 8051 is reset and
can be changed to any internal RAM address by
the programmer, using a data move command.
• The stack is limited in height to the size of the
internal RAM
• The stack has the potential to overwrite valuable
data in the register banks, bit addressable RAM
and scratch –pad RAM areas.
• The programmer is responsible for making sure
the stack doesn’t grow beyond predefined bounds
• The stack is normally placed high in internal RAM,
by an appropriate choice of the number placed in
the SP register, to avoid conflict with the register,
bit and the scratch-pad internal RAM areas
Special function registers
• The 8051 operations that do not use the internal
128-byte RAM addresses from 00h-7Fh are done by
a group of specific internal registers, each called a
Special function register(SFR), which may be
addressed much like internal RAM, using addresses
from 80h-FFh
• Some SFRs are bit addressable.
• This feature allows the programmer to change only
what needs to be altered, leaving the remaining bits
in that SFR unchanged
• Not all of the addresses from 80h-FFh are used for
SFRs, and attempting to use an address that is not
defined, or empty, results in unpredictable results.
SFRs and their addresses
0 1 2 3 4 5 6 7
F8 FF
F0 B F7
E8 EF
E0 ACC E7
D8 DF
D0 PSW D7
C8 CF
C0 C7
B8 IP BF
B0 P3 B7
A8 IE AF
A0 P2 A7
98 SCON SBUF 9F
90 T1 97
88 TCON TMOD TL0 TL1 TH0 TH1 8F
80 T0 SP DPL DPH PCON 87
• PC is not part of the SFR and has no internal RAM
address
• SFRs are named in certain op codes by their
functional names, such as A or TH0, and are
referenced by other op codes by their addresses,
such as 0E0h or 8ch
Internal ROM
• 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
• Block of internal program code, contained in an
internal ROM, occupies code address space of
0000h-0FFFh
• The PC is ordinarily used to address program code
bytes from addresses 0000h-FFFFh
• When the Program address exceeds 0FFFh, the
internal ROM capacity, 8051 automatically fetches
the code bytes from external program memory
• Code bytes can also be fetched exclusively from an
external memory by connecting the external access
pin (EA) to ground
Input/output pins, ports and circuits
TB1
Read pin P0.x
8051 IC
Writing “1” to Output Pin P1.X
TB1
Read pin
8051 IC
Writing “0” to Output Pin P1.X
TB1
Read pin
8051 IC
Reading “High” at Input Pin
1 1 P1.X pin
Internal CPU bus D Q
P1.X
0 M1
Write to latch Clk Q
TB1
Read pin
3. Read pin=1 Read latch=0
Write to latch=1
8051 IC
Reading “Low” at Input Pin
TB1
Read pin
3. Read pin=1 Read latch=0
Write to latch=1
8051 IC
• To aid in speeding up switching times when the
pin is used as an output, the internal FET pullup
has another FET in parallel with it
• The second FET is turned on for 2 oscillator time
periods during a low-to-high transition on the pin
• This arrangement provides a low impedance path
to the positive voltage supply to help reduce rise
times in charging any parasitic capacitances in
the external circuitry
Port2
• Port 2 may be used as in input/output port
similar in operation to port1
• The alternate use of port 2 is to supply a high-
order address byte in conjunction with the port 0
low-order byte to address external memory
• Port2 pins are momentarily changed by the
address control signals when supplying the high
byte of a 16-bit address
• Port 2 latches remain stable when external
memory is addressed, as they do not have to be
turned around for data input as is the case for
port 0
Port 3
• Port3 is an input/output similar to port1
• The input and output functions can be programmed
under the control of the P3 latches or under the
control of various other special function registers
• The port3 alternate uses are shown below.
Pin Alternate Use SFR
P3.0-RXD Serial data input SBUF
P3.1-TXD Serial data output SBUF
P3.2-INT0 External Interrupt0 TCON.1
P3.3-INT1 External interrupt1 TCON.3
P3.4-T0 External timer0 input TMOD
P3.5-T1 External timer1 input TMOD
P3.6-WR External memory write ---
pulse
P3.7-RD External memory read pulse ---
External Memory
• The system designer is not limited by the amount
of internal RAM and ROM available on chip
• Two separate external memory spaces are made
available by the 16-bit PC and DPTR and by
different control pins for enabling external ROM
and RAM chips
• Internal control circuitry accesses the correct
physical memory, depending on the machine
cycle state and the opcode being executed
• External RAM, which is accessed by the DPTR,
may also be needed when 128 bytes of internal
data storage is not sufficient
• External RAM, upto 64k, may also be added to
any chip in the 8051 family
• There are several reasons for adding external
memory, particularly Program Memory, when
applying the 8051 in a system.
• When project is in the prototype stage, having a
masked internal ROM for each program “try” is
prohibitive.
• To help the programmer the manufacturers make
available an EPROM version, the 8751, which has
4K of on-chip EPROM that may be programmed
and erased as needed as the program is
developed
• External program memory is fetched if either of
the following two conditions are satisfied.
External program memory is fetched if either of
the following two conditions are satisfied.
• EA is low. The microcontroller by default starts
searching for program from external program
memory.
• PC is higher than FFFH for 8051 or 1FFFH for 8052.
• PSEN tells the outside world whether the external
memory fetched is program memory or data
memory.
• EA is user configurable.
• PSEN is processor controlled