You are on page 1of 18

Features

8-Bit CPU Optimized for Control Applications


Extensive Boolean Processing Capabilities (Single-Bit Logic)
On-Chip Flash Program Memory
On-Chip Data RAM
Bidirectional and Individually Addressable I/O Lines
Multiple 16-Bit Timer/Counters
Full Duplex UART
Multiple Source/Vector/Priority Interrupt Structure
On-Chip Clock Oscillator
On-chip EEPROM (AT89S series)
SPI Serial Bus Interface (AT89S Series)
Flash
Watchdog Timer (AT89S Series)
The basic architectural structure of the AT89C51 core is shown in Figure 1.
Microcontroller
Block Diagram
Figure 1. Block Diagram of the AT89C core
Architectural
EXTERNAL
INTERRUPTS
Overview
ETC.
ON-CHIP
FLASH TIMER 1
INTERRUPT ON-CHIP COUNTER
CONTROL RAM TIMER 0 INPUTS

CPU

OSC BUS 4 I/O PORTS SERIAL


CONTROL PORT

TXD RXD
P0 P2 P1 P3

ADDRESS/DATA

For more information on the individual devices and features, refer to the Hardware
Descriptions and Data Sheets of the specific device.

0497B-B12/97

2-3
Figure 2. Block Diagram of the AT89S core

Figure 3. AT89C51/LV51 and AT89C52/LV52 Memory Structure

PROGRAM MEMORY DATA MEMORY


(READ ONLY) (READ/WRITE)

FFFFH: FFFFH:

EXTERNAL
EXTERNAL

INTERNAL
FFH:

EA = 0 EA = 1
EXTERNAL INTERNAL

0000 00 0000

PSEN RD WR

2-4 Architectural Overview


Architectural Overview

Reduced Power Modes The interrupt service locations are spaced at 8-byte inter-
vals: 0003H for External Interrupt 0, 000BH for Timer 0,
To exploit the power savings available in CMOS circuitry,
0013H for External Interrupt 1, 001BH for Timer 1, and so
Atmels Flash microcontrollers have two software-invoked
on. If an interrupt service routine is short enough (as is
reduced power modes.
often the case in control applications), it can reside entirely
Idle Mode. The CPU is turned off while the RAM and within that 8-byte interval. Longer service routines can use
other on-chip peripherals continue operating. In this a jump instruction to skip over subsequent interrupt loca-
mode, current draw is reduced to about 15 percent of the tions, if other interrupts are in use.
current drawn when the device is fully active.
The lowest addresses of program memory can be either in
Power Down Mode. All on-chip activities are suspended, the on-chip Flash or in an external memory. To make this
while the on-chip RAM continues to hold its data. In this selection, strap the External Access (EA) pin to either VCC
mode, the device typically draws less than 15 A, and or GND.
can be as low as 0.6 A.
For example, in the AT89C51 with 4K bytes of on-chip
In addition, these devices are designed using static logic, Flash, if the EA pin is strapped to VCC, program fetches to
which does not require continuous clocking. That is, the addresses 0000H through 0FFFH are directed to the inter-
clock frequency can be slowed or even stopped while wait- nal Flash. Program fetches to addresses 1000H through
ing for an internal event. FFFFH are directed to external memory.
In the AT89C52 (8K bytes Flash), EA = V CC selects
Memory Organization addresses 0000H through 1FFFH to be internal and
addresses 2000H through FFFFH to be external.
Logical Separation of Program Data Memory
All Atmel Flash microcontrollers have separate address If the EA pin is strapped to GND, all program fetches are
spaces for program and data memory, as shown in Figure directed to external memory.
3. The logical separation of program and data memory The read strobe to external memory, PSEN, is used for all
allows the data memory to be accessed by 8-bit addresses, external program fetches. Internal program fetches do not
which can be more quickly stored and manipulated by an 8- activate PSEN.
bit CPU. Nevertheless, 16-bit data memory addresses can The hardware configuration for external program execution
also be generated through the DPTR register. is shown in Figure 5. Note that 16 I/O lines (Ports 0 and 2)
Program memory can only be read. There can be up to 64K are dedicated to bus functions during external program
bytes of directly addressable program memory. The read memory fetches. Port 0 (P0 in Figure 5) serves as a multi-
strobe for external program memory is the Program Store plexed address/data bus. It emits the low byte of the Pro-
Enable signal (PSEN). gram Counter (PCL) as an address and then goes into a
Data memory occupies a separate address space from pro- float state while waiting for the arrival of the code byte from
gram memory. Up to 64K bytes of external memory can be the program memory. During the time that the low byte of
directly addressed in the external data memory space. The the Program Counter is valid on P0, the signal ALE
CPU generates read and write signals, RD and WR, during (Address Latch Enable) clocks this byte into an address
external data memory accesses. latch. Meanwhile, Port 2 (P2 in Figure 5) emits the high
byte of the Program Counter (PCH). Then PSEN strobes
External program memory and external data memory can the external memory, and the microcontroller reads the
be combined by applying the RD and PSEN signals to the code byte.
input of an AND gate and using the output of the gate as
the read strobe to the external program/data memory. Figure 4. Program Memory

Program Memory
Figure 4 shows a map of the lower part of the program
memory. After reset, the CPU begins execution from loca-
tion 0000H.
As shown in Figure 4, each interrupt is assigned a fixed
location in program memory. The interrupt causes the CPU
to jump to that location, where it executes the service rou-
tine. External Interrupt 0, for example, is assigned to loca-
tion 0003H. If External Interrupt 0 is used, its service rou-
tine must begin at location 0003H. If the interrupt is not
used, its service location is available as general purpose
program memory.

