Section 1 The 8051 Instruction Set The 8051 instruction set is optimized for 8-bit control applications. It provides a variety of fast addressing modes for accessing the internal RAM to facilitate byte operations on small data structures. The instruction set provides extensive support for one-bit vari- ables as a separate data type, allowing direct bit manipulation in control and logic systems that require Boolean processing.
(0.0)-Bank 0(00H-07H) (0.1)-Bank 1(08H-0FH) (1.0)-Bank 2(10H-17H) (1.1)-Bank 3(18H-1FH) Addressing The addressing modes in the 8051 instruction set are as follows: Modes 1.2.1 Direct Addressing In direct addressing the operand is specified by an 8-bit address field in the instruction. Only 128 Lowest bytes of internal Data RAM and SFRs can be directly addressed.
Page 5
Another type of indexed addressing is used in the “case jump” instruction. In this case the destination address of a jump instruction is computed as the sum of the base pointer and the Accumulator data. Atmel 8051 Microcontrollers Hardware Manual 4316B–8051–02/04...
For example, the ADD A, <byte> instruction can be written as: ADD A,7FH (direct addressing) ADD A,@ R0(indirect addressing) ADD A,R7 (register addressing) ADD A,# 127(immediate constant) Table 1-2. A list of the Atmel 8051 Arithmetic Instructions. Execution Time in X1 Mode Mnemonic Operation Addressing Modes @12 MHz (µs) ADD A, <byt>e...
ANL A, # 53H(immediate constant) All of the logical instructions that are Accumulator specific execute in 1 µs (using a 12 MHz clock and X1 mode). The others take 2 µs. Table 1-3. A list of the Atmel 8051 Logical Instructions Execution Time Mnemonic...
128 bytes of data RAM can be accessed only by indirect, and SFR space only by direct addressing. Note that in all 8051 devices, the stack resides in on-chip RAM, and grows upwards. The PUSH instruction first increments the Stack Pointer (SP), then copies the byte into the stack.
Page 9
ACC and @ Ri exchange low XCHD A, @Ri nibbles The Upper 128 are not implemented in the 8 standard 8051, nor in their ROMless. With these devices, if the SP points to the Upper 128 PUSHed bytes are lost, and POPped bytes are indeterminate.
Page 10
First, pointers R1 and R0 are set up to point to the two bytes containing the last four BCD digits. Then a loop is executed which leaves the last byte, location 2EH, holding the last two digits of the shifted number. The pointers are decremented, and the loop is Atmel 8051 Microcontrollers Hardware Manual 4316B–8051–02/04...
The other MOVC instruction works the same way, except the Program Counter (PC) is used as the table base, and the table is accessed through a subroutine. First the num- ber of the desired entry is loaded into the Accumulator, and the subroutine is called: Atmel 8051 Microcontrollers Hardware Manual 1-10 4316B–8051–02/04...
MOVC A, @A + PC Read Pgm Memory at (A + PC) Boolean 8051 devices contain a complete Boolean (single-bit) processor. The internal RAM con- Instructions tains 128 addressable bits, and the SFR space can support up to 128 other addressable bits.
Page 13
This is a signed (two’s complement) offset byte which is added to the PC in two’s complement arithmetic if the jump is executed. The range of the jump is therefore -128 to +127 Program Memory bytes relative to the first byte following the instruction. Atmel 8051 Microcontrollers Hardware Manual 1-12 Rev. 4316B–8051–02/04...
Direct Addressed bit in Internal Data RAM or Special Function Register. Jump Table 1-11 shows the list of unconditional jumps. Instructions Table 1-11. Unconditional Jumps in Atmel 8051 Mnemonic Operation Execution Time @ 12MHz (µs) JMP addr...
Page 15
If there is no interrupt in progress at the time RETI is executed, then the RETI is functionally identical to RET. Table 1-12 shows the list of conditional jumps available to the Atmel 8051 user. All of these jumps specify the destination address by the relative offset method, and so are limited to a jump distance of -128 to + 127 bytes from the instruction following the condi- tional jump instruction.
The 8051 Instruction Set Table 1-12. Conditional Jumps in Atmel 8051 Devices Execution Time Mnemonic Operation Addressing Modes @ 12MHz (µs) JZ rel Jump if A = 0 Accumulator only Jump if A ≠ 0 JNZ rel Accumulator only Decrement and jump if DJNZ <byte>,rel...
Page 18
Move direct byte to register ,#data Move immediate data to register direct,A Move Accumulator to direct byte direct,R Move register to direct byte direct,direct Move direct byte to direct direct,@R Move indirect RAM to direct byte 1-17 Atmel 8051 Microcontrollers Hardware Manual 4316B–8051–02/04...
Page 19
OR direct bit to Carry C,/bit OR complement of direct bit to Carry C,bit Move direct bit to Carry bit,C Move Carry to direct bit Jump if Carry is set Jump if Carry not set Atmel 8051 Microcontrollers Hardware Manual 1-18 4316B–8051–02/04...
Page 20
Jump if Not Equal CJNE ,#data,rel Compare immediate to indirect and Jump if Not Equal DJNZ ,rel Decrement register and Jump if Not Zero DJNZ direct,rel Decrement direct byte and Jump if Not Zero No Operation 1-19 Atmel 8051 Microcontrollers Hardware Manual 4316B–8051–02/04...
ORL C,bit ORL C,/bit MOV C,bit CJNE SETB C Note: Operations on SFR byte address 208 or bit addresses 209-215 (that is, the PSW or bits in the PSW) also affect flag settings. Atmel 8051 Microcontrollers Hardware Manual 1-20 4316B–8051–02/04...
The 8051 Instruction Set 1.13 Instruction Table Table 1-14 shows the Hex value of each instruction detailing the: byte size number of cycles flags modified by the instruction Table 1-14. 8051 Instruction Table 1-21 Atmel 8051 Microcontrollers Hardware Manual 4316B–8051–02/04...
Page 23
The 8051 Instruction Set Table 1-14. 8051 Instruction Table (Continued) Atmel 8051 Microcontrollers Hardware Manual 1-22 4316B–8051–02/04...
Page 24
The 8051 Instruction Set Table 1-14. 8051 Instruction Table (Continued) 1-23 Atmel 8051 Microcontrollers Hardware Manual 4316B–8051–02/04...
Page 28
Example: The label JMPADR is at program memory location 0123H. The following instruction, AJMP JMPADR is at location 0345H and loads the PC with 0123H. Bytes: 2 Cycles: 2 Encoding: a10 Operation: AJMP (PC) ← (PC) + 2 ) ← page address 10-0 1-27 Atmel 8051 Microcontrollers Hardware Manual 4316B–8051–02/04...
Page 30
;LOAD CARRY WITH INPUT PIN STATE C,ACC.7 ;AND CARRY WITH ACCUM. BIT 7 C,/OV ;AND WITH INVERSE OF OVERFLOW FLAG C,bit Bytes: 2 Cycles: 2 Encoding: bit address Operation: ANL ∧ (C) ← (C) (bit) 1-29 Atmel 8051 Microcontrollers Hardware Manual 4316B–8051–02/04...
Page 31
Operation: (PC) ← (PC) + 3 IF (A) < > (direct) THEN (PC) ← (PC) + relative offset IF (A) < (direct) THEN (C) ← 1 ELSE (C) ← 0 Atmel 8051 Microcontrollers Hardware Manual 1-30 4316B–8051–02/04...
Page 32
Operation: (PC) ← (PC) + 3 IF ((R )) < > data THEN (PC) ← (PC) + relative offset IF ((R )) < data THEN (C) ← 1 ELSE (C) ← 0 1-31 Atmel 8051 Microcontrollers Hardware Manual 4316B–8051–02/04...
Page 33
Example: Port 1 has previously been written with 5DH (01011101B). The following instruction,CLR P1.2 leaves the port set to 59H (01011001B). Bytes: 1 Cycles: 1 Encoding: Operation: CLR (C) ← 0 Bytes: 2 Cycles: 1 Encoding: bit address Operation: CLR (bit) ← 0 Atmel 8051 Microcontrollers Hardware Manual 1-32 4316B–8051–02/04...
Page 34
Example: Port 1 has previously been written with 5BH (01011101B). The following instruction sequence,CPL P1.1CPL P1.2 leaves the port set to 5BH (01011011B). Bytes: 1 Cycles: 1 Encoding: Operation: CPL (C) ← Bytes: 2 Cycles: 1 Encoding: bit address Operation: CPL (bit) ← (bit) 1-33 Atmel 8051 Microcontrollers Hardware Manual 4316B–8051–02/04...
Page 35
-contents of Accumulator are BCD ∨ [[(A ) > 9] [(AC) = 1]] ) ← (A THEN (A ) + 6 ∨ [[(A ) > 9] [(C) = 1]] ) ← (A THEN (A ) + 6 Atmel 8051 Microcontrollers Hardware Manual 1-34 4316B–8051–02/04...
Page 37
13 in the Accumulator (0DH or 00001101B) and the value 17 (11H or 00010001B) in B, since 251 = (13 x 18) + 17. Carry and OV are both cleared. Bytes: 1 Cycles: 4 Encoding: Operation: DIV ← (A)/(B) 15-8 Atmel 8051 Microcontrollers Hardware Manual 1-36 4316B–8051–02/04...
Page 45
30H in register 0, 40H in both the Accumulator and register 1, 10H in register B, and 0CAH (11001010B) both in RAM location 40H and output on port 2. MOV A,R Bytes: 1 Cycles: 1 Encoding: Operation: MOV (A) ← (R Atmel 8051 Microcontrollers Hardware Manual 1-44 4316B–8051–02/04...
Page 49
MOVC instruction is needed to “get around” the RET instruction above the table. If several bytes of code separate the MOVC from the table, the corresponding number is added to the Accumulator instead. Atmel 8051 Microcontrollers Hardware Manual 1-48...
Page 50
MOVX instruction using R0 or R1. Example: An external 256 byte RAM using multiplexed address/data lines is connected to the 8051 Port 0. Port 3 provides control lines for the external RAM. Ports 1 and 2 are used for normal I/O. Registers 0 and 1 contain 12H and 34H.
Page 51
12,800 (3200H), so B is changed to 32H (00110010B) and the Accumulator is cleared. The overflow flag is set, carry is cleared. Bytes: 1 Cycles: 4 Encoding: Operation: MUL ← (A) X (B) 15-8 Atmel 8051 Microcontrollers Hardware Manual 1-50 4316B–8051–02/04...
Page 52
Accumulator at run-time. The instruction, P1,#00110010B sets bits 5, 4, and 1 of output Port 1. 1-51 Atmel 8051 Microcontrollers Hardware Manual 4316B–8051–02/04...
Page 61
Example: R0 contains the address 20H. The Accumulator holds the value 3FH (0011111lB). Internal RAM location 20H holds the value 75H (01110101B). The following instruction, A,@R0 leaves RAM location 20H holding the values 3FH (00111111B) and 75H (01110101B) in the accumulator. Bytes: 1 Cycles: 1 Encoding: Operation: XCH Atmel 8051 Microcontrollers Hardware Manual 1-60 4316B–8051–02/04...
Page 62
75H (01110101B). The following instruction, XCHD A,@R0 leaves RAM location 20H holding the value 76H (01110110B) and 35H (00110101B) in the Accumulator. Bytes: 1 Cycles: 1 Encoding: Operation: XCHD i3-0 1-61 Atmel 8051 Microcontrollers Hardware Manual 4316B–8051–02/04...
Page 63
Accumulator at run-time. The following instruction, P1,#00110001B complements bits 5, 4, and 0 of output Port 1. Bytes: 1 Cycles: 1 Encoding: Operation: XRL (A) ← (A) V (R Atmel 8051 Microcontrollers Hardware Manual 1-62 4316B–8051–02/04...
Page 64
The 8051 Instruction Set 1-63 Atmel 8051 Microcontrollers Hardware Manual 4316B–8051–02/04...
Introduction This chapter presents a comprehensive description of the on-chip hardware features of the Atmel 8051 microcontrollers. Included in this description are: The port drivers and how they function both as ports and, for Ports 0 and 2, in bus...
Common Features Description Figure 2-1. 8051 Architecture Block Diagram Note: (*)For Timer 2 only. Figure 2-1 shows a functional block diagram of the 80C51s. Special Function A map of the on-chip memory area called SFR (Special Function Register) space is Registers shown in Figure 2-1.
Page 67
PCON Note: Reserved User software should not write to the reserved locations, since they may be used in derivative Atmel 8051 products to invoke new features. The functions of the SFRs are described as below. 2.2.1 Accumulator ACC is the Accumulator register. The mnemonics for accumulator-specific instructions, however, refer to the accumulator simply as A.
Page 68
Control Registers Special Function Registers IP, IE, TMOD, TCON, T2CON, SCON, and PCON contain control and status bits for the interrupt system, the timer/counters, and the serial port. They are described in later sections. 2-67 Atmel 8051 Microcontrollers Hardware Manual 4316B–8051–02/04...
The internal clocking signals are at half the oscillator frequency, and define the internal phases, states, and machine cycles, which are described in the next section. 2.3.1 More about the On- This section not yet available. chip Oscillator Atmel 8051 Microcontrollers Hardware Manual 2-68 4316B–8051–02/04...
2-cycle instruction that accesses external Data Memory. During a MOVX, two fetches are skipped while the external Data Memory is being addressed and strobed. Figure 2-3C and Figure 2-3D show the timing for a normal 1-byte, 2-cycle instruction and for a MOVX instruction. 2-69 Atmel 8051 Microcontrollers Hardware Manual 4316B–8051–02/04...
Page 71
Common Features Description Figure 2-3. 80C51 fetch/Execute Sequences. 2.4.2 X2 Mode This section not yet available. Atmel 8051 Microcontrollers Hardware Manual 2-70 4316B–8051–02/04...
“read pin” signal from the CPU. Some instructions that read a port activate the “read latch” signal, and others activate the “read latch” signal, and others activate the “read pin” signal. 2-71 Atmel 8051 Microcontrollers Hardware Manual 4316B–8051–02/04...
Page 73
In that conditions it can be used as a high-impedance input. Because Ports 1, 2, and 3 have fixed internal pull-ups they are sometimes called “quasi- bidirectional” ports. When configured as inputs they pull high and will source current (IIL, Atmel 8051 Microcontrollers Hardware Manual 2-72 4316B–8051–02/04...
Page 74
The output buffer of Ports 1, 2 and 3 can each drive 3LS TTL inputs. The pins can be Interfacing driven by open-collector and open-drain outputs, but note that 0-to-1 transition will not be fast. In the CMOS device, an input 0 turns off pull-up P3, leaving only the weak pull- 2-73 Atmel 8051 Microcontrollers Hardware Manual 4316B–8051–02/04...
Page 75
(complement bit, e.g., CPL P3.0) (increment, e.g., INC P2) (decrement, e.g., DEC P2) DJNZ (decrement and jump if not zero, e.g., DJNZ P3, LABEL) MOV PX.Y,C(move carry bit to bit Y of Port X) Atmel 8051 Microcontrollers Hardware Manual 2-74 4316B–8051–02/04...
2. Whenever the program counter (PC) contains a number that is larger than the memory size. This requires that the ROMless versions have EA wired low to enable the lower program bytes to be fetched from external memory. 2-75 Atmel 8051 Microcontrollers Hardware Manual 4316B–8051–02/04...
The main function of ALE is to provide a properly timed signal to latch the low byte of an address from P0 to an external latch during fetches from external Program Memory. For Atmel 8051 Microcontrollers Hardware Manual 2-76 4316B–8051–02/04...
Page 78
Set to map XRAM data in external XRAM memory. ALE Output bit Clear to restore ALE operation during internal fetches. Set to disable ALE operation during internal fetches. Reset Value = XXXX XX00b 2-77 Atmel 8051 Microcontrollers Hardware Manual 4316B–8051–02/04...
Common Features Description Timer/Counters The Atmel 80C51 Microcontrollers implement two general purpose, 16-bit tim- ers/counters. They are identified as Timer 0 and Timer 1, and can be independently configured to operate in a variety of modes as a timer or as an event counter. When operating as a timer, the timer/counter runs for a programmed length of time, then issues an interrupt request.
Page 80
Mode 1 is the same as Mode 0, except that the Timer register is being run with all 16 Timer) bits. Mode 1 configures timer 0 as a 16-bit timer with the TH0 and TL0 registers connected in cascade (see Figure 2-10). The selected input increments the TL0 register. 2-79 Atmel 8051 Microcontrollers Hardware Manual 4316B–8051–02/04...
Page 81
(counting F /6) and takes over use of the timer 1 interrupt (TF1) and run control (TR1) bits. Thus, operation of timer 1 is restricted when timer 0 is in mode 3. Atmel 8051 Microcontrollers Hardware Manual 2-80 4316B–8051–02/04...
5 bits of the TL1 register (see Figure 2-9). The upper 3 bits of the TL1 register are ignored. Prescaler overflow increments the TH1 register. 2-81 Atmel 8051 Microcontrollers Hardware Manual 4316B–8051–02/04...
Page 83
Flags are cleared when vectoring to the timer interrupt routine. Interrupts are enabled by setting bit in IE0 register. This assumes interrupts are globally enabled by setting EA bit in the IE0 register. Atmel 8051 Microcontrollers Hardware Manual 2-82 4316B–8051–02/04...
Page 84
Clear to select low level active (level triggered) for external interrupt 0 (INT0#). Set to select falling edge active (edge triggered) for external interrupt 0. Reset Value = 0000 0000b GATE1 C/T1# GATE0 C/T0# 2-83 Atmel 8051 Microcontrollers Hardware Manual 4316B–8051–02/04...
Page 85
High Byte of Timer 0. Reset Value = 0000 0000b Table 2-5. TL0 Register - TL0 (S:8Ah) Timer 0 Low Byte Register Number Mnemonic Description Low Byte of Timer 0. Reset Value = 0000 0000b Atmel 8051 Microcontrollers Hardware Manual 2-84 4316B–8051–02/04...
Page 86
When Timer 0 is in Mode 3, Timer 1 can be turned on and off by switching it out of and into its own Mode 3, or can still be used by the serial port as a baud rate generator, or in fact, in any application not requiring an interrupt. 2-85 Atmel 8051 Microcontrollers Hardware Manual 4316B–8051–02/04...
2.12 Timer 2 Timer 2 is a 16-bit timer/counter which is present in most of the Atmel 8051 microcon- trollers.The count is maintained by two 8-bit timer registers, TH2 and TL2, that are cascade connected. Like Timers 0 and 1, it can operate either as a timer or as an event counter.
Page 88
At overflow, the contents of the RCAP2H and RCAP2L registers are loaded into TH2 and TL2. In this mode, timer 2 overflows do not generate interrupts. The formula gives the clock-out frequency, 2-87 Atmel 8051 Microcontrollers Hardware Manual 4316B–8051–02/04...
9th data bit (TB8 in SCON) can be assigned the value of 0 or 1. Or, for example, the par- ity bit (P, in the PSW) could be moved into TB8. On receive, the 9th data bit goes into 2-91 Atmel 8051 Microcontrollers Hardware Manual 4316B–8051–02/04...
Page 93
Table 2-17. This register contains not only the mode selection bits, but also the 9th data bit for transmit and receive (TB8 and RB8), and the serial port interrupts bits (TI and RI). FE/SM0 Atmel 8051 Microcontrollers Hardware Manual 2-92 4316B–8051–02/04...
Page 94
The baud rate in Mode 2 depends on the value of bit SMOD in Special Function Regis- ter PCON. If SMOD = 0 (which is its value on reset), the baud rate is 1/64 the oscillator frequency. If SMOD = 1, the baud rate is 1/32 the oscillator frequency. 2-93 Atmel 8051 Microcontrollers Hardware Manual 4316B–8051–02/04...
Page 95
When the internal Baud Rate Generator is used, the Baud Rates are determined by the Generator (BRG) BRG overflow depending on the BRL reload value, the value of SPD bit (Speed Mode) in BDRCON register and the value of the SMOD1 bit in PCON register. Atmel 8051 Microcontrollers Hardware Manual 2-94 4316B–8051–02/04...
Page 96
The baud rate generator can be used for mode 1 or 3 (refer to Figure 2-22 on page 97), but also for mode 0 for UART, thanks to the bit SRC located in BDRCON register (Table 2-29.) 2-95 Atmel 8051 Microcontrollers Hardware Manual 4316B–8051–02/04...
Page 97
(Table 2-8). Note then the baud rates for transmit and receive can be simultaneously dif- Rates ferent. Setting RCLK and/or TCLK puts Timer 2 into its baud rate generator mode, as shown in Figure 2-22. Atmel 8051 Microcontrollers Hardware Manual 2-96 4316B–8051–02/04...
Page 98
The RCAP registers may be read, but shouldn’t be written to, because a write might overlap a reload and cause write and/or reload errors. In this case, turn the Timer off (clear TR2) before accessing the Timer 2 or RCAP registers. 2-97 Atmel 8051 Microcontrollers Hardware Manual 4316B–8051–02/04...
Page 99
RECEIVE is active, the contents of the receive shift register are shifted to the left one position. The value that comes in from the right is the value that was sam- pled at the P3.0 pin at S5P2 of the same machine cycle. Atmel 8051 Microcontrollers Hardware Manual 2-98 4316B–8051–02/04...
Page 100
1FFH is written into the input shift register. Resetting the divide-by-16 counter aligns its rollovers with the boundaries of the incoming bit times. 2-99 Atmel 8051 Microcontrollers Hardware Manual 4316B–8051–02/04...
Page 101
Common Features Description Figure 2-24. Serial Port Mode 0 Atmel 8051 Microcontrollers Hardware Manual 2-100 4316B–8051–02/04...
To address a device by its individual address, the SADEN mask byte must be 1111 1111b. For example: SADDR0101 0110b SADEN1111 1100b Given0101 01XXb The following is an example of how to use given addresses to address different slaves: Slave A:SADDR1111 0001b SADEN1111 1010b Given1111 0X0Xb Atmel 8051 Microcontrollers Hardware Manual 2-102 4316B–8051–02/04...
Page 104
80C51 microcontrollers that do not support automatic address recognition. Table 2-16. SADEN Register SADEN - Slave Address Mask Register (B9h) 2-103 Atmel 8051 Microcontrollers Hardware Manual 4316B–8051–02/04...
Page 105
Common Features Description Reset Value = 0000 0000b Not bit addressable Table 2-17. SADDR Register SADDR - Slave Address Register (A9h) Reset Value = 0000 0000b Not bit addressable Atmel 8051 Microcontrollers Hardware Manual 2-104 4316B–8051–02/04...
Page 106
Set by hardware at the end of the 8th bit time in mode 0, see Figure 2-26. and Figure 2-27. in the other modes. Reset Value = 0000 0000b Bit addressable Table 2-18. SADEN Register SADEN - Slave Address Mask Register for UART (B9h) Reset Value = 0000 0000b Table 2-19. SADDR Register 2-105 Atmel 8051 Microcontrollers Hardware Manual 4316B–8051–02/04...
Page 108
Cleared to auto-reload on timer 2 overflows or negative transitions on T2EX pin if EXEN2=1. Set to capture on negative transitions on T2EX pin if EXEN2=1. Reset Value = 0000 0000b Bit addressable SMOD1 SMOD0 2-107 Atmel 8051 Microcontrollers Hardware Manual 4316B–8051–02/04...
Page 109
Reset Value = 00X1 0000b Not bit addressable Power-off flag reset value will be 1 only after a power on (cold reset). A warm reset doesn’t affect the value of this bit. TBCK RBCK Atmel 8051 Microcontrollers Hardware Manual 2-108 4316B–8051–02/04...
If one of the flags was in a set condition at S5P2 of the preceding cycle, the polling cycle will find it and the interrupt system will 2-109 Atmel 8051 Microcontrollers Hardware Manual 4316B–8051–02/04...
Page 111
(but it does not save the PSW) and reloads the PC with an address that depends on the source of the interrupt being vectored to, as shown below. Table 2-23. Source Vector Address 0003H 000BH 0013H Atmel 8051 Microcontrollers Hardware Manual 2-110 4316B–8051–02/04...
Page 112
4 cycles to complete the next instruction if the instruction is MUL or DIV). Thus, in a single-interrupt system, the response time is always more than 3 cycles and less than 8 cycles. 2-111 Atmel 8051 Microcontrollers Hardware Manual 4316B–8051–02/04...
Page 113
No licenses to patents or other intellectual property of Atmel are granted by the Company in connection with the sale of Atmel products, expressly or by implication. Atmel’s products are not authorized for use as critical components in life support devices or systems.