2-5
Program memory addresses are always 16 bits wide, even Internal data memory is shown in Figure 7. The memory
though the actual amount of program memory used may be space is divided into three blocks, which are generally
less than 64K bytes. External program execution sacrifices referred to as the Lower 128, the Upper 128, and SFR
two of the 8-bit ports, P0 and P2, to the function of address- space.
ing the program memory.
Figure 7. Internal Data Memory
Figure 5. Executing from External Program Memory FFH FFH

ACCESSIBLE ACCESSIBLE
AT89 EXTERNAL UPPER BY INDIRECT BY DIRECT
PROGRAM 128 ADDRESSING ADDRESSING
MEMORY ONLY
80H 80H
P1 P0 INSTR. 7FH
ACCESSIBLE SPECIAL
EA BY DIRECT PORTS
LOWER FUNCTION
128 AND INDIRECT STATUS AND
REGISTERS
ALE ADDRESSING CONTROL BITS
TIMERS
LATCH ADDR 0 REGISTERS
STACK POINTER
P3 P2 ACCUMULATOR
(ETC.)
PSEN OE
Internal data memory addresses are always 1 byte wide,
which implies an address space of only 256 bytes. How-
ever, the addressing modes for internal RAM can in fact
Data Memory accommodate 384 bytes. Direct addresses higher than
The right half of Figure 3 shows the internal and external 7FH access one memory space, and indirect addresses
data memory spaces available on Atmels Flash microcon- higher than 7FH access a different memory space. Thus,
trollers. Figure 7 shows the Upper 128 and SFR space occupying
Figure 6 shows a hardware configuration for accessing up the same block of addresses, 80H through FFH, although
to 2K bytes of external RAM. In this case, the CPU exe- they are physically separate entities.
cutes from internal Flash. Port 0 serves as a multiplexed Figure 8 shows how the lower 128 bytes of RAM are
address/data bus to the RAM, and 3 lines of Port 2 are mapped. The lowest 32 bytes are grouped into 4 banks of 8
used to page the RAM. The CPU generates RD and WR registers. Program instructions call out these registers as
signals as needed during external RAM accesses. R0 through R7. Two bits in the Program Status Word
You can assign up to 64K bytes of external data memory. (PSW) select which register bank is in use. This architec-
External data memory addresses can be either 1 or 2 bytes ture allows more efficient use of code space, since register
wide. One-byte addresses are often used in conjunction instructions are shorter than instructions that use direct
with one or more other I/O lines to page the RAM, as addressing.
shown in Figure 6. Two-byte addresses can also be used, Figure 8. The Lower 128 Bytes of Internal RAM
in which case the high address byte is emitted at Port 2.
7FH
Figure 6. Accessing external data memory. If the program SCRATCH PAD
memory is internal, the other bits of P2 are available as I/O. AREA
30H
AT89 EXTERNAL
WITH DATA 2FH
INTERNAL MEMORY BANK BIT-ADDRESSABLE SPACE
P1 FLASH P0 DATA SELECT (BIT ADDRESSES 0-7F)
BITS IN 20H
EA VCC PSW

ALE
11 { 18H
1FH

LATCH ADDR 17H


10 { 10H 4 BANKS OF
8 REGISTERS
P3 P2 0FH R0-R7
RD
I/O PAGE
01 { 08H
WR WE OE
BITS RESET VALUE OF
07H
00 { 0 STACK POINTER

2-6 Architectural Overview


Architectural Overview

The next 16 bytes above the register banks form a block of Figure 10 gives a brief look at the Special Function Regis-
bit-addressable memory space. The microcontroller ter (SFR) space. SFRs include Port latches, timers, periph-
instruction set includes a wide selection of single-bit eral controls, etc. These registers can only be accessed by
instructions, and these instructions can directly address the direct addressing. In general, all Atmel microcontrollers
128 bits in this area. These bit addresses are 00H through have the same SFRs at the same addresses in SFR space
7FH. as the AT89C51 and other compatible microcontrollers.
All of the bytes in the Lower 128 can be accessed by either However, upgrades to the AT89C51 have additional SFRs.
direct or indirect addressing. The Upper 128 (Figure 9) can Sixteen addresses in SFR space are both byte- and bit-
only be accessed by indirect addressing. The Upper 128 addressable. The bit-addressable SFRs are those whose
bytes of RAM are only in the devices with 256 bytes of address ends in 000B. The bit addresses in this area are
RAM. 80H through FFH.
Figure 9. The Upper 128 Bytes of Internal RAM Figure 10. SFR Space

Byte Byte
address Bit address address Bit address
7F FF
F0 F7 F6 F5 F4 F3 F2 F1 F0 B

E0 E7 E6 E5 E4 E3 E2 E1 E0 ACC

General D0 D7 D6 D5 D4 D3 D2 D0 PSW
purpose
RAM B8 BC BB BA B9 B8 IP

B0 B7 B6 B5 B4 B3 B2 B1 B0 P3

A8 AF AC AB AA A9 A8 IE
30
A0 A7 A6 A5 A4 A3 A2 A1 A0 P2
2F 7F 7E 7D 7C 7B 7A 79 78
2E 77 76 75 74 73 72 71 70
99 not bit addressable SBUF
2D 6F 6E 6D 6C 6B 6A 69 68
98 9F 9E 9D 9C 9B 9A 99 98 SCON
Bit-addressable locations

2C 67 66 65 64 63 62 61 60
2B 5F 5E 5D 5C 5B 5A 59 58
90 97 96 95 94 93 92 91 90 P1
2A 57 56 55 54 53 52 51 50
29 4F 4E 4D 4C 4B 4A 49 48
8D not bit addressable TH1
28 47 46 45 44 43 42 41 40
8C not bit addressable TH0
27 3F 3E 3D 3C 3B 3A 39 38
8B not bit addressable TL1
26 37 36 35 34 33 32 31 30
8A not bit addressable TL0
25 2F 2E 2D 2C 2B 2A 29 28
89 not bit addressable TMOD
24 27 26 25 24 23 22 21 20
23 1F 19 18
88 8F 8E 8D 8C 8B 8A 89 88 TCON
1E 1D 1C 1B 1A
87 not bit addressable PCON
22 17 16 15 14 13 12 11 10
21 0F 0E 0D 0C 0B 0A 09 08
83 not bit addressable DPH
20 07 06 05 04 03 02 01 00
82 not bit addressable DPL
1F
Bank 3 81 not bit addressable SP
18
80 87 86 85 84 83 82 81 80 P0
17
Bank 2
10 Special Function Registers
0F
Bank 1
08
07 Default register
00 bank for R0-R7

RAM

2-7
Figure 11. PSW (Program Status Word) Register in Atmel Flash Microcontrollers

The Instruction Set Addressing Modes


All members of the Atmel microcontroller family execute the The addressing modes in the Flash microcontroller instruc-
same instruction set. This instruction set is optimized for 8- tion set are as follows.
bit control applications and it provides a variety of fast
addressing modes for accessing the internal RAM to facili- Direct Addressing
tate byte operations on small data structures. The instruc- In direct addressing, the operand is specified by an 8-bit
tion set provides extensive support for 1-bit variables as a address field in the instruction. Only internal data RAM and
separate data type, allowing direct bit manipulation in con- SFRs can be directly addressed.
trol and logic systems that require Boolean processing. Indirect Addressing
The following overview of the instruction set gives a brief In indirect addressing, the instruction specifies a register
description of how certain instructions can be used. that contains the address of the operand. Both internal and
external RAM can be indirectly addressed.
Program Status Word The address register for 8-bit addresses can be either the
The Program Status Word (PSW) contains status bits that Stack Pointer or R0 or R1 of the selected register bank.
reflect the current state of the CPU. The PSW, shown in The address register for 16-bit addresses can be only the
Figure 11, resides in SFR space. The PSW contains the 16-bit data pointer register, DPTR.
Carry bit, the Auxiliary Carry (for BCD operations), the two- Register Instructions
register bank select bits, the Overflow flag, a Parity bit, and The register banks, which contain registers R0 through R7,
two user-definable status flags. can be accessed by instructions whose opcodes carry a 3-
The Carry bit, in addition to serving as a Carry bit in arith- bit register specification. Instructions that access the regis-
metic operations, also serves as the Accumulator for a ters this way make efficient use of code, since this mode
number of Boolean operations. eliminates an address byte. When the instruction is exe-
The bits RS0 and RS1 select one of the four register banks cuted, one of the eight registers in the selected bank is
shown in Figure 8. A number of instructions refer to these accessed. One of four banks is selected at execution time
RAM locations as R0 through R7. The status of the RS0 by the two bank select bits in the PSW.
and RS1 bits at execution time determines which of the four
Register-Specific Instructions
banks is selected.
Some instructions are specific to a certain register. For
The Parity bit reflects the number of 1s in the Accumulator: example, some instructions always operate on the Accu-
P=1 if the Accumulator contains an odd number of 1s, and mulator, so no address byte is needed to point to it. In
P=0 if the Accumulator contains an even number of 1s. these cases, the opcode itself points to the correct register.
Thus, the number of 1s in the Accumulator plus P is always Instructions that refer to the Accumulator as A assemble as
even. Accumulator-specific opcodes.
Two bits in the PSW are uncommitted and can be used as
general purpose status flags.

2-8 Architectural Overview


Architectural Overview

The subroutine TABLE would look like the following exam- The instruction set for the Boolean processor is shown in
ple. Table 6. All bit accesses are by direct addressing. Bit
TABLE: MOVC A,@A + PC addresses 00H through 7FH are in the Lower 128, and bit
addresses 80H through FFH are in SFR space.
RET
The following example shows how easily an internal flag
The table itself immediately follows the RET (return)
can be moved to a port pin.
instruction in program memory. This type of table can have
up to 255 entries, numbered 1 through 255. Number 0 can MOV C,FLAG
not be used, because at the time the MOVC instruction is MOV P1.0,C
executed, the PC contains the address of the RET instruc- In this example, FLAG is the name of any addressable bit
tion. An entry numbered 0 would be the RET opcode itself. in the Lower 128 or SFR space. An I/O line (the LSB of Port
1, in this case) is set or cleared depending on whether the
Boolean Instructions flag bit is 1 or 0.
Atmels Flash microcontrollers contain a complete Boolean The Carry bit in the PSW is used as the single-bit Accumu-
(single-bit) processor. The internal RAM contains 128 lator of the Boolean processor. Bit instructions that refer to
addressable bits, and the SFR space can support up to 128 the Carry bit as C assemble as Carry-specific instructions
other addressable bits. All of the port lines are bit-address- (CLR C, etc). The Carry bit also has a direct address, since
able, and each one can be treated as a separate single-bit it resides in the PSW register, which is bit-addressable.
port. The instructions that access these bits are not just The Boolean instruction set includes ANL and ORL, but not
conditional branches, but a complete menu of move, set, the XRL (Exclusive OR) operation. Implementing XRL in
clear, complement, OR, and AND instructions. These kinds software is simple. Suppose, for example, that an applica-
of bit operations are not easily obtained in other architec- tion requires the Exclusive OR of two bits.
tures with any amount of byte-oriented software. C = bit1 .XRL. bit2
Table 6. Boolean Instructions The software to do this operation could be as follows.
Mnemonic Operation Execution MOV C,bit1
Time (s)
JNB
ANL C,bit C = C .AND. bit 2
bit2,0VER
ANL C,/bit C = C .AND. .NOT. bit 2 CPL C
ORL C,bit C = C .OR. bit 2 OVER (continue)
ORL C,/bit C = C .OR. .NOT. bit 2 First, bit1 is moved to the Carry. If bit2 = 0, then C now con-
tains the correct result. That is, bit1 .XRL. bit2 = bit1 if bit2
MOV C,bit C = bit 1
= 0. On the other hand, if bit2 = 1, C now contains the com-
MOV bit,C bit = C 2 plement of the correct result. C CARRY need only be
inverted (CPL C) to complete the operation.
CLR C C=0 1
This code uses the JNB instruction, one of a series of bit-
CLR bit bit = 0 1 test instructions which execute a jump if the addressed bit
SETB C C=1 1 is set (JC, JB, JBC) or if the addressed bit is not set (JNC,
JNB). In the above case, bit2 is being tested, and if bit2 = 0,
SETB bit bit = 1 1
the CPL C instruction is jumped over.
CPL C C = .NOT. C 1 If the addressed bit is set, JBC executes the jump and also
CPL bit bit = .NOT. bit 1 clears the bit. Thus, a flag can be tested and cleared in one
operation.
JC rel Jump if C = 1 2
All the PSW bits are directly addressable, so the Parity bit,
JNC rel Jump if C = 0 2 or the general purpose flags, for example, are also avail-
JB bit,rel Jump if bit = 1 2 able to the bit-test instructions.

JNB bit,rel Jump if bit = 0 2


JBC bit,rel Jump if bit = 1; CLR bit 2

2-13
Relative Offset Jump Instructions
The destination address for these jumps is specified to the Table 7 shows the list of unconditional jumps.
assembler by a label or by an actual address in program
Table 7. Unconditional Jumps in Flash Microcontrollers
memory. However, the destination address assembles to a
relative offset byte. This is a signed (twos complement) off- Mnemonic Operation Execution
set byte that is added to the PC in twos complement Time (s)
arthimetic if the jump is executed. JMP addr Jump to addr 2
The range of the jump is therefore -128 to +127 program JMP @A+DPTR Jump to A + DPTR 2
memory bytes relative to the first byte following the instruc-
tion. CALL addr Call subroutine at addr 2
RET Return from subroutine 2
RETI Return from interurpt 2

Table 8. Conditional Jumps in Flash Microcontrollers


Mnemonic Operation Addressing Modes Execution
Time (S)
Dir Ind Reg Imm
JZ rel Jump if A = 0 Accumulator Only 2
JNZ rel Jump if A 0 Accumulator Only 2
DJNZ <byte>,rel Decrement and jump if not zero X X 2
CJNE A,<byte>,rel Jump if A <byte> X X 2
CJNE <byte>,#data,rel Jump if <byte> #data X X 2

Table 7 lists a single JMP addr instruction, but in fact there the format required by the instruction does not support the
are threeSJMP, LJMP and AJMPwhich differ in the for- distance to the specified destination address, a Destina-
mat of the destination address. JMP is a generic mnemonic tion out of range message is written into the List file.
that can be used if the programmer does not care which The JMP @A+DPTR instruction supports case jumps. The
way the jump is encoded. destination address is computed at execution time as the
The SJMP instruction encodes the destination address as sum of the 16-bit DPTR register and the Accumulator. Typ-
a relative offset, as described above. The instruction is 2 ically, DPTR is set up with the address of a jump table, and
bytes long, consisting of the opcode and the relative offset the Accumulator is given an index to the table. In a 5-way
byte. The jump distance is limited to a range of -128 to branch, for example, an integer 0 through 4 is loaded into
+127 bytes, relative to the instruction following the SJMP. the Accumulator. The code to be executed might be as fol-
The LJMP instruction encodes the destination address as a lows.
16-bit constant. The instruction is 3 bytes long, consisting MOV DPTR, # JUMP__TABLE
of the opcode and two address bytes. The destination MOV A,INDEX__NUMBER
address can be anywhere in the 64K program memory
RL A
space.
JMP @A+ DPTR
The AJMP instruction encodes the destination address as
an 11-bit constant. The instruction is 2 bytes long, consist- The RL A instruction converts the index number (0 through
ing of the opcode, which itself contains 3 of the 11 address 4) to an even number in the range 0 through 8, because
bits, followed by another byte containing the low 8 bits of each entry in the jump table is 2 bytes long, as shown in the
the destination address. When the instruction is executed, following example.
these 11 bits are simply substituted for the low 11 bits in the JUMP__TABLE:
PC. The high 5 bits stay the same. Hence, the destination AJMP CASE__0
has to be within the same 2K block as the instruction fol-
AJMP CASE__1
lowing the AJMP.
AJMP CASE__2
In all cases, the programmer specifies the destination
address to the assembler the same way: as a label or as a AJMP CASE__3
16-bit constant. The assembler puts the destination AJMP CASE__4
address into the correct format for the given instruction. If

2-14 Architectural Overview


Architectural Overview

Table 8 shows a single CALL addr instruction, but there are mented, and the looping continued until the R1 data
two CALL instructionsLCALL and ACALLwhich differ in reached 2AH.
the format in which the subroutine address is given to the Another application of this instruction is in greater than,
CPU. CALL is a generic mnemonic that can be used if the less than comparisons. The two bytes in the operand field
programmer does not care which way the address is are taken as unsigned integers. If the first is less than the
encoded. second, then the Carry bit is set (1). If the first is greater
The LCALL instruction uses the 16-bit address format, and than or equal to the second, then the Carry bit is cleared.
the subroutine can be anywhere in the 64K program mem-
ory space. The ACALL instruction uses the 11-bit format,
and the subroutine must be in the same 2K block as the
CPU Timing
instruction following the ACALL. All Atmel Flash microcontrollers have an on-chip oscillator,
which can be used as the clock source for the CPU. To use
In any case, the programmer specifies the subroutine
the on-chip oscillator, connect a crystal or ceramic resona-
address to the assembler the same way: as a label or as a
tor between the XTAL1 and XTAL2 pins of the microcon-
16-bit constant. The assembler puts the address into the
troller, and connect the capacitors to ground as shown in
correct format for the given instructions.
Figure 14.
Subroutines should end with a RET instruction, which
Examples of how to drive the clock with an external oscilla-
returns execution to the instruction following the CALL.
tor are shown in Figure 15b.
RETI is used to return from an interrupt service routine. The
The internal clock generator defines the sequence of states
only difference between RET and RETI is that RETI tells
that make up the microcontroller machine cycle.
the interrupt control system that the interrupt in progress is
finished. If no interrupt is in progress at the time RETI is Figure 14. Using the On-Chip Oscillator
executed, then the RETI is functionally identical to RET. FLASH
MICROCONTROLLER
Table 8 shows the list of conditional jumps available. All of
these jumps specify the destination address by the relative
offset method and so are limited to a jump distance of -128
XTAL2
to +127 bytes from the instruction following the conditional QUARTZ CRYSTAL C1
OR CERAMIC
jump instruction. However, the user specifies to the assem- RESONATOR C2
bler the actual destination address the same way as the XTAL1

other jumps: as a label or a 16-bit constant. GND

There is no 0 bit in the PSW. The JZ and JNZ instructions


test the Accumulator data for that condition.
Figure 15. A: Oscillator Connections
The DJNZ instruction (Decrement and Jump if Not Zero) is
for loop control. To execute a loop N times, load a counter C2
byte with N and terminate the loop with a DJNZ to the XTAL2
beginning of the loop, as shown below for N = 10.
MOV COUNTER,#10
LOOP: (begin loop) C1
XTAL1
*
*
*
(end loop) GND

DJNZ COUNTER,LOOP
(continue)
The CJNE instruction (Compare and Jump if Not Equal)
can also be used for loop control, as shown in Figure 13.
Two bytes are specified in the operand field of the instruc-
tion. The jump is executed only if the two bytes are not
equal. In the example of Figure 13, the two bytes were the
data in R1 and the constant 2AH. The initial data in R1 was
2EH. Every time the loop was executed, R1 was decre-

2-15
Architectural Overview

The subroutine TABLE would look like the following exam- The instruction set for the Boolean processor is shown in
ple. Table 6. All bit accesses are by direct addressing. Bit
TABLE: MOVC A,@A + PC addresses 00H through 7FH are in the Lower 128, and bit
addresses 80H through FFH are in SFR space.
RET
The following example shows how easily an internal flag
The table itself immediately follows the RET (return)
can be moved to a port pin.
instruction in program memory. This type of table can have
up to 255 entries, numbered 1 through 255. Number 0 can MOV C,FLAG
not be used, because at the time the MOVC instruction is MOV P1.0,C
executed, the PC contains the address of the RET instruc- In this example, FLAG is the name of any addressable bit
tion. An entry numbered 0 would be the RET opcode itself. in the Lower 128 or SFR space. An I/O line (the LSB of Port
1, in this case) is set or cleared depending on whether the
Boolean Instructions flag bit is 1 or 0.
Atmels Flash microcontrollers contain a complete Boolean The Carry bit in the PSW is used as the single-bit Accumu-
(single-bit) processor. The internal RAM contains 128 lator of the Boolean processor. Bit instructions that refer to
addressable bits, and the SFR space can support up to 128 the Carry bit as C assemble as Carry-specific instructions
other addressable bits. All of the port lines are bit-address- (CLR C, etc). The Carry bit also has a direct address, since
able, and each one can be treated as a separate single-bit it resides in the PSW register, which is bit-addressable.
port. The instructions that access these bits are not just The Boolean instruction set includes ANL and ORL, but not
conditional branches, but a complete menu of move, set, the XRL (Exclusive OR) operation. Implementing XRL in
clear, complement, OR, and AND instructions. These kinds software is simple. Suppose, for example, that an applica-
of bit operations are not easily obtained in other architec- tion requires the Exclusive OR of two bits.
tures with any amount of byte-oriented software. C = bit1 .XRL. bit2
Table 6. Boolean Instructions The software to do this operation could be as follows.
Mnemonic Operation Execution MOV C,bit1
Time (s)
JNB
ANL C,bit C = C .AND. bit 2
bit2,0VER
ANL C,/bit C = C .AND. .NOT. bit 2 CPL C
ORL C,bit C = C .OR. bit 2 OVER (continue)
ORL C,/bit C = C .OR. .NOT. bit 2 First, bit1 is moved to the Carry. If bit2 = 0, then C now con-
tains the correct result. That is, bit1 .XRL. bit2 = bit1 if bit2
MOV C,bit C = bit 1
= 0. On the other hand, if bit2 = 1, C now contains the com-
MOV bit,C bit = C 2 plement of the correct result. C CARRY need only be
inverted (CPL C) to complete the operation.
CLR C C=0 1
This code uses the JNB instruction, one of a series of bit-
CLR bit bit = 0 1 test instructions which execute a jump if the addressed bit
SETB C C=1 1 is set (JC, JB, JBC) or if the addressed bit is not set (JNC,
JNB). In the above case, bit2 is being tested, and if bit2 = 0,
SETB bit bit = 1 1
the CPL C instruction is jumped over.
CPL C C = .NOT. C 1 If the addressed bit is set, JBC executes the jump and also
CPL bit bit = .NOT. bit 1 clears the bit. Thus, a flag can be tested and cleared in one
operation.
JC rel Jump if C = 1 2
All the PSW bits are directly addressable, so the Parity bit,
JNC rel Jump if C = 0 2 or the general purpose flags, for example, are also avail-
JB bit,rel Jump if bit = 1 2 able to the bit-test instructions.

JNB bit,rel Jump if bit = 0 2


JBC bit,rel Jump if bit = 1; CLR bit 2

2-13
Relative Offset Jump Instructions
The destination address for these jumps is specified to the Table 7 shows the list of unconditional jumps.
assembler by a label or by an actual address in program
Table 7. Unconditional Jumps in Flash Microcontrollers
memory. However, the destination address assembles to a
relative offset byte. This is a signed (twos complement) off- Mnemonic Operation Execution
set byte that is added to the PC in twos complement Time (s)
arthimetic if the jump is executed. JMP addr Jump to addr 2
The range of the jump is therefore -128 to +127 program JMP @A+DPTR Jump to A + DPTR 2
memory bytes relative to the first byte following the instruc-
tion. CALL addr Call subroutine at addr 2
RET Return from subroutine 2
RETI Return from interurpt 2

Table 8. Conditional Jumps in Flash Microcontrollers


Mnemonic Operation Addressing Modes Execution
Time (S)
Dir Ind Reg Imm
JZ rel Jump if A = 0 Accumulator Only 2
JNZ rel Jump if A 0 Accumulator Only 2
DJNZ <byte>,rel Decrement and jump if not zero X X 2
CJNE A,<byte>,rel Jump if A <byte> X X 2
CJNE <byte>,#data,rel Jump if <byte> #data X X 2

Table 7 lists a single JMP addr instruction, but in fact there the format required by the instruction does not support the
are threeSJMP, LJMP and AJMPwhich differ in the for- distance to the specified destination address, a Destina-
mat of the destination address. JMP is a generic mnemonic tion out of range message is written into the List file.
that can be used if the programmer does not care which The JMP @A+DPTR instruction supports case jumps. The
way the jump is encoded. destination address is computed at execution time as the
The SJMP instruction encodes the destination address as sum of the 16-bit DPTR register and the Accumulator. Typ-
a relative offset, as described above. The instruction is 2 ically, DPTR is set up with the address of a jump table, and
bytes long, consisting of the opcode and the relative offset the Accumulator is given an index to the table. In a 5-way
byte. The jump distance is limited to a range of -128 to branch, for example, an integer 0 through 4 is loaded into
+127 bytes, relative to the instruction following the SJMP. the Accumulator. The code to be executed might be as fol-
The LJMP instruction encodes the destination address as a lows.
16-bit constant. The instruction is 3 bytes long, consisting MOV DPTR, # JUMP__TABLE
of the opcode and two address bytes. The destination MOV A,INDEX__NUMBER
address can be anywhere in the 64K program memory
RL A
space.
JMP @A+ DPTR
The AJMP instruction encodes the destination address as
an 11-bit constant. The instruction is 2 bytes long, consist- The RL A instruction converts the index number (0 through
ing of the opcode, which itself contains 3 of the 11 address 4) to an even number in the range 0 through 8, because
bits, followed by another byte containing the low 8 bits of each entry in the jump table is 2 bytes long, as shown in the
the destination address. When the instruction is executed, following example.
these 11 bits are simply substituted for the low 11 bits in the JUMP__TABLE:
PC. The high 5 bits stay the same. Hence, the destination AJMP CASE__0
has to be within the same 2K block as the instruction fol-
AJMP CASE__1
lowing the AJMP.
AJMP CASE__2
In all cases, the programmer specifies the destination
address to the assembler the same way: as a label or as a AJMP CASE__3
16-bit constant. The assembler puts the destination AJMP CASE__4
address into the correct format for the given instruction. If

2-14 Architectural Overview


Architectural Overview

Table 8 shows a single CALL addr instruction, but there are mented, and the looping continued until the R1 data
two CALL instructionsLCALL and ACALLwhich differ in reached 2AH.
the format in which the subroutine address is given to the Another application of this instruction is in greater than,
CPU. CALL is a generic mnemonic that can be used if the less than comparisons. The two bytes in the operand field
programmer does not care which way the address is are taken as unsigned integers. If the first is less than the
encoded. second, then the Carry bit is set (1). If the first is greater
The LCALL instruction uses the 16-bit address format, and than or equal to the second, then the Carry bit is cleared.
the subroutine can be anywhere in the 64K program mem-
ory space. The ACALL instruction uses the 11-bit format,
and the subroutine must be in the same 2K block as the
CPU Timing
instruction following the ACALL. All Atmel Flash microcontrollers have an on-chip oscillator,
which can be used as the clock source for the CPU. To use
In any case, the programmer specifies the subroutine
the on-chip oscillator, connect a crystal or ceramic resona-
address to the assembler the same way: as a label or as a
tor between the XTAL1 and XTAL2 pins of the microcon-
16-bit constant. The assembler puts the address into the
troller, and connect the capacitors to ground as shown in
correct format for the given instructions.
Figure 14.
Subroutines should end with a RET instruction, which
Examples of how to drive the clock with an external oscilla-
returns execution to the instruction following the CALL.
tor are shown in Figure 15b.
RETI is used to return from an interrupt service routine. The
The internal clock generator defines the sequence of states
only difference between RET and RETI is that RETI tells
that make up the microcontroller machine cycle.
the interrupt control system that the interrupt in progress is
finished. If no interrupt is in progress at the time RETI is Figure 14. Using the On-Chip Oscillator
executed, then the RETI is functionally identical to RET. FLASH
MICROCONTROLLER
Table 8 shows the list of conditional jumps available. All of
these jumps specify the destination address by the relative
offset method and so are limited to a jump distance of -128
XTAL2
to +127 bytes from the instruction following the conditional QUARTZ CRYSTAL C1
OR CERAMIC
jump instruction. However, the user specifies to the assem- RESONATOR C2
bler the actual destination address the same way as the XTAL1

other jumps: as a label or a 16-bit constant. GND

There is no 0 bit in the PSW. The JZ and JNZ instructions


test the Accumulator data for that condition.
Figure 15. A: Oscillator Connections
The DJNZ instruction (Decrement and Jump if Not Zero) is
for loop control. To execute a loop N times, load a counter C2
byte with N and terminate the loop with a DJNZ to the XTAL2
beginning of the loop, as shown below for N = 10.
MOV COUNTER,#10
LOOP: (begin loop) C1
XTAL1
*
*
*
(end loop) GND

DJNZ COUNTER,LOOP
(continue)
The CJNE instruction (Compare and Jump if Not Equal)
can also be used for loop control, as shown in Figure 13.
Two bytes are specified in the operand field of the instruc-
tion. The jump is executed only if the two bytes are not
equal. In the example of Figure 13, the two bytes were the
data in R1 and the constant 2AH. The initial data in R1 was
2EH. Every time the loop was executed, R1 was decre-

2-15
Figure 15. B: External Clock Drive Configuration A data memory bus cycle takes twice as much time as a
program memory bus cycle. Figure 17 shows the relative
timing of the addresses being emitted at Ports 0 and 2 and
NC XTAL2 of ALE and PSEN. ALE latches the low address byte from
P0 into the address latch.
When the CPU is executing from internal program memory,
EXTERNAL
PSEN is not activated, and program addresses are not
OSCILLATOR XTAL1 emitted. However, ALE continues to be activated twice per
SIGNAL machine cycle and is therefore available as a clock output
signal. Note, however, that one ALE is skipped during the
GND execution of the MOVX instruction.

Machine Cycles
A machine cycle consists of a sequence of 6 states, num-
bered S1 through S6. Each state time lasts for two oscilla-
tor periods. Thus, a machine cycle lasts 12 oscillator peri-
ods or 1 s if the oscillator frequency is 12 MHz.
Each state is divided into a Phase 1 half and a Phase 2
half. Figure 16 shows the fetch/execute sequences in
states and phases for various kinds of instructions. Nor-
mally two program fetches are generated during each
machine cycle, even if the instruction being executed does
not require it. If the instruction being executed does not
need more code bytes, the CPU ignores the extra fetch,
and the Program Counter is not incremented.
Execution of a one-cycle instruction (Figure 16A and B)
begins during State 1 of the machine cycle, when the
opcode is latched into the Instruction Register. A second
fetch occurs during S4 of the same machine cycle. Execu-
tion is complete at the end of State 6 of this machine cycle.
The MOVX instructions take two machine cycles to exe-
cute. No program fetch is generated during the second
cycle of a MOVX instruction. This is the only time program
fetches are skipped. The fetch/execute sequence for
MOVX instructions is shown in Figure 16(D).
The fetch/execute sequences are the same whether the
program memory is internal or external to the chip. Execu-
tion times do not depend on whether the program memory
is internal or external.
Figure 17 shows the signals and timing involved in program
fetches when the program memory is external. If program
memory is external, the program memory read strobe
PSEN is normally activated twice per machine cycle, as
shown in Figure 17(A).
If an access to external data memory occurs, as shown in
Figure 17(B), two PSENs are skipped, because the
address and data bus are being used for the data memory
access.

2-16 Architectural Overview


Architectural Overview

Figure 16. State Sequences in Atmel Flash Microcontrollers

S1 S2 S3 S4 S5 S6 S1 S2 S3 S4 S5 S6 S1
P1 P2 P1 P2 P1 P2 P1 P2 P1 P2 P1 P2 P1 P2 P1 P2 P1 P2 P1 P2 P1 P2 P1 P2 P1 P2
OSC.
(XTAL2)

ALE

READ OPCODE
READ NEXT
OPCODE READ NEXT OPCODE AGAIN
(DISCARD)

S1 S2 S3 S4 S5 S6

(A) 1-byte, 1-cycle instruction, e.g., INC A


READ OPCODE
READ 2ND READ NEXT OPCODE
BYTE

S1 S2 S3 S4 S5 S6

(B) 2-byte, 1-cycle instruction, e.g., ADD A, #data


READ NEXT
READ OPCODE OPCODE (DISCARD) READ NEXT
OPCODE AGAIN

S1 S2 S3 S4 S5 S6 S1 S2 S3 S4 S5 S6

(C) 1-byte, 2-cycle instruction, e.g., INC DPTR

READ READ NEXT NO NO READ NEXT


OPCODE OPCODE (DISCARD) FETCH NO FETCH OPCODE
(MOVX) ALE AGAIN

S1 S2 S3 S4 S5 S6 S1 S2 S3 S4 S5 S6

(D) MOVX (1-byte, 2-cycle) ADDR DATA

ACCESS EXTERNAL MEMORY

2-17
Figure 17. Bus Cycles Executing from External Program Memory
ONE MACHINE ONE MACHINE
CYCLE CYCLE

S1 S2 S3 S4 S5 S6 S1 S2 S3 S4 S5 S6

ALE

PSEN
(A)
RD
WITHOUT A
MOVX
P2 PCH OUT PCH OUT PCH OUT PCH OUT PCH OUT PCH OUT

INST PCL INST PCL INST PCL INST PCL INST PCL
P0 IN OUT IN OUT IN OUT IN OUT IN OUT

PCL OUT PCL OUT PCL OUT PCL OUT PCL OUT
VALID VALID VALID VALID VALID

CYCLE 1 CYCLE 2

S1 S2 S3 S4 S5 S6 S1 S2 S3 S4 S5 S6

ALE

PSEN
(B)
RD WITH A
MOVX
P2 PCH OUT PCH OUT DPH OUT OR P2 OUT PCH OUT PCH OUT

INST PCL INST ADDR DATA PCL INST PCL


P0 IN OUT IN OUT IN OUT IN OUT

PCL OUT ADDR OUT PCL OUT


VALID VALID VALID

2-18 Architectural Overview


Architectural Overview

Interrupt Structure priority level the polling sequence determines a second pri-
ority structure.
The AT89C51 core provides 5 interrupt sources: 2 external
interrupts, 2 timer interrupts, and the serial port interrupt. Figure 20 shows how the IE and IP registers and the polling
What follows is an overview of the interrupt structure for the sequence work to determine which (if any) interrupt will be
AT89C51. Other Atmel Flash microcontrollers have addi- serviced.
tional interrupt sources and vectors. Refer to the data In operation, all the interrupt flags are latched into the inter-
sheets on other devices for further information on their rupt control system during State 5 of every machine cycle.
interrupts. The samples are polled during the following machine cycle.
If the flag for an enabled interrupt is found to be set (1), the
Interrupt Enables interrupt system generates an LCALL to the appropriate
Each of the interrupt sources can be individually enabled or location in program memory, unless some other condition
disabled by setting or clearing the Interrupt Enable (IE) bit blocks the interrupt. Several conditions can block an inter-
in the SFR. This register also contains a global disable bit, rupt, including an interrupt of equal or higher priority level
which can be cleared to disable all interrupts at once. Fig- already in progress.
ure 18 shows the IE register for the AT89C51.
The hardware-generated LCALL pushes the contents of
Figure 18. Interrupt Enable (IE) Register in the AT89C51
the Program Counter onto the stack and reloads the PC
(MSB) (LSB) with the beginning address of the service routine. As previ-
EA ES ET1 EX1 ET0 EX0 ously noted (Figure 4), the service routine for each interrupt
begins at a fixed location.
Enable bit = 1 enables the interrupt.
Only the Program Counter is automatically pushed onto the
Enable bit = 0 disables it.
stack, not the PSW or any other register. Because only the
Symbol Position Function PC is automatically saved, the programmer can decide how
EA IE.7 Disables all interrupts. If EA = 0, no
much time to spend saving other registers. This enhances
interrupt will be acknowledged. If EA = 1, the interrupt response time, albeit at the expense of
each interrupt source is individually enabled increasing the programmers burden of responsibility. As a
or disabled by setting or clearing its enable result, many interrupt functions that are typical in control
bit. applicationstoggling a port pin, reloading a timer, or
IE.6 reserved.* unloading a serial buffer, for examplecan often be com-
pleted in less time than it takes other architectures to begin
IE.5 reserved.*
them.
ES IE.4 Serial Port Interrupt enable bit. Figure 19. IP (Interrupt Priority) Register in the AT89C51
ET1 IE.3 Timer 1 Overflow Interrupt enable bit. (MSB) (LSB)
EX1 IE.2 External Interrupt 1 enable bit. PS PT1 PX1 PT0 PX0
ET0 IE.1 Timer 0 Overflow Interrupt enable bit. Priority bit = 1 assigns high priority.
EX0 IE.0 External Interrupt 0 enable bit. Priority bit = 0 assigns low priority.
*These reserved bits are used in other Atmel microcontrollers. Symbol Position Function

IP.7 reserved.*
Interrupt Priorities
Each interrupt source can also be individually programmed IP.6 reserved.*
to one of two priority levels by setting or clearing the Inter- IP.5 reserved.*
rupt Priority (IP) bit in the SFR. Figure 19 shows the IP reg- PS IP.4 Serial Port Interrupt priority bit.
ister in the AT89C51.
PT1 IP.3 Timer 1 Interrupt priority bit.
A low-priority interrupt can be interrupted by a high-priority
interrupt but not by another low-priority interrupt. A high-pri- PX1 IP.2 External Interrupt 1 priority bit.
ority interrupt can not be interrupted by any other interrupt PT0 IP.1 Timer 0 Interrupt priority bit.
source.
PX0 IP.0 External Interrupt 0 priority bit.
If two interrupt requests of different priority levels are
received simultaneously, the request of higher priority level *These reserved bits are used in other Atmel microcontrollers.
is serviced. If interrupt requests of the same priority level
are received simultaneously, an internal polling sequence
determines which request is serviced. Thus, within each

2-19
Simulating a Third Priority Level in Software As soon as any priority 1 interrupt is acknowledged, the IE
Some applications require more than the two priority levels register is redefined to disable all but priority 2 interrupts.
that are provided by on-chip hardware in Atmel Flash Then, a CALL to LABEL executes the RETI instruction,
microcontrollers. In these cases, relatively simple software which clears the priority 1 interrupt-in-progress flip-flop. At
can be written to produce the same effect as a third priority this point, any enabled priority 1 interrupt can be serviced,
level. but only priority 2 interrupts are enabled.
First, interrupts that require higher priority than 1 are POPping IE restores the original enable byte. Then, a nor-
assigned to priority 1 in the IP register. The service routines mal RET (rather than another RETI) is used to terminate
for priority 1 interrupts that are supposed to be interruptible the service routine. The additional software adds 10 ms (at
by priority 2 interrupts are written to include the following 12 MHz) to priority 1 interrupts.
code.
PUSH IE
MOV IE, # MASK
CALL LABEL
*******
(execute service routine)
*******
POP IE
RET
LABEL: RETI
Figure 20. AT89 Interrupt Control System

HIGH PRIORITY
IE REGISTER IP REGISTER INTERRUPT
0
INT0 IT0
1 IE0

TF0

0
INT1 IT1 IE1
1
INTERRUPT
POLLING
TF1 SEQUENCE

R1
T1

TF2
EXF2

LOW PRIORITY
GLOBAL INTERRUPT
ENABLE

INDIVIDUAL
INTERRUPT
ENABLES

Note: Only on AT89C52/AT89LV52/AT89S8252

2-20 Architectural Overview

You might also like