MCS® 51 Microcontroller Family User's Manual Order Number: 272383-002 February 1994 MCS® 51 MICROCONTROLLER FAMILY USER'S MANUAL ORDER NO.: 272383-002 FEBRUARY 1994 Intel Corporation makes no warranty for the use of its products and assumes no responsibility for any errors which may appear in this document nor does it make a commitment to update the information contained herein. Intel retains the right to make changes to these specifications at any time, without notice. Contact your local Intel sales office or your distributor to obtain the latest speCifications before placing your product order. MDS is an ordering code only and is not used as a product name or trademark of Intel Corporation. Intel Corporation and Intel's FASTPATH are not affiliated with Kinetics, a division of Excelan, Inc. or its FASTPATH trademark or products. ·Other brands and names are the property of their respective owners. Additional copies of this document or other Intel literature may be obtained from: Intel Corporation Literature Sales P.O. Box 7641 Mt. Prospect, IL 60056-7641 or call 1-800-879-4683 c-INTEL CORPORATION, 1993 MCS® 51 CONTENTS PAGE MICROCONTROLLER CHAPTER 1 FAMILY MCS 51 Family of Microcontrollers Architectural Overview ............................. 1-1 USER'S MANUAL CHAPTER 2 MCS 51 Programmer's Guide and Instruction Set ..........................................2-1 CHAPTER 3 8051,8052 and 80C51 Hardware Description ...............................................3-1 CHAPTER 4 8XC52154/58 Hardware Description ........... .4-1 CHAPTERS 8XC51 FX Hardware Description ................. 5-1 CHAPTER 6 87C51GB Hardware Description ................. 6-1 CHAPTER 7 83C152 Hardware Description .................... 7-1 iii MCS® 51 Family of 1 Micro con trollers Architectural Overview MCS® 51 FAMILY OF CONTENTS PAGE MICROCONTROLLERS INTRODUCTION ......................................... 1-3 ARCHITECTURAL CHMOS Devices ......................................... 1-5 OVERVIEW MEMORY ORGANIZATION IN MCsa 51 DEVICES ................................................. 1-6 Lo~~~g;~:~:~~~~ .~~.:.~~~~~.~. ~~.~. ~~~~. . . 1-6 Program Memory ......................................... 1-7 Data Memory ............................................... 1-8 THE MCSs 51 INSTRUCTION SET............. 1-9 Program Status Word .................................. 1-9 Addressing Modes ..................................... 1-10 Arithmetic Instructions ............................... 1-10 Logical Instructions .................................... 1-12 Data Transfers ........................................... 1-12 Boolean Instructions .................................. 1-14 Jump Instructions ...................................... 1-16 CPU TIMING ............................................. 1-17 Machine Cycles ......................................... 1-18 Interrupt Structu re ...................................... 1-20 ADDITIONAL REFERENCES ................... 1-22 1-1 MCS®-51 ARCHITECTURAL OVERVIEW INTRODUCTION The 8051 is the original member of the MCS®-51 family, and is the core for all MCS-51 devices. The features ofthe 8051 core are: • 8-bit CPU optimized for control applications • Extensive Boolean processing (single-bit logic) capabilities • 64K Program Memory address space • 64K Data Memory address space • 4K bytes of on-chip Program Memory • 128 bytes of on-chip Data RAM • 32 bidirectional and individually addressable I/O lines • Two 16-bit timer/counters • Full duplex UART • 6-source/5-vector interrupt structure with two priority levels • On-chip clock oscillator The basic architectural structure of this 8051 core is shown in Figure 1. EXTERNAL INTERRUPTS 4K ROM COUNTER 128 BYTES ] INPUTS RAM SERIAL PORT I. I. TXD RXD PO P2 P1 P3 ADDRESS/DATA 270251-1 Figure 1. Block Diagram of the 8051 Core 1-3 -- Table 1. The MeS· 51 Family of Microcontrollers €: 8 DEVICE ROM/EPROM Register Speed I/O Timerl UART Interrupt PCA AID SEP GSC DMA Lock Power Down (bytes) RAM (MHz) Pins Counters Sources Channels Channels Channels Bits & Idle Modes (bytes) 8051 Product Line 8031AH ROMlESS 128 12 32 2 1 5 0 0 0 0 0 8051AH 4KROM 128 12 32 2 1 5 0 0 0 0 0 0 8051AHP 4KROM 128 12 32 2 1 5 0 0 0 0 0 P - 8751H 4KEPROM 128 12 32 2 1 5 0 0 0 0 0 1 - 8751BH 4KEPROM 128 12 32 2 1 5 0 0 0 0 0 2 - 8052 Product Line 3C 8032AH ROMlESS 256 12 32 1 I o 3 6 0 0 0 0 0 en 8052AH 8KROM 256 12 32 I 3 1 6 I 0 0 I 0 0 0 0 @) 8752BH I 8KEPROM I 256 I 12 32 3 I 1 6 0 0 0 0 0 2 c.n • 80C51 Product Line ..... 80C31BH ROM lESS 128 1216 32 2 1 5 0 0 0 0 0 - Yes » :D 80C51BH 4KROM 128 1216 32 2 1 5 0 0 0 0 0 0 Yes 80C51BHP 4KROM 128 12,16 32 2 1 5 0 0 0 0 0 P Yes o 87C51 4KEPROM 128 12,16,20,24 32 1 ::::c 2 5 0 0 0 0 0 3 Yes i =i 8XC52154158 Product Line m .J:,.. 80C32 ROM lESS 256 12,16,20,24 I 32 3 1 6 0 0 0 0 0 Yes £l c :D 80C52 8KROM 256 12,16,20,24 i 32 3 1 6 0 0 0 0 0 1* Yes » r 87C52 8KEPROM 256 12,16,20,24 32 3 1 6 0 0 0 0 0 3 Yes o 80C54 16K ROM 256 i 12,16,20,24 32 3 1 6 0 0 0 0 0 1 Yes < m i :D 87C54 16K EPROM 256 12,16,20,24 32 3 1 6 0 0 0 0 0 3 Yes < i in 80C58 32KROM 256 12,16,20,24 32 3 1 6 0 0 0 0 0 1 Yes ~ i 87C58 32KEPROM 256 12,16,20,24 32 3 1 6 0 0 0 0 0 3 Yes I 8XL52154158 Product Line 80152 8KROM 256 12,16,20* 32 3 1 6 0 0 0 0 0 1 Yes 87152 8KOTP ROM 256 12,16,20* 32 3 1 6 0 0 0 0 0 3 Yes 80154 18K ROM 256 12,16,20* 32 3 1 6 0 0 0 0 0 1 Yes 87154 16KOTP ROM 256 12,16,20* 32 3 1 6 0 0 0 0 0 3 Yes i 80158 32KROM 256 12,16,20* 32 3 1 6 0 0 0 0 0 1 Yes=j 87158 32KOTP ROM 256 12,16,20* 32 3 1 6 0 0 0 0 0 3 Yes DEVICE ROMIEPROM Register Speed Table 1. The MeSaD 51 Family of Microcontrollers VO Timerl UART Interrupt PCA AID SEP GSC DMA Lock Power Down _. l I (bytes) RAM (MHz) Pins Counters Sources Channels Channels Channels Bits & Idle Modes . (bytes} 8XC51 FAlFB/FC Product Line 8 80C51FA ROMLESS 256 1216 32 3 1 7 5 0 0 0 0 Yes 83C51FA 8KROM 256 1216 32 3 1 7 5 0 0 0 0 0 Yes 87C51FA 8KEPROM 256 12,16,20,24 32 3 1 7 5 0 0 0 0 3 Yes I 83C51FB 16K ROM 256 12,16,20,24 32 3 1 7 5 0 0 0 0 1 Yes i 87C51FB 16K EPROM 256 12,16,20,24 32 3 1 7 5 0 0 0 0 3 Yes I 83C51FC 32K ROM 256 12,16,20,24 32 3 1 7 5 0 0 0 0 1 Yes I 87C51FC 32KEPROM 256 12,16,20,24 32 3 1 7 5 0 0 0 0 3 Yes I 3: (") 8XL51FAlFBIFC Product Line 80L51FA 83L51FA 87L51FA ROMLESS 8KROM 8KOTP ROM 256 256 256 12,16,20* 12,16,20* 12,16,20* 32 32 32 3 3 3 1 1 1 7 7 7 5 5 5 0 0 0 0 0 0 0 0 0 0 0 0 1 3 Yes Yes Yes en U1 ..I. . @> 83L51FB 16K ROM 256 12,16,20* 32 3 1 7 5 0 0 0 0 1 Yes l> 87L51FB 16KOTP ROM 256 12,16,20* 32 3 1 7 5 0 0 0 0 3 Yes :::0 (") 83L51FC 32KROM 256 12,16,20* 32 3 1 7 5 0 0 0 0 1 Yes ::J: 87L51FC 32KOTP ROM 256 121620* 32 3 1 7 5 0 0 0 0 3 Yes 8XC51 GX Product Line =i en 80C51GB ROMLESS 256 1216 48 3 1 15 10 8 1 0 0 Yes m (") 83C51GB 8KROM 256 12,16 48 3 1 15 10 8 1 0 0 1 Yes -I 87C51GB 8KEPROM 256 1216 48 3 1 15 10 8 1 0 0 I 3 I Yes c: 8XC152 Product Line* :::0 80C152JA ROMLESS 256 16.5 40 2 1 11 0 0 1 1 2 Yes l> r- 80C152JB ROMLESS 256 16.5 56 2 1 11 0 0 1 1 2 Yes o 83C152JA 8KROM 8XC51SL Product Line* 256 16.5 40 2 1 11 0 0 1 1 2 0 Yes < m 80C51SL-BG ROM LESS 256 16 24 2 1 10 0 4 0 1 0 Yes :::0 81C51SL-BG 8K*ROM 256 16 24 2 1 10 0 4 0 1 0 0 Yes < 83C51SL-BG 8KROM 256 16 24 2 1 10 0 4 1 in 80C51SLAH ROM LESS 256 16 24 2 1 10 0 4 0 0 1 0 0 0 Yes Yes :e 81C51SLAH 16K*ROM 256 16 24 2 1 10 0 4 0 1 0 0 Yes 83C51SLAH 16K ROM 256 16 24 2 1 10 0 4 0 1 0 0 Yes 87C51SLAH 16K EPROM 256 16 24 2 1 10 0 4 0 1 0 0 Yes 80C51SLAL ROM LESS 256 16 24 2 1 10 0 4 0 1 0 Yes 81C51SLAL 16K*ROM 256 16 24 2 1 10 0 4 0 1 0 0 Yes 83C51SLAL 16K ROM 256 16 24 2 1 10 0 4 0 1 0 0 Yes 87C51SLAL 16K EPROM 256 16 24 2 ___1_ .JO 0 4 0 1 0 0 Yes ROM/OTP ROM/EPROM (bytes): *ROM SystemSoft Standard BIOS Speed (Mhz): 241 24 MHz Internal-only operation 20* 20MHz Available for Commerlcal Temperature Range Only Lock Bits: 1* 1 Lock Bit for 20M Hz & 24MHz parts, no Lock Bit for 12 & 16MHz parts P Program verification disabled, external memory access limited to 4K 8XC152 Product Line* Communication Controller 8XC51SL Product Line* Keyboard Controller intel~ MCS®-S1 ARCHITECTURAL OVERVIEW PROGRAM MEMORY DATA MEMORY (READ ONLY) (READ/WRITE) ------------------------ F'F'F'F'H: r - - - - , --------------------------- F'F'F'F'H:--- EXTERNAL EXTERNAL EA=O EXTERNAL EA=l INTERNAL mrnl~ I ....._ _....._ 0000 .........._ _... 00 0000 ~_--' 270251-2 Figure 2. MCS®-51 Memory Structure CHMOS Devices MEMORY ORGANIZATION IN MCS®-51 DEVICES Functionally, the CHMOS devices (designated with "c" in the middle of the device name) are all fully compatible with the 8051, but being CMOS, draw less Logical Separation of Program and current than an HMOS counterpart. To further exploit Data Memory the power savings available in CMOS circuitry, two re- duced power modes are added: All MCS-51 devices have separate address spaces for • Software-invoked Idle Mode, during which the CPU Program and Data Memory, as shown in Figure 2. The is turned off while the RAM and other on-chip logical separation of Program and Data Memory allows peripherals continue operating. In this mode, cur- the Data Memory to be accessed by 8-bit addresses, rent draw is reduced to about 15% of the current which can be more quickly stored and manipulated by drawn when the device is fully active. an 8-bit CPU. Nevertheless, 16-bit Data Memory ad- dresses can also be generated through the DPTR regis- • Software-invoked Power Down Mode, during which ter. all on-chip activities are suspended. The on-chip RAM continues to hold its data. In this mode the Program Memory can only be read, not written to. device typically draws less than 10 p.A. There can be up to 64K bytes of Program Memory. In Although the 80C51 BH is functionally compatible with the ROM and EPROM versions of these devices the its HMOS counterpart, specific differences between the lowest 4K, 8K or 16K bytes of Program Memory are two types of devices must be considered in the design of provided on-chip. Refer to Table 1 for the amount of an application circuit if one wishes to ensure complete on-chip ROM (or EPROM) on each device. In the interchangeability between the HMOS and CHMOS ROMless versions all Program Memory is external. devices. These considerations are discussed in the Ap- The read strobe for external Program Memory is the plication Note AP-252, "Designing with the signal PSEN (program Store Enable). 80C51BH". For more information on the individual devices and features listed in Table 1, refer to the Hardware De- scriptions and Data Sheets of the specific device. 1-6 intel~ MCS®-S1 ARCHITECTURAL OVERVIEW Data Memory occupies a separate address space from The lowest 4K (or 8K or 16K) bytes of Program Mem- Program Memory. Up to 64K bytes of external RAM ory can be either in the on-chip ROM or in an external can be addressed in the external Data Mem012..!Pace. ROM. This selection is made by strapping the EA (Ex- The CPU generates read and write signals, RD and ternal Access) pin to either Vcc or Vss. WR, as needed during external Data Memory accesses. In the 4K byte ROM devices, if the EA pin is strapped External Program Memory and external Data Memory to Vce, then program fetches to addresses OOOOH may be combined if desired by applying the RD and through OFFFH are directed to the internal ROM. Pro- PSEN signals to the inputs of an AND gate and using gram fetches to addresses l000H through FFFFH are the output of the gate as the read strobe to the external directed to external ROM. Program/Data memory. In the 8K byte ROM devices, EA = Vee selects ad- dresses OOOOH through IFFFH to be internal, and ad- Program Memory dresses 2000H through FFFFH to be external. Figure 3 shows a map of the lower part of the Program In the 16K byte ROM devices, EA = Vec selects ad- Memory. After reset, the CPU begins execution from dresses OOOOH through 3FFFH to be internal, and ad- location OOOOH. dresses 4000H through FFFFH to be external. As shown in Figure 3, each interrupt is assigned a fixed If the EA pin is strapped to Vss, then all program location in Program Memory. The interrupt causes the fetches are directed to external ROM. The ROMless CPU to jump to that location, where it commences exe- parts must have this pin externally strapped to Vss to cution of the service routine. External Interrupt 0, for enable them to execute properly. example, is assigned to location 0003H. If External In- terrupt 0 is going to be used, its service routine must The read strobe to external ROM, PSEN, is used for all begin at location 0003H. If the interrupt is not going to external program fetches. PSEN is not activated for in- be used, its service location is available as general pur- ternal program fetches. pose Program Memory. EPROM ..-_ _ _ _-t INSTR • 002BH 0023H INTERRUPT LOCATIONS 00lBH=r 8 BYTES 0013H OOOBH 270251-4 0003H Figure 4. Executing from External OOOOH '-----' Program Memory 270251-3 The hardware configuration for external program exe- Figure 3. MCS@·S1 Program Memory cution is shown in Figure 4. Note that 16 I/O lines (ports 0 and 2) are dedicated to bus functions during The interrupt service locations are spaced at 8-byte in- external Program Memory fetches. Port 0 (PO in Figure tervals: 0003H for External Interrupt 0, OOOBH for 4) serves as a multiplexed address/data bus. It emits Timer 0, 0013H for External Interrupt 1, OOlBH for the low byte of the Program Counter (PCL) as an ad- Timer 1, etc. If an interrupt service routine is short dress, and then goes into a float state awaiting the arriv- enough (as is often the case in control applications), it al of the code byte from the Program Memory. During can reside entirely within that 8-byte interval. Longer the time that the low byte of the Program Counter is service routines can use a jump instruction to skip over valid on PO, the signal ALE (Address Latch Enable) subsequent interrupt locations, if other interrupts are in clocks this byte into an address latch. Meanwhile, Port use. 2 (P2 in Figure 4) emits the high byte of the Program Counter (peH). Then PSEN strobes the EPROM and the code byte is read into the microcontroller. 1-7 intelGD MCS®·51 ARCHITECTURAL OVERVIEW Program Memory addresses are always 16 bits wide, Internal Data Memory is mapped in Figure 6. The even though the actual amount of Program Memory memory space is shown divided into three blocks, used may be less than 64K bytes. External program which are generally referred to as the Lower 128, the execution sacrifices two of the 8-bit ports, PO and P2, to Upper 128, and SFR space. the function of addressing the Program Memory. Internal Data Memory addresses are always one byte wide, which implies an address space of only 256 bytes. Data Memory However, the addressing modes for internal RAM can in fact accommodate 384 bytes, using a simple trick. The right half of Figure 2 shows the internal and exter- Direct addresses higher than 7FH access one memory nal Data Memory spaces available to the MCS-51 user. space, and indirect addresses higher than 7FH access a different memory space. Thus Figure 6 shows the Up- Figure 5 shows a hardware configuration for accessing per 128 and SFR space occupying the same block of up to 2K bytes of external RAM. The CPU in this case addresses, 80H through FFH, although they are physi- is executing from internal ROM. Port 0 serves as a cally separate entities. multiplexed address/data bus to the RAM, and 3 lines of Port 2 are being used to page the RAM. The CPU generates RD and WR signals as needed during exter- 7FH nal RAM accesses. BANK 2FH ....-----,A DATA SELECT BITS IN psw~ 20H Jr. IT-ADDRESSABLE SPACE IT ADDRESSES 0-7F) lFH 11{ 18H IO{ 10H 17H BANKS OF" 01 { OF"H " 8 REGISTERS R0-R7 08H 07H fo-~ RESET VALUE OF oo{ 0 STACK POINTER 270251-7 270251-5 Figure 7. The Lower 128 Bytes of Internal RAM Figure 5. Accessing External Data Memory. The Lower 128 bytes of RAM are present in all If the Program Memory is Internal, the Other MCS-51 devices as mapped in Figure 7. The lowest 32 Bits of P2 are Available as 1/0. bytes are grouped into 4 banks of 8 registers. Program instructions call out these registers as RO through R 7. There can be up to 64K bytes of external Data Memo- Two bits in the Program Status Word (pSW) select ry. External Data Memory addresses can be either 1 or which register bank is in use. This allows more efficient 2 bytes wide. One-byte addresses are often used in con- use of code space, since register instructions are shorter junction with one or more other I/O lines to page the than instructions that use direct addressing. RAM, as shown in Figure 5. Two-byte addresses can also be used, in which case the high address byte is emitted at Port 2. FFH FFH·--------,....----.., FFH NO BIT-ADDRESSABLE : ACCESSIBLE ACCESSIBLE SPACES UPPER • BY INDIRECT BY DIRECT 128 • ADDRESSING ADDRESSING AVAILABLE AS STACK _0_N_LY_+~--.,80H SPACE IN DEVICES WITH 80H:.. 256 BYTES RAW 7FH ACCESSIBLE '\ ' - SPECIAL } PORTS NOT IIAPLEt.4ENTED IN 8051 L~~!R A:ri 1~~~~6T FUNCTION STAWS AND ADDRESSING REGISTERS CONTROL BITS 0'--_ _--' TIIAER REGISTERS STACK POINTER 80H ACCUIAULATOR 270251-8 (ETC.) 270251-6 Figure 8. The Upper 128 Bytes of Internal RAM Figure 6. Internal Data Memory 1-8 inteL MCS®-51 ARCHITECTURAL OVERVIEW PSW 7 PSW 0 CARRY F'LAG RECEIVES CARRY OUT PARITY OF' ACCUMULATOR SET F'ROM BIT 1 OF' ALU OPERANDS BY HARDWARE TO 1 IF' IT CONTAINS AN 000 NUMBER OF' 1S. OTHERWISE IT IS RESET TO 0 PSW 6 PSW 1 AUXILIARY CARRY F'LAG RECEIVES USER DEF'INABLE rLAG CARRY OUT F'RO~ BIT 1 OF' ADDITION OPERANDS PSW5-----l L -_ _ _ PSW2 GENERAL PURPOSE STATUS F'LAG OVERF'LOW F'LAG SET BY ARITH~ETIC OPERATIONS PSW4-------- L -_ _ _ _ _ PSW 3 REGISTER BANK SELECT BIT 1 REGISTER BANK SELECT BIT 0 270251-10 Figure 10. PSW (Program Status Word) Register in MCS@·51 Devices The next 16 bytes above the register banks form a block Sixteen addresses in SFR space are both byte- and bit- of bit-addressable memory space. The MCS-51 instruc- addressable. The bit-addressable SFRs are those whose tion set includes a wide selection of single-bit instruc- address ends in OOOB. The bit addresses in this area are tions, and the 128 bits in this area can be directly ad- 80H through FFH. dressed by these instructions. The bit addresses in this area are OOH through 7FH. THE MCS®-51 INSTRUCTION SET All of the bytes in the Lower 128 can be accessed by either direct or indirect addressing. The Upper 128 All members of the MCS-51 family execute the same (Figure 8) can only be accessed by indirect addressing. instruction set. The MCS-51 instruction set is opti- The Upper 128 bytes of RAM are not implemented in mized for 8-bit control applications. It provides a vari- the 8051, but are in the devices with 256 bytes of RAM. ety of fast addressing modes for accessing the internal (See Table 1). RAM to facilitate byte operations on small data struc- tures. The instruction set provides extensive support for Figure 9 gives a brief look at the Special Function Reg- one-bit variables as a separate data type, allowing direct ister (SFR) space. SFRs include the Port latches, tim- bit manipulation in control and logic systems that re- ers, peripheral controls, etc. These registers can only be quire Boolean processing. accessed by direct addressing. In general, all MCS-51 microcontrollers have the same SFRs as the 8051, and An overview of the MCS-51 instruction set is presented at the same addresses in SFR space. However, enhance- below, with a brief description of how certain instruc- ments to the 8051 have additional SFRs that are not tions might be used. References to "the assembler" in present in the 8051, nor perhaps in other proliferations this discussion are to Intel's MCS-51 Macro Assembler, of the family. ASM51. More detailed information on the instruction set can be found in the MCS-51 Macro Assembler Us- er's Guide (Order No. 9800937 for ISIS Systems, Order No. 122752 for DOS Systems). F'F'H I REGISTER-MAPPED PORTS EOH ACC ADDRESSES THAT END IN Program Status Word I OH OR 8H ARE ALSO I BIT-ADDRESSABLE The Program Status Word (PSW) contains several BOH PORT 3 status bits that reflect the current state of the CPU. The -PORT PINS I -ACCU~ULATOR PSW, shown in Figure 10, resides in SFR space. It con- -PSW tains the Carry bit, the Auxiliary Carry (for BCD oper- AOH PORT 2 (ETC.) ations), the two register bank select bits, the Overflow flag, a Parity bit, and two user-defmable status flags. 90H PORT 1 I The Carry bit, other than serving the functions of a SOH PORT 0 Carry bit in arithmetic operations, also serves as the "Accumulator" for a number of Boolean operations. 270251-9 Figure 9. SFR Space 1-9 inteiaD MCS®·51 ARCHITECTURAL OVERVIEW The bits RSO and RS 1 are used to select one of the four IMMEDIATE CONSTANTS register banks shown in Figure 7. A number of instruc- tions refer to these RAM locations as RO through R 7. The value of a constant can follow the opcode in Pro- The selection of which of the four banks is being re- gram Memory. For example, . ferred to is made on the basis of the bits RSO and RS 1 at execution time. MOV A, #100 The Parity bit reflects the number of Is in the Accumu- loads the Accumulator with the decimal number 100. lator: P = 1 if the Accumulator contains an odd num- The same number could be specified in hex digits as ber of 1s, and P = 0 if the Accumulator contains an 64H. even number of Is. Thus the number of Is in the Accu- mulator plus P is always even. INDEXED ADDRESSING Two bits in the PSW are uncommitted and may be used Only Program Memory can be accessed with indexed as general purpose status flags. addressing, and it can only be read. This addressing mode is intended for reading look-up tables in Program Memory. A 16-bit base register (either DPTR or the Addressing Modes Program Counter) points to the base of the table, and the Accumulator is set up with the table entry number. The addressing modes in the MCS-51 instruction set The address of the table entry in Program Memory is are as follows: formed by adding the Accumulator data to the base pointer. DIRECT ADDRESSING Another type of indexed addressing is used in the "case In direct addressing the operand is specified by an 8-bit jump" instruction. In this case the destination address address field in the instruction. Only internal Data of a jump instruction is computed as the sum of the RAM and SFRs can be directly addressed. base pointer and the Accumulator data. INDIRECT ADDRESSING Arithmetic Instructions In indirect addressing the instruction specifies a register The menu of arithmetic instructions is listed in Table 2. which contains the address of the operand. Both inter- The table indicates the addressing modes that can be nal and external RAM can be indirectly addressed. used with each instruction to access the oper- and. For example, the ADD A, instruction can The address register for 8-bit addresses can be RO or be written as: Rl of the selected register bank, or the Stack Pointer. The address register for 16-bit addresses can only be the ADD A,7FH (direct addressing) 16-bit "data pointer" register, DPTR. ADD A,@RO (indirect addressing) ADD A,R7 (register addressing) REGISTER INSTRUCTIONS ADD A,#127 (immediate constant) The register banks, containing registers RO through R 7, The execution times listed in Table 2 assume a 12 MHz can be accessed by certain instructions which carry a clock frequency. All of the arithmetic instructions exe- 3-bit register specification within the opcode of the in- cute in 1 JJ.s except the INC DPTR instruction, which struction. Instructions that access the registers this way takes 2 p.s, and the Multiply and Divide instructions, are code efficient, since this mode eliminates an address which take 4 JJ.s. byte. When the instruction is executed, one of the eight registers in the selected bank is accessed. One of four Note that any byte in the internal Data Memory space banks is selected at execution time by the two bank can be incremented or decremented without going select bits in the PSW. through the Accumulator. One of the INC instructions operates on the 16-bit REGISTER-SPECIFIC INSTRUCTIONS Data Pointer. The Data Pointer is used to generate Some instructions are specific to a certain register. For 16-bit addresses for external memory, so being able to example, some instructions always operate on the Ac- increment it in one 16-bit operation is a useful feature. cumulator, or Data Pointer, etc., so no address byte is needed to point to it. The opcode itself does that. In- The MUL AB instruction multiplies the Accumulator structions that refer to the Accumlator as A assemble by the data in the B register and puts the 16-bit product as accumulator-specific opcodes. into the concatenated B and Accumulator registers. 1-10 intet MCS®-S1 ARCHITECTURAL OVERVIEW Table 2. A List of the MCS®-S1 Arithmetic Instructions Addressing Modes Execution Mnemonic Operation Time (/-Ls) Dir Ind Reg Imm ADD A, A = A + X X X X 1 ADDC A, < byte> A = A + < byte> + C X X X X 1 SUBB A, A = A - < byte> - C X X X X 1 INC A A=A+1 Accumulator only 1 INC. = + 1 X X X 1 INC DPTR DPTR = DPTR + 1 Data Pointer only 2 DEC A A=A-1 Accumulator only 1 DEC = - 1 X X X 1 MUL AB B:A = BxA ACC and B only 4 DIV AB A = Int [AlB] 4 ACC and B only B = Mod [AlB] DA A Decimal Adjust Accumulator only 1 The DIY AB instruction divides the Accumulator by completes the shift in 4 /-Ls and leaves the B register the data in the B register and leaves the 8-bit quotient holding the bits that were shifted out. in the Accumulator, and the 8-bit remainder in the B register. The DA A instruction is for BCD arithmetic opera- tions. In BCD arithmetic, ADD and ADDC instruc- Oddly enough, DIY AB finds less use in arithmetic tions should always be followed by a DA A operation, "divide" routines than in radix conversions and pro- to ensure that the result is also in BCD. Note that DA grammable shift operations. An example of the use of A will not convert a binary number to BCD. The DA DIY AB in a radix conversion will be given later. In A operation produces a meaningful result only as the shift operations, dividing a number by 2n shifts its n second step in the addition of two BCD bytes. bits to the right. Using DIY AB to perform the division Table 3. A List of the MCS®-S1 Logical Instructions Addressing Modes Execution Mnemonic Operation Reg Imm Time (/-Ls) Dir Ind ANL A, A = A .AND. X X X X 1 ANL ,A = .AND.A X 1 ANL < byte> , # data = .AND. #data X 2 ORL A, A = A .OR. X X X X 1 ORL ,A = .OR.A X 1 ORL , # data = .OR. #data X 2 XRL A, A = A .XOR. X X X X 1 XRL ,A = .XOR.A X 1 XRL , # data = .XOR. #data X 2 CRL A A = OOH Accumulator only 1 CPL A A = .NOT.A Accumulator only 1 RL A Rotate ACC Left 1 bit Accumulator only 1 ALC A Rotate Left through Carry Accumulator only 1 AR A Rotate ACC Right 1 bit Accumulator only 1 ARC A Rotate Right through Carry Accumulator only 1 SWAP A Swap Nibbles in A Accumulator only 1 1-11 intelQD MCS®-51 ARCHITECTURAL OVERVIEW Logical Instructions The SWAP A instruction interchanges the high and low nibbles within the Accumulator. This is a useful Table 3 shows the list of MCS-51 logical instructions. operation in BCD manipulations. For example, if the The instructions that perfonn Boolean operations Accumulator contains a binary number which is known (AND, OR, Exclusive OR, NOT) on bytes perform the to be less than 100, it can be quickly converted to BCD operation on a bit-by-bit basis. That is, if the Accumu- by the following code: lator contains 00llOlO1B and contains 01010011B, then MOY B,#l0 DIY AB ANL A, SWAP A ADD A,B will leave the Accumulator holding 00010001B. Dividing the number by 10 leaves the tens digit in the The addressing modes that can be used to access the low nibble of the Accumulator, and the ones digit in the operand are listed in Table 3. Thus, the ANL B register. The SWAP and ADD instructions move the A, instruction may take any of the fonns tens digit to the high nibble of the Accumulator, and the ones digit to the low nibble. ANL A,7FH (direct addressing) ANL A,@R1 (indirect addressing) ANL A,R6 (register addressing) Data Transfers ANL A,#53H (immediate constant) INTERNAL RAM All of the logical instructions that are Accumulator- specific execute in 1JLs (using a 12 MHz clock). The Table 4 shows the menu of instructions that are avail- others take 2 JLs. able for moving data around within the internal memo- ry spaces, and the addressing modes that can be used Note that Boolean operations can be perfonned on any with each one. With a 12 MHz clock, all of these in- byte in the lower 128 internal Data Memory space or structions execute in either 1 or 2 JLs. the SFR space using direct addressing, without having to use the Accumulator. The XRL , #data in- The MOV < dest > , instruction allows data to struction, for example, offers a quick and easy way to be transferred between any two internal RAM or SFR invert port bits, as in locations without going through the Accumulator. Re- member the Upper 128 byes of data RAM can be ac- XRL PI, #OFFH cessed only by indirect addressing, and SFR space only by direct addressing. If the operation is in response to an interrupt, not using the Accumulator saves the time and effort to stack it in Note that in all MCS-51 devices, the stack resides in the service routine. on-chip RAM, and grows upwards. The PUSH instruc- tion first increments the Stack Pointer (SP), then copies The Rotate instructions (RL A, RLC A, etc.) shift the the byte into the stack. PUSH and POP use only direct Accumulator 1 bit to the left or right. For a left rota- addressing to identify the byte being saved or restored, tion, the MSB rolls into the LSB position. For a right rotation, the LSB rolls into the MSB position. Table 4. A List of the MCS@·51 Data Transfer Instructions that Access Internal Data Memory Space Addressing Modes Execution Mnemonic Operation Dir Ind Reg Imm Time (JLs) MOV A, A= X X X X 1 MOV ,A = A X X X 1 MOV , = X X X X 2 MOV DPTR,#data16 DPTR = 16-bit immediate constant. X 2 PUSH INC SP: MOV "@SP", X 2 POP MOV , "@SP" : DEC SP X 2 XCH A, ACC and < byte> exchange data X X X 1 XCHD A,@Ri ACC and @Ri exchange low nibbles X 1 1-12 intel~ MCS®-51 ARCHITECTURAL OVERVIEW but the stack itself is accessed by indirect addressing After the routine has been executed, the Accumulator using the SP register. This means the stack can go into contains the two digits that were shifted out on the the Upper 128, if they are implemented, but not into right. Doing the routine with direct MOVs uses 14 code SFR space. bytes and 9 J-Ls of execution time (assuming a 12 MHz clock). The same operation with XCHs uses less code In devices that do not implement the Upper 128, if the and executes almost twice as fast. SP points to the Upper 128, PUSHed bytes are lost, and POPped bytes are indeterminate. To right-shift by an odd number of digits, a one-digit shift must be executed. Figure 12 shows a sample of The Data Transfer instructions include a l6-bit MOV code that will right-shift a BCD number one digit, us- that can be used to initialize the Data Pointer (DPTR) ing the XCHD instruction. Again, the contents of the for look-up tables in Program Memory, or for l6-bit registers holding the number and of the Accumulator external Data Memory accesses. are shown alongside each instruction. The XCH A, instruction causes the Accumu- lator and addressed byte to exchange data. The XCHD A,@Ri instruction is similar, but only the low nibbles MOV R1,#2EH are involved in the exchange. Mav RO,#2DH loop for R1 = 2EH: To see how XCH and XCHD can be used to facilitate LOOP: MOV A,@R1 00 12 34 56 78 78 data manipulations, consider first the problem of shift- XCHD A,@RO 00 12 34 58 78 76 ing an 8-digit BCD number two digits to the right. Fig- SWAP A 00 12 34 58 78 67 ure 11 shows how this can be done using direct MOVs, MaV @R1,A 00 12 34 58 67 67 and for comparison how it can be done using XCH DEC R1 00 12 34 58 67 67 instructions. To aid in understanding how the code DEC RO 00 12 34 58 67 67 works, the contents of the registers that are holding the CJNE R1,#2AH,LOOP I ggl~~I~~I:;I~~1 ~~ BCD number and the content of the Accumulator are loop for R1 = 2DH: shown alongside each instruction to indicate their loop for R1 = 2CH: status after the instruction has been executed. loop for R1 = 28H: 08 01 23 45 67 01 CLR A 081011231451671 00 XCH A,2AH 100 01 23 45 67 08 2A 28 2C 20 2E ACC MOV A,2EH 00 12 34 56 78 78 Figure 12. Shifting a BCD Number MOV 2EH,2DH 00 12 34 56 56 78 One Digit to the Right MOV 2DH,2CH 00 12 34 34 56 78 MOV 2CH,28H 00 12 12 34 56 78 First, pointers R 1 and RO are set up to point to the two MaV 2BH,#0 00 00 12 34 56 78 bytes containing the last four BCD digits. Then a loop (a) Using direct MOVs: 14 bytes, 9 J-Ls is executed which leaves the last byte, location 2EH, 2A 28 2C 20 2E ACC holding the last two digits of the shifted number. The CLR A 00 12 34 56 78 00 pointers are decremented, and the loop is repeated for XCH A,2BH 00 00 34 56 78 12 location 2DH. The CJNE instruction (Compare and XCH A,2CH 00 00 12 56 78 34 Jump if Not Equal) is a loop control that will be de- XCH A,2DH 00 00 12 34 78 56 scribed later. XCH A,2EH 00 00 12 34 56 78 (b) USing XCHs: 9 bytes, 5 p.s The loop is executed from LOOP to CJNE for Rl = 2EH, 2DH, 2CH and 2BH. At that point the digit that Figure 11. Shifting a BCD Number was originally shifted out on the right has propagated Two Digits to the Right to location 2AH. Since that location should be left with Os, the lost digit is moved to the Accumulator. 1-13 MCS®-51 ARCHITECTURAL OVERVIEW EXTERNAL RAM Table 6. The MCS®-51 Lookup Table 5 shows a list of the Data Transfer instructions . ---- . ---- ..._-- -_ ... _....... T~hlp Rp~rf In!lltrll~tinn!ll Execution that access external Data Memory. Only indirect ad- Mnemonic Operation Time (/J-s) dressing can be used. The choice is whether to use a MOVe A,@A+DPTR Read Pgm Memory 2 one-byte address, @Ri, where Ri can be either RO or at (A+DPTR) Rl of the selected register bank, or a two-byte address, @DPTR. The disadvantage to using 16-bit addresses if MOVC A,@A+PC Read Pgm Memory 2 at (A+PC) only a few K bytes of external RAM are involved is that 16-bit addresses use all 8 bits of Port 2 as address bus. On the other hand, 8-bit addresses allow one to The first MOVC instruction in Table 6 can accommo- address a few K bytes of RAM, as shown in Figure 5, date a table of up to 256 entries, numbered 0 through without having to sacrifice all of Port 2. 255. The number of the desired entry is loaded into the Accumulator, and the Data Pointer is set up to point to All of these instructions execute in 2 /Ls, with a beginning of the table. Then 12 MHz clock. Table 5. A List of the MCS®-51 Data MOVC A,@A+DPTR Transfer Instructions that Access External Data Memory Space copies the desired table entry into the Accumulator. Address Execution The other MOVC instruction works the same way, ex- Mnemonic Operation Width Time (/J-s) cept the Program Counter (PC) is used as the table 8 bits MOVXA,@Ri Read external 2 base, and the table is accessed through a subroutine. RAM@Ri First the number of the desired entry is loaded into the Write external Accumulator, and the subroutine is called: 8 bits MOVX@Ri,A 2 RAM@Ri Read external MOV A,ENTRY_NUMBER 16 bits MOVX A,@DPTR 2 RAM@DPTR CALL TABLE Writeextemal 16 bits MOVX @DPTR,A 2 The subroutine "TABLE" would look like this: RAM@DPTR TABLE: MOVC A,@A + PC Note that in all external Data RAM accesses, the Ac- RET cumulator is always either the destination or source of the data. The table itself immediately follows the RET (return) instruction in Program Memory. This type of table can The read and write strobes to external RAM are acti- have up to 255 entries, numbered 1 through 255. Num- vated only during the execution of a MOVX instruc- ber 0 can not be used, because at the time the MOVC tion. Nonnally these signals are inactive, and in fact if instruction is executed, the PC contains the address of they're not going to be used at all, their pins are avail- the RET instruction. An entry numbered 0 would be able as extra 1/0 lines. More about that later. the RET opcode itself. LOOKUP TABLES Boolean Instructions Table 6 shows the two instructions that are available for reading lookup tables in Program Memory. Since MCS-51 devices contain a complete Boolean (single-bit) these instructions access only Program Memory, the processor. The internal RAM contains 128 addressable lookup tables can only be read, not updated. The mne- bits, and the SFR space can support up to 128 other monic is MOVC for "move constant". addressable bits. All of the port lines are bit-address- able, and each one can be treated as a separate single- If the table access is to external Program Memory, then bit port. The instructions that access these bits are not the read strobe is PSEN. just conditional branches, but a complete menu of move, set, clear, complement, OR, and AND instruc- tions. These kinds of bit operations are not easily ob- tained in other architectures with any amount of byte- oriented software. 1-14 intel~ MCS®-S1 ARCHITECTURAL OVERVIEW Table 7. A List of the MCS®-S1 Note that the Boolean instruction set includes ANL Boolean Instructions and ORL operations, but not the XRL (Exclusive OR) operation. An XRL operation is simple to implement in Execution software. Suppose, for example, it is required to form Mnemonic Operation Time (J.Ls) the Exclusive OR of two bits: ANL C,bit C = C .AND. bit 2 C = bit! .xRL. bit2 ANL C,/bit C = C .AND .. NOT. bit 2 ORL C,bit C = C .OR. bit 2 The software to do that could be as follows: ORL C,/bit C = C .OR. .NOT. bit 2 MOV C,bitl MOV C,bit C = bit 1 JNB bit2,OVER MOV bit,C bit = C 2 CPL C OVER: (continue) CLR C C=O 1 CLR bit bit = 0 1 First, bit! is moved to the Carry. If bit2 = 0, then C SETB C C=1 1 now contains the correct result. That is, bitl .XRL. bit2 = bit 1 if bit2 = O. On the other hand, if bit2 = 1 C SETB bit bit = 1 1 now contains the complement of the correct result. It CPL C C = .NOT.C 1 need only be inverted (CPL C) to complete the opera- tion. CPL bit bit = .NOT. bit 1 JC rei Jump ifC = 1 2 This code uses the JNB instruction, one of a series of JNC rei Jump ifC = 0 2 bit-test instructions which execute a jump if the ad- dressed bit is set (JC, JB, JBC) or if the addressed bit is JB bit,rel Jump if bit = 1 2 not set (JNC, JNB). In the above case, bit2 is being JNB bit, rei Jump if bit = 0 2 tested, and if bit2 = 0 the CPL C instruction is jumped JBC bit,rel Jump if bit = 1; CLR bit 2 over. JBC executes the jump if the addressed bit is set, and The instruction set for the Boolean processor is shown also clears the bit. Thus a flag can be tested and cleared in Table 7. All bit accesses are by direct addressing. Bit in one operation. addresses OOH through 7FH are in the Lower 128, and bit addresses 80H through FFH are in SFR space. AIl the PSW bits are directly addressable, so the Parity bit, or the general purpose flags, for example, are also Note how easily an internal flag can be moved to a port available to the bit-test instructions. pin: MOV C,FLAG RELATIVE OFFSET MOV P1.0,C The destination address for these jumps is specified to In this example, FLAG is the name of any addressable the assembler by a label or by an actual address in bit in the Lower 128 or SFR space. An I/O line (the Program Memory. However, the destination address LSB of Port 1, in this case) is set or cleared depending assembles to a relative offset byte. This is a signed on whether the flag bit is 1 or o. (two's complement) offset byte which is added to the PC in two's complement arithmetic if the jump is exe- The Carry bit in the PSW is used as the single-bit Accu- cuted. mulator of the Boolean processor. Bit instructions that refer to the Carry bit as C assemble as Carry-specific The range of the jump is therefore -128 to + 127 Pro- instructions (CLR C, etc). The Carry bit also has a gram Memory bytes relative to the first byte following direct address, since it resides in the PSW register, the instruction. which is bit-addressable. 1-15 intel~ MCS®-51 ARCHITECTURAL OVERVIEW Jump Instructions the Accumulator. Typically, DPTR is set up with the address of a jump table, and the Accumulator is given Table 8 shows the list of unconditional jumps. an index to the table. In a 5-way branch, for example, an integer 0 through 4 is loaded into the Accumulator. Table 8. Unconditional Jumps The code to be executed might be as follows: in MCS®-51 Devices Execution MOY DPTR, # JUMP_TABLE Mnemonic Operation MOY A,INDE"-NUMBER Time (p.s) RL A JMP addr Jump to addr 2 @A+DPTR JMP JMP @A+DPTR Jump to A + DPTR 2 CAll addr Call subroutine at addr 2 The RL A instruction converts the index number (0 through 4) to an even number on the range 0 through 8, RET Return from subroutine 2 because each entry in the jump table is 2 bytes long: RETI Return from interrupt 2 NOP No operation 1 JUMP_TABLE: AJMP CASE_O AJMP CASE_l The Table lists a single "JMP addr" instruction, but in AJMP CASE_2 fact there are three-SJMP, UMP and AJMP-which AJMP CASE_3 differ in the format of the destination address. JMP is a AJMP CASE_4 generic mnemonic which can be used if the program- mer does not care which way the jump is encoded. Table 8 shows a single "CALL addr" instruction, but there are two of them-LCALL and ACALL-which The SJMP instruction encodes the destination address differ in the format in which the subroutine address is as a relative offset, as described above. The instruction given to the CPU. CALL is ~ generic mnemoni~ which is 2 bytes long, consisting of the opcode and the relative can be used if the programmer does not care WhICh way offset byte. The jump distance is limited to a range of the address is encoded. -128 to + 127 bytes relative to the instruction follow- ing the SJMP. The LCALL instruction uses the 16-bit address format, and the subroutine can be anywhere in the 64K Pro- The UMP instruction encodes the destination address gram Memory space. The ACALL instruction uses the as a 16-bit constant. The instruction is 3 bytes long, ll-bit format, and the subroutine must be in the same consisting of the opcode and two address bytes. The 2K block as the instruction following the ACALL. destination address can be anywhere in the 64K Pro- gram Memory space. In any case the programmer specifies the subroutine address to the assembler in the same way: as a label or The AJMP instruction encodes the destination address as a 16-bit constant. The assembler will put the address as an II-bit constant. The instruction is 2 bytes long, into the correct format for the given instructions. consisting of the opcode, which itself contains 3 of the 11 address bits, followed by another byte containing the Subroutines should end with a RET instruction, which low 8 bits of the destination address. When the instruc- returns execution to the instruction following the tion is executed, these 11 bits are simply substituted for CALL. the low 11 bits in the Pc. The high 5 bits stay the same. Hence the destination has to be within the same 2K RETI is used to return from an interrupt service rou- block as the instruction following the AIMP. tine. The only difference between RET and RETI is that RET! tells the interrupt control system that the In all cases the programmer specifies the destination interrupt in progress is done. If there is no interrupt in address to the assembler in the same way: as a label or progress at the time RETI is executed, then the RETI as a 16-bit constant. The assembler will put the destina- is functionally identical to RET. tion address into the correct format for the given in- struction. If the format required by the instruction will Table 9 shows the list of conditional jumps available to not support the distance to the specified destination ad- the MCS-51 user. All of these jumps specify the desti- dress, a "Destination out of range" message is written nation address by the relative offset method, and so are into the List file. limited to a jump distance of -128 to + 127 bytes from the instruction following the conditional jump instruc- The JMP @A + DPTR instruction supports case tion. Important to note, however, the user specifies to jumps. The destination address is computed at execu- the assembler the actual destination address the same tion time as the sum of the 16-bit DPTR register and way as the other jumps: as a label or a 16-bit constant. 1-16 intet MCS®-S1 ARCHITECTURAL OVERVIEW Table 9. Conditional Jumps in MCS®-S1 Devices Addressing Modes Execution Mnemonic Operation Dir Ind Reg Imm Time (JLs) JZ rei Jump if A = 0 Accumulator only 2 JNZ rei Jump if A "* 0 Accumulator only 2 DJNZ ,rei Decrement and jump if not zero X X 2 CJNE A, ,rei Jump if A"* X X 2 CJNE ,#data,rel Jump if "* #data X X 2 There is no Zero bit in the PSW. The JZ and JNZ instructions test the Accumulator data for that condi- (]!I Mes -51 tion. HMOS OR CHMOS ~P-----4 XTAL2 The DJNZ instruction (Decrement and Jump if Not QUAR~ ~~~~~"""\c:::I Zero) is for loop control. To execute a loop N times, RESONATOR load a counter byte with N and terminate the loop with a DJNZ to the beginning of the loop, as shown below for N = 10: = ....._-_. . 270251-11 MOV COUNTER,# 10 LOOP: (begin loop) Figure 13. Using the On-Chip Oscillator * (]!I (end loop) MC5 -51 HMOS DJNZ COUNTER,LOOP OR CHMOS (continue) The CJNE instruction (Compare and Jump if Not EXTERNAl Equal) can also be used for loop control as in Figure 12. CLOCK SIGNAL Two bytes are specified in the operand field of the in- struction. The jump is executed only if the two bytes are not equal. In the example of Figure 12, the two 270251-12 bytes were the data in Rl and the constant 2AH. The A. HMOS or CHMOS initial data in Rl was 2EH. Every time the loop was executed, R 1 was decremented, and the looping was to (]!I MC5 -51 continue until the Rl data reached 2AH. HMOS ONLY EXTERNAL CLOCK Another application of this instruction is in "greater SIGNAl than, less than" comparisons. The two bytes in the op- erand field are taken as unsigned integers. If the first is less than the second, then the Carry bit is set (1). If the first is greater than or equal to the second, then the Carry bit is cleared. 270251-13 B.HMOSOnly CPU TIMING (]!I MCS -51 CHMOS All MCS-51 microcontrollers have an on-chip oscillator ONLY which can be used if desired as the clock source for the CPU. To use the on-chip oscillator, connect a crystal or ceramic resonator between the XTALI and XTAL2 EXTERNAL pins of the microcontroller, and capacitors to ground as CLOCK SIGNAL shown in Figure 13. 270251-14 C. CHMOS Only Figure 14. Using an External Clock 1-17 intel~ MCS®-51 ARCHITECTURAL OVERVIEW Examples of how to drive the clock with an external Machine Cycles oscillator are shown in Figure 14. Note that in the HMOS devices (8051, etc.) the signal at the XTAL2 pin A machine cycle consists of a sequence of 6 states, actually drives the internal clock generator. In the numbered S 1 through S6. Each state time lasts for two CHMOS devices (80C51BH, etc.) the signal at the oscillator periods. Thus a machine cycle takes 12 oscil- XTALI pin drives the internal clock generator. If only lator periods or 1 J.J.s if the oscillator frequency is one pin is going to be driven with the external oscillator 12 MHz. signal, make sure it is the right pin. Each state is divided into a Phase 1 half and a Phase 2 The internal clock generator defines the sequence of half. Figure 15 shows the fetch/execute sequences in states that make up the MCS-51 machine cycle. OSC. (XTAL2) I I I I I I I I I I I I I I S1 S2 53 ~~~~~~~~~~~~~~~~~~~~~~~~~~ 54 55 56 S1 S2 53 54 55 56 S1 ALE _[ _R:~D NEXT OPCODE AGAIN. - - - - - - r--.L..~-~--r-..l--r--.---' ______ '--_.L.-_.L.-_.J......_.J......_..L...__~ (A) 1-byte, 1-c:ycle IMlructlon, e.g., INC A. I I READ OPCODE. I : _ [_R:~ NEXT OPCODE. --------~.L..r_-r_-~..l-~-~__, ________ L.:..:.........L.-=......L~...L._J........-I_~ (8) 2-byte, 1-cycle IMlruction, e.g., ADD A, #data I READ OPCODE. I READ NEXT I OPCODE (DISCARD). I _______ '--_.L.-_.L.-_~_~_..L...__.J......_..L...__~_~_~_~_~ _____ _ (C) 1·byte, 2-cycle insIuclion, e.g."INC DPTR. I READ NEXT OPCODE AGAIN. D NO FETCH. I 1 I ____ _ _ _ _ _ _ _ _____ _ l L-_.J......_.J......_...I..-_..L...__...L...._~_~_~_-L.._-L.._--'-_....I ADDR DATA (D) MOVX (1·byte, 2-c:ycle) I ACCESS EXTERNAL MEMORY 270251-15 Figure 15. State Sequences in MCS®-51 Devices 1-18 intel~ MCS®-51 ARCHITECTURAL OVERVIEW states and phases for various kinds of instructions. Nor- The fetch/execute sequences are the same whether the mally two program fetches are generated during each Program Memory is internal or external to the chip. machine cycle, even if the instruction being executed Execution times do not depend on whether the Pro- doesn't require it. If the instruction being executed gram Memory is internal or external. doesn't need more code bytes, the CPU simply ignores the extra fetch, and the Program Counter is not incre- Figure 16 shows the signals and timing involved in pro- mented. gram fetches when the Program Memory is external. If Program Memory is external, then the Program Memo- Execution of a one-cycle instruction (Figure 15A and ry read strobe PSEN is normally activated twice per B) begins during State 1 of the machine cycle, when the machine cycle, as shown in Figure 16(A). opcode is latched into the Instruction Register. A sec- ond fetch occurs during S4 of the same machine cycle. If an access to external Data Memory occurs, as shown Execution is complete at the end of State 6 of this ma- in Figure 16(B), two PSENs are skipped, because the chine cycle. address and data bus are being used for the Data Mem- ory access. The MOVX instructions take two machine cycles to execute. No program fetch is generated during the sec- Note that a Data Memory bus cycle takes twice as ond cycle of a MOVX instruction. This is the only time much time as a Program Memory bus cycle. Figure 16 program fetches are skipped. The fetch/execute se- shows the relative timing of the addresses being emitted quence for MOVX instructions is shown in Figure at Ports 0 and 2, and of ALE and PSEN. ALE is used 15(D). to latch the low address byte from PO into the address latch. ALE PsEN (A) RID -------+----------------------~--------~----------~---- WITHOUT A MOVX. PO I 1 I lpCLOUT lPCLOUT lpCLOUT VALID VALID VALID ALE 56 T S1 PSEN (B) RD ------~----------~--~ WITH A MOVX. l PCL OUT lADDROUT lpCLOUT VALID VALID VALID 270251-16 Figure 16. Bus Cycles in MCS®-51 Devices Executing from External Program Memory 1-19 intet MCS®-51 ARCHITECTURAL OVERVIEW When the CPU is executing from internal Program named IE (Interrupt Enable). This register also con- Memory, PSEN is not activated, and program address- tains a global disable bit, which can be cleared to dis- es are not emitted. However, ALE continues to be acti- able all interrupts at once. Figure 17 shows the IE reg- vated twice per machine cycle and so is available as a ister for the 8051. clock output signal. Note, however, that one ALE is skipped during the execution of the MOVX instruction. INTERRUPT PRIORITIES Each interrupt source can also be individually pro- Interrupt Structure grammed to one of two priority levels by setting or clearing a bit in the SFR named IP (Interrupt Priority). The 8051 core provides 5 interrupt sources: 2 external interrupts, 2 timer interrupts, and the serial port inter- Figure 18 shows the IP register in the 8051. rupt. What follows is an overview of the interrupt A low-priority interrrupt can be interrupted by a high- structure for the 8051. Other MCS-51 devices have ad- priority interrupt, but not by another low-priority inter- ditional interrupt sources and vectors as shown in Ta- rupt. A high-priority interrupt can't be interrupted by ble 1. Refer to the appropriate chapters on other devic- es for further information on their interrupts. any other interrupt source. If two interrupt requests of different priority levels are INTERRUPT ENABLES received simultaneously, the request of higher priority level is serviced. If interrupt requests of the same priori- Each of the interrupt sources can be individually en- ty level are received simultaneously, an internal polling abled or disabled by setting or clearing a bit in the SFR sequence determines which request is serviced. Thus within each priority level there is a second priority structure determined by the polling sequence. (MSB) (LSB) 1EA 1-I -I ES 1ET1 1EX1 1ETO 1EXO 1 Figure 19 shows, for the 8051, how the IE and IP regis- Enable bit = 1 enables the interrupt. ters and the polling sequence work to determine which Enable bit = 0 disables it. if any interrupt will be serviced. Symbol Position Function EA IE.7 disables all interrupts. If EA = 0, no interrupt will be acknowledged. If EA (MSB) (LSB) = 1, each interrupt source is 1- 1- 1- 1PS 1PT1 1PX1 1PTO 1PXO 1 individually enabled or disabled by Priority bit = 1 assigns high priority. setting or clearing its enable bit. Priority bit = 0 assigns low priority. IE.6 reserved" IE.5 reserved" Symbol Position Function ES IE.4 Serial Port Interrupt enable bit. IP.7 reserved" ET1 IE.3 Timer 1 Overflow Interrupt enable bit. IP.6 reserved" EX1 IE.2 Extemal Interrupt 1 enable bit. IP.5 reserved" ETO IE.1 Timer 0 Overflow Interrupt enable bit. PS IP.4 Serial Port interrupt priority bit. EXO IE.O Extemal Interrupt 0 enable bit. PT1 IP.3 Timer 1 interrupt priority bit. "These reserved bits are used in other MCS-51 devices. PX1 IP.2 Extemal Interrupt 1 priority bit. PTO IP.1 Timer 0 interrupt priority bit. Figure 17. IE (Interrupt Enable) PXO IP.O Extemal Interrupt 0 priority bit. Register in the 8051 "These reserved bits are used in other MCS-51 devices. Figure 18.IP (Interrupt Priority) Register in the 8051 1-20 MCS®-51 ARCHITECTURAL OVERVIEW HIGH PRIORITY ~- - INTERRUPT IP REGISTER 0-0.(cD-+---I-<>-__ • a-~----~~ ••• T F ' O - - - - - - -....~D'" ~:>+--+-o-...___ INTERRUPT POLLING • a-~----~~ •• SEQUENCE • ~>+--+--o-___ a-~----~~ ••• • rr1-------------~~iI-<>-c,~---==+1 RI TI INDIVIDUAL GLOBAL LOW PRIORITY ENABLES DISABLE INTERRUPT 270251-17 Figure 19. 8051 Interrupt Control System In operation, all the interrupt flags are latched into the pleted in less time than it takes other architectures to interrupt control system during State 5 of every ma- commence them. chine cycle. The samples are polled during the follow- ing machine cycle. If the flag for an enabled interrupt is found to be set (1), the interrupt system generates an SIMULATING A THIRD PRIORITY LEVEL IN LCALL to the appropriate location in Program Memo- SOFTWARE ry' unless some other condition blocks the interrupt. Some applications require more than ,the two priori~y Several conditions can block an interrupt, among them levels that are provided by on-chip hardware m that an interrupt of equal or higher priority level is MCS-Sl devices. In these cases, relatively simple soft- already in progress. ware can be written to produce the same effect as a third priority level. The hardware-generated LCALL causes the contents of the Program Counter to be pushed onto the stack, and First, interrupts that are to have higher priority ~h~ 1 reloads the PC with the beginning address of the service are assigned to priority 1 in the IP (Interrupt Pnonty) routine. As previously noted (Figure 3), the service rou- register. The service routines for priority 1 interrupts tine for each interrupt begins at a fixed location. that are supposed to be interruptible by "priority 2" interrupts are written to include the following code: Only the Program Counter is automatically pushed onto the stack, not the PSW or any other register. Hav- PUSH IE ing only the PC be automatically ~ved allows the p~o­ MOV IE, # MASK grammer to decide how much time to spend savmg CALL LABEL which other registers. This enhances the interrupt re- ******* sponse time, albeit at the expense of increasing the pro- (execute service routine) grammer's burden of responsibility. As a result, many ******* interrupt functions that are typical in control applica- POP IE tions-toggling a port pin, for example, or reloading a RET timer, or unloading a serial buffer--can often be com- LABEL: RET! 1-21 MCS®-51 ARCHITECTURAL OVERVIEW As soon as any priority 1 interrupt is acknowledged, ADDITIONAL REFERENCES the IE (Interrupt Enable) register is re-defmed so as to disable all but "priority 2" interrupts. Then, a CALL to The following application notes are found in the Em- LABEL executes the RETI instruction, which clears bedded Control Applications handbook. (Order Num- the priority 1 interrupt-in-progress flip-flop. At this ber: 270648) point any priority 1 interrupt that is enabled can be 1. AP-69 "An Introduction to the Intel MCS®-Sl Sin- serviced, but only "priority 2" interrupts are enabled. gle-Chip Microcomputer Family" POPping IE restores the original enable byte. Then a 2. AP-70 "Using the Intel MCS®-Sl Boolean Process- normal RET (rather than another RETI) is used to ing Capabilities" terminate the service routine. The additional software adds 10 fLS (at 12 MHz) to priority 1 interrupts. 1-22 MCS® 51 Programmer's 2 Guide and Instruction Set MCS@ 51 PROGRAMMER'S CONTENTS PAGE GUIDE AND MEMORY ORGANIZATION ........................ 2-3 INSTRUCTION SET PROGRAM MEMORy ................................. 2-3 Data Memory ............................................... 2-4 INDIRECT ADDRESS AREA ...................... 2-6 DIRECT AND INDIRECT ADDRESS AREA ......................................................2-6 SPECIAL FUNCTION REGISTERS ............ 2-8 WHAT DO THE SFRs CONTAIN JUST AFTER POWER-ON OR A RESET ......... 2-9 SFR MEMORY MAP ................................. 2-10 PSW: PROGRAM STATUS WORD. BIT ADDRESSABLE ................................... 2-11 PCON: POWER CONTROL REGISTER. NOT BIT ADDRESSABLE .................... 2-11 INTERRUPTS ............................................ 2-12 IE: INTERRUPT ENABLE REGISTER. BIT ADDRESSABLE ............................ 2-12 ASSIGNING HIGHER PRIORITY TO ONE OR MORE INTERRUPTS ............. 2-13 PRIORITY WITHIN LEVEL ....................... 2-13 IP: INTERRUPT PRIORITY REGISTER. BIT ADDRESSABLE ............................ 2-13 TCON: TIMER/COUNTER CONTROL REGISTER. BIT ADDRESSABLE ........ 2-14 TMOD: TIMER/COUNTER MODE CONTROL REGISTER. NOT BIT ADDRESSABLE ................................... 2-14 TIMER SET-UP .........................................2-15 TIMER/COUNTER 0 ................................. 2-15 TIMER/COUNTER 1 .................................. 2-16 T2CON: TIMER/COUNTER 2 CONTROL REGISTER. BIT ADDRESSABLE ........ 2-17 TIMER/COUNTER 2 SET-UP ................... 2-18 SCON: SERIAL PORT CONTROL REGISTER. BIT ADDRESSABLE ........ 2-19 2-1 CONTENTS PAGE CONTENTS PAGE SERIAL PORT SET-UP ............................ 2-19 USING TIMER/COUNTER 2 TO GENERATE BAUD RATES .................. 2-20 GENERATING BAUD RATES .................. 2-19 Serial Port in Mode 0 ................................ 2-19 SERIAL PORT IN MODE 2 ....................'.. 2-20 Serial Port in Mode 1 ................................ 2-19 SERIAL PORT IN MODE 3 ...................... 2-20 USING TIMER/COUNTER 1 TO MCS®-S1 INSTRUCTION SET ................. 2-21 GENERATE BAUD RATES .................. 2-20 INSTRUCTION DEFINITIONS ................. 2-28 2-2 intel~ MCS®-51 PROGRAMMER'S GUIDE AND INSTRUCTION SET The infonnation presented in this chapter is collected from the MCS®-51 Architectural Overview and the Hardware Description of the 8051, 8052 and 80C51 chapters of this book. The material has been selected and rearranged to fonn a quick and convenient reference for the programmers of the MCS-51. This guide pertains specifically to the 8051, 8052 and 80C51. MEMORY ORGANIZATION PROGRAM MEMORY The 8051 has separate address spaces for Program Memory and Data Memory. The Program Memory can be up to 64K bytes long. The lower 4K (8K for the 8052) may reside on-chip. Figure 1 shows a map of the 8051 program memory, and Figure 2 shows a map of the 8052 program memory. ~~------------------~ ~~------------------~ &OK BYTES EXTERNAL 64K - - - OR ----i~. BYTES EXTERNAL 1~~------------------~ :1 :re_B_~_NA AND L ________ __L________' ~~_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _- J 270249-1 Figure 1. The 8051 Program Memory 2-3 intel~ MCS®-51 PROGRAMMER'S GUIDE AND INSTRUCTION SET ~~i--------------------~ ~ i~--------------------' 56K BYTES EXTERNAL 14K - - - OR ----I~~ BYTES EXTERNAL ~~ __________________- J AND 1o o ~IL________ _ INTERNAL 8_K_B_YTES ________ ~ 0000 ~---------------........ 270249-2 Figure 2. The 8052 Program Memory Data Memory: The 8051 can address up to 64K bytes of Data Memory external to the chip. The "MOVX" instruction is used to access the external data memory. (Refer to the MeS-51 Instruction Set, in this chapter, for detailed description of instructions). The 8051 has 128 bytes of on-chip RAM (256 bytes in the 8052) plus a number of Special Function Registers (SFRs). The lower 128 bytes of RAM can be accessed either by direct addressing (MOV data addr) or by indirect addressing (MOV @Ri). Figure 3 shows the 8051 and the 8052 Data Memory organization. 2-4 MCS®-S1 PROGRAMMER'S GUIDE AND INSTRUCTION SET OFFF , - - - - - - - - - - . . . , INTERNAL FFr-----------.., S~ 14K BYTES DIRECT EXTERNAL ADDRESSING ONLY 8O ....._ _ _ _ _ _ _ _ _-t --ANO--~~ 7F DIRECT. INDIRECT ADDRESSING 00 ......- - - - - - - - - - ~~----------.J 270249-3 Figure 3a. The 8051 Data Memory FFFF INTERNAL f INDIRECT ADDRESSING ONLY 80H TO fFH FF1 """ FF 14K SFR. BYTES DIRECT EXTERNAL 1- ADDRESSING ONLY --ANO--' 80 7F DIRECT. INDIRECT ADDRESSING 00 ~ 270249-4 Figure 3b. The 8052 Data Memory 2-5 int:et MCS®-S1 PROGRAMMER'S GUIDE AND INSTRUCTION SET INDIRECT ADDRESS AREA: Note that in Figure 3b the SFRs and the indirect address RAM have the same addresses (80H-OFFH). Neverthe- less, they are two separate areas and are accessed in two different ways. For example the instruction MOV 80H,#OAAH writes OAAH to Port 0 which is one of the SFRs and the instruction MOV RO,#80H MOV @RO,#OBBH writes OBBH in location 80H of the data RAM. Thus, after execution of both of the above instructions Port 0 will contain OAAH and location 80 of the RAM will contain OBBH. Note that the stack operations are examples of indirect addressing, so the upper 128 bytes of data RAM are available as stack space in those devices which implement 256 bytes of internal RAM. DIRECT AND INDIRECT ADDRESS AREA: The 128 bytes of RAM which can be accessed by both direct and indirect addressing can be divided into 3 segments as listed below and shown in Figure 4. 1. Register Banks 0-3: Locations 0 through IFH (32 bytes). ASM-51 and the device after reset default to register bank O. To use the other register banks the user must select them in the software (refer to the MeS-51 Micro Assembler User's Guide). Each register bank contains 8 one-byte registers, 0 through 7. Reset initializes the Stack Pointer to location 07H and it is incremented once to start from location 08H which is the first register (RO) of the second register bank. Thus, in order to use more than one register bank, the SP should be intialized to a different location of the RAM where it is not used for data storage (ie, higher part of the RAM). 2. Bit Addressable Area: 16 bytes have been assigned for this segment, 20H-2FH. Each one of the 128 bits of this segment can be directly addressed (0-7FH). The bits can be referred to in two ways both of which are acceptable by the ASM-51. One way is to refer to their addresses, ie. 0 to 7FH. The other way is with reference to bytes 20H to 2FH. Thus, bits 0-7 can also be referred to as bits 20.0-20.7, and bits 8-FH are the same as 21.0-21.7 and so on. Each of the 16 bytes in this segment can also be addressed as a byte. 3. Scratch Pad Area: Bytes 30H through 7FH are available to the user as data RAM. However, if the stack pointer has been initialized to this area, enough number of bytes should be left aside to prevent SP data destruction. 2-6 int"eL MCS®-S1 PROGRAMMER'S GUIDE AND INSTRUCTION SET Figure 4 shows the different segments of the on-chip RAM. 11~~'-----------8~----------~~~1 78 7F 70 T7 68 SF 60 67 SCRATCH 58 SF PAD 50 57 AREA 48 4F 40 47 38 3F 30 37 28 ••. 7F 2F BIT ADDRESSABLE 20 O••• ~ SEGMENT 18 3 1F 10 2 17 REGISTER 08 1 OF BANKS 00 0 07 270249-5 Figure 4. 128 Bytes of RAM Direct and Indirect Addressable 2-7 intaL MCS®-S1 PROGRAMMER'S GUIDE AND INSTRUCTION SET SPECIAL FUNCTION REGISTERS: Table I contains a list of all the SFRs and their addresses. Comparing Table I and Figure 5 shows that all of the SFRs that are byte and bit addressable are located on the first column of the diagram in Figure 5. Table 1 Symbol Name Address *ACC Accumulator OEOH *B B Register OFOH *PSW Program Status Word ODOH SP Stack Pointer 81H DPTR Data Pointer 2 Bytes DPL Low Byte 82H DPH High Byte 83H *PO PortO 80H *P1 Port 1 90H *P2 Port 2 OAOH *P3 Port 3 OBOH *IP Interrupt Priority Control OB8H *IE Interrupt Enable Control OA8H TMOD Timer/Counter Mode Control 89H *TCON Timer/Counter Control 88H *+ T2CON Timer/Counter 2 Control OC8H THO Timer/Counter 0 High Byte 8CH TLO Timer/Counter 0 Low Byte 8AH TH1 Timer/Counter 1 High Byte 8DH TL1 Timer/Counter 1 Low Byte 8BH +TH2 Timer/Counter 2 High Byte OCDH +TL2 Timer/Counter 2 Low Byte OCCH + RCAP2H T /C 2 Capture Reg. High Byte OCBH + RCAP2L T /C 2 Capture Reg. Low Byte OCAH *SCON Serial Control 98H SBUF Serial Data Buffer 99H PCON Power Control 87H • = Bit addressable + = 8052 only 2-8 infel~ MCS®-S1 PROGRAMMER'S GUIDE AND INSTRUCTION SET WHAT DO THE SFRs CONTAIN JUST AFTER POWER-ON OR A RESET? Table 2 lists the contents of each SFR after power-on or a hardware reset. Table 2. Contents of the SFRs after reset Register Value in Binary "'ACC 00000000 "'B 00000000 "'PSW 00000000 SP 00000111 DPTR DPH 00000000 DPL 00000000 "'PO 11111111 "'P1 11111111 "'P2 11111111 "'P3 11111111 "'IP 8051 XXXOOOOO, 8052 XXOOOOOO "'IE 80510XXOOOOO, 8052 OXOOOOOO TMOD 00000000 "'TCON 00000000 "'+ T2CON 00000000 THO 00000000 TLO 00000000 TH1 00000000 TL1 00000000 +TH2 00000000 +TL2 00000000 + RCAP2H 00000000 + RCAP2L 00000000 "'SCON 00000000 SBUF Indeterminate PCON HMOS OXXXXXXX CHMOS OXXXOOOO x = Undefined ., = Bit Addressable + = 8052 only 2-9 intel~ MCS®-51 PROGRAMMER'S GUIDE AND INSTRUCTION SET SFR MEMORY MAP Q a ........ V"'J"~'" F8 FF FO B F7 E8 EF EO ACC E7 D8 DF DO PSW D7 C8 T2CON RCAP2L RCAP2H TL2 TH2 CF CO C7 B8 IP BF BO P3 B7 A8 IE AF AO P2 A7 98 SCON SBUF 9F 90 P1 97 88 TCON TMOD TLO TL1 THO TH1 8F 80 PO SP DPL DPH PCON 87 t Figure 5 Bit Addressable 2-10 MCS®-S1 PROGRAMMER'S GUIDE AND INSTRUCTION SET Those SFRs that have their bits assigned for various functions are listed in this section. A brief description of each bit is provided for quick reference. For more detailed information refer to the Architecture Chapter of this book. PSW: PROGRAM STATUS WORD. BIT ADDRESSABLE. CY AC FO RS1 RSO OV P CY PSW.7 Carry Flag. AC PSW.6 Auxiliary Carry Flag. FO PSW.5 Flag 0 available to the user for general purpose. RSI PSW.4 Register Bank selector bit 1 (SEE NOTE 1). RSO PSW.3 Register Bank selector bit 0 (SEE NOTE I). OV PSW.2 Overflow Flag. PSW.I User definable flag. P psw.o Parity flag. Set/cleared by hardware each instruction cycle to indicate an odd/even number of 'I' bits in the accumulator. NOTE: 1. The value presented by RSO and RS1 selects the corresponding register bank. RS1 RSO Register Bank Address 0 0 0 OOH-07H 0 1 1 OSH-OFH 1 0 2 10H-17H 1 1 3 1SH-1 FH PCON: POWER CONTROL REGISTER. NOT BIT ADDRESSABLE. SMOO GF1 GFO PO IOL SMOD Double baud rate bit. If Timer 1 is used to generate baud rate and SMOD = 1, the baud rate is doubled when the Serial Port is used in modes 1, 2, or 3. Not implemented, reserved for future use. * Not implemented, reserved for future use. * Not implemented, reserved for future use. * GFI General purpose flag bit. GFO General purpose flag bit. PD Power Down bit. Setting this bit activates Power Down operation in the 80C51BH. (Available only in CHMOS). IDL Idle Mode bit. Setting this bit activates Idle Mode operation in the 80C51BH. (Available only in CHMOS). If Is are written to PO and IDL at the same time, PD takes precedence. *User software should not write 1s to reserved bits. These bits may be used in future MCS-S1 products to invoke new features. In that case, the reset or inactive value of the new bit will be 0, and its active value will be 1. 2-11 MCS®·51 PROGRAMMER'S GUIDE AND INSTRUCTION SET INTERRUPTS: In order to use any of the interrupts in the MeS-51, the following three steps must be taken. 1. Set the EA (enable all) bit in the IE register to 1. 2. Set the corresponding individual interrupt enable bit in the IE register to 1. 3. Begin the interrupt service routine at the corresponding Vector Address of that interrupt. See Table below. Interrupt Vector Source Address lEO 0003H TFO OOOBH IE1 0013H TF1 001BH RI&TI 0023H TF2&EXF2 002BH In addition, for external interrupts, pins INTO and INTI (P3.2 and P3.3) must be set to 1, and depending on whether the interrupt is to be level or transition activated, bits ITO or ITI in the TeON register may need to be set to 1. ITx = 0 level activated ITx = 1 transition activated IE: INTERRUPT ENABLE REGISTER. BIT ADDRESSABLE. If the bit is 0, the corresponding interrupt is disabled. If the bit is 1, the corresponding interrupt is enabled. EA ET2 ES ET1 EX1 ETO EXO EA IE.7 Disables all interrupts. If EA = 0, no interrupt will be acknowledged. If EA = 1, each interrupt source is individually enabled or disabled by setting or clearing its enable bit. IE.6 Not implemented, reserved for future use. * ET2 IE.5 Enable or disable the Timer 2 overflow or capture interrupt (8052 only). ES IE.4 Enable or disable the serial port interrupt. ETI IE.3 Enable or disable the Timer 1 overflow interrupt. EXI IE.2 Enable or disable External Interrupt 1. ETO IE.l Enable or disable the Timer 0 overflow interrupt. EXO IE.O Enable or disable External Interrupt O. 'User software should not write Is to reserved bits. These bits may be used in future MeS-5l products to invoke new features. In that case, the reset or inactive value of the new bit will be 0, and its active value will be 1. 2-12 inteL MCS®-51 PROGRAMMER'S GUIDE AND INSTRUCTION SET ASSIGNING HIGHER PRIORITY TO ONE OR MORE INTERRUPTS: In order to assign higher priority to an interrupt the corresponding bit in the IP register must be set to 1. Remember that while an interrupt service is in progress, it cannot be interrupted by a lower or same level interrupt. PRIORITY WITHIN LEVEL: Priority within level is only to resolve simultaneous requests of the same priority level. From high to low, interrupt sources are listed below: lEO TFO lEI TFI RI or TI TF2 or EXF2 IP: INTERRUPT PRIORITY REGISTER. BIT ADDRESSABLE. If the bit is 0, the corresponding interrupt has a lower priority and if the bit is 1 the corresponding interrupt has a higher priority. PT2 PS PT1 PX1 PTO PXO IP.7 Not implemented, reserved for future use.· IP. 6 Not implemented, reserved for future use.· PT2 IP.5 Defmes the Timer 2 interrupt priority level (8052 only). PS IP.4 Defines the Serial Port interrupt priority level. PTI IP.3 Defmes the Timer 1 interrupt priority level. PXl IP.2 Defmes External Interrupt 1 priority level. PTO IP. 1 Defines the Timer 0 interrupt priority level. PXO IP.O Defines the External Interrupt 0 priority level. ·User software should not write Is to reserved bits. These bits may be used in future MeS-51 products to invoke new features. In that case, the reset or inactive value of the new bit will be 0, and its active value will be 1. 2-13 intet MCS®-S1 PROGRAMMER'S GUIDE AND INSTRUCTION SET TCON: TIMER/COUNTER CONTROL REGISTER. BIT ADDRESSABLE. I TF1 TRQ . TFO -I TRO IE1 IT1 lEO ITO TFI TCON.7 Timer 1 overflow flag. Set by hardware when the Timer/Counter 1 overflows. Cleared by hard- ware as processor vectors to the interrupt service routine. TRI TCON. 6 Timer 1 run control bit. Set/cleared by software to tum Timer/Counter ION/OFF. TFO TCON. 5 Timer 0 overflow flag. Set by hardware when the Timer/Counter 0 overflows. Cleared by hard- ware as processor vectors to the service routine. TRO TCON.4 Timer 0 run control bit. Set/cleared by software to tum Timer/Counter 0 ON/OFF. lEI TCON.3 External Interrupt 1 edge flag. Set by hardware when External Interrupt edge is detected. Cleared by hardware when interrupt is processed. ITI TCON.2 Interrupt 1 type control bit. Set/cleared by software to specify falling edgellow level triggered External Interrupt. lEO TCON.l External Interrupt 0 edge flag. Set by hardware when External Interrupt edge detected. Cleared by hardware when interrupt is processed. ITO TCON.O Interrupt 0 type control bit. Set/cleared by software to specify falling edgellow level triggered External Interrupt. TMOD: TIMER/COUNTER MODE CONTROL REGISTER. NOT BIT ADDRESSABLE. I __________ C~ GATE C/T I M1 MO I GATE __________ ~ C/T I M1 MO I __________ __________ -J)'~ ~ ~) TIMER 1 TIMER 0 GATE When TRx (in TCON) is set and GATE = 1, TIMERICOUNTERx will run only while INTx pin is high (hardware control). When GATE = 0, TIMERICOUNTERx will run only while TRx = 1 (software control). clf Timer or Counter selector. Cleared for Timer operation (input from internal system clock). Set for Coun- ter operation (input from Tx input pin). Ml Mode selector bit. (NOTE 1) MO Mode selector bit. (NOTE 1) NOTE 1: M1 MO Operating Mode o o o 13-bit Timer (MCS-48 compatible) o 1 1 16-bit Timer/Counter o 2 8-bit Auto-Reload Timer/Counter 3 (Timer 0) TLO is an 8-bit Timer/Counter controlled by the standard Timer 0 control bits, THO is an 8-bit Timer and is controlled by Timer 1 control bits. 3 (Timer 1) Timer/Counter 1 stopped. 2-14 intel~ MCS®-S1 PROGRAMMER'S GUIDE AND INSTRUCTION SET TIMER SET-UP Tables 3 through 6 give some values for TMOD which can be used to set up Timer 0 in different modes. It is assumed that only one timer is being used at a time. Ifit is desired to run Timers 0 and 1 simultaneously, in any mode, the value in TMOD for Timer 0 must be ORed with the value shown for Timer 1 (Tables 5 and 6). For example, if it is desired to run Timer 0 in mode 1 GATE (external control), and Timer 1 in mode 2 COUNTER, then the value that must be loaded into TMOD is 69H (09H from Table 3 ORed with 60H from Table 6). Moreover, it is assumed that the user, at this point, is not ready to tum the timers on and will do that at a different point in the program by setting bit TRx (in TCON) to 1. TIMER/COUNTER 0 Asa Timer: Table 3 TMOD TIMER 0 INTERNAL EXTERNAL MODE FUNCTION CONTROL CONTROL (NOTE 1) (NOTE 2) 0 13-bit Timer OOH 08H 1 16-bit Timer 01H 09H 2 8-bit Auto-Reload 02H OAH 3 two 8-bit Timers 03H OSH As a Counter: Table 4 TMOD COUNTER 0 INTERNAL EXTERNAL MODE FUNCTION CONTROL CONTROL (NOTE 1) (NOTE 2) 0 13-bit Timer 04H OCH 1 16-bit Timer 05H ODH 2 8-bit Auto-Reload 06H OEH 3 one 8-bit Counter 07H OFH NOTES: 1. The Timer is turned ON/OFF by setting/clearing bit TRO in the software. 2. The Timer is turned ON/OFF by the 1 to 0 transition on INTO (P3.2) when TRO = 1 (hardware control). 2-15 MCS®-S1 PROGRAMMER'S GUIDE AND INSTRUCTION SET TIMER/COUNTER 1 As a Timer: Table 5 TMOD TIMER 1 INTERNAL EXTERNAL MODE FUNCTION CONTROL CONTROL (NOTE 1) (NOTE 2) 0 13-bit Timer OOH 80H 1 16-bit Timer 10H 90H 2 8-bit Auto-Reload 20H AOH 3 does not run 30H BOH As a Counter: Table 6 TMOD COUNTER 1 INTERNAL EXTERNAL MODE FUNCTION CONTROL CONTROL (NOTE 1) (NOTE 2) 0 13-bit Timer 40H COH 1 16-bit Timer SOH DOH 2 8-bit Auto-Reload 60H EOH 3 not available - - NOTES: 1. The Timer is turned ON/OFF by setting/clearing bit TR1 in the software. 2. The TImer is turned ON/OFF by the 1 to 0 transition on INT1 (P3.3) when TR1 = 1 (hardware control). 2-16 intel~ MCS®-S1 PROGRAMMER'S GUIDE AND INSTRUCTION SET T2CON: TIMER/COUNTER 2 CONTROL REGISTER. BIT ADDRESSABLE 8052 Only I TF2 I EXF2 RCLK TCLK EXEN2 TR2 C/T2 CP/RL2 TF2 T2CON.7 Timer 2 overflow flag set by hardware and cleared by software. TF2 cannot be set when either RCLK = 1 or CLK = 1 EXF2 T2CON. 6 Timer 2 external flag set when either a capture or reload is caused by a negative transition on T2EX, and EXEN2 = 1. When Timer 2 interrupt is enabled, EXF2 = 1 will cause the CPU to vector to the Timer 2 interrupt routine. EXF2 must be cleared by software. RCLK T2CON.5 Receive clock flag. When set, causes the Serial Port to use Timer 2 overflow pulses for its receive clock in modes 1 & 3. RCLK = 0 causes Timer 1 overflow to be used for the receive clock. TLCK T2CON. 4 Transmit clock flag. When set, causes the Serial Port to use Timer 2 overflow pulses for its transmit clock in modes 1 & 3. TCLK = 0 causes Timer 1 overflows to be used for the transmit clock. EXEN2 T2CON.3 Timer 2 external enable flag. When set, allows a capture or reload to occur as a result of negative transition on T2EX if Timer 2 is not being used to clock the Serial Port. EXEN2 = 0 causes Timer 2 to ignore events at T2EX. TR2 T2CON.2 Software START/STOP control for Timer 2. A logic 1 starts the Timer. C/T2 T2CON.l Timer or Counter select. o = Internal Timer. 1 = External Event Counter (falling edge triggered). CP/RL2 T2CON.O CapturelReload flag. When set, captures will occur on negative transitions at T2EX if EXEN2 = 1. When cleared, Auto-Reloads will occur either with Timer 2 overflows or negative transitions at T2EX when EXEN2 = 1. When either RCLK = 1 or TCLK = 1, this bit is ignored and the Timer is forced to Auto-Reload on Timer 2 overflow. 2-17 intel~ MCS®-S1 PROGRAMMER'S GUIDE AND INSTRUCTION SET TIMER/COUNTER 2 SET-UP Except for the baud rate generator mode, the values given for T2CON do not include the setting of the TR2 bit. Therefore, bit TR2 must be set, separately, to tum the Timer on. As a Timer: Table 7 T2CON MODE INTERNAL EXTERNAL CONTROL CONTROL (NOTE 1) (NOTE 2) 16-bit Auto-Reload OOH 08H 16-bit Capture 01H 09H BAUD rate generator receive & transmit same baud rate 34H 36H receive only 24H 26H transmit only 14H 16H As a Counter: Table 8 TMOD MODE INTERNAL EXTERNAL CONTROL CONTROL (NOTE 1) (NOTE 2) 16-bit Auto-Reload 02H OAH 16-bit Capture 03H OBH NOTES: 1. Capture/Reload occurs only on Timer/Counter overflow. 2. Capture/Reload occurs on Timer/Counter overflow and a 1 to 0 transition on T2EX (P1.1) pin except when Timer 2 is used in the baud rate generating mode. 2-18 infel~ MCS®-51 PROGRAMMER'S GUIDE AND INSTRUCTION SET SCON: SERIAL PORT CONTROL REGISTER. BIT ADDRESSABLE. SMO SM1 SM2 REN TB8 RB8 TI RI SMO SCON. 7 Serial Port mode specifier. (NOTE 1). SMI SCON.6 Serial Port mode specifier. (NOTE 1). SM2 SCON. 5 Enables the multiprocessor communication feature in modes 2 & 3. In mode 2 or 3, if SM2 is set to 1 then RI will not be activated if the received 9th data bit (RBS) is O. In mode 1, if SM2 = 1 then RI will not be activated if a valid stop bit was not received. In mode 0, SM2 should be O. (See Table 9). REN SCON.4 Set/Cleared by software to EnablelDisable reception. TBS SCON.3 The 9th bit that will be transmitted in modes 2 & 3. Set/Cleared by software. RBS SCON.2 In modes 2 & 3, is the 9th data bit that was received. In mode 1, ifSM2 = 0, RBS is the stop bit that was received. In mode 0, RBS is not used. TI SCON. 1 Transmit interrupt flag. Set by hardware at the end of the Sth bit time in mode 0, or at the beginning of the stop bit in the other modes. Must be cleared by software. RI SCON. 0 Receive interrupt flag. Set by hardware at the end of the Sth bit time in mode 0, or halfway through the stop bit time in the other modes (except see SM2). Must be cleared by software. NOTE 1: SMO SM1 Mode Description Baud Rate o 0 0 SHIFT REGISTER Fosc'/12 o 1 1 8-Bit UART Variable 0 2 9-Bit UART Fosc./64 OR Fosc./32 3 9-Bit UART Variable SERIAL PORT SET-UP: Table 9 MODE SCON SM2 VARIATION 0 10H Single Processor 1 SOH Environment 2 90H (SM2 = 0) 3 DOH 0 NA Multiprocessor 1 70H Environment 2 BOH (SM2 = 1) 3 FOH GENERATING BAUD RATES Serial Port in Mode 0: Mode 0 has a fixed baud rate which is 1/12 of the oscillator frequency. To run the serial port in this mode none of the Timer/Counters need to be set up. Only the SCON register needs to be defmed. OscFreq Baud Rate = - - - 12 Serial Port in Mode 1: Mode 1 has a variable baud rate. The baud rate can be generated by either Timer 1 or Timer 2 (S052 only). 2-19 intelGD MCS®-S1 PROGRAMMER'S GUIDE AND INSTRUCTION SET USING TIMER/COUNTER 1 TO GENERATE BAUD RATES: For this purpose, Timer 1 is used in mode 2 (Auto-Reload). Refer to Timer Setup section of this chapter. 8 dR _ K x Oscillator Freq. au ate - 32 x 12 x [256 - (TH1)l If SMOD = 0, then K = 1. If SMOD = 1, then K = 2. (SMOD is the PCON register). Most of the time the user knows the baud rate and needs to know the reload value for TH 1. Therefore, the equation to calculate TH 1 can be written as: TH1 = 256 _ K x Osc Freq. 384 x baud rate THI must be an integer value. Rounding offTHI to the nearest integer may not produce the desired baud rate. In this case, the user may have to choose another crystal frequency. Since the PCON register is not bit addressable, one way to set the bit is logical ORing the PCON register. (ie, ORL PCON, # 80H). The address of PCON is 87H. USING TIMER/COUNTER 2 TO GENERATE BAUD RATES: For this purpose, Timer 2 must be used in the baud rate generating mode. Refer to Timer 2 Setup Table in this chapter. If Timer 2 is being clocked through pin T2 (P1.0) the baud rate is: Timer 2 Overflow Rate Baud Rate = 16 And if it is being clocked internally the baud rate is: Osc Freq Baud Rate = 32 x [65536 - (RCAP2H, RCAP2L)] To obtain the reload value for RCAP2H and RCAP2L the above equation can be rewritten as: RCAP2H, RCAP2L = 65536 _ Osc F~e~ 32 x 8au ate SERIAL PORT IN MODE 2: The baud rate is fixed in this mode and is %2 or 1/64 of the oscillator frequency depending on the value of the SMOD bit in the PCON register. In this mode none of the Timers are used and the clock comes from the internal phase 2 clock. SMOD = 1, Baud Rate = 1'32 Osc Freq. SMOD = 0, Baud Rate = 1'64 Osc Freq. To set the SMOD bit: ORL PCON,#80H. The address of PCON is 87H. SERIAL PORT IN MODE 3: The baud rate in mode 3 is variable and sets up exactly the same as in mode 1. 2-20 intel~ MCS®-51 PROGRAMMER'S GUIDE AND INSTRUCTION SET MCS®-S1 INSTRUCTION SET Table 10. 8051 Instruction Set Summary Interrupt Response Time: Refer to Hardware De- Oscillator Mnemonic Description Byte scription Chapter. Period ARITHMETIC OPERATIONS Instructions that Affect Flag Settings(1) ADD A,Rn Add register to 12 Instruction Flag Instruction Flag Accumulator C OV AC C OV AC ADD A,direct Add direct byte to 2 12 ADD X X X CLRC 0 Accumulator ADDC X X X CPLC X ADD A,@Ri Add indirect RAM 12 SUBS X X X ANLC,bit X to Accumulator MUL 0 X ANLC,/bit X ADD A, # data Add immediate 2 12 DIV 0 X ORLC,bit X data to DA X ORLC,bit X Accumulator RRC X MOVC,bit X ADDC A,Rn Add register to 12 RLC X CJNE X Accumulator SETSC with Carry (l)Note that operations on SFR byte address 208 or ADDC A,direct Add direct byte to 2 12 bit addresses 209-215 (i.e., the PSW or bits in the Accumulator PSW) will also affect flag settings. with Carry ADDC A,@Ri Add indirect 12 Note on instruction set and addressing modes: RAM to Rn - Register R 7 - RO of the currently se- Accumulator lected Register Bank. with Carry direct - 8-bit internal data location's address. ADDC A, # data Add immediate 2 12 This could be an Internal Data RAM data to Acc location (0-127) or a SFR [i.e., I/O with Carry port, control register, status register, SUSS A,Rn Subtract Register 12 etc. (128-255)]. from Acc with @Ri - 8-bit internal data RAM location (0- borrow 255) addressed indirectly through reg- SUSS A,direct Subtract direct 2 12 ister Rl or RO. byte from Acc # data - 8-bit constant included in instruction. with borrow # data 16 - 16-bit constant included in instruction. SUSS A,@Ri Subtract indirect 12 addr 16 - 16-bit destination address. Used by RAMfromACC LCALL & UMP. A branch can be with borrow anywhere within the 64K-byte Pro- SUSS A, # data Subtract 2 12 gram Memory address space. immediate data addr 11 - II-bit destination address. Used by from Acc with ACALL & AlMP. The branch will be borrow within the same 2K-byte page of pro- INC A Increment 12 gram memory as the first byte of the Accumulator following instruction. INC Rn Increment register 12 reI - Signed (two's complement) 8-bit offset INC direct Increment direct 2 12 byte. Used by SIMP and all condition- byte al jumps. Range is -128 to + 127 INC @Ri Increment direct 12 bytes relative to first byte of the fol- RAM lowing instruction. DEC A Decrement 12 bit - Direct Addressed bit in Internal Data Accumulator RAM or Special Function Register. DEC Rn Decrement 12 Register DEC direct Decrement direct 2 12 byte DEC @Ri Decrement 12 indirect RAM All mnemonics copyrighted @ Intel Corporation 1980 2-21 intel~ MCS®-S1 PROGRAMMER'S GUIDE AND INSTRUCTION SET Table 10.8051 Instruction Set Summary (Continued) Oscillator) I oscmator! Mnemonic Description Byte Mnemonic Description Byte Period Period ARITHMETIC OPERATIONS (Continued) LOGICAL OPERATIONS (Continued) INC DPTR Increment Data 24 RL A Rotate 12 Pointer Accumulator Left MUL AS Multiply A & S 48 RLC A Rotate 12 DIV AS Divide A by 8 48 Accumulator Left DA A Decimal Adjust 12 through the Carry Accumulator RR A Rotate 12 LOGICAL OPERATIONS Accumulator ANL A,Rn AND Register to 12 Right Accumulator RRC A Rotate 12 ANL A,direct AND direct byte 2 12 Accumulator to Accumulator Right through ANL A,@Ri AND indirect 12 the Carry RAM to SWAP A Swap nibbles 12 Accumulator within the ANL A,#data AND immediate 2 12 Accumulator data to DATA TRANSFER Accumulator MOV A,Rn Move 12 ANL direct,A AND Accumulator 2 12 register to to direct byte Accumulator ANL direct, # data AND immediate 3 24 MOV A,direct Move direct 2 12 data to direct byte byte to ORL A,Rn OR register to 12 Accumulator Accumulator MOV A,@Ri Move indirect 12 ORL A,direct OR direct byte to 2 12 RAM to Accumulator Accumulator ORL A,@Ri OR indirect RAM 12 MOV A,#data Move 2 12 to Accumulator immediate ORL A,#data OR immediate 2 12 data to data to Accumulator Accumulator MOV Rn,A Move 12 ORL direct, A OR Accumulator 2 12 Accumulator to direct byte to register ORL direct, # data OR immediate 3 24 MOV Rn,direct Move direct 2 24 data to direct byte byte to XRL A,Rn Exclusive-OR 12 register register to MOV Rn,#data Move 2 12 Accumulator immediate data XRL A,direct Exclusive-OR 2 12 to register direct byte to MOV direct,A Move 2 12 Accumulator Accumulator XRL A,@Ri Exclusive-OR 12 to direct byte indirect RAM to MOV direct,Rn Move register 2 24 Accumulator to direct byte XRL A,#data Exclusive-OR 2 12 MOV direct,direct Move direct 3 24 immediate data to byte to direct Accumulator MOV direct,@Ri Move indirect 2 24 XRL direct,A Exclusive-OR 2 12 RAM to Accumulator to direct byte direct byte MOV direct, # data Move 3 24 XRL direct, # data Exclusive-OR 3 24 immediate data immediate data to direct byte to direct byte MOV @Ri,A Move 12 CLR A Clear 12 Accumulator to Accumulator indirect RAM CPL A Complement 12 All mnemonics copyrighted @) Intel Corporation 1980 Accumulator 2-22 in1'et MCS®-S1 PROGRAMMER'S GUIDE AND INSTRUCTION SET Table 10. 8051 Instruction Set Summary (Continued) Oscillator Oscillator Mnemonic Description Byte Mnemonic Description Byte Period Period DATA TRANSFER (Continued) BOOLEAN VARIABLE MANIPULATION MOV @Ri,directMove direct 2 24 CLR C Clear Carry 1 12 byte to CLR bit Clear direct bit 2 12 indirect RAM SETS C Set Carry 1 12 MOV @Ri,#data Move 2 12 SETS bit Set direct bit 2 12 immediate CPL C Complement 12 data to Carry indirect RAM CPL bit Complement 2 12 MOV DPTR,#data16 Load Data 3 24 direct bit Pointer with a ANL C,bit AND direct bit 2 24 16-bit constant to CARRY MOVC A,@A+DPTR Move Code 24 ANL C,/bit AND complement 2 24 byte relative to of direct bit DPTRtoAcc to Carry MOVC A,@A+PC Move Code 24 ORL C,bit OR direct bit 2 24 byte relative to to Carry PCtoAcc ORL C,/bit OR complement 2 24 MOVX A,@Ri Move 24 of direct bit External to Carry RAM (8-bit MOV C,bit Move direct bit 2 12 addr) toAcc to Carry MOVX A,@DPTR Move 24 MOV bit,C Move Carry to 2 24 External direct bit RAM (16-bit JC rei Jump if Carry 2 24 addr) toAcc is set MOVX @Ai,A Move Accto 24 JNC rei Jump if Carry 2 24 External RAM not set (8-bit addr) JS bit,rel Jump if direct 3 24 MOVX @DPTR,A Move Acc to 24 Bit is set External RAM JNS bit, rei Jump if direct 3 24 (16-bit addr) Bit is Not set PUSH direct Push direct 2 24 JSC bit,rel Jump if direct 3 24 byte onto Bit is set & stack clear bit POP direct Pop direct 2 24 PROGRAM BRANCHING byte from ACALL addr11 Absolute 2 24 stack Subroutine XCH A,An Exchange 12 Call register with LCALL addr16 Long 3 24 Accumulator Subroutine XCH A,direct Exchange 2 12 Call direct byte RET Return from 24 with Subroutine Accumulator RETI Return from 24 XCH A,@Ri Exchange 12 interrupt indirect RAM AJMP addr11 Absolute 2 24 with Jump Accumulator LJMP addr16 Long Jump 3 24 XCHD A,@Ri Exchange low- 12 SJMP rei Short Jump 2 24 order Digit (relative addr) indirect RAM All mnemonics copyrighted <9lntel Corporation 1980 with Acc 2-23 intel~ MCS®-51 PROGRAMMER'S GUIDE AND INSTRUCTION SET Table 10. 8051 Instruction Set Summary (Continued) I Osciilator Oscillator I Mnemonic Description Byte Mnemonic Description Byte Period Period PROGRAM BRANCHING (Continued) PROGRAM BRANCHING (Continued) JMP @A+DPTR Jump indirect 24 CJNE Rn, # data,rel Compare 3 24 relative to the immediate to DPTR register and JZ rei Jump if 2 24 Jump if Not Accumulator Equal is Zero CJNE @Ri,#data,rel Compare 3 24 JNZ rei Jump if 2 24 immediate to Accumulator indirect and is Not Zero Jump if Not CJNE A,direct,rel Compare 3 24 Equal direct byte to DJNZ Rn,rel Decrement 2 24 AccandJump register and if Not Equal Jump if Not CJNE A,#data,rel Compare 3 24 Zero immediate to DJNZ direct, rei Decrement 3 24 Acc and Jump direct byte if Not Equal and Jump if Not Zero NOP No Operation 12 All mnemonics copyrighted @Intel Corporation 1980 2-24 intet MCS®-S1 PROGRAMMER'S GUIDE AND INSTRUCTION SET Table 11. Instruction Opcodes in Hexadecimal Order Hex Number Hex Number Mnemonic Operands Mnemonic Operands Code of Bytes Code of Bytes 00 1 NOP 33 1 RLC A 01 2 AJMP codeaddr 34 2 AD DC A, # data 02 3 LJMP codeaddr 35 2 ADDC A,dataaddr 03 1 RR A 36 1 ADDC A,@RO 04 1 INC A 37 1 ADDC A,@R1 05 2 INC dataaddr 38 1 ADDC A,RO 06 1 INC @RO 39 1 ADDC A,R1 07 1 INC @R1 3A 1 ADDC A,R2 08 1 INC RO 38 1 ADDC A,R3 09 1 INC R1 3C 1 ADDC A,R4 OA 1 INC R2 3D 1 ADDC A,R5 08 1 INC R3 3E 1 ADDC A,R6 OC 1 INC R4 3F 1 ADDC A,R7 OD 1 INC R5 40 2 JC code addr OE 1 INC R6 41 2 AJMP codeaddr OF 1 INC R7 42 2 ORL dataaddr,A 10 3 J8C bit addr, code addr 43 3 ORL data addr, # data 11 2 ACALL code addr 44 2 ORL A, # data 12 3 LCALL code addr 45 2 ORL A,dataaddr 13 1 RRC A 46 1 ORL A,@RO 14 1 DEC A 47 1 ORL A,@R1 15 2 DEC data addr 48 1 ORL A,RO 16 1 DEC @RO 49 1 ORL A,R1 17 1 DEC @R1 4A 1 ORL A,R2 18 1 DEC RO 48 1 ORL A,R3 19 1 DEC R1 4C 1 ORL A,R4 1A 1 DEC R2 4D 1 ORL A,R5 18 1 DEC R3 4E 1 ORL A,R6 1C 1 DEC R4 4F 1 ORL A,R7 10 1 DEC R5 50 2 JNC codeaddr 1E 1 DEC R6 51 2 ACALL code addr 1F 1 DEC R7 52 2 ANL dataaddr,A 20 3 J8 bit addr, code addr 53 3 ANL data addr, # data 21 2 AJMP code addr 54 2 ANL A, # data 22 1 RET 55 2 ANL A,dataaddr 23 1 RL A 56 1 ANL A,@RO 24 2 ADD A, # data 57 1 ANL A,@R1 25 2 ADD A,dataaddr 58 1 ANL A,RO 26 1 ADD A,@RO 59 1 ANL A,R1 27 1 ADD A,@R1 5A 1 ANL A,R2 28 1 ADD A,RO 58 1 ANL A,R3 29 1 ADD A,R1 5C 1 ANL A,R4 2A 1 ADD A,R2 50 1 ANL A,R5 28 1 ADD A,R3 5E 1 ANL A,R6 2C 1 ADD A,R4 5F 1 ANL A,R7 20 1 ADD A,R5 60 2 JZ code addr 2E 1 ADD A,R6 61 2 AJMP codeaddr 2F 1 ADD A,R7 62 2 XRL dataaddr,A 30 3 JN8 bit addr, code addr 63 3 XRL data addr,#data 31 2 ACALL codeaddr 64 2 XRL A, # data 32 1 RETI 65 2 XRL A,dataaddr 2-25 intelGD MCS®·51 PROGRAMMER'S GUIDE AND INSTRUCTION SET Table 11. Instruction Opcodes in Hexadecimal Order (Continued) u .... "~A •.........,IfW. ........ ha .. Mnemonic Operands I _- .._..._-- I ..!.lAY N"mhAr Mnemonic Operands Code of Bytes Code of Bytes 66 1 XRL A,@RO 99 1 SUBB A,R1 67 1 XRL A,@R1 9A 1 SUBB A,R2 68 1 XRL A,RO 9B 1 SUBB A,R3 69 1 XRL A,R1 9C 1 SUBB A,R4 6A 1 XRL A,R2 9D 1 SUBB A,R5 6B 1 XRL A,R3 9E 1 SUBB A,R6 6C 1 XRL A,R4 9F 1 SUBB A,R7 6D 1 XRL A,R5 AO 2 ORL C,/bitaddr 6E 1 XRL A,R6 A1 2 AJMP codeaddr 6F 1 XRL A,R7 A2 2 MOV C,bitaddr 70 2 JNZ code addr A3 1 INC DPTR 71 2 ACALL code addr A4 1 MUL AB 72 2 ORL C,bitaddr A5 reserved 73 1 JMP @A+DPTR A6 2 MOV @RO,data addr 74 2 MOV A, # data A7 2 MOV @R1,data addr 75 3 MOV data addr, # data A8 2 MOV RO,data addr 76 2 MOV @RO,#data A9 2 MOV R 1,data addr 77 2 MOV @R1,#data AA 2 MOV R2,data addr 78 2 MOV RO,#data AB 2 MOV R3,data addr 79 2 MOV R1,#data AC 2 MOV R4,data addr 7A 2 MOV R2,#data AD 2 MOV R5,data addr 7B 2 MOV R3,#data AE 2 MOV R6,data addr 7C 2 MOV R4,#data AF 2 MOV R7,data addr 7D 2 MOV R5,#data BO 2 ANL C,/bitaddr 7E 2 MOV R6,#data B1 2 ACALL codeaddr 7F 2 MOV R7,#data B2 2 CPL bitaddr 80 2 SJMP code addr B3 1 CPL C 81 2 AJMP code addr B4 3 CJNE A, # data, code addr 82 2 ANL C,bitaddr B5 3 CJNE A,data addr,code addr 83 1 MOVC A,@A+PC B6 3 CJNE @RO,#data,codeaddr 84 1 DIV AB B7 3 CJNE @R1,#data,codeaddr 85 3 MOV data addr, data addr B8 3 CJNE RO,#data,code addr 86 2 MOV data addr,@RO B9 3 CJNE R1 , # data,code addr 87 2 MOV data addr,@R1 BA 3 CJNE R2,#data,code addr 88 2 MOV data addr,RO BB 3 CJNE R3,#data,code addr 89 2 MOV data addr,R1 BC 3 CJNE R4,#data,code addr 8A 2 MOV dataaddr,R2 BD 3 CJNE R5,#data,code addr 8B 2 MOV data addr,R3 BE 3 CJNE R6, # data,code addr 8C 2 MOV data addr,R4 BF 3 CJNE R7, # data, code addr 8D 2 MOV data addr,R5 CO 2 PUSH dataaddr 8E 2 MOV data addr,R6 C1 2 AJMP code addr 8F 2 MOV data addr,R7 C2 2 CLR bitaddr 90 3 MOV DPTR,#data C3 1 CLR C 91 2 ACALL code addr C4 1 SWAP A 92 2 MOV bitaddr,C C5 2 XCH A,dataaddr 93 1 MOVC A,@A+DPTR C6 1 XCH A,@RO 94 2 SUBB A, # data C7 1 XCH A,@R1 95 2 SUBB A,dataaddr C8 1 XCH A,RO 96 1 SUBB A,@RO C9 1 XCH A,R1 97 1 SUBB A,@R1 CA 1 XCH A,R2 98 1 SUBB A,RO CB 1 XCH A,R3 2-26 MCS®-51 PROGRAMMER'S GUIDE AND INSTRUCTION SET Table 11. Instruction Opcodes in Hexadecimal Order (Continued) Hex Number Hex Number Mnemonic Operands Mnemonic Operands Code of Bytes Code of Bytes CC 1 XCH A,R4 E6 1 MOV A,@RO CO 1 XCH A,R5 E7 1 MOV A,@R1 CE 1 XCH A,R6 E8 1 MOV A,RO CF 1 XCH A,R7 E9 1 MOV A,R1 DO 2 POP dataaddr EA 1 MOV A,R2 01 2 ACALL code addr EB 1 MOV A,R3 02 2 SETB bitaddr EC 1 MOV A,R4 03 1 SETS C ED 1 MOV A,R5 04 1 OA A EE 1 MOV A,R6 05 3 OJNZ data addr,code addr EF 1 MOV A,R7 06 1 XCHO A,@RO FO 1 MOVX @OPTR,A 07 1 XCHO A,@R1 F1 2 ACALL code addr 08 2 OJNZ RO,code addr F2 1 MOVX @RO,A 09 2 OJNZ R1 ,code addr F3 1 MOVX @R1,A OA 2 OJNZ R2,code addr F4 1 CPL A DB 2 OJNZ R3,code addr F5 2 MOV dataaddr,A DC 2 OJNZ R4,code addr F6 1 MOV @RO,A DO 2 OJNZ R5,code addr F7 1 MOV @R1,A DE 2 OJNZ R6,code addr F8 1 MOV RO,A OF 2 OJNZ R7,code addr F9 1 MOV R1,A EO 1 MOVX A,@OPTR FA 1 MOV R2,A E1 2 AJMP code addr FB 1 MOV R3,A E2 1 MOVX A,@RO FC 1 MOV R4,A E3 1 MOVX A,@R1 FO 1 MOV R5,A E4 1 CLR A FE 1 MOV R6,A E5 2 MOV A,dataaddr FF 1 MOV R7,A 2-27 MCS®-51 PROGRAMMER'S GUIDE AND INSTRUCTION SET INSTRUCTION DEFINITIONS ACALL addr11 Function: Absolute Call Description: ACALL unconditionally calls a subroutine located at the indicated address. The instruction increments the PC twice to obtain the address of the following instruction, then pushes the 16-bit result onto the stack (low-order byte fIrst) and increments the Stack Pointer twice. The destination address is obtained by successively concatenating the fIve high-order bits of the incremented PC, opcode bits 7-5, and the second byte of the instruction. The subroutine called must therefore start within the same 2K block of the program memory as the fIrst byte of the instruction following ACALL. No flags are affected. Example: Initially SP equals 07H. The label "SUBRTN" is at program memory location 0345 H. After executing the instruction, ACALL SUBRTN at location 0123H, SP will contain 09H, internal RAM locations 08H and 09H will contain 25H and 01H, respectively, and the PC will contain 0345H. Bytes: 2 Cycles: 2 Encoding: I a10 a9 a8 1 0 0 0 1 a7 a6 as a4 a3 a2 a1 aO Operation: ACALL (PC) ~ (PC) + 2 (SP) ~ (SP) + 1 «SP» ~ (PC7-O) (SP) ~ (SP) + 1 «SP» ~ (PCI5-S) (PCIO-O) ~ page address 2-28 infel~ MCS®-51 PROGRAMMER'S GUIDE AND INSTRUCTION SET ADD A, Function: Add Description: ADD adds the byte variable indicated to the Accumulator, leaving the result in the Accumula- tor. The carry and auxiliary-carry flags are set, respectively, if there is a carry-out from bit 7 or bit 3, and cleared otherwise. When adding unsigned integers, the carry flag indicates an overflow occured. OV is set if there is a carry-out of bit 6 but not out of bit 7, or a carry-out of bit 7 but not bit 6; otherwise OV is cleared. When adding signed integers, OV indicates a negative number pro- duced as the sum of two positive operands, or a positive sum from two negative operands. Four source operand addressing modes are allowed: register, direct, register-indirect, or imme- diate. Example: The Accumulator holds OC3H (llOOOOllB) and register 0 holds OAAH (10 10 10 lOB). The instruction, ADD A,RO will leave 6DH (OllOllOlB) in the Accumulator with the AC flag cleared and both the carry flag and OV set to 1. ADD A,Rn Bytes: Cycles: Encoding: I0 0 0 1 r r r Operation: ADD (A) ~ (A) + (Rn) ADD A,direct Bytes: 2 Cycles: Encoding: I0 0 0 o1 0 1 direct address Operation: ADD (A) ~ (A) + (direct) 2-29 inteL MCS®-S1 PROGRAMMER'S GUIDE AND INSTRUCTION SET ADD A,@Ri Bytes: Cycles: Encoding: 100 1 0 011 Operation: ADD (A) +- (A) + «Rj) ADD A, # data Bytes: 2 Cycles: Encoding: 100 1 0 o100 immediate data Operation: ADD (A) +- (A) + # data ADDC A, < src-byte > Function: Add with Carry Description: ADDC simultaneously adds the byte variable indicated, the carry flag and the Accumulator contents, leaving the result in the Accumulator. The carry and auxiliary-carry flags are set, respectively, if there is a carry-out from bit 7 or bit 3, and cleared otherwise. When adding unsigned integers, the carry flag indicates an overflow occured. OV is set if there is a carry-out of bit 6 but not out of bit 7, or a carry-out of bit 7 but not out of bit 6; otherwise OV is cleared. When adding signed integers, OV indicates a negative number produced as the sum of two positive operands or a positive sum from two negative operands. Four source operand addressing modes are allowed: register, direct, register-indirect, or imme- diate. Example: The Accumulator holds OC3H (1 100001 1B) and register 0 holds OAAH (10101010B) with the carry flag set. The instruction, ADDC A,RO wi11leave 6EH (01 101 1lOB) in the Accumulator with AC cleared and both the Carry flag and OV set to 1. 2-30 inteL MCS®-S1 PROGRAMMER'S GUIDE AND INSTRUCTION SET ADDC A,Rn Bytes: Cycles: Encoding: I °° 1 r r r I Operation: ADDC (A) ~ (A) + (C) + (RJ ADDC A,direct Bytes: 2 Cycles: Encoding: I °° 1 1 °1 °1 direct address I Operation: ADDC (A) ~ (A) + (C) + (direct) ADDC A,@Ri Bytes: Cycles: Encoding: i _O_O_1_....L...-0_ 1 _1----1 L....I Operation: ADDC (A) ~ (A) + (C) + «RJ) AD DC A, # data Bytes: 2 Cycles: Encoding: L..1_O_O_1_....L...-0_1_O_0...J immediate data Operation: ADDC (A) ~ (A) + (C) + #data 2-31 int:eL MCS®-51 PROGRAMMER'S GUIDE AND INSTRUCTION SET AJMP addr11 Function: Absolute Jump Description: AJMP transfers program execution to the indicated address, which is fonned at run-time by concatenating the high-order five bits of the PC (after incrementing the PC twice), opcode bits 7-5, and the second byte of the instruction. The destination must therefore be within the same 2K block of program memory as the first byte of the instruction following AJMP. Example: The label "JMPADR" is at program memory location 0123H. The instruction, AJMP JMPADR is at location 0345H and will load the PC with 0123H. Bytes: 2 Cycles: 2 Encoding: I a10 a9 a8 0 0 0 0 1 a7 a6 a5 a4 a3 a2 a1 aO Operation: AJMP (PC) ~ (PC) + 2 (PClO-O) ~ page address ANL < dest-byte > , < src-byte > Function: Logical-AND for byte variables Description: ANL perfonns the bitwise logical-AND operation between the variables indicated and stores the results in the destination variable. No flags are affected. The two operands allow six addressing mode combinations. When the destination is the Accu- mulator, the source can use register, direct, register-indirect, or immediate addressing; when the destination is a direct address, the source can be the Accumulator or immediate data. Note: When this instruction is used to modify an output port, the value used as the original port data will be read from the output data latch, not the input pins. Example: If the Accumulator holds OC3H (ll0000llB) and register 0 holds 55H (010101OIB) then the instruction, ANL A,RO will leave 4lH (OIOOOOOIB) in the Accumulator. When the destination is a directly addressed byte, this instruction will clear combinations of bits in any RAM location or hardware register. The mask byte detennining the pattern of bits to be cleared would either be a constant contained in the instruction or a value computed in the Accumulator at run-time. The instruction, ANL PI, #Olll00IIB will clear bits 7, 3, and 2 of output port 1. 2-32 intel~ MCS®-S1 PROGRAMMER'S GUIDE AND INSTRUCTION SET ANL A,Rn Bytes: Cycles: Encoding: 01 o 1 1 r r r 1 Operation: ANL (A) ~ (A) 1\ (Rn) ANL A,direct Bytes: 2 Cycles: Encoding: 1 0101 o1 0 1 direct address Operation: ANL (A) ~ (A) 1\ (direct) ANL A,@Ri Bytes: Cycles: Encoding: 1 0101 011 Operation: ANL (A) ~ (A) 1\ «Ri» ANL A, # data Bytes: 2 Cycles: Encoding: I0 1 o 1 o 1 0 0 immediate data Operation: ANL (A) ~ (A) 1\ #data ANL direct,A Bytes: 2 Cycles: Encoding: 0101 001 0 direct address 1 Operation: ANL (direct) ~ (direct) 1\ (A) 2-33 iniaL MCS®·51 PROGRAMMER'S GUIDE AND INSTRUCTION SET ANL direct, # data Byies: 3 Cycles: 2 Encoding: I0 1 0 1 0 0 1 1 direct address immediate data Operation: ANL (direct) +- (direct) 1\ #data ANL C, Function: Logical-AND for bit variables Description: If the Boolean value of the source bit is a logical 0 then clear the carry flag; otherwise leave the carry flag in its current state. A slash ("I") preceding the operand in the assembly language indicates that the logical complement of the addressed bit is used as the source value, but the source bit itself is not affected. No other flags are affected. Only direct addressing is allowed for the source operand. Example: Set the carry flag if, and only if, P1.0 = 1, ACe. 7 = 1, and OV = 0: MOV C,P1.0 ;LOAD CARRY WITH INPUT PIN STATE ANL C,ACC.7 ;AND CARRY WITH ACCUM. BIT 7 ANL C,/OV ;AND WITH INVERSE OF OVERFLOW FLAG ANL C,bit Bytes: 2 Cycles: 2 Encoding: I 1 000 001 0 bit address Operation: ANL (C) +- (C) 1\ (bit) ANL C,/bit Bytes: 2 Cycles: 2 Encoding: I1 o1 o0 0 0 bit address Operation: ANL (C) +- (C) 1\ -, (bit) 2-34 inteL MCS®-51 PROGRAMMER'S GUIDE AND INSTRUCTION SET CJNE < dest-byte > , < src-byte >, rei Function: Compare and Jump if Not Equal. Description: CJNE compares the magnitudes of the first two operands, and branches if their values are not equal. The branch destination is computed by adding the signed relative-displacement in the last instruction byte to the PC, after incrementing the PC to the start of the next instruction. The carry flag is set if the unsigned integer value of is less than the unsigned integer value of < src-byte > ; otherwise, the carry is cleared. Neither operand is affected. The first two operands allow four addressing mode combinations: the Accumulator may be compared with any directly addressed byte or immediate data, and any indirect RAM location or working register can be compared with an immediate constant. Example: The Accumulator contains 34H. Register 7 contains 56H. The first instruction in the se- quence, CJNE R7,#60H, NOT_EQ R7 = 6OH. JC IFR7 < 6OH. R7> 6OH. sets the carry flag and branches to the instruction at label NOTJQ. By testing the carry flag, this instruction determines whether R 7 is greater or less than 6OH. If the data being presented to Port I is also 34H, then the instruction, WAIT: CJNE A,PI,WAIT clears the carry flag and continues with the next instruction in sequence, since the Accumula- tor does equal the data read from PI. (If some other value was being input on PI, the program will loop at this point until the PI data changes to 34H.) CJNE A,direct,rel Bytes: 3 Cycles: 2 Encoding: I1011 0 1 0 1 direct address reI. address Operation: (PC) +- (PC) + 3 IF (A) < > (direct) THEN (PC) +- (PC) + relative offtet IF (A) < (direct) THEN (C) +- I ELSE (C) +- 0 2-35 infel~ MCS®-51 PROGRAMMER'S GUIDE AND INSTRUCTION SET CJNE A, # data,rel Bytes: 3 Cycles: 2 Encoding: L..I_1_o_1_-,--0_1_O_0---J immediate data reI. address Operation: (PC) ~ (PC) + 3 IF (A) < > data THEN (PC) ~ (PC) + relative offset IF (A) < data THEN (C)~ 1 ELSE (C)~O CJNE Rn, # data,rel Bytes: 3 Cycles: 2 Encoding: I 1 °1 1 1 r r r immediate data reI. address Operation: (PC) ~ (PC) + 3 IF (Rn) < > data THEN (PC) ~ (PC) + relative offset IF (Rn) < data THEN (C)~ 1 ELSE (C)~O CJNE @Ri,#data,rel Bytes: 3 Cycles: 2 Encoding: 1L..-1_O_1_-,--0_1_1--J immediate data reI. address I Operation: (PC) ~ (PC) + 3 IF «Ri» < > data THEN (PC) ~ (PC) + relative offset IF «Ri» < data THEN (C)~ 1 ELSE (C)~O 2-36 infel~ MCS®-51 PROGRAMMER'S GUIDE AND INSTRUCTION SET CLR A Function: Clear Accumulator Description: The Accumulator is cleared (all bits set on zero). No flags are affected. Example: The Accumulator contains 5CH (01011100B). The instruction, CLR A will leave the Accumulator set to OOH «()()(x)()()()()B). Bytes: Cycles: Encoding: I1 1 0 0 1 0 0 Operation: CLR (A) +-- 0 CLR bit Function: Clear bit Description: The indicated bit is cleared (reset to zero). No other flags are affected. CLR can operate on the carry flag or any directly addressable bit. Example: Port 1 has previously been written with 5DH (010111OlB). The instruction, CLR P1.2 will leave the port set to 59H (01011001B). CLR C Bytes: Cycles: Encoding: I1 0 0 0 0 1 1 Operation: CLR (C) +-- 0 CLR bit Bytes: 2 Cycles: Encoding: I1 0 0 0 0 1 0 bit address Operation: CLR (bit) +-- 0 2-37 MCS®-51 PROGRAMMER'S GUIDE AND INSTRUCTION SET CPL A Function: Complement Accumulator Description: Each bit of the Accumulator is logically complemented (one's complement). Bits which previ- ously contained a one are changed to a zero and vice-versa. No flags are affected. Example: The Accumulator contains SCH (01011100B). The instruction, CPL A wi11leave the Accumulator set to OA3H (lOlOOOllB). Bytes: Cycles: Encoding: I1 1 1 0 1 0 0 Operation: CPL (A) ~-, (A) CPL bit Function: Complement bit Description: The bit variable specified is complemented. A bit which had been a one is changed to zero and vice-versa. No other flags are affected. CLR can operate on the carry or any directly address- able bit. Note: When this instruction is used to modify an output pin, the value used as the original data will be read from the output data latch, not the input pin. Example: Port 1 has previously been written with SBH (01011101B). The instruction sequence, CPL Pl.l CPL Pl.2 wi11leave the port set to SBH (01011011B). CPL C Bytes: Cycles: Encoding: L...I _1_0_1_....1..-0_ 0_ 1_ 1--1 Operation: CPL (C) ~-, (C) 2-38 int'eL MCS®-S1 PROGRAMMER'S GUIDE AND INSTRUCTION SET CPL bit Bytes: 2 Cycles: Encoding: I 1 011 001 0 bit address Operation: CPL (bit) +- -, (bit) DA A Function: Decimal-adjust Accumulator for Addition Description: DA A adjusts the eight-bit value in the Accumulator resulting from the earlier addition of two variables (each in packed-BCD format), producing two four-bit digits. Any ADD or ADDC instruction may have been used to perform the addition. If Accumulator bits 3-0 are greater than nine (xxxxlOlO-xxxxllll), or if the AC flag is one, six is added to the Accumulator producing the proper BCD digit in the low-order nibble. This internal addition would set the carry flag if a carry-out of the low-order four-bit field propagat- ed through all high-order bits, but it would not clear the carry flag otherwise. If the carry flag is now set, or if the four high-order bits now exceed nine (lOlOxxxx-lllxxxx), these high-order bits are incremented by six, producing the proper BCD digit in the high-order nibble. Again, this would set the carry flag if there was a carry-out of the high-order bits, but wouldn't clear the carry. The carry flag thus indicates if the sum of the original two BCD variables is greater than 100, allowing multiple precision decimal addition. OV is not affected. All of this occurs during the one instruction cycle. Essentially, this instruction performs the. decimal conversion by adding OOH, 06H, 6OH, or 66H to the Accumulator, depending on initial Accumulator and PSW conditions. Note: DA A cannot simply convert a hexadecimal number in the Accumulator to BCD nota- tion, nor does DA A apply to decimal subtraction. 2-39 intel ~ MCS@·51 PROGRAMMER'S GUIDE AND INSTRUCTION SET Example: The Accumulator holds the value 56H (010101IOB) representing the packed BCD digits of the decimal number 56. Register 3 contains the value 67H (OII00IIIB) representing the packed BCD digits of the decimal number 67. The carry flag is set. The instruction sequence. ADDC A,R3 DA A will first perform a standard twos-complement binary addition, resulting in the value OBEH (10111110) in the Accumulator. The carry and auxiliary carry flags will be cleared. The Decimal Adjust instruction will then alter the Accumulator to the value 24H (OOI00I00B), indicating the packed BCD digits of the decimal number 24, the low-order two digits of the decimal sum of 56,67, and the carry-in. The carry flag will be set by the Decimal Adjust instruction, indicating that a decimal overflow occurred. The true sum 56, 67, and 1 is 124. BCD variables can be incremented or decremented by adding 01H or 99H. If the Accumulator initially holds 30H (representing the digits of 30 decimal), then the instruction sequence, ADD A,#99H DA A will leave the carry set and 29H in the Accumulator, since 30 + 99 = 129. The low-order byte of the sum can be interpreted to mean 30 - 1 = 29. Bytes: Cycles: Encoding: I1 10 1 0 1 0 0 Operation: DA -contents of Accumulator are BCD IF U(A3-O) > 9] V [(AC) = 1]] THEN(A3_0) ~ (A3-O) + 6 AND IF U(A7-4) > 9] V [(C) = 1]] THEN (A7-4) ~ (A7-4) + 6 2-40 infel4P MCS®-51 PROGRAMMER'S GUIDE AND INSTRUCTION SET DEC byte Function: Decrement Description: The variable indicated is decremented by 1. An original value ofOOH will underflow to OFFH. No flags are affected. Four operand addressing modes are allowed: accumulator, register, direct, or register-indirect. Note: When this instruction is used to modify an output port, the value used as the original port data will be read from the output data latch, not the input pins. Example: Register 0 contains 7FH (OI111111B). Internal RAM locations 7EH and 7FH contain OOH and 4OH, respectively. The instruction sequence, DEC @RO DEC RO DEC @RO wi11leave register 0 set to 7EH and internal RAM locations 7EH and 7FH set to OFFH and 3FH. DEC A Bytes: Cycles: Encoding: 1 °0 0 1 0 1 0 0 Operation: DEC (A) ~ (A) - I DEC Rn Bytes: Cycles: Encoding: ...._O_O_O_1--,-_1_r_r_r....l 1 Operation: DEC (Rn) ~ (Rn) - 1 2-41 intelGP MCS®·51 PROGRAMMER'S GUIDE AND INSTRUCTION SET DEC direct Bytes: 2 Cycles: Encoding: 1 0001 o1 0 1 direct address Operation: DEC (direct) ~ (direct) - 1 DEC @Ri Bytes: Cycles: Encoding: I 000 1 011 Operation: DEC «Ri» ~ «Ri» - 1 DlV AB Function: Divide Description: DIV AB divides the unsigned eight-bit integer in the Accumulator by the unsigned eight-bit integer in register B. The Accumulator receives the integer part of the quotient; register B receives the integer remainder. The carry and OV flags will be cleared. Exception: if B had originally contained OOH, the values returned in the Accumulator and B- register will be undefined and the overflow flag will be set. The carry flag is cleared in any case. Example: The Accumulator contains 251 (OFBH or lllllOllB) and B contains 18 (12H or 000100lOB). The instruction, DIV AB will leave 13 in the Accumulator (ODH or 00001101B) and the value 17 (11H or 0001000IB) in B, since 251 = (13 X 18) + 17. Carry and OV will both be cleared. Bytes: Cycles: 4 Encoding: I1000 0 1 0 0 Operation: DIV ~?;~8 ~ (A)/(B) 2-42 int:et MCS®-S1 PROGRAMMER'S GUIDE AND INSTRUCTION SET DJNZ , Function: Decrement and Jump if Not Zero Description: DJNZ decrements the location indicated by 1, and branches to the address indicated by the second operand if the resulting value is not zero. An original value of OOH will underflow to OFFH. No flags are affected. The branch destination would be computed by adding the signed relative-displacement value in the last instruction byte to the PC, after incrementing the PC to the fIrst byte of the following instruction. The location decremented may be a register or directly addressed byte. Note: When this instruction is used to modify an output port, the value used as the original port data will be read from the output data latch, not the input pins. Example: Internal RAM locations 4OH, SOH, and 60H contain the values 01H, 70H, and ISH, respec- tively. The instruction sequence, DJNZ 4OH,LABEL_l DJNZ SOH,LABEL_2 DJNZ 6OH,LABEL_3 will cause a jump to the instruction at label LABEL_2 with the values OOH, 6FH, and ISH in the three RAM locations. The fIrst jump was not taken because the result was zero. This instruction provides a simple way of executing a program loop a given number of times, or for adding a moderate time delay (from 2 to S12 machine cycles) with a single instruction. The instruction sequence, MOV R2,#8 TOGGLE: CPL P1.7 DJNZ R2,TOGGLE will toggle P1.7 eight times, causing four output pulses to appear at bit 7 of output Port 1. Each pulse will last three machine cycles; two for DJNZ and one to alter the pin. DJNZ Rn,rel Bytes: 2 Cycles: 2 Encoding: L...1_1_1_0_1-..L_1_r_r_r-, reI. address Operation: DJNZ (PC) ~ (PC) + 2 (Rn) ~ (Rn) - 1 IF (Rn) > 0 or (Rn) < 0 THEN (PC) ~ (PC) + reI 2-43 infel~ MCS®-51 PROGRAMMER'S GUIDE AND INSTRUCTION SET DJNZ direct,rel Bytes: 3 Cycles: 2 Encoding: I 1 10 1 0 1 0 1 direct address reI. address Operation: DJNZ (PC) ~ (PC) + 2 (direct) ~ (direct) - 1 IF (direct) > 0 or (direct) < 0 THEN (PC) ~ (PC) + reI INC Function: Increment Description: INC increments the indicated variable by 1. An original value of OFFH will overflow to OOH. No flags are affected. Three addressing modes are allowed: register, direct, or register-indirect. Note: When this instruction is used to modify an output port, the value used as the original port data will be read from the output data latch, not the input pins. Example: Register 0 contains 7EH (011111110B). Internal RAM locations 7EH and 7PH contain OFFH and 4OH, respectively. The instruction sequence, INC @RO INC RO INC @RO will leave register 0 set to 7PH and internal RAM locations 7EH and 7PH holding (respective- ly) OOH and 41H. INC A Bytes: Cycles: Encoding: I 0 0 0 0 I0 1 0 0 Operation: INC (A)~(A) + 2-44 intelqp MCS®-S1 PROGRAMMER'S GUIDE AND INSTRUCTION SET INC Rn Bytes: Cycles: Encoding: I 0 000 I 1 r r r Operation: INC (Rn) ~ (Rn) + INC direct Bytes: 2 Cycles: Encoding: I0 0 0 0 Io1 0 1 direct address Operation: INC (direct) ~ (direct) + INC @Ri Bytes: Cycles: Encoding: I 0 000 I 011 Operation: INC «Ri» ~ «Ri» + INC DPTR Function: Increment Data Pointer Description: Increment the 16-bit data pointer by 1. A 16-bit increment (modulo 2 16) is performed; an overflow of the low-order byte of the data pointer (DPL) from OFFH to OOH will increment the high-order byte (DPH). No flags are affected. This is the only 16-bit register which can be incremented. Example: Registers DPH and DPL contain 12H and OFEH, respectively. The instruction sequence, INC DPTR INC DPTR INC DPTR will change DPH and DPL to 13H and 01H. Bytes: Cycles: 2 Encoding: I1 0 10 0 0 1 1 Operation: INC (DPTR) ~ (DPTR) + 1 2-45 intelQD MCS®-S1 PROGRAMMER'S GUIDE AND INSTRUCTION SET JB bit,rel Function: Jump if Bit set Description: If the indicated bit is a one, jump to the address indicated; otherwise proceed with the next instruction. The branch destination is computed by adding the signed relative-displacement in the third instruction byte to the PC, after incrementing the PC to the first byte of the next instruction. The bit tested is not modified. No flags are affected. Example: The data present at input port 1 is llOOlOlOB. The Accumulator holds 56 (010101 lOB). The instruction sequence, JB P1.2,LABELl JB ACC.2,LABEL2 will cause program execution to branch to the instruction at label LABEL2. Bytes: 3 Cycles: 2 Encoding: I 0 0 10 0 0 0 0 bit address reI. address Operation: JB (PC) ~ (PC) + 3 IF (bit) = 1 THEN (PC) ~ (PC) + reI JBC bit, rei Function: Jump if Bit is set and Clear bit Description: If the indicated bit is one, branch to the address indicated; otherwise proceed with the next instruction. The bit will not be cleared if it is already a zero. The branch destination is comput- ed by adding the signed relative-displacement in the third instruction byte to the PC, after incrementing the PC to the first byte of the next instruction. No flags are affected. Note: When this instruction is used to test an output pin, the value used as the original data will be read from the output data latch, not the input pin. Example: The Accumulator holds 56H (010101 lOB). The instruction sequence, JBC ACC.3,LABELl JBC ACC.2,LABEL2 will cause program execution to continue at the instruction identified by the label LABEL2, with the Accumulator modified to 52H (OlOlOOlOB). 2-46 intel ~ MCS®-51 PROGRAMMER'S GUIDE AND INSTRUCTION SET Bytes: 3 Cycles: 2 Encoding: I0 0 0 1 0 0 0 0 bit address reI. address Operation: mc (PC) +- (PC) + 3 IF (bit) = I THEN (bit) +- 0 (PC) +- (PC) + reI JC rei Function: Jump if Carry is set Description: If the carry flag is set, branch to the address indicated; otherwise proceed with the next instruction. The branch destination is computed by adding the signed relative-displacement in the second instruction byte to the PC, after incrementing the PC twice. No flags are affected. Example: The carry flag is cleared. The instruction sequence, JC LABELl CPL C JC LABEL 2 will set the carry and cause program execution to continue at the instruction identified by the label LABEL2. Bytes: 2 Cycles: 2 Encoding: I0 10 0 0 0 0 0 reI. address Operation: JC (PC) +- (PC) + 2 IF (C) = I THEN (PC) +- (PC) + reI 2-47 int:et MCS®-S1 PROGRAMMER'S GUIDE AND INSTRUCTION SET JMP @A+DPTR Function: Jump indirect Description: Add the eight-bit unsigned contents of the Accumulator with the sixteen-bit data pointer, and load the resulting sum to the program counter. This will be the address for subsequent instruc- tion fetches. Sixteen-bit addition is performed (modulo 2 16): a carry-out from the low-order eight bits propagates through the higher-order bits. Neither the Accumulator nor the Data Pointer is altered. No flags are affected. Example: An even number from 0 to 6 is in the Accumulator. The following sequence of instructions will branch to one of four AJMP instructions in a jump table starting at JMP_TBL: MOV DPTR, #JMP_TBL JMP @A+DPTR AJMP LABELO AJMP LABELl AJMP LABEL2 AJMP LABEL3 If the Accumulator equals 04H when starting this sequence, execution will jump to label LABEL2. Remember that AJMP is a two-byte instruction, so the jump instructions start at every other address. Bytes: Cycles: 2 Encoding: I0 1 1 1 0 0 1 1 Operation: JMP (PC) ~ (A) + (DPTR) 2-48 infel~ MCS®-51 PROGRAMMER'S GUIDE AND INSTRUCTION SET JNB bit,rel Function: Jump if Bit Not set Description: If the indicated bit is a zero, branch to the indicated address; otherwise proceed with the next instruction. The branch destination is computed by adding the signed relative-displacement in the third instruction byte to the PC, after incrementing the PC to the first byte of the next instruction. The bit tested is not modified. No flags are affected. Example: The data present at input port 1 is 110010 lOB. The Accumulator holds 56H (010101 lOB). The instruction sequence, JNB P1.3,LABELl JNB ACC.3,LABEL2 will cause program execution to continue at the instruction at label LABEL2. Bytes: 3 Cycles: 2 Encoding: I0 0 1 1 0 0 0 0 bit address reI. address Operation: JNB (PC) ~ (PC) + 3 IF (bit) = 0 THEN (PC) ~ (PC) + reI. JNC rei Function: Jump if Carry not set Description: If the carry flag is a zero, branch to the address indicated; otherwise proceed with the next instruction. The branch destination is computed by adding the signed relative-displacement in the second instruction byte to the PC, after incrementing the PC twice to point to the next instruction. The carry flag is not modified. Example: The carry flag is set. The instruction sequence, JNC LABEll CPL C JNC LABEL2 will clear the carry and cause program execution to continue at the instruction identified by the label LABEL2. Bytes: 2 Cycles: 2 Encoding: I0 1 0 1 0 0 0 0 reI. address Operation: JNC (PC) ~ (PC) + 2 IF (C) = 0 THEN (PC) ~ (PC) + reI 2-49 inteL MCS®-51 PROGRAMMER'S GUIDE AND INSTRUCTION SET JNZ rei Function: Jump if Accumulator Not Zero Description: If any bit of the Accumulator is a one, branch to the indicated address; otherwise proceed with the next instruction. The branch destination is computed by adding the signed relative-dis- placement in the second instruction byte to the PC, after incrementing the PC twice. The Accumulator is not modified. No flags are affected. Example: The Accumulator originally holds OOH. The instruction sequence, JNZ LABELl INC A JNZ LABEL2 will set the Accumulator to OIH and continue at label LABEL2. Bytes: 2 Cycles: 2 Encoding: I0 1 1 1 0 0 0 0 reI. address Operation: JNZ (PC) ~ (PC) + 2 IF (A) of: 0 THEN (PC) ~ (PC) + reI JZ rei Function: Jump if Accumulator Zero Description: If all bits of the Accumulator are zero, branch to the address indicated; otherwise proceed with the next instruction. The branch destination is computed by adding the signed relative-dis- placement in the second instruction byte to the PC, after incrementing the PC twice. The Accumulator is not modified. No flags are affected. Example: The Accumulator originally contains OIH. The instruction sequence, JZ LABELl DEC A JZ LABEL2 will change the Accumulator to OOH and cause program execution to continue at the instruc- tion identified by the label LABEL2. Bytes: 2 Cycles: 2 Encoding: I0 110 0 0 0 0 reI. address Operation: JZ (PC) ~ (PC) + 2 IF (A) = 0 THEN (PC) ~ (PC) + reI 2-50 inteL MCS®-51 PROGRAMMER'S GUIDE AND INSTRUCTION SET LCALL addr16 Function: Long call Description: LCALL calls a subroutine located at the indicated address. The instruction adds three to the program counter to generate the address of the next instruction and then pushes the 16-bit result onto the stack (low byte first), incrementing the Stack Pointer by two. The high-order and low-order bytes of the PC are then loaded, respectively, with the second and third bytes of the LCALL instruction. Program execution continues with the instruction at this address. The subroutine may therefore begin anywhere in the full 64K-byte program memory address space. No flags are affected. Example: Initially the Stack Pointer equals 07H. The label "SUBRTN" is assigned to program memory location 1234H. After executing the instruction, LCALL SUBRTN at location 0123H, the Stack Pointer will contain 09H, internal RAM locations 08H and 09H will contain 26H and 01H, and the PC will contain 1234H. Bytes: 3 Cycles: 2 Encoding: L...1_o_o_o_-,--0_0_1_0--l addr15-addrS addr7-addrO Operation: LCALL (PC) ~ (PC) + 3 (SP) ~ (SP) + 1 «SP» ~ (PC7-O) (SP) ~ (SP) + 1 «SP» +- (PC15-S) (PC) ~ addr15-O LJMP addr16 Function: Long Jump Description: UMP causes an unconditional branch to the indicated address, by loading the high-order and low-order bytes of the PC (respectively) with the second and third instruction bytes. The destination may therefore be anywhere in the full 64K program memory address space. No flags are affected. Example: The label "JMPADR" is assigned to the instruction at program memory location 1234H. The instruction, UMP JMPADR at location 0123H will load the program counter with 1234H. Bytes: 3 Cycles: 2 Encoding: 1 ° 0 0 0 1 0 0 1 0 addr15-addrS addr7-addrO Operation: UMP (PC) +- addf15-O 2-51 MCS®·51 PROGRAMMER'S GUIDE AND INSTRUCTION SET MOV < dest-byte > , Function: Move byte variable Description: The byte variable indicated by the second operand is copied into the location specified by the first operand. The source byte is not affected. No other register or flag is affected. This is by far the most flexible operation. Fifteen combinations of source and destination addressing modes are allowed. Example: Internal RAM location 30H holds 4OH. The value of RAM location 40H is 10H. The data present at input port 1 is llOOl010B (OCAH). MOV RO,#30H ;RO <= 30H MOV A,@RO ;A <= 40H MOV Rl,A ;RI <= 40H MOV B,@RI ;B <= lOH MOV @RI,PI ;RAM (40H) < = OCAH MOV P2,PI ;P2 #OCAH leaves the value 30H in register 0, 40H in both the Accumulator and register I, lOH in register B, and OCAH (1 100 10 lOB) both in RAM location 40H and output on port 2. MOV A,Rn Bytes: Cycles: Encoding: I1110 1 r r r Operation: MOV (A) ~ (Rn) 'MOV A,direct Bytes: 2 Cycles: Encoding: I11 10 o1 0 1 direct address Operation: MOV (A) ~ (direct) MOV A,ACC is not a valid instruction. 2-52 inteL MCS®-51 PROGRAMMER'S GUIDE AND INSTRUCTION SET MOV A,@Ri Bytes: Cycles: Encoding: I1 1 1 0 o1 Operation: MOY (A) ~ «Ri» MOV A, # data Bytes: 2 Cycles: Encoding: 1 01 1 1 o1 o0 immediate data Operation: MOY (A) ~ #data MOV Rn,A Bytes: Cycles: Encoding: I1 1 1 1 r r r I Operation: MOY (Rn)~(A) MOV Rn,direct Bytes: 2 Cycles: 2 Encoding: I 1 010 1 r r r direct addr. Operation: MOY (Rn) ~ (direct) MOV Rn, # data Bytes: 2 Cycles: Encoding: 01 1 1 1 r r r immediate data 1 Operation: MOY (Rn) ~ #data 2-53 intelQD MCS®·51 PROGRAMMER'S GUIDE AND INSTRUCTION SET MOV direct,A Bytes: 2 Cycles: Encoding: I1 1 1 o1 0 1 direct address Operation: MOV (direct) ~ (A) MOV direct,Rn Bytes: 2 Cycles: 2 Encoding: I 1 000 1 r r r direct address Operation: MOV (direct) ~ (Rn) MOV direct,direct Bytes: 3 Cycles: 2 Encoding: I 1 000 o1 0 1 dir. addr. (src) dir. addr. (dest) Operation: MOV (direct) ~ (direct) MOV direct,@Ri Bytes: 2 Cycles: 2 Encoding: I 1 000 o1 direct addr. Operation: MOV (direct) ~ «Ri» MOV direct, # data Bytes: 3 Cycles: 2 Encoding: I0 1 o1 0 1 direct address immediate data I Operation: MOV (direct) ~ #data 2-54 intel~ MCS®-51 PROGRAMMER'S GUIDE AND INSTRUCTION SET MOV @Ri,A Bytes: Cycles: Encoding: I111 011 Operation: MOV «Ri» +- (A) MOV @Ri,direct Bytes: 2 Cycles: 2 Encoding: I 1 o1 0 011 direct addr. Operation: MOV «Ri» +- (direct) MOV @Ri,#data Bytes: 2 Cycles: Encoding: 101 011 immediate data Operation: MOV «RI» +- #data MOV < dest-bit > , < src-bit > Function: Move bit data Description: The Boolean variable indicated by the second operand is copied into the location specified by the first operand. One of the operands must be the carry flag; the other may be any directly addressable bit. No other register or flag is affected. Example: The carry flag is originally set. The data present at input Port 3 is 1100010 lB. The data previously written to output Port 1 is 35H (OOl10101B). MOV P1.3,C MOV C,P3.3 MOV P1.2,C will leave the carry cleared and change Port 1 to 39H (001 1lOOlB). 2-55 MCS®-51 PROGRAMMER'S GUIDE AND INSTRUCTION SET MOV C,bit Bytes: 2 Cycles: Encoding: I 1 010 001 0 bit address Operation: MOV (C) ~ (bit) MOV bit,C Bytes: 2 Cycles: 2 Encoding: I1 o0 1 001 0 bit address Operation: MOV (bit) ~ (C) MOV DPTR,#data16 Function: Load Data Pointer with a 16-bit constant Description: The Data Pointer is loaded with the 16-bit constant indicated. The 16-bit constant is loaded into the second and third bytes of the instruction. The second byte (DPH) is the high-order byte, while the third byte (DPL) holds the low-order byte. No flags are affected. This is the only instruction which moves 16 bits of data at once. Example: The instruction, MOV DPTR, # 1234H will load the value 1234H into the Data Pointer: DPH will hold 12H and DPL will hold 34H. Bytes: 3 Cycles: 2 Encoding: I10 0 1 0 0 0 0 immed. data 15-8 I. immed. data7-0 Operation: MOV (DPTR) ~ # datal 5-0 DPH 0 DPL ~ #datal5-8 0 #data7-O 2-56 MCS®-S1 PROGRAMMER'S GUIDE AND INSTRUCTION SET MOVC A,@A+ Function: Move Code byte Description: The MOVC instructions load the Accumulator with a code byte, or constant from program memory. The address of the byte fetched is the sum of the original unsigned eight-bit Accumu- lator contents and the contents of a sixteen-bit base register, which may be either the Data Pointer or the PC. In the latter case, the PC is incremented to the address of the following instruction before being added with the Accumulator; otherwise the base register is not al- tered. Sixteen-bit addition is performed so a carry-out from the low-order eight bits may propagate through higher-order bits. No flags are affected. Example: A value between 0 and 3 is in the Accumulator. The following instructions will translate the value in the Accumulator to one of four values defined by the DB (define byte) directive. REL_PC: INC A MOVC A,@A+PC RET DB 66H DB 77H DB 88H DB 99H If the subroutine is called with the Accumulator equal to OlH, it will return with 77H in the Accumulator. The INC A before the MOVC instruction is needed to "get around" the RET instruction above the table. If several bytes of code separated the MOVC from the table, the corresponding number would be added to the Accumulator instead. MOVC A,@A+DPTR Bytes: Cycles: 2 Encoding: I10 0 1 0 0 1 1 Operation: MOVC (A) ~ «A) + (DPTR» MOVC A,@A + PC Bytes: Cycles: 2 Encoding: I1000 0 0 1 1 Operation: MOVC (PC) ~ (PC) + 1 (A) ~ «A) + (PC» 2-57 int"eL MCS®-S1 PROGRAMMER'S GUIDE AND INSTRUCTION SET MOVX < dest-byte > , Function: Move External Description: The MOVX instructions transfer data between the Accumulator and a byte of external data memory, hence the "X" appended to MOV. There are two types of instructions, differing in whether they provide an eight-bit or sixteen-bit indirect address to the external data RAM. In the first type, the contents of RO or Rl in the current register bank provide an eight-bit address multiplexed with data on po. Eight bits are sufficient for external 1/0 expansion decoding or for a relatively small RAM array. For somewhat larger arrays, any output port pins can be used to output higher-order address bits. These pins would be controlled by an output instruction preceding the MOVX. In the second type of MOVX instruction, the Data Pointer generates a sixteen-bit address. P2 outputs the high-order eight address bits (the contents of DPH) while po multiplexes the low- order eight bits (DPL) with data. The P2 Special Function Register retains its previous con- tents while the P2 output buffers are emitting the contents of DPH. This form is faster and more efficient when accessing very large data arrays (up to 64K bytes), since no additional instructions are needed to set up the output ports. It is possible in some situations to mix the two MOVX types. A large RAM array with its high-order address lines driven by P2 can be addressed via the Data Pointer, or with code to output high-order address bits to P2 followed by a MOVX instruction using RO or Rl. Example: An external 256 byte RAM using multiplexed addressldata lines (e.g., an Intel 8155 RAMI I/OlTimer) is connected to the 8051 Port O. Port 3 provides control lines for the external RAM. Ports 1 and 2 are used for nonnal I/O. Registers 0 and 1 contain 12H and 34H. Location 34H of the external RAM holds the value 56H. The instruction sequence, MOVX A,@Rl MOVX @RO,A copies the value 56H into both the Accumulator and external RAM location 12H. 2-58 intelqp MCS®-51 PROGRAMMER'S GUIDE AND INSTRUCTION SET MOVX A,@Ri Bytes: Cycles: 2 Encoding: I 1 1 10 0 0 1 i Operation: MOVX (A) ~ «Ri» MOVX A,@DPTR Bytes: Cycles: 2 Encoding: I 1 1 1 0 I0 0 0 0 I Operation: MOVX (A) ~ «DPTR» MOVX @Ri,A Bytes: Cycles: 2 Encoding: I 1 1 1 1 0 0 1 i Operation: MOVX «Ri» ~ (A) MOVX @DPTR,A Bytes: Cycles: 2 Encoding: I1 1 1 1 I0 0 0 0 I Operation: MOVX (DPTR)~ (A) 2-59 intel~ MCS®-S1 PROGRAMMER'S GUIDE AND INSTRUCTION SET MUL AB Function: Multiply Description: MUL AB mUltiplies the unsigned eight-bit integers in the Accumulator and register B. The low-order byte of the sixteen-bit product is left in the Accumulator, and the high-order byte in B. If the product is greater than 255 (OFFH) the overflow flag is set; otherwise it is cleared. The carry flag is always cleared. Example: Originally the Accumulator holds the value 80 (50H). Register B holds the value 160 (OAOH). The instruction, MUL AB will give the product 12,800 (3200H), so B is changed to 32H (OOII00lOB) and the Accumula- tor is cleared. The overflow flag is set, carry is cleared. Bytes: Cycles: 4 Encoding: I10 10 0 1 0 0 Operation: MUL (Ah-O +- (A) X (B) (BhS-8 NOP Function: No Operation Description: Execution continues at the following instruction. Other than the PC, no registers or flags are affected. Example: It is desired to produce a low-going output pulse on bit 7 of Port 2 lasting exactly 5 cycles. A simple SETB/CLR sequence would generate a one-cycle pulse, so four additional cycles must be inserted. This may be done (assuming no interrupts are enabled) with the instruction sequence, CLR P2.7 NOP NOP NOP NOP SETB P2.7 Bytes: Cycles: Encoding: I0 0 0 0 I 0 0 0 0 Operation: NOP (PC)~(PC) + 2-60 inteL MCS®-S1 PROGRAMMER'S GUIDE AND INSTRUCTION SET ORL < dest-byte > Function: Logical-OR for byte variables Description: ORL perfonns the bitwise logical-OR operation between the indicated variables, storing the results in the destination byte. No flags are affected. The two operands allow six addressing mode combinations. When the destination is the Accu- mulator, the source can use register, direct, register-indirect, or immediate addressing; when the destination is a direct address, the source can be the Accumulator or immediate data. Note: When this instruction is used to modify an output port, the value used as the original port data will be read from the output data latch, not the input pins. Example: If the Accumulator holds OC3H (110000IIB) and RO holds 55H (OlOIOlOlB) then the in- struction, ORL A,RO will leave the Accumulator holding the value OD7H (llOIOIIIB). When the destination is a directly addressed byte, the instruction can set combinations of bits in any RAM location or hardware register. The pattern of bits to be set is detennined by a mask byte, which may be either a constant data value in the instruction or a variable computed in the Accumulator at run-time. The instruction, ORL PI, #001 100 lOB will set bits 5, 4, and I of output Port 1. ORL A,Rn Bytes: Cycles: Encoding: I0 0 0 1 r r r Operation: ORL (A) +- (A) V (Rn) 2-61 intel~ MCS®-S1 PROGRAMMER'S GUIDE AND INSTRUCTION SET ORL A,direct Bytes: 2 Cycles: Encoding: 01 o 0 o10 1 direct address 1 Operation: ORL (A) ~ (A) V (direct) ORL A,@Ri Bytes: Cycles: Encoding: 1 0100 I o11i Operation: ORL (A) ~ (A) V «Ri» ORL A, # data Bytes: 2 Cycles: Encoding: 1 0100 I o10 0 immediate data Operation: ORL (A) ~ (A) V # data ORL direct,A Bytes: 2 Cycles: Encoding: 0100 001 0 direct address 1 I Operation: ORL (direct) ~ (direct) V (A) ORL direct, # data Bytes: 3 Cycles: 2 Encoding: 1 01 o 0 o0 1 1 direct addr. immediate data Operation: ORL (direct) ~ (direct) V # data 2-62 intel~ MCS®-S1 PROGRAMMER'S GUIDE AND INSTRUCTION SET ORL C, Function: Logical-OR for bit variables Description: Set the carry flag if the Boolean value is a logical 1; leave the carry in its current state otherwise. A slash ("/") preceding the operand in the assembly language indicates that the logical complement of the addressed bit is used as the source value, but the source bit itself is not affected. No other flags are affected. Example: Set the carry flag if and only if P1.0 = 1, ACC. 7 = 1, or OV = 0: MOV C,P1.0 ;LOAD CARRY WITH INPUT PIN P10 ORL C,ACC.7 ;OR CARRY WITH THE ACC. BIT 7 ORL C,/OV ;OR CARRY WITH THE INVERSE OF OV. ORL C,bit Bytes: 2 Cycles: 2 Encoding: 1 01 1 1 001 0 bit address Operation: ORL (C) ~ (C) V (bit) ORL C,/bit Bytes: 2 Cycles: 2 Encoding: I1 o1 0 o0 0 0 bit address Operation: ORL (C) ~ (C) V (bit) 2-63 inteL MCS®-S1 PROGRAMMER'S GUIDE AND INSTRUCTION SET POP direct Function: Pop from stack. Description: The contents of the internal RAM location addressed by the Stack Pointer is read, and the Stack Pointer is decremented by one. The value read is then transferred to the directly ad- dressed byte indicated. No flags are affected. Example: The Stack Pointer originally contains the value 32H, and internal RAM locations 30H through 32H contain the values 20H, 23H, and OlH, respectively. The instruction sequence, POP DPH POP DPL will leave the Stack Pointer equal to the value 30H and the Data Pointer set to 0123H. At this point the instruction, POP SP will leave the Stack Pointer set to 20H. Note that in this special case the Stack Pointer was decremented to 2FH before being loaded with the value popped (20H). Bytes: 2 Cycles: 2 Encoding: I 1 0 1 0 0 0 0 direct address Operation: POP (direct) +- «SP» (SP) +- (SP) - 1 PUSH direct Function: Push onto stack Description: The Stack Pointer is incremented by one. The contents of the indicated variable is then copied into the internal RAM location addressed by the Stack Pointer. Otherwise no flags are affect- ed. Example: On entering an interrupt routine the Stack Pointer contains 09H. The Data Pointer holds the value 0123H. The instruction sequence, PUSH DPL PUSH DPH will leave the Stack Pointer set to OBH and store 23H and OlH in internal RAM locations OAH and OBH, respectively. Bytes: 2 Cycles: 2 Encoding: I1100 0 0 0 0 direct address Operation: PUSH (SP) +- (SP) + 1 «SP» +- (direct) 2-64 intel~ MCS®-51 PROGRAMMER'S GUIDE AND INSTRUCTION SET RET Function: Return from subroutine Description: RET pops the high- and low-order bytes of the PC successively from the stack, decrementing the Stack Pointer by two. Program execution continues at the resulting address, generally the instruction immediately following an ACALL or LCALL. No flags are affected. Example: The Stack Pointer originally contains the value OBH. Internal RAM locations OAH and OBH contain the values 23H and OlH, respectively. The instruction, RET will leave the Stack Pointer equal to the value 09H. Program execution will continue at location 0123H. Bytes: Cycles: 2 Encoding: I °° 1 ° °° 1 ° Operation: RET (PCIS-S) ~ «SP» (SP) ~ (SP) - 1 (PC7-O) ~ «SP» (SP) ~ (SP) - 1 RETI Function: Return from interrupt Description: RETI pops the high- and low-order bytes of the PC successively from the stack, and restores the interrupt logic to accept additional interrupts at the same priority level as the one just processed. The Stack Pointer is left decremented by two. No other registers are affected; the PSW is not automatically restored to its pre-interrupt status. Program execution continues at the resulting address, which is generally the instruction immediately after the point at which the interrupt request was detected. If a lower- or same-level interrupt had been pending when the RETI instruction is executed, that one instruction will be executed before the pending interrupt is processed. Example: The Stack Pointer originally contains the value OBH. An interrupt was detected during the instruction ending at location 0122H. Internal RAM locations OAH and OBH contain the values 23H and OlH, respectively. The instruction, RETI will leave the Stack Pointer equal to 09H and return program execution to location 0123H. Bytes: Cycles: 2 Encoding: 1-1 _0_0_ _-,---0_0_1_0...... Operation: RETI (PCIS-S) ~ «SP» (SP) ~ (SP) - 1 (PC7-O) ~ «SP» (SP) ~ (SP) - 1 2-65 inteL MCS®-S1 PROGRAMMER'S GUIDE AND INSTRUCTION SET RL A Function: Rotate Accumulator Left Description: The eight bits in the Accumulator are rotated one bit to the left. Bit 7 is rotated into the bit 0 position. No flags are affected. Example: The Accumulator holds the value OCSH (lIOOOlOlB). The instruction, RLA leaves the Accumulator holding the value 8BH (lOOOlOllB) with the carry unaffected. Bytes: Cycles: Encoding: I0 0 1 0 0 0 1 1 Operation: RL (An + I) ~ (An) n = 0 - 6 (AO)~(A7) RLC A Function: Rotate Accumulator Left through the Carry flag Description: The eight bits in the Accumulator and the carry flag are together rotated one bit to the left. Bit 7 moves into the carry flag; the original state of the carry flag moves into the bit 0 position. No other flags are affected. Example: The Accumulator holds the value OCSH (11000 101 B), and the carry is zero. The instruction, RLC A leaves the Accumulator holding the value 8BH (lOOOlOlOB) with the carry set. Bytes: Cycles: Encoding: _0_0_1_....1.-0_ 0_ 1_ 1--1 L...l Operation: RLC (An + 1) ~ (An) n = 0 - 6 (AO)~(C) (C) ~ (A7) 2-66 MCS®-S1 PROGRAMMER'S GUIDE AND INSTRUCTION SET RR A Function: Rotate Accumulator Right Description: The eight bits in the Accumulator are rotated one bit to the right. Bit 0 is rotated into the bit 7 position. No flags are affected. Example: The Accumulator holds the value OC5H (11000101B). The instruction, RR A leaves the Accumulator holding the value OE2H (111OOO10B) with the carry unaffected. Bytes: Cycles: Encoding: I0 0 0 0 I 0 0 1 1 Operation: RR (An) +- (An + 1) n = 0 - 6 (A7) +- (AO) RRC A Function: Rotate Accumulator Right through Carry flag Description: The eight bits in the Accumulator and the carry flag are together rotated one bit to the right. Bit 0 moves into the carry flag; the original value of the carry flag moves into the bit 7 position. No other flags are affected. Example: The Accumulator holds the value OC5H (11000101B), the carry is zero. The instruction, RRC A leaves the Accumulator holding the value 62 (Ol1ooolOB) with the carry set. Bytes: Cycles: Encoding: I0 0 0 1 0 0 1 1 Operation: RRC (An) +- (An + 1) n = 0 - 6 (A7) +- (C) (C) +- (AO) 2-67 intelGP MCS®-S1 PROGRAMMER'S GUIDE AND INSTRUCTION SET SETB Function: Set Bit Description: SETB sets the indicated bit to one. SETB can operate on· the carry flag or any directly addressable bit. No other flags are affected. Example: The carry flag is cleared. Output Port 1 has been written with the value 34H (OOl10100B). The instructions, SETB C SETB Pl.O will leave the carry flag set to 1 and change the data output on Port 1 to 35H (OOl10101B). SETB C Bytes: Cycles: Encoding: I1 1 0 1 o0 1 1 Operation: SETB (C)~ 1 SETB bit Bytes: 2 Cycles: Encoding: I1 1 0 1 001 0 bit address Operation: SETB (bit) ~ 1 2-68 intel~ MCS®-S1 PROGRAMMER'S GUIDE AND INSTRUCTION SET SJMP rei Function: Short Jump Description: Program control branches unconditionally to the address indicated. The branch destination is computed by adding the signed displacement in the second instruction byte to the PC, after incrementing the PC twice. Therefore, the range of destinations allowed is from 128 bytes preceding this instruction to 127 bytes following it. Example: The label "RELADR" is assigned to an instruction at program memory location 0123H. The instruction, SJMP RELADR will assemble into location 0100H. After the instruction is executed, the PC will contain the value 0123H. (Note: Under the above conditions the instruction following SJMP will be at l02H. Therefore, the displacement byte of the instruction will be the relative offset (0123H-0102H) = 21H. Put another way, an SJMP with a displacement ofOFEH would be a one-instruction infinite loop.) Bytes: 2 Cycles: 2 Encoding: I1 0 0 0 0 0 0 0 reI. address Operation: SJMP (PC) ~ (PC) + 2 (PC) ~ (PC) + reI 2-69 MCS®-S1 PROGRAMMER'S GUIDE AND INSTRUCTION SET SUBB A, < src-byte > Function: Subtract with borrow Description: SUBB subtracts the indicated variable and the carry flag together from the Accumulator, leaving the result in the Accumulator. SUBB sets the carry (borrow) flag if a borrow is needed for bit 7, and clears C otherwise. (If C was set before executing a SUBB instruction, this indicates that a borrow was needed for the previous step in a multiple precision subtraction, so the carry is subtracted from the Accumulator along with the source operand.) AC is set if a borrow is needed for bit 3, and cleared otherwise. OV is set if a borrow is needed into bit 6, but not into bit 7, or into bit 7, but not bit 6. When subtracting signed integers OV indicates a negative number produced when a negative value is subtracted from a positive value, or a positive result when a positive number is subtracted from a negative number. The source operand allows four addressing modes: register, direct, register-indirect, or imme- diate. Example: The Accumulator holds OC9H (llOOlOOlB), register 2 holds 54H (0 10 10 1OOB), and the carry flag is set. The instruction, SUBB A,R2 will leave the value 74H (01110100B) in the accumulator, with the carry flag and AC cleared but OV set. Notice that OC9H minus 54H is 75H. The difference between this and the above result is due to the carry (borrow) flag being set before the operation. If the state of the carry is not known before starting a single or multiple-precision subtraction, it should be explicitly cleared by a CLR C instruction. SUBB A,Rn Bytes: Cycles: Encoding: ....._1_0_0_1---'-_1_r_r_r.... 1 Operation: SUBB (A) ~ (A) - (C) - (Rn) 2-70 inteL MCS®-S1 PROGRAMMER'S GUIDE AND INSTRUCTION SET SUBB A,direct Bytes: 2 Cycles: Encoding: I10 0 1 0 1 0 1 direct address Operation: SUBB (A) ...- (A) - (C) - (direct) SUBB A,@Ri Bytes: Cycles: Encoding: I1 0 0 1 0 1 1 Operation: SUBB (A) ...- (A) - (C) - «Ri» SUBB A,#data Bytes: 2 Cycles: Encoding: I10 0 1 0 1 0 0 immediate data Operation: SUBB (A)"'- (A) - (C) - #data SWAP A Function: Swap nibbles within the Accumulator Description: SWAP A interchanges the low- and high-order nibbles (four-bit fields) of the Accumulator (bits 3-0 and bits 7-4). The operation can also be thOUght of as a four-bit rotate instruction. No flags are affected. Example: The Accumulator holds the value OC5H (11000lOlB). The instruction, SWAP A leaves the Accumulator holding the value 5CH (OlOlll00B). Bytes: Cycles: Encoding: I1100 0 1 0 0 Operation: SWAP (A3-O) :: (A7-4) 2-71 infelQP MCS®·51 PROGRAMMER'S GUIDE AND INSTRUCTION SET XCH A, < byte> Function: Exchange Accumulator with byte variable Description: XCH loads the Accumulator with the contents of the indicated variable, at the same time writing the original Accumulator contents to the indicated variable. The source/destination operand can use register, direct, or register-indirect addressing. Example: RO contains the address 20H. The Accumulator holds the value 3FH (00111111B). Internal RAM location 20H holds the value 7SH (01I10101B). The instruction, XCH A,@RO will leave RAM location 20H holding the values 3FH (00111111B) and 75H (011101OlB) in the accumulator. XCH A,Rn Bytes: Cycles: Encoding: 11 100 1r r r Operation: XCH (A) :: (Rn) XCH A,direct Bytes: 2 Cycles: Encoding: 1 1 100 o1 0 1 direct address Operation: XCH (A) :: (direct) XCH A,@Ri Bytes: Cycles: Encoding: 1 1 100 011 Operation: XCH (A) :: «Ri» 2-72 MCS®-51 PROGRAMMER'S GUIDE AND INSTRUCTION SET XCHD A,@Ri Function: Exchange Digit Description: XCHD exchanges the low-order nibble of the Accumulator (bits 3-0), generally representing a hexadecimal or BCD digit, with that of the internal RAM location indirectly addressed by the specified register. The high-order nibbles (bits 7-4) of each register are not affected. No flags are affected. Example: RO contains the address 20H. The Accumulator holds the value 36H (OOl1011OB). Internal RAM location 20H holds the value 75H (011 10 10 lB). The instruction, XCHD A,@RO will leave RAM location 20H holding the value 76H (011 101 lOB) and 35H (OOlI0101B) in the Accumulator. Bytes: Cycles: Encoding: L-1_1_1_0_1-L-_0_1_1----J Operation: XCHD (A3-O) :: «Ri3-0)) XRL < dest-byte > , < src-byte > Function: Logical Exclusive-OR for byte variables Description: XRL performs the bitwise logical Exclusive-OR operation between the indicated variables, storing the results in the destination. No flags are affected. The two operands allow six addressing mode combinations. When the destination is the Accu- mulator, the source can use register, direct, register-indirect, or immediate addressing; when the destination is a direct address, the source can be the Accumulator or immediate data. (Note: When this instruction is used to modify an output port, the value used as the original port data will be read from the output data latch, not the input pins.) Example: If the Accumulator holds OC3H (1 100001 IB) and register 0 holds OAAH (1010101OB) then the instruction, XRL A,RO will leave the Accumulator holding the value 69H (OIIOIOOlB). When the destination is a directly addressed byte, this instruction can complement combina- tions of bits in any RAM location or hardware register. The pattern of bits to be complement- ed is then determined by a mask byte, either a constant contained in the instruction or a variable computed in the Accumulator at run-time. The instruction, XRL PI, #OOll000lB will complement bits 5, 4, and 0 of output Port 1. 2-73 inteL MCS®-S1 PROGRAMMER'S GUIDE AND INSTRUCTION SET XRL A,Rn Bytes: Cycles: Encoding: 01 1 0 1 r r r 1 Operation: XRL (A) +- (A) ¥ (Rn) XRL A,direct Bytes: 2 Cycles: Encoding: 1 01 1 0 o1 0 1 direct address Operation: XRL (A) +- (A) ¥ (direct) XRL A,@Ri Bytes: Cycles: Encoding: 1 01 1 0 o1 1 i Operation: XRL (A) +- (A) ¥ «Ri» XRL A, # data Bytes: 2 Cycles: Encoding: I0 110 o1 0 0 immediate data Operation: XRL (A) +- (A) ¥ #data XRL direct,A Bytes: 2 Cycles: Encoding: 1 01 1 0 001 0 direct address I Operation: XRL (direct) +- (direct) ¥ (A) 2-74 intet MCS®-51 PROGRAMMER'S GUIDE AND INSTRUCTION SET XRL direct,#data Bytes: 3 Cycles: 2 Encoding: I0 1 1 0 0 0 1 1 direct address immediate data Operation: XRL (direct) ~ (direct) ¥ # data 2-75 8051, 8052 and 80C51 3 Hardware Description 8051, 8052 and 80C51 Hardware Description CONTENTS PAGE CONTENTS PAGE INTRODUCTION ........................................ 3-3 INTERRUPTS ........................................... 3-23 Special Function Registers ......................... 3-3 Priority Level Structure ............................. 3-24 How Interrupts Are Handled ..................... 3-24 PORT STRUCTURES AND OPERATION ........................................... 3-6 External Interrupts .................................... 3-25 1/0 Configurations ....................................... 3-7 Response Time ......................................... 3-25 Writing to a Port .......................................... 3-7 SINGLE-STEP OPERATION .................... 3-26 Port Loading and Interfacing ...................... 3-8 Read-Modify-Write Feature ........................ 3-9 RESET ...................................................... 3-26 ACCESSING EXTERNAL MEMORy ......... 3-9 POWER-ON RESET ...... ........................... 3-27 TIMER/COUNTERS ................................... 3-9 POWER-SAVING MODES OF OPERATION ......................................... 3-27 Timer 0 and Timer 1.................................. 3-10 CHMOS Power Reduction Modes ............ 3-27 Timer 2 ...................................................... 3-12 EPROM VERSIONS ................................. 3-29 SERIAL INTERFACE ............................... 3-13 Exposure to Light.. .................................... 3-29 Multiprocessor Communications .............. 3-14 Program Memory Locks ........................... 3-29 Serial Port Control Register ...................... 3-14 ONCE Mode ............................................. 3-30 Baud Rates ............................................... 3-15 More About Mode 0 .................................. 3-17 THE ON-CHIP OSCILLATORS ................ 3-30 More About Mode 1 .................................. 3-17 HMOS Versions ........................................ 3-30 More About Modes 2 and 3 ...................... 3-20 CHMOS Versions ..................................... 3-32 INTERNAL TIMING ....... ........................... 3-33 3-1 int:et 8051, 8052 AND 80C51 HARDWARE DESCRIPTION INTRODUCTION • The EPROM versions of the 8051AH, 8052AH and 8OC51BH This chapter presents a comprehensive description of the on-chip hardware features of the MCS@-51 micro- The devices under consideration are listed in Table 1. controllers. Included in this description are As it becomes unwieldy to be constantly referring to • The port drivers and how they function both as each of these devices by their individual names, we will ports and, for Ports 0 and 2, in bus operations adopt a convention of referring to them generically as 8051s and 8052s, unless a specific member of the group • The Timer/Counters is being referred to, in which case it will be specifically • The Serial Interface named. The "8051s" include the 805tAH, 8OC51BH, and their ROMless and EPROM versions. The "8052s" • The Interrupt System are the 8052AH, 8032AH and 8752BH. • Reset • The Reduced Power Modes in the CHMOS devices Figure 1 shows a functional block diagram of the 8051 s and 8052s. Table 1. The MC8-S1 Family of Microcontrollers Device ROMless EPROM ROM RAM 1S-bit Ckt Name Version Version Bytes Bytes Timers Type 8051AH 8031AH 8751H,8751BH 4K 128 2 HMOS 8052AH 8032AH 8752BH 8K 256 3 HMOS 80C51BH 80C31BH 87C51 4K 128 2 CHMOS Special Function Registers A map of the on-chip memory area called SFR (Special Function Register) space is shown in Figure 2. SFRs marked by parentheses are resident in the 8052s but not in the 805ts. 3-3 int'eL HARDWARE DESCRIPTION OF THE 8051, 8052 AND 80C51 PO.O-PO.7 P2.0-P2.7 r- - - - - --- ~!:H.:~' tfjtH:~-- - - - --- -- --, ~ ~ -=- I I I 1 I I I I 1 I Ir--~-. I~=.;.;.J I 1 I 1 1,--_-,--. PSEN ALE EA RST I I I I 1 I !r-H':R"i=R- - - - - __ .J "Resident in 805218032 only. P1.0-P1.7 P3.0-P3.7 270252-1 Figure 1. MCS-S1 Architectural Block Diagram 3-4 inteL . HARDWARE DESCRIPTION OF THE 8051, 8052 AND 80C51 8 Bytes F8 FF FO B F7 E8 EF EO ACC E7 08 OF DO PSW 07 C8 (T2CON) (RCAP2L) (RCAP2H) (TL2) (TH2) CF CO C7 B8 IP BF BO P3 B7 A8 IE AF AO P2 A7 98 SCON SBUF 9F 90 P1 97 88 TCON TMOO TLO TL1 THO TH1 8F 80 PO SP OPL OPH PCON 87 Figure 2. SFR Map. ( ••• ) Indicates Resident in 80525, not in 80515 Note that not all of the addresses are occupied. Unoc- to hold a 16-bit address. It may be manipulated as a cupied addresses are not implemented on the chip. 16-bit register or as two independent 8-bit registers. Read accesses to these addresses will in general return random data, and write accesses will have no effect. PORTS 0 TO 3 User software should not write Is to these unimple- PO, PI, P2 and P3 are the SFR latches of Ports 0, 1,2 mented locations, since they may be used in future and 3, respectively. MCS-51 products to invoke new features. In that case the reset or inactive values of the new bits will always SERIAL DATA BUFFER be 0, and their active values will be 1. The Serial Data Buffer is actually two separate regis- The functions of the SFRs are outlined below. ters, a transmit buffer and a receive buffer register. When data is moved to SBUF, it goes to the transmit ACCUMULATOR buffer where it is held for serial transmission. (Moving ACC is the Accumulator register. The mnemonics for a byte to SBUF is what initiates the transmission.) Accumulator-Specific instructions, however, refer to When data is moved from SBUF, it comes from the the Accumulator simply as A. receive buffer. B REGISTER TIMER REGISTERS The B register is used during multiply and divide oper- Register pairs (THO, TLO), (THl, TLl), and (TH2, ations. For other instructions it can be treated as anoth- TL2) are the 16-bit Counting registers for Timer/Coun- er scratch pad register. ters 0, 1, and 2, respectively. PROGRAM STATUS WORD CAPTURE REGISTERS The PSW register contains program status information The register pair (RCAP2H, RCAP2L) are the Cap- as detailed in Figure 3. ture registers for the Timer 2 "Capture Mode." In this mode, in response to a transition at the 8052's T2EX STACK POINTER pin, TH2 and TL2 are copied into RCAP2H and The Stack Pointer Register is 8 bits wide. It is incre- RCAP2L. Timer 2 also has a 16-bit auto-reload mode, mented before data is stored during PUSH and CALL and RCAP2H and RCAP2L hold the reload value for executions. While the stack may reside anywhere in on- this mode. More about Timer 2's features in a later chip RAM, the Stack Pointer is initialized to 07H after section. a reset. This causes the stack to begin at location 08H. CONTROL REGISTERS DATA POINTER Special Function Registers IP, IE, TMOD, TCON, The Data Pointer (DPTR) consists of a high byte neON, SCON, and PCON contain control and status (DPH) and a low byte (DPL). Its intended function is bits for the interrupt system, the Timer/Counters, and the serial port. They are described in later sections. 3-5 inteL HARDWARE DESCRIPTION OF THE 8051, 8052 AND 80C51 (MSB) (LSB) CY I AC I FO I RS1 RSO I ov I - P Symbol Position Name and Significance Symbol Position Name and Significance CY PSW.7 Carry flag. OV PSW.2 Overflow flag. AC PSW.6 Auxiliary Carry flag. PSW.1 User definable flag. (For BCD operations.) P psw.o Parity flag. FO PSW.5 Flag 0 Set! cleared by hardware each (Available to the user for general instruction cycle to indicate an odd/ purposes.) even number of "one" bits in the RS1 PSW.4 Register bank select control bits 1 & Accumulator, i.e., even parity. RSO PSW.3 O. Set!cleared by software to NOTE: determine working register bank (see The contents of (RS1, RSO) enable the working register banks as follows: Note). (O.Q)-Bank 0 (00H-07H) (0.1 )-Bank 1 (OSH-OFH) (1.0)-Bank 2 (10H-17H) (1.1 )-Bank 3 (1SH-1FH) Figure 3. PSW: Program Status Word Register WRITE TO WRITE LATCH TO LATCH 270252-3 270252-2 B. Port 1 Bit A. PortO Bit ALTERNATE OUTPUT FUNCTION AOOR vCC READ LATCH 'NT.BUS WRITE TO WRITE LATCH TO LATCH ALTERNATE INPUT FUNCTION 270252-4 270252-5 C. Port 2 Bit D. Port 3 Bit Figure 4. 8051 Port Bit Latches and 1/0 Buffers ·See Figure 5 for details of the internal pullup. PORT STRUCTURES AND external memory address, time-multiplexed with the byte being written or read. Port 2 outputs the high byte OPERATION of the external memory address when the address is 16 All four ports in the 8051 are bidirectional. Each con- bits wide. Otherwise the Port 2 pins continue to emit sists of a latch (Special Function Registers PO through the P2 SFR content. P3), an output driver, and an input buffer. All the Port 3 pins, and (in the 8052) two Port 1 pins The output drivers of Ports 0 and 2, and the input buff- are multifunctional. They are not only port pins, but ers of Port 0, are used in accesses to external memory. also serve the functions of various special features as In this application, Port 0 outputs the low byte of the listed on the following page. 3-6 intet HARDWARE DESCRIPTION OF THE 8051, 8052 AND 80C51 Port Pin Alternate Function ADDR/DATA BUS). To be used as an input, the port *P1.0 T2 (Timer/Counter 2 bit latch must contain a 1, which turns off the output external input) driver FET. Then, for Ports 1, 2, and 3, the pin is *P1.1 T2EX (Timer/Counter 2 pulled high by the internal pullup, but can be pulled low by an external source. Capture/Reload trigger) P3.0 RXD (serial input port) Port 0 differs in not having internal pullups. The pullup P3.1 TXD (serial output port) FET in the PO output driver (see Figure 4) is used only P3.2 INTO (external interrupt) when the Port is emitting Is during external memory P3.3 INT1 (external interrupt) accesses. Otherwise the pullup PET is off. Consequent- P3.4 TO (Timer/Counter 0 external ly PO lines that are being used as output port lines are input) open drain. Writing a 1 to the bit latch leaves both P3.S T1 (Timer/Counter 1 external output FETs off, so the pin floats. In that condition it input) can be used a high-impedance input. P3.6 WR (external Data Memory Because Ports 1, 2, and 3 have fixed internal pullups write strobe) they are sometimes called "quasi-bidirectional" ports. P3.7 RD (external Data Memory When configured as inputs they pull high and will read strobe) source current (IlL, in the data sheets) when externally pulled low. Port 0, on the other hand, is considered *P1.0 and Pl.l serve these alternate functions only on "true" bidirectional, because when configured as an in- the 8052. put it floats. The alternate functions can only be activated if the cor- All the port latches in the 8051 have Is written to them responding bit latch in the port SFR contains a 1. Oth- by the reset function. If a 0 is subsequently written to a erwise the port pin is stuck at o. port latch, it can be reconfigured as an input by writing a 1 to it. 1/0 Configurations Writing to a Port Figure 4 shows a functional diagram of a typical bit latch and I/O buffer in each of the four ports. The bit In the execution of an instruction that changes the val- latch (one bit in the port's SFR) is represented as a ue in a port latch, the new value arrives at the latch Type D flip-flop, which will clock in a value from the during S6P2 of the final cycle of the instruction. How- internal bus in response to a "write to latch" signal ever, port latches are in fact sampled by their output from the CPU. The Q output of the flip-flop is placed buffers only during Phase 1 of any clock period. (Dur- on the internal bus in response to a "read latch" signal ing Phase 2 the output buffer holds the value it saw from the CPU. The level of the port pin itself is placed during the previous Phase 1). Consequently, the new on the internal bus in response to a "read pin" signal value in the port latch won't actually appear at the from the CPU. Some instructions that read a port acti- output pin until the next Phase 1, which will be at SIPl vate the "read latch" signal, and others activate the of the next machine cycle. See Figure 39 in the Internal "read pin" signal. More about that later. Timing section. As shown in Figure 4, the output drivers of Ports 0 and If the change requires a O-to-l transition in Port 1,2, or 2 are switchable to an internal ADDR and ADDR/ 3, an additional pullup is turned on during SIPl and DATA bus by an internal CONTROL signal for use in S1P2 of the cycle in which the transition occurs. This is external memory accesses. During external memory ac- done to increase the transition speed. The extra pullup cesses, the P2 SFR remains unchanged, but the PO SFR can source about 100 times the current that the normal gets Is written to it. pullup can. It should be noted that the internal pullups are field-effect transistors, not linear resistors. The pull- Also shown in Figure 4, is that if a P3 bit latch contains up arrangements are shown in Figure 5. a 1, then the output level is controlled by the signal labeled "alternate output function." The actual P3.X In HMOS versions of the 8051, the fixed part of the pin level is always available to the pin's alternate input pullup is a depletion-mode transistor with the gate function, if any. wired to the source. This transistor will allow the pin to source about 0.25 mA when shorted to ground. In Ports 1, 2, and 3 have internal pullups. Port 0 has open parallel with the fixed pullup is an enhancement-mode drain outputs. Each I/O line can be independently used transistor, which is activated during SI whenever the as an input or an output. (ports 0 and 2 may not be port bit does a O-to-l transition. During this interval, if used as general purpose I/O when being used as the the port pin is shorted to ground, this extra transistor will allow the pin to source an additional 30 mAo 3-7 intel~ HARDWARE DESCRIPTION OF THE 8051, 8052 AND 80C51 VCC I 2 OSC. PERIODS ENHANCEMENT MODE FET aD ~ 270252-6 A. HMOS Configuration. The enhancement mode transistor is turned on for 2 osc. periods after Q makes a o-to-1 transition. VCC Vce Vce o FROM PORT LATCH INPUT o--~ I--~~ DATA READ PORT PIN 270252-7 B. CHMOS Configuration. pFET 1 is turned on for 2 osc. periods after Q makes a O-t0-1 transition. During this time, pFET 1 also turns on pFET 3 through the inverter to form a latch which holds the 1. pFET 2 is also on. Figure 5. Ports 1 And 3 HMOS And CHMOS Internal Pullup Configurations. Port 2 is Similar Except That It Holds The Strong Pullup On While Emitting 1s That Are Address Bits. (See Text, "Accessing External Memory".) In the CHMOS versions, the pullup consists of three Port Loading and Interfacing pFETs. It should be noted that an n-channel FET (nFET) is turned on when a logical 1 is applied to its The output buffers of Ports 1, 2, and 3 can each drive 4 gate, and is turned off when a logical 0 is applied to its LS TTL inputs. These ports on HMOS versions can be gate. A p-channel FET (PFET) is the opposite: it is on driven in a normal manner by any TTL or NMOS cir- when its gate sees a 0, and off when its gate sees a 1. cuit. Both HMOS and CHMOS pins can be driven by open-collector and open-drain outputs, but note that 0- pFET 1 in Figure 5 is the transistor that is turned on for to-l transitions will not be fast. In the HMOS device, if 2 oscillator periods after a O-to-l transition in the port the pin is driven by an open-collector output, a O-to-l latch. While it's on, it turns on pFET3 (a weak pull- transition will have to be driven by the relatively weak up), through the inverter. This inverter and pFET form depletion mode FET in Figure 5(A). In the CHMOS a latch which hold the 1. device, an input 0 turns off pullup pFET3, leaving only the very weak pullup pFET2 to drive the transition. Note that if the pin is emitting a 1, a negative glitch on the pin from some external source can turn off pFET3, In external bus mode, Port 0 output buffers can each causing the pin to go into a float state. pFET2 is a very drive 8 LS TTL inputs. As port pins, they require exter- weak pullup which is on whenever the nFET is off, in nal pullups to drive any inputs. traditional CMOS style. It's only about %0 the strength of pFET3. Its function is to restore a 1 to the pin in the event the pin had a 1 and lost it to a glitch. 3-8 infel~ HARDWARE DESCRIPTION OF THE 8051, 8052 AND 80C51 Read-Modify-Write Feature Whenever a 16-bit address is used, the high byte of the address comes out on Port 2, where it is held for the Some instructions that read a port read the latch and duration ofthe read or write cycle. Note that the Port 2 others read the pin. Which ones do which? The instruc- drivers use the strong pullups during the entire time tions that read the latch rather than the pin are the ones that they are emitting address bits that are Is. This is that read a value, possibly change it, and then rewrite it during the execution of a MOVX @DPTR instruction. to the latch. These are called "read-modify-write" in- During this time the Port 2 latch (the Special Function structions. The instructions listed below are read-mod- Register) does not have to contain Is, and the contents ify-write instructions. When the destination operand is of the Port 2 SFR are not modified. If the external a port, or a port bit, these instructions read the latch memory cycle is not immediately followed by another rather than the pin: external memory cycle, the undisturbed contents of the Port 2 SFR will reappear in the next cycle. ANL (logical AND, e.g., ANL PI, A) ORL (logical OR, e.g., ORL P2, A) If an 8-bit address is being used (MOVX @Ri), the (logical EX-OR, e.g., XRL P3, A) contents of the Port 2 SFR remain at the Port 2 pins XRL throughout the external memory cycle. This will facili- JBC Gump if bit = 1 and clear bit, e.g., tate paging. mc Pl.1, LABEL) In any case, the low byte of the address is time-multi- CPL (complement bit, e.g., CPL P3.0) plexed with the data byte on Port o. The ADDRI INC (increment, e.g., INC P2) DATA signal drives both FETs in the Port 0 output DEC (decrement, e.g., DEC P2) buffers. Thus, in this application the Port 0 pins are not (decrement and jump if not zero, e.g., open-drain outputs, and do not require external pull- DJNZ ups. Signal ALE (Address Latch Enable) should be DJNZ P3, LABEL) used to capture the address byte into an external latch. MOV, PX.Y, C (move carry bit to bit Y of Port X) The address byte is valid at the negative transition of CLR PX.Y (clear bit Y of Port X) ALE. Then, in a write cycle, the data byte to be written appears on Port 0 just before WR is activated, and re- SETB PX. Y (set bit Y of Port X) mains there until after WR is deactivated. In a read cycle, the incoming byte is accepted at Port 0 just be- It is not obvious that the last three instructions in this list are read-modify-write instructions, but they are. fore the read strobe is deactivated. They read the port byte, all 8 bits, modify the addressed During any access to external memory, the CPU writes bit, then write the new byte back to the latch. OFFH to the Port 0 latch (the Special Function Regis- ter), thus obliterating whatever information the Port 0 The reason that read-modify-write instructions are di- SFR may have been holding. If the user writes to Port 0 rected to the latch rather than the pin is to avoid a during an external memory fetch, the incoming code possible misinterpretation of the voltage level at the byte is corrupted. Therefore, do not write to Port 0 if pin. For example, a port bit might be used to drive the external program memory is used. base of a transistor. When a 1 is written to the bit, the transistor is turned on. If the CPU then reads the same External Program Memory is accessed under two con- port bit at the pin rather than the latch, it will read the ditions: base voltage of the transistor and interpret it as a o. 1) Whenever signal EA is active; or Reading the latch rather than the pin will return the 2) Whenever the program counter (PC) contains a correct value of 1. number that is larger than OFFFH (IFFFH for the 8052). ACCESSING EXTERNAL MEMORY This requires that the ROMless versions have EA wired low to enable the lower 4K (8K for the 8032) program Accesses to external memory are of two types: accesses bytes to be fetched from external memory. to external Program Memory and accesses to external Data Memory. Accesses to external Program Memory When the CPU is executing out of external Program use signal PSEN (program store enable) as the read Memory, all 8 bits of Port 2 are dedicated to an output strobe. Accesses to external Data Memory use RD or function and may not be used for general purpose I/O. WR (alternate functions of P3. 7 and P3.6) to strobe the During external program fetches they output the high memory. Refer to Figures 36 through 38 in the Internal byte of the PC. During this time the Port 2 drivers use Timing section. the strong pullups to emit PC bits that are Is. Fetches from external Program Memory always use a 16-bit address. Accesses to external Data Memory can TIMER/COUNTERS use either a 16-bit address (MOVX @DPTR) or an The 8051 has two 16-bit Timer/Counter registers: Tim- 8-bit address (MOVX @Ri). er 0 and Timer 1. The 8052 has these two plus one 3-9 intel~ HARDWARE DESCRIPTION OF THE 8051, 8052 AND 80C51 more: Timer 2. All three can be configured to operate four operating modes, which are selected by bit-pairs either as timers or event counters. (MI, MO) in TMOD. Modes 0, I, and 2 are the same for both Timer/Counters. Mode 3 is different. The four In the "Timer" function, the register is incremented operating modes are described in the following text. every machine cycle. Thus, one can think of it as count- ing machine cycles. Since a machine cycle consists of 12 oscillator periods, the count rate is '112 of the oscillator MODE 0 frequency. Either Timer in Mode 0 is an 8-bit Counter with a divide-by-32 prescaler. This 13-bit timer is MCS-48 In the "Counter" function, the register is incremented compatible. Figure 7 shows the Mode 0 operation as it in response to a I-to-O transition at its corresponding applies to Timer I. external input pin, TO, Tl or (in the 8052) T2. In this function, the external input is sampled during S5P2 of In this mode, the Timer register is configured as a every machine cycle. When the samples show a high in 13-Bit register. As the count rolls over from allIs to all one cycle and a low in the next cycle, the count is incre- Os, it sets the Timer interrupt flag TF1. The counted mented. The new count value appears in the register input is enabled to the Timer when TRI = 1 and either during S3PI of the cycle following the one in which the GATE = 0 or INTI = 1. (Setting GATE = I allows transition was detected. Since it takes 2 machine cycles the Timer to be controlled by external input INTI, to (24 oscillator periods) to recognize a I-to-O transition, facilitate pulse width measurements.) TR I is a control the maximum count rate is '124 of the oscillator fre- bit in the Special Function Register TCON (Figure 8). quency. There are no restrictions on the duty cycle of GATE is in TMOD. the external input signal, but to ensure that a given level is sampled at least once before it changes, it The 13-Bit register consists of all 8 bits of THI and the should be held for at least one full machine cycle. lower 5 bits of TL1. The upper 3 bits of TLI are inde- terminate and should be ignored. Setting the run flag In addition to the "Timer" or "Counter" selection, (TRI) does not clear the registers. Timer 0 and Timer I have four operating modes from which to select. Timer 2, in the 8052, has three modes Mode 0 operation is the same for Timer 0 as for Timer of operation: "Capture," "Auto-Reload" and "baud 1. Substitute TRO, TFO and INTO for the correspond- rate generator." ing Timer I signals in Figure 7. There are two different GATE bits, one for Timer I (TMOD.7) and one for Timer 0 (TMOD.3). Timer 0 and Timer 1 These Timer/Counters are present in both the 8051 and MODE 1 the 8052. The "Timer" or "Counter" function is select- ed by control bits clf in the Special Function Register Mode 1 is the same as Mode 0, except that the Timer TMOD (Figure 6). These two Timer/Counters have register is being run with all 16 bits. (MSB) (LSB) L~_G_A_T_E____C_/_T____M_1_____M_0~Jl GATE CIT T M1 MO J Timer 1 Timer 0 GATE Gating control when sel Timer/Counter "x" is enabled M1 MO Operating Mode only while "INTx" pin is high and "TAx" control pin is o o 8-bit Timer/Counter "THx" with "TLx" as 5·bit set. When cleared Timer "x" is enabled whenever prescaler. "TRx" control bit is set. o 16-bit Timer/Counter "THx" and "TLx" are c/f Timer or Counter Selector cleared for Timer operation cascaded; there is no prescaler. (input from internal system clock). Set for Counter o a-bit auto-reload Timer/Counter "THx" holds a operation (input from "Tx" input pin). value which is to be reloaded into "TLx" each time it overflows. (Timer 0) TLO is an a·bit Timer/Counter controlled by the standard Timer 0 control bits. THO is an 8-bit timer only controlled by Timer 1 control bits. (Timer 1) Timer/Counter 1 stopped. Figure 6. TMOD: Timer/Counter Mode Control Register 3-10 inteL HARDWARE DESCRIPTION OF THE 8051, 8052 AND 80C51 INTERRUPT CONTROL T1 PIN-----~ 270252-9 Figure 7. Timer/Counter 1 Mode 0: 13-Bit Counter (MSB) (LSB) TF1 TR1 TFO TRO IE1 IT1 lEO ITO Symbol Position Name and Significance Symbol Position Name and Significance TF1 TCON.7 Timer 1 overflow Flag. Set by IE1 TCON.3 Interrupt 1 Edge flag. Set by hardware hardware on Timer/Counter overflow. when external interrupt edge Cleared by hardware when processor detected. Cleared when interrupt vectors to interrupt routine. processed. TR1 TCON.6 Timer 1 Run control bit. Set/cleared IT1 TCON.2 Interrupt 1 Type control bit. Set/ by software to turn Timer/Counter on/ cleared by software to specify falling off. edge/low level triggered external interrupts. TFO TCON.5 Timer 0 overflow Flag. Set by hardware on Timer/Counter overflow. lEO TCON.1 Interrupt 0 Edge flag. Set by hardware Cleared by hardware when processor when external interrupt edge vectors to interrupt routine. detected. Cleared when interrupt Timer 0 Run control bit. Set/cleared processed. TRO TCON.4 by software to turn Timer/Counter on/ ITO TCON.O Interrupt 0 Type control bit. Set/ off. cleared by software to specify falling edge/low level triggered external interrupts. Figure 8.TeON: Timer/Counter Control Register MODE 2 Timer 0 in Mode 3 establishes TLO and THO as two separate counters. The logic for Mode 3 on Timer 0 is Mode 2 configures the Timer register as an 8-bit Coun- shown in Figure 10. TLO uses the Timer 0 control bits: ter (TL 1) with automatic reload, as shown in Figure 9. clf, GATE, TRO, INTO, and TFO. THO is locked into Overflow from TLl not only sets TFl, but also reloads a timer function (counting machine cycles) and takes TLI with the contents ofTHl, which is preset by soft- over the use ofTRI and TFI from Timer 1. Thus, THO ware. The reload leaves THI unchanged. now controls the "Timer 1" interrupt. Mode 2 operation is the same for Timer/Counter O. Mode 3 is provided for applications requiring an extra 8-bit timer or counter. With Timer 0 in Mode 3, an 8051 can look like it has three Timer/Counters, and an MODE 3 8052, like it has four. When Timer 0 is in Mode 3, Timer 1 can be turned on and off by switching it out of Timer 1 in Mode 3 simply holds its count. The effect is and into its own Mode 3, or can still be used by the the same as setting TRI = O. serial port as a baud rate generator, or in fact, in any application not requiring an interrupt. 3-11 infel~ HARDWARE DESCRIPTION OF THE 8051, 8052 AND 80C51 INTERRUPT 270252-10 Figure 9. Timer/Counter 1 Mode 2: 8-Bit Auto-Reload aBC ~G- 1/1210se 1112 fose - - - - - - - , t - - - - INTERRUPT TO PIN - - - - - - - - ' CONTROL 1/12 fose ----------+1-01 1 ·1. ._(8_~_O_) H. t-- _ ______ :lc~eoNTRoL .. _TF1_.. INTERRUPT TR1 - 270252-11 Figure 10. Timer/Counter 0 Mode 3: Two 8-Bit Counters Timer 2 Table 2. Timer 2 Operating Modes Timer 2 is a 16-bit Timer/Counter which is present only in the 8052. Like Timers 0 and 1, it can operate RCLK + TCLK CP/RL2 TR2 Mode either as a timer or as an event counter. This is selected 0 0 1 16-bit Auto-Reload by bit C!T2 in the Special Function Register T2CON 0 1 1 16-bit Capture (Figure 11). It has three operating modes: "capture," "auto-load" and "baud rate generator," which are se- 1 X 1 Baud Rate Generator lected by bits in nCON as shown in Table 2. X X 0 (off) 3-12 HARDWARE DESCRIPTION OF THE 8051, 8052 AND 80C51 (MSB) (LSB) TF2 EXF2 RCLK TCLK EXEN2 TR2 C/T2 CP/RL2 Symbol Position Name and Significance TF2 T2CON.7 Timer 2 overflow flag set by a Timer 2 overflow and must be cleared by software. TF2 will not be set when either RCLK = 1 or TCLK = 1. EXF2 T2CON.6 Timer 2 external flag set when either a capture or reload is caused by a negative transition on T2EX and EXEN2 = 1. When Timer 2 interrupt is enabled, EXF2 = 1 will cause the CPU to vector to the Timer 2 interrupt routine. EXF2 must be cleared by software. RCLK T2CON.5 Receive clock flag. When set, causes the serial port to use Timer 2 overflow pulses for its receive clock in Modes 1 and 3. RCLK = 0 causes Timer 1 overflow to be used for the receive clock. TCLK T2CON.4 Transmit clock flag. When set, causes the serial port to use Timer 2 overflow pulses for its transmit clock in modes 1 and 3. TCLK = 0 causes Timer 1 overflows to be used for the transmit clock. EXEN2 T2CON.3 Timer 2 external enable flag. When set, allows a capture or reload to occur as a result of a negative transition on T2EX if Timer 2 is not being used to clock the serial port. EXEN2 = 0 causes Timer 2 to ignore events at T2EX. TR2 T2CON.2 Startlstop control for Timer 2. A logic 1 starts the timer. C/T2 T2CON.1 Timer or counter select. (Timer 2) o = Internal timer (OSC/12) 1 = External event counter (falling edge triggered). CP/RL2 T2CON.O capture/Reload flag. When set, captures will occur on negative transitions at T2EX if EXEN2 = 1. When cleared, auto-reloads will occur either with Timer 2 overflows or negative transitions at T2EX when EXEN2 = 1. When either RCLK = 1 or TCLK = 1, this bit is ignored and the timer is forced to auto-reload on Timer 2 overflow. Figure 11. T2CON: Timer/Counter 2 Control Register In the Capture Mode there are two options which are added feature that a I-to-O transition at external input selected by bit EXEN2 in T2CON. If EXEN2 = 0, T2EX will also trigger the 16-bit reload and set EXF2. then Timer 2 is a 16-bit timer or counter which upon overflowing sets bit TF2, the Timer 2 overflow bit, The auto-reload mode is illustrated in Figure. 13. which can be used to generate an interrupt. If EXEN2 = 1, then Timer 2 still does the above, but with the The baud rate generator mode is selected by RCLK = added feature that a I-to-O transition at external input 1 and/or TCLK = 1. It will be described in conjunc- T2EX causes the current value in the Timer 2 registers, tion with the serial port. TL2 and TH2, to be captured into registers RCAP2L and RCAP2H, respectively. (RCAP2L and RCAP2H are new Special Function Registers in the 8052.) In SERIAL INTERFACE addition, the transition at T2EX causes bit EXF2 in T2CON to be set, and EXF2, like TF2, can generate an The serial port is full duplex, meaning it can transmit interrupt. and receive simultaneously. It is also receive-buffered, meaning it can commence reception of a second byte The Capture Mode is illustrated in Figure 12. before a previously received byte has been read from the receive register. (However, if the first byte still In the auto-reload mode there are again two options, hasn't been read by the time reception of the second which are selected by bit EXEN2 in T2CON. If byte is complete, one of the bytes will be lost). The EXEN2 = 0, then when Timer 2 rolls over it not only serial port receive and transmit registers are both ac- sets TF2 but also causes the Timer 2 registers to be cessed at Special Function Register SBUF. Writing to reloaded with the 16-bit value in registers RCAP2L SBUF loads the transmit register, and reading SBUF and RCAP2H, which are preset by software. If EXEN2 accesses a physically separate receive register. = 1, then Timer 2 still does the above. but with the 3-13 iniaL HARDWARE DESCRIPTION OF THE 8051, 8052 AND 80C51 12 P l N - - - - - - ' nMER2 INTERRUPT 12EX PIN EXEN2 270252-12 Figure 12. Timer 2 in Capture Mode The serial port can operate in 4 modes: Multiprocessor Communications Mode 0: Serial data enters and exits through RXD. Modes 2 and 3 have a special provision for multipro- TXD outputs the shift clock. 8 bits are transmitted/re- cessor communications. In these modes, 9 data bits are ceived: 8 data bits (LSB first). The baud rate is fixed at received. The 9th one goes into RB8. Then comes a 1112 the oscillator frequency. stop bit. The port can be programmed such that when the stop bit is received, the serial port interrupt will be Mode 1: 10 bits are transmitted (through TXD) or re- activated only if RB8 = 1. This feature is enabled by ceived (through RXD): a start bit (0), 8 data bits (LSB setting bit SM2 in SCON. A way to use this feature in first), and a stop bit (1). On receive, the stop bit goes multiprocessor systems is as follows. into RB8 in Special Function Register SCaN. The baud rate is variable. When the master processor wants to transmit a block of data to one of several slaves, it first sends out an ad- Mode 2: 11 bits are transmitted (through TXD) or re- dress byte which identifies the target slave. An address ceived (through RXD): a start bit (0), 8 data bits (LSB byte differs from a data byte in that the 9th bit is 1 in an first), a programmable 9th data bit, and a stop bit (1). address byte and 0 in a data byte. With SM2 = 1, no On Transmit, the 9th data bit (TB8 in SCON) can be slave will be interrupted by a data byte. An address assigned the value of 0 or 1. Or, for example, the parity byte, however, will interrupt all slaves, so that each bit (p, in the PSW) could be moved into TB8. On re- slave can examine the received byte and see if it is being ceive, the 9th data bit goes into RB8 in Special Functon addressed. The addressed slave will clear its SM2 bit Register SCON, while the stop bit is ignored. The baud and prepare to receive the data bytes that will be com- rate is programmable to either 1/32 or V64 the oscillator ing. The slaves that weren't being addressed leave their frequency. SM2s set and go on about their business, ignoring the coming data bytes. Mode 3: 11 bits are transmitted (through TXD) or re- SM2 has no effect in Mode 0, and in Mode 1 can be ceived (through RXD): a start bit (0), 8 data bits (LSB used to check the validity of the stop bit. In a Mode 1 first), a programmable 9th data bit and a stop bit (1). In reception, if SM2 = 1, the r~eive interrupt will not be fact, Mode 3 is the same as Mode 2 in all respects activated unless a valid stop bit is received. except the baud rate. The baud rate in Mode 3 is vari- able. Serial Port Control Register In all four modes, transmission is initiated by any in- struction that uses SBUF as a destination register. Re- The serial port control and status register is the Special ception is initiated in Mode 0 by the condition RI = 0 Function Register SCaN, shown in Figure 14. This and REN = 1. Reception is initiated in the other register contains not only the mode selection bits, but modes by the incoming start bit if REN = 1. also the 9th data bit for transmit and receive (TB8 and RB8), and the serial port interrupt bits (TI and RI). 3-14 inteL HARDWARE DESCRIPTION OF THE 8051, 8052 AND 80C51 nMER2 INTERRUPT EXEN2 270252-13 Figure 13. Timer 2 in Auto-Reload Mode (MSB) (LSB) SMO SM1 SM2 REN TBS RBS TI RI Where SMO, SM1 specify the serial port mode, as follows: • TBS is the 9th data bit that will be transmitted in Modes 2 and 3. Set or SMO SM1 Mode Description Baud Rate clear by software as desired. 0 0 0 shift register fose/ 12 0 1 1 8-bitUART variable • RBS in Modes 2 and 3, is the 9th data bit 0 9-bitUART that was received. In Mode 1, if SM2 1 2 fose/64 or = 0, RBS is the stop bit that was received. In Mode 0, RBS is not used. fose/32 3 9-bit UART variable • TI is transmit interrupt flag. Set by enables the multiprocessor hardware at the end of the 8th bit time • SM2 communication feature in Modes 2 in Mode 0, or at the beginning of the and 3. In Mode 2 or 3, if SM2 is set to stop bit in the other modes, in any 1 then RI will not be activated if the serial transmission. Must be cleared received 9th data bit (RBS) is O. In by software. Mode 1, if SM2 = 1 then RI will not be activated if a valid stop bit was not • RI is receive interrupt flag. Set by received. In Mode 0, SM2 should be hardware at the end of the 8th bit time O. in Mode 0, or halfway through the stop • REN enables serial reception. Set by bit time in the other modes, in any software to enable reception. Clear serial reception (except see SM2). by software to disable reception. Must be cleared by software. Figure 14. SCON: Serial Port Control Register Baud Rates The baud rate in Mode 0 is fixed: 2SMOD Mode 2 Baud Rate = ~ X (Oscillator Frequency) Oscillator Frequency Mode 0 Baud Rate = 12 In the 8051, the baud rates in Modes 1 and 3 are deter- mined by the Timer 1 overflow rate. In the 8052, these The baud rate in Mode 2 depends on the value of bit baud rates can be determined by Timer 1, or by Timer SMOD in Special Function Register peON. If SMOD 2, or by both (one for transmit and the other for re- = 0 (which is the value on reset), the baud rate Y64 the ceive). oscillator frequency. If SMOD = 1, the baud rate is Y32 the oscillator frequency. 3-15 inteL HARDWARE DESCRIPTION OF THE 8051, 8052 AND 80C51 Using Timer 1 to Generate Baud Rates mode (high nibble of TMOD = 0010B). In that case, the baud rate is given by the formula When Timer 1 is used as the baud rate generator, the baud rates in Modes 1 and 3 are determined by the Modes I, 3 2SMOD Oscillator Frequency Timer 1 overflow rate and the value of SMOD as fol- lows: Baud Rate = ----n- X 12x [256 - (TH1)1 Modes I, 3 2SMOD One can achieve very low baud rates with Timer 1 by Baud Rate = ----n- X (Timer 1 Overflow Rate) leaving the Timer 1 interrupt enabled, and configuring the Timer to run as a 16-bit timer (high nibble of TMOD = 000IB), and using the Timer 1 interrupt to The Timer 1 interrupt should be disabled in this appli- do a 16-bit software reload. cation. The Timer itself can be configured for either "timer" or "counter" operation, and in any of its 3 Figure 15 lists various commonly used baud rates and running modes. In the most typical applications, it is how they can be obtained from Timer 1. configured for "timer" operation, in the auto-reload Timer 1 Baud Rate fosc SMOD Reload cif Mode Value Mode 0 Max: 1 MHZ 12MHZ X X X X Mode 2 Max: 375K 12MHZ 1 X X X Modes 1, 3: 62.5K 12MHZ 1 0 2 FFH 19.2K 11.059 MHZ 1 0 2 FDH 9.6K 11.059 MHZ 0 0 2 FDH 4.8K 11.059 MHZ 0 0 2 FAH 2.4K 11.059 MHZ 0 0 2 F4H 1.2K 11.059 MHZ 0 0 2 E8H 137.5 11.986 MHZ 0 0 2 1DH 110 6MHZ 0 0 2 72H 110 12MHZ 0 0 1 FEEBH Figure 15. Timer 1 Generated Commonly Used Baud Rates Using Timer 2 to Generate Baud Rates 11). Note then the baud rates for transmit and receive can be simultaneously different. Setting RCLK and/or In the 8052, Timer 2 is selected as the baud rate genera- TCLK puts Timer 2 into its baud rate generator mode, tor by setting TCLK and/or RCLK in T2CON (Figure as shown in Figure 16. lIMER1 OVEllR.OW fIX CLOCK TXCLOCK EXEN2 L NOTE AVAILAIIIL/TY OF ADOfTIONAL EXTERNAL INTERRUPT 270252-14 Figure 16. Timer 2 in Baud Rate Generator Mode 3-16 HARDWARE DESCRIPTION OF THE 8051, 8052 AND 80C51 The baud rate generator mode is similar to the auto-re- Transmission is initiated by any instruction that uses load mode, in that a rollover in TH2 causes the Timer 2 SBUF as a destination register. The "write to SBUF" registers to be reloaded with the 16-bit value in registers signal at S6P2 also loads a 1 into the 9th position of the RCAP2H and RCAP2L, which are preset by software. transmit shift register and tells the TX Control block to commence a transmission. The internal timing is such Now, the baud rates in Modes 1 and 3 are determined that one full machine cycle will elapse between "write by Timer 2's overflow rate as follows: to SBUF," and activation of SEND. Timer 2 Overflow Rate SEND enables the output of the shift register to the Modes 1, 3 Baud Rate = - - - - - - - - alternate output function line of P3.0, and also enables 16 SHIFT CLOCK to the alternate output function line of The Timer can be configured for either "timer" or P3.1. SHIFT CLOCK is low during S3, S4, and S5 of "counter" operation. In the most typical applications, it every machine cycle, and high during S6, S1 and S2. At is configured for "timer" operation (C/T2 = 0). "Tim- S6P2 of every machine cycle in which SEND is active, er" operation is a little different for Timer 2 when it's the contents of the transmit shift register are shifted to being used as a baud rate generator. Normally, as a the right one position. timer it would increment every machine cycle (thus at As data bits shift out to the right, zeroes come in from lju the oscillator frequency). As a baud rate generator, the left. When the MSB of the data byte is at the output however, it increments every state time (thus at lj2 the position of the shift register, then the 1 that was initial- oscillator frequency). In that case the baud rate is given ly loaded into the 9th position, is just to the left of the by the formula MSB, and all positions to the left of that contain zeroes. Modes 1, 3 Oscillator Frequency This condition flags the TX Control block to do one Baud Rate = ---:---------''----:;.----:- last shift and then deactivate SEND and set TI. Both of 32x [65536 - (RCAP2H, RCAP2L)1 these actions occur at S1P1 of the 10th machine cycle after "write to SBUF." where (RCAP2H, RCAP2L) is the content of RCAP2H and RCAP2L taken as a 16-bit unsigned in- Reception is initiated by the condition REN = 1 and teger. R 1 = O. At S6P2 of the next machine cycle, the RX Control unit writes the bits 11111110 to the receive Timer 2 as a baud rate generator is shown in Figure 16. shift register, and in the next clock phase activates RE- This Figure is valid only if RCLK + TCLK = 1 in CEIVE. T2CON. Note that a rollover in TH2 does not set TF2, and will not generate an interrupt. Therefore, the Timer RECEIVE enables SHIFT CLOCK to the· alternate 2 interrupt does not have to be disabled when Timer 2 output function line of P3.1. SHIFT CLOCK makes is in the baud rate generator mode. Note too, that if transitions at S3Pl and S6PI of every machine cycle. EXEN2 is set, a 1-to-O transition in TIEX will set At S6P2 of every machine cycle in which RECEIVE is EXF2 but will not cause a reload from (RCAP2H, active, the contents of the receive shift register are shift- RCAP2L) to (TH2, TL2). Thus when Timer 2 is in use ed to the left one position. The value that comes in as a baud rate generator, T2EX can be used as an extra from the right is the value that was sampled at the P3.0 external interrupt, if desired. pin at S5P2 of the same machine cycle. As data bits come in from the right, 1s shift out to the It should be noted that when Timer 2 is running (TR2 left. When the 0 that was initially loaded into the right- = 1) in "timer" function in the baud rate generator most position arrives at the leftmost position in the shift mode, one should not try to read or write TH2 or TL2. register, it flags the RX Control block to do one last Under these conditions the Timer is being incremented shift and load SBUF. At S1P1 of the 10th machine every state time, and the results of a read or write may cycle after the write to SCON that cleared RI, RE- not be accurate. The RCAP registers may be read, but CEIVE is cleared and RI is set. shouldn't be written to, because a write might overlap a reload and cause write and/or reload errors. Turn the Timer off (clear TR2) before accessing the Timer 2 or More About Mode 1 RCAP registers, in this case. Ten bits are transmitted (through TXD), or received (through RXD): a start bit (0), 8 data bits (LSB first), More About Mode 0 and a stop bit (1). On receive, the stop bit goes into RB8 in SCON. In the 8051 the baud rate is determined Serial data enters and exits through RXD. TXD out- by the Timer 1 overflow rate. In the 8052 it is deter- puts the shift clock. 8 bits are transmitted/received: 8 mined either by the Timer 1 overflow rate, or the Timer data bits (LSB first). The baud rate is fixed at 1/12 the 2 overflow rate, or both (one for transmit and the other oscillator frequency. for receive). Figure 17 shows a simplified functional diagram of the Figure 18 shows a simplified functional diagram of the serial port in Mode 0, and associated timing. serial port in Mode 1, and associated timings for trans- mit receive. 3-17 HARDWARE DESCRIPTION OF THE 8051, 8052 AND 80C51 WRITE TO SBUF ---lr-:~j;i:Jr~--~t-----1-r---____~~ Rxe P3.0ALT OUTPUT FUNCTION 56 --_------~ Txe P3.1 ALT OUTPUT FUNCTION ~----t RX CLOCK RXCONTROL SHIFT REN -....r--t--_____... START AI ~----~-r~~~-T~~ Rxe P3.0ALT INPUT FUNCTION REAO SBUF ALE ---Q.WRITE TO SBUF SEND S6P2 rl------------------------------------------------~~ SHIFT RXD (DATA OUT) , TRANSMIT TXD (SHIFT CLOCK) t l n~ _________S3__ ~_~ _______________________________________~r_____ --II WRITE TO SCON (CLEAR RI) AIIl~ ________________________________________________ ~r_____ RECEIVE ~ RECEIVE SHIFT RXD(DATAIN)------~~--~~--~~----u_~--~~--~~----ti=~--~~---- 270252-15 Figure 17. Serial Port Mode 0 3-18 HARDWARE DESCRIPTION OF THE 8051, 8052 AND 80C51 TIMER' TIMER2 OVERFLOW OVERFLOW TXD TX €LOCKa L---J~ __L -__~__ - L____n~ ____L -__~___ I WRITE TO SBUF -----.~S~E~N~D======================================~'--_ __ DATA LS1P1 I TRANSMIT SHIFT L-0 a n "'"'fiD\ I DO I 01 I 02 03 04 05 0 6 CbLJ STOP BIT TI START BIT r----"--'----'---- ~_L- _ _n___J~__L __ _~_ __ L_ _~_ __ _ ~ __________________________________________________ ~r____ 270252-16 Figure 18. Serial Port Mode 1. TCLK, RCLK and Timer 2 are Present in the 8052/8032 Only. Transmission is initiated by any instruction that uses times are synchronized to the divide-by-16 counter, not SBUF as a destination register. The "write to SBUF" to the "write to SBUF" signal). signal also loads a 1 into the 9th bit position of the transmit shift register and flags the TX Control unit The transmission begins with activation of SEND, that a transmission is requested. Transmission actually which puts the start bit at TXD. One bit time later, commences at SIP 1 of the machine cycle following the DATA is activated, which enables the output bit of the next rollover in the divide-by-16 counter. (Thus, the bit transmit shift register to TXD. The first shift pulse oc- curs one bit time after that. 3-19 intel~ HARDWARE DESCRIPTION OF THE 8051, 8052 AND 80C51 As data bits shift out to the right, zeroes are clocked in mit, the 9th data bit (TB8) can be assigned the value of from the left. When the MSB of the data byte is at the o or 1. On receive. the 9th data bit I!:oes into RB8 in output position of the shift register, then th~ 1 that was SCON. The baud r~te is programmable to either 1/32 or initially loaded into the 9th position is just to the left of 1/64 the oscillator frequency in Mode 2. Mode 3 may the MSB, and all positions to the left of that contain have a variable baud rate generated from either Timer 1 zeroes. This condition flags the TX Control unit to do or 2 depending on the state of TCLK and RCLK. one last shift and then deactivate SEND and set TI. This occurs at the 10th divide-by-16 rollover after Figures 19 and 20 show a functional diagram of the "write to SBUF." serial port in Modes 2 and 3. The receive portion is exactly the same as in Mode 1. The transmit portion Reception is initiated by a detected 1-to-O transition at differs from Mode 1 only in the 9th bit of the transmit RXD. For this purpose RXD is sampled at a rate of 16 shift register. times whatever baud rate has been established. When a transition is detected, the divide-by-16 counter is imme- Transmission is initiated by any instruction that uses diately reset, and IFFH is written into the input shift SBUF as a destination register. The "write to SBUF' register. Resetting the divide-by-16 counter aligns its signal also loads TB8 into the 9th bit position of the rollovers with the boundaries of the incoming bit times. transmit shift register and flags the TX Control unit that a transmission is requested. Transmission com- The 16 states of the counter divide each bit time into mences at SIPl of the machine cycle following the next 16ths. At the 7th, 8th, and 9th counter states of each bit rollover in the divide-by-16 counter. (Thus, the bit time, the bit detector samples the value of RXD. The times are synchronized to the divide-by-16 counter, not value accepted is the value that was seen in at least 2 of to the "write to SBUF" signal.) the 3 samples. This is done for noise rejection. If the value accepted during the first bit time is not 0, the The transmission begins with activation of SEND, receive circuits are reset and the unit goes back to look- which puts the start bit at TXD. One bit time later, ing for another I-to-O transition. This is to provide re- DATA is activated, which enables the output bit of the jection of false start bits. If the start bit proves valid, it transmit shift register to TXD. The first shift pulse oc- is shifted into the input shift register, and reception of curs one bit time after that. The first shift clocks a 1 the rest of the frame will proceed. (the stop bit) into the 9th bit position of the shift regis- ter. Thereafter, only zeroes are clocked in. Thus, as As data bits come in from the right, Is shift out to the data bits shift out to the right, zeroes are clocked in left. When the start bit arrives at the leftmost position from the left. When TB8 is at the output position of the in the shift register, (which in mode 1 is a 9-bit regis- shift register, then the stop bit is just to the left of TB8, ter), it flags the RX Control block to do one last shift, and all positions to the left of that contain zeroes. This load SBUF and RB8, and set RI. The signal to load condition flags the TX Control unit to do one last shift SBUF and RB8, and to set RI, will be generated if, and and then deactivate SEND and set TI. This occurs at only if, the following conditions are met at the time the the 11th divide-by-16 rollover after "write to SBUF." final shift pulse is generated. Reception is initiated by a detected I-to-O transition at 1) RI = 0, and RXD. For this purpose RXD is sampled at a rate of 16 2) Either 5M2 = 0, or the received stop bit = 1 times whatever baud rate has been established. When a transition is detected, the divide-by-16 counter is imme- If either of these two conditions is not met, the received diately reset, and IFFH is written to the input shift frame is irretrievably lost. If both conditions are met, register. the stop bit goes into RB8, the 8 data bits go into SBUF, and RI is activated. At this time, whether the At the 7th, 8th and 9th counter states of each bit time, above conditions are met or not, the unit goes back to the bit detector samples the value of RXD. The value looking for a I-to-O transition in RXD. accepted is the value that was seen in at least 2 of the 3 samples. If the value accepted during the first bit time is not 0, the receive circuits are reset and the unit goes More About Modes 2 and 3 back to looking for another I-to-O transition. If the start bit proves valid, it is shifted into the input shift Eleven bits are transmitted (through TXD), or received register, and reception of the rest of the frame will pro- (through RXD): a start bit (0), 8 data bits (LSB first), a ceed. programmable 9th data bit, and a stop bit (1). On trans- 3-20 intelQD HARDWARE DESCRIPTION OF THE 8051, 8052 AND 80C51 WRITE TO SBUF TXD PHASE 2 CLOCK ('hfosc) MODE2 (SMOD IS PCON.7) L . . . . _ - - _ t LOAD SBUF S H I F T t - - - - -..... RXD READ _ _ _" SBUF TRANSMIT f STOP BIT ~-------------------------------------- ____________________~r---- 270252-17 Figure 19. Serial Port Mode 2 3-21 intet HARDWARE DESCRIPTION OF THE 8051, 8052 AND 80C51 T!MER 1 T!MER 2 OVERFLOW OVERFLOW TXD TCLK- LOAD sBUF SHIFT 1 - - - - - - , RXD 80S1 INTERNAL BUS TX ~' ____JL-__~L_ _-JL__~L-_JL-__~~__~L__~I~_ _JL-__~~__~L____ q WRITE TO SBUF - - - , SEND DATA ~L~S~1P~1~1========================================~L-_______ SHIFT ~nnnp~ TRANSMIT ~_...J.--=D=o_LQ1::X::~QL) ,TB8 I STOP BIT I I RXD BIT DETECTOR' STAAT BIT I RECEIVE SAMPLE TIMES SHIFT ,__~IL-__-JI____~L____J~____JL-__-JIL-__-JL____~L____~~_____ ________________________________________________________________-Jr----- R~~I 270252-18 Figure 20. Serial Port Mode 3. TCLK, RCLK, and Timer 2 are Present in the 8052/8032 Only. 3-22 intel~ HARDWARE DESCRIPTION OF THE 8051, 8052 AND 80C51 As data bits come in from the right, Is shift out to the was transition-activated. If the interrupt was level-acti- left. When the start bit arrives at the leftmost position vated, then the external requesting source is what con- in the shift register (which in Modes 2 and 3 is a 9-bit trols the request flag, rather than the on-chip hardware. register), it flags the RX Control block to do one last shift, load SBUF and RB8, and set RI. The signal to The Timer 0 and Timer 1 Interrupts are generated by load SBUF and RB8, and to set RI, will be generated if, TFO and TF1, which are set by a rollover in their re- and only if, the following conditions are met at the time spective Timer/Counter registers (except see Timer 0 in the final shift pulse is generated: Mode 3). When a timer interrupt is generated, the flag that generated it is cleared by the on-chip hardware 1) RI = 0, and when the service routine is vectored to. 2) Either 5M2 = 0 or the received 9th data bit = 1 The Serial Port Interrupt is generated by the logical OR If either of these conditions is not met, the received of RI and TI. Neither of these flags is cleared by hard- frame is irretrievably lost, and RI is not set. If both ware when the service routine is vectored to. In fact, conditions are met, the received 9th data bit goes into the service routine will normally have to determine RB8, and the first 8 data bits go into SBUF. One bit whether it was RI or TI that generated the interrupt, time later, whether the above conditions were met or and the bit will have to be cleared in software. not, the unit goes back to looking for a I-to-O transition at the RXD input. In the 8052, the Timer 2 Interrupt is generated by the logical OR of TF2 and EXF2. Neither of these flags is Note that the value of the received stop bit is irrelevant cleared by hardware when the service routine is vec- to SBUF, RB8, or RI. tored to. In fact, the service routine may have to deter- mine whether it was TF2 or EXF2 that generated the interrupt, and the bit will have to be cleared in soft- INTERRUPTS ware. The 8051 provides 5 interrupt sources. The 8052 pro- All of the bits that generate interrupts can be set or vides 6. These are shown in Figure 21. cleared by software, with the same result as though it had been set or cleared by hardware. That is, interrupts The External Interrupts INTO and INTI can each be can be generated or pending interrupts can be canceled either level-activated or transition-activated, depending in software. on bits ITO and IT! in Register TCON. The flags that actually generate these interrupts are bits lEO and IE! (MSB) (LSB) in TCON. When an external interrupt is generated, the flag that generated it is cleared by the hardware when I EA I - IET21 ES IET1 IEX1 IETO IEXO I Enable Bit = 1 enables the interrupt. the service routine is vectored to only if the interrupt Enable Bit = 0 disables it. Symbol Position Function EA IE.7 disables all interrupts. If EA = 0, no interrupt will be acknowledged. If EA = 1. each interrupt source is individually enabled or disabled by setting or clearing its enable bit. IE.6 reserved. ~--------------------------------- ET2 IE.5 Timer 2 interrupt enable bit. ES IE.4 Serial Port interrupt enable bit. ET1 IE.3 Timer 1 interrupt enable bit. INTERRUPT SOURCES EX1 IE.2 Extemal interrupt 1 enable bit. ETO IE.1 Timer 0 interrupt enable bit. EXO IE.O Extemal interrupt 0 enable bit. W1------------------------------- User software should never write 1s to unimplemented bits. since they may be used in future MCS-51 products. Figure 22. IE: Interrupt Enable Register 270252-19 Figure 21. MCS®·51 Interrupt Sources 3-23 intel~ HARDWARE DESCRIPTION OF THE 8051, 8052 AND 80C51 Each of these interrupt sources can be individually en- ceived simultaneously, an internal polling sequence de- abled or disabled by setting or clearLllg a bit in Special tennL'les which request is serviced. Thus \IJithin each Function Register IE (Figure 22). IE contains also a priority level there is a second priority structure deter- global disable bit, EA, which disables all interrupts at mined by the polling sequence, as follows: once. Source Priority Within Level Note in Figure 22 that bit position IE.6 is unimple- 1. lEO (highest) mented. In the 8051s, bit position IE.5 is also unimple- 2. TFO mented. User software should not write Is to these bit 3. IE1 positions, since they may be used in future MCS-51 4. TF1 products. 5. RI +TI 6. TF2 + EXF2 (lowest) Priority Level Structure Note that the "priority within level" structure is only Each interrupt source can also be individually pro- used to resolve simultaneous requests of the same priori- grammed to one of two priority levels by setting or ty level. clearing a bit in Special Function Register IP (Figure 23). A low-priority interrupt can itself be interrupted The IP register contains a number of unimplemented by a high-priority interrupt, but not by another low-pri- bits. IP.7 and IP.6 are vacant in the 8052s, and in the ority interrupt. A high-priority interrupt can't be inter- 8051s these and IP.5 are vacant. User software should rupted by any other interrupt source. not write Is to these bit positions, since they may be used in future MCS-51 products. (MSS) (LSS) 1-1-1~1~lrnl~I~I~1 How Interrupts Are Handled Priority bit = 1 assigns high priority. The interrupt flags are sampled at S5P2 of every ma- Priority bit = 0 assigns low priority. chine cycle. The samples are polled during the follow- Symbol Position Function ing machine cycle. The 8052's Timer 2 interrupt cycle IP.7 reserved is different, as described in the Response Time Section. IP.6 reserved If one of the flags was in a set condition at S5P2 of the preceding cycle, the polling cycle will find it and the PT2 IP.5 Timer 2 interrupt priority bit. interrupt system will generate an LCALL to the appro- PS IP.4 Serial Port interrupt priority bit. priate service routine, provided this hardware-generat- PT1 IP.3 Timer 1 interrupt priority bit. ed LCALL is not blocked by any of the following con- PX1 IP.2 External interrupt 1 priority bit. ditions: PTO IP.1 Timer 0 interrupt priority bit. 1. An interrupt of equal or higher priority level is al- PXO IP.O External interrupt 0 priority bit. ready in progress. User software should never write 1s to unimplemented bits. since 2. The current (polling) cycle is not the final cycle in they may be used in future MCS-51 products. the execution of the instruction in progress. 3. The instruction in progress is RETI or any write to Figure 23. IP: Interrupt Priority Register the IE or IP registers. If two requests of different priority levels are received Any of these three conditions will block the generation simultaneously, the request of higher priority level is of the LCALL to the interrupt service routine. Condi- serviced. If requests of the same priority level are re- tion 2 ensures that the instruction in progress will be ········--C1 • I • C2 • I C3 I • C4 pi cs--····· ISSP21 56 ........ ~,\---'--~l---'--~l---'----- 11 '\'l h1 "-------"'" INTERRUPTS ARE POLLED ... LONG CALL TO INTERRUPT A- INTERRUPT ROUTINE INTERRUPT INTERRUPT VECTOR ADDRESS GOES LATCHED ACTIVE 270252-20 This is the fastest possible response when C2 is the final cycle of an instruction other than RETI or an access to IE or IP. Figure 24. Interrupt Response Timing Diagram 3-24 inteL HARDWARE DESCRIPTION OF THE 8051, 8052 AND 80C51 completed before vectoring to any service routine. Con- External Interrupts dition 3 ensures that if the instruction in progress is RETI or any access to IE or IP, then at least one more The external sources can be programmed to be level-ac- instruction will be executed before any interrupt is vec- tivated or transition-activated by setting or clearing bit tored to. ITI or ITO in Register TCON. If ITx = 0, external interrupt x is triggered by a detected low at the INTx The polling cycle is repeated with each machine cycle, pin. If ITx = 1, external interrupt x is edge-triggered. and the values polled are the values that were present at In this mode if successive samples of the INTx pin SSP2 of the previous machine cycle. Note then that if show a high in one cycle and a low in the next cycle, an interrupt flag is active but not being responded to for interrupt request flag lEx in TCON is set. Flag bit lEx one of the above conditions, and is not still active when then requests the interrupt. the blocking condition is removed, the denied interrupt will not be serviced. In other words, the fact that the Since the external interrupt pins are sampled once each interrupt flag was once active but not serviced is not machine cycle, an input high or low should hold for at remembered. Every polling cycle is new. least 12 oscillator periods to ensure sampling. If the external interrupt is transition-activated, the external The polling cycle/LCALL sequence is illustrated in source has to hold the request pin high for at least one Figure 24. machine cycle, and then hold it low for at least one machine cycle to ensure that the transition is seen so Note that if an interrupt of higher priority level goes that interrupt request flag lEx will be set. lEx will be active prior to SSP2 of the machine cycle labeled C3 in automatically cleared by the CPU when the service Figure 24, then in accordance with the above rules it routine is called. will be vectored to during CS and C6, without any in- struction of the lower priority routine having been exe- If the external interrupt is level-activated, the external cuted. source has to hold the request active until the requested interrupt is actually generated. Then it has to deacti- Thus the processor acknowledges an interrupt request vate the request before the interrupt service routine is by executing a hardware-generated LCALL to the ap- completed, or else another interrupt will be generated. propriate servicing routine. In some cases it also clears the flag that generated the interrupt, and in other cases it doesn't. It never clears the Serial Port or Timer 2 Response Time flags. This has to be done in the user's software. It The INTO and INTI levels are inverted and latched clears an external interrupt flag (lEO or lEI) only if it into the interrupt flags lEO and lEI at SSP2 of every was transition-activated. The hardware-generated machine cycle. Similarly, the Timer 2 flag EXF2 and LCALL pushes the contents of the Program Counter the Serial Port flags RI and TI are set at SSP2. The onto the stack (but it does not save the PSW) and re- values are not actually polled by the circuitry until the loads the PC with an address that depends on the next machine cycle. source of the interrupt being vectored to, as shown be- low. The Timer 0 and Timer 1 flags, TFO and TFI, are set at Vector SSP2 of the cycle in which the timers overflow. The Source Address values are then polled by the circuitry in the next cycle. lEO 0003H However, the Timer 2 flag TF2 is set at S2P2 and is TFO OOOBH polled in the same cycle in which the timer overflows. IE1 0013H If a request is active and conditions are right for it to be TF1 001BH acknowledged, a hardware subroutine call to the re- RI + TI 0023H quested service routine will be the next instruction to be TF2 + EXF2 002BH executed. The call itself takes two cycles. Thus, a mini- mum of three complete machine cycles elapse between Execution proceeds from that location until the RETI activation of an external interrupt request and the be- instruction is encountered. The RETI instruction in- ginning of execution of the first instruction of the serv- forms the processor that this interrupt routine is no ice routine. Figure 24 shows interrupt response timings. longer in progress, then pops the top two bytes from the stack and reloads the Program Counter. Execution of A longer response time would result if the request is the interrupted program continues from where it left blocked by one of the 3 previously listed conditions. If off. an interrupt of equal or higher priority level is already in progress, the additional wait time obviously depends Note that a simple RET instruction would also have on the nature of the other interrupt's service routine. If returned execution to the interrupted program, but it the instruction in progress is not in its fmal cycle, the would have left the interrupt control system thinking additional wait time cannot be more than 3 cycles, since an interrupt was still in progress. the longest instructions (MUL and DIY) are only 4 3-25 intet HARDWARE DESCRIPTION OF THE 8051, 8052 AND 80C51 cycles long, and if the instruction in progress is RETI RESET or an access to IE or IP, the additional wait time can- not be more than 5 cycles (a maximum of one more The reset input is the RST pin, which is the input to a cycle to complete the instruction in progress, plus 4 Schmitt Trigger. cycles to complete the next instruction if the instruction is MUL or DIY). A reset is accomplished by holding the RST pin high for at least two machine cycles (24 oscillator periods), Thus, in a single-interrupt system, the response time is while the oscillator is running. The CPU responds by always more than 3 cycles and less than 9 cycles. generating an internal reset, with the timing shown in Figure 25. SINGLE-STEP OPERATION The external reset signal is asynchronous to the internal clock. The RST pin is sampled during State 5 Phase 2 The 8051 interrupt structure allows single-step execu- of every machine cycle. The port pins will maintain tion with very little software overhead. As previously their current activities for 19 oscillator periods after a noted, an interrupt request will not be responded to logic 1 has been sampled at the RST pin; that is, for 19 while an interrupt of equal priority level is still in prog- to 31 oscillator periods after the external reset signal ress, nor will it be responded to after RETI until at has been applied to the RST pin. least one other instruction has been executed. Thus, once an interrupt routine has been entered, it cannot be While the RST pin is high, ALE and PSEN are weakly re-entered until at least one instruction of the interrupt- pulled high. After RST is pulled low, it will take 1 to 2 ed program is executed. One way to use this feature for machine cycles for ALE and PSEN to start clocking. single-stop operation is to program one of the external For this reason, other devices can not be synchronized interrupts (say, INTO) to be level-activated. The service to the internal timings of the 8051. routine for the interrupt will terminate with the follow- ing code: Driving the ALE and PSEN pins to 0 while reset is JNB P3.2,$ ;Wait Here Till INTO Goes High active could cause the device to go into an indetermi- nate state. JB P3.2,$ ;Now Wait Here Till it Goes Low RETI :Go Back and Execute One Instruction The internal reset algorithm writes Os to all the SFRs except the port latches, the Stack Pointer, and SBUF. Now if the INTO pin, which is also the P3.2 pin, is held The port latches are initialized to FFH, the Stack normally low, the CPU will go right into the External Pointer to 07H, and SBUF is indeterminate. Table 3 Interrupt 0 routine and stay there until INTO is pulsed lists the SFRs and their reset values. (from low to high to low). Then it will execute RETI, go back to the task program, execute one instruction, The internal RAM is not affected by reset. On power and immediately re-enter the External Interrupt 0 rou- up the RAM content is indeterminate. tine to await the next pulsing of P3.2. One step of the task program is executed each time P3.2 is pulsed. /--12 osc. PERIODS ----l I S5 I S6 I S 1 I S2 I S3 I S4 I S5 I S6 I S 1 I S2 I S3 I S4 I S5 I S6 I S1 I S2 I S3 I S4 I RST: 1/////////7 \-.-J '--' C INTERNAL RESET SIGNAL SAMPLE RST SAMPLE RST 1 1 PSEN: po: 1 - 1 1 osc. PERIODS - ......~1------19 osc. PERIODS - - - - - 1 1 270252-33 Figure 25. Reset Timing 3-26 inteL HARDWARE DESCRIPTION OF THE 8051, 8052 AND 80C51 Table 3. Reset Values of the SFRs POWER-ON RESET SFR Name Reset Value For HMOS devices when Vee is turned on an automat- PC OOOOH ic reset can be obtained by connecting the RST pin to ACC OOH Vee through a 10 p.F capacitor and to Vss through an B OOH 8.2 Kn resistor (Figure 26). The CHMOS devices do not require this resistor although its presence does no PSW OOH harm. In fact, for CHMOS devices the external resistor SP 07H can be removed because they have an internal pulldown DPTR on the RST pin. The capacitor value could then be re- OOOOH duced to 1 p.F. PO-P3 FFH IP (8051) XXXOOOOOB When power is turned on, the circuit holds the RST pin IP (8052) high for an amount of time that depends on the capaci- XXOOOOOOB tor value and the rate at which it charges. To ensure a IE (8051) OXXOOOOOB valid reset the RST pin must be held high long enough IE (8052) OXOOOOOOB to allow the oscillator to start up plus two machine TMOD OOH cycles. TCON OOH On power up. Vcc should rise within approximately THO OOH ten milliseconds. The oscillator start-up time will de- TLO pend on the oscillator frequency. For a 10 MHz crystal, OOH the start-up time is typically 1 ms. For a 1 MHz crystal, TH1 OOH the start-up time is typically 10 ms. TL1 OOH TH2 (8052) OOH With the given circuit, reducing Vcc quickly to 0 caus- es the RST pin voltage to momentarily fall below OV. TL2 (8052) OOH However, this voltage is internally limited and will not RCAP2H (8052) OOH harm the device. RCAP2L (8052) OOH NOTE: SCON OOH The port pins will be in a random state until SBUF Indeterminate the oscillator has started and the internal reset PCON (HMOS) OXXXXXXXB algorithm has written Is to them. PCON (CHMOS) OXXXOOOOB Powering up the device without a valid reset could cause the CPU to start executing instructions from an indeterminate location. This is because the SFRs, spe- cifically the Program Counter, may not get properly vee initialized. + POWER-SAVING MODES OF 10u'='= vee I-- OPERATION For applications where power consumption is critical 8051 the CHMOS version provides power reduced modes of operation as a standard feature. The power down mode in HMOS devices is no longer a standard feature and is RST being phased out. 8.2KH CHMOS Power Reduction Modes vss CHMOS versions have two power-reducing modes, ~ Idle and Power Down. The input through which back- 270252-21 up power is supplied during these operations is VCC. Figure 27 shows the internal circuitry which imple- Figure 26. Power on Reset Circuit ments these features. In the Idle mode (lDL = I). the oscillator continues to run and the Interrupt, Serial Port, and Timer blocks continue to be clocked, but the 3-27 intel~ HARDWARE DESCRIPTION OF THE 8051, 8052 AND 80C51 clock signal is gated off to the CPU. In Power Down (MSB) (LSB) (PD = 1), the oscillator is frozen. The Idle and Power Down modes are activated by setting bits in Special SMOD - I - I - I GF1 GFO PD IDL Function Register PCON. The address of this register Symbol Position Name and Function is 87H. Figure 26 details its contents. SMOD PCON.7 Double Baud rate bit. When set to a 1 and Timer 1 is used to generate baud In the HMOS devices the PCON register only contains rate, and the Serial Port is used in SMOD. The other four bits are implemented only in modes 1,2, or 3. the CHMOS devices. User software should never write PCON.6 (Reserved) Is to unimplemented bits, since they may be used in future MCS-51 products. PCON.5 (Reserved) PCON.4 (Reserved) GF1 PCON.3 General-purpose flag bit. IDLE MODE GFO PCON.2 General-purpose flag bit. An instruction that sets PCON.O causes that to be the PD PCON.1 Power Down bit. Setting this bit last instruction executed before going into the Idle activates power down operation. mode. In the Idle mode, the internal clock signal is IDL PCON.O Idle mode bit. Setting this bit activates gated off to the CPU, but not to the Interrupt, Timer, idle mode operation. and Serial Port functions. The CPU status is preserved If 1s are written to PD and IDL at the same time, PD takes in its entirety: the Stack Pointer, Program Counter, precedence. The reset value of PCON is (OXXXOOOO). Program Status Word, Accumulator, and all other reg- In the HMOS devices the PCON register only contains SMOD. isters maintain their data during Idle. The port pins The other four bits are implemented only in the CHMOS devices. hold the logical states they had at the time Idle was User software should never write 1s to unimplemented bits, since they may be used in future MCS-51 products. activated. ALE and PSEN hold at logic high levels. There are two ways to terminate the Idle. Activation of Figure 28. PCON: Power Control Register any enabled interrupt will cause PCON.O to be cleared by hardware, terminating the Idle mode. The interrupt The flag bits GFO and GFI can be used to give an will be serviced, and following RETI the next instruc- indication if an interrupt occurred during normal oper- tion to be executed will be the one following the in- ation or during an Idle. For example, an instruction struction that put the device into Idle. that activates Idle can also set one or both flag bits. When Idle is terminated by an interrupt, the interrupt service routine can examine the flag bits. ~ The other way of terminating the Idle mode is with a hardware reset. Since the clock oscillator is still run- ning, the hardware reset needs to be held active for only XTAL 2 = XTAL 1 two machine cycles (24 oscillator periods) to complete the reset. INTERRUPT. The signal at the RST pin clears the IDL bit directly i-+---C:>SERIAL PORT, and asynchronously. At this time the CPU resumes nMERBLOCKS program execution from where it left off; that is, at the CPU instruction following the one that invoked the Idle Mode. As shown in Figure 25, two or three machine cycles of program execution may take place before the internal reset algorithm takes control. On-chip hard- 270252-22 ware inhibits access to the internal RAM during this Figure 27. Idle and Power Down Hardware time, but access to the port pins is not inhibited. To eliminate the possibility of unexpected outputs at the port pins, the instruction following the one that invokes Idle should not be one that writes to a port pin or to external Data RAM. POWER DOWN MODE An instruction that sets PCON.l causes that to be the last instruction executed before going into the Power Down mode. In the Power Down mode, the on-chip oscillator is stopped. With the clock frozen, all func- 3-28 intel~ HARDWARE DESCRIPTION OF THE 8051,8052 AND 80C51 Table 4. EPROM Versions of the 8051 and 8052 Device EPROM EPROM Ckt Time Required to VPP Name Version Bytes Type Program Entire Array 8051AH 8751H/8751BH 4K HMOS 21.0V/12.75V 4 minutes 80C51BH 87C51 4K CHMOS 12.75V 13 seconds 8052AH 8752BH 8K HMOS 12.75V 26 seconds tions are stopped, but the on-chip RAM and Special Program Memory Locks Function Registers are held. The port pins output the values held by their respective SFRs. ALE and PSEN In some microcontroller applications it is desirable that output lows. the Program Memory be secure from software piracy. Intel has responded to this need by implementing a The only exit from Power Down for the 8OC51 is a Program Memory locking scheme in some of the MCS- hardware reset. Reset redefines all the SFRs, but does 51 devices. While it is impossible for anyone to guaran- not change the on-chip RAM. tee absolute security against all levels of technological sophistication, the Program Memory locks in the MCS- In the Power Down mode of operation, VCC can be 51 devices will present a substantial barrier against ille- reduced to as low as 2V. Care must be taken, however, gal readout of protected software. to ensure that VCC is not reduced before the Power Down mode is invoked, and that VCC is restored to its normal operating level, before the Power Down mode is One Lock Bit Scheme on 8751H terminated. The reset that terminates Power Down also frees the oscillator. The reset should not be activated The 8751H contains a lock bit which, once pro- before VCC is restored to its normal operating level, grammed, denies electrical access by any external means to the on-chip Program Memory. The effect of and must be held active long enough to allow the oscil- lator to restart and stabilize (nonnally less than 10 this lock bit is that while it is programmed the internal msec). Program Memory can not be read out, the device can not be further programmed, and it can not execute ex- ternal Program Memory. Erasing the EPROM array deactivates the lock bit and restores the device's full EPROM VERSIONS functionality. It can then be re-programmed. The EPROM versions of these devices are listed in Ta- ble 4. The 8751H programs at VPP = 21V using one The procedure for programming the lock bit is detailed 50 msec PROG pulse per byte programmed. This re- in the 8751H data sheet. sults in a total programming time (4K bytes) of approx- imately 4 minutes. Two Program Memory Lock Schemes The 8751BH, 8752BH and 87C51 use the faster The 8751BH, 8752BH and 87C51 contain two Program "Quick-Pulse" programmingTM algorithm. These de- Memory locking schemes: Encrypted Verify and Lock vices program at VPP = 12.75V using a series of Bits. twenty-five 100 ,."s PROG pulses per byte programmed. This results in a total programming time of approxi- Encryption Array: Within the EPROM is an array of mately 26 seconds for the 8752BH (8 Kbytes) and encryption bytes that are initially unprogrammed (all 13 seconds for the 87C51 (4 Kbytes). 1's). The user can program the array to encrypt the code bytes during EPROM verification. The verifica- Detailed procedures for programming and verifying tion procedure sequentially XNORs each code byte each device are given in the data sheets. with one of the key bytes. When the last key byte in the array is reached, the verify routine starts over with the first byte of the array for the next code byte. If the key Exposure to Light bytes are unprogrammed, the XNOR process leaves the code byte unchanged. With the key bytes programmed, It is good practice to cover the EPROM window with the code bytes are encrypted and can be read correctly an opaque label when the device is in operation. This is only if the key bytes are known in their proper order. not so much to protect the EPROM array from inad- Table 6 lists the number of encryption bytes available vertent erasure, but to protect the RAM and other on- on the various products. chip logic. Allowing light to impinge on the silicon die while the device is operating can cause logical malfunc- When using the encryption array, one important factor tion. should be considered. If a code byte has the value 3-29 intel~ HARDWARE DESCRIPTION OF THE 8051, 8052 AND 80C51 OFFH, verifying the byte will produce the encryption When Lock Bit 1 is programmed, the logic level at the byte value. If a large block of code is left unpro- EA pin is sampled and latched during reset. If the de- grammed, a verification routine will display the enclyp.. vice -is powered up without a reset, the latch initializes tion array contents. For this reason all unused code to a random value, and holds that value until reset is bytes should be programmed with some value other activated. It is necessary that the latched value of EA than OFFH, and not all of them the same value. This be in agreement with the current logic level at that pin will ensure maximum program protection. in order for the device to function properly. Program Lock Bits: Also included in the Program Lock scheme are Lock Bits which can be enabled to ROM PROTECTION provide varying degrees of protection. Table 5 lists the The 8051AHP and 8OC51BHP are ROM Protected Lock Bits and their corresponding effect on the micro- versions of the 8051AH and 8OC51BH, respectively. To controller. Refer to Table 6 for the Lock Bits available incorporate this Protection Feature, program verifica- on the various products. tion has been disabled and external memory accesses have been limited to 4K. Refer to the data sheets on Erasing the EPROM also erases the Encryption Array these parts for more information. and the Lock Bits, returning the part to full functionali- ty. Table 5. Program Lock Bits and their Features ONCETM Mode Program Lock Bits The ONCE ("on-circuit emulation") mode facilitates Protection Type testing and debugging of systems using the device with- LB1 LB2 LB3 out the device having to be removed from the circuit. 1 U U U No program lock features The ONCE mode is invoked by: enabled. (Code verify will 1. Pull ALE low while the device is in reset and PSEN still be encrypted by the is high; encryption array if 2. Hold ALE low as RST is deactivated. programmed.) 2 p U U MOVC instructions While the device is in ONCE mode, the Port 0 pins go executed from external into a float state, and the other port pins and ALE and program memory are PSEN are weakly pulled high. The oscillator circuit disabled from fetching remains active. While the device is in this mode, an code bytes from internal emulator or test CPU can be used to drive the circuit. memory, EA is sampled Normal operation is restored after a normal reset is and latched on reset, and applied. further programming of the EPROM is disabled. p THE ON-CHIP OSCILLATORS 3 P U Same as 2, also verify is disabled. 4 p P P Same as 3, also external HMOS Versions execution is disabled. The on-chip oscillator circuitry for the HMOS P-Programmed (HMOS-I and HMOS-II) members of the MCS-51 fam- U-Unprogrammed ily is a single stage linear inverter (Figure 29), intended for use as a crystal-controlled, positive reactance oscil- Any other combination of the Lock Bits is not defmed. lator (Figure 30). In this application the crystal is oper- ated in its fundamental response mode as an inductive Table 6. Program Protection reactance in parallel resonance with capacitance exter- Device Lock Bits Encrypt Array nal to the crystal. 8751BH LB1,LB2 32 Bytes 8752BH LB1,LB2 32 Bytes 87C51 LB1,LB2,LB3 64 Bytes 3-30 intel~ HARDWARE DESCRIPTION OF THE 8051, 8052 AND 80C51 XTAL1 if SUBST. 270252-23 Figure 29. On-Chip Oscillator Circuitry in the HMOS Versions of the MCS®-S1 Family In general, crystals used with these devices typically Q2 have the following specifications: TO INTERNAL nMINGCKTS ESR (Equivalent Series Resistance) see Figure 31 Co (Shunt Capacitance) 7;0 pF max. Vss CL (Load Capacitance) 30pF ±3 pF 8051 -------- XTAL1---- XTAU----- Drive Level ImW • ~.-,-- QUARTZ CRYSTAL OR CERAMIC RESONATOR 500 400 270252-24 300 Figure 30. Using the HMOS On-Chip Oscillator 200 The crystal specifications and capacitance values (Cl 100 and C2 in Figure 30) are not critical. 30 pF can be used in these positions at any frequency with good quality crystals. A ceramic resonator can be used in place of 4 8 12 16 the crystal in cost-sensitive applications. When a ce- CRYSTAL FREQUENCY in t.tHz ramic resonator is used, Cl and C2 are normally select- 270252-34 ed to be of somewhat higher values, typically, 47 pF. The manufacturer of the ceramic resonator should be Figure 31. ESR vs Frequency consulted for recommendations on the values of these capacitors. 3-31 HARDWARE DESCRIPTION OF THE 8051, 8052 AND 80C51 Frequency, tolerance and temperature range are deter- CHMOS Versions mined by the system requirements. The on-chip oscillator circuitry for the 8OC5IBH, A more in-depth discussion of crystal specifications, ce- shown in Figure 33, consists of a single stage linear ramic resonators, and the selection of values for C 1 and inverter intended for use as a crystal-controlled, posi- C2 can be found in Application Note AP-155, "Oscilla- tive reactance oscillator in the same manner as the tors for Microcontrollers," which is included in the HMOS parts. However, there are some important dif- Embedded Applications Handbook. ferences. To drive the HMOS parts with an external clock One difference is that the 80C51 BH is able to turn off source, apply the external clock signal to XTAL2, and its oscillator under software control (by writing a 1 to ground XTALl, as shown in Figure 32. A pullup resis- the PD bit in PCON). Another difference is that in the tor may be used (to increase noise margin), but is op- 8OC51BH the internal clocking circuitry is driven by tional if VOH of the driving gate exceeds the VIH MIN the signal at XTALl, whereas in the HMOS versions it specification of XTAL2. is by the signal at XTAL2. The feedback resistor Rr in Figure 33 consists of paral- VCC leled n- and p- channel FETs controlled by the PD bit, such that Rr is opened when PD = 1. The diodes D 1 8051 and D2, which act as clamps to VCC and VSS, are parasitic to the Rr FETs. EXTERNAL OSCILLATOR SIGNAL t">0-.....- - - 1 XTAL2 XTAL1 The oscillator can be used with the same external com- ponents as the HMOS versions, as shown in Figure 34. TTL Typically, Cl = C2 = 30 pF when the feedback ele- GATE VSS ment is a quartz crystal, and Cl = C2 = 47 pF when a WITH TOTEM-POLE ceramic resonator is used. OUTPUT 270252-25 To drive the CHMOS parts with an external clock source, apply the external clock signal to XTALl, and Figure 32. Driving the HMOS MCS@·51 leave XTAL2 float, as shown in Figure 35. Parts with an External Clock Source TO INTERNAL VCC nMINGCKTS D1 4000 XTAL1 XTAL2 D2 PD---~II+-......., 270252-26 Figure 33. On-Chip Oscillator Circuitry in the CHMOS Versions of the MCS@·51 Family 3-32 infel~ HARDWARE DESCRIPTION OF THE 8051, 8052 AND 80C51 VCC TO INTERNAL TIMING CKTS Vss XTAL2------ 8OC51 __---.---QUARTZ CRYSTAL OR CERAMIC RESONATOR 270252-27 Figure 34. Using the CHMOS On-Chip Oscillator 8OC51 INTERNAL TIMING NC XTAL2 Figures 36 through 39 show when the various strobe and port signals are clocked internally. The figures do -::.0.----4 XTAL1 not show rise and fall times of the signals, nor do they show propagation delays between the XTAL signal and Vss events at other pins. CMOS GATE Rise and fall times are dependent on the external load- 270252-28 ing that each pin must drive. They are often taken to be something in the neighborhood of 10 nsec, measured Figure 35. Driving the CHMOS MCS®-51 between O.8V and 2.0V. Parts with an External Clock Source Propagation delays are different for different pins. For The reason for this change from the way the HMOS a given pin they vary with pin loading, temperature, part is driven can be seen by comparing Figures 29 and vce, and manufacturing lot. If the XTAL waveform is 33. In the HMOS devices the internal timing circuits taken as the timing reference, prop delays may vary are driven by the signal at XTAL2. In the CHMOS from 25 to 125 nsec. devices the internal timing circuits are driven by the signal at XTAL 1. The AC Timings section of the data sheets do not refer- ence any timing to the XTAL waveform. Rather, they relate the critical edges of control and input signals to each other. The timings published in the data sheets include the effects of propagation delays under the specified test conditions. 3-33 HARDWARE DESCRIPTION OF THE 8051, 8052 AND 80C51 1STATE 11 STATE 21 STATE 31 STATE 41 STATE 5 ISTATE 6 ISTATE 11STATE 21 l~iP2l~iP2I~IP2I~IP2I~IP2I~I~I~IP2I~IP21 XTAL: ALE: P2: PCHOUT PCHOUT PCH OUT 270252-29 Figure 36. External Program Memory Fetches 41 STATE 51 STATE 61 STATE 1 ISTATE 21 STATE 31 STATE 41 STATE 51 I STATE ~1P2 ~1P2 ~1P2 ~1P2 ~1P2 ~1P2 ~1P2 ~1P2 XTAL: ALE: RD: DATA SAMPLED FLOAT PO: PCH OR PCH OR P2: P2SFR DPH OR P2 SFR OUT P2SFR 270252-30 Figure 37. External Data Memory Read Cycle 3-34 intel4P HARDWARE DESCRIPTION OF THE 8051, 8052 AND 80C51 STATE 41 STATE 51 STATE 61 STATE 1 ISTATE 21 STATE 31 STATE 41 STATE 51 I ~I~ ~I~ ~I~ ~I~ ~I~ ~I~ ~I~ ~I~ XTAL: ALE: WR: PCL OUT IF F-~Y IS EXTERNAL PO: - - - - - I DPL OR RI OUT DATA OUT out ~ P2 PCH OR PCH OR DPH OR P2 SFR OUT P2SFR P2SFR 270252-31 Figure 38. External Data Memory Write Cycle 41 STATE 51 STATE 61 STATE 1 \ STATE 2\ STATE 3\ STATE 41 STATE 5\ I STATE ~1P2 ~1P2 ~1P2 ~1P2 ~1P2 ~1P2 ~1P2 ~I~ XTAL: ~ 'P1'P2,P3 ~PO'P1,P2,P3 INPUTS SAMPLED: RST =rl.-RST MaY PORT, SRC: OLD DATA NEW DATA SERIAL PORT SHIFT CLOCK (IIODEO) -+I I+- RXD PIN SAMPLED RXD SAMPLED -.j ~ 270252-32 Figure 39. Port Operation 3-35 HARDWARE DESCRIPTION OF THE 8051, 8052 AND 80C51 ADDITIONAL REFERENCES The following application notes and articles are found in the Embedded Applications handbook. (Order Number: 270648) 1. AP-125 "Designing Microcontroller Systems for Electrically Noisy Environments". 2. AP-155 "Oscillators for Microcontrollers". 3. AP-252 "Designing with the 80C51BH". 4. AR-517 "Using the 8051 Microcontroller with Resonant Transducers". 3-36 8XC52154158 Hardware 4 Description 8XC52154/58 CONTENTS PAGE HARDWARE DESCRIPTION INTRODUCTION ........................................ 4-3 PIN DESCRIPTION .................................... 4-3 DATA MEMORy ......................................... 4-3 SPECIAL FUNCTION REGISTERS ........... 4-3 TIMER 2 ..................................................... 4-4 CAPTURE MODE ...................................... 4-6 AUTO-RELOAD (Up or Down Counter) ...................................... 4-6 BAUD RATE GENERATOR ........................ 4-8 PROGRAMMABLE CLOCK OUT .............. 4-9 UART .......................................................... 4-9 INTERRUPTS ........................................... 4-11 Interrupt Priority Structure ......................... 4-11 POWER DOWN MODE ............................ 4-12 POWER OFF FLAG ................................. 4-12 Program Memory Lock ............................. 4-12 ONCE MODE ........................................... 4-13 ADDITIONAL REFERENCES .................. 4-13 4-1 int'et 8XC52/54/58 HARDWARE DESCRIPTION INTRODUCTION PIN DESCRIPTION The 8XC52/54/58 is a highly integrated 8-bit micro- The 8XC5X pin-out is the same as the 8OC5l. The only controller based on the MCS®-51 architecture. The key difference is the alternate function of pins PLO and features are an enhanced serial port for multi-processor Pl.l. PLO is the external clock input for Timer 2. Pl.l communications and an up/down timer/counter. As is the Reload/Capture/Direction Control for Timer 2. this product is CHMOS, it has two software selectable reduced power modes: Idle Mode and Power Down Mode. Being a member of the MCS-51 family, the DATA MEMORY 8XC52/54/58 is optimized for control applications. The 8XC5X implements 256 bytes of on-chip RAM. This document presents a comprehensive description of The upper 128 bytes occupy a parallel address space to the on-chip hardware features of the 8XC52/54/58 as the Special Function Registers. That means they have they differ from the 80C5IBH. It begins by describing the same addresses, but they are physically separate how the I/O functions are different and then discusses from SFR space. each of the peripherals as follows: When an instruction accesses an internal location above • 256 Bytes On-Chip RAM address 7FH, the CPU knows whether the access is to • Special Function Registers (SFR) the upper 128 bytes of RAM or the SFR space by the • Timer 2 addressing mode used in the instruction. Instructions - Capture Timer/Counter that use direct addressing access SFR space. For exam- ple, - Up/Down Timer/Counter - Baud Rate Generator MOV OAOH, #data (Direct Addressing) • Full-Duplex Programmable Serial Interface with accesses the SFR at location OAOH (which is P2). In- - Framing Error Detection structions that use indirect addressing access the upper - Automatic Address Recognition 128 bytes of RAM. For example, • 6 Interrupt Sources MOV @RO, #data (Indirect Addressing) • Enhanced Power Down Mode • Power Off Flag where RO contains OAOH, accesses the data byte at ad- dress OAOH, rather than P2 (whose address is OAOH). • ONCE Mode Note that stack operations are examples of indirect ad- dressing, so the upper 128 bytes of data RAM are avail- The 8XC52154/58 uses the standard 8051 instruction set and is pin-for-pin compatible with the existing able as stack space. MCS-51 family of products. Table 1 summarizes the product names and memory differences of the various 8XC52/54/58 products currently available. Through- SPECIAL FUNCTION REGISTERS out this document, the products will generally be re- A map of the on-chip memory area called the Special ferred to as the 8XC5X. Function Register (SFR) space is shown in Table 2. Table 1. 8XC52/54/58 Microcontrollers Note that not all of the addresses are occupied. Unoc- ROM EPROM ROMless ROM/EPROM RAM cupied addresses may not be implemented on the chip. Device Version Version Bytes Bytes Read accesses to these addresses will in general return random data, and write accesses will have an indetermi- 80C52 87C52 80C32 8K 256 nate effect. 80C54 87C54 80C32 16K 256 80C58 87C58 80C32 32K 256 User software should not write Is to these unlisted lo- cations, since they may be used in future MCS-51 prod- For a description of the features that are the same as ucts to invoke new features. In that case the reset or the 80C51, the reader should refer to the MCS-51 Ar- inactive values of the new bits will always be O. chitectural Overview, MCS-51 Programmers Guide/ Instruction Set, and the Hardware Description of the 8OC51 in the Embedded Microcontrollers and Proces- sors Handbook (Order #270645). ' 4-3 Order Number: 270783-004 intet 8XC52/54/58 HARDWARE DESCRIPTION Table 2. 8XC5X SFR Map and Reset Values OF8H OFFH B OFOH OF7H 00000000 OE8H OEFH ACC OEOH OE7H 00000000 OD8H ODFH PSW ODOH OD7H 00000000 T2CON T2MOD RCAP2L RCAP2H TL2 TH2 OCBH OCFH 00000000 XXXXXXOO 00000000 00000000 00000000 00000000 OCOH OC7H IP SADEN OB8H OBFH XOOOOOOO 00000000 P3 IPH OBOH OB7H 11111111 XOOOOOOO IE SADDR OA8H OAFH 00000000 00000000 P2 OAOH OA7H 11111111 SCaN SBUF 98H 9FH 00000000 XXXXXXXX P1 90H 97H 11111111 TCON TMOD TLO TL1 THO TH1 88H 8FH 00000000 00000000 00000000 00000000 00000000 00000000 PO SP DPL DPH PCON 80H 87H 11111111 00000111 00000000 00000000 00000000 Timer Registers-Control and status bits are contained Interrupt Registers-The individual interrupt enable in registers T2CON and T2MOD for Timer 2. The reg- bits are in the IE register. Two priorities can be set for ister pair (RCAP2H, RCAP2L) are the CaptureIRe- each of the 6 interrupt sources in the IP register. The load registers for Timer 2 in 16-bit capture mode or 16- IPH register allows four priorities. bit auto-reload mode. Serial Port Registers-Registers SADDR and SA- TIMER 2 DEN are used to define the Given and the Broadcast addresses for the Automatic Address Recognition fea- Timer 2 is a 16-bit Timer/Counter which can operate ture. either as a timer or an event counter. This is selectable by bit C/T2 in the SFR T2CON (Table 3). It has three 4-4 int:et 8XC52/54/58 HARDWARE DESCRIPTION operating modes: capture, auto-reload (up or down ternal input pin, T2. In this function, the external input counting), and baud rate generator. The modes are se- is sampled during S5P2 of every machine cycle. When lected by bits in T2CON as shown in Table 4. the samples show a high in one cycle and a low in the next cycle, the count is incremented. The new count Timer 2 consists of two 8-bit registers, TH2 and TL2. value appears in the register during 83Pl of the cycle In the Timer function, the TL2 register is incremented following the one in which the transition was detected. every machine cycle. Thus one can think of it as count- Since it takes 2 machine cycles (24 oscillator periods) to ing machine cycles. Since a machine cycle consists of 12 recognize a I-to-O transition, the maximum count rate oscillator periods, the count rate is 1,112 of the oscillator is %4 of the oscillator frequency. To ensure that a given frequency. level is sampled at least once before it changes, it should be held for at least one full machine cycle. In the Counter function, the register is incremented in response to a I-to-O transition at its corresponding ex- Table 3. T2CON-Timer/Counter 2 Control Register T2CON Address = OC8H Reset Value = 0000 OOOOB Bit Addressable Bit 7 6 5 4 3 2 o Symbol Function TF2 Timer 2 overflow flag set by a Timer 2 overflow and must be cleared by software. TF2 will not be set when either RCLK = 1 or TCLK = 1. EXF2 Timer 2 external flag set when either a capture or reload is caused by a negative transition on T2EX and EXEN2 = 1. When Timer 2 interrupt is enabled, EXF2 = 1 will cause the CPU to vector to the Timer 2 interrupt routine. EXF2 must be cleared by software. EXF2 does not cause an interrupt in up/down counter mode (DCEN = 1). RCLK Receive clock enable. When set, causes the serial port to use Timer 2 overflow pulses for its receive clock in serial port Modes 1 and 3. RCLK = 0 causes Timer 1 overflow to be used for the receive clock. TCLK Transmit clock enable. When set, causes the serial port to use Timer 2 overflow pulses for its transmit clock in serial port Modes 1 and 3. TCLK = 0 causes Timer 1 overflows to be used for the transmit clock. EXEN2 Timer 2 external enable. When set, allows a capture or reload to occur as a result of a negative transition on T2EX if Timer 2 is not being used to clock the serial port. EXEN2 = 0 causes Timer 2 to ignore events at T2EX. TR2 Start/Stop control for Timer 2. TR2 = 1 starts the timer. C/T2 Timer or counter select for Timer 2. C/T2 = 0 for timer function. C/T2 = 1 for external event counter (falling edge triggered). CP/RL2 Capture/Reload select. CP /RL2 = 1 causes captures to occur on negative transitions at T2EX if EXEN2 = 1. CP/RL2 = 0 causes automatic reloads to occur when Timer 2 overflows or negative transitions occur at T2EX when EXEN2 = 1. When either RCLK or TCLK = 1, this bit is ignored and the timer is forced to auto-reload on Timer 2 overflow. 4-5 infel~ 8XC52/54/58 HARDWARE DESCRIPTION Table 4. Timer 2 Operating Modes RCLK + TCLK CP/RL2 TR2 MODE 0 0 1 16-Bit Auto-Reload 0 1 1 16-Bit Capture 1 X 1 Baud Rate Generator X X 0 (Off) CAPTURE MODE is invoked by a bit named DCEN (Down Counter En- able) located in the SFR T2MOD (see Table 5). Upon In the capture mode there are two options selected by reset the DCEN bit is set to 0 so that Timer 2 will bit EXEN2 in T2CON. If EXEN2 = 0, Timer 2 is a default to count up. When DCEN is set, Timer 2 can 16-bit timer or counter which upon overflow sets bit count up or down depending on the value of the T2EX TF2 in T2CON. This bit can then be used to generate pin. an interrupt. If EXEN2 = 1, Timer 2 still does the above, but with the added feature that a I-to-O tran- Figure 2 shows Timer 2 automatically counting up sition at external input T2EX causes the current value when DCEN = O. In this mode there are two options in TH2 and TL2 to be captured into RCAP2H and selected by bit EXEN2 in T2CON. If EXEN2 = 0, RCAP2L, respectively. In addition, the transition at Timer 2 counts up to OFFFFH and then sets the TF2 T2EX causes bit EXF2 in T2CON to be set. The EXF2 bit upon overflow. The overflow also causes the timer bit, like TF2, can generate an interrupt. The capture registers to be reloaded with the 16-bit value in mode is illustrated in Figure 1. RCAP2H and RCAP2L. The values in RCAP2H and RCAP2L are preset by software. If EXEN2 = I, a 16-bit reload can be triggered either by an overflow or AUTO-RELOAD (Up or Down Counter) by a I-to-O transition at external input T2EX. This transition also sets the EXF2 bit. Both the TF2 and Timer 2 can be programmed to count up or down when EXF2 bits can generate an interrupt if enabled. configured in its 16-bit auto-reload mode. This feature 0-1 C/T2=1 T2 PIN TRANSITION DETECTION T2EX PIN 270783-1 Figure 1. Timer 2 in Capture Mode 4-6 int"eL 8XC52/54/58 HARDWARE DESCRIPTION Table 5. T2MOo-Timer 2 Mode Control Register T2MOD Address = OC9H Reset Value = XXXX XXOOB Not Bit Addressable T20E DCEN Bit 7 6 5 4 3 2 o Symbol Function Not implemented, reserved for future use. T20E Timer 2 Output Enable bit. DCEN When set, this bit allows Timer 2 to be configured as an up/down counter. 0-1 CjT2=1 OVERFLOW T2 PIN TIMER 2 INTERRUPT TRANSITION DETECTION T2EX PIN 270783-2 Figure 2. Timer 2 Auto Reload Mode (DCEN = 0) (DOWN COUNTING RELOAD VALUE) TOGGLE 0-1 C/f2=1 T2 PIN COUNT DIRECTION 1=UP O=DOWN T2EX PIN .270783-3 Figure 3. Timer 2 Auto Reload Mode (DCEN = 1) 4-7 infel~ 8XC52/54/58 HARDWARE DESCRIPTION P1.0 (T2) - - . , Transition T20£ (T2 ... 00.1) 'LDetector P1.1 (T2EX) ~ + v=-=\Il----+~+---1r:;;lI----.. L:.:J EXEN2 Timer 2 Interrupt 270783-6 Figure 4. Timer 2 in Clock-Out Mode Setting the DCEN bit enables Timer 2 to count up or BAUD RATE GENERATOR down as shown in Figure 3. In this mode the T2EX pin controls the direction of count. A logic 1 at T2EX Timer 2 is selected as the baud rate generator by setting makes Timer 2 count up. The timer will overflow at TCLK and/or RCLK in T2CON (Table 3). Note that OFFFFH and set the TF2 bit. This overflow also causes the baud rates for transmit and receive can be different. the 16-bit value in RCAP2H and RCAP2L to be re- This is accomplished by using Timer 2 for the receiver loaded into the timer registers, TH2 and TL2, respec- or transmitter and using Timer 1 for the other function. tively. Setting RCLK and/or TCLK puts Timer 2 into its baud rate generator mode, as shown in Figure s. A logic 0 at T2EX makes Timer 2 count down. Now the timer underflows when TH2 and TL2 equal the The baud rate generator mode is similar to the auto-re- values stored in RCAP2H and RCAP2L. The under- load mode, in that a rollover in TH2 causes the Timer 2 flow sets the TF2 bit and causes OFFFFH to be reload- registers to be reloaded with the 16-bit value in registers ed into the timer registers. RCAP2H and RCAP2L, which are preset by software. The EXF2 bit toggles whenever Timer 2 overflows or The baud rates in Modes 1 and 3 are determined by underflows. This bit can be used as a 17th bit of resolu- Timer 2's overflow rate as follows: tion if desired. In this operating mode, EXF2 does not flag an interrupt. Timer 2 Overflow Rate Modes 1 and 3 Baud Rates = 16 4-8 8XC52/54/58 HARDWARE DESCRIPTION The Timer can be configured for either "timer" or The Clock-Out frequency depends on the oscillator fre- "counter" operation. In most apE!!cations, it is config- quency and the reload value of Timer 2 capture regis- ured for "timer" operation (CPIf2 = 0). The "timer" ters (RCAP2H, TCAP2L) as shown in this equation: operation is different for Timer 2 when it's being used as a baud rate generator. Normally, as a timer, it incre- I k Oscillator Frequency ments every machine cycle (thUS at Y12 the oscillator C oc -Out Frequency = 4 X (65536 - RCAP2H, RCAP2L) frequency). As a baud rate generator, however, it incre- ments every state time (thUS at Y2 the oscillator fre- In the clock-out mode, Timer 2 roll-overs will not gen- quency). The baud rate formula is given below: erate an interrupt. This is similar to when Timer 2 is used as a baud-rate generator. It is possible to use Tim- Modes 1 and 3 Oscillator Frequency er 2 as a baud-rate generator and a clock generator Baud Rate 32 x [65536 - (RCAP2H, RCAP2L)) simultaneously. Note, however, that the baud-rate and clock-out frequencies can not be determined indepen- where (RCAP2H, RCAP2L) is the content of dently from one another since they both use RCAP2H RCAP2H and RCAP2L taken as a 16-bit unsigned in- and RCAP2L. teger. Timer 2 as a baud rate generator is shown in Figure 5. UART This figure is valid only if RCLK or TCLK = 1 in T2CON. Note that a rollover in TH2 does not set TF2, The UART in the 8XC5X operates identically to the and will not generate an interrupt. Note too, that if UART in the 80C51 except for the following enhance- EXEN2 is set, a I-to-O transition in T2EX will set ments. For a complete understanding of the 8XC5X EXF2 but will not cause a reload from (RCAP2H, UART please refer to the description in the 8OC51 RCAP2L) to (TH2, TL2). Thus when Timer 2 is in use Hardware Description chapter in the Embedded Mi- as a baud rate generator, T2EX can be used as an extra crocontrollers and Processors Handbook. external interrupt, if desired. Framing Error Detection-Framing Error Detection It should be noted that when Timer 2 is running (TR2 allows the serial port to check for valid stop bits in = 1) in "timer" function in the baud rate generator modes 1, 2 or 3. A missing stop bit can be caused, for mode, one should not try to read or write TH2 or TL2. example, by noise on the serial lines, or transmission by Under these conditions the Timer is being incremented two CPUs simultaneously. every state time, and the results of a read or write may not be accurate. The RCAP2 registers may be read, but If a stop bit is missing a Framing Error bit (FE) is set. shouldn't be written to, because a write might overlap a The FE bit can be checked in software after each recep- reload and cause write and/or reload errors. The timer tion to detect communication errors. Once set, the FE should be turned off (clear TR2) before accessing the bit must be cleared in software. A valid stop bit will not Timer 2 or RCAP2 registers. clear FE. The FE bit is located in SCON and shares the same bit PROGRAMMABLE CLOCK OUT address as SMO. Control bit SMODO in the PCON reg- ister (location PCON.6) determines whether the SMO A 50% duty cycle clock can be programmed to come or FE bit is accessed. If SMODO = 0, then accesses to out on P1.0. This pin, besides being a regular I/O pin, SCON.7 are to SMO. If SMODO = 1, then accesses to has two alternate functions. It can be programmed (1) SCON.7 are to FE. to input the external clock for Timer/Counter 2 or (2) to output a 50% duty cycle clock ranging from 61 Hz Automatic Address Recognition-Automatic Address to 4 MHz at a 16 MHz operating frequency. Recognition reduces the CPU time required to service the serial port. Since the CPU is only interrupted when To configure the Timer/Counter 2 as a clock generator, it receives its own address, the software overhead to bit Clf2 (T2CON.l) must be cleared and bit T20E compare addresses is eliminated. With this feature en- (T2MOD.l) must be set. Bit TR2 (T2CON.2) starts abled in one of the 9-bit modes, the Receive Interrupt and stops the timer. (RI) flag will only get set when the received byte corre- sponds to either a Given or Broadcast address. 4-9 8XC52/54/58 HARDWARE DESCRIPTION T!MER 1 OVERrLOW NOTE: OSC. FREQ. 15 DIVIDED BY 2, NOT 12. - - -5MOD1 cr1 C/f2=1 T2 PIN TRANSITION DETECTION T2EX PIN TIMER 2 INTERRUPT 270783-4 Figure 5. Timer 2 in Baud Rate Generator Mode A way to use this feature in multiprocessor systems is A slave's individual address is specified in SADDR. as follows: SADEN is a mask byte that defmes don't-care bits to form the Given Address. These don't-cares allow flexi- When the master processor wants to transmit a block of bility in the user-defined protocol to address one or data to one of several slaves, it first sends out an ad- more slaves at a time. The following is an example of dress byte which identifies the target slave. Remember, how the user could defme Given Addresses to selective- an address byte has its 9th bit set to 1, whereas a data ly address different slaves. byte has its 9th bit set to o. All the slave processors should have their SM2 bits set to 1 so they will only be Slave 1: interrupted by an address byte. The Automatic Address Recognition feature allows only the addressed slave to SADDR 1111 0001 be interrupted. In this mode, the address comparison SADEN 1111 1010 occurs in hardware, not software. (On the 80eSI serial port, an address byte interrupts all slaves for an address GIVEN 11110XOX comparison). Slave 2: The addressed slave then clears its SM2 bit and pre- SADDR 11110011 pares to receive the data bytes that will be coming. The SADEN 11111001 other slaves are unaffected by these data bytes as they are still waiting to receive an address byte. GIVEN 11110XX1 The feature works the same way in the 8-bit mode The SADEN bits are selected such that each slave can (Mode 1) as in the 9-bit modes, except that the stop bit be addressed separately. Notice that bit 0 (LSB) is a takes the place of the 9th data bit. If SM2 is set, the RI don't-care for Slave l's Given Address, but bit 0 = 1 flag is set only if the received byte matches the Given or for Slave 2. Thus, to selectively communicate with just Broadcast Address and is terminated by a valid stop Slave 1 the master must send an address with bit 0 = 0 bit. Setting the SM2 bit has no effect on Mode O. (e.g., 1111 0000). The master can selectively communicate with groups of Similarly, bit 1 = 0 for Slave 1, but is a don't-care for slaves by using the Given Address. Addressing all Slave 2. Now to communicate with just Slave 2 an ad- slaves at once is possible with the Broadcast Address. dress with bit 1 = 1 must be used (e.g., 1111 0111). These addresses are defined for each slave by two Spe- cial Function Registers: SADDR and SADEN. 4-10 8XC52/54/58 HARDWARE DESCRIPTION Finally, for a master to communicate with both slaves rupts (Timers 0, 1 and 2) and the serial port interrupt. at once the address must have bit 0 = 1 and bit 1 = o. These interrupts are all shown in Figure 6. Notice, however, that bit 2 is a don't-care for both slaves. This allows two different addresses to select Timer 2 Interrupt is generated by the logical OR of bits both slaves (1111 0001 or 1111 0101). If a third slave TF2 and EXF2 in register T2CON. Neither of these was added that required its bit 2 = 0, then the latter flags is cleared by hardware when the service routine is address could be used to communicate with Slave 1 and vectored to. In fact, the service routine may have to 2 but not Slave 3. determine whether it was TF2 or EXF2 that generated the interrupt and that bit will have to be cleared in The master can also communicate with all slaves at software. once with the Broadcast Address. It is formed from the logical OR of the SADDR and SADEN registers with The Timer 0 and Timer 1 flags, TFO and TFl, are set at zeroes defined as don't-cares. The don't-cares also al- S5P2 of the cycle in which the timers overflow. The low flexibility in defining the Broadcast Address, but in values are then polled by the circuitry in the next cycle. most applications a Broadcast Address will be OFFH. However, the Timer 2 flag, TF2 is set at S2P2 and is polled in the same cycle in which the timer overflows. SADDR and SADEN are located at address OA9H and OB9H, respectively. On reset, the SADDR and SADEN registers are initialized to OOH which defmes Interrupt Priority Structure the Given and Broadcast Addresses as XXXX XXXX (all don't-cares). This assures the 8XC5X serial port to A second Interrupt Priority register (IPH) has been be backwards compatible with other MCS@-51 prod- added, increasing the number of priority levels to four. ucts which do not implement automatic address recog- Table 6 shows this second register. The added register nition. becomes the MSB of the priority select bits and the existing IP register acts as the LSB. This scheme main- tains compatibility with the rest of the MCS-51 family. INTERRUPTS Table 7 shows the bit values and priority levels associ- ated with each combination. The 8XC5X has a total of 6 interrupt vectors: two ex- ternal interrupts (INTO and INTI), three timer inter- Table 6. IPH: Interrupt Priority High Register IPH Address = OB7H Reset Value = XOOO 0000 PPCH PT2H PSH PT1H PX1H PTOH PXOH Bit 7 6 5 4 3 2 o Symbol Function - Not Implemented, reserved for future use. PPCH PCA interrupt priority high bit. PT2H Timer 2 interrupt priority high bit. PSH Serial Port interrupt priority high bit. PT1H Timer 1 interrupt priority high bit. PX1H External interrupt 1 priOrity high bit. PTOH Timer 0 interrupt priority high bit. PXOH External interrupt priority hi gh bit. 4-11 intel~ 8XC52/54/58 HARDWARE DESCRIPTION Table 70 Priority Level Bit Values With an external interrupt, INTO or INTI must be en- abled and configured as level-sensitive before entering l""i'iOfit'j oilS Interrupt Priority Level Power Down. Holding the pin low restarts the oscilla- IPHox IPox tor and bringing the pin back high completes the exit. After the RETI instruction is executed in the interrupt 0 0 Level 0 (Lowest) service routine, the next instruction will be the one fol- 0 1 Level 1 lowing the instruction that put the device in Power Down. 1 0 Level 2 1 1 Level 3 (Highest) POWER OFF FLAG The Power Off Flag (pOp) located at PCON.4 is set by POWER DOWN MODE hardware when Vee rises from 0 to approximately 5V. POF can also be set or cleared by software. This allows The 8XC5X can exit Power Down with either a hard- the user to distinguish between a "cold start" reset and ware reset or external interrupt. Reset redefines all the a "warm start" reset. SFRs but does not change the on-chip RAM. An exter- nal interrupt allows both the SFRs (except PD in A cold start reset is one that is coincident with Vee PCON) and the on-chip RAM to retain their values. being turned on to the device after it was turned off. A warm start reset occurs while Vee is still applied to the device and could be generated, for example, by an exit from Power Down. Immediately after reset, the user's software can check the status of the POF bit. POF = 1 would indicate a cold start. The software then clears POF and com- mences its tasks. POF = 0 immediately after reset would indicate a warm start. ITO-------------------------+ Vee must remain above 3V for POF to retain a o. Program Memory Lock In some microcontroller applications it is desirable that the Program Memory be secure from software piracy. The 8XC5X has varying degrees of program protection depending on the device. Table 8 outlines the lock schemes available for each device. TF1 Encryption Array: Within the EPROMIROM is an ar- ray of encryption bytes that are initially unprogrammed TI RI D (all l's). For EPROM devices, the user can program the encryption array to encrypt the program code bytes during EPROM verification. For ROM devices, the IT2 EXF2 D 270783-5 user submits the encryption array to be programmed by the factory. If an encryption array is submitted, LBt will also be programmed by the factory. The encryption array is not available without the Lock Bit. Program Figure 60 Interrupt Sources code verification is performed as usual except that each code byte comes out exclusive-NOR'ed (XNOR) with To properly terminate Power Down the reset or exter- one ,-of the key bytes. Therefore, to read the nal interrupt should not be applied before Vee is re- ROMIEPROM code, the user has to know the encryp- stored to its normal operating level and must be held tion key bytes in their proper sequence. active long enough for the oscillator to restart and sta- bilize (normally less than 10 msec). Unprogrammed bytes have the value OFFH. If the En- cryption Array is left unprogrammed, all the key bytes have the value OFFH. Since any code byte XNOR'ed 4-12 infel4P 8XC52/54/58 HARDWARE DESCRIPTION with OFFH leaves the byte unchanged, leaving the En- ONCE MODE cryption Array unprogrammed in effect bypasses the encryption feature. The ON-Circuit Emulation (ONCE) mode facilitates testing and debugging of systems using the 8XC5X Program Lock Bits: Also included in the Program without having to remove the device from the circuit. Lock scheme are Lock Bits which can be enabled to The ONCE mode is invoked by either: provide varying degrees of protection. Table 9 lists the 1. Pulling ALE low while the device is in reset and Lock Bits and their corresponding influence on the mi- PSEN is high; crocontroller. Refer to Table 8 for the Lock Bits avail- able on the various products. The user is responsible for 2. Holding ALE low as RESET is deactivated. programming the Lock Bits on EPROM devices. On ROM devices, LBI is automatically set by the factory While the device is in ONCE mode, the Port 0 pins go when the encryption array is submitted. The Lock Bit into a float state, and the other port pins, ALE, and is not available without the encryption array on ROM PSEN are weakly pulled high. The oscillator circuit devices. remains active. While the device is in this mode, an emulator or test CPU can be used to drive the circuit. Erasing the EPROM also erases the Encryption Array and the Lock Bits, returning the part to full functionali- Normal operation is restored after a valid reset is ap- ty. plied. Table 8. Program Protection ADDITIONAL REFERENCES Device Lock Bits Encrypt Array 80C52 LB1 64 Bytes The following application notes provide supplemental information to this document and can be found in the 80C54 LB1 64 Bytes Embedded Applications handbook (Order No. 270648). 80C58 LB1 64 Bytes 87C52 LB1,LB2,LB3 64 Bytes 1. AP-125 "Designing Microcontroller Systems for Electrically Noisy Environments" 87C54 LB1,LB2,LB3 64 Bytes 87C58 LB1,LB2,LB3 64 Bytes 2. AP-155 "Oscillators for Microcontrollers" 3. AP-252 "Designing with the 8OC51BH" 4. AP-4lO "Enhanced Serial Port on the 83C51FA" Table 9. Lock Bits Program Lock Bits Protection Type LB1 LB2 LB3 1 U U U No program lock features enabled. (Code verify will still be encrypted by the encryption array if programmed.) 2 P U U MOVe instructions executed from external program memory are disabled from fetching code bytes from internal memory, EA is sampled and latched on reset, and further programming of the EPROM is disabled. 3 P P U Same as 2, also verify is disabled. 4 P P P Same as 3, also external execution is disabled. P = Programmed U = Unprogrammed Any other combination of Lock Bits is not defined. 4-13 8XC51FX Hardware 5 Description HARDWARE DESCRIPTION OF THE 8XC51 FX CONTENTS PAGE CONTENTS PAGE 1.0 INTRODUCTION .................................. 5-3 7.4 Baud Rates .................................... 5-30 7.5 Using Timer 1 to Generate Baud 2.0 MEMORy .............................................. 5-3 Rates ................................................ 5-30 2.1 Program Memory ............................. 5-3 7.6 Using Timer 2 to Generate Baud 2.2 Data Memory ................................... 5-3 Rates ................................................ 5-30 3.0 SPECIAL FUNCTION 8.0 INTERRUPTS ..................................... 5-32 REGISTERS ........................................... 5-4 8.1 External Interrupts ......................... 5-33 4.0 PORT STRUCTURES AND 8.2 Timer Interrupts .............................. 5-33 OPERATION ........................................... 5-7 8.3 PCA Interrupt.. ............................... 5-33 4.1 I/O Configurations ............................ 5-7 8.4 Serial Port Interrupt.. ...................... 5-33 4.2 Writing to a Port ............................... 5-8 8.5 Interrupt Enable ............................. 5-33 4.3 Port Loading and Interfacing .......... 5-10 8.6 Priority Level Structure .................. 5-33 4.4 Read-Modify-Write Feature ............ 5-10 8.7 Response Time .............................. 5-37 4.5 Accessing External Memory .......... 5-10 9.0 RESET ................................................ 5-37 5.0 TIMERS/COUNTERS ......................... 5-12 9.1 Power-On Reset ............................ 5-38 5.1 TIMER 0 AND TIMER 1 ................. 5-12 10.0 POWER-SAVING MODES OF 5.2 TIMER 2 ......................................... 5-15 OPERATION ......................................... 5-38 6.0 PROGRAMMABLE COUNTER 10.1 Idle Mode ..................................... 5-38 ARRAy .................................................. 5-18 10.2 Power Down Mode ...................... 5-40 6.1 PCA 16-Bit Timer/Counter ............. 5-20 10.3 Power Off Flag ............................. 5-40 6.2 Capture/Compare Modules ............ 5-22 11.0 EPROM VERSIONS ......................... 5-40 6.3 16-Bit Capture Mode ...................... 5-24 6.4 16-Bit Software Timer Mode .......... 5-24 12.0 PROGRAM MEMORY LOCK ........... 5-40 6.5 High Speed Output Mode .............. 5-25 13.0 ONCE MODE ................................... 5-41 6.6 Watchdog Timer Mode ................... 5-25 6.7 Pulse Width Modulator Mode ......... 5-26 14.0 ON-CHIP OSCiLLATOR ................... 5-42 7.0 SERIAL INTERFACE ......................... 5-27 15.0 CPU TIMING .................................... 5-43 7.1 Framing Error Detection ................ 5-28 7.2 Multiprocessor Communications .... 5-28 7.3 Automatic Address Recognition ..... 5-28 5-1 intel~ 8XC51 FX HARDWARE DESCRIPTION 1.0 INTRODUCTION Table 1. C51FX Family of Microcontrollers ROM! The 8XCSIFX is a highly integrated 8-bit microcon- ROM EPROM ROMless RAM troller based on the MCS-51 architecture. As a member EPROM Device Version Version Bytes of the MCS-51 family, the 8XCSIFX is optimized for Bytes control applications. Its key feature is the programma- 83C51FA 87C51FA 80C51FA 8K 256 ble counter array (PCA) which is capable of measuring and generating pulse information on five I/O pins. Also 83C51FB 87C51FB 80C51FA 16K 256 included are an enhanced serial port for multi-proces- 83C51FC 87C51FC 80C51FA 32K 256 sor communications, an up/down timer/counter, and a program lock scheme for the on-chip program memory. Since the 8XCSIFX products are CHMOS, they have two software selectable reduced power modes: Idle 2.0 MEMORY ORGANIZATION Mode and Power Down Mode. All MCS-Sl devices have a separate address space for The 8XCSIFX uses the standard 80S 1 instruction set Program and Data Memory. Up to 64 Kbytes each of and is pin-for-pin compatible with the existing MCS-Sl external Program and Data Memory can be addressed. family of products. This document presents a comprehensive description of 2.1 Program Memory the on-chip hardware features of the 8XCSIFX. It be- gins with a discussion of the on-chip memory and then If the EA pin is connected to Vss, all program fetches discusses each of the peripherals listed below. are directed to external memory. On the 83CSIFA (or 87CSIFA), if the EA pin is connected to Vee, then Please note that 8XCSIFX does not include the program fetches to addresses OOOOH through 1FFFH 80CSIFA and 83CSIFA. Therefore, these devices do are directed to internal ROM and fetches to addresses not have some of the features found on the 8XCSIFX. 2000H through FFFFH are to external memory. These features are: programmable clock out, four level interrupt priority structure, enhanced program lock On the 83CSIFB (or 87CSIFB) if EA is connected to scheme and asynchronous port reset. VCC, program fetches to addresses OOOOH through 3FFFH are directed to internal ROM, and fetches to • Four 8-Bit Bidirectional Parallel Ports addresses 4000H through FFFFH are to external mem- • Three 16-Bit Timer/Counters with ory. - One Up/Down Timer/Counter On the 83CSIFC (or 87CSIFC) if EA is connected to - Clock Out Vce, program fetches to addresses OOOOH through • Programmable Counter Array with 7FFFH are directed to internal ROM or EPROM and - Compare/Capture fetches to addresses 8000H through FFFFH are to ex- ternal memory. - Software Timer - High Speed Output - Pulse Width Modulator 2.2 Data Memory - Watchdog Timer The CSIFX implements 256 bytes of on-chip data • Full-Duplex Programmable Serial Port with RAM. The upper 128 bytes occupy a parallel address space to the Special Function Registers. That means - Framing Error Detection they have the same addresses, but are physically sepa- - Automatic Address Recognition rate from SFR space. • Interrupt Structure with When an instruction accesses an intemallocation above - Seven Interrupt Sources address 7FH, the CPU knows whether the access is to - Four Priority Levels the upper 128 bytes of data RAM or to SFR space by • Power-Saving Modes the addressing mode used in the instruction. Instruc- tions that use direct addressing access SFR space. For - Idle Mode example: - Power Down Mode MOV OAOH,#data Table 1 summarizes the product names and memory differences of the various 8XCSIFX products currently available. Throughout this document, the products will generally be referred to as the CSIFX. 5-3 intel~ 8XC51FX HARDWARE DESCRIPTION PO.O-PO.7 PSfN TIMING ALE/Pmm EljVPP RST 270653-1 Figure 1. 8XC51FX Functional Block Diagram accesses the SFR at location OAOH (which is P2). In- 3.0 SPECIAL FUNCTION REGISTERS structions that use indirect addressing access the upper 128 bytes of data RAM. For example: A map of the on-chip memory area called the SFR (Special Function Register) space is shown in Table 2. MOV@RO,#data Note that not all of the addresses are occupied. Unoc- where RO contains OAOH, accesses the data byte at ad- cupied addresses are not implemented on the chip. dress OAOH, rather than P2 (whose address is OAOH). Read accesses to these addresses will in general return Note that stack operations are examples of indirect ad- random data, and write accesses will have no effect. dressing, so the upper 128 bytes of data RAM are avail- able as stack space. 5-4 8XC51FX HARDWARE DESCRIPTION User software should not write Is to these unimple- The functions of the SFRs are outlined below. More mented locations, since they may be used in future information on the use of specific SFRs for each periph- MCS-51 products to invoke new features. In that case eral is included in the description of that peripheral. the reset or inactive values of the new bits will always be 0, and their active values will be 1. Accumulator: ACC is the Accumulator register. The mnemonics for Accumulator-Specific instructions, however, refer to the Accumulator simply as A. Table 2. SFR Mapping and Reset Values F8 CH CCAPOH CCAP1H CCAP2H CCAP3H CCAP4H FF 00000000 XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX FO *8 F7 00000000 E8 CL CCAPOL CCAP1L CCAP2L CCAP3L CCAP4L EF 00000000 XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX EO * ACC E7 00000000 D8 CCON CMOD CCAPMO CCAPM1 CCAPM2 CCAPM3 CCAPM4 DF OOXOOOOO OOXXXOOO XOOOOOOO XOOOOOOO XOOOOOOO XOOOOOOO XOOOOOOO DO * PSW D7 00000000 C8 T2CON T2MOD RCAP2L RCAP2H TL2 TH2 CF 00000000 XXXXXXOO 00000000 00000000 00000000 00000000 co C7 B8 * IP SADEN BF XOOOOOOO 00000000 80 * P3 IPH B7 11111111 XOOOOOOO A8 * IE SADDR AF 00000000 00000000 AO * P2 A7 11111111 98 * SCON * SBUF 9F 00000000 XXXXXXXX 90 * P1 97 11111111 88 * TCON *TMOD *TLO * TL1 * THO * TH1 8F 00000000 00000000 00000000 00000000 00000000 00000000 80 * PO * SP * DPL * DPH * PCON ** 87 11111111 00000111 00000000 00000000 OOXXOOOO * = Found in the 8051 core (See 8051 Hardware DeSCription for explanations of these SFRs). ** = See description of PCON SFR. Bit PCON.4 is not affected by reset. X = Undefined. 5-5 intel~ 8XC51FX HARDWARE DESCRIPTION Table 3. PSW: Program Status Word Register PSW Address = ODOH Reset Value = 0000 OOOOB Bit Addressable CY AC FO RS1 RSO OV P Bit 7 6 5 4 3 2 o Symbol Function CY Carry flag. AC Auxiliary Carry flag. (For BCD Operations) FO Flag o. (Available to the user for general purposes). RS1 Register bank select bit 1. RSO Register bank select bit o. RS1 RSO Working Register Bank and Address o 0 Bank 0 (00H-07H) o 1 Bank 1 (OSH-OFH) 1 0 Bank 2 (10H-17H) 1 1 Bank 3 (1SH-1 FH) OV Overflow flag. User definable flag. P Parity flag. Set! cleared by hardware each instruction cycle to indicate an odd/even number of "one" bits in the Accumulator, i.e., even parity. B Register: The B register is used during multiply and RCAP2L) are the capture/reload registers for Timer 2 divide operations. For other instructions it can be treat- in 16-bit capture mode or 16-bit auto-reload mode. ed as another scratch pad register. Programmable Counter Array (PCA) Registers: The Stack Pointer: The Stack Pointer Register is 8 bits 16-bit PCA timer/counter consists of registers CH and wide. It is incremented before data is stored during CL. Registers CCON and CMOD contain the control PUSH and CALL executions. The stack may reside and status bits for the PCA. The CCAPMn (n = 0, I, anywhere in on-chip RAM. On reset, the Stack Pointer 2, 3, or 4) registers control the mode for each of the five is initialized to 07H causing the stack to begin at loca- PCA modules. The register pairs (CCAPnH, CCAPnL) tion 08H. are the 16-bit compare/capture registers for each PCA module. Data Pointer: The Data Pointer (DPTR) consists of a high byte (DPH) and a low byte (DPL). Its ,intended Serial Port Registers: The Serial Data Buffer, SBUF, function is to hold a 16-bit address, but it may be ma- is actually two separate registers: a transmit buffer and nipulated as a 16-bit register or as two independent a receive buffer register. When data is moved to SBUF, 8-bit registers. it goes to the transmit buffer where it is held for serial transmission. (Moving a byte to SBUF initiates the Program Status Word: The PSW register contains pro- transmission). When data is moved from SBUF, it gram status information as detailed in Table 3. comes from the receive buffer. Register SCON contains the control and status bits for the Serial Port. Registers Ports 0 to 3 Registers: PO, PI, P2, and P3 are the SFR SADDR and SADEN are used to defme the Given and latches of Port 0, Port I, Port 2, and Port 3 respective- the Broadcast addresses for the Automatic Address ly. Recognition feature. Timer Registers: Register pairs (THO, TLO), (THI, Interrupt Registers: The individual interrupt enable TLI), and (TH2, TL2) are the 16-bit count registers for bits are in the IE register. Two priorities can be set for Timer/Counters 0, I, and 2 respectively. Control and each of the 7 interrupts in the IP register. status bits are contained in registers TCON and TMOD for Timers 0 and I and in registers T2CON and Power Control Register: PCON controls the Power T2MOD for Timer 2. The register pair (RCAP2H, Reduction Modes. Idle and Power Down Modes. 5-6 infel~ 8XC51FX HARDWARE DESCRIPTION 4.0 PORT STRUCTURES AND Table 4. Alternate Port Functions OPERATION Port Pin Alternate Function All four ports in the C51FX are bidirectional. Each consists of a latch (Special Function Registers PO PO.O/ADO- Multiplexed Byte of AddresslData for through P3), an output driver, and an input buffer. PO. 7/AD7 External Memory P1.0/T2 Timer 2 External Clock Input/Clock- The output drivers of Ports 0 and 2, and the input buff- Out ers of Port 0, are used in accesses to external memory. P1.1/T2EX Timer 2 Reload/CapturelDirection In this application, Port 0 outputs the low byte of the Control external memory address, time-multiplexed with the byte being written or read. Port 2 outputs the high byte Pl.2/ECI PCA External Clock Input of the external memory address when the address is P1.3/CEXO PCA Module 0 Capture Input, 16 bits wide. Otherwise the Port 2 pins continue to emit Compare/PWM Output the P2 SFR content. P1.4/CEXI PCA Module 1 Capture Input, Compare/PWM Output All the Port 1 and Port 3 pins are multifunctional. They are not only port pins, but also serve the functions P1.5/CEX2 PCA Module 2 Capture Input, of various special features as listed in Table 4. Compare/PWM Output P1.6/CEX3 PCA Module 3 Capture Input, The alternate functions can only be activated if the cor- Compare/PWM Output responding bit latch in the port SFR contains a 1. Oth- P1.7/CEX4 PCA Module 4 Capture Input, erwise the port pin is stuck at O. . Compare/PWM Output P2.0/AS- High Byte of Address for External 4.1 1/0 Configurations P2.7/AI5 Memory Figure 2 shows a functional diagram of a typical bit P3.0/RXD Serial Port Input latch and I/O buffer in each of the four ports. The bit latch (one bit in the port's SFR) is represented as a P3.1/TXD Serial Port Output Type D flip-flop, which clocks in a value from the in- P3.2/INTO External Interrupt 0 ternal bus in response to a "write to latch" signal from P3.3/INT External Interrupt 1 the CPU. The Q output of the flip-flop is placed on the internal bus in response to a "read latch" signal from P3.4/T0 Timer 0 External Clock Input the CPU. The level of the port pin itself is placed on the P3.5/Tl Timer 1 External Clock Input internal bus in response to a "read pin" signal from the P3.6/WR Write Strobe for External Memory CPU. Some instructions that read a port activate the "read latch" signal, and others activate the "read pin" P3.7/RD Read Strobe for External Memory signal. See the Read-Modify-Write Feature section. As shown in Figure 2, the output drivers of Ports 0 and 2 are switchable to an internal ADDRESS and AD- DRESSIDATA bus by an internal CONTROL signal for use in external memory accesses. During external memory accesses, the P2 SFR remains unchanged, but the PO SFR gets Is written to it. 5-7 intel~ 8XC51FX HARDWARE DESCRIPTION A. Port 0 Bit B. Port 1 or Port 3 Bit ALTERNATE OUTPUT READ FUNCTION LATCH INT. B;..;U;..;;S_-+-~ WRITE TO _ _rl·\.......II LATCH WRITE TO LATCH READ PIN 270653-2 ALTERNATE INPUT FUNCTION 270653-4 C. Port 2 Bit ADDR VCC CONTROL READ LATCH INT. BUS WRITE TO LATCH 270653-3 ·See Figure 4 for details of the internal pullup Figure 2. C51FX Port Bit Latches and 1/0 Buffers Also shown in Figure 2 is that if a PI or P3 latch When configured as inputs they pull high and will contains aI, then the output level is controlled by the source current (IlL in the data sheets) when externally signal labeled "alternate output function." The actual pulled low. Port 0, on the other hand, is considered pin level is always available to the pin's alternate input "true" bidirectional, because it floats when configured function, if any. as an input. Ports 1,2, and 3 have internal pullups. Port 0 has open All the port latches have Is written to them by the reset drain outputs. Each I/O line can be independently used function. If a 0 is subsequently written to a port latch, it as an input or an output (ports 0 and 2 may not be used can be reconfigured as an input by writing a 1 to it. as general purpose I/O when being used as the AD- DRESSIDATA BUS). To be used as an input, the port bit latch must contain a 1, which turns off the output 4.2 Writing to a Port driver FET. On Ports 1,2, and 3, the pin is pulled high by the internal pullup, but can be pulled low by an In the execution of an instruction that changes the external source. value in a port latch, the new value arrives at the latch during State 6 Phase 2 of the final cycle of the instruc- Port 0 differs from the other ports in not having inter- tion. However, port latches are in fact sampled by their nal pullups. The pullup FET in the PO output driver output buffers only during Phase 1 of any clock period. (see Figure 2) is used only when the Port is emitting Is (During Phase 2 the output buffer holds the value it during external memory accesses. Otherwise the pullup saw during the previous Phase 1). Consequently, the FET is off. Consequently PO lines that are being used as new value in the port latch won't actually appear at the output port lines are open drain. Writing a 1 to the bit output pin until the next Phase 1, which will be at SIPl latch leaves both output FETs off, which floats the pin of the next machine cycle. Refer to Figure 3. For more and allows it to be used as a high-impedance input. information on internal timings refer to the CPU Tim- Because Ports 1 through 3 have fixed internal pull ups ing section. they are sometimes call "quasi-bidirectional" ports. 5-8 infel~ 8XC51FX HARDWARE DESCRIPTION STATE 41 STATE 51 STATE 61 STATE 1 ISTATE 21 STATE 31 STATE 41 STATE 51 I~I~ ~I~ ~I~ ~I~ ~I~ ~I~ ~I~ ~I~ XTAL1: ~PO,P1,P2,P3 PO'P1,P2,P3~ ~RST RST~ INPUTS SAMPLED: MOV PORT, SRC: OLD DATA NEWOATA SERIAL PORT SHIFT CLOCK (MOOED) -.f ~ RXD PIN SAMPLED RXD SAMPLED -.j I+- 270653-33 Figure 3. Port Operation If the change requires a O-to-l transition in Ports 1, 2, pFET 1 in is the transistor that is turned on for 2 oscil- and 3, an additional pullup is turned on during SIPl lator periods after a O-to-l transition in the port latch. and S1P2 of the cycle in which the transition occurs. A 1 at the port pin turns on pFET3 (a weak pull-up), This is done to increase the transition speed. The extra through the invertor. This invertor and pFET form a pullup can source about 100 times the current that the latch which hold the 1. normal pullup can. The internal pullups are field-effect transistors, not linear resistors. The pull-up arrange- If the pin is emitting a 1, a negative glitch on the pin ments are shown in Figure 4. from some external source can tum off pFET3, causing the pin to go into a float state. pFET2 is a very weak The pullup consists of three pFETs. Note that an pullup which is on whenever the nFET is off, in tradi- n-channel FET (nFET) is turned on when a logical 1 is tional CMOS style. It's only about '110 the strength of applied to its gate, and is turned off when a logical 0 is pFET3. Its function is to restore a 1 to the pin in the applied to its gate. A p-channel FET (pFET) is the event the pin had a 1 and lost it to a glitch. opposite: it is on when its gate sees a 0, and off when its gate sees a 1. Vcc Vcc Vcc o FROM PORT LATCH INPUT CJ---IC DATA READ PORT PIN 270653-5 CHMOS Configuration. pFET 1 is turned on for 2 OSC. periods after Q makes a O-to-1 transition. During this time, pFET 1 also turns on pFET 3 through the inverter to form a latch which holds the 1. pFET 2 is also on. Port 2 is similar except that it holds the strong pullup on while emitting 1s that are address bits. (See text, "Accessing External Memory".) Figure 4. Ports 1 and 3 Internal Pullup Configurations 5-9 8XC51FX HARDWARE DESCRIPTION 4.3 Port Loading and Interfacing DJNZ (decrement and jump if not zero, e.g., DJNZ P3, LABEL) The output buffers of Ports I, 2, and 3 can each sink MOV, PX.Y, C (move carry bit to bit Y of Port X) 1.6 rnA at 0.45 V. These port pins can be driven by open-collector and open-drain outputs although O-to-I CLR PX.Y (clear bit Y of Port X) transitions will not be fast since there is little current SETBPX.Y (set bit Y of Port X) pulling the pin up. An input 0 turns off pullup pFET3, leaving only the very weak pullup pFET2 to drive the It is not obvious that the last three instructions in this transition. list are read-modify-write instructions, but they are. They read the port byte, all 8 bits, modify the addressed In external bus mode, Port 0 output buffers can each bit, then write the new byte back to the latch. sink 3.2 rnA at 0.45 V. However, as port pins they require external pullups to be able to drive any inputs. The reason that read-modify-write instructions are di- rected to the latch rather than the pin is to avoid a See the latest revision of the data sheet for design-in possible misinterpretation of the voltage level at the information. pin. For example, a port bit might be used to drive the base of a transistor. When a I is written to the bit, the transistor is turned on. If the CPU then reads the same 4.4 Read-Modify-Write Feature port bit at the pin rather than the latch, it will read the Some instructions that read a port read the latch and base voltage of the transistor and interpret it as a O. others read the pin. Which ones do which? The instruc- Reading the latch rather than the pin will return the tions that read the latch rather than the pin are the ones correct value of 1. that read a value, possibly change it, and then rewrite it to the latch. These are called "read-modify-write" in- structions. Listed below are the read-modify-write in- 4.5 Accessing External Memory structions. When the destination operand is a port, or a Accesses to external memory are of two types: accesses port bit, these instructions read the latch rather than to external Program Memory and accesses to external the pin: Data Memory. Accesses to external Program Memory ANL (logical AND, e.g., ANL PI, A) use signal PSEN (program store enable) as the' read strobe. Accesses to external Data Memory use RD or ORL (logical OR, e.g., ORL P2, A) WR (alternate functions ofP3.7 and P3.6) to strobe the XRL (logical EX-OR, e.g., XRL P3, A) memory. Refer to Figures 5 through 7. JBC (jump if bit = I and clear bit, e.g., mc Pl.I, LABEL) Fetches from external Program Memory always use a 16-bit address. Accesses to external Data Memory can CPL (complement bit, e.g., CPL P3.0) use either a 16-bit address (MOVX @ DPTR) or an INC (increment, e.g., INC P2) 8-bit address (MOVX @ Ri). DEC (decrement, e.g., DEC P2) 11 STATE 21 STATE 31 STATE 4/ STATE 5 ISTATE 61 STATE 1ISTATE 21 I STATE ~I~~I~ ~I~ ~I~ ~I~ ~I~ ~I~ ~I~ XTAL1: ALE: PSEN:~ -.I DATA ~SAMPLED L PO: ~: PCH OUT PCH OUT PCHOUT 270653-30 Figure 5. External Program Memory Fetches 5-10 int:et 8XC51FX HARDWARE DESCRIPTION 41 STATE 51 61 STATE I 21 31 41 51 STATE STATE 1 STATE STATE STATE STATE I ~I~ ~I~ ~I~ ~I~ ~I~ ~I~ ~I~ ~I~ XTAL1: ALE: Ro: PO: PCH OR PCH OR ~: DPH OR P2 SFR OUT ~SFR ~SFR 270653-31 Figure 6. External Data Memory Read Cycle I~I~41 ~I~51 ~I~61 ~I~ I~I~21 ~I~31 ~I~41 ~I~51 STATE STATE STATE STATE 1 STATE STATE STATE STATE XTAL1: ALE: WR: PCL OUT IF ~--- IS EXTERNAL PO: DPLOR RI OUT DATA OUT ~~ ~ ~ PCH OR PCHOR DPH OR ~ SFR OUT ~SFR ~SFR 270653-32 Figure 7. External Data Memory Write Cycle 5-11 intet 8XC51 FX HARDWARE DESCRIPTION Whenever a 16-bit address is used, the high byte of the 5.0 TIMERS/COUNTERS address comes out on Port 2, where it is held for the duration of the read or write cycle. The Port 2 drivers The C51FX has three 16-bit Timer/Counters: Timer 0, use the strong pullups during the entire time that they Timer 1, and Timer 2. Each consists of two 8-bit regis- are emitting address bits that are Is. This occurs when ters, THx and TLx, (x = 0, 1, and 2). All three can be the MOVX @ DPTR instruction is executed. During configured to operate either as timers or event counters. this time the Port 2 latch (the Special Function Regis- ter) does not have to contain Is, and the contents of the In the Timer function, the TLx register is incremented Port 2 SFR are not modified. If the external memory every machine cycle. Thus one can think of it as count- cycle is not immediately followed by another external ing machine cycles. Since a machine cycle consists of 12 memory cycle,· the undisturbed contents of the Port 2 oscillator periods, the count rate is 1112 of the oscilla- SFR will reappear in the next cycle. tor frequency. If an 8-bit address is being used (MOVX @ Ri), the In the Counter function, the register is incremented in contents of the Port 2 SFR remain at the Port 2 pins response to a I-to-O transition at its corresponding ex- throughout the external memory cycle. In this case, ternal input pin-TO, Tl, or T2. In this function, the Port 2 pins can be used to page the external data mem- external input is sampled during S5P2 of every machine ory. cycle. When the samples show a high in one cycle and a low in the next cycle, the count is incremented. The In either case, the low byte of the address is time-multi- new count value appears in the register during S3Pl of plexed with the data byte on Port O. The ADDRESS/ the cycle following the one in which the transition was DATA signal drives both FETs in the Port 0 output detected. Since it takes 2 machine cycles (24 oscillator butTers. Thus, in external bus mode the Port 0 pins are periods) to recognize a I-to-O transition, the maximum not open-drain outputs and do not require external count rate is ~4 of the oscillator frequency. There are pullups. The ALE (Address Latch Enable) signal no restrictions on the duty cycle of the external input should be used to capture the address byte into an ex- signal, but to ensure that a given level is sampled at ternal latch. The address byte is valid at the negative least once before it changes, it should be held for at transition of ALE. Then, in a write cycle, the data byte least one full machine cycle. to be written appears on Port 0 just before WR is acti- vated, and remains there until after WR is deactivated. In addition to the Timer or Counter selection, Timer 0 In a read cycle, the incomin~e is accepted at Port 0 and Timer 1 have four operating modes from which to just before the read strobe (RD) is deactivated. select: Modes 0 - 3. Timer 2 has three modes of opera- tion: Capture, Auto-Reload, and Baud Rate Generator. During any access to external memory, the CPU writes OFFH to the Port 0 latch (the Special Function Regis- ter), thus obliterating the information in the Port 0 5.1 Timer 0 and Timer 1 SFR. Also, a MOV PO instruction must not take place during external memory accesses. If the user writes to The Timer or Counter function is selected by control Port 0 during an external memory fetch, the incoming bits cif in the Special Function Register TMOD (Ta- code byte is corrupted. Therefore, do not write to Port ble 5). These two Timer/Counters have four operating o if external program memory is used. modes, which are selected by bit-pairs (Ml, MO) in TMOD. Modes 0, 1, and 2 are the same for both Tim- External Program Memory is accessed under two con- er/Counters. Mode 3 operation is different for the two ditions: timers. 1. Whenever signal EA is active, or 2. Whenever the program counter (PC) contains an ad- MODE 0 dress greater than IFFFH (8K) for the 8XC51FA or 3FFFH (16K) for the 8XC51FB, or 7FFFH (32K) Either Timer 0 or Timer 1 in Mode 0 is an 8-bit Coun- for the 87C51FC. ter with a divide-by-32 presca1er. Figure 8 shows the Mode 0 operation for either timer. This requires that the ROMless versions have EA wired to VSS enable the lower 8K, 16K, or 32K program In this mode, the Timer register is configured as a bytes to be fetched from external memory. 13-bit register. As the count rolls over from all Is to all Os, it sets the Timer interrupt flag TFx. The counted When the CPU is executing out of external Program input is enabled to the Timer when TRx = 1 and either Memory, all 8 bits of Port 2 are dedicated to an output GATE = 0 or INTx = 1. (Setting GATE = 1 allows function and may not be used for general purpose I/O. the Timer to be controlled by external input INTx, to During external program fetches they output the high facilitate pulse width measurements). TRx and TFx are byte of the PC with the Port 2 drivers using the strong pullups to emit bits that are Is. 5-12 infel~ 8XC51FX HARDWARE DESCRIPTION control bits in SFR TCON (Table 6). The GATE bit is MODE 2 in TMOD. There are two different GATE bits, one for Timer 1 (TMOD.7) and one for Timer 0 (TMOD.3). Mode 2 configures the Timer register as an 8-bit Coun- ter (TLx) with automatic reload, as shown in Figure 10. The 13-bit register consists of all 8 bits of THx and the Overflow from TLx not only sets TFx, but also reloads lower 5 bits of TLx. The upper 3 bits of TLx are inde- TLx with the contents of THx, which is preset by soft- terminate and should be ignored. Setting the run flag ware. The reload leaves THx unchanged. (TRx) does not clear these registers. MODE 1 Mode 1 is the same as Mode 0, except that the Timer register uses all 16 bits. Refer to Figure 9. In this mode, THx and TLx are cascaded; there is no prescaler. Table 5. TMOD: Timer/Counter Mode Control Register TMOD Address = 89H Reset Value = 0000 OOOOB Not Bit Addressable TIMER 1 TIMER 0 I GATE I CIT I M 1 I MO GATE I CIT I M1 I MO I B~ 7 6 5 4 3 2 o Symbol Function GATE Gating control when set. Timer/Counter 0 or 1 is enabled only while INTO or INT1 pin is high and TRO or TR1 control pin is set. When cleared, Timer 0 or 1 is enabled whenever TRO or TR 1 control bit is set. CIT Timer or Counter Selector. Clear for Timer operation (input from internal system clock). Set for Counter operation (input from TO or T1 input pin). M1 MO Operating Mode o 0 8-bit Timer/Counter. THx with TLx as 5-bit prescaler. o 1 16-bit Timer/Counter. THx and TLx are cascaded; there is no prescaler. o 8-bit auto-reload Timer/Counter. THx holds a value which is to be reloaded into TLx each time it overflows. (Timer 0) TLO is an 8-bit Timer/Counter controlled by the standard Timer 0 control bits. THO is an 8-bit timer only controlled by Timer 1 control bits. (Timer 1) Timer/Counter stopped. INTERRUPT CONTROL OVERFLOW TxPlN-----' x=Oor1 270653-6 Figure 8. Timer/Counter 0 or 1 in Mode 0: 13-Bit Counter 5-13 intel~ 8XC51FX HARDWARE DESCRIPTION Table 6. TCON: Timer/Counter Control Register TCON Address = 88H Reset Value = 0000 OOOOB Bit Addressable TF1 TR1 TFO TRO IE1 IT1 lEO ITO Bit 7 6 5 4 3 2 o Symbol Function TF1 Timer 1 overflow Flag. Set by hardware on Timer/Counter overflow. Cleared by hardware when processor vectors to interrupt routine. TR1 Timer 1 Run control bit. Set/cleared by software to turn Timer/Counter 1 on/off. TFO Timer 0 overflow Flag. Set by hardware on Timer/Counter 0 overflow. Cleared by hardware when processor vectors to interrupt routine. TRO Timer 0 Run control bit. Set/cleared by software to turn Timer/Counter 0 on/off. IE1 Interrupt 1 flag. Set by hardware when external interrupt 1 edge is detected (transmitted or level-activated). Cleared when interrupt processed only if transition- activated. IT1 Interrupt 1 Type control bit. Set/cleared by software to specify falling edge/low level triggered external interrupt 1. lEO Interrupt 0 flag. Set by hardware when external interrupt 0 edge is detected (transmitted or level-activated). Cleared when interrupt processed only if transition- activated. ITO Interrupt 0 Type control bit. Set/cleared by software to specify falling edge/low level triggered external interrupt o. THx TLx INTERRUPT CONTROL OVERFLOW Tx PIN - - - - - - ' x = Oor1 GATE 270653-34 Figure 9. Timer/Counter 0 or 1 in Mode 1: 16-Bit Counter MODE 3 a timer function (counting machine cycles) and takes over the use ofTRl and TFl from Timer 1. Thus THO Timer 1 in Mode 3 simply holds its count. The effect is now controls the Timer 1 interrupt. the same as setting TRI = O. Mode 3 is provided for applications requiring an extra Timer 0 in Mode 3 establishes TLO and THO as two 8-bit timer or counter. When Timer 0 is in Mode 3, separate counters. The logic for Mode 3 on Timer 0 is Timer 1 can be turned on and off by switching it out of shown in Figure 11. TLO uses the Timer 0 control bits: and into its own Mode 3, or can still be used by the CIT, GATE, TRO, INTO, and TFO. THO is locked into serial port as a baud rate generator, or in any applica- tion not requiring an interrupt. 5-14 infel~ 8XC51 FX HARDWARE DESCRIPTION INTERRUPT Tx PIN _ _ _ _ _---J GATE INTxPIN x = Oor1 270653-7 Figure 10. Timer/Counter 1 Mode 2: 8-Bit Auto-Reload osc ~G-- 111210SC 1/12 fOSC - - - - - - - , ~-- INTERRUPT TO PIN _ _ _ _ _- - J CONTROL OVERFLOW 1/12fOSC -----------It-°1 I ·1 (:, H _ _ _ _ _ _ _...."TCONTROL OVERFLOW W1 ~INTERRUPT TR1 - 270653-8 Figure 11. Timer/Counter 0 Mode 3: Two 8-Bit Counters 5.2 Timer 2 Table 7. Timer 2 Operating Modes RCLK + TCLK CP/RL2 T2*OE TR2 Mode Timer 2 is a 16-bit Timer/Counter which can operate either as a timer or as an event counter. This is selected 0 0 0 1 16-Bit by bit cm in the Special Function Register T2CON Auto-Reload (Table 8). It has three operating modes: capture, auto- 0 1 0 1 16-Bit reload (up or down counting), and baud rate generator. Capture The modes are selected by bits in T2CON as shown in 1 X X 1 Baud_Rate Table 7. Generator X 0 1 1 Clock-Out on P1.0 X X X 0 Timer Off 5-15 inteL 8XC51FX HARDWARE DESCRIPTION Table 8. T2CON: TimerICounter 2 Control Register T2CON Address = OC8H Reset Value = 0000 OOOOB Bit Addressable TF2 EXF2 RCLK TCLK I EXEN21 TR2 Bit 7 6 5 4 3 2 o Symbol Function TF2 Timer 2 overflow flag set by a Timer 2 overflow and must be cleared by software. TF2 will not be set when either RCLK = 1 or TCLK = 1. EXF2 Timer 2 external flag set when either a capture or reload is caused by a negative transition on T2EX and EXEN2 = 1. When Timer 2 interrupt is enabled EXF2 = 1 will cause the CPU to vector to the Timer 2 interrupt routine. EXF2 must be cleared by software. EXF2 does not cause an interrupt in up/down counter mode (DCEN = 1). RCLK Receive clock flag. When set, causes the serial port to use Timer 2 overflow pulses for its receive clock in serial port Modes 1 and 3. RCLK = 0 causes Timer 1 overflow to be used for the receive clock. TCLK Transmit clock flag. When set, causes the serial port to use Timer 2 overflow pulses for its transmit clock in serial port Modes 1 and 3. TCLK = 0 causes Timer 1 overflows to be used for the transmit clock. EXEN2 Timer 2 external enable flag. When set, allows a capture or reload to occur as a result of a negative transition on T2EX if Timer 2 is not being used to clock the serial port. EXEN2 = 0 causes Timer 2 to ignore events at T2EX. TR2 Start/ stop control for Timer 2. A logic 1 starts the timer. C/T2 Timer or counter select. (Timer 2) o = Internal timer (OSC/12 or OSC/2 in baud rate generator mode). 1 = External event counter (falling edge triggered). CP/RL2 Capture/Reload flag. When set, captures will occur on negative transitions at T2EX if EXEN2 = 1. When cleared, auto-reloads will occur either with Timer 2 overflows or negative transitions at T2EX when EXEN2 = 1. When either RCLK = 1 or TCLK = 1, this bit is ignored and the timer is forced to auto-reload on Timer 2 overflow. CAPTURE MODE 16-bit timer or counter which upon overflow sets bit TF2 in T2CON. This bit can then be used to generate In the capture mode there are two options selected by an interrupt. If EXEN2 = 1, Timer 2 still does the bit EXEN2 in T2CON. If EXEN2 = 0, Timer 2 is a above, but with the added feature that a I-to-O tran- CAPTURE TIMER 2 INTERRUPT TRANsmON DETECTION T2EX PIN EXEN2 270653-9 Figure 12. Timer 2 in Capture Mode 5-16 intelQP 8XC51FX HARDWARE DESCRIPTION sition at external input T2EX causes the current value default to count up. When DCEN is set, Timer 2 can in the Timer 2 registers, TH2 and TL2, to be captured count up or down depending on the value of the T2EX into registers RCAP2H and RCAP2L, respectively. In pin. addition, the transition at T2EX causes bit EXF2 in T2CON to be set. The EXF2 bit, like TF2, can generate Figure 13 shows Timer 2 automatically counting up an interrupt. The capture mode is illustrated in Figure when DCEN = O. In this mode there are two options 12. selected by bit EXEN2 in T2CON. If EXEN2 = 0, Timer 2 counts up to OFFFFH and then sets the TF2 bit upon overflow. The overflow also causes the timer AUTO-RELOAD MODE registers to be reloaded with the 16-bit value in (UP OR DOWN COUNTER) RCAP2H and RCAP2L. The values in RCAP2H and Timer 2 can be programmed to count up or down when RCAP2L are preset by software. If EXEN2 = 1, a 16- configured in its 16-bit auto-reload mode. This feature bit reload can be triggered either by an overflow or by a is invoked by a bit named DCEN (Down Counter En- I-to-O transition at external input T2EX. This tran- able) located in the SFR T2MOD (see Table 9). Upon sition also sets the EXF2 bit. Either the TF2 or EXF2 reset the DCEN bit is set to 0 so that Timer 2 will bit can generate the Timer 2 interrupt if it is enabled. Table 9. T2MOD: Timer 2 Mode Control Register T2MOD Address = OC9H Reset Value = XXXX XXOOB Not Bit Addressable T20E I DCEN I Bit 7 6 5 4 3 2 o Symbol Function Not implemented, reserved for future use. * T20E Timer 2 Output Enable bit. DCEN Down Count Enable bit. When set, this allows Timer 2 to be configured as an up/down counter. ·User software should not write 1s to reserved bits. These bits may be used in future 8051 family products to invoke new features. In that case, the reset or inactive value of the new bit will be 0, and its active value will be 1. The value read from a reserved bit is indeterminate. OVERFLOW _ + em =1 T2 PIN D----' TR2 TRANsmON DETECTION TIMER 2 INTERRUPT T2EX PIN EXEN2 270653-10 Figure 13. Timer 2 Auto Reload Mode (DCEN = 0) 5-17 inteL 8XC51FX HARDWARE DESCRIPTION Setting the DCEN bit enables Timer 2 to count up or The Clock-out frequency depends on the oscillator fre- down as shown in Figure 14. In this mode the T2EX quency and the reload value of Timer 2 capture regis- pin controls the direction of count. A logic 1 at T2EX ters (RCAP2H, RCAP2L) as shown in this equation: makes Timer 2 count up. The timer will overflow at OFFFFH and set the TF2 bit which can then generate Clock-out Frequency = an interrupt if it is enabled. This overflow also causes a the 16-bit value in RCAP2H and RCAP2L to be re- Oscillator Frequency loaded into the timer registers, TH2 and TL2, respec- 4 x (65536 - RCAP2H, RCAP2L) tively. In the Clock-Out mode Timer 2 roll-overs will not gen- A logic 0 at T2EX makes Timer 2 count down. Now erate an interrupt. This is similar to when Timer 2 is the timer underflows when TH2 and TL2 equal the used as a baud-rate generator. It is possible to use Tim- values stored in RCAP2H and RCAP2L. The under- er 2 as a baud-rate generator and a clock generator flow sets the TF2 bit and causes OFFFFH to be reload- simultaneously. Note, however, that the baud-rate and ed into the timer registers. Clock-out frequencies cannot be determined indepen- dently of one another since they both use the values in The EXF2 bit toggles whenever Timer 2 overflows or RCAP2H and RCAP2L. underflows. This bit can be used as a 17th bit of resolu- tion if desired. In this operating mode, EXF2 does not generate an interrupt. 6.0 PROGRAMMABLE COUNTER ARRAY BAUD RATE GENERATOR MODE The Programmable Counter Array (PCA) consists of a The baud rate generator mode is selected by setting the 16-bit timer/counter and five 16-bit compare/capture modules as shown in Figure 15a. The PCA timer/coun- RCLK and/or TCLK bits in T2CON. Timer 2 in this ter serves as a common time base for the five modules mode will be described in conjunction with the serial port. and is the only timer which can service the PCA. Its clock input can be programmed to count anyone of the following signals: PROGRAMMABLE CLOCK OUT • oscillator frequency -:- 12 A 50% duty cycle clock can be programmed to come • oscillator frequency -:- 4 out on PtO. This pin, besides being a regular I/O pin, • Timer 0 overflow has two alternate functions. It can be programmed (1) • external input on ECI (p1.2). to input the external clock for Timer/Counter 2 or (2) to output a 50% duty cycle clock ranging from 61 Hz Each compare/capture module can be programmed in to 4 MHz at a 16 MHz operating frequency. anyone of the following modes: To configure the Timer/Counter 2 as a clock generator, • rising and/or falling edge capture bit CIT2 (in T2CON) must be cleared and bit T20E in • software timer T2MOD must be set. Bit TR2 (T2CON.2) also must be • high speed output set to start the timer (see Table 6 for operating modes). • pulse width modulator. Module 4 can also be programmed as a watchdog tim- er. When the compare/capture modules are programmed in the capture mode, software timer, or high speed out- put mode, an interrupt can be generated when the mod- ule executes its function. All five modules plus the PCA timer overflow share one interrupt vector (more about this in the PCA Interrupt section). 5-18 intel~ 8XC51 FX HARDWARE DESCRIPTION (DOWN COUNnNG RELOAD VALUE) _ + cfi2 = T2 PIN [J---I 1 COUNT DIRECnON 1 = UP 0= DOWN (UP COUNnNG RELOAD VALUE) T2EX PIN 270653-11 Figure 14. Timer 2 Auto Reload Mode (DCEN = 1) Pl.0 (T2) ---, Transition T20E (T2t.lOD.1) 't- Detector P1.1 v-\l1------t-o (T2EX)~ ........- ... EXEN2 270653-35 Figure 15. Timer 2 in Clock-Out Mode 5-19 intelqp 8XC51FX HARDWARE DESCRIPTION MODULE 0 .........£11 Pl .3/CEXO ~---- .... -168ITS- ...._---_... MODULE 1 ""--------'"L..1:11 Pl.7/CEX4 ~---- .... 270653-12 Figure 15a. Programmable Counter Array The PCA timer/counter and compare/capture modules gram of this timer. The clock input can be selected share Port 1 pins for external I/O. These pins are listed from the following four modes: below. If the port pin is not used for the PCA, it can • Oscillator frequency -:- 12 still be used for standard I/O. The CL register is incremented at S5P2 of every machine cycle. With a 16 MHz crystal, the timer PCA Component External 110 Pin increments every 750 nanoseconds. 16-bit Counter P1.2/ ECI • Oscillator frequency -:- 4 16-bit Module 0 P1.3 / CEXO The CL register is incremented at SIP2, S3P2 and 16-bit Module 1 P1.4 / CEX1 S5P2 of every machine cycle. With a 16 MHz crys- 16-bit Module 2 P1.5/ CEX2 tal, the timer increments every 250 nanoseconds. 16-bit Module 3 P1.6/ CEX3 • Timer 0 overflows 16-bit Module 4 P1.7/ CEX4 The CL register is incremented at S5P2 of the ma- chine cycle when Timer 0 overflows. This mode al- lows a programmable input frequency to the PCA. 6.1 PCA 16-Bit Timer/Counter • External input The PCA has a free-running 16-bit timer/counter con- The CL register is incremented at the first one of sisting of registers CH and CL (the high and low bytes SIP2, S3P2 and S5P2 after a I-to-O transi- of the count value). These two registers can be read or tion is detected on the ECI pin (P1.2). P1.2 is sam- written to at any time. Figure 16 shows a block dia- pled at SIP2, S3P2 and S5P2 of every machine cy- cle. The maximum input frequency in this mode is oscillator frequency -:- 8. rosc/12 rose/4 TIMER 0 .......--If-O'''':" 0 + - + INTERRUPT OVERrLOW EXTERNAL INPUT (ECI) 270653-13 Figure 16. PCA Timer/Counter 5-20 infel~ 8XC51FX HARDWARE DESCRIPTION CH is incremented after two oscillator periods when The CCON register, shown in Table 11, contains two CL overflows. more bits which are associated with the PCA timer/ counter. The CF bit gets set by hardware when the The mode register CMOD contains the Count Pulse counter overflows, and the CR bit is set or cleared to Select bits (CPSI and CPSO) to specify the clock input. turn the counter on or off. The other five bits in this CMOD is shown in Table 10. This register also con- register are the event flags for the compare/capture tains the ECF bit which enables the PCA counter over- modules and will be discussed in the next section. flow to generate the PCA interrupt. In addition, the user has the option of turning off the PCA timer during Idle Mode by setting the Counter Idle bit (CIDL). The Watchdog Timer Enable bit (WDTE) will be discussed in a later section. Table 10. CMOD: PCA Counter Mode Register CMOD Address = OD9H Reset Value = OOXX XOOOB Not Bit Addressable CIDL I WDTE I CPS1 CPSO ECF Bit 7 6 5 4 3 2 o Symbol Function CIDL Counter Idle control: CIDL = 0 programs the PCA Counter to continue functioning during idle Mode. CIDL = 1 programs it to be gated off during idle. WDTE Watchdog Timer Enable: WDTE = 0 disables Watchdog Timer function on PCA Module 4. WDTE = 1 enables it. Not implemented, reserved for future use. * CPS1 PCA Count Pulse Select bit 1. CPSO PCA Count Pulse Select bit O. CPS1 CPSO Selected PCA Input** o 0 Internal clock, Fosc+ 12 o 1 Internal clock, Fosc + 4 1 0 Timer 0 overflow 1 External clock at ECIIP1.2 pin (max. rate = Fosc + 8) ECF PCA Enable Counter Overflow interrupt: ECF = 1 enables CF bit in CCON to generate an interrupt. ECF = 0 disables that function of CF. NOTE: *User software should not write 1s to reserved bits. These bits may be used in future 8051 family products to invoke new features. In that case, the reset or inactive value of the new bit will be 0, and its active value will be 1. The value read from a reserved bit is indeterminate. **Fosc = oscillator frequency 5-21 inleL 8XC51FX HARDWARE DESCRIPTION Table 11. CCON: PCA Counter Control Register CCON Address = OOSH Reset Value = OOXO OOOOB Bit Addressable CF CR CCF4 CCF3 CCF2 CCF1 CCFO Bit 7 6 5 4 3 2 o Symbol Function CF PCA Counter Overflow flag. Set by hardware when the counter rolls over. CF flags an interrupt if bit ECF in CMOO is set. CF may be set by either hardware or software but can only be cleared by software. CR PCA Counter Run control bit. Set by software to turn the PCA counter on. Must be cleared by software to turn the PCA counter off. Not implemented, reserved for future use*. CCF4 PCA Module 4 interrupt flag. Set by hardware when a match or capture occurs. Must be cleared by software. CCF3 PCA Module 3 interrupt flag. Set by hardware when a match or capture occurs. Must be cleared by software. CCF2 PCA Module 2 interrupt flag. Set by hardware when a match or capture occurs. Must be cleared by software. CCF1 PCA Module 1 interrupt flag. Set by hardware when a match or capture occurs. Must be cleared by software. CCFO PCA Module 0 interrupt flag. Set by hardware when a match or capture occurs. Must be cleared by software. -NOTE: User software should not write 1s to reserved bits. These bits may be used in future 8051 family products to invoke new features. In that case, the reset or inactive value of the new bit will be 0, and its active value will be 1. The value read from a reserved bit is indeterminate. 6.2 Capture/Compare Modules when a module's event flag is set. The event flags (CCFn) are located in the CCON register and get set Each of the five compare/capture modules has six pos- when a capture event, software timer, or high speed sible functions it can perform: output event occurs for a given module. 16-bit Capture, positive-edge triggered Table 13 shows the combinations of bits in the - 16-bit Capture, negative-edge triggered CCAPMn register that are valid and have a defmed - 16-bit Capture, both positive and negative-edge function. Invalid combinations will produce undefined triggered results. - 16-bit Software Timer Each module also has a pair of 8-bit compare/capture - 16-bit High Speed Output registers (CCAPnH and CCAPnL) associated with it. - 8-bit Pulse Width Modulator. These registers store the time when a capture event oc- curred or when a compare event should occur. For the In addition, module 4 can be used as a Watchdog Tim- PWM mode, the high byte regiser CCAPnH controls er. The modules can be programmed in any combina- the duty cycle of the waveform. tion of the different modes. The next five sections describe each of the compare/ Each module has a mode register called CCAPMn capture modes in detail. (n = 0, 1, 2, 3, or 4) to select which function it will perform. The CCAPMn register is shown in Table 12. Note the ECCFn bit which enables the PCA interrupt 5-22 inteL 8XC51FX HARDWARE DESCRIPTION Table 12. CCAPMn: PCA Modules Compare/Capture Registers CCAPMn Address CCAPMO ODAH Reset Value = XOOO OOOOS (n = 0-4) CCAPM1 ODSH CCAPM2 ODCH CCAPM3 ODDH CCAPM4 ODEH Not Sit Addressable IECOMn I CAPPn I CAPNn I MATn TOGn PWMn I ECCFn I Sit 7 6 5 4 3 2 o Symbol Function Not implemented, reserved for future use*. ECOMn Enable Comparator. ECOMn = 1 enables the comparator function. CAPPn Capture Positive, CAPPn = 1 enables positive edge capture. CAPNn Capture Negative, CAPNn = 1 enables negative edge capture. MATn Match. When MATn = 1, a match of the PCA counter with this module's compare/ capture register causes the CCFn bit in CCON to be set, flagging an interrupt. TOGn Toggle. When TOGn = 1, a match of the PCA counter with this module's compare/capture register causes the CEXn pin to toggle. PWMn Pulse Width Modulation Mode. PWMn = 1 enables the CEXn pin to be used as a pulse width modulated output. ECCFn Enable CCF interrupt. Enables compare/capture flag CCFn in the CCON register to generate an interrupt. NOTE: ·User software should not write 1s to reserved bits. These bits may be used in future 8051 family products to invoke new features. In that case, the reset or inactive value of the new bit will be 0, and its active value will be 1. The value read from a reserved bit is indeterminate. Table 13. PCA Module Modes (CCAPMn Register) - ECOMn CAPPn CAPNn MATn TOGn PWMn ECCFn Module Function X 0 0 0 0 0 0 0 No operation X X 1 0 0 0 0 X 16-bit capture by a postive-edge trigger on CEXn X X 0 1 0 0 0 X 16-bit capture by a negative-edge trigger on CEXn X X 1 1 0 0 0 X 16-bit capture by a transition on CEXn X 1 0 0 1 0 0 X 16-bit Software Timer X 1 0 0 1 1 0 X 16-bit High Speed Output X 1 0 0 0 0 1 0 8-bitPWM X 1 0 0 1 x 0 x Watchdog Timer X = Don't Care 5-23 8XC51FX HARDWARE DESCRIPTION 6.3 16-Bit Capture Mode In the interrupt service routine, the 16-bit capture value must be saved in RAM before the next capture event Both positive and negative transitions can trigger a cap- occurs. A subsequent capture on the same CEXn pin ture with the PCA. This gives the PCA the flexibility to will write over the first capture value in CCAPnH and measure periods, pulse widths, duty cycles, and phase CCAPnL. differences on up to five separate inputs. Setting the CAPPn and/or CAPNn in the CCAPMn mode register select the input trigger-positive and/or negative tran- 6.4 16·Bit Software Timer Mode sition-for module n. Refer to Figure 17. In the compare mode, the 16-bit value of the PCA tim- The external input pins CEXO through CEX4 are sam- er is compared with a 16-bit value pre-loaded in the pled for a transition. When a valid transition is detected module's compare registers (CCAPnH, CCAPnL). The (positive and/or negative edge), hardware loads the comparison occurs three times per machine cycle in 16-bit value of the PCA timer (CH, CL) into the mod- order to recognize the fastest possible clock input (Le. ule's capture registers (CCAPnH, CCAPnL). The re- Y4 x oscillator frequency). Setting the ECOMn bit in sulting value in the capture registers reflects the PCA the mode register CCAPMn enables the comparator timer value at the time a transition was detected on the function as shown in Figure 18. CEXn pin. For the Software Timer mode, the MATn bit also needs Upon a capture, the module's event flag (CCFn) in to be set. When a match occurs between the PCA timer CCON is set, and an interrupt is flagged if the ECCFn and the compare registers, a match signal is generated bit in the mode register CCAPMn is set. The PCA in- and the module's event flag (CCFn) is set. An interrupt terrupt will then be generated if it is enabled. Since the is then flagged if the ECCFn bit is set. The PCA inter- hardware does not clear an event flag when the inter- rupt is generated only if it has been properly enabled. rupt is vectored to, the flag must be cleared in software. Software must clear the event flag before the next inter- rupt will be flagged. -+---. INTERRUPT _--_ ...... PCA .... TIMER/COUNTER CEXn PIN n = 0, 1, 2. 3 or 4 x = Don't Care CCAPMn MODE REGISTER 270653-14 Figure 17. PCA 16-Bit Capture Mode 5-24 intet 8XC51 FX HARDWARE DESCRIPTION During the interrupt routine, a new 16-bit compare val- regular hold-off signals to the Watchdog. These circuits ue can be written to the compare registers (CCAPnH are used in applications that are subject to electrical and CCAPnL). Notice, however, that a write to noise, power glitches, electrostatic discharges, etc., or CCAPnL clears the ECOMn bit which temporarily dis- where high reliability is required. ables the comparator function while these registers are being updated so an invalid match does not occur. A The Watchdog Timer function is only available on write to CCAPnH sets the ECOMn bit and re-enables PCA module 4. In this mode, every time the count in the comparator. For this reason, user software should the PCA timer matches the value stored in module 4's write to CCAPnL first, then CCAPnH. compare registers, an internal reset is generated. (See Figure 19.) The bit that selects this mode is WDTE in the CMOD register. Module 4 must be set up in either 6.5 High Speed Output Mode compare mode as a Software Timer or High Speed Out- put. The High Speed Output (HSO) mode toggles a CEXn pin when a match occurs between the PCA timer and a When the PCA Watchdog Timer times out, it resets the pre-loaded value in a module's compare registers. For chip just like a hardware reset, except that it does not this mode, the TOGn bit needs to be set in addition to drive the reset pin high. the ECOMn and MATn bits as seen in Figure 18. By setting or clearing the pin in software, the user can To hold off the reset, the user has three options: select whether the CEXn pin will change from a logical (1) periodically change the compare value so it will o to a logical 1 or vice versa. The user also has the never match the PCA timer, option of flagging an interrupt when a match event oc- curs by setting the ECCFn bit. (2) periodically change the PCA timer value so it will never match the compare value, The HSO mode is more accurate than toggling port (3) disable the Watchdog by clearing the WDTE bit pins in software because the toggle occurs before before a match occurs and then later re-enable it. branching to an interrupt. That is, interrupt latency will not effect the accuracy of the output. If the user The first two options are more reliable because the does not change the compare registers in an interrupt Watchdog Timer is never disabled as in option #3. The routine, the next toggle will occur when the PCA timer second option is not recommended if other PCA mod- rolls over and matches the last compare value. ules are being used since this timer is the time base for all five modules. Thus, in most applications the first solution is the best option. 6.6 Watchdog Timer Mode If a Watchdog Timer is not needed, module 4 can still A Watchdog Timer is a circuit that automatically in- be used in other modes. vokes a reset unless the system being watched sends PCA CEXn PIN TIMER/COUNTER L..-_ _- ' ENABLE CCAPMn MODE REGISTER RESET WRITE TO CCAPnL n = 0, 1, 2, 3 or 4 WRITE TO CCAPnH x = Don't Care "1" 270653-15 Figure 18. PCA 16-Bit Comparator Mode: Software Timer and High Speed Output 5-25 8XC51FX HARDWARE DESCRIPTION 6.7 Pulse Width Modulator Mode The PCA generates 8-bit PWMs by comparing the low byte of the PCA tL'ller (CL) with the low byte of the Any or all of the five PCA modules can be pro- module's compare registers (CCAPnL). Refer to Figure grammed to be a Pulse Width Modulator. The PWM 20. When CL < CCAPnL the output is low. When CL output can be used to convert digital data to an analog ~ CCAPnL the output is high. The value in CCAPnL signal by simple external circuitry. The frequency of the controls the duty cycle of the waveform. To change the PWM depends on the clock sources for the PCA timer. value in CCAPnL without output glitches, the user With a 16 MHz crystal the maximum frequency of the must write to the high byte register (CCAPnH). This PWM waveform is 15.6 KHz. value is then shifted by hardware into CCAPnL when CL rolls over from OFFH to OOH which corresponds to the next period of the output. PCA TIMER/COUNTER L...._ _... F~~--I-'" -+--+ RESET ENABLE CCAPM4 MODE REGISTER RESET WRITE TO CCAP4L x = Don't Care 270653-16 Figure 19. Watchdog Timer Mode CL MADE FF TO 00 ----lIi~ TRANsmON "0" CL < CCAPnL CL 2: CCAPnL COCn PIN "I" n = 0,1,2,3 or 4 x = Don't Care CCAPMn MODE REGISTER 270653-17 Figure 20. peA 8-Bit PWM Mode 5-26 intel~ 8XC51 FX HARDWARE DESCRIPTION DUTY CYCLE CCAPnH OUTPUT WAVEFORtol 100:; 00 90:; 25 50:; 128 107. 230 0.47. 255 270653-18 Figure 21. CCAPnH Varies Duty Cycle CCAPnH can contain any integer from 0 to 255 to vary The serial port can operate in 4 modes: the duty cycle from a 100% to 0.4% (see Figure 21). Mode 0: Serial data enters and exits through RXD. TXD outputs the shift clock. S bits are transmitted/re- 7.0 SERIAL INTERFACE ceived: S data bits (LSB first). The baud rate is fixed at 1/12 the oscillator frequency. The serial port is full duplex, meaning it can transmit and receive simultaneously. It is also receive-buffered, Mode 1: 10 bits are transmitted (through TXD) or re- meaning it can commence reception of a second byte ceived (through RXD): a start bit (0), S data bits (LSB before a previously received byte has been read from first), and a stop bit (1). On receive, the stop bit goes the receive register. (However, if the first byte still into RBS in Special Function Register SCON. The hasn't been read by the time reception of the second baud rate is variable. byte is complete, one of the bytes will be lost). The serial port receive and transmit registers are both ac- Mode 2: 11 bits are transmitted (through TXD) or re- cessed through Special Function Register SBUF. Actu- ceived (through RXD): a start bit (0), S data bits (LSB ally, SBUF is two separate registers, a transmit buffer first), a programmable 9th data bit, and a stop bit (1). and a receive buffer. Writing to SBUF loads the trans- Refer to Figure 22. On Transmit, the 9th data bit (TBS mit register, and reading SBUF accesses a physically in SCON) can be assigned the value of 0 or 1. Or, for separate receive register. example, the parity bit (P in the PSW) could be moved into TBS. On receive, the 9th data bit goes into RBS in The serial port control and status register is the Special SCON, while the stop bit is ignored. (The validity of Function Register SCON, shown in Table 14. This reg- the stop bit can be checked with Framing Error Detec- ister contains the mode selection bits (SMO and SMl); tion.) The baud rate is programmable to either 1/32 or the SM2 bit for the multiprocessor modes (see Multi- 1'64 the oscillator frequency. processor Communications section); the Receive En- able bit (REN); the 9th data bit for transmit and receive (TBS and RBS); and the serial port interrupt bits (TI and RI). I I~ STOP BIT \ NINTH DATA BIT (Modes 2 and 3 only) 270653-19 Figure 22. Data Frame: Modes 1, 2 and 3 5-27 intel~ 8XC51FX HARDWARE DESCRIPTION Mode 3: 11 bits are transmitted (through TXD) or re- byte has its 9th bit set to O. All the slave processors ceived (through RXD): a start bit (0), 8 data bits (LSB should have their SM2 bits set to 1 so they will only be first), a programmable 9th data bit and a stop bit (1). In interrupted by an address byte. In fact, the C51FX has fact, Mode 3 is the same as Mode 2 in all respects an Automatic Address Recognition feature which al- except the baud rate. The baud rate in Mode 3 is vari- lows only the addressed slave to be interrupted. That is, able. the address comparison occurs in hardware, not soft- ware. (On the 8051 serial port, an address byte inter- In all four modes, transmission is initiated by any in- rupts all slaves for an address comparison.) struction that uses SBUF as a destination register. Re- ception is initiated in Mode 0 by the condition RI = 0 The addressed slave's software then clears its SM2 bit and REN = 1. Reception is initiated in the other and prepares to receive the data bytes that will be com- modes by the incoming start bit ifREN = 1. For more ing. The other slaves are unaffected by these data bytes. detailed information on each serial port mode, refer to They are still waiting to be addressed since their SM2 the "Hardware Description of the 8051, 8052, and bits are all set. 80C51." 7.3 Automatic Address Recognition 7.1 Framing Error Detection Automatic Address Recognition reduces the CPU time Framing Error Detection allows the serial port to check required to service the serial port. Since the CPU is for valid stop bits in modes 1, 2, or 3. A missing stop bit only interrupted when it receives its own address, the can be caused, for example, by noise on the serial lines, software overhead to compare addresses is eliminated. or transmission by two CPUs simultaneously. With this feature enabled in one of the 9-bit modes, the Receive Interrupt (RI) flag will only get set when the If a stop bit is missing, a Framing Error bit FE is set. received byte corresponds to either a Given or Broad- The FE bit can be checked in software after each recep- cast address. tion to detect communication errors. Once set, the FE bit must be cleared in software. A valid stop bit will not The feature works the same way in the 8-bit mode clear FE. (Mode 1) as in the 9-bit modes, except that the stop bit takes the place of the 9th data bit. If SM2 is set, the RI The FE bit is located in SCON and shares the same bit flag is set only if the received byte matches the Given or address as SMO. Control bit SMODO in the PCON reg- Broadcast Address and is terminated by a valid stop ister (location PCON.6) determines whether the SMO bit. Setting the SM2 bit has no effect in Mode O. or FE bit is accessed. If SMODO = 0, then accesses to SCON.7 are to SMO. If SMODO = 1, then accesses to The master can selectively communicate with groups of SCON.7 are to FE. slaves by using the Given Address. Addressing all slaves at once is possible with the Broadcast Address. These addresses are defined for each slave by two Spe- 7.2 Multiprocessor Communications cial Function Registers: SADDR and SADEN. Modes 2 and 3 provide a 9-bit mode to facilitate multi- A slave's individual address is specified in SADDR. processor comunication. The 9th bit allows the control- SADEN is a mask byte that defines don't-cares to form ler to distinguish between address and data bytes. The the Given Address. These don't-cares allow flexibility 9th bit is set to 1 for address bytes and set to 0 for data in the user-defmed protocol to address one or more bytes. When receiving, the 9th bit goes into RB8 in slaves at a time. The following is an example of how the SCON. When transmitting, TB8 is set or cleared in user could define Given Addresses to selectively ad- software. dress different slaves. The serial port can be programmed such that when the Slave 1: stop bit is received the serial port interrupt will be acti- SADDR 1111 0001 vated only if the received byte is an address byte (RB8 SADEN 1111 1010 = 1). This feature is enabled by setting the SM2 bit in SCON. A way to use this feature in multiprocessor sys- GIVEN 1111 OXOX tems is as follows. Slave 2: When the master processor wants to transmit a block of SADDR 1111 0011 data to one of several slaves, it first sends out an ad- SADEN 1111 1001 dress byte which identifies the target slave. Remember, an address byte has its 9th bit set to 1, whereas a data GIVEN 1111 OXX1 5-28 8XC51FX HARDWARE DESCRIPTION Table 14. SCON: Serial Port Control Register SCON Address = 9SH Reset Value = 0000 OOOOB Bit Addressable ISMO/FEI SM1 SM2 REN TBS RBS TI RI Bit: 7 6 5 4 3 2 o (SMODO = 0/1)* Symbol Function FE Framing Error bit. This bit is set by the receiver when an invalid stop bit is detected. The FE bit is not cleared by valid frames but should be cleared by software. The SMODO* bit must be set to enable access to the FE bit. SMO Serial Port Mode Bit 0, (SMODO must = 0 to access bit SMO) SM1 Serial Port Mode Bit 1 SMO SM1 Mode Description Baud Rate** o 0 o shift register Fose/ 12 o 1 1 S-bit UART variable o 2 9-bit UART Fose/64 or Fose/32 3 9-bit UART variable SM2 Enables the Automatic Address Recognition feature in Modes 2 or 3. If SM2 = 1 then RI will not be set unless the received 9th data bit (RBS) is 1, indicating an address, and the received byte is a Given or Broadcast Address. In Mode 1, if SM2 = 1 then RI will not be activated unless a valid stop bit was received, and the received byte is a Given or Broadcast Address. In Mode 0, SM2 should be O. REN Enables serial reception. Set by software to enable reception. Clear by software to disable reception. TBS The 9th data bit that will be transmitted in Modes 2 and 3. Set or clear by software as desired. RBS In modes 2 and 3, the 9th data bit that was received. In Mode 1, if SM2 = 0, RBS is the stop bit that was received. In Mode 0, RBS is not used. TI Transmit interrupt flag. Set by hardware at the end of the Sth bit time in Mode 0, or at the beginning of the stop bit in the other modes, in any serial transmission. Must be cleared by software. RI Receive interrupt flag. Set by hardware at the end of the Sth bit time in Mode 0, or halfway through the stop bit time in the other modes, in any serial reception (except see SM2). Must be cleared by software. NOTE: *SMODO is located at peONS. **Fosc = oscillator frequency The SADEN byte are selected such that each slave can Notice, however, that bit 3 is a don't-care for both be addressed separately. Notice that bit 1 (LSB) is a slaves. This allows two different addresses to select don't-care for Slave l's Given Address, but bit 1 = 1 both slaves (1111 0001 or 1111 0101). If a third slave for Slave 2. Thus, to selectively communicate with just was added that required its bit 3 = 0, then the latter Slave 1 the master must send an address with bit 1 = 0 address could be used to communicate with Slave 1 and (e.g. 1111 00(0). 2 but not Slave 3. Similarly, bit 2 = 0 for Slave 1, but is a don't-care for The master can also communicate with all slaves at Slave 2. Now to communicate with just Slave 2 an ad- once with the Broadcast Address. It is formed from the dress with bit 2 = 1 must be used (e.g. 11110111). logical OR of the SADDR and SADEN registers with zeros defmed as don't-cares. The don't-cares also allow Finally, for a master to communicate with both slaves at once the address must have bit 1 = 1 and bit 2 = o. 5-29 intelGD 8XC51FX HARDWARE DESCRIPTION flexibility in defining the Broadcast Address, but in Modes 1 and 3 = SMOD1 x Timer 1 Overflow Rate most applications a Broadcast Address will be OFFH. Baud Rate 2 32 SADDR and SADEN are located at address A9H and The Timer 1 interrupt should be disabled in this appli- B9H, respectively. On reset, the SADDR and SADEN cation. The Timer itself can be configured for either registers are initialized to OOH which defines the Given "timer" or "counter" operation, and in any of its 3 and Broadcast Addresses as XXXX XXXX (all don't- running modes. In most applications, it is configured cares). This assures the C51FX serial port to be back- for "timer" operation in the auto-reload mode (high wards compatibility with other MCS@-51 products nibble of TMOD = 00 lOB). In this case, the baud rate which do not implement Automatic Addressing. is given by the formula: Modes 1 and 3 _ 2SMOD1 x Oscillator Frequency 7.4 Baud Rates Baud Rate - 32 x 12 x [256 - (TH1)J The baud rate in Mode 0 is fixed: One can achieve very low baud rates with Timer 1 by leaving the Timer 1 interrupt enabled, and configuring Oscillator Frequency the Timer to run as a 16-bit timer (high nibble of Mode 0 Baud Rate = 12 TMOD = 000lB), and using the Timer 1 interrupt to do a 16-bit software reload. The baud rate in Mode 2 depends on the value of bit SMODI in Special Function Register PCON. If Table 15 lists various commonly used baud rates and SMODI = 0 (which is the value on reset), the baud how they can be obtained from Timer 1. rate is %4 the oscillator frequency. IfSMODI = 1, the baud rate is %2 the oscillator frequency. 7.6 Using Timer 2 to Generate Baud Oscillator Frequency Mode 2 Baud Rate = 2SMOD1 X 64 Rates Timer 2 is selected as the baud rate generator by setting The baud rates in Modes 1 and 3 are determined by the TCLK and/or RCLK in T2CON (Table 7). Note that Timer 1 overflow rate, or by Timer 2 overflow rate, or the baud rates for transmit and receive can be simulta- by both (one for transmit and the other for receive). neously different. Setting RCLK and/or TCLK puts Timer 2 into its baud rate generator mode, as shown in Figure 23. 7.5 Using Timer 1 to Generate Baud Rates The baud rate generator mode is similar to the auto-re- load mode, in that a rollover in TH2 causes the Timer 2 When Timer 1 is used as the baud rate generator, the registers to be reloaded with the 16-bit value in registers baud rates in Modes 1 and 3 are determined by the RCAP2H and RCAP2L, which are preset by software. Timer 1 overflow rate and the value of SMODI as fol- lows: Table 15. Timer 1 Generated Commonly Used Baud Rates Timer 1 Baud Rate fose SMOD Reload clf Mode Value Mode 0 Max: 1 MHz 12MHz X X X X Mode 2 Max: 375K 12MHz 1 X X X Modes 1, 3: 62.5K 12 MHz 1 0 2 FFH 19.2K 11.059 MHz 1 0 2 FDH 9.6K 11.059 MHz 0 0 2 FDH 4.8K 11.059 MHz 0 0 2 FAH 2.4K 11.059 MHz 0 0 2 F4H 1.2K 11.059 MHz 0 0 2 E8H 137.5 11.986 MHz 0 0 2 1DH 110 6MHz 0 0 2 72H 110 12 MHz 0 0 1 FEEBH 5-30 intelGP 8XC51FX HARDWARE DESCRIPTION The baud rates in Modes 1 and 3 are determined by as a baud rate generator, T2EX can be used as an extra Timer 2's overflow rate as follows: external interrupt, if desired. Modes 1 and 3 Baud Rates = Timer 2 Overflow Rate It should be noted that when Timer 2 is running (TR2 16 = 1) in "timer" function in the baud rate generator mode, one should not try to read or write TH2 or TL2. The Timer can be configured for either "timer" or Under these conditions the Timer is being incremented "counter" operation. In most aEE.lications, it is config- every state time, and the results of a read or write may ured for "timer" operation (CIT2 = 0). The "Timer" not be accurate. The RCAP2 registers may be read, but operation is different for Timer 2 when it's being used shouldn't be written to, because a write might overlap a as a baud rate generator. Normally, as a timer, it incre- reload and cause write and/or reload errors. The timer ments every machine cycle (1/12 the oscillator frequen- should be turned off (clear TR2) before accessing the cy). As a baud rate generator, however, it increments Timer 2 or RCAP2 registers. every state time (% the oscillator frequency). The baud rate formula is given below: Table 16lists commonly used baud rates and how they can be obtained from Timer 2. Modes 1 and 3 Oscillator Frequency Baud Rate 32 x [65536 - (RCAP2H, RCAP2L)1 Table 16. Timer 2 Generated Commonly Used Baud Rates where (RCAP2H, RCAP2L) is the content of Baud Osc Timer 2 RCAP2H and RCAP2L taken as a 16-bit unsigned integer. Rate Freq RCAP2H RCAP2L Timer 2 as a baud rate generator is shown in Figure 23. 375K 12MHz FF FF This figure is valid only if RCLK and/or TCLK = 1 in 9.6K 12MHz FF D9 T2CON. Note that a rollover in TH2 does not set TF2, 4.8K 12MHz FF B2 and will not generate an interrupt. Therefore, the Timer 2.4K 12MHz FF 64 2 interrupt does not have to be disabled when Timer 2 1.2K 12MHz FE C8 is in the baud rate generator mode. Note too, that if 300 12MHz FB 1E EXEN2 is set, a I-to-O transition in T2EX will set 110 12MHz F2 AF EXF2 but will not cause a reload from (RCAP2H, 300 6MHz FD 8F RCAP2L) to (TH2, TL2). Thus when Timer 2 is in use 110 6MHz F9 57 TIMER 1 OVERFLOW RXCLOCK TX CLOCK "T1IIIER 2" 12EXPIN INTERRUPT EXEN2 L NOTE AVALA8IUTY 0# ADDmONAL EXTERNAL INTERRUPT 270653-20 Figure 23. Timer 2 in Baud Rate Generator Mode 5-31 infelqp 8XC51FX HARDWARE DESCRIPTION 8.0 INTERRUPTS All of the bits that generate interrupts can be set or cleared bv software, with the same result as thous;h it The C51FX has a total of 7 interrupt vectors: two ex- had been -set or cleared by hardware. That is, interrupts ternal interrupts (INTO and INTI), three timer inter- can be generated or pending interrupts can be cancelled rupts (Timers 0, 1, and 2), the PCA interrupt, and the in software. serial port interrupt. These interrupts are all shown in Figure 24. Each of these interrupts will be briefly described fol- lowed by a discussion of the interrupt enable bits and the interrupt priority levels. TFO------------------------------------------------~ INT1---CI' TF1------------------------------------------------~ INTERRUPT SOURCES TI RI D TF'2 EXF2 D ~ 270653-21 (See exceptions when Timer 2 is used as baud rate generator or an up/down counter.) Figure 24. Interrupt Sources 5-32 intel~ 8XC51FX HARDWARE DESCRIPTION 8.1 Externallnterrupts 8.3 peA Interrupt External Interrupts INTO and INTI can each be either The PCA interrupt is generated by the logical OR of level-activated or transition-activated, depending on CF, CCFO, CCFl, CCF2, CCF3, and CCF4 in register bits ITO and ITI in register TCON. If ITx = 0, exter- CCON. None of these flags is cleared by hardware nal interrupt x is triggered by a detected low at the when the service routine is vectored to. Normally the INTx pin. If ITx = 1, external interrupt x is negative service routine will have to determine which bit flagged edge-triggered. The flags that actually generate these the interrupt and clear that bit in software. The PCA interrupts are bits lEO and lEI in TCON. These flags interrupt is enabled by bit EC in the Interrupt Enable are cleared by hardware when the service routine is register (see Table 16). In addition, the CF flag and vectored to only if the interrupt was transition-activat- each of the CCFn flags must also be enabled by bits ed. If the interrupt was level-activated, then the exter- ECF and ECCFn in registers CMOD and CCAPMn nal requesting source is what controls the request flag, respectively, in order for that flag to be able to cause an rather than the on-chip hardware. interrupt. Since the external interrupt pins are sampled once each machine cycle, an input high or low should hold for at 8.4 Serial Port Interrupt least 12 oscillator periods to ensure sampling. If the external interrupt is transition-activated, the external The serial port interrupt is generated by the logical OR source has to hold the request pin high for at least one of bits RI and TI in register SCON. Neither of these cycle, and then hold it low for at least one cycle to flags is cleared by hardware when the service routine is ensure that the transition is seen so that interrupt re- vectored to. The service routine will normally have to quest flag lEx will be set. lEx will be automatically determine whether it was RI or TI that generated the cleared by the CPU when the service routine is called. interrupt, and the bit will have to be cleared in soft- ware. If external interrupt INTO or INTI is level-activated, the external source has to hold the request active until the requested interrupt is actually generated. Then it 8.5 Interrupt Enable has to deactivate the request before the interrupt serv- ice routine is completed, or else another interrupt will Each of these interrupt sources can be individually en- be generated. abled or disabled by setting or clearing a bit in the Interrupt Enable (IE) register. (See Table 17.) Note that IE also contains a global disable bit, EA. If EA is 8.2 Timer Interrupts set (1), the interrupts are individually enabled or dis- abled by their corresponding bits in IE. If EA is clear Timer 0 and Timer 1 Interrupts are generated by TFO (0), all interrupts are disabled. and TFI in register TCON, which are set by a rollover in their respective Timer/Counter registers (except see Timer 0 in Mode 3). When a timer interrupt is generat- 8.6 Priority Level Structure ed, the flag that generated it is cleared by the on-chip hardware when the service routine is vectored to. Each interrupt source can also be individually pro- grammed to one of two priority levels, by setting or Timer 2 Interrupt is generated by the logical OR of bits clearing a bit in the Interrupt Priority (IP) register TF2 and EXF2 in register T2CON. Neither of these shown in Table 18. A low-priority interrupt can itself flags is cleared by hardware when the service routine is be interrupted by a higher priority interrupt, but not by vectored to. In fact, the service routine may have to another low-priority interrupt. A high priority inter- determine whether it was TF2 or EXF2 that generated rupt cannot be interrupted by any other interrupt the interrupt, and the bit will have to be cleared in source. software. 5-33 8XC51 FX HARDWARE DESCRIPTION Table 17. IE: Interrupt Enable Register IE Address = OASH Reset Value = 0000 00008 Bit Addressable EA I EC ET2 ES ET1 EX1 ETO EXO Bit 7 6 5 4 3 2 o Enable Bit = 1 enables the interrupt. Enable Bit = 0 disables it. Symbol Function EA Global disable bit. If EA = 0, all Interrupts are disabled. If EA = 1, each Interrupt can be individually enabled or disabled by setting or clearing its enable bit. EC PCA interrupt enable bit. ET2 Timer 2 interrupt enable bit. ES Serial Port interrupt enable bit. ET1 Timer 1 interrupt enable bit. EX1 External interrupt 1 enable bit. ETO Timer 0 interrupt enable bit. EXO External interrupt 0 enable bit. Table 18.IP: Interrupt Priority Registers IP Address = OB8H Reset Value = XOOO 00008 Bit Addressable PPC PT2 PS PT1 PX1 PTO PXO Bit 7 6 5 4 3 2 o Priority Bit = 1 assigns high priority Priority Bit = 0 assigns low priority Symbol Function Not implemented, reserved for future use. • PPC PCA interrupt priority bit. PT2 Timer 2 interrupt priority bit. PS Serial Port interrupt priority bit. PT1 Timer 1 interrupt priority bit. PX1 External interrupt 1 priority bit. PTa Timer a interrupt priority bit. PXO External interrupt a priority bit. NOTE: ·User software should not write 1s to reserved bits. These bits may be used in future 8051 family products to invoke new features. In that case, the reset or inactive value of the new bit will be 0, and its active value will be 1. The value read from a reserved bit is indeterminate. 5-34 inteL 8XC51FX HARDWARE DESCRIPTION If two requests of different priority levels are received Table 21. Priority Level Bit Values simultaneously, the request of higher priority level is serviced. If requests of the same priority level are re- Priority Bits Interrupt Priority ceived simultaneously, an internal polling sequence de- Level termines which request is serviced. Thus within each IPH.x IP.x priority level there is a second priority structure deter- mined by the polling sequence shown in Table 19. 0 0 Level 0 (Lowest) 0 1 Level 1 Note that the "priority within level" structure is only used to resolve simultaneous requests of the same priori- 1 0 Level 2 ty level. 1 1 Level 3 (Highest) Table 19. Interrupt Priority within Level Polling Sequence How Interrupts are Handled 1 (Highest) INTO 2 Timer 0 The interrupt flags are sampled at SSP2 of every ma- 3 INT1 chine cycle. The samples are polled during the follow- ing machine cycle. The Timer 2 interrupt cycle is 4 Timer 1 slightly different, as described in the Response Time 5 PCA section. If one of the flags was in a set condition at 6 Serial Port SSP2 of the preceding cycle, the polling cycle will find 7 (Lowest) Timer 2 it and the interrupt system will generate an LCALL to the appropriate service routine, provided this hard- ware-generated LCALL is not blocked by any of the 8XC51FX Interrupt Priority Structure following conditions: In the 8XCS1FX, a second Interrupt Priority register 1. An interrupt of equal or higher priority level is al- (IPH) has been added, increasing the number of priori- ready in progress. ty levels to four. Table 20 shows this second register. 2. The current (polling) cycle is not the final cycle in The added register becomes the MSB of the priority the execution of the instruction in progress. select bits and the existing IP register acts as the LSB. 3. The instruction in progress is RET! or any write to This scheme maintains compatibility with the rest of the IE or IP registers. the MCS-S1 family. Table 21 shows the bit values and priority levels associated with each combination. Table 20. IPH: Interrupt Priority High Register IPH Address = OB7H Reset Value = XOOO 0000 Not Bit Addressable PPCH PT2H PSH PT1H PX1H PTOH PXOH Bit 7 6 5 4 3 2 o Symbol Function Not implemented, reserved for future use. PPCH PCA interrupt priority high bit. PT2H Timer 2 interrupt priority high bit. PSH Serial Port interrupt priority high bit. PT1H Timer 1 interrupt priority high bit. PX1H External interrupt 1 priority high bit. PTOH Timer 0 interrupt priority high bit. PXOH External interrupt priority high bit. 5-35 intelqp 8XC51FX HARDWARE DESCRIPTION Any of these three conditions will block the generation Table 22. Interrupt Vector Address of the LCALL to the interrupt service routine. Condi- I I tion 2 ensures that the instruction in progress will be Interrupt Interrupt Cleared by Vector completed before vectoring to any service routine. Con- Source Request Bits Hardware Address dition 3 ensures that if the instruction in progress is INTO lEO No (level) 0OO3H RET! or any write to IE or IP, then at least one more Yes (trans.) instruction will be executed before any interrupt is vec- tored to. TIMER 0 TFO Yes OOOBH The polling cycle is repeated with each machine cycle, and the values polled are the values that were present at INT1 IE1 No (level) 0013H S5P2 of the previous machine cycle. If the interrupt Yes (trans.) flag for a level-sensitive external interrupt is active but not being responded to for one of the above conditions TIMER 1 TF1 Yes 001BH and is not still active when the blocking condition is removed, the denied interrupt will not be serviced. In SERIAL PORT RI,TI No 0023H other words, the fact that the interrupt flag was once active but not serviced is not remembered. Every poll- ing cycle is new. TIMER 2 TF2, EXF2 No 002BH The polling cycle/LCALL sequence is illustrated in PCA CF,CCFn No 0033H Figure 25. (n = 0-4) Note that if an interrupt of a higher priority level goes Execution proceeds from that location until the RETI active prior to S5P2 of the machine cycle labeled C3 in instruction is encountered. The RET! instruction in- Figure 25, then in accordance with the above rules it forms the processor that this interrupt routine is no will be vectored to during C5 and C6, without any in- longer in progress, then pops the top two bytes from the struction of the lower priority routine having been exe- stack and reloads the Program Counter. Execution of cuted. the interrupted program continues from where it left off. Thus the processor acknowledges an interrupt request by executing a hardware-generated LCALL to the ap- Note that a simple RET instruction would also have propriate servicing routine. The hardware-generated returned execution to the interrupted program, but it LCALL pushes the contents of the Program Counter would have left the interrupt control system thinking onto the stack (but it does not save the PSW) and re- interrupt was still in progress. loads the PC with an address that depends on the source of the interrupt being vectored to, as shown in Note that the starting addresses of consecutive inter- Table 22. rupt service routines are only 8 bytes apart. That means if consecutive interrupts are being used (lEO and TFO, for example, or TFO and lEI), and if the first interrupt routine is more than 7 bytes long, then that routine will have to execute a jump to some other memory location where the service routine can be completed without overlapping the starting address of the next interrupt routine. ••••• .. ·---C11--.~I...- - C 2 - -...--II.---C3 I. C4 • I. CS--.. · .. ISSP21 56 ........ ~. .\-,-.....L...-.....:l~)..~----L---41l~--L---- t-;-1 '"'"___ ,.._--A-------..v------~~---,......._····· INTERRUPTS LONG CALL TO INTERRUPT ROUTINE I EI ARE POLLED INTERRUPT INTERRUPT INTERRUPT VECTOR ADDRESS GOES LATCHED ACTIVE 270653-22 This is the fastest possible response when C2 is the final cycle of an instruction other than RETI or write IE or IP. Figure 25. Interrupt Response Timing Diagram 5-36 8XC51 FX HARDWARE DESCRIPTION 8.7 Response Time or write to IE or IP, the additional wait time cannot be more than 5 cycles (a maximum of one or more cycle to The INTO and INTI levels are inverted and latched complete the instruction in progress, plus 4 cycles to into the Interrupt Flags lEO and lEI at S5P2 of every complete the next instruction if the instruction is MUL machine cycle. Similarly, the Timer 2 flag EXF2 and or DIV). the Serial Port flags RI and TI are set at S5P2. The values are not actually polled by the circuitry until the Thus, in a single-interrupt system, the response time is next machine cycle. always more than 3 cycles and less than 9 cycles. The Timer 0 and Timer 1 flags, TFO and TF1, are set at S5P2 of the cycle in which the timers overflow. The 9.0 RESET values are then polled by the circuitry in the next cycle. However, the Timer 2 flag TF2 is set at S2P2 and is The reset input is the RST pin, which has a Schmitt polled in the same cycle in which the timer overflows. Trigger input. A reset is accomplished by holding the RST pin high for at least two machine cycles (24 oscil- If a request is active and conditions are right for it to be lator periods) while the oscillator is running. The CPU acknowledged, a hardware subroutine call to the re- responds by generating an internal reset, with the tim- quested service routine will be the next instruction to be ing shown in Figure 26. executed. The call itself takes two cycles. Thus, a mini- mum of three complete machine cycles elapses between The external reset signal is asynchronous to the internal activation of an external interrupt request and the be- clock. The RST pin is sampled during State 5 Phase 2 ginning of execution of the service routine's first in- of every machine cycle. ALE and PSEN will maintain struction. Figure 25 shows interrupt response timing. their current activities for 19 oscillator periods after a logic 1 has been sampled at the RST pin; that is, for 19 A longer response time would result if the request is to 31 oscillator periods after the external reset signal blocked by one of the 3 previously listed conditions. If has been applied to the RST pin. The port pins are an interrupt of equal or higher priority level is already driven to their reset state as soon as a valid high is in progress, the additional wait time obviously depends detected on the RST pin, regardless of whether the on the nature of the other interrupt's service routine. If clock is running. the instruction in progress is not in its final cycle, the additional wait time cannot be more than 3 cycles, since the longest instructions (MUL and DIV) are only 4 cycles long, and if the instruction in progress is RETI 1---12 OSC. PERIODS ----l I S5 I S6 I S 1 I S2 I S3 I S4 I S5 I S6 I S 1 I S2 I S3 I S4 I S5 I S6 I S 1 I S2 I S3 I S4 I RST: J II!!!!!!! ( SAMPLE, RST , SAMPLE RST CMERNAL RESET SIGNAL - ALE: I , n .~----~ ~----~ PSEN: po: , - 1 1 OSC. PERIODS --+,•.- - - - - 1 9 OSC. PERIODS ------.1. I 270653-23 Figure 26. Reset Timing 5-37 infel~ 8XC51FX HARDWARE DESCRIPTION While the RST pin is high, the port pins, ALE and Note that the port pins will be in a random state until the PSEN are weakly pulled high. After RST is pulled low, oscillator has started and the internal reset algorithm it will take 1 to i machine cycles for ALE and PSEN to has written Is to them. start clocking. For this reason, other devices can not be synchronized to the internal timings of the 8XC51FX. Powering up the device without a valid reset could cause the CPU to start executing instructions from an Driving the ALE and PSEN pins to 0 while reset is indeterminate location. This is because the SFRs, spe- active could cause the device to go into an indetermi- cifically the Program Counter, may not get properly nate state. initialized. The internal reset algorithm redefines all the SFRs. Ta- ble 1 lists the SFRs and their reset values. The internal 10.0 POWER-SAVING MODES OF RAM is not affected by reset. On power up the RAM OPERATION content is indeterminate. For applications where power consumption is critical, the C51FX provides two power reducing modes of op- 9.1 Power-On Reset eration: Idle and Power Down. The input through which backup power is supplied during these opera- For CHMOS devices, when VCC is turned on, an auto- tions is Vee. Figure 28 shows the internal circuitry matic reset can be obtained by connecting the RST pin which implements these features. In the Idle mode to VCC through a 1 p.F capacitor (Figure 27). The (IDL = I), the oscillator continues to run and the In- CHMOS devices do not require an external resistor like terrupt, Serial Port, PCA, and Timer blocks continue the HMOS devices because they have an internal pull- to be clocked, but the clock signal is gated off to the down on the RST pin. CPU. In Power Down (PD = 1), the oscillator is fro- zen. The Idle and Power Down modes are activated by When power is turned on, the circuit holds the RST pin setting bits in Special Function Register PCON (Table high for an amount of time that depends on the capaci- 23). tor value and the rate at which it charges. To ensure a valid reset the RST pin must be held high long enough to allow the oscillator to start up plus two machine 10.1 Idle Mode cycles. An instruction that sets PCON.O causes that to be the last instruction executed before going into the Idle Vcc--~~--------------~ mode. In the Idle mode, the internal clock signal is gated off to the CPU, but not to the Interrupt, Timer, and Serial Port functions. The PCA can be pro- + Vec grammed either to pause or continue operating during Idle (refer to the PCA section for more details). The 8XC51 FA/Fe/Fe CPU status is preserved in its entirety: the Stack Point- er, Program Counter, Program Status Word, Accumu- RST lator, and all other registers maintain their data during Idle. The port pins hold the logical states they had at the time Idle was activated. ALE and PSEN hold at Vss logic high levels. There are two ways to terminate the Idle Mode. Activa- 270653-24 tion of any enabled interrupt will cause PCON.O to be Figure 27. Power on Reset Circuitry cleared by hardware, terminating the Idle mode. The interrupt will be serviced, and following RETI the next On power up, Vee should rise within approximately instruction to be executed will be the one following the ten milliseconds. The oscillator start-up time will de- instruction that put the device into Idle. pend on the oscillator frequency. For a 10 MHz crystal, the start-up time is typically 1 msec. For a 1 MHz The flag bits (GFO and GFl) can be used to give an crystal, the start-up time is typically 10 msec. indication if an interrupt occurred during normal oper- ation or during Idle. For example, an instruction that With the given circuit, reducing Vcc quickly to 0 caus- activates Idle can also set one or both flag bits. When es the RST pin voltage to momentarily fall below OV. Idle is terminated by an interrupt, the interrupt service However, this voltage is internally limited and will not routine can examine the flag bits. harm the device. 5-38 in1et 8XC51FX HARDWARE DESCRIPTION The other way of tenninating the Idle mode is with a The signal at the RST pin clears the IDL bit directly hardware reset. Since the clock oscillator is still run- and asynchronously. At this time the CPU resumes ning, the hardware reset needs to be held active for only program execution from where it left off; that is, at the two machine cycles (24 oscillator periods) to complete instruction following the one that invoked the Idle the reset. Mode. As shown in Figure 26, two or three machine cycles of program execution may take place before the ~~ XTAL 2 -= XTAL 1 INTERRUPT, ..........-C>SERIAL PORT, TIMER BLOCKS CPU 270653-25 Figure 28. Idle and Power Down Hardware Table 23. PCON: Power Control Register PCON Address = 87H Reset Value = OOXX OOOOB Not Bit Addressable ISMOD1 ISMODO I POF GF1 GFO PO IDL Bit 7 6 5 4 3 2 o Symbol Function SMOD1 Double Baud rate bit. When set to a 1 and Timer 1 is used to generate baud rates, and the Serial Port is used in modes 1, 2, or 3. SMODO When set, Read/Write accesses to SCON.7 are to the FE bit. When clear, Read/Write accesses to SCON.7 are to the SMO bit. Not implemented, reserved for future use. * POF Power Off Flag. Set by hardware on the rising edge of Vee. Set or cleared by software. This flag allows detection of a power failure caused reset. Vee must remain above 3V to retain this bit. GF1 General-purpose flag bit. GFO General-purpose flag bit. PD Power Down bit. Setting this bit activates Power Down operation. IDL Idle mode bit. Setting this bit activates idle modes operation. If 1s are written to PO and IDL at the same time, PO takes precedence. NOTE: ·User software should not write 1s to unimplemented bits. These bits may be used in future 8051 family products to invoke new features. In that case, the reset or inactive value of the new bit will be 0, and its active value will be 1. The value read from a reserved bit is indeterminate 5-39 intel4P 8XC51FX HARDWARE DESCRIPTION internal reset algorithm takes control. On-chip hard- warm start reset occurs while Vee is still applied to the ware inhibits access to the internal RAM during this device and could be generated, for example, by a time, but access to the port pins is not inhibited. To Watchdog Timer or an exit from Power Down. . eliminate the possibility of unexpected outputs at the port pins, the instruction following the one that invokes Immediately after reset, the user's software can check Idle should not be one that writes to a port pin or to the status of the POF bit. POF = 1 would indicate a external Data RAM. cold start. The software then clears POF and com- mences its tasks. POF = 0 immediately after reset would indicate a warm start. 10.2 Power Down Mode Vee must remain above 3 volts for POF to retain a o. An instruction that sets PCON.1 causes that to be the last instruction executed before going into the Power Down mode. In this mode the on-chip oscillator is 11.0 EPROM VERSIONS stopped. With the clock frozen, all functions are stopped, but the on-chip RAM and Special Function The 8XC51FX uses the Improved "Quick-Pulse" pro- Registers are held. The port pins output the values held grammingTM algorithm. These devices program at Vpp by their respective SFRs, and ALE and PSEN output = 12.75V (and Vee = 5.0V) using a series of five lows. In Power Down Vcc can be reduced to as low as 100 J1.s PROG pulses per byte programmed. This re- 2V. Care must be taken, however, to ensure that Vee is sults in a total programming time of approximately 5 not reduced before Power Down is invoked. seconds for the 87C51FA's 8 Kbytes, 10 seconds for the 87C51FB's 16 Kbytes, and 20 seconds for the The C51FX can exit Power Down with either a hard- 87C51FC's 32 Kbytes. ware reset or external interrupt. Reset redefines all the SFRs but does not change the on-chip RAM. An exter- Exposure to Light: The EPROM window must be cov- nal interrupt allows both the SFRs and the on-chip ered with an opaque label when the device is in opera- RAM to retain their values. tion. This is not so much to protect the EPROM array from inadvertent erasure, but to protect the RAM and To properly terminate Power Down the reset or exter- other on-chip logic. Allowing light to impinge on the nal interrupt should not be executed before Vee is silicon die while the device is operating can cause logi- restored to its normal operating level and must be held cal malfunction. active long enough for the oscillator to restart and sta- bilize (normally less than 10 msec). 12.0 PROGRAM MEMORY LOCK With an external interrupt, INTO or INTI must be en- abled and configured as level-sensitive. Holding the pin In some microcontroller applications, it is desirable low restarts the oscillator and bringing the pin back that the Program Memory be secure from software pi- high completes the exit. After the RETI instruction is racy. The C51FX has varying degrees of program pro- executed in the interrupt service routine, the next in- tection depending on the device. Table 24 outlines the struction will be the one following the instruction that lock schemes available for each device. put the device in Power Down. Encryption Array: Within the EPROMIROM is an ar- ray of encryption bytes that are initially unprogrammed 10.3 Power Off Flag (all I's). For EPROM devices, the user can program the encryption array to encrypt the program code bytes The Power Off Flag (pOp) located at PCON.4, is set during EPROM verification. For ROM devices, the by hardware when Vee rises from 0 to 5 Volts. POF user submits the encryption array to be programmed by can also be set or cleared by software. This allows the the factory. If an encryption array is submitted, LBI user to distinguish between a "cold start" reset and a will also be programmed by the factory. The encryption "warm start" reset. array is not available without the Lock Bit. Program code verification is performed as usual, except that each A cold start reset is one that is coincident with Vee code byte comes out exclusive-NOR'ed (XNOR) with being turned on to the device after it was turned off. A 5-40 intel~ 8XC51FX HARDWARE DESCRIPTION one of the key bytes. Therefore, to read the Table 24. C51FX Program Protection ROMIEPROM code, the user has to know the encryp- tion key bytes in their proper sequence. Device Lock Bits Encrypt Array 83C51FA None None Unprogrammed bytes have the value OFFH. If the En- cryption Array is left unprogrammed, all the key bytes 83C51FB LB1 64 Bytes have the value OFFH. Since any code byte XNOR'ed 83C51FC LB1 64 Bytes with OFFH leaves the byte unchanged, leaving the En- cryption Array unprogrammed in effect bypasses the 87C51FA LB1,LB2, LB3 64 Bytes encryption feature. 87C51FB LB1,LB2,LB3 64 Bytes When using the encryption array feature, one impor- 87C51FC LB1,LB2,LB3 64 Bytes tant factor should be considered. If a code byte has the value OFFH, verifying the byte will produce the encryp- tion byte value. If a large block (> 64 bytes) of code is left unprogrammed, a verification routine will display 13.0 ONCETM MODE the encryption array contents. For this reason all un- The ONCE (ON-Circuit Emulation) mode facilitates used code bytes should be programmed with some val- testing and debugging of systems using the C51FX ue other than OFFH, and not all of them the same val- without having to remove the device from the circuit. ue. This will ensure maximum program protection. The ONCE mode is invoked by: Program Lock Bits: Also included in the Program 1. Pulling ALE low while the device is in reset and Lock scheme are Lock Bits which can be enabled to PSEN is high; provide varying degrees of protection. Table 25 lists the 2. Holding ALE low as RST is deactivated. Lock Bits and their corresponding influence on the mi- crocontroller. Refer to Table 24 for the Lock Bits avail- While the device is in ONCE mode, the Port 0 pins go able on the various products. The user is responsible for into a float state, and the other port pins, ALE, and programming the Lock Bits on EPROM devices. On PSEN are weakly pulled high. The oscillator circuit ROM devices, LBI is automatically set by the factory remains active. While the device is in this mode, an when the encryption array is submitted. The Lock Bit emulator or test CPU can be used to drive the circuit. is not available without the encryption array on ROM devices. Normal operation is restored after a valid reset is ap- plied. Erasing the EPROM also erases the Encryption Array and the Lock Bits, returning the part to full functionali- ty. Table 25. Lock Bits Program Lock Bits Protection Type LB1 LB2 LB3 1 U U U No program lock features enabled. (Code verify will still be encrypted by the encryption array if programmed.) 2 P U U MOVC instructions executed from external program memory are disabled from fetching code bytes from internal memory, EA is sampled and latched on reset, and further programming of the EPROM is disabled. 3 P P U Same as 2, also verify is disabled. 4 P P P Same as 3, also external execution is disabled. P = Programmed U = Unprogrammed Any other combination of the Lock Bits is not defined. 5-41 intel~ 8XC51FX HARDWARE DESCRIPTION 14.0 ON-CHIP OSCILLATOR Frequency, tolerance, and temperature range are deter- mined by the system requirements. The on-chip oscillator for the CHMOS devices, shown in Figure 29, consists of a single stage linear inverter A ceramic resonator can be used in place of the crystal intended for use as a crystal-controlled, positive reac- in cost-sensitive applications. When a ceramic resona- tance oscillator. In this application the crystal is operat- tor is used, Cl and C2 are normally selected as higher ing in its fundamental response mode as an inductive values, typically 47 pF. The manufacturer ofthe ceram- reactance in parallel resonance with capacitance exter- ic resonator should be consulted for recommendations nal to the crystal (Figure 30). on the values of these capacitors. The oscillator on the CHMOS devices can be turned off A more in-depth discussion of crystal specifications, ce- under software control by setting the PD bit in the ramic resonators, and the selection of values for Cl and peON register. The feedback resistor Rr in Figure 29 C2 can be found in Application Note AP-155, "Oscilla- consists of paralleled n- and p-channel FETs controlled tors for Microcontrollers" in the Embedded Applica- by the PD bit, such that Rr is opened when PD = 1. tions handbook. The diodes Dl and D2, which act as clamps to Vee and V ss, are parasitic to the Rr FETs. To drive the CHMOS parts with an external clock source, apply the external clock signal to XTALI and The crystal specifications and capacitance values (Cl leave XTAL2 floating as shown in Figure 31. This is an and C2 in Figure 30) are not critical. 30 pF can be used important difference from the HMOS parts. With in these positions at any frequency with good quality HMOS, the external clock source is applied to XTAL2, crystals. In general, crystals used with these devices and XTALI is grounded. typically have the following specifications: ESR (Equivalent Series Resistance) see Figure 32 An external oscillator may encounter as much as a 100 pF load at XTALl when it starts up. This is due to Co (shunt capacitance) 7.0 pF maximum interaction between the amplifier and its feedback ca- CL (load capacitance) 30 pF ± 3 pF pacitance. Once the external signal meets the V IL and V IH specifications the capacitance will not exceed Drive Level lMW 20pF. TO INTERNAL Vcc nMINGCKTS D1 4000 XTAL1 XTAU D2 r PD 270653-26 Figure 29. On-Chip Oscillator Circuitry 5-42 intel~ 8XC51 FX HARDWARE DESCRIPTION VCC TO INTERNAL TIMING CKTS Vss XTAL2------ 8OC51 . ----QUARTZ CRYSTAL OR CERAMIC RESONATOR 270653-27 Figure 30. Using the CHMOS On-Chip Oscillator 8XC51FX 500 ~400 J: ~ 300 II:: ~ 200 270653-28 100 Figure 31. Driving the CHMOS Parts with an External Clock Source 4 8 12 16 CRYSTAL FREQUENCY in tdHz 15.0 CPU TIMING 270653-29 The internal clock generator defines the sequence of Figure 32. ESR vs Frequency states that make up a machine cycle. A machine cycle consists of 6 states, numbered SI through S6. Each each other. The timings published in the data sheets state time lasts for two oscillator periods. Thus a ma- include the effects of propagation delays under the chine cycle takes 12 oscillator periods or 1 microsecond specified test condition. if the oscillator frequency is 12 MHz. Each state is then divided into a Phase 1 and Phase 2 half. ADDITIONAL REFERENCES Rise and fall times are dependent on the extemalload- ing that each pin must drive. They are approximately The following application notes provide supplemental 10 nsec, measured between O.8V and 2.0V. information to this document and can be found in the Embedded Applications handbook. Propagation delays are different for different pins. For 1. AP-125 "Designing Microcontroller Systems for a given pin they vary with pin loading, temperature, Electrically Noisy Environments" Vee, and manufacturing lot. If the XTALI waveform 2. AP-155 "Oscillators for Microcontrollers" is taken as the timing reference, propagation delays may vary from 25 to 125 nsec. 3. AP-252 "Designing with the 80C51BH" 4. AP-410 "Enhanced Serial Port on the 83C51FA" The AC Timings section of the data sheets do not refer- 5. AP-415 "83C51FA/FB PCA Cookbook" ence any timing to the XTALl waveform. Rather, they relate the critical edges of control and input signals to 6. AB-41 "Software Serial Port Implemented with the PCA" 7. AP-425 "Small DC Motor Control" 8. The appropriate data sheet. 5-43 87C51GB Hardware 6 Description 87C51 GB Hardware Description CONTENTS PAGE CONTENTS PAGE 1.0 INTRODUCTION TO THE 7.0 PROGRAMMABLE COUNTER 8XC51GB ................................................ 6-3 ARRAy .................................................. 6-23 7.1 PCA Timer/Counter ........................ 6-24 2.0 MEMORY ORGANIZATION ................. 6-3 Reading the PCA Timer .................. 6-26 2.1 Program Memory ............................. 6-3 7.2 Compare/Capture Modules ............ 6-26 2.2 Data Memory ................................... 6-3 7.3 PCA Capture Mode ........................ 6-27 3.0 SPECIAL FUNCTION 7.4 Software Timer Mode ..................... 6-29 REGISTERS ........................................... 6-5 7.5 High Speed Output Mode .............. 6-30 4.0 1/0 PORTS ............................................ 6-8 7.6 Watchdog Timer Mode ................... 6-30 4.1 I/O Configurations ............................ 6-8 7.7 Pulse Width Modulator Mode ......... 6-31 4.2 Writing to a Port ............................... 6-9 8.0 SERIAL PORT .................................... 6-33 4.3 Port Loading and Interfacing .......... 6-10 8.1 Framing Error Detection ................ 6-35 4.4 Read-Modify-Write Instructions ...... 6-10 8.2 Multiprocessor Communications .... 8-35 4.5 Accessing External Memory ........... 6-11 8.3 Automatic Address R~cognition ..... 6-36 5.0 TIMER/COUNTERS ........................... 6-13 8.4 Baud Rates .................................... 6-36 5.1 Timer 0 and Timer 1 ....................... 6-13 8.5 Timer 1 to Generate Baud Mode 0 ............................................ 6-14 Rates ................................................ 6-36 Mode 1 ............................................ 6-15 8.6 Timer 2 to Generate Baud Rates ................................................ 6-37 Mode 2 ............................................ 6-16 Mode 3 ............................................ 6-16 9.0 SERIAL EXPANSION PORT .............. 6-38 5.2 Timer 2 ........................................... 6-17 9.1 Programmable Modes and Clock Options ............................................. 6-39 Timer 2 Capture Mode .................... 6-18 9.2 SEP Transmission or Reception .... 6-40 Timer 2 Auto-Reload Mode ............. 6-18 5.3 Programmable Clock Out .............. 6-20 10.0 HARDWARE WATCHDOG TIMER ................................................... 6-40 6.0 AID CONVERTER .............................. 6-21 10.1 Using the WDT ............................ 6-40 6.1 AID Special Function Registers ..... 6-21 10.2 WDT During Power Down and 6.2 AID Comparison Mode .................. 6-22 Idle ................................................... 6-40 6.3 AID Trigger Mode ........................... 6-22 11.0 OSCILLATOR FAIL DETECT ........... 6-40 6.4 AID Input Modes ............................ 6-22 11.1 OFD During Power Down ............. 6-41 6.5 Using the AID with Fewer than 8 Inputs ............................................... 6-22 6.6 AID in Power Down ........................ 6-23 6-1 CONTENTS PAGE CONTENTS PAGE 12.0 INTERRUPTS ................................... 6-41 14.0 POWER-SAVING MODES ............... 6-49 12.1 External Interrupts ....................... 6-41 14.1 Idle Mode ..................................... 6-51 12.2 Timer Interrupts ............................ 6-43 14.2 Power Down Mode ...................... 6-51 12.3 peA Interrupt ............................... 6-43 14.3 Power Off Flag ............................. 6-51 12.4 Serial Port Interrupt.. .................... 6-43 15.0 EPROM/OTP PROGRAMMING .. ..... 6-52 12.5 Interrupt Enable ........................... 6-43 15.1 Program Memory Lock ................ 6-52 12.6 Interrupt Priorities ........................ 6-45 Program Lock Bits ............................ 6-52 12.7 Interrupt Processing ..................... 6-47 12.8 Interrupt Response Time ............. 6-48 16.0 ONCE MODE .... ............................... 6-52 13.0 RESET .............................................. 6-49 17.0 ON-CHIP OSCiLLATOR ................... 6-52 13.1 Power-On Reset .......................... 6-49 18.0 CPU TIMING .................................... 6-54 6-2 intel~ 87C51GB HARDWARE DESCRIPTION 1.0 INTRODUCTION TO THE • Interrupt Structure with 8XC51GB - 15 interrupt sources - Four priority levels The 8XC51GB is a highly integrated 8-bit microcon- troller based on the MCS®-51 architecture. As a mem- • Power-Saving Modes ber of the MCS-51 family, the 8XC51GB is optimized - Idle Mode' for control applications. Its key features are an analog - Power Down Mode to digital converter and two programmable counter ar- rays (PCA) capable of measuring and generating pulse The table below summarizes the product names of the information on ten I/O pins. Also included are an en- various 8XC51GB products currently available. hanced serial port for multi-processor communications, Throughout this document, the products will generally a serial expansion port, hardware watchdog timer, os- be referred to as the 8XC51GB. Figure 1 shows a func- cillator fail detection, an up/down timer/counter and a tional block diagram of the 8XC51GB. program lock scheme for the on-chip program memory. Since the 8XC51GB is CHMOS, it has two software selectable reduced power modes: Idle Mode and Power ROMI ROM OTP ROMless RAM Down Mode. OTP Device Version Version Bytes Bytes The 8XC51GB used the standard 8051 instruction set and is functionally compatible with the existing 87C51GB 87C51GB 80C51GB 8K 256 MCS-51 family of products. This document presents a comprehensive description of 2.0 MEMORY ORGANIZATION the on-chip hardware features of the 8XC51GB. It be- gins with a discussion of how the memory is organized, All MCS-51 devices have a separate address space for followed by the instruction set, and then discusses each Program Memory and Data Memory. The logical sepa- of the peripherals listed below. ration of Program and Data Memory allows the Data • Six 8-bit Bidirectional Parallel Ports Memory to be accessed by S-bit addresses, which can be more quickly stored and manipulated by an 8-bit CPU. • Three 16-bit Timer/Counters with Nevertheless, 16-bit Data Memory addresses can also - One Up/Down Timer/Counter be generated through the DPTR register. Up to - Programmable Clock Output 64 Kbytes each of external Program and Data Memory can be addressed. • Analog to Digital Converter with - 8 channels - 8-bit resolution 2.1 Program Memory - compare mode Program Memory can only be read, not written to. • Two Programmable Counter Arrays with There can be up to 64 Kbytes of Program Memory. The read strobe for external Program Memory is the - Compare/Capture signal PSEN (program Store Enable). PSEN is not acti- - Software Timer vated for internal program fetches. - High Speed Output If the EA (External Access) pin is connected to Vss, all - Pulse Width Modulator program fetches are directed to external memory. For - Watchdog Timer (pCA only) the ROMless devices, all:...E!ogram fetches must be to • Full-Duplex Programmable Serial Port with external memory. If the EA pin is connected to Vee, then program fetches greater than SK are to external - Framing Error Detection addresses for the 8XC51GB products. - Automatic Address Recognition • Serial Expansion Port On the 87C51GB with EA connected to Vee, program fetches to addresses OOOOH through IFFFH are to in- - four programmable modes ternal ROM, and fetches to addresses 2000H through - four selectable frequencies FFFFH are to external memory. • Hardware Watchdog Timer • Reset 2.2 Data Memory - asynchronous - active low The 8XC51 GB implements 256 bytes of on-chip data RAM. The memory space is divided into three blocks, • Oscillator Fail Detection 6-3 infel~ 87C51GB HARDWARE DESCRIPTION P2.0-P2.7 SF"Rs TIMERs P.C.A P.CA.l SERIAL PORTS PSEN AL~PROG EA/VPP RESET --":~-'l....--'-TRIGIN COMPREf AVREF ~~~_~T""AVSS XTALl P1.0-Pl.7 PS.O-PS.7 P4.0-P4.7 P3.0- P3.7 270897-1 Figure 1. 87C51GB Block Diagram which are generally referred to as the Lower 128, the When an instruction accesses an intemallocation above Upper 128, and SFR space. The Upper 128 bytes occu- address 7FH, the CPU knows whether the access is to py a parallel address space to the Special Function Reg- the upper 128 bytes of data RAM or to SFR space by isters. That means they have the same addresses, but the addressing mode used in the instruction. Instruc- they are physically separate from SFR space. tions that use direct addressing access SFR space. For example, The Lower 128 bytes of RAM are present in all MeS-51 devices. All ofthe bytes in the Lower 128 can MOV OAOH, data be accessed by either direct or indirect addressing. The lowest 32 bytes are grouped into 4 banks of 8 registers. accesses the SFR at location OAOH (which is P2). In- Program instructions call out these registers as RO structions that use indirect addressing access the upper through R7. Two bits in the Program Status Word 128 bytes of data RAM. For example, (pSW) select which register bank is in use. This allows more efficient use of code space, since register instruc- MOV @RO, data tions are shorter than instructions that use direct ad- dressing. 6-4 infel~ 87C51GB HARDWARE DESCRIPTION where RO contains OAOH, accesses the data byte at ad- latches, timers, peripheral controls, etc. These registers dress OAOH, rather than P2 (whose address is OAOH). can only be accessed by direct addressing. Sixteen ad- Note that stack operations are examples of indirect ad- dresses in SFR space are both byte- and bit-addressable. dressing, so the upper 128 bytes of data RAM are avail- The bit-addressable SFRs are those whose address ends able as stack space. in OOOB. The bit addresses in this area are 80H through OFFH. 3.0 SPECIAL FUNCTION REGISTERS Not all of the addresses are occupied. Unoccupied ad- dresses are not implemented on the chip. Read accesses A map of the on-chip memory area called by the SFR to these addresses will in general return random data, (Special Function Register) space is shown in Table 1. and write accesses will have no effect. Special Function Registers (SFRs) include the Port Table 1. SFR Mapping and Reset Values P5 CH CCAPOH CCAP1H CCAP2H CCAP3H CCAP4H F8 FF 00000000 00000000 XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX *B AD7 SEPSTAT FO F7 00000000 00000000 XXXXXOOO C1CON CL CCAPOL CCAP1L CCAP2L CCAP3L CCAP4L E8 EF 00000000 00000000 XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX *ACC AD6 SEPDAT EO E7 00000000 00000000 XXXXXXXX CCON CMOD CCAPMO CCAPM1 CCAPM2 CCAPM3 CCAPM4 D8 DF OOXOOOOO OOXXXOOO XOOOOOOO XOOOOOOO XOOOOOOO XOOOOOOO XOOOOOOO *PSW AD5 SEPCON DO D7 00000000 00000000 XXOOOOOO T2CON T2MOD RCAP2L RCAP2H TL2 TH2 C8 CF 00000000 XXXXXXOO 00000000 00000000 00000000 00000000 P4 AD4 EXICON ACMP co 00000000 00000000 XOOOOOOO 00000000 C7 *IP SADEN C1CAPOH C1CAP1H C1CAP2H C1CAP3H C1CAP4H CH1 B8 BF XOOOOOOO 00000000 XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX 00000000 *P3 AD3 IPAH IPA IPH BO B7 11111111 00000000 00000000 00000000 XOOOOOOO *IE SADDR C1CAPOL C1CAP1L C1CAP2L C1CAP3L C1CAP4L CL1 A8 AF 00000000 00000000 XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX 00000000 *P2 AD2 OSCR WDTRST lEA AO A7 00000000 00000000 XXXXXXXO XXXXXXXX 00000000 *SCON *SBUF C1CAPMO C1CAPM1 C1CAPM2 C1CAPM3 C1CAPM4 C1MOD 98 9F 00000000 XXXXXXXX XOOOOOOO XOOOOOOO XOOOOOOO XOOOOOOO XOOOOOOO XXXXOOOO *P1 AD1 ACON 90 97 00000000 00000000 XXOOOOOO *TCON *TMOD *TLO *TL1 *THO *TH1 88 8F 00000000 00000000 00000000 00000000 00000000 00000000 *PO *SP *DPL *DPH ADO *PCON** 80 87 11111111 00000111 00000000 00000000 00000000 OOXXOOOO * = Found in the 8051 core (see 8051 Hardware DeSCription for explanations of these SFRs). ** = See description of peON SFR. Bit PCON.4 is not affected by reset. X = Undefined. 6-5 infel~ 87C51GB HARDWARE DESCRIPTION User software should not write 1's to these unimple- Ports 0 to 5 Registers: PO, PI, P2, P3, P4, and P5 are mented locations, since they may be used in future the SFR latches of Ports 0 through 5 respectively. MCS-51 products to invoke -new features. In that case the reset or inactive values of the new bits will always Timer Registers: Register pairs (THO, TLO), (TH1, be 0, and their active values will be 1. TLl) and (TH2, TL2) are the 16-bit count registers for Timer/Counters 0, 1, and 2 respectively. Control and The functions of the SFRs are outlined below. More status bits are contained in registers TCON and TMOD information on the use of specific SFRs for each periph- for Timers 0 and 1 and in registers T2CON and eral is included in the description of that peripheral. T2MOD for Timer 2. The register pair (RCAP2H, RCAP2L) are the capture/reload registers for Timer 2 Accumulator: ACC is the Accumulator register. The in 16-bit capture mode or 16-bit auto-reload mode. mnemonics for Accumulator-Specific instructions, however, refer to the Accumulator simply as A. Programmable Counter Array (PCA and PCAl) Regis- ters: The 16-bit PCA and PCA1 timer/counters consist B Register: The B register is used during multiply and of register CH (CHI) and CL (CL1). Registers CCON divide operations. For other instructions it can be treat- (C1CON) and CMOD (C1MOD) contain the control ed as another scratch pad register. and status bits for the PCA (and PCA1). The CCAPMn (n = 0, 1, 2, 3, or 4) and the CICAPMn Stack Pointer: The Stack Pointer Register is 8 bits registers control the mode for each of the five PCA and wide. It is incremented before data is stored during the five PCA1 modules. The register pairs (CCAPnH, PUSH and CALL executions. The stack may reside CCAPnL and C1CAPnH, C1CAPnL) are the 16-bit anywhere in on-chip RAM. On reset, the Stack Pointer compare/capture registers for each PCA and PCA 1 is initialized to 07H causing the stack to begin at loca- module. tion 08H. Serial Port Registers: The Serial Data Buffer, SBUF, is Data Pointer: The Data Pointer (DPTR) consists of a actually two separate registers: a transmit buffer and a high byte (DPH) and a low byte (DPL). Its intended receive buffer register. When data is moved to SBUF, it function is to hold a 16-bit address, but it may be ma- comes from the receive buffer. Register SCON contains nipUlated as a 16-bit register or as two independent the control and status bits for the Serial Port. Registers 8-bit registers. SADDR and SADEN are used to defme the Given and the Broadcast addresses for the Automatic Address Program Status Word: The PSW register contains pro- Recognition feature. gram status information as detailed in Table 2. Table 2. PSW: Program Status Word Register PSW Address = ODOH Reset Value = 0000 OOOOB Bit Addressable CY AC FO I RS1 RSO I I OV P Bit 7 6 543 2 o Symbol Function CY Carry flag. AC Auxiliary Carry flag. (For BCD Operations) FO Flag O. (Available to the user for general purposes). RS1 Register bank select bit 1. RSO Register bank select bit o. RS1 RSO Working Register Bank and Address o 0 Bank 0 (00H-07H) o 1 Bank 1 (OBH-OFH) o Bank 2 (10H-17H) 1 Bank 3 (1BH-1FH) OV Overflow flag. User definable flag. P Parity flag. Set! cleared by hardware each instruction cycle to indicate an odd/even number of "one" bits in the Accumulator, i.e., even parity. 6-6 intet 87C51GB HARDWARE DESCRIPTION Serial Expansion Port Registers: The Serial Expansion channels 0 through 7 respectively. The register ACMP Port is controlled through the register SEPCON. contains the results of the AID comparison feature. SEPDAT contains data for the Serial Expansion Port ACON is the control register for AID conversions. and SEPSTAT is used to monitor its status. Power Control Register: PCON controls the Power Re- Interrupt Registers: The individual interrupt enable duction Modes, Idle and Power Down. bits are in the IE and lEA registers. One of four priori- ty levels can be selected for each interrupt using the IP, Oscillator Fail Detect Register: The OSCR register is IPH, IPA and IPAH registers. The EXICON register used both to monitor the status of the OFD circuitry controls the selection of the activation polarity for ex- and to disable the feature. ternal interrupts two and three. Watchdog Timer Register: The WatchDog Timer Analog to Digital Converter Registers: The results of ReSeT (WDTRST) register is used to keep the watch- AID conversions are placed in registers ADO, AD!, dog timer from periodically resetting the part. AD2, AD3, AD4, ADS, AD6, and AD7 for analog Table 3. Alternate Port Functions Port Pin Alternate Function PO.O/ ADO-PO. 7 / AD7 Multiplexed Byte of Address/Data for external memory. P1.0/T2 Timer 2 External Clock InputiClockout P1.1/T2EX Timer 2 Reload/Capture/Direction Control P1.2/ECI PCA External Clock Input P1.3/CEXO PCA Module 0 Capture Input, Compare/PWM Output P1.4/CEX1 PCA Module 1 Capture Input, Compare/PWM Output P1.5/CEX2 PCA Module 2 Capture Input, Compare/PWM Output P1.6/CEX3 PCA Module 3 Capture Input, Compare/PWM Output P1.7/CEX4 PCA Module 4 Capture Input, Compare/PWM Output P2.0/ A8-P2. 7 / A 15 High Byte of Address for External Memory P3.0/RXD Serial Port Input P3.1/TXD Serial Port Output P3.2/INTO External Interrupt 0 P3.3/INT1 External Interrupt 1 P3.4/TO Timer 0 External Clock Input P3.5/T1 Timer 1 External Clock Input P3.6/WR Write Strobe for External Memory P3.7/RD Read Strobe for External Memory P4.0/SEPCLK Clock Source for SEP P4.1/SEPDAT Data I/O for SEP P4.2/ECI1 PCA1 External Clock Input P4.3/C1EXO PCA 1 Module 0, Capture Input, Compare/PWM Output P4.4/C1EX1 PCA1 Module 1, Capture Input, Compare/PWM Output P4.5/C1EX2 PCA 1 Module 2, Capture Input, Compare/PWM Output P4.6/C1EX3 PCA 1 Module 3, Capture Input, Compare/PWM Output P4.7/C1EX4 PCA 1 Module 4, Capture Input, Compare/PWM Output P5.2/INT2 External Interrupt 2 P5.3/INT3 External Interrupt 3 P5.4/INT4 External Interrupt 4 P5.5/INT5 External Interrupt 5 P5.6/1NT6 External Interrupt 6 NOTE: The alternate functions can only be activated if the corresponding bit latch in the port SFR contains a 1. Otherwise the port pin will not go high. 6-7 87C51GB HARDWARE DESCRIPTION 4.0 1/0 PORTS 4.1 1/0 Configurations All six ports in the 8XCSIGB are bidirectional. Each Functional diagrams of a bit latch and I/O buffer in consists of a latch (Special Function Register PO each of the four ports are shown in Figure 2. through PS), output driver and an input buffer. All the ports, except for Port 0, have Schmitt Trigger inputs. The bit latch (one bit in the port's SFR) is represented as a Type D flip-flop, which clocks in a value from the The output drivers of Ports 0 and 2, and the input buff- internal bus in response to a "write to latch" signal ers of Port 0, are used in accesses to external memory. from the CPU. The Q output of the flip-flop is placed In this application, Port 0 outputs the low byte of the on the internal bus in response to a "read latch" signal external memory address, time-multiplexed with the from the CPU. The level of the port pin itself is placed byte being written or read. Port 2 outputs the high byte on the internal bus in response to a "read pin" signal of the external memory address when the address is 16 from the CPU. Some instructions that read a port acti- bits wide. Otherwise the Port 2 pins continue to emit vate the "read latch" signal, and others activate the the P2 SFR content. "read pin" signal. Those that read the latch are the Read-Modify-Write instructions. All the Port 1, Port 3, Port 4 and most of Port 5 pins are multi-functional. They are not only port pins, but The output drivers of Ports 0 and 2 are switchable to an also serve the functions of various special features as internal ADDRESS and ADDRESS/DATA bus by an shown in Table 3. internal control signal for use in external memory ac- cesses. During external memory accesses, the P2 SFR ADDR/DATA ALTERNATE OUTPUT FUNCTION INT. B;;,..;U;,.;;S_..........-t WRITE TO LATCH WRITE TO LATCH READ PIN 270897-2 ALTERNATE A. PortO Bit INPUT FUNCTION 270897-3 B. Port 1,3,4, or 5 Bit ADDR VCC CONTROL READ LATCH INT. BUS WRITE TO LATCH 270897-4 C. Port 2 Bit "See Figure 4 for details of the internal pulJup. Figure 2. 8XC51GB Port Bit Latches and 1/0 Buffers 6-8 intel~ 87C51GB HARDWARE DESCRIPTION remains unchanged, but the PO SFR gets 1s written to port lines are open drain. Writing a 1 to the bit latch it. leaves both output FETs off, which floats the pin and allows it to be used as a high-impedance input. Because If a PI through P5 latch contains a 1, then the output Ports 1 through 5 have fixed internal pull ups they are level is controlled by the signal labeled "alternate out- sometimes called "quasi-bidirectional" ports. put function." The pin level is always available to the pin's alternate input function, if any. When configured as inputs they pull high and will source current (IlL in the data sheets) when externally Ports 1 through 5 have internal pull ups. Port 0 has pulled low. Port 0, on the other hand, is considered open drain outputs. Each 1/0 line can be independently "true" bidirectional, because it floats when configured used as an input or an output (ports 0 and 2 may not be as an input. used as general purpose 1/0 when being used as the ADDRESSIDATA BUS). To be used as an input, the The latches for ports 0 and 3 have Is written to them by port bit latch must contain a 1, which turns off the the reset function. If a 0 is subsequently written to a output driver FET. On Ports 1 through 5 the pin is port latch, it can be reconfigured as an input by writing pulled high by the internal pullup, but can be pulled a 1 to it. low by an external source. PI, P2, P4, and P5 reset to a low state. While in reset 4.2 Writing to a Port these pins can sink large amounts of current. If these ports are to be used as inputs and externally driven In the execution of an instruction that changes the val- high while in reset, the user should be aware of possible ue in a port latch, the new value arrives at the latch contention. A simple solution is to use open collector during State 6, Phase 2 of the final cycle of the instruc- interfaces with these port pins or to buffer the inputs. tion. However, port latches are sampled by their output buffers only during Phase 1 of any clock period. (Dur- Port 0 differs from the other ports in not having inter- ing Phase 2 the output buffer holds the value it saw nal pullups. The pullup FET in the PO output driver is during the previous Phase 1). Consequently, the new used only when the port is emitting Is during external value in the port latch won't actually appear at the memory accesses. Otherwise the pullup FET is off. output pin until the next Phase 1, which will be at SIPl Consequently PO lines that are being used as output of the next machine cycle. Refer to Figure 3. STATE 41 STATE 51 STATE 61 STATE 1 ISTATE 21 STATE 31 STATE 41 STATE 51 I~I~ ~I~ ~I~ ~I~ ~I~ ~I~ ~I~ ~I~ XTAL1: PO, P1, P2, P3, P4, P5 ~ ~ 'P1,P2,P3'P4'P5 INPUTS SAMPLED: RST RST =::r-l.- MOV PORT, SRC: OLD DATA NEW DATA SERIAL PORT SHIFT CLOCK (MODE 0) -+I I-- RXD PIN SAMPLED RXD SAMPLED ~ I+- 270897-5 Figure 3. Port Operation 6-9 inteL 87C51GB HARDWARE DESCRIPTION For more information on internal timings refer to the 4.3 Port Loading and Interfacing CPU Timing section. The output buffers of Ports 1 through 5 can each sink If the change requires a O-to-1 transition in Ports 1 at least the amount of current specified by VOL in the through 5, an additional pullup is turned on during data sheet. These port pins can be driven by open-col- SlP1 and SlP2 of the cycle in which the transition lector and open-drain outputs although O-to-l tran- occurs. This is done to increase the transition speed. sitions will not be fast since there is little current pull- The extra pullup can source about 100 times the cur- ing the pin up. An input 0 turns off pullup pFET2, rent that the normal pullup can. The internal pullups leaving only the very weak pullup pFET2 to drive the are field-effect transistors, not linear resistors. The pull- transition. up arrangements are shown in Figure 4. In external bus mode, Port 0 output buffers can each The pullup consists of three pFETs. Note that an sink the amount of current specified at the test condi- n-channel FET (nFET) is turned on when a logical 1 is tions for VOL1 in the data sheet. However, as port pins applied to its gate, and is turned off when a logical 0 is they require external pullups to be able to drive any applied to its gate. A p-channel FET (PFET) is the inputs. opposite: it is on when its gate sees a 0, and off when its gate sees a 1. See the latest revision of the data sheet for design-in information. pFET 1 is the transistor that is turned on for 2 oscilla- tor periods after a O-to-l transition in the port latch. A 1 at the port pin turns on pFET3 (a weak pullup), 4.4 Read-Modify-Write Instructions through the inverter. This inverter and pFET form a latch which hold the 1. Some instructions that read a port read the latch and others read the pin. Which ones do which? The instruc- If the pin is emitting ai, a negative glitch on the pin tions that read the latch rather than the pin are the ones from some external source can tum off pFET2, causing that read a value, possibly change it, and then rewrite it the pin to go into a float state. pFET2 is a very weak to the latch. These are called "read-modify-write" pullup which is on whenever the nFET is off, in tradi- instructions. Listed on the following page, are the tional CMOS style. It's only about '!to the strength of read-modify-write instructions. When the destination pFET2. Its function is to restore a I to the pin in the event the pin had a 1 and lost it to a glitch. Vcc Vcc Vcc Q FROM PORT LATCH INPUT CI---C: DATA READ PORT PIN 270897-6 NOTE: a CHMOS Configuration. pFET 1 is turned on for 2 osc. periods after mades a O-to-1 transition. During this time, pFET 1 also turns on pFET 3 through the inverter to form a latch which holds the 1. pFET 2 is also on. Port 2 is similar except that it holds the strong pullup on while emitting 1s that are address bits. (See text, "Accessing External Memory".) Figure 4. Ports 1, 3, 4, and 5 Internal Pullup Configuration 6-10 intel~ 87C51GB HARDWARE DESCRIPTION operand is a port, or a port bit, these instructions read They read the port byte, all 8 bits, modify the addressed the latch rather than the pin: bit, then write the new byte back to the latch. ANL (logical AND, e.g. ANL PI, A) The reason that read-modify-write instructions are di- ORL (logical OR, e.g. ORL P2, A) rected to the latch rather than the pin is to avoid a XRL (logical EX-OR, e.g. XRL P3, A) possible misinterpretation of the voltage level at the JBC Gump if bit = 1 and clear bit, e.g. JBC pin. For example, a port bit might be used to drive the Pl.l, LABEL) base of a transistor. When a 1 is written to the bit, the transistor is turned on. If the CPU then reads the same CPL (complement bit, e.g. CPL P3.0) port bit at the pin rather than the latch, it will read the INC (increment, e.g. INC P2) base voltage of the transistor and interpret it as a O. DEC (decrement, e.g. DEC P2) Reading the latch rather than the pin will return the correct value of 1. 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) 4.5 Accessing External Memory CLR PX.Y (clear bit Y of Port X) Accesses to external memory are of two types: accesses SETB px.y (set bit Y of Port X) to external Program Memory and accesses to external Data Memory. Accesses to external Program Memory It is not obvious that the last three instructions in this use signal PSEN (program store enable) as the read list are read-modify-write instructions, but they are. strobe. Accesses to external Data Memory use RD or WR (alternate functions ofP3.7 and P3.6) to strobe the memory. Refer to Figures 5 through 7. STATE 11 STATE 21 STATE 31 STATE 41 STATE 51 STATE 61 STATE 11STATE 21 1~I~ ~I~ ~I~ ~I~ ~I~ ~In ~I~ ~I~ XTAL1: ALE: . . . ., ~ --.l DATA I--SAMPLEO L PO: ~: PCH OUT PCH OUT PCHOUT 270897-7 Figure 5. External Program Memory Fetches 6-11 intel~ 87C51GB HARDWARE DESCRIPTION I STATE 41 STATE 5 1STATE 61 STATE 1 1 STATE 21 STATE 31 STATE 41 STATE 5 I I~I~I~I~I~I~I~I~I~I~I~I~I~I~I~I~I XTAL1: ALE: AD: PO: ~: PCH OR DPH OR ~ SFR OUT I PCH OR ~SFR ~SFR 270897-8 Figure 6. External Data Memory Read Cycle STATE 41 STATE 51 STATE 61 STATE ISTATE 21 STATE 31 STATE 41 STATE 51 1 I~I~ ~I~ ~I~ ~I~ ~I~ ~I~ ~I~ ~I~ XTAL1: ALE: WR: PCLOUTIF PO: DPLORRI OUT DATA OUT ~-- ~f ~ IS EXTERNAL ~ PCH OR PCHOR OPH OR ~ SFR OUT ~SFR P2SFR 270897-9 Figure 7. External Data Memory Write Cycle 6-12 87C51GB HARDWARE DESCRIPTION Fetches from external Program Memory always use a function and may not be used for general purpose I/O. 16-bit address. Accesses to external Data Memory can During external program fetches they output the high use either a 16-bit address (MOVX @ DPTR) or an byte of the PC with the Port 2 drivers using the strong 8-bit address (MOVX @ Ri). pullups to emit bits that are 1s. Whenever a 16-bit address is used, the high byte of the address comes out on Port 2, where it is held for the 5.0 TIMER/COUNTERS duration of the read or write cycle. The Port 2 drivers use the strong pullups during the entire time that they The 8XC51GB has three 16-bit Timer/Counters: Tim- are emitting address bits that are Is. This occurs when er 0, Timer I, and Timer 2. Each consists of two 8-bit the MOVX @ DPTR instruction is executed. During registers: THx and TLx with x = 0, I, or 2. All three this time the Port 2 latch (the Special Function Regis- can be configured to operate either as timers or event ter) does not have to contain Is, and the contents of the counters. Port 2 SFR are not modified. If the external memory cycle is not immediately followed by another external In the Timer function, the TLx register is incremented memory cycle, the undisturbed contents of the Port 2 every machine cycle. Thus, you can think of it as count- SFR will reappear in the next cycle. ing machine cycles. Since a machine cycle consists of 12 oscillator periods, the count rate is 1/12 of the oscillator If an 8-bit address is being used (MOVX @ Ri), the frequency. contents of the Port 2 SFR remain at the Port 2 pins throughout the external memory cycle. In this case, In the Counter function, the register is incremented in Port 2 pins can be used to page the external data mem- response to a I-to-O transition at its corresponding ex- ory. ternal input pin: TO, T1, or T2. In this function, the external input is sampled during S5P2 of every machine In either case, the low byte of the address is time-multi- cycle. When the samples show a high in one cycle and a plexed with the data byte on Port O. The ADDRESS/ low in the next cycle, the count is incremented. The DATA signal drives both PETs in the Port 0 output new count value appears in the register during S3Pl of buffers. Thus, in external bus mode the Port 0 pins are the cycle following the one in which the transition was not open-drain outputs and do not require external detected. Since it takes 2 machine cycles (24 oscillator pullups. The ALE (Address Latch Enable) signal periods) to recognize a 1-to-0 transition, the maximum should be used to capture the address byte into an ex- count rate is 1;24 of the oscillator frequency. There are ternal latch. The address byte is valid at the negative no restrictions on the duty cycle of the external input transition of ALE. Then, in a write cycle, the data byte signal, but to ensure that a given level is sampled at to be written appears on Port 0 just before WR is acti- least once before it changes, it should be held for at vated, and remains there until after WR is deactivated. least one full machine cycle. In a read cycle, the incomin~e is accepted at Port 0 just before the read strobe (RD) is deactivated. Timer 0 and Timer 1 have four operating modes: Mode 0: 13-bit timer During any access to external memory, the CPU writes Mode 1: 16-bit timer OFFH to the Port 0 latch (the Special Function Regis- ter), thus obliterating the information in the Port 0 Mode 2: 8-bit auto-reload timer SFR. Also, a MOV PO instruction must not take place Mode 3: Timer 0 as two separate 8-bit timers during external memory accesses. If the user writes to Port 0 during an external memory fetch, the incoming Also, its possible to use Timer 1 to generate baud rates. code byte is corrupted. Therefore, do not write to Port o if external program memory is used. Timer 2 has three modes of operation: Timer 2 Capture External Program Memory is accessed under two con- ditions: Timer 2 Auto-Reload (up or down counting), and 1. Whenever signal EA is high, or Timer 2 as a Baud Rate Generator 2. Whenever the program counter (PC) contains an ad- dress greater than 1FFFH (8K). 5.1 Timer 0 and Timer 1 This requires that the ROMless versions have EA wired The Timer/Counter function is selected by control bits to Vss to enable the lower 8K of program bytes to be C_Tx in TMOD (Table 4). These two Timer/Coun- fetched from external memory. ters have four operating modes, which are selected by bit-pairs (MIx, MOx) also in TMOD. Mode 0, Mode 1, When the CPU is executing out of external Program and Mode 2 are the same for both Timer/Counters. Memory, all 8 bits of Port 2 are dedicated to an output Mode 3 operation is different for the two timers. 6-13 intel~ 87C51GB HARDWARE DESCRIPTION Table 4. TMOD: Timer/Counter Mode Control Register TMOD Address = 89H Reset Value = 0000 OOOOB Not Bit Addressable MO GATE Bit 7 6 5 4 3 2 o Symbol Function GATE Gating control when set. Timer/Counter 0 or 1 is enabled only while INTO or INT1 pin is high and TRO or TR 1 control pin is set. When cleared, Timer 0 or 1 is enabled whenever TRO or TR 1 control bit is set. clf Timer or Counter Selector. Clear for Timer operation (input from internal system clock). Set for Counter operation (input from TO or T1 input pin). M1 MO Operating Mode o 0 8-bit Timer/Counter. THx with TLx as 5-bit prescaler. o 1 16-bit Timer/Counter. THx and TLx are cascaded; there is no prescaler. o 8-bit auto-reload Timer/Counter. THx holds a value which is to be reloaded into TLx each time it overflows. (Timer 0) TLO is an 8-bit Timer/Counter controlled by the standard Timer 0 control bits. THO is an 8-bit timer only controlled by Timer 1 control bits. (Timer 1) Timer/Counter stopped. MODE 0 As the count rolls over from all 1s to alIOs, it sets the timer interrupt flag TFO or TF1. The counted input is Either Timer 0 or Timer 1 in Mode 0 is an 8-bit counter enabled to the timer when TRO or TR1 = 1, and either with a divide-by-32 prescaler. In this mode, the Timer GATEx = 0 or INTx pin = 1. (Setting GATEx = 1 register is configured as a 13-bit register. Figure 8 allows the Timer to be controlled by external input shows the Mode 0 operation for either timer. INTx pin, to facilitate pulse width measurements). INTERRUPT Tx PIN _ _ _ _ _....J CONTROL OVERFLOW 270897-10 Figure 8. Timer/Counter 0 or 1 in Mode 0: 13-Bit Counter 6-14 intel~ 87C51GB HARDWARE DESCRIPTION TRx and TFx are control bits in the SFR TeON. The MODE 1 GATEx bits are in TMOD. There are two different GATE bits: one for Timer 1 (TMOD.7) and one for Mode 1 is the same as Mode 0, except that the Timer Timer 0 (TMOD.3). register uses all 16-bits. In this mode, THx and TLx are cascaded; there is no prescaler. Refer to Figure 9. The 13-bit register consists of all 8 bits ofTHx and the lower 5 bits of TLx. The upper 3 bits of TLx are inde- As the count rolls over from allIs to aliOs, it sets the terminate and should be ignored. Setting the run flag timer interrupt flag TFO or TF1. The counted input is (TRx) does not clear these registers. enabled to the timer when TRO or TRI = I, and either GATEx = 0 or INTx pin = 1. (Setting GATEx = 1 Table 5. TCON: Timer/Counter Control Register TCON Address = aaH Reset = 0000 OOOOS Sit Addressable TF1 TR1 I TFO I TRO I IE1 IT1 lEO ITO Sit 7 6 5 4 3 2 o Symbol Function TF1 Timer 1 overflow Flag. Set by hardware on Timer/Counter overflow. Cleared by hardware when processor vectors to interrupt routine. TR1 Timer 1 Run control bit. Set/cleared by software to turn Timer/Counter 1 on/off. TFO Timer 0 overflow Flag. Set by hardware on Timer/Counter 0 overflow. Cleared by hardware when processor vectors to interrupt routine. TRO Timer 0 Run control bit. Set/cleared by software to turn Timer/Counter 0 on/off. IE1 Interrupt 1 flag. Set by hardware when external interrupt 1 edge is detected (transmitted or level-activated). Cleared when interrupt processed only if transition-activated. IT1 Interrupt 1 Type control bit. Set/cleared by software to specifiy falling edge/low level triggered external interrupt 1. lEO Interrupt 0 flag. Set by hardware when external interrupt 0 edge is detected (transmitted or level-activated). Cleared when interrupt processed only if transition-activated. ITO Interrupt 0 Type control bit. Set/cleared by software to specify falling edge/low level triggered external interrupt O. clf = 0 THx TLx INTERRUPT TxPIN _____----'-J cif = 1 CONTROL OVERFLOW GATE INTxPIN 270897-11 Figure 9. Timer/Counter 0 or 1 in Mode 1: 16-Bit Counter 6-15 int:eL 87C51GB HARDWARE DESCRIPTION allows the Timer to be controlled by external input (Setting GATEx = 1 allows the Timer to be controlled INTx pin to facilitate pulse width measurements). bv external input INTx pin. to facilitate pulse width nieasurements). - . - TRx and TFx are control bits in the SRF TCON. The GATEx bits are in TMOD. There are two different TRx and TFx are control bits in the SFR TCON. The GATE bits: one for Timer 1 (TMOD.7) and one for GATEx bits are in TMOD. There are two different Timer 0 (TMOD.3). GATE bits: one for Timer 1 (TMOD.7) and one for Timer 0 (TMOD.3). MODE 2 MODE 3 Mode 2 configures the Timer register as an 8-bit Coun- ter (TLx) with automatic reload as shown in Figure 10. Timer 1 in Mode 3 simply holds its count. The effect is Overflow from TLx not only sets TFx, but also reloads the same as setting TR 1 = O. TLx with the contents of THx, which is preset by soft- ware. The reload leaves THx unchanged. Timer 0 in Mode 3 establishes TLO and THO as two separate counters. TLO uses the Timer 0 control bits: The counted input is enabled to the timer when TRO or C_TO, GATEO, TRO, and TFO. THO is locked into a TRI = 1, and either GATEx = 0 or INTx pin = 1. ciT = 0 INTERRUPT TxPIN _____. . .1ciT' - =1 TRx-----f GATE iNTi PIN 270897-12 Figure 10. Timer/Counter 1 Mode 2: 8-Bit Auto-Reload osc ~G- 1112fOSC 1112 fOSC - - - - - - - , INTERRUPT T O P I N - - - - -..... CONTROL OVERFLOW 1I12fosc ----------+1.-(T1 1 ·1 (::.) ~INTERRUPT _ _____ }~CONTROL OVERFLOW TR1 - 270897-13 Figure 11. Timer/Counter 0 Mode 3: Two 8-Bit Counters 6-16 intel~ 87C51GB HARDWARE DESCRIPTION timer function (counting machine cycles) and takes Timer 2 Auto-Reload (up or down counting), and over the use of TRI and TFI from Timer 1. Thus THO Timer 2 as a Baud Rate Generator. now controls the Timer I interrupt. The logic for Mode 3 on Timer 0 is shown in Figure II. The modes are also selected by bits in T2CON as shown in Table 6. Mode 3 is provided for applications requiring an extra 8-bit timer or counter. When Timer 0 is in Mode 3, Table 6. Timer 2 Operating Modes Timer I can be turned on and off by switching it out of and into its own Mode 3, or can still be used by the RCLK + TCLK CP/RL2 T2·0E TR2 Mode serial port as a baud rate generator, or in any applica- 0 0 0 1 16-Bit tion not requiring an interrupt. Auto-Reload 0 1 0 1 16-Bit 5.2 Timer 2 Capture 1 X X 1 BauLRate Timer 2 is a 16-bit Timer/Counter which can operate Generator either as a timer or as an event counter. This is selected X 0 1 1 Clock-Out by bit C_T2 in the SFR T2CON (Table 7). It has the on P1.0· following three operating modes: X X X 0 Timer Off Timer 2 Capture, ·Present only on the 87C51 FC. Table 7. T2CON: Timer/Counter 2 Control Register T2CON Address = OC8H Reset Value = 0000 OOOOB Bit Addressable TF2 EXF2 RCLK TCLK I EXEN21 TR2 C/T2 ICPiRL21 Bit 7 6 5 4 3 2 o Symbol Function TF2 Timer 2 overflow flag set by a Timer 2 overflow and must be cleared by software. TF2 will not be set when either RCLK = 1 or TCLK = 1. EXF2 Timer 2 external flag set when either a capture or reload is caused by a negative transition on T2EX and EXEN2 = 1. When Timer 2 interrupt is enabled EXF2 = 1 will cause the CPU to vector to the Timer 2 interrupt routine. EXF2 must be cleared by software. EXF2 does not cause an interrupt in up/ down counter mode (DCEN = 1). RCLK Receive clock flag. When set, causes the serial port to use Timer 2 overflow pulses for its receive clock in serial port Modes 1 and 3. RCLK = 0 causes Timer 1 overflow to be used for the receive clock. TCLK Transmit clock flag. When set, causes the serial port to use Timer 2 overflow pulses for its transmit clock in serial port Modes 1 and 3. TCLK = 0 causes Timer 1 overflows to be used for the transmit clock. EXEN2 Timer 2 external enable flag. When set, allows a capture or reload to occur as a result of a negative transition on T2EX if Timer 2 is not being used to clock the serial port. EXEN2 = 0 causes Timer 2 to ignore events at T2EX. TR2 Start/stop control for Timer 2. A logiC 1 starts the timer. C/T2 Timer or counter select, (Timer 2) 0= Internal timer (OSC/12 or OSC/2 in baud rate generator mode.) 1 = External event counter (falling edge triggered). CP/RL2 Capture/Reload flag. When set, captures will occur on negative transition at T2EX if EXEN2 = 1. When cleared, auto-reloads will occur either with Timer 2 overflows or negative transitions at T2EX when EXEN2 = 1. When either RCLK = 1 or TCLK = 1, this bit is ignored and the timer is forced to auto-reload on Timer 2 overflow. 6-17 inteJ~ 87C51GB HARDWARE DESCRIPTION The T2 Pin has another alternate function on the addition, the transition at T2EX causes bit EXF2 in 87C5IGB. It can be configured as a ProlZrammable T2CON to be set. The EXF2 bit, like TF2, can generate Clock Out. - - an interrupt. Figure 12 illustrates this. TIMER 2 CAPTURE MODE TIMER 2 AUTO-RELOAD (UP OR DOWN COUNTER) In the capture mode there are two options selected by bit EXEN2 in T2CON. If EXEN2 = 0, Timer 2 is a Timer 2 can be programmed to count up or down when 16-bit timer on counter which, upon overflow, sets bit configured in its 16-bit auto-reload mode. This feature TF2 in T2CON. This bit can then be used to generate is invoked by a bit named DCEN (Down Counter En- an interrupt. If EXEN2 = 1, Timer 2 still does the able) located in the SFR T2MOD (see Table 8). Upon above, but with the added feature that a I-to-O tran- reset the DCEN bit is set to 0 so that Timer 2 will sition at external input T2EX causes the current value default to count up. When DCEN is set, Timer 2 can in the Timer 2 registers (TH2 and TL2) to be captured count up or down depending on the value of the T2EX into registers RCAP2H and RCAP2L, respectively. In pin. _ T2 PIN []-----I + c/f2 = 1 CAPTURE TIMER 2 INTERRUPT TRANSITION DETECTION T2EX PIN EXEN2 270897-14 Figure 12. Timer 2 in Capture Mode Table 8. T2MOD: Timer 2 Mode Control Register T2MOD Address = OC9H Reset Value = XXXX XXOOB Not Bit Addressable IT20EIDCENI Bit 7 6 5 4 3 2 o Symbol Function Not implemented, reserved for future use. * T20E Timer 2 Output Enable bit. DECN Down Count Enable bit. When set, this allows Timer 2 to be configured as an up/down counter ·User software should not write 1s to reserved bits. These bits may be used in future 8051 family products to invoke new features. In that case, the reset or inactive value of the new bit will be 0, and its active value will be 1. The value read from a reserved bit is indeterminate. 6-18 infel~ 87C51GB HARDWARE DESCRIPTION In the auto-reload mode with DCEN = 0, there are Setting the DCEN bit enables Timer 2 to count up or two options selected by bit EXEN2 in T2CON. If down as shown in Figure 14. In this mode the T2EX EXEN2 = 0, Timer 2 counts up to OFFFFH and then pin controls the direction of count. A logic 1 at T2EX sets the TF2 bit upon overflow. The overflow also caus- makes Timer 2 count up. The timer will overflow at es the timer registers to be reloaded with the 16-bit OFFFFH and set the TF2 bit which can then generate value in RCAP2H and RCAP2L. The values in an interrupt if it is enabled. This overflow also causes RCAP2H and RCAP2L are preset by software. If the 16-bit value in RCAP2H and RCAP2L to be re- EXEN2 = 1, a 16-bit reload can be triggered either by loaded into the timer registers, TH2 and TL2, respec- an overflow or by a I-to-O transition at external input tively. T2EX. This transition also sets the EXF2 bit. Either the TF2 or EXF2 bit can generate the Timer 2 inter- A logic 0 at T2EX makes Timer 2 count down. Now rupt if it is enabled. Figure 13 shows timer 2 automati- the timer underflows when TH2 and TL2 equal the cally counting up when DCEN = O. values stored in RCAP2H and RCAP2L. The under- OVERFLOW _ ... cjf2 = 1 T2 PIN I:}----' TRANSITION DETECTION TIMER 2 INTERRUPT T2EX PIN EXEN2 270897-15 Figure 13. Timer 2 Auto Reload Mode (OCEN = 0) (DOWN COUNTING RELOAD VALUE) _ ... cm = T2 PIND--' 1 COUNT DIRECTION 1 = UP 0= DOWN (UP COUNTING RELOAD VALUE) T2EX PIN 270897-16 Figure 14. Timer 2 Auto Reload Mode (OCEN = 1) 6-19 intel~ 87C51GB HARDWARE DESCRIPTION flow sets the TF2 bit and causes OFFFFH to be reload- To configure the Timer/Counter 2 as a clock generator, ed into the timer registers. bit C_T2 (in T2CON) must be cleared and bit T20E (in T2MOD) must be set. Bit TR2 (in T2CON) also The EXF2 bit toggles whenever Timer 2 overflows or must be set to start the timer. underflows. This bit can be used as a 17th bit of resolu- tion if desired. In this operating mode, EXF2 does not The Clock Out frequency depends on the oscillator fre- generate an interrupt. quency and the reload value of Timer 2 capture regis- ters (RCAP2H, RCAP2L) as shown in this equation: 5.3 Programmable Clock Out Clock Out _ Oscillator Frequency Frequency - 4 X [65536 - (RCAP2H, RCAP2L)] The 87C51GB has a new feature. A 50% duty cycle clock can be programmed to come out on P1.0. This In the Clock Out mode, Timer 2 roll-overs will not pin, besides being a regular I/O pin, has two alternate generate an interrupt. This is similar to when it is used functions. It can be programmed (1) to input the exter- as a baud-rate generator. It is possible to use Timer 2 as nal clock for Timer/Counter 2, or (2) to output a 50% a baud-rate generator and a clock generator simulta- duty cycle clock ranging from 61 Hz to 4 MHz at a neously. Note, however, that the baud-rate and the 16 MHz operating frequency. Figure 15 shows Timer 2 clock-out frequency will be the same. in clock-out mode. Pl.0 (T2) --, TRANSITION T20E (T2MOD. 1) 'LDETECTOR (T:~X) 101----. . 1-:-::I-. . .B . .--. :~~i~R2UPT 1 ..--0 EXEN2 270897-17 Figure 15. Timer 2 in Clock-Out Mode 6-20 inteL 87C51GB HARDWARE DESCRIPTION 6.0 AID CONVERTER Table 9. AID SFRs The AID converter on the 8XC51GB consists of: 8 (MSB) (LSB) : IADO ... AD7 analog inputs (ACHO-ACH7), an external trigger in- put (TRIGIN), separate analog voltage supplies (AVss I : : : : : : 084H ... OF4H and AVREF), a comparison reference input I;:N (MSS) (LSS) (COMPREF) and internal circuitry. The internal cir- cuitry includes: an 8 channel multiplexer, a 256 element 1- : - : AIF : ACE :ACS1>CSO: AIM: ATM resistive ladder, a comparator, sample-and-hold capaci- tor, successive approximation register, AID trigger (MSS) (LSB) ACMP control, a comparison result register and 8 AID result EMP O:CMP 1 :CMP 2:CMP 3:CMP 4:CMP S:CMP S:CMP 71oC7H registers as shown in the AID block diagram, Figure 16. ADO through AD7 contain the results of the 8 analog AVREF must be held within the tolerances stated on conversion. Each SFR is updated as each conversion is the 8XC51GB data sheet. The accuracy of the AID complete, starting with the lowest channel and ending cannot be improved, for instance, by tying A VREF to with channel 7. Y2 the voltage on Vee. ACMP is the comparison result register. ACMP is or- ganized differently than all the other SFRs in that 6.1 AID Special Function Registers CMPO occupies the MSB and CMP7 the LSB. CMPO The AID has 10 SFRs associated with it. The SFRs are shown in Table 9. CONTINUOUS 14--.....- TRIGIN (Trigger In) ATM - - - - - - - - -. .- AVREF _-----4....---..- CONVERSION ENABLE (ACE) MUX .--------. : ADO RESULT . , - - - ._------_. I I ~ ••• LADDER ACS1 .--------. . : AD6 RESULT . , - - - -------_. EJ ........ ACHO • • .-------- .. ... : AD7 RESULT / ~---' El • .-------_. 1+-------. . . ACH7 Air r:C~;~;~~;··,--- .-------_. 0 ......_-....- SCAN/SELECT (AIM) COWPREr AVSS 270897-18 Figure 16. AID Block Diagram 6-21 87C51GB HARDWARE DESCRIPTION through CMP7 correspond to analog inputs 0 through edge is detected, the AID conversions begin on the next 7. CMPn is set to a 1 if the analog input is greater than machine cycle and complete when channel 7 is convert- COMPREF. CMPn is cleared if the analog input is less ed. After channel 7 is converted, AIF is set and the than or equal to COMPREF. conversions halt until another trigger is detected while ACE= 1. External triggers are ignored while a conver- ACON is the AID control register and contains the sion cycle is in progress. AID Interrupt Flag (AIF), AID Conversion Enable (ACE), AID Channel Select (ACSO and ACSl), AID Input Mode (AIM), and AID Trigger Mode (ATM). 6.4 AID Input Modes The 8XC51GB has two input modes: Scan mode and 6.2 AID Comparison Mode Select mode. Clearing AIM places the 8XC51 GB in Scan mode. In Scan mode the analog conversions occur The AID Comparison mode is always active while the in the sequence ACHO, ACHl, ACH2, ACH3, ACH4, AID converter is enabled. The Comparison mode is ACH5, ACH6, and ACH7. The result of each analog used to compare each analog input against an external conversion is placed in the corresponding analog result reference voltage applied to COMPREF. Whenever the register: ADO, ADl, AD2, AD3, AD4, AD5, AD6, AID converter is triggered, each bit in ACMP is updat- and AD7. ed as each analog conversion is completed, starting with channel 0 up to channel 7 regardless of whether Setting AIM activates Select mode. In Select mode, one Select or Scan mode is invoked. The comparison mode of the lower 4 analog inputs (ACHO-ACH3) is con- can provide a quicker "greater-than or less-than" deci- verted four times. After the first four conversions are sion than can be performed with software and it is more complete the cycle continues with ACH4 through code efficient. It can also be used to convert the analog ACH7. The results of the first four conversion are inputs into digital inputs with a variable threshold. If placed in the lower four result registers (ADO through the comparison mode is not used, COMPREF should AD3). The rest of the conversions are placed in their be tied to Vee or Vss. matching result register. ACSO and ACSI determine which analog inputs are used as shown in Table 10. 6.3 AID Trigger Mode Table 10. AID Channel Selection The analog converter can be triggered either internally Selected ACS1 ACSO or externally. To enable internal trigger mode, ATM Channel should be cleared. 0 0 ACHO 0 1 ACH1 When in internal trigger mode, AID conversions begin in the machine cycle which follows the setting of the 1 0 ACH2 ACE bit. The lowest channel (see "AID Input Modes" 1 1 ACH3 below) is converted first, followed by all the other chan- nels in sequence. The AIF flag is set upon completion of the channel 7 conversion. AIF will flag an interrupt 6.5 USing the AID with Fewer than if the AID interrupt is enabled. Once a conversion cy- 8 Inputs cle is completed, a new cycle begins, starting with the lowest channel. If the user wishes each channel to be There are several options for a user who wishes to con- converted only once, the ACE bit should be cleared. vert fewer than eight analog input channels. If time is Clearing ACE stops all AID conversion activity. If a not critical the user can simply wait for the AID inter- new AID cycle begins, the result of the previous con- rupt to be generated by the AIF bit after channel 7 is version will be overwritten. converted and can ignore the results for unused chan- nels. If a user needs to know the results of a conversion In external mode, the AID conversions begin when a immediately after it occurs, a timer should be used to falling edge is detected at the TRIGIN pin. There is no generate an interrupt. The amount of time required for edge detector on the TRIGIN pin; is it sampled once each AID conversion is specified in the 8XC51GB data every machine cycle. sheet. The user could also periodically poll the result registers, provided he or she is looking only for a A negative edge is recognized when TRIGIN is high in. change in the analog voltage. Using the Select mode one machine cycle and low in the next. For this reason, (see above) does not reduce the time required for a con- TRIGIN should be held high for at least one machine version cycle but will convert a given channel more cycle and low for one machine cycle. Once the falling frequently. 6-22 intaL 87C51GB HARDWARE DESCRIPTION 6.6 AID in Power Down Table 11. PCA and PCA 1 SFRs PCA PCA1 The AID on the 8XC5lGB contains circuitry that lim- its the amount of current dissipated during Power SFRs: Down mode to leakage current only. For this circuitry CCON ...................... C1CON to function properly, AVREF should be tied to Vee CMOD ...................... C1MOD during power down. The I PD specification in the data CCAPMO ................... C1 CAPMO sheet includes the current for the entire chip. While CCAPM1 ................... C1CAPM1 AVREF is tied to Vee during Power Down, the voltage may be reduced to the minimum voltage as shown in CCAPM2 ................... C1 CAPM2 the data sheet. CCAPM3 ................... C1 CAPM3 CCAPM4 ................... C1 CAPM4 CL ......................... CL1 7.0 PROGRAMMABLE COUNTER CCAPOL .................... C1 CAPOL ARRAY CCAP1 L .................... C1 CAP1 L CCAP2L .................... C1 CAP2L Programmable Counter Arrays (PCAs) provide more CCAP3L .................... C1 CAP3L timing capabilities with less CPU intervention than the CCAP4L .................... C1 CAP4L standard timer/counters. Their advantages include re- duced software overhead and improved accuracy. For CH ......................... CH1 example, a PCA can provide better resolution than CCAPOH .................... C1 CAPOH Timers 0, 1, and 2 because the peA clock rate can be CCAP1 H .................... C1 CAP1 H three times faster. A PCA can also perform many tasks CCAP2H .................... C1 CAP2H that these hardware timers cannot (i.e. measure phase CCAP3H .................... C1 CAP3H differences between signals or generate PWMs). CCAP4H .................... C1 CAP4H The 8XC5lGB has two PCAs called PCA and PCAL BITS: The following text and figures address only PCA but ECI. ........................ ECI1 are also applicable to PCA 1 with the following excep- CEXO ....................... C1EXO tions: CEX1 ....................... C1EX1 1. PCAI, Module 4 does not support the Watchdog CEX2 ....................... C1EX2 Timer CEX3 ....................... C1EX3 2. All the SFRs and bits have Is added to their names CEX4 ....................... C1EX4 (see Table 11). CCFO ....................... C1CFO 3. Port 4 is the interface for PCAI: CCF1 ....................... C1CF1 CCF2 ....................... C1CF2 P4.2 ECIl CCF3 ....................... C1CF3 P4.3 CIEXI P4.4 CIEX2 CCF4 ....................... C1CF4 P4.5 CIEX2 CR ......................... CR1 P4.6 CIEX3 CF ......................... CF1 P4.7 CIEX4 16 BITS EACH Pl.3/CEXO Pl.4/CEXl 16 BITS PCA TIt.lER/COUNTER t - - - - t - - - M Pl.5/CEX2 Pl.6/CEX3 Pl.7/CEX4 270897-19 Figure 17. PCA Block Diagram 6-23 intel~ 87C51GB HARDWARE DESCRIPTION 4. There has been one additional bit added to CICON When the compare/capture modules are programmed to allow both PCAs to be enabled simultaneously. in the caDture mode. software timer. or hie:h soeed out- The bit is called CRE and occupies bit position 5 of put mod~, an interrupt can be gene~ated ;'heitever the CICN. Its bit address is OEDH. When CRE is set, module executes its function. All five modules plus the both CR and CRt must be set to enable PCAL PCA timer overflow share one PCA interrupt vector. Each PCA consists of a 16-bit timer/counter and five The PCA timer/counter and compare/capture modules 16-bit compare/capture modules as shown in Figure share Port 1 pins for external I/O. These pins are listed 17. The PCA timer/counter serves as a common time below. If the port pin is not used for the PCA, it can base for the five modules and is the only timer which still be used for standard I/O. can service the PCA. Its clock input can be pro- grammed to count anyone of the following signals: peA Component External I/O Pin Oscillator frequency / 12 16-bit Counter P1.2/ ECI Oscillator frequency / 4 16-bit Module 0 P1.3 / CEXO Timer 0 overflow 16-bit Module 1 P1.4 / CEX1 External input on ECI (P1.2). 16-bit Module 2 P1.5 / CEX2 16-bit Module 3 P1.6/ CEX3 The compare/capture modules can be programmed in 16-bit Module 4 P1.7 / CEX4 anyone of the following modes: rising and/or falling edge capture software timer 7.1 PCA Timer/Counter high speed output The PCA has a free-running 16-bit timer/counter con- pulse width modulator. sisting of registers CH and CL (the high and low bytes of the count value). These two registers can be read or Module 4 can also be programmed as a watchdog written to at any time. Reading the PCA timer as a full timer. 16-bit value simultaneously requires using one of the PCA modules in the capture mode and toggling a port pin in software. F"OSC/12 F"OSC/4 TIMER 0 INTERRUPT OVERFLOW EXTERNAL INPUT (ECI) 270897-20 Figure 18. PCA Timer/Counter 6-24 intet 87C51GB HARDWARE DESCRIPTION The clock input can be selected from the following four External input: modes: The PCA timer increments when a 1-to-O transition is detected on the ECI pin (p1.2). The maximum input Oscillator frequency I 12: frequency in this mode is oscillator frequency / 8. The PCA timer increments once per machine cycle. With a 16 MHz crystal, the timer increments every The mode register CMOD (Table 12) contains the 750 ns. Count Pulse Select bits (CPS1 and CPSO) to specify the clock input. This register also contains the ECF bit which enables the PCA counter overflow to generate Oscillator frequency I 4: the PCA interrupt. In addition, the user has the option of turning off the PCA timer during Idle Mode by set- The PCA timer increments three times per machine ting the Counter Idle bit (CIDL). This can further re- cycle. With a 16 MHz crystal, the timer increments duce power consumption by an additional 30%. every 250 ns. The CCON (Table 13) register contains two more bits which are associated with the PCA timer/counter. The Timer 0 overflows: CF bit gets set by hardware when the counter over- The PCA timer increments whenever Timer 0 over- flows, and the CR bit is set or cleared to tum the coun- flows. This mode allows a programmable input fre- ter on or off. quency to the PCA. Table 12. CMOD: PCA Counter Mode Register CMOD Address = 009H Reset Value = OOXX XOOOB Not Bit Addressable CIOL I WOTE I CPS1 CPSO ECF Bit 7 6 5 4 3 2 o Symbol Function CIOL Counter Idle control: CIOL = 0 programs the PCA Counter to continue functioning during idle Mode. CIOL = 1 programs it to be gated off during idle. WOTE Watchdog Timer Enable: WDTE = 0 disables Watchdog Timer function on PCA Module 4. WDTE = 1 enables it. Not implemented, reserved for future use. * CPS1 PCA Count Pulse Select bit 1. CPSO PCA Count Pulse Select bit O. CPS1 CPSO Selected PCA Input* * o 0 Internal clock, Fosc -:- 12 o 1 Internal clock, Fosc -:- 4 o Timer 0 overflow External clock at ECI/P1.2 pin (max. rate = Fosc -:- 8) ECF PCA Enable Counter Overflow interrupt: ECF = 1 enables CF bit in CCON to generate an interrupt. ECF = 0 disables that function of CF. NOTE: ·User software should not write 1s to reserved bits. These bits may be used in future 8051 family products to invoke new features. In that case, the reset or inactive value of the new bit will be 0, and its active value will be 1. The value read from a reserved bit is indeterminate. ·*Fosc = oscillator frequency 6-25 infel~ 87C51GB HARDWARE DESCRIPTION Table 13. CCON: PCA Counter Control Register CCON Address = 008H Reset Value = OOXO OOOOB Bit Addressable CF CR CCF4 CCF3 CCF2 CCF1 CCFO Bit 7 6 5 4 3 2 o Symbol Function CF PCA Counter Overflow flag. Set by hardware when the counter rolls over. CF flags an interrupt if bit ECF in CMOO is set. CF may be set by either hardware or software but can only be cleared by software. CR PCA Counter Run control bit. Set by software to turn the PCA counter on. Must be cleared by software to turn the PCA counter off. Not implemented, reserved for future use*. CCF4 PCA Module 4 interrupt flag. Set by hardware when a match or capture occurs. Must be cleared by software. CCF3 PCA Module 3 interrupt flag. Set by hardware when a match or capture occurs. Must be cleared by software. CCF2 PCA Module 2 interrupt flag. Set by hardware when a match or capture occurs. Must be cleared by software. CCF1 PCA Module 1 interrupt flag. Set by hardware when a match or capture occurs. Must be cleared by software. CCFO PCA Module 0 interrupt flag. Set by hardware when a match or capture occurs. Must be cleared by software. • NOTE: User software should not write 1s to reserved bits. These bits may be used in future 8051 family products to invoke new features. In that case, the reset or inactive value of the new bit will be 0, and its active value will be 1. The value read from a reserved bit is indeterminate. READING THE PCA TIMER 7.2 Compare/Capture Modules Some applications may require that the full 16-bit PCA Each of the five compare/capture modules has six pos- timer value be read simultaneously. Since the timer sible functions it can perform: consists of two 8-bit registers (CH, CL), it would nor- 16-bit Capture, positive-edge triggered mally take two MOV instructions to read the whole timer value. An invalid read could occur if the registers 16-bit Capture, negative-edge triggered rolled over in between the execution of the two MOVs. 16-bit Capture, both positive and negative-edge triggered However, with the PCA Capture Mode the 16-bit timer 16-bit Software Timer value can be loaded into the capture registers by tog- gling a port pin. For example, configure Module 0 to 16-bit High Speed Output capture falling edges and initialize P1.3 to be high. 8-bit Pulse Width Modulator. Then, when the user wants to read the PCA timer, clear P1.3 and the full 16-bit timer value will be saved In addition, module 4 can be used as a Watchdog Tim- in the capture registers. It's still optional whether the er. The modules can be programmed in any combina- user wants to generate an interrupt with the capture. tion of the different modes. 6-26 intel~ 87C51GB HARDWARE DESCRIPTION Each module has a mode register called CCAPMn 7.3 PCA Capture Mode (n = 0, 1, 2, 3, or 4) to select which function it will perform. The ECCFn bit enables the PCA interrupt Both positive and negative transitions can trigger a cap- when a module's event flag is set. The event flags ture with the PCA. This gives the PCA the flexibility to (CCFn) are located in the CCON register and get set measure periods, pulse widths, duty cycles, and phase when a capture event, software timer, or high speed differences on up to five separate inputs. Setting the output event occurs for a given module. CAPPn and/or CAPNn bits in the CCAPMn mode register (Table 14) selects the input trigger-positive Each module also has a pair of 8-bit compare/capture and/or negative transition-for module n. Refer to Fig- registers (CCAPnH and CCAPnL) associated with it. ure 19. These registers store the time when a capture event oc- curred or when a compare event should occur. For the Table 15 shows the combinations of bits in the PWM mode, the high byte register CCAPnH controls CCAPMn register that are valid and have a dermed the duty cycle of the waveform. function. Invalid combinations will produce undefined results. Table 14. CCAPMn: PCA Modules Compare/Capture Registers CCAPMn Address CCAPMO ODAH Reset Value = XOOO OOOOB (n = 0-4) CCAPM1 ODBH CCAPM2 ODCH CCAPM3 ODDH CCAPM4 ODEH Not Bit Addressable IECOMnlcAPPnlcAPNnl MATn TOGn I PWMn I ECCFn I Bit 7 6 5 4 3 2 0 Symbol Function Not implemented, reserved for future use"'. ECOMn Enable Comparator. ECOMn = 1 enables the comparator function. CAPPn Capture Positive, CAPPn = 1 enables positive edge capture. CAPNn Capture Negative, CAPNn = 1 enables negative edge capture. MATn Match. When MATn = 1, a match of the peA counter with this module's compare/ capture register causes the CCFn bit in CCON to be set, flagging an interrupt. TOGn Toggle. When TOGn = 1, a match of the PCA counter with this module's compare/capture register causes the CEXn pin to toggle. PWMn Pulse Width Modulation Mode. PWMn = 1 enables the CEXn pin to be used as a pulse width modulated output. ECCFn Enable CCF interrupt. Enables compare/ capture flag CCFn in the CCON register to generate an interrupt. NOTE: ·User software should not write 1s to reserved bits. These bits may be used in future 8051 family products to invoke new features. In that case, the reset or inactive value of the new bit will be 0, and its active value will be 1. The value read from a reserved bit is indeterminate. 6-27 intelGD 87C51GB HARDWARE DESCRIPTION Table 15. PCA Module Modes (CCAPMn Register) I I - ECOMn CAPPn CAPNn MATn TOGn PWMn ECCfn Moduie Function X 0 0 0 0 0 0 0 No operation X X 1 0 0 0 0 X 16-bit capture by a postive-edge trigger on CEXn X X 0 1 0 0 0 X 16-bit capture by a negative-edge trigger on CEXn X X 1 1 0 0 0 X 16-bit capture by a transition on CEXn X 1 0 0 1 0 0 X 16-bit Software Timer X 1 0 0 1 1 0 X 16-bit High Speed Output X 1 0 0 0 0 1 0 a-bit PWM X 1 0 0 1 x 0 x Watchdog Timer x = Don't Care -+---+ INTERRUPT PCA -..,..--.,....- TIMER/COUNTER CEXn PIN n = 0, 1, 2, 3 or 4 CCAPMn MODE REGISTER x = Don't Care 270897-21 Figure 19. PCA 16-Bit Capture Mode The external input pins CEXO through CEX4 are sam- In the interrupt service routine, the 16-bit capture value pled for a transition. When a valid transition is detected must be saved in RAM before the next capture event (positive and/or negative edge), hardware loads the occurs. A subsequent capture on the same CEXn pin 16-bit value of the PCA timer (CH, CL) into the mod- will write over the first capture value in CCAPnH and ule's capture registers (CCAPnH, CCAPnL). The re- CCAPnL. sulting value in the capture registers reflects the PCA timer value at the time a transition was detected on the The time it takes to service this interrupt routine deter- CEXn pin. mines the resolution of back-to-back events with the same PCA module. To store two 8-bit registers and Upon a capture, the module's event flag (CCFn) in clear the event flags takes at least 9 machine cycles. CCON is set, and an interrupt is flagged if the ECCFn That includes the call to the interrupt routine. At bit in the mode register CCAPMn is set. The PCA in- 12 MHz, this routine would take less than 10 f.Ls. How- terrupt will then be generated if it is enabled. Since the ever, depending on the frequency and interrupt latency, hardware does not clear an event flag when the inter- the resolution will vary with each application. rupt is vectored to, the flag must be cleared in software. 6-28 infel~ 87C51GB HARDWARE DESCRIPTION 7.4 Software Timer Mode For the Software Timer mode, the MATn bit also needs to be set. When a match occurs between the PCA timer In most applications a software timer is used to trigger and the compare registers, a match signal is generated interrupt routines which must occur at periodic inter- and the module's event flag (CCFn) is set. An interrupt vals. The user preloads a 16-bit value in a module's is then flagged if the ECCFn bit is set. The PCA inter- compare registers. When a match occurs between this rupt is generated only if it has been properly enabled. compare value and the PCA timer value, an event flag Software must clear the event flag before the next inter- is set and an interrupt can then be generated. rupt will be flagged. In the PCA compare mode, the 16-bit value of the PCA During the interrupt routine, a new 16-bit compare val- timer is compared with a 16-bit value pre-loaded in the ue can be written to the compare registers (CCAPnH module's compare registers (CCAPnH, CCAPnL) as and CCAPnL). Notice, however, that a write to seen in Figure 20. The comparison occurs three times CCAPnL clears the ECOMn bit which temporarily dis- per machine cycle in order to recognize the fastest pos- ables the comparator function while these registers are sible clock input (i.e. 1/4 X oscillator frequency). Set- being updated so an invalid match does not occur. A ting the ECOMn bit in the mode register CCAPMn write to CCAPnH sets the ECOMn bit and re-enables enables the comparator function. the comparator. For this reason, user software should write to CCAPnL first, then CCAPnH. -10----+ INTERRUPT PCA TIMER/COUNTER ENABLE CCAPMn MODE REGISTER RESET WRITE TO CCAPnL WRITE TO CCAPnH "1" 270897-22 Figure 20. PCA 16-Bit Comparator Mode: Software Timer 6-29 intet 87C51GB HARDWARE DESCRIPTION 7.5 High Speed Output Mode Without any CPU intervention, the fastest waveform the PCA can generate with the HSO mode is a 30.5 Hz The High Speed Output (HSO) mode toggles a CEXn signal at 16 MHz. pin when a match occurs between the PCA timer and a pre-loaded value in a module's compare registers. For this mode, the TOGn bit needs to be set in addition to 7.6 Watchdog Timer Mode the ECOMn and MATn bits in the CCAPMn mode register. By setting or clearing the pin in software, the A Watchdog Timer is a circuit that automatically in- user can select whether the CEXn pin will change from vokes a reset unless the system being watched sends a logical 0 to a logical 1 or vice versa. The user also has regular hold-off signals to the Watchdog. These circuits the option of flagging an interrupt when a match event are used in applications that are subject to electrical occurs by setting the ECCFn bit. See Figure 21. noise, power glitches, electrostatic discharges, etc., or where high reliability is required. The HSO mode is more accurate than toggling port pins in software because the toggle occurs before The Watchdog Timer function is only available on branching to an interrupt. That is, interrupt latency PCA Module 4. If a Watchdog Timer is not needed, will not effect the accuracy of the output. In fact, the Module 4 can still be used in other modes. interrupt is optional. Only if the user wants to change the time for the next toggle is it necessary to update the As a Watchdog timer, every time the count in the PCA compare registers. Otherwise, the next toggle will occur timer matches the value stored in module 4's compare when the PCA timer rolls over and matches the last registers, an internal reset is generated (see Figure 22). compare value. The bit that selects this mode is WnTE in the CMOD register. Module 4 must be set up in either compare mode as a "Software Timer" or High Speed Output. PCA CEXn PIN TIMER/COUNTER ENABLE CCAPMn MODE REGISTER RESET WRITE TO CCAPnL WRITE TO CCAPnH "1" 270897-23 Figure 21. PCA 16-Bit Comparator Mode: High Speed Output 6-30 inteL 87C51GB HARDWARE DESCRIPTION I CCAP4H I CCAP4L I PCA ......- -.... RESET TIMER/COUNTER ENABLE CCAPM4 MODE REGISTER RESET r--. >-----. WRITE TO CCAP4L WRITE TO _ __ CCAP4H "1" 270897-24 Figure 22. Watchdog Timer Mode To hold off the reset, the user has three options: counter goes astray and gets stuck in an infmite loop, 1. periodically change the compare value so it will nev- interrupts will still be serviced, and the watchdog will er match the peA timer, not reset the controller. Thus, the purpose of the watch- dog would be defeated. Instead, call this subroutine 2. periodically change the peA timer value so it will from the main program within 65536 counts of the never match the compare value, peA timer. 3. disable the Watchdog by clearing the WDTE bit be- fore a match occurs and then later re-enable it. 7.7 Pulse Width Modulator Mode The first two options are more reliable because the Watchdog Timer is never disabled as in option #3. The Any or all of the five peA modules can be pro- second option is not recommended if other peA mod- grammed to be a Pulse Width Modulator. The PWM ules are being used since this timer is the time base for output can be used to convert digital data to an analog all five modules. Thus, in most applications the first signal by simple external circuitry. The frequency of the solution is the best option. PWM depends on the clock source for the peA timer. With a 16 MHz crystal the maximum frequency of the The watchdog routine should not be part of an inter- PWM waveform is 15.6 KHz. Table 16 shows the vari- rupt service routine. Why? Because if the program ous frequencies that are possible. 6-31 87C51GB HARDWARE DESCRIPTION Table 16. PWM Frequencies PWM Frequency PCA Timer Mode 12MHz 16 MHz 1/12 Osc. Frequency 3.9 KHz 5.2 KHz 1/4 Osc. Frequency 11.8 KHz 15.6 KHz Timer 0 Overflow: 8-bit 15.5 Hz 20.3 Hz 16-bit 0.06 Hz 0.08 Hz 8-bit Auto-Reload 3.9 KHz to 15.3 Hz 5.2 KHz to 20.3 Hz External Input (Max) 5.9 KHz 7.8 KHz For this mode, the ECOMn bit and the PWMn bits in The value in CCAPnL controls the duty cycle of the the CCAPMn mode register need to be set. The PCA waveform. To change the value in CCAPnL without generates 8-bit PWMs by comparing the low byte of the output glitches, the user must write to the high byte PCA timer (CL) with the low byte of the module's register (CCAPnH). This value is then shifted by hard- compare registers (CCAPnL). When CL < CCAPnL ware into CCAPnL when CL rolls over from OFFH to the output is low. When CL > CCAPnL the output is OOH which corresponds to the next period of the out- high. Refer to Figure 23. put. CL MADE FFTO 00 ---~ TRANSITION CEXn PIN CCAPMn MODE REGISTER 270897-25 Figure 23. PCA a-Bit PWM Mode 6-32 infel4P 87C51GB HARDWARE DESCRIPTION CCAPnH can contain any integer from 0 to 255 to vary the receive register. (However, if the first byte still the duty cycle from a 100% to 0.4%. A 0% duty cycle hasn't been read by the time reception of the second can be obtained by writing directly to the port pin with byte is complete, one of the bytes will be lost). the CLR bit instruction. To calculate the CCAPnH val- ue for a given duty cycle, use the following equation: The serial port receive and transmit registers are both accessed through Special Function Register SBUF. Ac- CCAPnH = 256 x (1 - Duty Cycle) tually, SBUF is two separate registers, a transmit buffer and a receive buffer. Writing to SBUF loads the trans- where CCAPnH is an S-bit integer and Duty Cycle is mit register, and reading SBUF accesses a physically expressed as a fraction. See Figure 24. separate receive register. The serial port control and status register is the Special 8.0 SERIAL PORT Function Register SCON (Table 17). This register con- tains the mode selection bits (SMO and SM1); the SM2 The serial port is full duplex, meaning it can transmit bit for the multiprocessor modes; the Receive Enable and receive simultaneously. It is also receive-buffered, bit (REN); the 9th data bit for transmit and receive meaning it can commence reception of a second byte (TBS and RBS); and the serial port interrupt bits (TI before a previously received byte has been read from and RI). DUTY CYCLE CCAPnH OUTPUT WAVEFORM 100% 00 90% 25 50% 128 10% 230 0.4% 255 270897-26 Figure 24. CCAPnH Varies Duty Cycle 6-33 intel~ 87C51GB HARDWARE DESCRIPTION Table 17. SCON: Serial Port Control Register SCON Address = 98H Reset Value = 0000 OOOOB Bit Addressable ISMO/FE I SM1 SM2 REN TB8 RB8 TI RI Bit: 7 6 5 4 3 2 o (SMODO=0/1)* Symbol Function FE Framing Error bit. This bit is set by the receiver when an invalid stop bit is detected. The FE bit is not cleared by valid frames but should be cleared by software. The SMODO* bit must be set to enable access to the FE bit. SMO Serial Port Mode Bit 0, (SMODO must = 0 to access SMO) SM1 Serial Port Mode Bit 1 SMO SM1 Mode Description Baud Rate** o 0 0 shift register Fosc/12 o 1 1 8-bit UART variable o 0 9-bit UART Fosc/64 or Fosc/32 1 3 9-bit UART variable SM2 Enables the Automatic Address Recognition feature in Modes 2 or 3. If SM2 = 1 then RI will not be set unless the received byte is a Given or Broadcast Address. In Mode 1, if SM2 = 1 then RI will not be activated unless a valid stop bit was received, and the received byte is a Given or Broadcast Address. In Mode 0, SM2 should be o. REN Enables serial reception. Set by software to enable reception. Cleared by software to disable reception. TB8 The 9th data bit that will be transmitted in Modes 2 and 3. Set or clear by software as desired. RB8 In modes 2 and 3, the 9th data bit that was received. In Mode 1 if SM2 = 0, RB8 is the stop bit that was received. In Mode 0, RB8 is not used. TI Transmit interrupt flag. Set by hardware at the end of the 8th bit time in Mode 0, or at the beginning of the stop bit in the other modes, in any serial transmission. Must be cleared by software. RI Receive interrupt flag. Set by hardware at the end of the 8th bit time in Mode 0 or halfway through the stop bit time in the other modes, in any serial reception (except see SM2). Must be cleared by software. NOTE: *SMODO is located at PCONS. **Fosc = oscillator frequency The serial port can operate in 4 modes: In all four modes, transmission is initiated by any in- Mode 0: Shift Register, fIxed frequency struction that uses SBUF as a destination register. Re- ception is initiated in Mode 0 by the condition RI = 0 Mode 1: 8-Bit UART, variable frequency and REN = 1. Reception is initiated in the other Mode 2: 9-Bit UART, fIxed frequency modes by the incoming start bit if REN = 1. Mode 3: 9-Bit UART, variable frequency Mode 0: Serial data enters and exits through RXD. The baud rate in some modes is fIxed and in others is TXD outputs the shift clock. 8 bits are transmitted/re- generated by Timer 1 or Timer 2. ceived: 8 data bits (LSB fIrst). The baud rate is fIxed at 1/12 the oscillator frequency. 6-34 infel~ 87C51GB HARDWARE DESCRIPTION Mode 1: 10 bits are transmitted (through TXD) or re- 8.1 Framing Error Detection ceived (through RXD): a start bit (0), 8 data bits (LSB first), and a stop bit (1). On receive, the stop bit goes Framing Error Detection allows the serial port to check into RB8 in SCON. The baud rate in Mode 1 is vari- for valid stop bits in modes 1, 2, or 3. A missing stop bit able: you can use either Timer 1 to generate baud rates can be caused, for example, by noise on the serial lines, and/or Timer 2 to generate baud rates. Figure 25 shows or transmission by two CPUs simultaneously. the mode 1 Data Frame. If a stop bit is missing, a Framing Error bit (FE) is set. The FE bit can be checked in software after each recep- tion to detect communication errors. Once set, the FE ----, I DO 101 1021031041051061071 bit must be cleared in software. A valid stop bit will not I· I Data Byte • I' ·I clear FE. I Start Bit I Stop Bit The FE bit is located in SCON and shares the same bit 270897-27 address as SMO. Control bit SMODO in the PCON reg- ister determines whether the SMO or FE bit is accessed. Figure 25. Mode 1 Data Frame IfSMODO = 0, then accesses to SCON.7 are to SMO. If SMODO = 1, then accesses to SCON.7 are to FE. Mode 2: 11 bits are transmitted (through TXD) or re- ceived (through RXD): a start bit (0), 8 data bits (LSB first), a programmable 9th data bit, and a stop bit (1). 8.2 Multiprocessor Communications On Transmit, the 9th data bit (TB8 in SCON) can be assigned the value of 0 or 1. Or, for example, the parity Modes 2 and 3 provide a 9-bit mode to facilitate multi- bit (P in the PSW) could be moved into TB8. On re- processor communication. The 9th bit allows the con- ceive, the 9th data bit goes into RB8 in SCON, while troller to distinguish between address and data bytes. the stop bit is ignored. (The validity of the stop bit can The 9th bit is set to 1 for address and set to 0 for data be checked with Framing Error Detection.) The baud bytes. When receiving, the 9th bit goes into RB8 in rate is programmable to either 1/32 or 1/64 the oscilla- SCON. When transmitting, the ninth bit TB8 is set or tor frequency. See Figure 26. cleared in software. The serial port can be programmed such that when the stop bit is received the serial port interrupt will be acti- ----, IDO 101 1021031041051061071 osl vated only if the received byte is an address byte (RB8 I' · I' Data Byte • I' •I· • = 1). This feature is enabled by setting the SM2 bit in SCON. A way to use this feature in multiprocessor sys- stal Bit I stol Bit tems is as follows. Ninth Data Bit 270897-28 When the master processor wants to transmit a block of data to one of several slaves, it first sends out an ad- Figure 26. Mode 2 Data Frame dress byte which identifies the target slave. Remember, an address byte has its 9th bit set to 1, whereas a data Mode 3: 11 bits are transmitted (through TXD) or re- byte has its 9th bit set to O. All the slave processors ceived (through RXD): a start bit (0), 8 data bits (LSB should have their SM2 bits set to 1 so they will only be first), a programmable 9th data bit and a stop bit (1). In interrupted by an address byte. In fact, the 8XC51GB fact, Mode 3 is the same as Mode 2 in all respects has an Automatic Address Recognition feature which except the baud rate. The baud rate in Mode 3 is vari- allows only the addressed slave to be interrupted. That able: you can use Timer 1 and/or Timer 2 to generate is, the address comparison occurs in hardware, not soft- baud rates. See Figure 27. ware. (On the 8051 serial port, an address byte inter- rupts all slaves for an address comparison.) The addressed slave then clears its SM2 bit and pre- ----, IDO 101 102 I03 1041 051 06 1071 osl pares to receive the data bytes that will be corning. The I. · I' Data Byte • I. . I' • other slaves are unaffected by these data bytes. They are still waiting to be addressed since their SM2 bits are stal Bit I stol Bit all set. Ninth Data Bit 270897-29 Figure 27. Mode 3 Data Frame 6-35 87C51GB HARDWARE DESCRIPTION 8.3 Automatic Address Recognition zeros defined as don't-cares. The don't-cares also allow flexibility in defining the Broadcast Address, but in Automatic Address Recognition reduces the CPU time most applications a Broadcast Address will be OFFH. required to service the serial port. Since the CPU is only interrupted when it receives its own address, the The feature works the same way in the 8-bit mode software overhead to compare addresses is eliminated. (Mode 1) as in the 9-bit modes, except that the stop bit Automatic address recognition is enabled by setting the takes the place of the 9th data bit. If SM2 is set, the RI SM2 bit in SCON. With this feature enabled in one of flag is set only if the received byte matches the Given or the 9-bit modes, the Receive Interrupt (RI) flag will Broadcast Address and is terminated by a valid stop only get set when the received byte corresponds to ei- bit. Setting the SM2 bit has no effect in Mode O. ther a Given or Broadcast address. On reset, the SADDR and SADEN registers are initial- The master can selectively communicate with groups of ized to OOH, which defines the Given and Broadcast slaves by using the Given Address. Addressing all Addresses as XXXX XXXX (all don't-cares). This as- slaves at once is possible with the Broadcast Address. sures the 8XC51GB serial port to be backwards com- These addresses are defined for each slave by two Spe- patibility with other MCS-51 products which do not cial Function Registers: SADDR and SADEN. implement Automatic Addressing. A slave's individual address is specified in SADDR. SADEN is a mask byte that defmes don't-cares to form 8.4 Baud Rates the Given Address. These don't-cares allow flexibility in the user-defined protocol to address one or more The baud rate in Mode 0 is fixed: slaves at a time. The following is an example of how the user could defme Given Addresses to selectively ad- Oscillator Frequency Mode 0 Baud Rate = 12 dress different slaves. Slave 1: The baud rate in Mode 2 depends on the value of bit SADDR = 1111 0001 SMODI in Special Function Register PCON. If SADEN = 1111 1010 SMODI = 0 (which is the value on reset), the baud rate is 1/64 the oscillator frequency. If SMODI = 1, GIVEN = 1111 OXOX the baud rate is 1/32 the oscillator frequency. Slave 2: Oscillator Frequency Mode 2 Baud Rate = 2 SMOD1 X 64 SADDR = 1111 0011 SADEN = 1111 1001 The baud rates in Mode 1 and Mode 3 are determined GIVEN = 1111 OXX1 by the Timer 1 overflow rate, or by Timer 2 overflow rate, or by both (one for transmit and the other for The SADEN bytes are selected such that each slave can receive). be addressed separately. Notice that bit 1 (LSB) is a don't-care for Slave 1's Given Address, but bit 1 = 1 for Slave 2. Thus, to selectively communicate with just Slave 1 the master must send an address with bit 1 = 0 8.5 Timer 1 to Generate Baud Rates (e.g. 1111 00(0). Similarly, bit 2 = 0 for Slave 1, but is When Timer 1 is used as the baud rate generator, the a don't-care for Slave 2. Now to communicate with just baud rates in Modes 1 and 3 are determined by the Slave 2 an address with bit 2 = 1 must be used (e.g. Timer 1 overflow rate and the value of SMODt as fol- 1111 0111). Finally, for a master to communicate with lows: both slaves at once the address must have bit 1 = 1 and bit 2 = O. Modes 1 and 3 = 2 SMOD Timer 1 Overflow Rate Baud Rate 1x 32 Notice, however, that bit 3 is a don't-care for both slaves. This allows two different addresses to select Figure 28 shows how commonly used Baud Rates may both slaves (1111 0001 or 1111 0101). If a third slave be generated. The Timer 1 interrupt should be disabled was added that required its bit 3 = 0, then the latter in this application. Timer 1 can be configured for either address could be used to communicate with Slave 1 and "timer" or "counter" operation, and in any of its 3 2 but not Slave 3. running modes. In most applications, it is configured for "timer" operation in the auto-reload mode (high The master can also communicate with all slaves at once with the Broadcast Address. It is formed from the logical OR of the SADDR and SADEN registers with 6-36 intet 87C51GB HARDWARE DESCRIPTION nibble ofTMOD = 0010B). In this case, the baud rate 8.6 Timer 2 to Generate Baud Rates is given by the formula: Timer 2 is selected as the baud rate generator by setting Modes 1 and 3 = 2 SMOD1 x Oscillator Frequency TCLK and/or RCLK in T2CON. Note that the baud Baud Rate 32 x 12 x [256 - (TH1)] rates for transmit and receive can be simultaneously different. Setting RCLK and/or TCLK puts Timer 2 One can achieve very low baud rates with Timer 1 by into· its baud rate generator mode as shown in Figure leaving the Timer 1 interrupt enabled, and configuring 29. the Timer to run as a 16-bit timer (high nibble of TMOD = OOOlB), and using the Timer 1 interrupt to do a 16-bit software reload. Timer 1 Baud Rate Fosc SMOD C_T Mode Reload Value Mode 0 Max: 1 MHz 12MHz X X X X Mode 2 Max: 375K 12MHz 1 X X X Modes 1 & 3: 62.5K 12MHz 1 0 2 FFH 19.2K 11.059 MHz 1 0 2 FDH 9.6K 11.059 MHz 0 0 2 FDH 4.8K 11.059 MHz 0 0 2 FAH 2.4K 11.059 MHz 0 0 2 F4H 1.2K 11.059 MHz 0 0 2 E8H 137.5 11.986 MHz 0 0 2 1DH 110 6MHz 0 0 2 72H 110 12 MHz 0 0 1 FEEBH Figure 28. Timer 1 Generated Commonly Used Baud Rates nllER1 OVERFLOW RXCLOCK TXCLOCK "1WER 2·' T2EX PIN INTERRUPT EXEN2 L NOTE AVALABIUTY OF ADDITIONAL EXTERNAL INTERRUPT 270897-30 Figure 29. Timer 2 in Baud Rate Generator Mode 6-37 intel~ 87C51GB HARDWARE DESCRIPTION The baud rate generator mode is similar to the auto-re- Table 18 lists commonly used baud rates and how they load mode, in that a rollover in TH2 causes the Timer 2 can be obtained from Timer 2. registers to be reloaded with the 16-bit value in registers RCAP2H and RCAP2L, which are preset by software. It should be noted that when Timer 2 is running (TR2 = 1) in ''timer'' function in the baud rate generator The baud rates in Modes 1 and 3 are determined by mode, one should not try to read or write TH2 or TL2. Timer 2's overflow rate as follows: Under these conditions the Timer is being incremented every state time, and the results of a read or write may Modes 1 and 3 Timer 2 Overflow Rate not be accurate. The RCAP2 registers may be read, but Baud Rates = 16 shouldn't be written to, because a write might overlap a reload and cause write andlor reload errors. The timer Timer 2 can be configured for either "timer" or "coun- should be turned off (clear TR2) before accessing the ter" operation. In most applications, it is configured for Timer 2 or RCAP2 registers. "timer" operation (C_T2 = 0). The "Timer" opera- tion is different for Timer 2 when it's being used as a Table 18. Timer 2 Generated Baud Rates baud rate generator. Normally, as a timer, it increments Timer 2 every machine cycle (1/12 the oscillator frequency). As Baud Rate Fosc a baud rate generator, however, it increments every RCAP2H RCAP2L state time (1'2 the oscillator frequency). The baud rate formula is given below: 375K 12MHz FFH FFH 9.6K 12MHz FFH D9H Modes 1 and 3 _ Oscillator Frequency 4.BK 12MHz FFH B2H Baud Rate - 32 x [65536 - (RCAP2H, RCAP2L)1 2.4K 12MHz FFH 64H 1.2K 12MHz FEH CBH where (RCAP2H, RCAP2L) is the content of 300 12MHz FBH 1EH RCAP2H and RCAP2L taken as a 16-bit unsigned in- 110 12MHz F2H AFH teger. 300 6MHz FDH 8FH 110 6MHz F9H 57H Timer 2 as a baud rate generator is valid only if RCLK andlor TCLK = 1 in T2CON. Note that a rollover in TH2 does not set TF2, and will not generate an inter- rupt. Therefore, the Timer 2 interrupt does not have to 9.0 SERIAL EXPANSION PORT be disabled when Timer 2 is in the baud rate generator mode. Note too, that if EXEN2 is set, a 1-to-O tran- The Serial Expansion Port (SEP) allows a wide variety sition on the T2EX pin will set EXF2 but will not cause of serially hosted peripherals to be connected to the a reload from (RCAP2H, RCAP2L) to (TH2, TL2). 8XCSIGB. The SEP has four programmable modes Thus when Timer 2 is in use as a baud rate generator, and four clock options. There is a single bi-directional T2EX can be used as an extra external interrupt, if data pin (p4.1) and a clock output pin (p4.0). Data desired. transfers consist of 8 clocks with 8 bits of data received or transmitted. When not transmitting or receiving the data and clock pins are inactive. There are 3 SFRs asso- ciated with the SEP as shown in Figure 30. ~(~M_S~8)~~____- r____~______~____~____~____~__~(~LS_8~) SEPCON I ~:~~:~~:~ ~ ~I~ ~(~M~S~8)~~____- r____~______~____~____~____~__~(=LS~8~) SEPDAT L...I_ _....L-_ _L....-_---L_ _--L.._ _....L.-_ _.l....-_---L_ _ ~I OE7H r-(:..M_S....:8)~-.--____- r____~______..__----_r_----__._----__..--~(.:..LS:....:8...:..,) SEPSTAT I : SEPFWR : SEPFRD: SEPIF I OF7H Figure 30. SEP SFRs 6-38 infel~ 87C51GB HARDWARE DESCRIPTION None of the SEP SFRs are bit addressable. However, Table 19. Determination of SEP Modes the individual bits of SEPSTAT and SEPCON are sig- nificant and have symbolic names associated with them CLKPOL CLKPH SEPMode as shown. The meaning of these bits are: 0 0 SEPMODEO SEPE - SEP Enable bit 0 1 SEPMODE1* SEPREN - SEP Receive ENable 1 0 SEPMODE2 1 1 SEPMODE3* CLKPOL - CLocK POLarity CLKPH - CLocK PHase The four clock options determine the rate at which data SEPS 1 - SEP Speed select 1 is shifted out of or into the SEP. All four rates are SEPSO - SEP Speed select 0 fractions of the oscillator frequency. Table 20 shows the SEPFWR - SEP Fault during WRite various rates that can be selected for the SEP. SEPFRD - SEP Fault during ReaD Table 20. SEP Data Rates SEPIF - SEP Interrupt Flag SEPS1 SEPSO Data Rate 0 0 Fosc/12 9.1 Programmable Modes and 0 1 Fosc/ 24 Clock Options 1 0 Fosc/ 48 1 1 Fosc/ 96 The four programmable modes determine the inactive level of the clock pin and which edge of the clock is used for transmission or reception. These four modes are shown in Figure 31. Table 19 shows how the modes are determined. SEPMODEO CLOCK SEPMODE2 CLOCK DATA SAMPLED DATA OUTPUT • SEPMODE1 CLOCK • SEPWODE3 CLOCK DATA SAMPLED DATA OUTPUT 270897-31 ·Cannot be used for receive mode. Figure 31. SEP Modes 6-39 87C51GB HARDWARE DESCRIPTION 9.2 SEP Transmission or Reception rupts may still be serviced, even after a software upset. To make the best use of the WDT. it should be serviced To transmit or receive a byte the user should initialize in those sections of code that will periodically be exe- the SEP mode (CLKPOL and CLKPH), clock frequen- cuted within the time required to prevent a WDT reset. cy (SEPS 1 and SEPSO), and enable the SEP (SEPE). A transmission then occurs if the user loads data into SEPDATA. A reception occurs if the user sets 10.2 WDT During Power Down and SEPREN while SEPDATA is empty and a transmis- Idle sion is not in progress. When 8 bits have been received SEPREN will be cleared by hardware. Once the trans- In Power Down mode the oscillator stops, which means mission or reception is complete, SEPIF will be set. the WDT also stops. While in Power Down the user SEPIF remains set until cleared by software. SEPIF is does not need to service the WDT. There are two meth- also the source of the SEP interrupt. Data is transmit- ods of exiting Power Down: by a reset or via a level ted and received MSB first. activated external interrupt which is enabled prior to entering Power Down. If Power Down is exited with If the user attempts to read or write the SEPDATA reset, servicing of the WDT should occur as it normally register or write to the SEPCON register while the SEP does whenever the 8XC51GB is reset. Exiting Power is transmitting or receiving an error bit is set. The Down with an interrupt is significantly different. The SEPFWR bit is set if the action occurred while the SEP interrupt is held low which brings the device out of was transmitting. The SEPFRD bit is set if the action Power Down and starts the oscillator. The user must occurred while the SEP was receiving. There is no in- hold the interrupt low long enough for the oscillator to terrupt associated with these error bits. The bit remains stabilize. When the interrupt is brought high, the inter- set until cleared by software. The attempted read or rupt is serviced. To prevent the WDT from resetting write of the register is ignored. The reception of trans- the device while the interrupt pin is held low, the WDT mission that was in progress will not be affected. is not started until the interrupt is pulled high. It is suggested that the WDT be reset during the interrupt service routine for the interrupt used to exit Power 10.0 HARDWARE WATCHDOG TIMER Down. The hardware WatchDog Timer (WDT) resets the To ensure that the WDT does not overflow within a 8XC51GB when it overflows. The WDT is intended as few states of exiting of powerdown, it is best to reset the a recovery method in situations where the CPU may be WDT just before entering powerdown. subjected to a software upset. The WDT consists of a 14-bit counter and the WatchDog Timer ReSeT In Idle mode, the oscillator continues to run. To pre- (WDTRST) SFR. The WDT is always enabled and in- vent the WDT from resetting the 8XC51GB while in crements while the oscillator is running. There is no Idle, the user should always set up a timer that will way to disable the WDT. This means that the user must periodically exit Idle, service the WDT, and re-enter still service the WDT while testing or debugging an Idle mode. application. The WDT is loaded with 0 when the 8XC51GB exits reset. The WDT described in this sec- tion is not the Watchdog Timer associated with PCA 11.0 OSCILLATOR FAIL DETECT module 4. The WDT does not drive the Reset pin. The Oscillator Fail Detect (OFD) circuitry keeps the 8XC51GB in reset when the oscillator speed is below 10.1 Using the WDT the OFD trigger frequency. The OFD trigger frequency is shown in the data sheet as a minimum and maxi- Since the WDT is automatically enabled while the mum. If the oscillator frequency is below the minimum, processor is running, the user only needs to be con- the device is held in reset. If the oscillator frequency is cerned with servicing it. The 14-bit counter overflows greater than the maximum, the device will not be held when it reaches 16383 (3FFFH). The WDT increments in reset. If the frequency is between the minimum and once every machine cycle. This means the user must maximum, it is indeterminate whether the device will reset the WDT at least every 16383 machine cycles. If be held in reset or not. the user does not wish to use the functionality of the WDT in an application, a timer interrupt can be used The OFD is automatically enabled when the device to reset the WDT. To reset the WDT the user must comes out of reset or when Power Down is exited with write 01EH and OE1H to WDTRST. WDTRST is a a reset or an interrupt. write only register. The WDT count cannot be read or written. Using a timer interrupt is not recommended in The OFD is intended to function only in situations applications that make use of the WDT because inter- where there is a gross failure of the oscillator, such as a 6-40 intaL 87C51GB HARDWARE DESCRIPTION broken crystal. To fulfill this need the OFD trigger fre- quency is significantly below the normal operating fre- quency. The OFD will not reset the 8XC5lGB if the oscillator frequency should change to another point within the operating range. 11.1 OFD During Power Down rro----------------------------__. In Power Down, the 8XC5lGB oscillator stops in or- iNfi--~ der to conserve power. To prevent the 8XC5lGB from immediately resetting itself out of power down the OFD must be disabled prior to setting the PD bit. Writ- ing the sequence "OElH, OlEH" to the OSCillatoR (OSCR) SFR, turns the OFD off. Once disabled, the rro------------------------------+ OFD can only be re-enabled by a reset or exit from Power Down with an interrupt. The status of the OFD CF--:JECF (whether on or oft) can be determined by reading OSCR. The LSB indicates the status of the OFD. The upper 7 bits of OSCR will always be ls when read. If OSCR = OFFH, the OFD is enabled. If OSCR CCFn~ECCFn l '------F Y OFEH, the OFD is disabled. 1 YL-'-:;7'-'---' RI-------- n------~ 12.0 INTERRUPTS The 8XC5lGB has a total of 15 interrupt vectors: seven rr2-------- EXF2-------~ external interrupts (INTO, INTI, INT2, INT3, INT4, INT5, and INT6), three timer interrupts (Timers 0, 1, and 2), two PCA interrupts (pCAO and PCAl), the AI INT2 D interrupt, the SEP interrupt, and the serial port in- terrupt. Figure 32 shows the interrupt sources. All of the bits that generate interrupts can be set or cleared by software, with the same result as though it had been set or cleared by hardware. That is, interrupts INT3 can be generated or pending interrupts can be canceled in software. INT4------i 12.1 Externallnterrupts External Interrupts INTO and INTI can each be either INT5------1 level-activated or negative edge-triggered, depending on bits ITO and ITI in register TCON. IfITx = 0, exter- INT6------1 nal interrupt x is triggered by a detected low at the INTx pin. If ITx = 1, external interrupt x is negative edge-triggered. CF1--:JECl INT2 and INT3 can each be either negative or positive edge-triggered, depending on bits IT2 and IT3 in regis- ter EXICON. IflTx = 0, external interrupt x is nega- 1YI..-.-r ClCFn~EClCFn tive edge-triggered. If ITx = 1, external interrupt x is positive edge-triggered. 1 y 5, ) INT4, INT5, and INT6 are positive edge-triggered AIF--------------------__. S~------------------------__. only. 270897-32 Figure 32. Interrupt Sources 6-41 87C51GB HARDWARE DESCRIPTION Table 21. EXICON: External Interrupt Control Register EXICON Reset Value = XOOO OOOOB Address = OC6H Not Bit Addressable Bit 7 6 5 4 3 210 EXICON IE6 IE5 IE4 IE3 IE2 IT3 IT2 Symbol Function Not implemented, reserved for future use. * IE6 Interrupt 6 Edge flag. This bit is set by hardware when an external interrupt edge is detected. IE5 Interrupt 5 Edge flag. This bit is set by hardware when an external interrupt edge is detected. IE4 Interrupt 4 Edge flag. This bit is set by hardware when an external interrupt edge is detected. IE3 Interrupt 3 Edge flag. This bit is set by hardware when an external interrupt edge is detected. IE2 Interrupt 2 Edge flag. This bit is set by hardware when an external interrupt edge is detected. IT3 Interrupt 3 Type control bit. This bit is set or cleared by software to control whether INT3 is positive or negative transition activated. When IT3 is high, IE3 is set by a positive transition on pin INT3. When IT3 is low, IE3 is set by a negative transition on pin INT3. IT2 Interrupt 2 Type control bit. This bit is set or cleared by software to control whether INT2 is positive or negative transition activated. When IT2 is high, IE2 is set by a positive transition on pin INT2. When IT2 is low, IE2 is set by a negative transition on pin INT2. ·Using software should not write 1s to reserved bits. These bits may be used in future 8051 family products to invoke new features. In that case, the reset or inactive value of the new bit will be 0, and its active value will be 1. The value read from reserved bit is indeterminate. The flags that actually generate the interrupts are bits external interrupt is transition-activated, the external lEO and IE1 in TCON and IE2, IE3, IE4, IES, and IE6 source has to hold the request pin high for at least one in EXICON. These flags are cleared by hardware when cycle, and then hold it low for at least one cycle to the service routine is vectored to if the interrupt was ensure that the transition is seen so that interrupt re- transition-activated. If the interrupt was level-activated, quest flag lEx will be set. lEx will be automatically then the external requesting source is what controls the cleared by the CPU when the service routine is called. request flag, rather than the on-chip hardware. The ex- ternal interrupts are enabled through bits EXO and If external interrupt INTO or INT1 is level-activated, EX1 in the IE register and EX2, EX3, EX4, EXS, and the external source has to hold the request active until EX6 in the lEA register. the requested interrupt is actually generated. Then it has to deactivate the request before the interrupt serv- Since the external interrupt pins are sampled once each ice routine is completed, or else another interrupt will machine cycle, an input high or low should hold for at be generated. least 12 oscillator periods to ensure sampling. If the 6-42 intaL 87C51GB HARDWARE DESCRIPTION 12.2 Timer Interrupts The PCA interrupt is enabled by bit EC in the IE regis- ter. The PCAI interrupt is enabled by bit ECI in the Timer 0 and Timer I interrupts are generated by TFO lEA register. In addition, the CF (CFl) flag and each and TFI in register TCON, which are set by a rollover of the CCFn (ClCFn) flags must also be individually in their respective Timer/Counter registers; the excep- enabled by bits ECF (ECFl) and ECCFn (ECICFn) in tion is Timer 0 in Mode 3. When a timer interrupt is registers CMOD (CIMOD) and CCAPMn generated, the flag that generated it is cleared by the (ClCAPMn), respectively, in order for that flag to be on-chip hardware when the service routine is vectored able to cause an interrupt. to. These timer interrupts are enabled by bits ETO and ETI in the IE register. 12.4 Serial Port Interrupt Timer 2 interrupt is generated by the logical OR of bits TF2 and EXF2 in register T2CON. Neither of these The serial port interrupt is generated by the logical OR flags is cleared by hardware when the service routine is of bits RI and TI in register SCON. Neither of these vectored to. In fact, the service routine may have to flags is cleared by hardware when the service routine is determine whether it was TF2 or EXF2 that generated vectored to. The service routine will normally have to the interrupt, and the bit will have to be cleared in determine whether it was RI or TI that generated the software. The Timer 2 interrupt is enabled by the ET2 interrupt, and the bit will have to be cleared in soft- bit in the IE register. ware. The serial port interrupt is enabled by bit ES in the IE register. 12.3 peA Interrupt 12.5 Interrupt Enable The PCA interrupts are generated by the logical OR of five event flags (CCFn, ClCFn) and the PCA timer Each of these interrupt sources can be individually en- overflow flag (CF, CFl) in the registers CCON and abled or disabled by setting or clearing a bit in the ClCON. None of these flags are cleared by hardware Interrupt Enable (IE and lEA) registers as shown in when the service routine is vectored to. Normally the Table 22. Note that IE also contains a global disable service routine will have to determine which bit flagged bit, EA. If EA is set (1), the interrupts are individually the interrupt and clear that bit in software. This allows enabled or disabled by their corresponding bits in IE the user to define the priority of servicing each PCA and lEA. If EA is clear (0), all interrupts are disabled. module. Figure 33 shows the interrupt control system.- 6-43 intel~ 87C51GB HARDWARE DESCRIPTION S~--------------------------~~ INT2 - - - - - - - INT3 INTI ~ Q. :::s l- I- CD Air ~ .E INT4 INTS--------I ~------------~~------~+<~ ~~------------~~------~~ INT6---------I Lowest Priority Interrupt 270897-33 Figure 33. Interrupt Control System 6-44 87C51GB HARDWARE DESCRIPTION Table 22. Interrupt Enable Registers IE Address = DASH Reset Value = 0000 OOOOB Bit Addressable EA EC ET2 ES ET1 EX1 ETO I EXO ! Bit 7 6 5 4 3 2 0 lEA Address = OA7H Reset Value = 0000 OOOOB Not Bit Addressable I EAD I EX6 I EX5 I EX4 I EX3 I EX2 I EC1 IESEP! Bit 7 6 5 4 3 2 0 Enable bit = 1 enables the interrupt Enable bit = 0 disables the interrupt Symbol Function EA Global disable bit. If EA=O, all Interrupts are disabled. If EA= 1, each Interrupt can be individually enabled or disabled by setting or clearing its enable bit. EC PCA interrupt enable bit. ET2 Timer 2 interrupt enable bit ES Serial Port interrupt enable bit. ET1 Timer 1 interrupt enable bit. EX1 External interrupt 1 enable bit. ETO Timer 0 interrupt enable bit. EXO External interrupt 0 enable bit. EAD AID converter interrupt enable bit. EX6 External interrupt 6 enable bit. EX5 External interrupt 5 enable bit. EX4 External interrupt 4 enable bit. EX3 External interrupt 3 enable bit. EX2 External interrupt 2 enable bit. EC1 PCA1 interrupt enable bit. ESEP Serial Expansion Port interrupt enable bit. 12.6 Interrupt Priorities A low-priority interrupt can itself be interrupted by a higher priority interrupt, but not by another interrupt Each interrupt source on the 8XC51 GB can be individ- of the same priority. The highest priority interrupt can- ually programmed to one of four priority levels, by set- not be interrupted by any other interrupt source. ting or clearing the bits in the Interrupt Priority (lP and IPA) registers and the Interrupt Priority High If two or more requests of different priority levels are (IPH and IPAH) registers. See Table 23. The IPH reg- received simultaneously, the request of higher priority isters have the same bit map as the IP registers with an level is serviced. If requests of the same priority level "H" added to each bit's name. This gives each interrupt are received simultaneously, an internal polling se- source two bits for setting the priority levels. The LSB quence determines which request is serviced. Thus of the Priority Select Bits is in the IP SFR, and the within each priority level there is a second priority MSB is in the IPH SFR. structure determined by the polling sequence shown in Table 24. 6-45 intel4P 87C51GB HARDWARE DESCRIPTION Table 23. Interrupt Priority Registers IP Address = OBSH Reset Value = XOOO OOOOB Bit Addressable I - I PPC I PT2 I PS PT1 PX1 PTO I PXO I IPA Address = OB6H Reset Value = 0000 OOOOB Not Bit Addressable I PAD I PX6 I PX5 I PX4 PX3 PX2 PC1 I PSEPI IPH Address = OB7H Reset Value = XOOO OOOOB Not Bit Addressable I - IPPPC IPT2H I PSH IPT1H IPX1H I PTOH I PXOH I IPHA Address = OB5H Reset Value = 0000 OOOOB Not Bit Addressable IPADH I PX6H I PX5H I PX4H I PX3H I PX2H I PC1 H IpSEP~ Priority Bit Priority Bit H Priority 0 0 Lowest 0 1 1 0 1 1 Highest Symbol Function Not Implemented, reserved for future use* PPC, PPCH PCA interrupt priority bits PT2,PT2H Timer 2 interrupt priority bits PS, PSH Serial Port interrupt priority bits PT1,PT1H Timer 1 interrupt priority bits PX1,PX1H External interrupt 1 interrupt priority bits PTO,PTOH Timer 0 interrupt priority bits PXO,PXOH External interrupt 0 interrupt priority bits PAD, PADH AID converter interrupt priority bits PX6,PX6H External interrupt 6 interrupt priority bits PX5, PX5H External interrupt 5 interrupt priority bits PX4,PX4H External interrupt 4 interrupt priority bits PX3,PX3H External interrupt 3 interrupt priority bits PX2,PX2H External interrupt 2 interrupt priority bits PC1, PC1H PCA 1 interrupt priority bits PSEP, PSEPH Serial Expansion Port interrupt priority bits NOTE: ·User software should not write 1s to reserved bits. These bits may be used in future 8051 f{lmily products to invoke new features. In that case, the reset or inactive value of the new bit will be 0, and its active value will be 1. The value read from a reserved bit is indeterminate. 6-46 inteL 87C51GB HARDWARE DESCRIPTION Table 24. Interrupt Polling Sequence not being responded to for one of the above conditions and is not still active when the blocking condition is 1 (Highest) INTO removed, the denied interrupt will not be serviced. In 2 SEP other words, the fact that the interrupt flag was once 3 INT2 active but not serviced is not remembered. Every poll- 4 Timer 0 ing cycle is new. 5 PCA1 6 INT3 The polling cycle/LCALL sequence is illustrated in the 7 INT1 Interrupt Response Timing Diagram. 8 AID Note that if an interrupt of a higher priority level goes 9 INT4 active prior to S5P2 of the machine cycle labeled C3 in 10 Timer 1 the diagram, then in accordance with the above rules it 11 PCA will be vectored to during C5 and C6, without any in- 12 INT5 struction of the lower priority routine having been exe- 13 PCA cuted. This is the fastest possible response when C2 is 14 Timer 2 the final cycle of an instruction other than RETI or 15 (Lowest) INT6 write IE or IP. Thus the processor acknowledges an interrupt request Note that the "priority within level" structure is only by executing a hardware-generated LCALL to the ap- used to resolve simultaneous requests of the same prior- propriate servicing routine. The hardware-generated ity level. LCALL pushes the contents of the Program Counter onto the stack (but it does not save the PSW) and re- loads the PC with an address that depends on the 12.7 Interrupt Processing source of the interrupt being vectored to. Table 25 shows the interrupt vector addresses. The interrupt flags are sampled at S5P2 of every ma- chine cycle. The samples are polled during the follow- Table 25. Interrupt Vector Addresses ing machine cycle. The Timer 2 overflow interrupt is slightly different, as described in the Interrupt Re- Interrupt Interrupt Cleared by Vector sponse Time section. If one of the flags was in a set Source Request Bits Hardware Address condition at S5P2 of the preceding cycle, the polling cycle will fmd it and the interrupt system will generate INTO lEO No (level) 0003H an LCALL to the appropriate service routine, provided Yes (trans.) this hardware-generated LCALL is not blocked by any Timer 0 TFO Yes OOOBH of the following conditions: INT1 IE1 No (level) 0013H 1. An interrupt of equal or higher priority level is al- ready in progress. Yes (trans.) 2. The current (polling) cycle is not the final cycle in Timer 1 TF1 Yes 001BH the execution of the instruction in progress. 0023H Serial Port RI,TI No 3. The instruction in progress is RETI or any write to the IE or IP registers. Timer 2 TF2,EXF2 No 002BH PCA CF,CCFn No 0033H Any of these three conditions will block the generation (n = 0-4) of the LCALL to the interrupt service routine. Condi- tion 2 ensures that the instruction in progress will be AID AIF No 003BH completed before vectoring to any service routine. Con- PCA1 CF1, C1CCFn No 0043H dition 3 ensures that if the instruction in progress is (n = 0-4) RETI or any write to IE or IP, then at least one more instruction will be executed before any interrupt is vec- SEP SEPIF No 004BH tored to. INT2 IE2 Yes 0053H The polling cycle is repeated with each machine cycle, INT3 IE3 Yes 005BH and the values polled are the values that were present at S5P2 of the previous machine cycle. If the interrupt INT4 IE4 Yes 0063H flag for a level-sensitive external interrupt is active but INT5 IE5 Yes 006BH INT6 IE6 Yes 0073H 6-47 87C51GB HARDWARE DESCRIPTION Execution proceeds from that location until the RETI The Timer 0 and Timer t flags, TFO and TF 1, are set at instmction is encountered, The RETI instruction in- S5P2 of the cycle in which the tLmers ove:r:f!ow. The forms the processor that this interrupt routine is no values are then polled by the circuitry in the next cycle. longer in progress, then pops the top two bytes from the However, the Timer 2 flag TF2 is set at S2P2 and is stack and reloads the Program Counter. Execution of polled in the same cycle in which the timer overflows. the interrupted program continues from where it left off. If a request is active and conditions are right for it to be acknowledged, a hardware subroutine call to the re- Note that a simple RET instruction would also have quested service routine will be the next instruction to be returned execution to the interrupted program, but it executed. The call itself takes two cycles. Thus, a mini- would have left the interrupt control system thinking mum of three complete machine cycles elapses between interrupt was still in progress. activation of an external interrupt request and the be- ginning of execution of the service routine's first in- The starting addresses of consecutive interrupt service struction. See Figure 34. routines are only 8 bytes apart. That means if consecu- tive interrupts are being used (lEO and TFO, for exam- A longer response time would result if the request is ple, or TFO and lEt), and if the first interrupt routine is blocked by one of the 3 conditions discussed in the In- more than 7 bytes long, then that routine will have to terrupt Processing section. If an interrupt of equal or execute a jump to some other memory location where higher priority level is already in progress, the addition- the service routine can be completed without overlap- al wait time obviously depends on the nature of the ping the starting address of the next interrupt routine. other interrupt's service routine. If the instruction in progress is not in its final cycle, the additional wait time cannot be more than 3 cycles, since the longest instruc- 12.8 Interrupt Response Time tions (MUL and DIV) are only 4 cycles long, and if the instruction in progress is RETI or write to IE or IP, the The INTO and INTt levels are inverted and latched additional wait time cannot be more than 5 cycles (a into the Interrupt Flags lEO, and lEt at S5P2 of every maximum of one or more cycles to complete the in- machine cycle. The level of interrupts 2 through 6 are struction in progress, plus 4 cycles to complete the next also latched into the appropriate flags (IE2-IE6) in instruction if the instruction is MUL or DIV). S5P2. Similarly, the Timer 2 flag EXF2 and the Serial Port flags RI and TI are set at SSP2. The values are not Thus, in a single-interrupt system, the response time is actually polled by the circuitry until the next machine always more than 3 cycles and less than 9 cycles. cycle. ········---C1 • I • C2 • I C3 • I • C4 • I cs-_····· ISSP21 S6 r.1 INTERRUPT ........ ~ INTERRUPT '----- " ... INTERRUPTS ARE POLLED ,.. 11 LONG CALL TO INTERRUPT 11 ,.. ---- ..... INTERRUPT ROUTINE VECTOR ADDRESS GOES LATCHED ACTIVE 270897-34 This is the fastest possible response when C2 is the final cycle of an instruction other than RETI or write IE or IP. Figure 34. Interrupt Response Timing Diagram 6-48 int'eL 87C51GB HARDWARE DESCRIPTION 13.0 RESET When power is turned on, the circuit holds the RESET pin high for an amount of time that depends on The reset input is the RESET pin, which has a Schmitt the capacitor value and the rate at which it charges. To Trigger input. A reset is accomplished by holding the ensure a valid reset the RESET pin must be held low RESET pin low for at least two machine cycles (24 long enough to allow the oscillator to start up plus two oscillator periods). On the 8XC51GB, reset is asyn- machine cycles. chronous to the CPU clock. This means that the oscil- lator does not have to be running for the 1/0 pins to be On power up, Vee should rise within approximately in their reset condition. However, Vee has to be within ten milliseconds. The oscillator start-up time will de- the specified operating conditions. pend on the oscillator frequency. For a 10 MHz crystal, the start-up time is typically 1 ms. For a 1 MHz crystal, Once Reset has reached a high level, the 8XC51GB the start-up time is typically 10 ms. may remain in its reset state for up to 5 machine cycles. This is caused by the OFD circuitry. Powering up the device without a valid reset could cause the CPU to start executing instructions from an While the RESET pin is low, the port pins, ALE and indeterminate location. This is because the SFRs, spe- PSEN are weakly pulled high. After RESET is pulled cifically the Program Counter, may not get properly high, it will take up to 5 machine cycles for ALE and initialized. PSEN to start clocking. For this reason, other devices can not be synchronized to the internal timings of the 8XC51GB. 14.0 POWER-SAVING MODES Driving the ALE and PSEN pins to 0 while reset is For applications where power consumption is critical, active could cause the device to go into an indetermi- the 8XC51GB provides two power reducing modes of nate state. operation: Idle and Power Down. The input through which backup power is supplied during these opera- The internal reset algorithm redefines most of the tions is Vee. The Idle and Power Down modes are SFRs. Refer to individual SFRs for their reset values. activated by setting bits IDL and PD, respectively, in The internal RAM is not affected by reset. On power the SFR PCON (Table 26). Figure 36 shows the Idle up the RAM content is indeterminate. and Power Down circuitry. In the Idle mode (IDL = 1), the oscillator continues to 13.1 Power-On Reset run and the Interrupt, Serial Port, PCA, and Timer blocks continue to be clocked, but the clock signal is For CHMOS devices, when Vee is turned on, an auto- gated off to the CPU. In Power Down (PD = 1), the matic reset can be obtained by connecting the oscillator is frozen. RESET pin to Vss through a 1 p.F capacitor. The CHMOS devices do not require an external resistor like the HMOS devices because they have an internal pullup on the RESET pin. Figure 35 shows this. Vee RST vce 8XC51GB Vss Vss 270897-35 Figure 35. Power-on Reset Circuitry 6-49 intel~ 87C51GB HARDWARE DESCRIPTION Table 26. PCON: Power Control Register PCON Address = 87H Reset Value = OOXX OOOOB Not Bit Addressable ISMOD11 SMODO I POF GF1 GFO PD IDL Bit 7 6 5 4 3 2 o Symbol Function SMOD1 Double Baud rate bit. When set to a 1 and Timer 1 is used to generate baud rates, and the Serial Port is used in modes 1, 2, or 3. SMODO When set, Read/Write accesses to SCON.7 are to the FE bit. When clear, Read/Write accesses to SCON.7 are to the SMO bit. Not implemented, reserved for future use. '" POF Power Off Flag. Set by hardware on the rising edge of Vee. Set or cleared by software. This flag allows detection of a power failure caused reset. Vee must remain above 3V to retain this bit. GF1 General-purpose flag bit. GFO General-purpose flag bit. PD Power Down bit. Setting this bit activates Power Down operation. IDL Idle mode bit. Setting this bit activates idle modes operation. If 1s are written to PD and IDL at the same time, PD takes precedence. NOTE: ·User software should not write 1s to unimplemented bits. These bits may be used in future 8051 family products to invoke new features. In that case, the reset or inactive value of the new bit will be 0, and its active value will be 1. The value read from a reserved bit is indeterminate. ~!~ XTAL 2 '= XTAL 1 INTERRUPT, ~+--C>SERIAL PORT, TIMER BLOCKS CPU 270897-36 Figure 36. Idle and Power Down Hardware 6-50 inteL 87C51GB HARDWARE DESCRIPTION 14.1 Idle Mode 14.2 Power Down Mode An instruction that sets the IDL bit causes that to be An instruction that sets the PD bit causes that to be the the last instruction executed before going into the Idle last instruction executed before going into the Power mode. In the Idle mode, the internal clock signal is Down mode. In this mode the on-chip oscillator is gated off to the CPU, but not to the Interrupt, Timer, stopped. With the clock frozen, all functions are and Serial Port functions. The PCA and PCA 1 timers stopped, but the on-chip RAM and Special Function can be programmed either to pause or continue operat- Registers are held. The port pins output the values held ing during Idle with the CIDL (ClIDL) bit in CMOD by their respective SFRs, and ALE and PSEN output (CIMOD). The CPU status is preserved in its entirety: lows. In Power Down, Vee can be reduced to as low as the Stack Pointer, Program Counter, Program Status 2V. Care must be taken, however, to ensure that Vee is Word, Accumulator, and all other registers maintain not reduced before Power Down is invoked. If the Os- their data during Idle. The port pins hold the logical cillator Fail Detect circuitry is not disabled before en- states they had at the time Idle was activated. ALE and tering powerdown, the part will reset itself (see Section PSEN hold at logic high levels. Refer to Table 27. 11.0 "Oscillator Fail Detect"). Table 28 shows the status of external pins during Power Down mode. Table 27. Status of the External Pins during Idle Mode Table 28. Status of the External Pins during Power Down Mode Program Ports ALE PSEN Porto Port 1 Port 2 Memory 3,4,5 Program Ports ALE PSEN PortO Port 1 Port 2 Memory 3,4,5 Internal 1 1 Data Data Data Data External 1 1 Float Data Address Data Internal 0 0 Data Data Data Data External 0 0 Float Data Data Data There are two ways to terminate the Idle Mode. Activa- tion of any enabled interrupt will cause the IDL bit to The 8XC5IGB can exit Power Down with either a be cleared by hardware, terminating the Idle mode. The hardware reset or external interrupt. Reset redefines interrupt will be serviced, and following RET! the next most of the SFRs but does not change the on-chip instruction to be executed will be the one following the RAM. An external interrupt allows both the SFRs and instruction that put the device into Idle. the on-chip RAM to retain their values. The flag bits (GFO and GFI in PCON) can be used to To properly terminate Power Down the reset or exter- give an indication if an interrupt occurred during nor- nal interrupt should not be executed before Vcc is re- mal operation or during Idle. For example, an instruc- stored to its normal operating level and must be held tion that activates Idle can also set one or both flag bits. active long enough for the oscillator to restart and sta- When Idle is terminated by an interrupt, the interrupt bilize (normally less than 10 ms). service routine can examine the flag bits. With an external interrupt, INTO or INTI must be en- The other way of terminating the Idle mode is with a abled and configured as level-sensitive. Holding the pin hardware reset. Since the clock oscillator is still run- low restarts the oscillator and bringing the pin back ning, the hardware reset needs to be held active for only high completes the exit. After the RETI instruction is two machine cycles (24 oscillator periods) to complete executed in the interrupt service routine, the next in- the reset. struction will be the one following the instruction that put the device in Power Down. The signal at the RESET pin clears the IDL bit directly and asynchronously. At this time the CPU resumes program execution from where it left off; that is, at the 14.3 Power Off Flag instruction following the one that invoked the Idle Mode. As shown in the Reset Timing diagram, two or The Power Off Flag (pOF) located at PCON.4 is set by three machine cycles of program execution may take hardware when Vee rises from OV to 5V. POF can also place before the internal reset algorithm takes control. be set or cleared by software. This allows the user to On-chip hardware inhibits access to the internal RAM distinguish between a "cold start" reset and a "warm during this time, but access to the port pins is not inhib- start" reset. ited. To eliminate the possibility of unexpected outputs at the port pins, the instruction following the one that A cold start reset is one that is coincident with Vee invokes Idle should not be one that writes to a port pin being turned on to the device after it was turned off. A or to external Data RAM. warm start reset occurs while Vee is still applied to the device and could be generated, for example, by a Watchdog Timer or an exit from Power Down. 6-51 infel~ 87C51GB HARDWARE DESCRIPTION Immediately after reset, the user's software can check Table 29. EPROM/OTP Lock Bits the status of the POF bit. POF = 1 would indicate a cold start. The software then clears POF and com- Program mences its tasks. POF = 0 immediately after reset Lock Bits Logic Enabled would indicate a warm start. LB1 LB2 LB3 U U U No Program Lock features Vee must remain above 3V for POF to retain a O. enabled. (Code Verify will still be encrypted by the Encryption Array.) 15.0 EPROM/OTP PROGRAMMING P U U MOVC instructions executed The 8XC51GB uses the fast "Quick-Pulse" Program- from external program ming algorithm. The devices program at Vpp = memory are disabled from 12.7SV (and Vee = S.OV) using a series offive 100 p.s fetching code bytes from PROG pulses per byte programmed. internal memory. EA is sampled and latched on reset, and further 15.1 Program Memory Lock programming of EPROM is disabled. In some microcontroller applications it is desirable that the Program Memory be secure from software piracy. P P U Same as above, but Verify is The 8XCSIGB has a three-level program lock feature also disabled (option which protects the code of the on-chip EPROM/OTP available on EPROM only). or ROM. P P P Same as above and all external program execution is Within the EPROM/OTP/ROM are 64 bytes of En- inhibited and internal RAM cryption Array that are initially unprogrammed (all cannot be read externally. ls). The user can program the Encryption Array to encrypt the program code bytes during EPROM/OTPI NOTE: ROM verification. The verification procedure is per- All other combinations of lock bits may produce indetermi- formed as usual except that each code byte comes out nate results and should not be used. exclusive-NOR'ed (XNOR) with one of the key bytes. Therefore, to read the ROM code the user has to know the 64 key bytes in their proper sequence. 16.0 ONCE MODE Unprogrammed bytes have the value OFFH. So if the The ONCE (ON-Circuit Emulation) mode facilitates Encryption Array is left unprogrammed, all the key testing and debugging of systems using the 8XCSIGB bytes have the value OFFH. Since any code byte without having to remove the device from the circuit. XNORed with OFFH leaves the byte unchanged, leav- The ONCE mode is invoked by: ing the Encryption Array unprogrammed in effect by- 1. Pulling ALE low while the device is in reset and passes the encryption feature. PSEN is high; 2. Holding ALE low as RST is deactivated. PROGRAM LOCK BITS While the device is in ONCE mode, the Port 0 pins go Also included in the Program Lock scheme are three into a float state, and the other port pins, ALE, and Lock Bits which can be programmed to disable certain PSEN are weakly pulled high. The oscillator circuit functions as shown in Table 29. remains active. While the device is in this mode, an emulator or test CPU can be used to drive the circuit. To obtain maximum security of the on-board program and data, all 3 Lock Bits and the Encyption Array must Normal operation is restored after a valid reset is ap- be programmed. plied. Erasing the EPROM also erases the Encryption Array and the Lock Bits, returning the part to full functionali- 17.0 ON-CHIP OSCILLATOR ty. The on-chip oscillator for the CHMOS devices consists of a single stage linear inverter intended for use as a 6-52 87C51GB HARDWARE DESCRIPTION crystal-controlled, positive reactance oscillator. In this The crystal specifications and capacitance values (Cl application the crystal is operating in its fundamental and C2 in Figure 39) are not critical. 30 pF can be used response mode as an inductive reactance in parallel res- in these positions at any frequency with good quality onance with capacitance external to the crystal. Figure crystals. In general, crystals used with these devices 37 shows the on-chip oscillator circuitry. typically have the following specifications: The oscillator on the CHMOS devices can be turned off ESR (Equivalent Series Resistance) under software control by setting the PD bit in the CO (shunt capacitance) 7.0 pF maximum PCON register (Figure 38). The feedback resistor Rr shown in the figure consists of parallel n- and p-channel CL (load capacitance) 30pF ±3pF PETs controlled by the PD bit, such that Rr is opened Drive Level IMW when PD = 1. The diodes D 1 and D2, which act as clamps to Vee and Vss, are parasitic to the Rr PETs. TO INTERNAL Vee nMINGCKTS D1 4000 XTAL1 XTAL2 D2 r PD 270897-37 Figure 37. On-Chip Oscillator Circuitry Vec TO INTERNAL nMlNGCKTS Vss XTAL2------ 8OC51 ~--- QUARTZ CRYSTAL ORCERAMle RESONATOR 270897-38 Figure 38. Using the CHMOS On-Chip Oscillator 6-53 intel~ 87C51GB HARDWARE DESCRIPTION There are no requirements on the duty cycle of the external clock signal, since the input to the internal clocking circuitry is through a divid~-by-two flip-~op. 500 However, minimum and maximum high and low tImes specified in the data sheets must be observed. Refer to ~ 400 :t the External Clock Specifications for this information. ~ 300 It: An external oscillator may encounter as much as a ~ 200 100 pF load at XTALI when it starts up. This is due to interaction between the amplifier and its feedback ca- 100 pacitance. Once the external signal meets the VIL and VIH specifications, the capacitance will not exceed 20pF. 4 8 12 16 CRYSTAL FREQUENCY in WHz 270897-39 18.0 CPU TIMING Figure 39. ESR vs Frequency The internal clock generator defines the sequence of states that make up a machine cycle. A machine cycle Frequency, tolerance, and temperature range are deter- consists of 6 states, numbered S1 through S6. Each mined by the system requirements. state time lasts for two oscillator periods. Thus a ma- chine cycle takes 12 oscillator periods or 1 p.s if the A ceramic resonator can be used in place of the crystal oscillator frequency is 12 MHz. Each state is then di- in cost-sensitive applications. When a ceramic resona- vided into a Phase 1 and Phase 2 half. tor is used, C 1 and C2 are normally selected as higher values, typically 47 pF. The manufacturer ofthe cer~­ Rise and fall times are dependent on the external load- ic resonator should be consulted for recommendations ing that each pin must drive. They are approximately on the values of these capacitors. 10 ns, measured between O.8V and 2.0V. A more in-depth discussion of crystal specifications, ce- Propagation delays are different for different pins. For ramic resonators, and the selection of values for C 1 and a given pin they vary with pin loading, temperature, C2 can be found in Application Note AP-155, "Oscilla- Vee, and manufacturing lot. If the XTAL 1. waveform tors for Microcontrol1ers" in the Embedded Control is taken as the timing reference, propagatIon delays Applications handbook. . may vary from 25 ns to 125 ns. To drive the CHMOS parts with an external clock The AC Timings section of the data sheets do not refer- source, apply the external clock signal to XTAL 1 and ence any timing to the XTALl waveform. Rather, they leave XTAL2 floating. Refer to the External Clock relate the critical edges of control and input signals to Source diagram. This is an important difference from each other. The timings published in the data sheets the HMOS parts. With HMOS, the external clock include the effects of propagation delays under the source is applied to XTAL2, and XTAL 1 is grounded. specified test condition. See Figure 40. 8XC51GB XTAL2 External Oscillator X > - - - t XTAL1 Signal 270897-40 Figure 40. Driving the CHMOS Devices with an External Clock Sources 6-54 83C152 Hardware 7 Description 83C152 HARDWARE CONTENTS PAGE DESCRIPTION 1.0 INTRODUCTION .................................. 7-3 2.0 COMPARISON OF 80C152 AND 80C51 BH FEATURES ............................ 7-3 2.1 Memory Space ................................. 7-3 2.2 Interrupt Structure .......................... 7-11 2.3 Reset ............................................. 7-12 2.4 Ports 4,5 and 6 ............................. 7-13 2.5 Timers/Counters ............................ 7-13 2.6 Package ......................................... 7-13 2.7 Pin Description ............................... 7-14 2.8 Power Down and Idle ..................... 7-17 2.9 Local Serial ChanneL ..................... 7-17 3.0 GLOBAL SERIAL CHANNEL ............ 7-17 3.1 Introduction .................................... 7-17 3.2 CSMAlCD Operation ..................... 7-20 3.3 SDLC Operation ............................ 7-27 3.4 User Defined Protocols .................. 7-34 3.5 Using the GSC ............................... 7-34 3.6 GCS Operation .............................. 7-42 3.7 Register Descriptions ..................... 7-44 3.8 Serial Backplane vs Network Environment ..................................... 7-47 4.0 DMA OPERATION ............................. 7-47 4.1 DMA with the 80C152 .................... 7-47 4.2 Timing Diagrams ............................ 7-50 4.3 Hold/Hold Acknowledge ................. 7-50 4.4 DMA Arbitration ............................. 7-55 4.5 Summary of DMA Control Bits ....... 7-59 5.0 INTERRUPT STRUCTURE ................ 7-60 5.1 GSC Transmitter Error Conditions ........................................ 7-62 5.2 GSC Receiver Error Conditions ........................................ 7-63 6.0 GLOSSARy ........................................ 7-64 7-1 inteL 83C152 HARDWARE DESCRIPTION 1.0 INTRODUCTION use of external program memory. The second difference is that RESET is active low in the 83C152 and active The 83C152 Universal Communications Controller is high in the 80C51BH. This is very important to design- an 8-bit microcontroller designed for the intelligent ers who may currently be using the 80C5IBH and plan- management of peripheral systems or components. The ning to use the 83C152, or are planning on using both 83C152 is a derivative of the 80C51BH and retains the devices on the same board. The third difference is that same functionality. The 83C152 is fabricated on the GFO and GFl, general purpose flags in PCON, have same CHMOS III process as the 8OC51BH. What been renamed GFIEN and XRCLK. GFIEN enables makes the 83C152 different is that it has added func- idle flags to be generated in SDLC mode, and XRCLK tions and peripherals to the basic 80C51BH architec- enables the receiver to be externally clocked. All of the 1ure that are supported by new Special Function Regis- previously unused bits are now being used and inter- ters (SFRs). These enhancements include: a high speed rupt vectors have been added to support the new en- multi-protocol serial communication interface, two hancements. Programmers using old code generated for channels for DMA transfers, HOLD/HLDA bus con- the 8OC51 BH will have to examine their programs to trol, a fifth I/O port, expanded data memory, and ex- ensure that new bits are properly loaded, and that the panded program memory. new interrupt vectors will not interfere with their pro- gram. In addition to a standard UART, referred to here as Local Serial Channel (LSC), the 83C152 has an on- Throughout the rest ofthis manual the 8OC152 and the board multi-protocol communication controller called 83C152 will be referred to generically as the "CI52". the Global Serial Channel (GSC). The GSC interface supports SDLC, CSMA/CD, user defmable protocols, The C152 is based on the 8OC51BH architecture and and a subset of HDLC protocols. The GSC capabilities utilizes the same 8OC51BH instruction set. Figure 1.1 is include: address recognition, collision resolution, CRC a block diagram of the C152. Readers are urged to generation, flag generation, automatic retransmission, compare this block diagram with the 8OC51BH block and a hardware based acknowledge feature. This high diagram. There have been no new instructions added. speed serial channel is capable of implementing the All the new features and peripherals are supported by Data Link Layer and the Physical Link Layer as shown an extension of the Special Function Registers (SFRs). in the OSI open systems communication model. This Very little of the information pertaining specifically to model can be found in the document "Reference Model the 80C51BH core will be discussed in this chapter. for Open Systems Interconnection Architecture", The detailed information on such functions as: the in- ISO/TC97/SCI6 N309. struction set, port operation, timer/counters, etc., can be found in the MCS®-51 Architecture chapter in the The DMA circuitry consists of two 8-bit DMA chan- Intel Embedded Controller Handbook. Knowledge of nels with 16-bit addressability. The control signals; the 80C51BH is required to fully understand this man- Read (RD), Write (WR), hold and hold acknowledge ual and the operation ofthe C152. To gain a basic un- (HOLD/HLDA) are used to access external memory. derstanding on the operation of the 8OC51 BH, the The DMA channels are capable of addressing up to reader should familiarize himself with the entire MCS- 64K bytes (16 bits). The destination or source address 51 chapter of the Embedded Controller Handbook. can be automatically incremented. The lower 8 bits of the address are multiplexed on the data bus Port 0 and Another source of information that the reader may fmd the upper eight bits of address will be on Port 2. Data is helpful is Intel's LAN Components User's Manual, or- transmitted over an 8-bit address/data bus. Up to 64K der number 230814. Inside are descriptions of various bytes of data may be transmitted for each DMA activa- protocols, application examples, and application notes tion. dealing with different serial communication environ- ments. The new I/O port (P4) functions the same as Ports 1-3, found on the 8OC5IBH. 2.0 COMPARISON OF 80C152 AND Internal memory has been doubled in the 83C152. Data 80C51BH FEATURES memory has been expanded to 256 bytes, and internal program memory has been expanded to 8K bytes. 2.1 Memory Space There are also some specific differences between the 83C152 and the 80C51BH. The first is that the number- A good understanding of the memory space and how it ing system between the 83C152 and the 80C51BH is is used in the operation of MCS-51 products is essen- slightly different. The 83C152 and the 8OC51BH are tial. All the enhancements on the C152 are implement- factory masked ROM devices. The 8OC152 and the ed by accessing Special Function Registers (SFRs), 80C31BH are ROMless devices which require the added data memory, or added program memory. 7-3 _. €: ~Jl~~~tii~ti ~~"',~~~~~~~~~~~~~~ P••5 Pl.6 10 P••5 P•• 6 Pl.7 11 P•• 6 P•• 7 EBEN 12 P••7 N.C. Rfffi 13 P6.3 P3.0 1. EA EA P3.1 15 ALE ALE (TOP V£W) P3.2 16 PSEN PSEN P5.0 17 EPSEN N.C. 80C152JB 80C152JA/JC P3.3 18 P6.2 N.C. P3•• 80C152JD P6.7 83C152JA/JC N.C. P5.1 P6 .• N.C. N.C. P5.2 P5.7 N.C. P2.7 P5.3 N.C. P2.7 P2.6 P3.5 P3.5 P2.6 P3.6 P3.6 P2.5 P2•• P3.7 P2 •• P2.3 N.C. P2.3 270427-37 270427-6 Figure 2.58. PLCC Pin Out Figure 2.5C. PLCC Pin Out 2.7 Pin Description The pin description for the 80C51BH also applies to the C152 and is listed below. Changes have been made to the descriptions as they apply to the C152. PIN DESCRIPTION Pin # Description DIP PLCC(1) 48 2 Vcc-Supply voltage. 24 3,33(2) VSS-Circuit ground. 18-21, 27-30, Port o-Port 0 is an 8-bit open drain bi-directionall/O port. As an output port each 25-28 34-37 pin can sink 8 LS TTL inputs. Port 0 pins that have 1s written to them float, and in that state can be used as high-impedance inputs. Port 0 is also the multiplexed low-order address and data bus during accesses to external program memory if EBEN is pulled low. During accesses to external Data Memory, Port 0 always emits the low-order address byte and serves as the multiplexed data bus. In these applications it uses strong internal pull ups when emitting 1s. Port 0 also outputs the code bytes during program verification. External pullups are required during program verification. NOTES: 1. N.C. pins on PLCC package may be connected to internal die and should not be used in customer applications. 2. It is recommended that both Pin 3 and Pin 33 be grounded for PLCC devices. 7-14 83C152 HARDWARE DESCRIPTION PIN DESCRIPTION (Continued) Pin # Description DIP PLCC(1) 1-8 4-11 Port 1-Port 1 is an 8-bit bidirectional 1/0 port with internal pullups. Port 1 pins that have 1s written to them are pulled high by the internal pull ups, and in that state can be used as inputs. As inputs, Port 1 pins that are externally being pulled low will source current (IlL, on the data sheet) because of the internal pullups. Port 1 also serves the functions of various special features of the 8XC152, as listed below: Pin Name Alternate Function P1.0 GRXD GSC data input pin P1.1 GTXD GSC data output pin P1.2 DEN GSC enable signal for an external driver P1.3 TXC GSC input pin for external transmit clock P1.4 RXC GSC input pin for external receive clock P1.5 HLD DMA hold input! output P1.6 HLDA DMA hold acknowledge input! output 29-36 41-48 Port 2-Port 2 is an 8-bit bi-directionall/O port with internal pullups. Port 2 pins that have 1s written to them are pulled high by the internal pullups, and in that state can be used as inputs. As inputs, Port 2 pins that are externally being pulled low will source current (IlL, on the data sheet) because of the internal pullups. Port 2 emits the high-order address byte during fetches from external Program Memory if EBEN is pulled low. During accesses to external Data Memory that use 16-bit addresses (MOVX @ DPTR and DMA operations), Port 2 emits the high-order address byte. In these applications it uses strong internal pullups when emitting 1s. During accesses to external Data Memory that use 8-bit addresses (MOVX @ Ri), Port 2 emits the contents of the P2 Special Function Register. Port 2 also receives the high-order address bits during program verification. 10-17 14-16, Port 3-Port 3 is an 8-bit bi-directionall/O port with internal pullups. Port 3 pins that 18,19, have 1s written to them are pulled high by the internal pullups, and in that state can 23-25 be used as inputs. As inputs, Port 3 pins that are externally being pulled low will source current (IlL, on the data sheet) because of the pullups. Port 3 also serves the functions of various special features of the MCS-51 Family, as listed below: Pin Name Alternate Function P3.0 RXD Serial input line P3.1 TXD Serial output line P3.2 INTO External interrupt 0 P3.3 INT1 External interrupt 1 P3.4 TO Timer 0 external input P3.5 T1 Timer 1 external input P3.6 WR External Data Memory Write strobe P3.7 RD External Data Memory Read strobe 47-40 65-58 Port 4-Port 4 is an 8-bit bi-directionalliO port with internal pull ups. Port 4 pins that have 1s written to them are pulled high by the internal pullups, and in that state can be used as inputs. As inputs, Port 4 pins that are externally being pulled low will source current (IlL, on the data sheet) because of the internal pullups. In addition, Port 4 also receives the low-order address bytes during program verification. NOTES: 1. N.C. pins on PLCC package may be connected to internal die and should not be used in customer applications. 2. It is recommended that both Pin 3 and Pin 33 be grounded for PLCC devices. 7-15 intel~ 83C152 HARDWARE DESCRIPTION PIN DESCRIPTION , (Continued) j Pin # Description DIP PLCC(1} 9 13 RST-Reset input. A logic low on this pin for three machine cycles while the oscillator is running resets the device. An internal pullup resistor permits a power- on reset to be generated using only an external capacitor to Vss. Although the GSC recognizes the reset after three machine cycles, data may continue to be transmitted for up to 4 machine cycles after Reset is first applied. 38 55 ALE-Address Latch Enable output signal for latching the low byte of the address during accesses to external memory. In normal operation ALE is emitted at a constant rate of Ys the oscillator frequency, and may be used for external timing or clocking purposes. Note, however, that one ALE pulse is skipped during each access to external Data Memory. While in Reset, ALE remains at a constant high level. 37 54 PSEN-Program Store Enable is the Read strobe to External Program Memory. When the 8XC152 is executing from external program memory, PSEN is active (low). When the device is executing code from External Program Memory, PSEN is activated twice each machine cycle, except that two PSEN activations are skipped during each access to External Data Memory. While in Reset, PSEN remains at a constant high level. 39 56 EA-External Access enable. EA must be externally pulled low in order to enable the 8XC152 to fetch code from External Program Memory locations OOOOH to OFFFH. EA must be connected to VCC for internal program execution. 23 32 XTAL 1-lnput to the inverting oscillator amplifier and input to the internal clock generating circuits. 22 31 XTAL2-Output from the oscillator amplifier. N/A 17,20 Port 5-Port 5 is an 8-bit bi-directionall/O port with internal pullups. Port 5 pins 21,22 that have 1s written to them are pulled high by the internal pullups, and in that state 38,39 can be used as inputs. As inputs, Port 5 pins that are externally being pulled low will 40,49 source current (IlL. on the data sheet) because of the internal puliuPs. Port 5 is also the multiplexed low-order address and data bus during accesses to external program memory if EBEN is pulled high. In this application it uses strong pullups when emitting 1s. N/A 67,66 Port 6-Port 6 is an 8-bit bi-directionalllO port with internal pull ups. Port 6 pins 52,57 that have 1s written to them are pulled high by the internal pullups, and in that state 50,68 can be used as inputs. As inputs, Port 6 pins that are externally pulled low will 1,51 source current (IlL, on the data sheet) because of· the internal pullups. Port 6 emits the high-order address byte during fetches from external Program Memory if EBEN is pulled high. In this application it uses strong pullups when emitting 1s. N/A 12 EBEN-E-Bus Enable input that designates whether program memory fetches take place via Ports 0 and 2 or Ports 5 and 6. Table 2.1 shows how the ports are used in conjunction with EBEN. 53 EPSEN-E-bus Program Store Enable is the Read strobe to external program memory when EBEN is high. Table 2.1 shows when EPSEN is used relative to PSEN depending on the status of EBEN and EA. NOTES: 1. N.C. pins on PLCC package may be connected to internal die and should not be used in customer applications. 2. It is recommended that both Pin 3 and Pin 33 be grounded for PLCC devices. 7-16 int'et 83C152 HARDWARE DESCRIPTION 2.8 Power Down and Idle 2.9 Local Serial Channel Both of these operations function identically as in the The Local Serial Channel (LSC) is the name given to 80C5IBH. Application Note 252, "Designing with the the UART that exists on all MeS-51 devices. The 80C51BH" gives an excellent explanation on the use of LSC's function and operation is exactly the same as on the reduced power consumption modes. Some of the the 8OC51BH. For a description on the use of the LSC, items not covered in AP-252 are the considerations that refer to the 8051152 Hardware Description Chapter in are applicable when using the GSC or DMA in con- the Intel Embedded Controller Handbook, under Serial junction with the power saving modes. Interface. The GSC continues to operate in Idle as long as the interrupts are enabled. The interrupts need to be en- 3.0 GLOBAL SERIAL CHANNEL abled, so that the CPU can service the FIFO's. In order to properly terminate a reception or transmission the C152 must not be in idle when the EOF is transmitted 3.1 Introduction or received. After servicing the GSC, user software will need to again invoke the Idle command as the CPU The Global Serial Channel (GSC) is a multi-protocol, does not automatically re-enter the Idle mode after high performance serial interface targeted for data rates servicing the interrupts. up to 2 MBPS with on-chip clock recovery, and 2.4 MBPS using the external clock options. In applications The GSC does not operate while in Power Down so the using the serial channel, the GSC implements the Data steps required prior to entering Power Down become Link Layer and Physical Link Layer as described in the more complicated. The sequence when entering Power ISO reference model for open systems interconnection. Down and the status of the I/O is of major importance in preventing damage to the C152 or other components The GSC is designed to meet the requirements of a in the system. Since the only way to exit Power Down wide range of serial communications applications and is is with a Reset, several problem areas become very sig- optimized to implement Carrier-Sense Multi-Access nificant. Some of the problems that merit careful con- with Collision Detection (CSMA/CD) and Synchro- sideration are cases where the Power Down occurs dur- nous Data Link Control (SDLC) protocols. The GSC ing the middle of a transmission, and the possibility architecture is also designed to provide flexibility in de- that other stations are not or cannot enter this same fining non-standard protocols. This provides the ability mode. The state of the GSC I/O pins becomes critical to retrofit new products into older serial technologies, and the GSC status will need to be saved before power as well as the development of proprietary interconnect down is entered. There will also need to be some meth- schemes for serial backplane environments. od of identifying to the CPU that the following Reset is probably not a cold start and that other stations on the The versatility of the GSC is demonstrated by the wide link may have already been initialized. range of choices available to the user. The various modes of operation are summarized in Table 3.1. In The DMA circuitry stops operation in both Idle and subsequent sections, each available choice of operation Power Down modes. Since operation is stopped in both will be explained in detail. modes, the process should be similar in each case. Spe- cific steps that need to be taken include: notification to In using Table 3.1, the parameters listed vertically (on other devices that DMA operation is about to cease for the left hand side) represent an option that is selected a particular station or network, proper withdrawal (X). The parameters listed horizontally (along the top from DMA operation, and saving the status of the of the table) are all the parameters that could theoreti- DMA channels. Again, the status of the I/O pins dur- cally be selected (Y). The symbol at the junction of ing Power Down needs careful consideration to avoid both X and Y determines the applicability of the option damage to the C152 or other components. Y. Port 4 returns to its input state, which is high level Note, that not all combinations are backwards compati- using weak pullup devices. ble. For example, Manchester encoding requires half duplex, but half duplex does not require Manchester encoding. 7-17 83C152 HARDWARE DESCRIPTION Table 3.1 _.,...._--- IAVAII AAI c= OPTIONS --+ DATA DU- ACKNOW- ...__ .. RECOG- _-- PRE- ENCODING FLAGS CRC PLEX LEDGE NITION BACKOFF AMBLE M N N 0 1 N 1 3 H F N H U N 8 1 N A D N 8 A R R 1 1 0 6 2 A U 0 A S 0 B 6 0 L E 0 B N = NOT AVAILABLE N Z Z 1 I N B B L L N R E N I B R T T N I M= MANDATORY C I 1 I E I I F L E D R E T I M E E E T 0= OPTIONAL H 1 D T T W D I T A R R P= NORMALLY PREFERRED E 1 L C A A E A L N M X=N/A S 1 E C U R F L A I T 0 I T E I L T N E T 0 N E I R E S D T SELECTED I J, FUNCTION C DATA ENCODING: MANCHESTER(CSMAlCD) X N N 1 P 1 0 0 M N 0 0 0 0 0 0 0 0 0 N 0 NRZI (SDLC) N x N P 1 1 0 0 0 0 0 N P 0 0 0 N N N 0 0 NRZ (EXT CLK) N N X 0 0 1 0 0 0 0 0 N 0 0 0 0 0 0 0 0 0 FLAGS:01111110 (SDLC) N P 0 X 1 1 0 0 0 0 0 N P 0 0 0 N N N 0 0 11/IDLE P N 0 1 X 1 0 0 0 N 0 0 0 0 0 0 0 0 0 1 0 CRC:NONE 1 1 1 1 1 X N N 1 N 1 1 1 1 1 1 N N N 1 1 16·BITCCITI 0 0 0 0 0 N X N 0 0 0 0 0 0 0 0 0 0 0 0 0 32·BIT AUTODIN II 0 0 0 0 0 N N X 0 N 0 0 0 0 0 0 0 0 0 0 0 DUPLEX:HALF 0 0 0 0 0 1 0 0 X N 0 0 0 0 0 0 0 0 0 0 0 FULL N 0 0 M N N M N N X 0 N P 0 0 0 N N N 0 0 ACKNOWLEDGEMENT:NONE 0 0 0 0 0 1 0 0 0 0 X N N 0 0 0 0 0 0 0 0 HARDWARE 0 N N N 0 1 0 0 0 N N X N 0 0 0 N 0 0 N 0 USER DEFINED 0 P 0 0 0 1 0 0 0 P N N X 0 0 0 0 0 0 0 0 ADDRESS RECOGNITION: NONE/ALL 0 0 0 0 0 1 0 0 0 0 0 0 0 X N N 0 0 0 0 0 8·BIT 0 0 0 0 0 1 0 0 0 0 0 0 0 N X N 0 0 0 0 0 16-BIT 0 0 0 0 0 1 0 0 0 0 0 0 0 N N X 0 0 0 0 0 COLLISION RESOLUTION: NORMAL 0 N 0 N 0 N 0 0 M N 0 N 0 0 0 0 X N N N 0 ALTERNATE 0 N 0 N 0 N 0 0 M N 0 0 0 0 0 0 N X N N 0 DETERMINISTIC 0 N 0 N 0 N 0 0 M N 0 0 0 0 0 0 N N X N 0 PREAMBLE:NONE N 0 0 0 1 1 0 0 0 0 0 N 0 0 0 0 N N N X N 8·BIT 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 N X 32·BIT 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 N N 64·BIT 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 N N JAM:D.C. M N N N 0 N 0 0 M N 0 0 0 0 0 0 0 0 0 N 0 CRC M N N N 0 N 0 0 M N 0 0 0 0 0 0 0 0 0 N 0 CLOCKING:EXTERNAL N M N 0 0 N 0 0 0 0 0 N 0 0 0 0 N N N 0 0 INTERNAL 0 0 N 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 CONTROL: CPU 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 DMA 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 RAW RECEIVE: 1 1 1 1 1 1 1 1 1 N 1 1 1 1 1 1 0 0 0 1 1 RAW TRANSMIT: 1 1 1 1 1 1 1 1 1 N 1 1 1 1 1 1 N N N 1 1 CSMAlCD: 0 N N 1 P 1 0 0 M N 0 0 0 0 0 0 0 0 0 N 0 SDLC: N 0 0 P 1 1 0 0 0 0 0 N 0 0 0 0 N N N P 0 7-18 intel~ 83C152 HARDWARE DESCRIPTION Table 3.1 (Continued) AVAILABLE ~ PRE- OPTIONS AMBLE JAM CLOCK CONTROL 3 6 D C E I C D R R C S 2 4 C R X N P M A A S D N=NOT AVAILABLE B B C T T U A W W M L M = MANDATORY I I I E E R T A C 0= OPTIONAL T T R R E R I P = NORMALLY PREFERRED N N C A C X=N/A A A E N D L L I S V M SELECTED E I J, FUNCTION T DATA ENCODING: MANCHESTER 0 0 0 0 N M 0 0 0 0 M N NRZI 0 0 N N N M 0 0 0 0 N M NRZ 0 0 0 0 M N 0 0 0 0 0 0 FLAGS:0111111Q 0 0 N N 0 0 0 0 0 1 1 P 11/1DLE 0 0 0 0 0 0 0 0 0 1 P 1 CRC:NONE 1 1 N N 1 1 1 1 1 1 1 1 16-BIT CCITT 0 0 0 0 0 0 0 0 1 1 0 0 32-BIT AUTODIN II 0 0 0 0 0 0 0 0 1 1 0 0 DUPLEX:HALF 0 0 0 0 0 0 0 0 0 0 0 0 FULL 0 0 N N 0 0 0 0 N N N P ACKNOWLEDGEMENT:NONE 0 0 0 0 0 0 0 0 0 0 0 0 HARDWARE 0 0 0 0 N 0 0 0 N N 0 N USER DEFINED 0 0 0 0 0 0 0 0 0 0 0 1 ADDRESS RECOGNITION: NONE 0 0 0 0 0 0 0 0 0 0 0 0 8-BIT 0 0 0 0 0 0 0 0 1 1 0 0 16-BIT 0 0 0 0 0 0 0 0 1 1 0 0 COLLISION RESOLUTION: NORMAL 0 0 0 0 N 0 0 0 0 N M N ALTERNATE 0 0 0 0 N 0 0 0 0 N M N DETERMINISTIC 0 0 0 0 N 0 0 0 0 N M N PREAMBLE:NONE N N N N 0 0 0 0 0 0 N P 8-BIT N N 0 0 0 0 0 0 1 1 0 0 32-BIT X N 0 0 0 0 0 0 1 1 0 0 64-BIT N X 0 0 0 0 0 0 1 1 0 0 JAM:D.C. 0 0 X N N 0 0 0 0 N M N CRC 0 0 N X N 0 0 0 0 N M N CLOCKING:EXTERNAL 0 0 N N X N 0 0 0 0 N 0 INTERNAL 0 0 0 0 N X 0 0 0 0 0 0 CONTROL:CPU 0 0 0 0 0 0 X N 0 0 0 0 DMA 0 0 0 0 0 0 N X 0 0 0 0 RAW RECEIVE: 1 1 0 0 1 1 1 1 X N 1 1 RAW TRANSMIT: 1 1 N N 1 1 1 1 N X 1 1 CSMAlCD: 0 0 0 0 N 0 0 0 0 0 X N SOLe: 0 0 N N 0 0 0 0 0 0 N X 7-19 83C152 HARDWARE DESCRIPTION Note 1: Programmable in Raw transmit or receive resolve the contention. There are three different modes mode. of collision resolution made available to the user on the C152. Re-transmission is attempted when a resolution Almost all the options available from Table 3.1 can be algorithm indicates that a station's opportunity has ar- implemented with the proper software to perform the rived. functions that are necessary for the options selected. In Table 3.1, a judgment has been made by the authors on Normally, in CSMA/CD, re-transmission slot assign- which options are practical and which are not. What ments are intended to be random. This method gives all this means is that in Table 3.1, an "N" should be inter- stations an equal opportunity to utilize the serial com- preted as meaning that the option is either not practical munication link but also leaves the possibility of anoth- when implemented with user software or that it cannot er collision due to two stations having the same slot be done. An "0" is used when that function is one of assignment. There is an option on the C152 which al- several that can be implemented with the GSC without lows all the stations to have their slot assignments pre- additional user software. viously determined by user software. This pre-assign- ment of slots is called the deterministic resolution The GSC is targeted to operate at bit rates up to 2.4 mode. This method allows resolution after the first col- MBps using the external clock options and up to 2 lision and ensures the access of the link to each station MBps using the internal baud rate generator, internal during the resolution. Deterministic resolution can be data formatting and on-chip clock recovery. The baud advantageous when the link is being heavily used and rate generator allows most standard rates to be collisions are frequently occurring and in real time ap- achieved. These standards include the proposed plications where determinism is required. Deterministic IEEE802.3 LAN standard (1.0MBps) and the Tl stan- resolution may also be desirable if it is known before- dard (1.544MBps). The baud rate is derived from the hand that a certain station's communication needs to be crystal frequency. This makes crystal selection impor- prioritized over those of other stations if it is involved tant when determining the frequency and accuracy of in a collision. the baud rate. The user needs to be aware that after reset, the GSC is 3.2.2 CSMA/CD FRAME FORMAT in CSMA/CD mode, IFS = 256 bit times, and a bit The frame format in CSMA/CD consists of a pream- time equals 8 oscillator periods. The GSC will remain ble, Beginning of Frame flag (BOp), address field, in- in this mode until the interframe space expires. If the formation field, CRC, and End of Frame flag (EOP) as user changes to SDLC mode or the parameters used in shown in Figure 3.1. CSMAlCD, these changes will not take effect until the interframe space expires. A requirement for the inter- frame space timer to begin is that the receiver be in an IPREAMBLE IBOF IADDRESS IINFO ICRC IEOF I idle state. This makes it possible for the GSC to be in Figure 3.1 Typical CSMA/CD Frame some other mode than the user intends for a significant amount of time after reset. To prevent unwanted GSC PREAMBLE - The preamble is a series of alternating errors from occurring, the user should not enable the 1s and Os. The length of the preamble is programmable GSC or the GSC interrupts for 170 machine cycles to be 0, 8, 32, or 64 bits. The purpose of the preamble is «256 X 8)/12) after LNI bit is set. to allow all the receivers to synchronize to the same clock edges and identifies to the other stations on-line that there is activity indicating the link is being used. 3.2 CSMA/CD Operation For these reasons zero preamble length is not compati- ble with standard CSMA/CD, protocols. When using 3.2.1 CSMA/CD OVERVIEW CSMA/CD, the BOF is considered part of the pream- CSMA/CD operates by sensing the transmission line ble compared to SDLC, where the BOF is not part of for a carrier, which indicates link activity. At the end of the preamble. This means that if zero preamble length link activity, a station must wait a period of time, called were to be used in CSMA/CD mode, no BOF would be the deference period, before transmission may begin. generated. It is strongly recommended that zero pream- The deference period is also known as the interframe ble length never be used in CSMAlCD mode. If the space. The interframe space is explained in Section preamble contains two consecutive Os, the preamble is 3.2.3. considered invalid. If the C152 detects an invalid pre- amble, the frame is ignored. With this type of operation, there is always the possibil- ity of a collision occurring after the deference period BOF - In CSMAlCD the Beginning-Of-Frame is a part due to line delays. If a collision is detected after trans- of the preamble and consists of two sequential Is. The mission is started, a jamming mechanism is used to en- purpose of the BOF is to identify the end of the pream- sure that all stations monitoring the line are aware of ble and indicate to the receiver(s) that the address will the collision. A resolution algorithm is then executed to immediately follow. 7-20 intelqp 83C152 HARDWARE DESCRIPTION ADDRESS - The address field is used to identify which algorithm can be used but IEEE 802.3 uses a 32-bit messages are intended for which stations. The user CRC. The generation polynomial the C152 uses with must assign addresses to each destination and source. the 32-bit CRC is: How the addresses are assigned, how they are main- G(X) = X32 + X26 + X23 + X22 + X16 + X12 + tained, and how each transmitter is made aware of XU + XlO + X8 + X7 + X5 + X4 + X2 which addresses are available is an issue that is left to +X+1 the user. Some suggestions are discussed in Section 3.5.5. Generally, each address is unique to each station The CRC generator, as shown in Figure 3.2, operates but there are special cases where this is not true. In by taking each bit as it is received and XOR'ing it with these special cases, a message is intended for more than bit 31 of the current CRC. This result is then placed in one station. These multi-targeted messages are called temporary storage. The result of XOR'ing bit 31 with broadcast or multicast-group addresses. A broadcast the received bit is then XOR'd with bits 0, 1,3,4,6, 7, address consisting of all Is will always be received by 9, 10, 11, 15,21,22,25 as the CRC is shifted right one all stations. A multicast-group address usually is indi- position. When the CRC is shifted right, the temporary cated by using a las the first address bit. The user can storage space holding the result of XOR'ing bit 31 and choose to mask off all or selective bits of the address so the incoming bit is shifted into position O. The whole that the GSC receives all messages or multicast-group process is then repeated with the next incoming or out- messages. The address length is programmable to be 8 going bit. or 16 bits. An address consisting of allIs will always be received by the GSC on the C152. The address bits are The user has no access to the CRC generator or the bits always passed from the GSC to the CPU. With user which constitute the CRC while in CSMA/CD. On software, the address can be extended beyond 16 bits, transmission, the CRC is automatically appended to but the automatic address recognition will only work the data being sent, and on reception, the CRC bits are on a maximum of 16 bits. User software will have to not normally loaded into the receive FIFO. Instead, resolve any remaining address bits. they are automatically stripped. The only indication the user has for the status of the CRC is a pass/fail flag. INFO - This is the information field and contains the The pass/fail flag only operates during reception. A data that one device on the link wishes to transmit to CRC is considered as passing when the the CRC gener- another device. It can be of any length the user wishes ator has 11000111 00000100 11011010 01111011B as a but needs to be in multiples of 8 bits. This is because remainder after all of the data, including the CRC multiples of 8 bits are used to transfer data into or out checksum, from the transmitting station has been cy- of the GSC FIFOs. The information field is delineated cled through the CRC generator. The preamble, BOF from the rest of the components of the frame by the and EOF are not included as part of the CRC algo- preceding address field and the following CRC. The rithm. An interrupt is available that will interrupt the receiver determines the position of the end of the infor- CPU if the CRC of the receiver is invalid. The user can mation field by passing the bytes through a temporary enable the CRC to be passed to the CPU by placing the storage space. When the EOF is received the bytes in receiver in the raw receive mode. temporary storage are the CRC, and the last bit re- ceived previous to the CRC constitute the end of the This method of calculating the CRC is compatible with information field. IEEE 802.3. CRC - The Cyclic Redundancy Check (CRC) is an er- EOF - The End Of Frame indicates when the transmis- ror checking algorithm commonly used in serial com- sion is completed. The end flag in CSMAlCD consists munications. The C152 offers two types of CRC algo- of an idle condition. An idle condition is assumed when rithms, a 16-bit and a 32-bit. The 16-bit algorithm is there is no transitions and the link remains high for 2 or normally used in the SDLC mode and will be described more bit times. in the SDLC section. In CSMA/CD applications either 7-21 inteL 83C152 HARDWARE DESCRIPTION 270427-8 Figure 3.2. CRC Generator 3.2.3 INTERFRAME SPACE In most applications, the period of the interframe space will be equal to or greater than the amount of time The interframe space is the amount of time that trans- needed to tum-around the received frame. The turn- mission is delayed after the link is sensed as being idle around period is the amount of time that is needed by and is used to separate transmitted frames. In alternate user software to complete the handling of a received backoff mode, the interframe space may also be includ- frame and be prepared to receive the next frame. An ed in the determination of when retransmissions may interframe space smaller than the required tum-around actually begin. The C152 allows programmable inter- period could be used, but would allow some frames to frame spaces of even numbers of bit times from 2 to be missed. 256. The hardware enforces the interframe space in SDLC mode as well as in CSMAlCD mode. When a GSC transmitter has a new message to send, it will first sense the link. If activity is detected, transmis- The period of the interframe space is determined by the sion will be deferred to allow the frame in progress to contents of IFS. IFS is an SFR that is programmable complete. When link activity ceases, the station contin- from 0 to 254. The interframe space is measured in bit ues deferring for one interframe space period. times. The value in IPS multiplied by the bit time equals the interframe space unless IFS equals O. If IFS As mentioned earlier, the interframe space is used dur- does equal 0, then the interframe space will equal 256 ing the collision resolution period as well as during nor- bit times. One of the considerations when loading the mal transmission. The backoff method selected affects IFS is that only even numbers (LSB must be 0) can be how the deference period is handled during normal used because only the 7 most significant bits are loaded transmission. If normal backoff mode is selected, the into IFS. The LSB is controlled by the GSC and deter- interframe space timer is reset if activity occurs during mines which half of the IFS is currently being used. In approximately the first half of the interframe space. If some modes, the interframe space timer is re-triggered alternate backoff or deterministic backoff is selected, if activity is detected during the first half of the period. the timer is not reset. In all cases when the interframe The GSC determines which half of the interframe space space timer expires, transmission may begin, regardless is currently being used by examining the LSB. A one if there is activity on the link or not. Although the indicates the first half and zero indicates the second C152 resets the interframe space timer if activity is de- half of the IFS. tected during the first one-half of the interframe space, this is not necessarily true of all CSMAlCD systems. After reset IFS is 0, which delays the first transmission (IEEE 802.3 recommends that the interframe space be for both SDLC and CSMAlCD by 256 bit times (after reset if activity is detected during the first two-thirds or reset, a bit time equals 8 oscillator clock periods). less of the interframe space.) 7-22 intet 83C152 HARDWARE DESCRIPTION 3.2.4 CSMA/CD DATA ENCODING Narrow Pulses Manchester encoding/decoding is automatically select- A valid Manchester waveform must stay high or low ed when the user software selects CSMA/CD transmis- for at least a half bit-time, nominally 4 sample-times. sion mode (See Figure 3.3). In Manchester encoding Jitter tolerance allows a waveform which stays high or the value of the bit is determined by the transition in low for 3 sample-times to also be considered valid. A the middle of the bit time, a positive transition is decod- sample sequence which shows a second transition only ed as a 1 and a negative transition is decoded as a O. lor 2 sample-times after the previous transition is con- The Address and Info bytes are transmitted LSB first. sidered to be the result of a collision. Thus, sample The CRC is transmitted MSB first. sequences such as 0000110000 and 111101111 are inter- preted as collisions. If the external IX clock feature is chosen the transmis- sion mode is always NRZ (see Section 3.5.11). Using The GSC hardware recognizes the collision to have oc- CSMA/CD with the external clock option is not sup- curred within 3/8 to 1/2 bit-time following the second ported because the data needs reformatting from NRZ transition. to Manchester for the receiver to be able to detect code violations and collisions. Missing o-to-1 Transition 3.2.5 COLLISION DETECTION A O-to-l transition is expected to occur at the center of any bit cell that begins with O. If the previous I-to-O The GSC hardware detects collisions by detecting Man- transition occurred at the bit cell edge, a jitter tolerance chester waveform violations at its GRXD pin. Three of ± 1 sample is allowed. Sample sequences such as kinds of waveform violations are detected: a missing 1111 :00001111 and 1111:00000 1111 are valid, where 0-t0-1 transition where one was expected, a 1-to-O tran- ":" indicates a bit cell edge. Sequences of the form sition where none was expected, and a waveform that 1111 :OOOOOOXXX are interpreted as collisions. stays low (or high) for too short a time. For these kinds of sequences, the GSC recognizes the collision to have occurred within 1 to 1 1/8 bit-times Jitter Tolerance after the previous 1-to-0 transition. A valid Manchester waveform must have a transition at the midpoint of any bit cell, and may have a transition If the previous 1-to-0 transition occurred at the center at the edge of any bit cell. Therefore, transitions will of the previous bit cell, a jitter tolerance of ± 2 samples nominally be separated by either 1/2 bit-time or 1 bit- is allowed. Thus, sample sequences such as time. 1111 0000:0000 1111 and 111100000:00000 1111 are val- id. Sequences of the form l11100000:000000XXX are The GSC samples the GRXD pin at the rate of 8 x the interpreted as collisions. bit rate. The sequence of samples for the received bit sequence 00 1 would nominally be: For these kinds of sequences, the GSC recognizes the collision to have occurred within 1 5/8 to 1 3/4 bit- samples: 1 1 1 1 0 0 0 0 : 1 1 1 1 0 0 0 0 : 0 0 0 0 1 1 1 1 : times after the previous I-to-O transition. bit value: 0 0 1 : <-bitcell->: <-bit cell->: <-bitcell->: Unexpected 1-to-0 Transition The sampling system allows a jitter tolerance of ± 1 If the line is at a logic 1 during the first half of a bit cell, sample for transitions that are 1/2 bit-time apart, and then it is expected to make a 1-to-O transition at the ± 2 samples for transitions that are 1 bit-time apart. midpoint of the bit cell. If the transition is missed, it is assumed that this bit cell is the first half of an EOF flag o o o BIT ' ;-TIME- 270427-14 Figure 3.3. Manchester Encoding 7-23 intel~ 83C152 HARDWARE DESCRIPTION (line idle for two bit-times). One bit-time later (which (GREN = 0), and the Receive Error Interrupt flag marks the midpoint of the next bit cell), if there is still RCABT is set. If DCR has been selected, the GSC no I-to-O transition, a valid EOF is aSsumed and the participates in the resolution algorithm. line idle bit (LNI in TSTAT) gets set. Incoming bits take 112 bit time to get from the GRXD However, if the assumed EOF flag is interrupted by a pin to the bit decoder. The bit decoder strips off the I-to-O transition in the bit-time following the first miss- preamble/BOF bits, and the first bit after BOF is shift- ing transition, a collision is assumed. In that case the ed into a serial strip buffer. The length of the strip GSC hardware recognizes the collision to have oc- buffer is equal to the number of bits in the selected curred within 1/2 to 5/8 bit-time after the unexpected CRC. It is within this buffer that address recognition transition. takes place. If the address is recognized as one for which reception should proceed, then when the first address bit exits the strip buffer it is shifted into an 8-bit 3.2.6 RESOLUTION OF COLLISIONS shift register. When the shift register is full, its content How the GSC responds to a detected collision depends is transferred to RFIFO. That is the event that deter- on what it was doing at the time the collision was de- mines whether a collision sets RCABT or not. tected. What it might be doing is either transmitting or receiving a frame, or it might be inactive. GSC Transmitting If the GSC is in the process of transmitting a frame at GSC Inactive the time the collision is detected, it will in every case The collision is detected whether the GSC is active or execute its jamlbackoff procedure. Its reponse beyond not. If the GSC is neither transmitting nor receiving at that depends on whether the first byte of the frame has the time the collision is detected, it takes no action un- been transferred from TFIFO to the output shift regis- less user software has selected the Deterministic Colli- ter yet or not. That transfer takes place at the beginning sion Resolution (OCR) algorithm. If DCR has been of the first bit of the BOF; that is, 2 bit-times before the selected, the GSC will participate in the resolution al- end of the preamblelBOF sequence. gorithm. If the transfer from TFIFO hasn't occurred yet, the GSC hardware will try again to gain access to the line GSC Receiving after its backoff time has expired. Up to 8 automatic restarts can be attempted. If the 8th restart is interrupt- If the GSC is already in the process of receiving a frame ed by yet another collision, the transmitter is disabled at the time the collision is detected, its response de- (TEN = 0) and the Transmit Error Interrupt flag pends on whether the first byte of the frame has been TCDT is set. transferred into RFIFO yet or not. If that hasn't oc- curred, the GSC simply aborts the reception, but takes If the transfer from TFIFO occurs before a collision is no other action unless DCR has been selected. If DCR detected, the transmitter is disabled (TEN = 0) and has been selected, the GSC participates in the resolu- the TCDT flag is set. tion algorithm. The response of the GSC to detected collisions is sum- If the reception has already progressed to the point marized in Figure 3.4. where a byte has been transferred to RFIFO by the time the collision is detected, the receiver is disabled What the GSC was doing Response nothing None, unless DCR = 1. If DCR = 1, begin DCR countdown. Receiving a Frame, first None, unless DCR = 1. byte not in RFIFO yet. If DCR = 1, begin DCR countdown. Receiving a Frame, first Set RCABT, clear GREN. byte already in RFIFO. If DCR = 1, begin DCR countdown. Transmitting a Frame, first Execute jam/backoff. byte still in TFIFO Restart if collision count :::;; 8. Transmitting a Frame, first Execute jam/backoff. byte already taken from TFIFO Set TCDT, clear TEN. Figure 3-4. Response to a Detected Collision. References to OCR and the OCR Countdown Have to Do with the Deterministic Collision Resolution Algorithm. 7-24 intaL 83C152 HARDWARE DESCRIPTION Jam In the Deterministic algorithm, the GSC backs off to await its pre-determined tum. The jam signal is generated by any 8XC152 that is in- volved in transmitting a frame at the time a collision is detected at its GRXD pin. This is to ensure that if one Random Backoff transmitting station detects a collision, all the other sta- In either of the random algorithms, the first thing that tions on the network will also detect a collision. happens after a collision is detected is that a 1 gets shifted into the TCDCNT (Transmit Collision Detect If a transmitting 8XC152 detects a collision during the Count) register, from the right. preamblelBOF part of the frame that it is trying to transmit, it will complete the preamble/BOF and then Thus if the software cleared TCDCNT before telling begin the jam signal in the first bit time after BOF. If the GSC to transmit, then TCDCNT keeps track of the collision is detected later in the frame, the jam sig- how many times the transmission had to be aborted nal will begin in the next bit time after the collision was because of collisions: detected. TCDCNT = OOOOOOOO first attempt The jam signal lasts for the same number of bit times as 00000001 first collision the selected CRC length~ither 16- or 32-bit times. 00000011 second collision The 8XC152 provides two types of jam signals that can 00000111 third collision be selected by user software. If the node is DC-coupled 00001111 fourth collision to the network, the DC jam can be selected. In this case the GTXD pin is pulled to a logic 0 for the duration of the jam. If the node is AC-coupled to the network, then 11111111 eighth collision AC jam must be selected. In this case the GSC takes the CRC it has calculated thus far in the transmission, After TCDCNT gets a 1 shifted into it, the logical inverts each bit, and transmits the inverted CRC. The AND of TCDCNT and PRBS is loaded into a count- selection of DC or AC jam is made by setting or clear- down timer named BKOFF. PRBS is the name of an ing the DCJ bit, which resides in the SFR named SFR which contains the output of a pseudo-random MYSLOT. binary sequence generator. Its function is to provide a random number for use in the backoff algorithm. When the jam signal is completed, the 8XC152 goes into an idle state. Presumeably, other stations on the Thus on the first collision BKOFF gets loaded random- network are also generating their own jam signals, after ly with either OOOOOOOO or 00000001. If there is a sec- which they too go into an idle state. When the 8XC152 ond collision it gets loaded with the random selection of detects the idle state at its own GRXD pin, the backoff 00000000, 00000oo 1, 00000o 10, or 00000o 11. On the sequence begins. third collision there will be a random selection among 8 possible numbers. On the fourth, among 16, etc. Figure 3.5 shows the logical arrangement of PRBS, TCDCNT, Backoff and BKOFF. There are three software selectable collision resolution algorithms in the 8XC152. The selection is made by BKOFF starts counting down from its preload value, writing values to 3 bits: counting slot times. At any time, the current value in BKOFF can be read by the CPU, but CPU writes to DCR M1 MO Algorithm BKOFF have no effect. While BKOFF is counting 0 0 0 Normal Random down, if its current value is not 0, transmission is dis- 0 1 1 Alternate Random abled. The output signal "BKOFF = 0" is asserted when BKOFF reaches 0, and is used to re-enable trans- 1 1 1 Deterministic mission. Ml and MO reside in GMOD, and DCR is in At that time transmission can proceed, subject of MYSLOT. course to IFS enforcement, unless: • shifting a 1 into TCDCNT from the right caused a 1 In the Normal Random algorithm, the GSC backs off to shift out from the MSB of TCDCNT, or for a random number of slot times and then decides whether to restart the transmission. The backoff time • the collision was detected after TFIFO had been ac- begins as soon as a line idle condition is detected. cessed by the transmit hardware. The Alternate Random algorithm is the same as the Normal Random except the backoff time doesn't start until an IFS has transpired. 7-25 int:eL 83C152 HARDWARE DESCRIPTION SKOFF = MYSLOT 270427-38 Figure 3.5. 8ackoff Timer Logic In either of these cases, the transmitter is disabled most protocols, the slot period must be equal to or (TEN = 0) and the Transmit Error flag TCDT is set. greater than the longest round trip propagation time The automatic restart is canceled. plus the jam time. Where the Normal and Alternate Random backoff al- gorithms differ is that in Normal Random backoff ~he Deterministic Backoff BKOFF timer starts counting down as soon as a Ime In the Deterministic backoff mode, the GSC is assigned idle condition is detected, whereas in Alternate Ran- (in software) a slot number. The slot assignment is wri!- dom hackoff the BKOFF timer doesn't start counting ten to the low 6 bits of the register MYSLOT. This down till the IFS expires. same register also contains, in the 2 high bit positions, the control bits DCJ and DCR. The Alternate Random mode was designed for net- works in which the slot time is less than the IFS. If the Slot assignments therefore can run from 0 to 63. It will randomly assigned backoff time for a given transmitter tum out that the higher the slot assignment, the sooner happens to be 0, then it is free to transmit as soon as the the GSC will get to restart its transmission in the event IFS ends. If the slot time is shorter than the IFS, Nor- of a collision. mal Random mode would nearly guarantee that if there's a first collision there will be a second collision. The highest slot assignment in the network is written The situation is avoided in Alternate Random mode, by each station's software into its TCDCNT register. since the BKOFF countdown doesn't start till the IPS Normally the highest slot assignment is just the total is over. number of stations that are going to participate in the backoff algorithm. The unit of count to the BKOFF timer is the slot time. The slot time is measured in bit-times, and is deter- In deterministic backoff mode a collision will not cause mined by a CPU write to the register SLOTTM. The a 1 to be shifted into TCDCNT. TCDCNT will still be slot time clock is a I-byte downcounter which starts its ANDed with PRBS and the result loaded into BKOFF. countdown from the value written to SLOTTM. It is In order to insure that all stations have the same value decremented each bit time when a backoff is in prog- loaded into BKOFF, which determines the first slot ress, and when it gets to I it generates one tick in the number to occur, the PRBS should be loaded with slot time clock. The next state after 1 is the reload value OFFH· the PRBS will maintain this value until either which was written to SLOTTM. If 0 is the value writ- the 8XCI52 is reset or the user writes some other value ten to SLOTTM, the slot time clock will equal 256 bit into PRBS. Mter BKOFF is loaded it begins counting times. down slot times as soon as the IPS ends. Slot times are defmed by the user, the same way as before, by loading A CPU write to SLOTTM accesses the reload register. SLOTTM with the number of bit times per slot. A CPU read of SLOTTM accesses the downcounter. In 7-26 intel~ 83C152 HARDWARE DESCRIPTION When BKOFF equals the slot assignment (as defmed in A transmitting station with HABEN enabled expects MYSLOT), the signal "BKOFF = MYSLOT" in Fig- an acknowledge. It must receive one prior to the end of ure 3.5 is asserted for one slot time, during which the the interframe space, or else an error is assumed and GSC can restart its transmission. the NOACK bit is set. Setting of the TON bit is also delayed until the end of the interframe space. Collisions While BKOFF is counting down, if any activity is de- detected during the interframe space will also cause tected at the GRXD pin, the countdown is frozen until NOACK to be set. the activity ends, a line idle condition is detected, and an IFS transpires. Then the countdown resumes from If the user software has enabled OMA servicing of the where it left off. GSC, an interrupt is generated when TON is set. TON will be set at the end of the interframe space if a hard- If a collision is detected at the GRXD pin while ware based acknowledge is required and received. If the BKOFF is counting down, the collision resolution algo- GSC is serviced by the CPU, the user must time out the rithm is restarted from the beginning. interframe space and then check TDN before disabling the transmitter or transmit error interrupts. NOACK In effect, the GSC "owns" its assigned slot number, but will generate a transmit error interrupt if the transmit- with one exception. Nobody owns slot number 0. ter and interrupts are enabled during the interframe Therefore if the GSC is assigned slot number 0, then space. when BKOFF = 0, this station and any other station that has something to say at this time will have an equal chance to take the line. 3.3 SOLe Operation 3.2.7 HARDWARE BASED ACKNOWLEDGE 3.3.1 SDLC OVERVIEW Hardware Based Acknowledge (HBA) is a data link SOLC is a communication protocol developed by IBM packet acknowledging scheme that the user software and widely used in industry. It is based on a primary / can enable with CSMA/CO protocol. It is not an op- secondary architecture and requires that each second- tion with SOLC protocol however. ary station have a unique address. The secondary sta- tions can only communicate to the primary station, and In general HBA can give improved system response then, only when the primary station allows communi- time and increased effective transmission rates over ac- cation to take place. This eliminates the possibility of knowledge schemes implemented in higher layers of the contention on the serial line caused by the secondary network architecture. Another benefit is the possibility station's trying to transmit simultaneously. of early release of the transmit buffer as soon as the acknowledge is received. In the C152, SOLC can be configured to work in either full or half duplex. When adhering to strict SOLC pro- The acknowledge consists of a preamble followed by an tocol, full duplex is required. Full duplex is selected idle condition. A receiving station with HABEN en- whenever a 16-bit CRC is selected. At the end of a valid abled will send an acknowledge only if the incoming reset the 16-bit CRC is selected. To select half duplex address is unique to the receiving station and if the with a 16-bit CRC, the receiver must be turned off by frame is determined to be correct with no errors. For user software before transmission. The receiver is the acknowledge to be sent, TEN must be set. For the turned off by clearing the GREN bit (RSTAT.l). The transmitting station to recognize the acknowledge receiver needs to be turned off because the address that GREN must be set. A zero as the LSB of the address is transmitted is the address of the secondary station's indicates that the address is unique and not a group or receiver. If not turned off, the receiver could mistake broadcast address. Errors can be caused by collisions, the outgoing message as being intended for itself. When incorrect CRC, misalignment, or FIFO overflow. The 32-bit CRCs are used, half duplex is the only method receiver sends the acknowledge as soon as the line is available for transmission. sensed to be idle. The user must program the interframe space and the preamble length such that the acknowl- edge is completed before IFS expires. This is normally done by programming IPS larger than the preamble. 7-27 intet 83C152 HARDWARE DESCRIPTION 3.3.2 SOLC Frame Format CONTROL - The control field is used for initialization of the system, identifying the sequence of a frame, to The format of an SOLC frame is shown in Figure 3.6. identify if the message is complete, to tell secondary The frame consists of a Beginning of Frame flag, Ad- stations if a response is expected, and acknowledgement dress field, Control Field, Information field (optional), of previously sent frames. The user software is responsi- a CRC, and the End of Frame flag. ble for insertion of the control field as the GSC hard- ware has no provisions for the management of this IBOF IADDRESS ICONTROL IINFO ICRG IEOF I field. The interpretation and formation of the control field must also be handled by user software. The infor- Figure 3.6. Typical SOLC Frame mation following the control field is typically used for information transfer, error reporting, and various other BOF - The begin of frame flag for SOLC is 01111110. functions. These functions are accomplished by the for- It is only one of two possible combinations that have six mat of the control field. There are three formats avail- consecutive ones in SOLC. The other possibility is an able. The types of formats are Informational, Supervi- abort character which consists of eight or more consec- sory, or Unnumbered. Figure 3.7 shows the various for- utive ones. This is because SOLC utilizes a process mat types and how to identify them. called bit stuffmg. Bit stuffing is the insertion of a 0 as the next bit every time a sequence of five consecutive Is Since the user software is responsible for the implemen- is detected. The receiver automatically removes a 0 af- tation of the control field, what follows is a simple ex- ter every consecutive group of five ones. This removal planation on the control field and its functions. For a of the 0 bit is referred to as bit stripping. Bit stuffing is complete understanding and proper implementation of discussed in Section 3.3.4. All the procedures required SOLC, the user should refer to the IBM document, for bit stuffmg and bit stripping are automatically han- GA27-3093-2, IBM Synchronous Oata Link Control dled by the GSC. General Information. Within that document, is another list of IBM documents which go into detail on the In standard SOLC protocol the BOF signals the start of SOLC protocol and its use. a frame and is limited to 8 bits in length. Since there is no preamble in SOLC the BOF is considered an entire The control field is eight bits wide and the format is separate field and marks the beginning of the frame. determined by bits 0 and 1. If bit 0 is a zero, then the The BOF also serves as the clock synchronization frame is an informational frame. If bit 0 is a one and bit mechanism and the reference point for determining the 1 a zero, then it is a supervisory frame, and if bit 0 is a position of the address and control fields. one and bit 1 a one then the frame is an unnumbered frame. ADORESS - The address field is used to identify which stations the message is intended for. Each secondary In an informational frame bits 3,2,1 contain the se- station must have a unique address. The primary sta- quence count of the frame being sent. tion must then be made aware of which addresses are assigned to each station. The address length is specified Bit 4 is the PIF (po11/Final) bit. If bit 4 equals 1 and as 8-bits in standard SOLC protocols but it is expand- originates from th~ primary, then the secondary station able to 16-bits in the C152. User software can further is expected to initiate a transmission. If bit 4 equals 1 expand the number of address bits, but the automatic and originates from a secondary station, then the frame address recognition feature works on a maximum of 16- is the final frame in a transmission. bits. Bits 7,6,5 contain the sequence count a station expects In SDLC the addresses are normally unique for each on the next transmission to it. The sequence count can station. However, there are several classes of messages vary from OOOB to 111 B. The count then starts over that are intended for more than one station. These mes- again at OOOB after the value II1B is incremented. The sages are called broadcast and group addressed frames. acknowledgement is recognized by the receiving station An address consisting of all Is will alway~ be automati- when it decodes bits 7,6,5 of an incoming frame. The cally received by the GSC, this is defined as the broad- station sending the transmission is acknowledging the cast address in SOLC. A group address is an address frames received up to the count represented in bits 7,6,5 that is common to more than one station. The GSC (sequence count-I). With this method, up to seven se- provides address masking bits to provide the capability quential frames may be transmitted prior to an ac- of receiving group addresses. knowledgement being received. If eight frames were al- lowed to pass before an acknowledgement, the sequence If desired, the user software can mask off all the bits of count would roll over and this would negate the pur- the address. This type of masking puts the GSC in a pose of the sequence numbers. promiscuous mode so that all addresses are received. 7-28 intel~ 83C152 HARDWARE DESCRIPTION BIT POSITIONS- 7 6 5 4 3 2 o RE¢EPT!ION S~NDING SEQUENCE 0 POLL/ 1 SEQUENCE FINAL 270427-15 RECEPTION SEQUENCE - The sequence expected in the SENDING SEQUENCE portion of the control byte in the next received frame. This also confirms correct reception of up to seven frames prior to the sequence given. POLLIFINAL - Identifies the frame as being a polling request from the master station or the last in a series of frames from the master or secondary. SENDING SEQUENCE - Identifies the sequence of the frame being transmitted. o - If bit 0 = 0 the frame is identified as a informational format type. IN FORMATION FORMAT ~ _ _ _ _ _ _---.r--_......_ _'""!'-_---.,...--_ _ _---, 7 POSITIONS-,...-- 6 5 432 0 RE¢EPT!ION SEQU ENcE POLL/ FINAL MOi.DE 0:: 1 270427-16 RECEPTION SEQUENCE - Expected sequence of frame for next reception. POLLIFINAL - Identifies frame as being a polling request from the master station or the last in a series of frames from the master or secondary. MODE - Identifies whether receiver is ready (00), not ready (10) or a frame was rejected (01). The rejected frame is identified by the reception sequence. 0,1 - If bits 1,0 = 0,1 the frame is identified as a supervisory format type. SUPERVISORY FORMAT BIT POSITIONS- 7 5 6 4 3 2 o COMMAND/ POLL/ COMMAND/ R~SPONSE FINAL RESPONSE 1 1 270427-17 COMMAND/RESPONSE - Identifies the type of command or response. POLLIFINAL - Identifies frame as being a polling request from the master station or the last in a series of frames from the master or secondary. 1,1 - If bits 1,0 = 1,1 the frame is identified as an unnumbered format type. NONSEQUENCED FORMAT 270427-18 Figure 3.7. SOLC Control Field 7-29 intel~ 83C152 HARDWARE DESCRIPTION Following the informational control field comes the in- When the mode is 10, the sending station is indicating formation to be transferred. that its receiver is not ready to accept frames. In the supervisory format (bits 1,0 = 0,1) bits 3,2 de- Mode 11 is an illegal mode in SDLC protocol. termine which mode is being used. Bits 7,6,5 represent the value of the sequence the sta- When the mode is 00 it indicates that the receive line of tion expects when the next transfer occurs for that sta- the station that sent the supervisory frame is enabled tion. There is no information following the control field and ready to accept frames. when the supervisory format is used. When the mode is 01, it indicates that previously a In the unnumbered format (bits 1,0 = 1,1) bits 7, 6, 5, received frame was rejected. The value in the receive 3, 2 (notice bit 4 is missing) indicate commands from count identifies which frame(s) need to be retransmit- the primary to secondary stations or requests of second- ted. ary stations to the primary. The standard commands are: BITS 7 6 5 3 2 Command o 0 0 0 0 Unnumbered Information (UI) o 0 0 0 1 Set initialization mode (SIM) o 1 0 0 0 Disconnect (DISC) o 0 1 0 0 Response optional (UP) o 0 Function descriptor in information field (CFGR) o 1 1 Identification in information field. (XID) o 0 Test pattern in information field. (TEST) The standard responses are: BITS 7 6 5 3 2 Command o 0 0 0 0 Unnumbered information (UI) o 0 0 0 Request for initialization (RIM) o 0 0 1 Station in disconnected mode (DM) 1 0 0 0 1 Invalid frame received (FRMR) o 1 1 0 0 Unnumbered acknowledgement (UA) 1 1 1 Signal loss of input (BCN) 1 0 0 1 Function descriptor in information field (CFGR) o 1 0 0 0 Station wants to disconnect (RD) o 1 1 Identification in information field (XID) o 0 Test pattern in information field (TEST) 7-30 intel~ 83C152 HARDWARE DESCRIPTION In an unnumbered frame, information of variable rithms, a 16-bit and a 32-bit. The 32-bit algorithm is length may follow the control field if VI is used, or normally used in CSMAlCD applications and is de- information of fixed length may follow if FRMR is scribed in section 3.2.2. In most SDLC applications a used. 16-bit CRC is used and the hardware configuration that supports 16-bit CRC is shown in Figure 3.8. The gener- As stated earlier, the user software is responsible for the ating polynomial that the CRC generator uses with the proper management of the control field. This portion of 16-bit CRC is: the frame is passed to or from the GSC FIFOs as basic informational type data. G(X) = X16 + X12 + X5 + 1 INFO - This is the information field and contains the The way the CRC operates is that as a bit is received it data that one device on the link wishes to transmit to is XOR'd with bit 15 of the current CRC and placed in another device. It can be of any length the user wishes, temporary storage. The result of XOR'ing bit 15 with but must be a multiple of 8 bits. It is possible that some the received bit is then XOR'd with bit 4 and bit 11 as frames may contain no information field. The informa- the CRC is shifted one position to the right. The bit in tion field is identified to the receiving stations by the temporary storage is shifted into position O. preceding control field and the following CRC. The GSC determines where the last of the information field The required CRC length for SDLC is 16 bits. The is by passing the bits through the CRC generator. CRC is automatically stripped from the frame and not When the last bit or BOF is received the bits that re- passed on to the CPU. The last 16 bits are then run main constitute the CRC. though the CRC generator to insure that the correct remainder is left. The remainder that is checked for is CRC - The Cyclic Redundancy Check (CRC) is an er- 001110100001111B (IDOF Hex). If there is a mis- ror checking sequence commonly used in serial com- match, an error is generated. The user software has the munications. The C152 offers two types of CRC algo- option of enabling this interrupt so the CPV is notified. 270427-19 Figure 3.8. 16-Bit CRC 7-31 intel~ 83C152 HARDWARE DESCRIPTION EOF - The End Of Frame (BOp) indicates when the 3.3.5 SENDING ABORT CHARACTER transmission is complete. The EOF is identified bv the end flag. An end -flag consists of the bit pa"ttern An abort character is one of the exceptions to the rule 01111110. The EOF can also serve as the BOF for the that disallows more than 5 consecutive Is. The abort next frame. character consists of any occurrence of seven or more consecutive ones. The simplest way for the C152 to send an abort character is to clear the TEN bit. This 3.3.3 DATA ENCODING causes the output to be disabled which, in turn, forces it The transmission of data in SDLC mode is done via to a constant high state. The delay necessary to insure NRZI encoding as shown in Figure 3.9. NRZI encod- that the link is high for seven bit times is a task that ing transmits data by changing the state of the output needs to be handled by user software. Other methods of whenever a 0 is being transmitted. Whenever a 1 is sending an abort character are using the IFS register or transmitted the state of the output remains the same as using the Raw Transmit mode. Using IFS still entails the previous bit and remains valid for the entire bit clearing the TEN bit, but TEN can be immediately re- time. When SDLC mode is selected it automatically enabled. The next message will not begin until the IFS enables the NRZI encoding on the transmit line and expires. The IFS begins timing out as soon as DEN NRZI decoding on the receive line. The Address and goes high which identifies the end of transmission. This Info bytes are transmitted LSB first. The CRC is trans- also requires that IFS contain a value equal to or great- mitted MSB first. er than 8. This method may have the undesirable effect that DEN goes high and disables the external drivers. The other alternative is to switch to Raw Transmit 3.3.4 BIT STUFFING/STRIPPING mode. Then, writing OFFH to TFIFO would generate a high output for 8 bit times. This method would leave In SDLC mode one of the primary rules of the protocol DEN active during the transmission of the abort char- is that in any normal data transmission, there will never acter. be an occurrence of more than 5 consecutive Is. The GSC takes care of this housekeeping chore by automat- When the receiver detects seven or more consecutive Is ically inserting a 0 after every occurrence of 5 consecu- and data has been loaded into the receive FIFO, the tive Is and the receiver automatically removes a zero RCABT flag is set in RSTAT and that frame IS ig- after receiving 5 consecutive Is. All the necessary steps nored. If no data has been loaded into the receive required for implementing bit stuffing and stripping are FIFO, there are no abort flags set and that frame is just incorporated into the GSC hardware. This makes the ignored. A retransmitted frame may immediately fol- operation transparent to the user. About the only time Iowan abort character, provided the proper flags are this operation becomes apparent to the user, is if the used. actual data on the transmission medium is being moni- tored by a device that is not aware of the automatic insertion of Os. The bit stuffmglstripping guarantees that there will be at least one transition every 6 bit times while the line is active. o o o NRZ~ BIT I T1ME-: 270427-20 Figure 3.9. NRZI Encoding 7-32 intet 83C152 HARDWARE DESCRIPTION 3.3.6 LINE IDLE passing the message to the downstream station. This delay is necessary so that a station can decode its own If 15 or more consecutive Is are detected by the receiv- address before the message is passed on. The various er the Line Idle bit (LNI) in TSTAT is set. The seven networks are shown in Figure 3.10. Is from the abort character may be included when sens- ing for a line idle condition. The same methods used for sending the Abort character can be used for creating 3.3.9 HDLC/SDLC COMPARISON the Idle condition. However, the values would need to HOLC (High level Oata Link Control) is a standard be changed to reflect 15 bit times, instead of seven bit adopted by the International Standards Organization times. (ISO). The HOLC standard is defmed in the ISO docu- ment #ISO 6159 - HOLC unbalanced classes ofproce- 3.3.7 ACKNOWLEDGEMENT dures. IBM developed the SOLC protocol as a subset of HOLC. SOLC conforms to HOLC protocol require- Acknowledgment in SOLC is an implied acknowledge ments, but is more restrictive. SOLe contains a more and is contained in the control field. Part of the control precise defmition on the modes of operation. frame is the sequence number of the next expected frame. This sequence number is called the Receive Some of the major differences between SOLC and Count. In transmitting the Receive Count, the receiver HOLC are: is in fact acknowledging all the previous frames prior to SDLC HOLe the count that was transmitted. This allows for the Unbalanced (primary/ Balanced transmission of up to seven frames before an acknowl- secondary) (peer to peer) edge is required back to the transmitter. The limitation Modulo 8 (no extensions Modulo 128 (up to 127 of seven frames is necessary because the Receive Count in the control field is limited to three binary digits. This allowed, up to 7 out- outstanding frames means that if an eighth transmission occurred this standing frames before before acknowledge would cause the next Receive Count to repeat the first acknowledge is required) is required) count that still is waiting for an acknowledge. This 8-bit addressing only Extended addressing would defeat the purpose of the acknowledgement. The Byte aligned data Variable size of data processing and general maintenance of the sequence count must be done by the user software. The Hard- The C152 does not support HOLC implementation re- ware Based Acknowledge option that is provided in the quiring data alignment other than byte alignment. The C152 is not compatible with standard SOLC protocol. user will find that many of the protocol parameters are programmable in the C152 which allows easy imple- mentation of proprietary or standard HOLC network. 3.3.8 PRIMARY/SECONDARY STATIONS User software needs to implement the control field All SOLC networks are based upon a primary/second- functions. ary station relationship. There can be only one primary station in a network and all the other stations are con- 3.3.10 USING A PREAMBLE IN SOLe sidered secondary. All communication is between the primary and secondary station. Secondary station to When transmitting a preamble in SOLC mode, the user secondary station direct communication is prohibited. should be aware that the pattern of 10101010 ... is If there is a need for secondary to secondary communi- output. NRZI encoding is used in SDLC when the in- cation, the user software will have to make allowances ternal baud rate generator is the clock source and this for the master to act as an intermediary. Secondary means that a transition will occur every two bit times, stations are allowed use of the serial line only when the when a 0 is transmitted. This compares with some oth- master permits them. This is done by the master polling er SOLC devices, most of which transmit the pattern the secondary stations to see if they have a need to OOOOOOOO . . . which will cause a transition every bit access the serial line. This should prevent any collisions time. Our past experience has shown that the C152 pre- from occurring, provided each secondary station has its amble does not cause a problem with most other devic- own unique address. This arrangement also partially es. This is because the preamble is used only to define determines the types of networks supported. Normal the relative bit time boundaries within some variation SOLC networks consist of point-to-point, multi-drop, allowed by the receiving station, and the C152 pream- or ring configurations and the C152 supports all of ble fulfills this function. The C152 does not have any these. However, some SOLC processors support an au- problems with receiving a preamble consisting of all Os. tomatic one bit delay at each node that is not supported One note of caution however. If idle fill flags are used by the C 152. In a "Loop Mode" configuration, is is in conjunction with a preamble, the addresses OO(OO)H necessary that the transmission be delayed from the re- and 55(55)H should not be assigned to any C152 as the ception of the frames from the upstream station before preamble following the idle fill flags will be interpreted as an address. 7-33 83C152 HARDWARE DESCRIPTION 3.4 User Defined Protocols 3.5 Using the GSC The explanation on the implementation of user defmed protocols would go beyond the scope of this manual, 3.5.1 LINE DISCIPLINE but examining Table 3.1 should give the reader a con- Line discipline is how the management of the transfer solidated list of most of the possibilities. In this manual, of data over the physical medium is controlled. Two any deviation from the documents that cover the imple- types of line discipline will be discussed in this section: mentation of CSMA/CD or SDLC are considered user full duplex and half duplex. defined protocols. Examples of this would be the use of SDLC with the 32-bit CRC selected or CSMAlCD with hardware based acknowledge. Point-to-Point Network 270427-21 Multi-Drop Network 270427-22 Ring Network 270427-23 Figure 3.10. SDLC Networks 7-34 inteL 83C152 HARDWARE DESCRIPTION Full duplex is the simultaneous transmission and recep- expansion plans become a mute issue. However, it is tion of data. Full duplex uses anywhere from two to strongly suggested that there always be some allowance four wires. At least one wire is needed for transmission for future modifications. and one wire for reception. Usually there will also be a ground reference on each signal if the distance from Some of the general areas that will impact the overall station to station is relatively long. Full-duplex opera- scheme on how to incorporate future changes to the tion in the C152 requires that both the receive and the system are: transmit portion of the GSC are functioning at the same time. Since both the transmitter and receiver are 1) Communication of the change to all the stations or operating, two CRC generators are also needed. The the primary station. C152 handles this problem by having one 32-bit CRC generator and one 16-bit CRC generator. When sup- 2) Maximum distance for communication. This will af- porting full-duplex operation, the 32-bit CRC generator fect the drivers used and the slot time. is modified to work as a 16-bit CRC generator. When- ever the 16-bit CRC is selected, the GSC automatically 3) More stations may be on the line at one time. This enters the full duplex mode. Half duplex with a 16-bit may impact the interframe space or the collision resolu- CRC is discussed in the following paragraph. tion used. Half duplex is the alternate transmission and reception 4) If using CSMAlCD without deterministic resolu- of data over a single common wire. Only one or two tion, any increase in network size will have a negative wires are needed in half-duplex systems. One wire is impact on the average throughput of the network and needed for the signal and if the distance to be covered is lower the efficiency. The user will have to give careful long there will also be a wire for the ground reference. consideration when deciding how large a system can In half-duplex mode, only the receiver or transmitter ultimately be and still maintain adequate performance. can operate at one time. When the receiver or transmit- ter operates is determined by user software, but typical- ly the receiver will always be enabled unless the GSC is 3.5.3 DMA SERVICING OF GSC CHANNELS transmitting. When using the C152 in half-duplex and There are two sources that can be used to control the the receiver is connected to the transmitter it is possible GSc. The first is CPU control and the second is DMA that a station will receive its' own transmission. This control. can occur if a broadcast address is sent, the address mask register(s) are filled with all Is, or the address CPU control is used when user software takes care of being sent matches the sending stations address the tasks such as: loading the TFIFO, reading the RFI- through the use of the address masking registers. The Fa, checking the status flags, and general tracking of receiver must be disabled by the user while transmitting the transmission process. As the number of tasks grow if any of these conditions will occur, unless the user and higher data transfer rates are used, the overhead wants a station to receive its own transmission. The required by the CPU becomes the dominant consump- receiver is disabled by clearing GREN (and GAREN if tion of time. Eventually, a point is reached where the used). Half-duplex operation in the C152 is supported CPU is spending 100% of its time responding to the with either 16-bit or 32-bit CRCs. Whenever a 32-bit needs of the GSc. An alternative is to have the DMA CRC is selected, only half-duplex operation can be sup- channels control the GSC. ported by the GSC. It is possible to simulate full-duplex operation with a 32-bit CRC, but this would require A detailed explanation on the general use of the DMA that the CRC be performed with software. Calculating channels is covered in Section 4. In this section only the CRC with the CPU would greatly reduce the data those details required for the use of the DMA channels rates that could be used with the GSC. Whenever a 16- with the GSC will be covered. bit CRC is selected, full-duplex operation is automati- cally chosen and the GSC must be reconfigured if half- The DMA channels can be configured by user software duplex operation is preferred. so that the GSC data transfers are serviced by the DMA controller. Since there are two DMA channels, 3.5.2 PLANNING FOR NETWORK CHANGES one channel can be used to service the receiver, and one AND EXPANSIONS channel can be used to service the transmitter. In using the DMA channels, the CPU is relieved of much of the A complete explanation on how to plan for network time required to do the basic servicing of the GSC buff- expansion will not be covered in this manual as there ers. The types of servicing that the DMA channels can are far too many possibilities that would need to be provide are: loading of the transmit FIFO, removing discussed. But there are several areas that will have data from the receive FIFO, notification of the CPU major impact when allowing for changes in the system. when the transmission or reception has ended, and re- In cases where there will never be any changes allowed, sponse to certain error conditions. When using the 7-35 intel~ 83C152 HARDWARE DESCRIPTION DMA channels the source or destination of the data that will be received, up to 64K. If not using the Done intended for serial transmission can be internal data flag, then GSC servicing would be driven by the receive memory, external data memory, or any of the SFRs. Done (RON) flag and/or interrupt. RDN is set when the EOF is detected. When using the RDN flag, RFNE The only tasks required after initialization of the DMA should also be checked to insure that all the data has and GSC registers are enabling the proper interrupts been emptied out of the receive FIFO. and informing the DMA controller when to start. After the DMA channels are started all that is required of the The byte count register is used for all transmissions and CPU is to respond to error conditions or wait until the this means that all packets going out will have to be of end of transmission. the same length or the length of the packet to be sent will have to be known prior to the start of transmission. Initialization of the DMA channels requires setting up When using the DMA channels to service the GSC the control, source, and destination address registers. transmitter, there is no practical way to disable the On the DMA channel servicing the receiver, the con- Done flag. This is because the transmit done flag trol register needs to be loaded as follows: DCONn.2 = (TDN) is set when the transmit FIFO is empty and the 0, this sets the transfer mode so that response is to GSC last message bit has been transmitted. But, when using interrupts and put the DMA control in alternate cycle the DMA channel to service the transmitter, loads to mode; DCONn.3 = 1, this enables the demand mode; the TFIFO continue to occur until the byte count DeONn.4 = 0, this clears the automatic increment reaches O. This makes it impossible to use TDN as a option for the source address; and DCONn.5 = 1, this flag to stop the DMA transfers to TFIFO. It is possible defines the source as SFR. The DMA channel servicing to examine some other registers or conditions, such as the receiver also needs its source address register to the current byte count, to determine when to stop the contain the address of RFIFO (SARHN = XXH, DMA transfers to TFIFO, but this is not recommended SARLN = OF4H). On the DMA channel servicing the as a way to service the DMA and GSC when transmit- transmitter, the control register needs to be loaded as ting because frequent reading of the DMA registers will follows: DCONn.2 = 0; DCONn.3 = 1; DCONn.6 = cause the effective DMA transfer rate to slow down. 0, this clears the automatic increment option for the destination address; and DCONn.7 = 1, this sets the When using the DMA channels, initialization of the destination as SFR. The DMA channel serving the GSC would be exactly the same as normal except that transmitter also requires that its destination address TSTAT.O = 1 (DMA), this informs the GSC that the register contains the address of TFIFO (DARHN = DMA channels are going to be used to service the GSC. XXH, DARLN = 85H). Assuming that DCONO Although only TSTAT is written to, both the receiver would be serving the receiver and DCONI the trans- and transmitter use this same DMA bit. mitter, DCONO would be loaded with XXlOlOXOB and DCONI would be loaded with lOXXlOXOB. The The interrupts EGSTE (IEN1.5), GSC transmit error; contents of SARHO and DARHI do not have any im- EGSTY (lEN 1.3), GSC transmit valid; EGSRE pact when using internal SFRs as the source or destina- (IENl.l), GSC receive error; and EGSRV (IEN1.0), tion. GSC receive valid; need to be enabled. The DMA inter- rupts are normally not used when servicing the GSC When using the DMA channels to service the GSC, the with the DMA channels. To ensure that the DMA in- byte count registers will also need to be initialized. terrupts are not responded to is a function of the user software and should be checked by the software to The Done flag for the DMA channel servicing the re- make sure they are not enabled. Priority for these inter- ceiver should be used if fixed packet lengths only are rupts can also be set at this time. Whether to use high being transmitted or to insure that memory is not over- or low priority needs to be decided by the user. When written by long received data packets. Overwriting of responding to the GSC interrupts, if a buffer is being data can occur when using a smaller buffer than the used to store the GSC information, then the DMA reg- packet size. In these cases the servicing of the DMA isters used for the buffer will probably need updating. and/or GSC would be in response to the DMA Done flag when the byte count reaches zero. After this initialization, all that needs to be done when the GSC is actually going to be used is: load the byte In some cases the buffer size is not the limiting factor count, set-up the source addresses for the DMA chan- and the packet lengths will be unknown. In these cases nel servicing the transmitter, set-up the destination ad- it would be desirable to eliminate the function of the dresses for the DMA channel servicing the receiver, Done flag. To effectively disable the Done flag for the and start the DMA transfer. The GSC enable bits DMA channel servicing the receiver, the byte count should be set first and then the GO bits for the DMA. should be set to some number larger than any packet This initiates the data transfers. 7-36 in1:et 83C152 HARDWARE DESCRIPTION This simplifies the maintenance of the GSC and can Initialization of the system can be broken down into make the implementation of an external buffer for several steps. First, are the assumptions of each net- packetized information automatic. work station. An external buffer can be used as the source of data for The first assumption is that the type of data encoding transmission, or the destination of data from the receiv- to be used is predetermined for the system and that er. In this arrangement, the message size is limited to each station will adhere to the same basic rules defining the RAM size or 64K, whichever is smaller. By using that encoding. The second assumption is that the basic an external buffer, the data can be accessed by other protocol and line discipline is predetermined and devices which may want access to the serial data. The known. This means that all stations are using CSMAJ amount of time required for the external data moves CD or SDLC or whatever, and that all stations are will also decrease. Under CPU control, a "MOVX" either full or half duplex. The third assumption is that command would take 24 oscillator periods to complete. the baud rate is preset for the whole system. Although Under DMA control, external to internal, or internal to the baud rate could probably be determined by the mi- external, data moves take only 12 oscillator periods. croprocessor just by monitoring the link, it will make it much simpler if the baud rate is known in advance. 3.5.4 BAUD RATE One of the first things that will be required during sys- tem initialization is the assignment of unique addresses The GSC baud rate is determined by the contents of the for each station. In a two-station only environment this SFR, BAUD, or the external clock. The formula used to determine the baud rate when using the internal is not necessary and can be ignored. However, keep in clock is: mind, that all systems should be constructed for easy future expansions. Therefore, even in only a two station (fosc)/«BAUD+ 1)*8) system, addresses should be assigned. There are three basic ways in which addresses can be assigned. The For example if a 12 MHz oscillator is used the baud first, and most common is preassigned addresses that rate can vary from: are loaded into the station by the user. This could be done with a DIP-switch, through a keyboard. The sec- 12,000,000/«0+1)*8) = 1.5 MBPS ond method of assigning addresses is to randomly as- sign an address and then check for its uniqueness to: throughout the system, and the third method is to make an inquiry to the system for the assignment of a 12,000,000/«255+ 1)*8) = 5.859 KBPS unique address. Once the method of address assignment is determined, the method should become part of the There are certain requirements that the external clock specifications for the system to which all additions will will need to meet. These requirements are specified in have to adhere. This, then, is the final assumption. the data sheet. For a description of the use of the GSC with external clock please read Section 3.5.11. The negotiation process may not be clear for some readers. The following two procedures are given as a guideline for dynamic address assignment. 3.5.5 INITIALIZATION In the first procedure, a station assumes a random ad- Initialization can be broken down into two major com- dress and then checks for its uniqueness throughout the ponents, 1) initialization of the component so that its system. As a station is initialized into the system it serial port is capable of proper communication; and 2) sends out a message containing its assumed address. initialization of the system or a station so that intelligi- The format of the message should be such that any ble communication can take place. station decoding the address recognizes it as a request for initialization. If that address is already used, the Most of the initialization of the component has already receiving station returns a message, with its own ad- been discussed in the previous sections. Those items not dress stating that the address in question is already tak- covered are the parameters required for the component en. The initializing station then picks another address. to effectively communicate with other components. When the initializing station sends its inquiry for the These types of issues are common to both system and address check, a timer is also started. If the timer ex- component initialization and will be covered in the fol- pires before the inquiry is responded to, then that sta- lowing text. tion assumes the address chosen is okay. 7-37 intet 83C152 HARDWARE DESCRIPTION In the second procedure, an initializing station asks for In Raw Receive, the transmitter should be externally an address assignment from the system. This reauires connected to the receiver. To do this a oart Din should that some station on the link take care of the task of be used to enable an external device to ~onn~t the two maintaining a record of which addresses are used. This pins together. In Raw Receive mode the receiver acts as station will be called station-I. When the initializing normal except that all bytes following the BOF are station, called station-2, gets on the link, it sends out a loaded into the receive FIFO, including the CRC. Also message with a broadcast address. The format of the address recognition is not active but needs to be per- message should be such that all other stations on the formed in software. If SDLC is selected as the protocol, link recognize it as a request for address assignment. zero-bit deletion is still enabled. The transmitter still Part of the message from station-2 is a random number operates as normal and in this mode most of the trans- generated by the station requesting the address. Sta- mitter functions and an external transceiver can be test- tion-2 then examines all received messages for this ran- ed. This is also the only way that the CRC can be read dom number. The random number could be the address by the CPU, but the CRC error bit will not be set. of the received message or could be within the informa- tion section of a broadcast frame. All the stations, ex- cept station-I, on the link should ignore the initializa- 3.5.7 EXTERNAL DRIVER INTERFACE tion request. Station-I, upon receiving the initialization A signal is provided from the C152 to enable transmit- request, assigns an address and returns it to station-2. ter drivers for the serial link. This is provided for sys- Station-l will be required to format the message in such tems that require more than what the GSC ports are a manner so that all stations on the link recognize it as capable of delivering. The voltage and currents that the a response to initialization. This means that all stations GSC is capable of providing are the same levels as those except station-2 ignore the return message. for normal port operation. The signal used to enable the external drivers is DEN. No similar signal is needed for 3.5.6 TEST MODES the receiver. There are two test modes associated with the GSC that DEN is active one bit time before transmission begins. are made available to the user. The test modes are In CSMA/CD DEN remains active for two bit times named Raw Receive and Raw Transmit. The test after the CRC is transmitted. In SDLC DEN remains modes are selected by the proper setting of the two active until the last bit of the EOF is transmitted. mode bits in GMOD (MO = GMOD.5, Ml = GMOD.6). If MI,MO = 0,1 then Raw Transmit is se- lected. If M1,MO = 1,0 then Raw Receive is enabled. 3.5.8 JITTER (RECEIVE) The 32-bit CRC cannot be used in any of the test Data jitter is the difference between the actual transmit- modes, or else CRC errors will occur. ted waveform and the exact calculated value(s). In NRZI, data jitter would be how much the actual wave- In Raw Transmit, the transmit output is internally con- form exceeds or falls short of one calculated bit time. A nected to the Receiver input. This is intended to be bit time equals l/baud rate. If using Manchester encod- used as a local loop-back test mode, so that all data ing, there can be two transitions during one bit time as written to the transmitter will be returned by the re- shown in Figure 3.11. This causes a second parameter ceiver. Raw Transmit can also be used to transmit user to be considered when trying to figure out the complete data. If Raw Transmit is used in this way the data is data jitter amount. This other parameter is the half-bit emitted with no preamble, flag, address, CRC, and no jitter. The half-bit jitter is comprised of the difference in bit insertion. The data is still encoded with whatever time that the half-bit transition actually occurs and the format is selected, Manchester with CSMA/CD, NRZI calculated value. Jitter is important because if the tran- with SDLC or as NRZ if external clocks are used. The sition occurs too soon it is considered noise, and if the receiver still operates as normal and in this mode most transition occurs too late, then either the bit is missed of the receive functions can be tested. or a collision is assumed. 7-38 intet 83C152 HARDWARE DESCRIPTION LOGICAL VALUE o o o ~ANCHESTER ENCODING "1" BIT TI~E "1" BIT TI~E (8 X BAUD RECEIV SA~PLlNG t • I I I • RATE .-. I • • 10 ._. I I I I I I I I I I RECEIVED ..- ..- I I I I I I I I DATA I I I I I I I I I I I ....- - - "1" BIT TI~E - - - - - - - - "0" BIT TI~E - - - - - I • RECEIVED I I I .-. I I I I I I • _____ I I I I ..- I I I I I I I I ---- .. DATA I I I I I I I I I I I I 270427-24 Figure 3.11. Jitter 3.5.9 Transmit Waveforms In CSMA/CD the preamble consists of alternating Is and Os. Consequently, the preamble looks like the The GSC is capable of three types of data encoding, waveform in Figure 3.13A and 3.13B. Manchester, NRZI, and NRZ. Figure 3.12 shows ex- amples of all three types of data encoding. 3.5.11 External Clocking 3.5.10 Receiver Clock Recovery To select external clocking, the user is given three choices. External clocking can be used with the trans- The receiver is always monitored at eight times the mitter, with the receiver, or with both. To select exter- baud rate frequency, except when an external clock is nal clocking for the transmitter, XTCLK (GMOD.7) used. When using an external clock the receiver is load- has to be set to a 1. To select external clocking for the ed during the clock cycle. receiver, XRCLK (pCON.3) has to be set to a 1. Set- ting both bits to 1 forces external clocking for the re- In CSMA/CD mode the receiver synchronizes to the ceiver and transmitter. The minimum frequency the transmitted data during the preamble. If a pulse is de- GSC can be externally clocked at is 0 Hz (D.C.). tected as being too short it is assumed to be noise or a collision. If a pulse is too long it is assumed to be a The external transmit clock is applied to pin 4 (TXC), collision or an idle condition. P1.3. The external receive clock is applied to pin 5 (RXC), Pl.4. To enable the external clock function on In SDLC the synchronization takes place. during the the port pin, that pin has to be set to a 1 in the appro- BOF flag. In addition, pulses less than four sample pe- priate SFR, P 1. riods are ignored, and assumed to be noise. This sets a lower limit on the pulse size of received zeros. 7-39 intel~ 83C152 HARDWARE DESCRIPTION , Bii , ,'-TIt.AE-', o o o NRZ L NRZj 270427-25 Figure 3.12. Transmit Waveforms Whenever the external clock option is used, the format (AMSKO, AMSKI) in the C152. These function with of the transmitted and received data is restricted to the GSC receiver only. The transmitted address is treat- NRZ encoding and the protocol is restricted to SDLC. ed like any other data. The address is transmitted under With external clock, the bit stuifmg/stripping is still software control by placing the address byte(s) at the active with SDLC protocol. proper location (usually first) in the sequence of bytes to be output in the outgoing packet. 3.5.12 Determining Receiver Errors The C152 can have up to four different 8-bit addresses It is possible that several receiver error bits will be set or two different 16-bit addresses assigned to each sta- in response to a single cause. The multiple errors that tion. When using 16-bit addressing, ADRO:ADRI form can occur are: one address and ADR2:ADR3 form the second ad- dress. If the receiver is enabled, it looks for a matching AE and CRCE may both be set when an alignment address after every BOF flag is detected. As the data is error occurs due to a bad CRC caused by the mis- received, if the 8th (or 16th) bit does not match the aligned frame. address recognition circuitry, the rest of the frame is ignored and the search continues for another flag. If the RCABT, AE, and CRCE may be set when an abort address does match the address recognition circuitry, occurs. the address and all subsequent data is passed into the receive FIFO until the EOF flag or an error occurs. OVR, AE, and CRCE may be set when a overrun oc- The address is not stripped and is also passed to curs. RFIFO. The address masking registers, AMSKO and AMSK1, In order to determine the correct cause of the error a work in conjunction with ADRO and ADRI respective- specific order should be followed when examining the ly to identify "don't care" bits. A 1 in any position in error bits. This order is: the AMSKn register makes the respective bit in the ADRn register irrelevant. These combinations can then l)OVR be used for form group addresses. If the masking regis- 2) RCBAT ters are filled with allIs, the C152 will receive all pack- 3) AE ets, which is called the promiscuous mode. If 16-bit 4) CRCE addressing is used, AMSKO:AMSK I form one 16-bit address mask. 3.5.13 Addressing There are four 8-bit address registers (ADRO, ADRI, ADR2, ADR3) and two 8-bit address mask registers 7-40 83C152 HARDWARE DESCRIPTION CSMA/CO Clock Recovery o o o 000 0', IDEAL WAVEFORM , I I I I I I I I I , I I I I I I I 8X SAMPLING RATE 1llllllllIIIIIII!I!II/IIIIIIII!I!l/l/llil/ll!llIllllllkllllllllllllllllri1IIIIIldllllllldllll!l!IIIII//III/IIIIIIIIII//!111111111111111111111111 ACTUAL WAVEFORM RECOVERED BIT STREAM CLOCK 270427-26 Figure 3.13A. Clock Recovery SOLC Clock Recovery o 000 o , 0: 0: IDEAL WAVEFORM , , I I I I I 1 I I I I I I t I I 1 I 8X SAMPLING RATE 111!lIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII11111111111111111111111111111111111111111111111111111111111111""1111 ACTUAL WAVEFORM RECOVERED BIT STREAM CLOCK 270427-27 Figure 3.138. Clock Recovery 7-41 83C152 HARDWARE DESCRIPTION 3.6 GSC Operation If the receiver detects a collision during reception in CSMA/CD mode and if any bytes have been loaded 3.6. i Determining Line Discipline into the receive FIFO, the RCABT flag is set. The GSC hardware then halts reception and resets GREN. The In normal operation the GSC uses full or half duplex user software needs to fIlter any collision fragment data operation. When using a 32-bit CRC (GMOD.3 = 1), which may have been received. If the collision occurred operation can only be half duplex. If using a 16-bit prior to the data being loaded into RFIFO the CPU is CRC (GMOD.3 = 0), full duplex is selected by de- not notifIed and the receiver is left enabled. At the end fault. When using a 16-bit CRC the receiver can be of a reception the RON bit is set and OREN is cleared. turned off while transmitting (RSTAT.l = 0), and the In HABEN mode this causes an acknowledgement to transmitter can be turned off during reception be transmitted if the frame did not have a broadcast or (TSTAT.I = 0). This simulates half-duplex operation multi-cast address. The user software can enable the when using a 16-bit CRC. interrupt for RON to determine when a frame is com- pleted. Normally, HDLC uses a 16-bit CRC, so half duplex is determined by turning off the receiver or transmitter. In DMA mode the interrupts are generated by the in- This is so that the receiver will not detect its own ad- ternal "transmit/receive done" (TDN,RON) condi- dress as transmission takes place. This also needs to be tions. When the CPU responds to TDN or RON, done when using CSMA/CD with a 16-bit CRC for the checks are performed to see if the transmit underrun same reason. error has occurred. The underrun condition is only checked when using the DMA channels. 3.6.2 CPU/DMA CONTROL OF THE GSC Upon power up the CPU mode is initialized. General The data for transmission or reception can be handled DMA control is covered in Section 4.0. DMA control by either the CPU (TSTAT.O = 0) or DMA controller of the GSC is covered in Section 3.5.4. IfDMA is to be (TSTAT.O = 1). This allows the user two sets of flags used for serving the GSC, it must be configured into the to control the FIFO. Associated with these flags are serial channel demand mode and the DMA bit in interrupts, which may be enabled by the user software. TSTAT has to be set. Either one or both sets of flags may be used at the same time. 3.6.3 COLLISIONS AND BACKOFF In CPU control mode the flags (RFNE, TFNF) are gen- The actions that are taken by the GSC if a collision erated by the condition of the receive or transmit FI- occurs while transmitting depend on where the colli- Fo's. After loading a byte into the transmit FIFO, sion occurs. If a collision occurs in CSMAlCD mode there is a one machine cycle latency until the TFNF following the preamble and BOF flag, the TCDT flag is flag is updated. Because of this latency, the status of set and the transmit hardware completes a jam. When TFNF should not be checked immediately following this type of collision occurs, there will be no automatic the instruction to load the transmit FIFO. If using the retry at transmission. After the jam, control is returned interrupts to service the transmit FIFO,the one ma- to the CPU and user software must then initiate what- chine cycle of latency must be considered if the TFNF ever actions are necessary for a proper recovery. The flag is checked prior to leaving the subroutine. possibility that data might have been loaded into or from the GSC deserves special consideration. If these When using the CPU for control, transmission normal- fragments of a message have been passed on to other ly is initiated by setting the TEN bit (TSTAT.l) and devices, user software may have to perform some exten- then writing to TFIFO. TEN must be set before load- sive error handling or notifIcation. Before starting a ing the transmit FIFO, as setting TEN clears the trans- new message, the transmit and receive FIFOs will need mit FIFO. TCDCNT should also be checked by user to be cleared. If DMA servicing is being used the point- software and cleared if a collision occurred on a prior ers must also be reinitialized. It should be noted that a transmission. collision should never occur after the BOF flag in a well designed system, since the system slot time will likely To enable the receiver, GREN (RSTAT.I) is set. After be less than the preamble length. The occurrence of GREN is set, the GSC begins to look for a valid BOF. such a situation is normally due to a station on the link After detecting a valid BOF the GSC attempts to that is not adhering to proper CSMA/CD protocol or match the received address byte(s) against the address is not using the same timings as the rest of the network. match registers. When a match occurs the frame is loaded into the GSC. Due to the CRC strip hardware, A collision occurring during the preamble or BOF flag there is a 40 or 24 bit time delay following the BOF is the normal type of collision that is expected. When until the fIrst data byte is loaded into RFIFO if the 32 this type of collision occurs the GSC automatically or 16 bit CRC is chosen. If the end of frame is detected handles the retransmission attempts for as many as before data is loaded into the receive FIFO, the receiver eight tries. If on the eighth attempt a collision occurs, ignores that frame. 7-42 intel~ 83C152 HARDWARE DESCRIPTION the transmitter is disabled, although the jam and back- are more than 256 stations involved in the collision off are performed. If enabled, the CPU is then inter- there would be no resolution since at least two of the rupted. The user software should then determine what stations will always have the same backoff interval se- action to take. The possibilities range from just report- lected. ing the error and aborting transmission to reinitializing the serial channel registers and attempt retransmission. All the stations monitor the link as long as that station is active, even if not attempting to transmit. This is to If less than eight attempts are desired TCDCNT can be ensure that each station always defers the minimum loaded with some value which will reduce the number amount of time before attempting a transmission and so of collisions possible before TCDCNT overflows. The that addresses are recognized. However, the collision value loaded should consist of all Is as the least signifi- detect circuitry operates slightly differently. cant bits, e.g. 7, OFH, 3FH. A solid block of Is is sug- gested because TCDCNT is used as a mask when gen- In normal back-off mode, a transmitting station always erating the random slot number assignment. The monitors the link while transmitting. If a collision is TCDCNT register operates by shifting the contents one detected one or more of the transmitting stations apply bit position to the left as each collision is detected. As the jam signal and all transmitting stations enter the each shift occurs a 1 is loaded into the LSB. When back-off algorithm. The receiving stations also con- TCDCNT overflows, GSC operation stops and the stantly monitor for a collision but do not take part in CPU is notified by the setting of the TCDT bit which the resolution phase. This allows a station to try to can flag an interrupt. transmit in the middle of a resolution period. This in tum mayor may not cause another collision. If the new The amount of time that the GSC has before it must be station trying to transmit on the link does so during an ready to retransmit after a collision is determined by unused slot time then there will probably not be a colli- the mode which is selected. The mode is determined sion. If trying to transmit during a used slot time, then MO (GMOD.5) and MI (GMOD.6). If MO and Ml there will probably be a collision. The actions the re- equal 0,0 (normal backofi) then the minimum period ceiver does take when detecting a collision is to just before retransmission will be either the interframe stop receiving data if data has not been loaded into space or the backoff period, whichever is longer. If MO RFIFO or to stop reception, clear receiver enable and Ml equall,l (alternate backofi) then the minimum (REN) and set the receiver abort flag (RCABT - period before retransmission will be the interframe RSTAT.6). space plus the backoff period. Both of these are shown in Figure 3.4. Alternate backoff must be enabled if us- If deterministic resolution is used, the transmitting sta- ing deterministic resolution. If the GSC is not ready to tions go through pretty much the same process as in retransmit by the time its assigned slot becomes avail- normal back-off, except that the slots are predeter- able, the slot time is lost and the station must wait until mined. All the receivers go through the back-off algo- the collision resolution time period has passed. rithm and may only transmit during their assigned slot. Instead of waiting for the collision resolution to pass, the transmission could be aborted. The decision to 3.6.4 SUCCESSFUL ENDING OF abort is usually dependent on the number of stations on TRANSMISSIONS AND RECEPTIONS the link and how many collisions have already oc- In both CSMAlCD and SDLC modes, the TDN bit is curred. The number of collisions can be obtained by set and TEN cleared at the end of a successful trans- examining the register, TCDCNT. The abort is normal- mission. The end of the transmission occurs when the ly implemented by clearing TEN. The new transmis- TFIFO is empty and the last byte has been transmitted. sion begins by setting TEN and loading TFIFO. The In CSMAlCD the user should clear the TCDCNT reg- minimum amount of time available to initiate a retrans- ister after successful transmission. mission would be one interframe space period after the line is sensed as being idle. At the end of a successful reception, the RDN bit is set and GREN is cleared. The end of reception occurs As the number of stations approach 256 the probability when the EOF flag is detected by the GSC hardware. of a successful transmission decreases rapidly. If there 7-43 intel~ 83C152 HARDWARE DESCRIPTION 3.7 Register Descriptions The length includes the two bit Begin Of Frame (BOF) flag in CSMA/CD but does not include the SDLC flag. ADRO,1,2,3 (9SH, OASH, OBSH, OC5H) - Address In SDLC mode, the BOF is an SDLC flag, otherwise it Match Registers 0,1,2,3 - Contains the address match is two consecutive ones. Zero length is not compatible values which determines which data will be accepted as in CSMA/CD mode. The user software is responsible valid. In 8 bit addressing mode, a match with any of the for setting or clearing these bits. four registers will trigger acceptance. In 16 bit address- ing mode a match with ADR1:ADRO or ADR3:ADR2 GMOD.3 (CT) - CRC Type - If set, 32 bit AUTODIN- will be accepted. Addressing mode is determined in 11-32 is used. If cleared, 16 bit CRC-CCITT is used. GMOD (AL). The user software is responsible for setting or clearing this flag. AMSKO,1 (ODSH, OE5H) - Address Match Mask 0,1 - Identifies which bits in ADRO,I are "don't care" bits. GMOD.4 (AL) - Address Length - If set, 16 bit ad- Writing a one to a bit in AMSKO,1 masks out that dressing is used. If cleared, 8 bit addressing is used. In 8 corresponding bit in ADDRO,l. bit mode a match with any of the 4 address registers will be accepted (ADRO, ADRI, ADR2, ADR3). BAUD (94H) - GSC Baud Rate Generator - Contains "Don't Care" bits may be masked in ADRO and ADRI the value of the programmable baud rate. The data rate with AMSKO and AMSKI. In 16 bit mode, addresses will equal (frequency of the oscillator)/«BAUD + 1) are matched against "ADR1:ADRO" or "ADR3: X (8». Writing to BAUD actually stores the value in a ADR2". Again, "Don't Care" bits in ADR1:ADRO reload register. The reload register contents are copied can be masked in AMSKI :AMSKO. A received address into the BAUD register when the Baud register decre- of all ones will always be recognized in any mode. The ments to OOH. Reading BAUD yields the current timer user software is responsible for setting or clearing this value. A read during GSC operation will give a value flag. that may not be current because the timer could decre- ment between the time it is read by the CPU and by the GMOD.5,6 (MO,MI) - Mode Select - Two test modes, time the value is loaded into its destination. an optional "alternate backoff" mode, or normal back- off can be enabled with these two bits. The user soft- BKOFF (OC4H) - BackoffTimer - The backofftimer is ware is responsible for setting or clearing the mode bits. an eight bit count-down timer with a clock period equal to one slot time. The backoff time is used in the MI MO Mode CSMAlCD collision resolution algorithm. The user o 0 Normal software may read the timer but the value may be inval- o 1 Raw Transmit id as the timer is clocked asynchronously to the CPU. 1 0 Raw Receive Writing to OC4H will have no effect. 1 1 Alternate Backoff GMOD(84H) In raw receive mode, the receiver operates as normal 7 6543210 except that all the bytes following the BOF are loaded into the receive FIFO, including the CRC. The trans- I XTCLK I M1 I MO I AL I CT I PL1 I PLO I PR I mitter operates as normal. Figure 3.14. GMOD In raw transmit mode the transmit output is internally GMOD.O (PR) - Protocol - If set, SDLC protocols with connected to the receiver input. The internal connec- NRZI encoding and SDLC flags are used. If cleared, tion is not at the actual port pin, but inside the port CSMAlCD link access with Manchester encoding is latch. All data transmitted is done without a preamble, used. The user software is responsible for setting or flag or zero bit insertion, and without appending a clearing this flag. CRC. The receiver operates as normal. Zero bit dele- tion is performed. GMOD.l,2 (PLO,I) - Preamble length In alternate backoff mode the standard backoff process PLI PLO LENGTH (BITS) is modified so the the backoff is delayed until the end of o 0 0 the IFS. This should help to prevent collisions con- o 1 8 stantly happening because the IFS time is usually larger 1 0 32 than the slot time. 1 1 64 7-44 83C152 HARDWARE DESCRIPTION GMOD.7 (XTCLK) - External Transmit Clock - If set PCON(087H) an external IX clock is used for the transmitter. If 7654 3 210 cleared the internal baud rate generator provides the transmit clock. The input clock is applied to P1.3 ISMOolARSIREOIGARENIXRCLKIGFIENlpollOL I (T X C). The user software is responsible for setting or clearing this flag. External receive clock is enabled by PCON contains bits for power control, LSC control, setting PCON.3. DMA control, and GSC control. The bits used for the GSC are PCON.2, PCON.3, and PCON.4. IFS (OA4H) - Interframe Spacing - Determines the number of bit times separating transmitted frames in PCON.2 (GFIEN) - GSC Flag Idle Enable - Setting CSMA/CD and SDLC. A bit time is equal to l/baud GFIEN to a 1 caused idle flags to be generated between rate. Only even interframe space periods can be used. transmitted frames in SDLC mode. SDLC idle flags The number written into this register is divided by two consist of 01111110 flags creating the sequence and loaded in the most significant seven bits. Complete 01111110011111110 ...... 011111110. A possible side interframe space is obtained by counting this seven bit effect of enabling GFIEN is that the maximum possible number down to zero twice. A user software read of latency from writing to TFIFO until the first bit is this register will give a value where the seven most sig- transmitted increased from approximately 2 bit-times nificant bits gives the current count value and the least to around 8 bit-times. GFIEN has no effect with significant bit shows a one for the first count-down and CSMA/CD. a zero for the second count. The value read may not be valid as the timer is clocked in periods not necessarily PCON.3 (XRCLK) - GSC External Receive Clock En- associated with the CPU read of IFS. Loading this reg- able - Writing a 1 to XRCLK enables an external clock ister with zero results in 256 bit times. to be applied to pin 5 (Port 1.4). The external clock is used to determine when bits are loaded into the receiv- er. PCON.4 (GAREN) - GSC Auxiliary Receiver Enable Bit - This bit needs to be set to a 1 to enable the recep- tion of back-to-back SDLC frames. A bacK-to-back SDLC frame is when the EOF and BOF is shared be- Figure 3.15. MYSLOT tween two sequential frames intended for the same sta- tion on the link. If GAREN contains a 0 then the re- MYSLOT.O, 1, 2, 3, 4, 5 - Slot Address - The six ad- ceiver will be disabled upon reception of the EOF and dress bits choose 1 of 64 slot addresses. Address 63 has by the time user software re-enables the receiver the the highest priority and address 1 has the lowest. A first bit(s) may have already passed, in the case of back- value of zero will prevent a station from transmitting to-back frames. Setting GAREN to a 1, prevents the during the collision resolution period by waiting until receiver from being disabled by the EOF but GREN all the possible slot times have elapsed. The user soft- will be cleared and can be checked by user software to ware normally initializes this address in the operating determine that an EOF has been received. GAREN has software. no effect if the GSC is in CSMA/CD mode. MYSLOT.6 (DCR) - Deterministic Collision Resolu- PRBS (OE4H) - Pseudo-Random Binary Sequence - tion Algorithm - When set, the alternate collision reso- This register contains a pseudo-random number to be lution algorithm is selected. Retriggering of the IFS on used in the CSMA/CD backoff algorithm. The number reappearance of the carrier is also disabled. When using is generated by using a feedback shift register clocked this feature Alternate Backoff Mode must be selected by the CPU phase clocks. Writing all ones to the PRBS and several other registers must be initialized. User will freeze the value at all ones. Writing any other value software must initialize TCDCNT with the maximum to it will restart the PRBS generator. The PRBS is ini- number of slots that are most appropriate for a particu- tialized to all zero's during RESET. A read of location lar application. The PRBS register must be set to all OE4H will not necessarily give the seed used in the ones. This disables the PRBS by freezing it's contents at backoff algorithm because the PRBS counters are OFFH. The backoff timer is used to count down the clocked by internal CPU phase clocks. This means the number of slots based on the slot timer value setting the contents of the PRBS may have been altered between period of one slot. The user software is responsible for the time when the seed was generated and before a setting or clearing this flag. , READ has been internally executed. MYSLOT.7 (DCJ) - D.C. Jam - When set selects D.C. type jam, when clear, selects A.C. type jam. The user software is responsible for setting or clearing this flag. 7-45 83C152 HARDWARE DESCRIPTION RFIFO (OF4H) - Receive FIFO - RFIFO is a 3 byte RSTAT. 6 (RCABT) - Receiver Collision!Abort Detect buffer that is loaded each time the GSC receiver has a - If set. indicates that a collision was detected after data byte of data. Associated with RFIFO is a pointer that is had been loaded into the receive FIFO in CSMAlCD automatically updated with each read of the FIFO. A mode. In SDLC mode, RCABT indicates that 7 consec- read of RFIFO fetches the oldest data in the FIFO. utive ones were detected prior to the end flag but after data has been loaded into the receive FIFO. AE may RSTAT (OE8H) - Receive Status Register also be set. The setting of this flag is controlled by the 7654321 0 GSC. \oRIRCABTI AE ICRCEI RON\ RFNEIGRENI HABEN\ RSTAT.7 (OVR) - Overrun - If set, indicates that the receive FIFO was full and new shift register data was Figure 3.16. RSTAT written into it. AE and/or CRCE may also be set. The setting of this flag is controlled by the GSC and it is RSTAT.O (HABEN) - Hardware Based Acknowledge cleared by user software. Enable - If set, enables the hardware based acknowl- edge feature. The user software is responsible for setting SLOTTM (OBH) - Slot Time - Determines the length of or clearing this flag. the slot time used in CSMA/CD. A slot time equals (SLOTTM) X (1 / baud rate). A read of SLOTTM will RSTAT.l (GREN) - Receiver Enable - When set, the give the value of the slot time timer but the value may receiver is enabled to accept incoming frames. The user be invalid as the timer is clocked asynchronously to the must clear RFIFO with software before enabling the CPU. Loading SLOTTM with 0 results in 256 bit receiver. RFIFO is cleared by reading the contents of times. RFIFO until RFNE = O. After each read of RFIFO, it takes one machine cycle for the status of RFNE to be TCDCNT (OD4H) - Transmit Collision Detect Count - updated. Setting GREN also clears RDN, CRCE, AE, Contains the number of collisions that have occurred if and RCABT. GREN is cleared by hardware at the end probabilistic CSMA/CD is used. The user software of a reception or if any receive errors are detected. The must clear this register before transmitting a new frame user software is responsible for setting this flag and the so that the GSC backoff hardware can accurately dis- GSC or user software can clear it. The status ofGREN tinguish a new frame from a retransmit attempt. has no effect on whether the receiver detects a collision in CSMA/CD mode as the receiver input circuitry al- In deterministic backoff mode, TCDCNT is used to ways monitors the receive pin. hold the maximum number of slots. RSTAT.2 (RFNE) - Receive FIFO Not Empty - If set, TFIFO (85H) - GSC Transmit FIFO - TFIFO is a 3 indicates that the receive FIFO contains data. The re- byte buffer with an associated pointer that is automati- ceive FIFO is a three byte buffer into which the receive cally updated for each write by user software. Writing a data is loaded. A CPU read of the FIFO retrieves the byte to TFIFO loads the data into the next available oldest data and automatically updates the FIFO point- location in the transmit FIFO. Setting TEN clears the ers. Setting GREN to a one will clear the receive FIFO. transmit FIFO so the transmit FIFO should not be The status of this flag is controlled by the GSC. It is written to prior to setting TEN. If TEN is already set cleared if user empties receive FIFO. transmission begins as soon as data is written to TFI- FO. RSTAT.3 (RON) - Receive Done - If set, indicates the successful completion of a receiver operation. Will not TSTAT (OD8) - Transmit Status Register be set if a CRC, alignment, abort, or FIFO overrun 7 6543210 error occurred. The status of this flag is controlled by the GSC. I I LNI NOACK UR I TCDT I TON I TFNF TEN \ DMA \I RSTAT.4 (CRCE) - CRC Error - If set, indicates that a Figure 3.17. TSTAT properly aligned frame was received with a mismatched CRC. The status of this flag is controlled by the GSC. TSTAT.O (DMA) - DMA Select - If set, indicates that DMA channels are used to service the GSC FIFO's and RSTAT.5 (AE) - Alignment Error - In CSMA/CD GSC interrupts occur on TON and RDN, and also en- mode, AE is set if the receiver shift register (an internal ables UR to become set. If cleared, indicates that the serial-to-parallel converter) is not full and the CRC is GSC is operating in its normal mode and interrupts bad when an EOF is detected. In CSMA/CD the EOF occur on TFNF and RFNE. For more information on is a line idle condition (see LNI) for two bit times. If DMA servicing please refer to the DMA section on the CRC is correct while in CSMAlCD mode, AE is DMA serial demand mode (4.2.2.3). The user software not set and any mis-alignment is assumed to be caused is responsible for setting or clearing this flag. by dribble bits as the line went idle. In SDLC mode, AE is set if a non-byte-aligned flag is received. CRCE may also be set. The setting of this flag is controlled by the GSC. 7-46 intel~ 83C152 HARDWARE DESCRIPTION TSTAT.l (TEN) - Transmit Enable - When set causes 3.S Serial Backplane vs. Network TDN, UR, TCDT, and NOACK flag to be reset and Environment the TFIFO cleared. The transmitter will clear TEN af- ter a successful transmission, a collision during the The C152 GSC port is intended to fulfill the needs of data, CRC, or end flag. The user software is responsible both serial backplane environment and the serial com- for setting but the GSC or user software may clear this munication network environment. The serial backplane flag. If cleared during a transmission the GSC transmit is where typically, only processor to processor commu- pin goes to a steady state high level. This is the method nications take place within a self contained box. The used to send an abort character in SDLC. Also DEN is communication usually only encompasses those items forced to a high level. The end of transmission occurs which are necessary to accomplish the dedicated task whenever the TFIFO is emptied. for the box. In these types of applications there may not be a need for line drivers as the distance between the TSTAT.2 (TFNF) - Transmit FIFO not full - When transmitter and receiver is relatively short. The net- set, indicates that new data may be written into the work environment; however, usually requires transmis- transmit FIFO. The transmit FIFO is a three byte buff- sion of data over large distances and requires drivers er that loads the transmit shift register with data. The and/or repeaters to ensure the data is received on both status of this flag is controlled by the GSC. ends. TSTAT.3 (TDN) - Transmit Done - When set, indi- cates the successful completion of a frame transmission. 4.0 DMA Operation If HABEN is set, TDN will not be set until the end of the IFS following the transmitted message, so that the The C152 contains DMA (Direct Memory Accessing) acknowledge can be checked. If an acknowledge is ex- logic to perform high speed data transfers between any pected and not received, TDN is not set. An acknowl- two of edge is not expected following a broadcast or multi-cast packet. The status of this flag is controlled by the GSC. Internal Data RAM Internal SFRs TSTAT.4 (TCDn - Transmit Collision Detect - If set, External Data RAM indicates that the transmitter halted due to a collision. It is set if a collision occurs during the data or CRC or If external RAM is involved, the Port 2 and Port 0 ~ if there are more than eight collisions. The status of this are used as the address/data bus, and RD and WR flag is controlled by the GSC. signals are generated as required. TSTAT.5 (UR) - Underrun - If set, indicates that in Hardware is also implemented to generate a Hold Re- DMA mode the last bit was shifted out of the transmit quest signal and await a Hold Acknowledge response register and that the DMA byte count did not equal before commencing a DMA that involves external zero. When an underrun occurs, the transmitter halts RAM. without sending the CRC or the end flag. The status of this flag is controlled by the GSC. Alternatively, the HoldlHold Acknowledge hardware can be programmed to accept a Hold Request signal TSTAT.6 (NOACK) - No Acknowledge - If set, indi- from an external device and generate a Hold Acknowl- cates that no acknowledge was received for the previous edge signal in response, to indicate to the requesting frame. Will be set only if HABEN is set and no ac- device that the C152 will not commence a DMA to or knowledge is received prior to the end of the IFS. from external ~M while the Hold Request is active. NOACK is not set following a broadcast or a multi- cast packet. The status of this flag is controlled by the GSC. 4.1 DMA with the SOC152 TSTAT.7 (LNI) - Line Idle - If set, indicates the re- The C152 contains two identical general purpose 8-bit ceive line is idle. In SDLC protocol it is set if 15 consec- DMA channels with 16-bit addressability: DMAO and utive ones are received. In CSMAlCD protocol, line DMA1. DMA transfers can be executed by either chan- idle is set if GR X D remains high for approximately 1.6 nel independent of the other, but only by one channel at bit times. LNI is cleared after a transition on GR X D. a time. During the time that a DMA transfer is being The status of this flag is controlled by the GSC. executed, program execution is suspended. A DMA transfer takes one machine cycle (12 oscillator 7-47 infel~ 83C152 HARDWARE DESCRIPTION DMA CHANNEL 0 OMA CHANNEL 1 .I DARHO I I DARlO I, .I DARHl I I DARll I, DESTINATION ADDRESS DESTINATION ADDRESS .I SARHO I I SARLO I, SARHl I I SARll I , SOURCE ADDRESS SOURCE ADDRESS BCRHO I I BCRlO .I BCRHl I I BCRl1 I, BYTE COUNT BYTE COUNT I OCONO I I DCON1 I DMAO CONTROL Dt.tA 1 CONTROL PCON '--' \.. Two new bits in PCON control Hold/Hold Acknowledge logic 270427-28 Figure 4.1. DMA Registers periods) per byte transferred, except when the destina- Two other bits in DCONn specify the physical source tion and source are both in External Data RAM. In of the data to be transferred. These are SAS (Source that case the transfer takes two machine cycles per Address Space) and ISA (Increment Source Address). byte. The term DMA Cycle will be used to mean the If SAS = 0, the source is in data memory external to transfer of a single data byte, whether it takes 1 or 2 the C152. If SAS = 1, the source is internal. If SAS = machine cycles. 1 and ISA = 0, the internal source is an SFR. If SAS = 1 and ISA = 1, the internal source is in the 256-byte Associated with each channel are seven SFRs, shown in data RAM. Figure 4.1. SARLn and SARHn holds the low and high bytes of the source address. Taken together they form a In any case, ifISA = 1, the source address is automati- 16-bit Source Address Register. DARLn and DARHn cally incremented after each byte transfer. If ISA = 0, hold the low and high bytes of the destination address, it is not. and together form the Destination Address Register. BCRLn and BCRHn hold the low and high bytes of the The functions of these four control bits are summarized number of bytes to be transferred, and together form below: the Byte Count Register. DCONn contains control and flag bits. DAS IDA Destination Auto-Increment 0 0 External RAM no Two bits in DCONn are used to specify the physical 1 External RAM yes 0 destination of the data transfer. These bits are DAS 1 0 SFR no (Destination Address Space) and IDA (Increment Des- tination Address). If DAS = 0, the destination is in 1 1 Internal RAM yes data memory external to the C152. If DAS = 1, the SAS ISA Source Auto-Increment destination is internal to the C152. If DAS = 1 and IDA = 0, the internal destination is a Special Function 0 0 External RAM no Register (SFR). If DAS = 1 and IDA = 1, the inter- 0 1 External RAM yes nal destination is in the 256-byte data RAM. 1 0 SFR no 1 1 Internal RAM yes In any case, if IDA = 1, the destination address is automatically incremented after each byte transfer. If IDA = 0, it is not. 7-48 inteL 83C152 HARDWARE DESCRIPTION There are four modes in which the DMA channel can dress. On-chip hardware then clears the flag (RI, TI, operate. These are selected by the bits DM and TM RFNE, or TFNF) that initiated the DMA, and decre- (Demand Mode and Transfer Mode) in DCONn: ments BCRn. Note that since the flag that initiated the DMA is cleared, it will not generate an interrupt unless OM TM Operating Mode DMA servicing is held off or the byte count equals O. 0 0 Alternate Cycles Mode DMA servicing may be held off when alternate cycle is being used or by the status of the HOLDIHLDA logic. 0 1 Burst Mode In these situations the interrupt for the LSC may occur 1 0 Serial Port Demand Mode before the DMA can clear the RI or TI flag. This is 1 1 External Demand Mode because the LSC is serviced according to the status of RI and TI, whether or not the DMA channels are being The operating modes are described below. used for the transferring of data. The GSC does not use RFNE or TFNF flags when using the DMA channels so these do not need to be disabled. When using the 4.1.1 ALTERNATE CYCLE MODE DMA channels to service the LSC it is recommended that the interrupts (RI and TI) be disabled. If the dec- In Alternate Cycles Mode the DMA is initiated by set- remented BCRn is OOOOH, on-chip hardware then ting the GO bit in DCONn. Following the instruction clears the GO bit and sets the DONE bit. The DONE that set the GO bit, one more instruction is executed, bit flags an interrupt. and then the first data byte is transferred from the source address to the destination address. Then another instruction is executed, and then another byte of data is 4.1.4 EXTERNAL DEMAND MODE transferred, and so on in this manner. In External Demand Mode the DMA is initiated by Each time a data byte is transferred, BCRn (Byte one of the External Interrupt pins, provided the GO bit Count Register for DMA Channel n) is decremented. is set. INTO initiates a Channel 0 DMA, and INTI When it reaches ()()(x)H, on-chip hardware clears the initiates a Channell DMA. GO bit and sets the DONE bit, and the DMA ceases. The DONE bit flags an interrupt. If the external interrupt is configured to be transition- activated, then each I-to-O transition at the interrupt pin sets the corresponding external interrupt flag, and 4.1.2 BURST MODE generates one DMA Cycle. Then, BCRn is decrement- ed. No more DMA Cycles take place until another Burst Mode differs from Alternate Cycles mode only in I-to-O transition is seen at the external interrupt pin. If that once the data transfer has begun, program execu- the decremented BCRn = OOOOH, on-chip hardware tion is entirely suspended until BCRn reaches OOOOH, clears the GO bit and sets the DONE bit. If the exter- indicating that all data bytes that were to be transferred nal interrupt is enabled, it will be serviced. have been transferred. The interrupt control hardware remains active during the DMA, so interrupt flags may If the external interrupt is configured to be level-acti- get set, but since program execution is suspended, the vated, then DMA Cycles commence when the interrupt interrupts will not be serviced while the DMA is in pin is pulled low, and continue for as long as the pin is progress. held low and BCRn is not OOOOH. If BCRn reaches 0 while the interrupt pin is still low, the GO bit is cleared, 4.1.3 SERIAL PORT DEMAND MODE the DONE bit is set, and the DMA ceases. If the exter- nal interrupt is enabled, it will be serviced. In this mode the DMA can be used to service the Local Serial Channel (LSC) or the Global Serial Channel If the interrupt pin is pulled up before BCRn reaches (GSC). OOOOH, then the DMA ceases, but the GO bit is still 1 and the DONE bit is still O. An external interrupt is not In Serial Port Demand Mode the DMA is initiated by generated in this case, since in level-activated mode, any of the following conditions, if the GO bit is set: pulling the pin to a logical 1 clears the interrupt flag. If Source Address = SBUF .AND. RI = 1 the interrupt pin is then pulled low again, DMA trans- Destination Address = SBUF .AND. TI = 1 fers will continue from where they were previously stopped. Source Address = RFIFO .AND. RFNE = 1 Destination Address = TFIFO .AND. TFNF = 1 The timing for the DMA Cycle in the transition-acti- vated mode, or for the first DMA Cycle in the level-ac- Each time one of the above conditions is met, one tivated mode is as follows: If the I-to-O transition is DMA Cycle is executed; that is, one data byte is trans- ferred from the source address to the destination ad- 7-49 infel~ 83C152 HARDWARE DESCRIPTION detected before the final machine cycle of the instruc- and RD and/or WR signals are generated as needed, in tion in progress, then the DMA commences a..c; soon as the same manner as in the execution of a MOVX the instruction in progress is completed. Otherwise, one @DPTR instruction. more instruction will be executed before the DMA starts. No instruction is executed during any DMA Cy- cle. 4.3 Hold/Hold Acknowledge Two operating modes of HoldlHold Acknowledge log- 4.2 Timing Diagrams ic are available, and either or neither may be invoked by software. In one mode, the C152 generates a Hold Timing diagrams for single-byte DMA transfers are Request signal and awaits a Hold Acknowledge re- shown in Figures 4.2 through 4.5 for four kinds of sponse before commencing a DMA that involves exter- DMA Cycles: internal memory to internal memory, in- nal RAM. This is called the Requester Mode. ternal memory to external memory, external memory to internal memory, and external memory to external In the other mode, the C152 accepts a Hold Request memory. In each case we assume the C152 is executing signal from an external device and generates a Hold out of external program memory. If the C 152 is execut- Acknowledge signal in response, to indicate to the re- ing out of internal program memory, then PSEN is in- questing device that the C152 will not commence a active, and the Port 0 and Port 2 pins emit PO and P2 DMA to or from external RAM while the Hold Re- SFR data. If External Data Memory is involved, the quest is active. This is called the Arbiter mode. Port 0 and Port 2 pins are used as the address/data bus, 1-1- - - - - - 1 2 osc. PERIODS --------11 ALE ~_ _ _ _ _ _ _ _ _~;___\."'___ _ fflN--1 PO ~----------------------------------~---~ ~ _________________ _____________ !~Of! .l..\!:::.J~~!~ P2~~____________p_2_S_rR________~---JX~---PC-H----- t - - - - - - - D W A CYCLE-------t--RES~~~~~RAt.I 270427-29 Figure 4.2. DMA Transfer from Internal Memory to Internal Memory 1-1- - - - - - 1 2 osc. PERIODS - - - - - 1 ALE~~_ _ _ _ _ _ _ _ _r-\~ ___ fflN--1 '---- PO INST : : ) DARLn X Dt.lA DATA OUT X XPCL :::~INST P2~______________D_A_RH_n_____________JX~___ PC_H_____ WR \~ ______I 1 - - - - - - - O t . l A CYCLE _ _ _ _ _ _~I,-RES~E~~~g~RAt.I 270427-30 Figure 4.3. DMA Transfer from Internal Memory to External Memory 7-50 intaL 83C152 HARDWARE DESCRIPTION 1-1- - - - - 1 2 OSC. PERIODS -------11 ALE . - J \___________r-\'-___ mN--./ PO INST ::l SARln : : : : : : :~l~~:::::::X DATA IN ::€~Of!:X PCl :::lINST P2~______________ SA_R_Hn____________~X~ PC_H_____ __ iffi '''''' _ _ _ _ _..J1 I1------ Dt.lA CYCLE _ _ _ _ _ _-1-1_ RES~E~~~g~RAt.l 270427-31 Figure 4.4. DMA Transfer from External Memory to Internal Memory 1 ' " ' - - - - 1 2 OSC. PERIODS - - - - - r - - - - - - - 1 2 OSC. PERIODS - - - - 1 ALE PO ~:]SARln::::::!~O!f:::)DATAIN:!~~!T~_ _ _D_AT_A_O_UT_ _ _>CE1::~ P2~~__________ SA_R_Hn__________JX~ __________D_A_RH_n________X~__PC_H___ ''''''_ _ _ _ _...J 1 , _____--1 1 - - - - - - - - - - D t . l A CYClE-----------~II-RES~E~~~g~RAt.l 270427-32 Figure 4.5. DMA Transfer from External Memory to External Memory 4.3.1 REQUESTER MODE 4.3.2 ARBITER MODE The Requester Mode is selected by setting the control For DMAs that are to be driven by some device other bit REQ, which resides in PCON. In that mode, when than the C152, a different version of the HoldlHold the C152 wants to do a DMA to External Data Memo- Acknowledge protocol is available. In this version, the ry, it first generates a Hold Request signal, HLD, and device which is to drive the DMA sends a Hold Re- waits for a Hold Acknowledge signal, HLDA, before quest signal, HLD, to the C152. If the C152 is current- commencing the DMA operation. Note that program ly performing a DMA to or from External Data Memo- execution continues while HLDA is awaited. The ry, it will complete this DMA before responding to the DMA is not begun until a logical 0 is detected at the Hold Request. When the C152 responds to the Hold HLDA pin. Then, once the DMA has begun, it goes to Request, it does so by activating a Hold Acknowledge completion regardless of the logic level at HLDA. signal, HLDA. This indicates that the C152 will not commence a new DMA to or from External Data The protocol is activated only for DMAs (not for pro- Memory while HLD remains active. gram fetches or MOVX operations), and only for DMAs to or from External Data Memory. If the data Note that in the Arbiter Mode the C152 does not sus- destination and source are both internal to the C152, pend program execution at all, even if it is executing the HLDIHLDA protocol is not used. from external program memory. It does not surrender use of its own bus. The HLD output is an alternate function of port pin P1.5, and the HLDA input is an alternate function of The Hold Request input, HLD, is at P1.5. The Hold port pin P1.6. Acknowledge output, HLDA, is at P1.6. This 7-51 83C152 HARDWARE DESCRIPTION version of the HoldlHold Acknowledge feature is se- es are done only through DMA operations, not by lected by setting the control bit ARB in PCON. MOVX instructions. The functions of the ARB and REQ bits in PCON, One CPU is programmed to be the Arbiter and the then, are other, to be the Requester. The ALE Switch selects which CPU's ALE signal will be directed to the address ARB REQ Hold/Hold Acknowledge Logic latch. The Arbiter's ALE is selected if HLDA is high, 0 0 Disabled and the Requester's ALE is selected if HLDA is low. 0 1 C152 generates HLD, detects HLDA 1 0 C152 detects HLD, generates HLDA 1 1 Invalid ALE (ARB) 4.3.3 USING THE HOLD/HOLD ACKNOWLEDGE IF' HiJjA =1 ALE (REO) IF' RIllA =0 The HOLDIHOLDA logic only affects DMA opera- tion with external RAM and doesn't affect other opera- tions with external RAM, such as MOVX instruction. 270427-34 Figure 4.6 shows a system in which two 83C152s are sharing a global RAM. In this system, both CPUs are Figure 4.7. ALE Switch Select executing from internal ROM. Neither CPU uses the bus except to access the shared RAM, and such access- The ALE Switch logic can be implemented by a single 74HCOO, as shown in Figure 4.7. '--------+l0E '----------+1 WE 270427-33 Figure 4.6. Two 83C152s Sharing External RAM 7-52 83C152 HARDWARE DESCRIPTION 4.3.4 INTERNAL LOGIC OF THE ARBITER When the arbiter wants to DMA the XRAM, it first activates DMXRQ. This signal prevents Q2 from being The internal logic of the arbiter is shown in Figure 4.8. set if it is not already set. An output low from Q2 en- In operation an input low at HLD sets Q2 if the arbi- ables the arbiter to carry out its DMA to XRAM, and ter's internal signal DMXRQ is low. DMXRQ is the maintains an output high at HLDA. When the arbiter arbiter's "DMA to XRAM Request". Setting Q2 acti- completes its DMA, the signal DMXRQ goes to 0, vates HLDA through Q3. Q2 being set also disables which enables Q2 to accept signals from the HLD input any DMAs to XRAM that the arbiter might decide to again. do during the requester's DMA. Figure 4.9 shows the minimum response time, 4 to 7 CPU oscillator periods, between a transition at the HLD input and the response at HLDA. Inhibit Arbiter's DMXRQ DMA to XRAM HLD Input (Pl.5) HLDA Output (P1.6) Clock 1 Clock 2 Clock 1 270427-39 Figure 4.8. Internal Logic of the Arbiter 7-53 83C152 HARDWARE DESCRIPTION HLD ;nput ~~ I \oUo~o- - - - - - - I CPU Osc. Periods IIIIIItIIIIIIIIIIIII I I Clock 1 Clock 2 HLDA Output • III II 2 Osc. 0 "Osc. 0 Periods Periods 270427.,.40 Figure 4.9. Minimum HLD/HLDA Response Time Inhibit Requester's DMXRQ DMA to XRAM HLDA Input (P1.6) Clock 1 Clock 2 270427-41 Figure 4.10. Internal Logic of the Requester (Clock 1 and Clock 2 are Shown in Figure 4.9) 7-54 intel~ 83C152 HARDWARE DESCRIPTION 4.3.5 Internal Logic of the Requester the request and receive another acknowledge before an- other DMA cycle to XRAM can proceed. Obviously in The internal logic of the requester is shown in Figure this case, the "alternate cycles" mode may consist of 4.10. Initially, the requester's internal signal DMXRQ single DMA cycles separated by any number of instruc- (DMA to XRAM Request) is at 0, so Q2 is set and the tion cycles, depending on how long it takes the request- HLD output is high. As long as Q2 stays set, the re- er to regain the bus. quester is inhibited from starting any DMA to XRAM. A channel 1 DMA in progress will always be overrid- When the requester wants to DMA the XRAM, it first den by a DMA request of any kind from channel O. If a activates DMXRQ. This signal enables Q2 to be cleared channel 1 DMA to XRAM is in progress and is over- (but doesn't clear it), and, if HLDA is high, also acti- ridden by a channel 0 DMA which does not require the vates the HLD output. bus, DMXRQ will~ 0 during the channel 0 DMA, thus de-activating HLD. Again, the requester must re- A I-to-O transition from HLDA can now clear Q2, new its request for the bus, and must receive a new 1- which will enable the requester to commence its DMA to-O transition in HLDA before channel 1 can continue to XRAM. Q2 being low also maintains an output low its DMA to XRAM. at HLD. When the DMA is completed, DMXRQ goes to 0, which sets Q2 and de-activates HLD. 4.4 DMA Arbitration Only DMXRQ going to 0 can set Q2. That means once Q2 gets cleared, enabling the requester's DMA to pro- The DMA Arbitration described in this section is not ceed, the arbiter has no way to stop the requester's arbitration between two devices wanting to access a DMA in progress. At this point, de-activating HLDA shared RAM, but on-chip arbitration between the two will have no effect on the requester's use of the bus. DMA channels on the 8XC152. Only the requester itself can stop the DMA in progress, and when it does, it de-activates both DMXRQ and The 8XC152 provides two DMA channels, either of HLD. which may be called into operation at any time in re- sponse to real time conditions in the application circuit. If the DMA is in alternate cycles mode, then each time Since a DMA cycle always uses the 8XC152's internal a DMA cycle is completed DMXRQ goes to 0, thus de- bus, and there's only one internal bus, only one DMA activating HLD. Once HLD has been de-activated, it channel can be serviced during a single DMA cycle. can't be re-asserted till after HLDA has been seen to go Executing program instructions also requires the inter- high (through flip-flop QIA). Thus every time the nal bus, so program execution will also be suspended in DMA is suspended to allow an instruction cycle to pro- order for a DMA to take place. ceed, the requester gives up the bus and must renew 270427-42 Figure 4.11. Internal Bus Usage 7-55 iniaL 83C152 HARDWARE DESCRIPTION Figure 4.11 shows the three tasks to which the internal The return value is based on the condition of the GO bus of the 8XC152 can be dedicated, In this figure, bit for e.ach channel; and on the value returned by an- Instruction Cycle means the complete execution of a other function, named mode~ogic (). The algorithm single instruction, whether it takes 1, 2 or 4 machine for mode~ogic () is the same for both channels. The cycles. DMA Cycle means the transfer of a single data function is shown in Figure 4.13 as a pseudo-HLL byte from source to destination, whether it takes 1 or 2 function, mode~ogic (n), where n = 0 when the func- machine cycles. Each time a DMA Cycle or an Instruc- tion is invoked for DMA channel 0, and n = 1 when tion Cycle is executed, on-chip arbitration logic deter- it's invoked for DMA channell. The value returned by mines which type of cycle is to be executed next. this function is either 0 or 1, and will be passed on to the DMA arbitration logic in Figure 4.12. Note that when an instruction is executed, if the in- struction wrote to a DMA register (defined in Figure Note that the arbitration logic as shown in Figure 4.12 4.1 but excluding PCON), then another instruction is always gives precedence to channel 0 over channell. If executed without further arbitration. Therefore, a sin- GOO is set and mode_logic (0) returns ai, then a gle write or a series of writes to DMA registers will DMAO cycle is called without further reference to the prevent a DMA from taking place, and will continue to situation in channell. That is not to say a DMAI Cy- prevent a DMA from taking place until at least one cle will be interrupted once it has begun. Once a cycle instruction is executed which does not write to any has begun, be it an Instruction Cycle or a DMA Cycle, DMA register. it will be completed without interruption. The logic that determines whether the next cycle will be The statements in mode~ogic (n), Figure 4.13, are ex- a DMAO cycle, a DMAI cycle, or an Instruction Cycle ecuted sequentially until an "if' condition, based on the is shown in Figure 4.12 as a pseudo-HLL function. The DMA mode programmed into DCONn, is satisfied. statements in Figure 4.12 are executed sequentially un- For example, if the channel is configured to Burst less an "if' condition is satisfied, in which case the cor- mode, then the first if-condition is satisfied, so the "re- responding "return" is executed and the remainder of turn 1" expression is executed and the remainder of the the function is not. The return value of 0, 1, or 2 is function is not. passed to the arbitration logic block in Figure 4.11 to determine which exit path from the block is used. arbitration_logic: if' (GOO 1 .AND. mode_logic(O) 1) return 0; if' (GOl 1 .AND. mode_logic(l) 1) return 1; else return 2; end arbitration_logic; Figure 4.12. DMA Arbitration Logic 7-56 intel~ 83C152 HARDWARE DESCRIPTION mode_logic(n) : if (DCONn indicates burst_mode) return 1; if (DCONn indicates extern_demand_mode) if (demand_flag =1) return 1; else return 0; if (DCONn indicates SP_demand_mode) if (SARn = SBUF .AND. RI = 1) return 1; if (DARn = SBUF .AND. TI = 1) return 1; if (SARn = RFIFO .AND. RFNE = 1) return 1 ; if (DARn = TFIFO .AND. TFNF = 1 .AND. previous_cycle = instruction_cycle) return l', else return 0; if (DCONn indicates alt_cycles_mode) if (DCONm indicates .NOT. alt_cycles_mode .OR. GOm = 0) if (previous_cycle = instruction_cycle) return 1; else return 0; if (previous_cycle = instruction_cycle .AND. previous_dma_cycle = .NOT. DMAn) return 1; return 0; end mode_logic(n) ; Figure 4.13. DMA Mode Logic 7-57 inteL 83C152 HARDWARE DESCRIPTION If the channel is configured to External Demand mode, For example, consider the situation where channel 0 is then the first if-condition is not satisfied but the second configured to service TFIFO and channel 1 is config- one is. In that case the block of statements following ured to Alternate Cycles mode. Then DMAs to TFIFO that if-condition and delimited by {... l is executed: if will always override the alternate cycles of channel 1. If the demand flag (lEO for channel 0 and lEI for chan- TFIFO needs more than 1 byte it will receive them in nel I) is set, the "return I" expression is executed and precendence over channell, but each DMA to TFIFO the remainder of the function is not. If the demand flag must be preceded by an Instruction cycle. The sequence is not set, the "return 0" expression is executed and the of cycles might be: remainder of the function is not. DMA1 cycle If the channel is configured to Serial Port Demand Instruction cycle mode, the source and destination addresses, SARn and DMA1 cycle, during which TFNF gets set DARn, have to be checked to see which Serial Port Instruction cycle buffer is being addressed, and whether its demand flag DMAO cycle is set. Instruction cycle DMAO cycle, as a result of which TFNF gets cleared SARn refers to the I6-bit source address for "this chan- Instruction cycle nel." Note that the condition DMA1 cycle Instruction cycle SARn = SBUP DMA1 cycle cannot be true unless the SAS and ISA bits in DCONn .Instruction cycle are configured to select SFR space. If SARn is numeri- cally equal to the address of SBUF (99H), and SAS and ISA are configured to select internal RAM rather than The requirement that a DMA to TFIFO be preceded SFR space, then SARn refers to location 99H in the by an Instruction cycle can result in the normal prece- "upper 128" of internal RAM, not to SBUF. dence of channel 0 over channel 1 being thwarted. Con- sider for example the situation where channel 0 is con- If the test for SARn = SBUF is true, and if the flag RI figured to service TFIFO, and is in the process of doing is set, mode_logic (n) returns as 1 and the remainder so, and channel 1 decides it wants to do a Burst mode of the function is not executed. Otherwise, execution DMA. The sequence of events might be: proceeds to the next if-condition, testing DARn against SBUF and T1 against 1. Instruction cycle (sets GO bit in DCON1) Instruction cycle (during which TFNF gets set) The same considerations regarding SAS and ISA in the DMAO cycle SARn test are now applied to DAS and IDA in the DMA1 cycle DARn test. If SFR space isn't selected, no Serial Port DMA1 cycle buffer is being addressed. DMAI cycle Note that if DMA channel n is configured to Alternate DMAI cycle (completes channell burst) Cycles mode, the logic must examine the other DCON Instruction cycle register, DCONm, to determine if the other channel is DMAO cycle also configured to Alternate Cycles mode and whether Instruction cycle its GO bit is set. In Figure 4.13, the symbol DCONn refers to the DCON register for "this channel," and DCONm refers to "the other channel." This sequence begins with two Instruction cycles. The first one accesses a DMA register (DCON1), and there- A careful examination of the logic in Figure 4.13 will fore is followed by another Instruction cycle, which reveal some idiosyncracies that the user should be presumably does not access a DMA register. After the aware of. First, the logic allows sequential DMA cycles second Instruction cycle both channels are ready to to be generated to service RFIFO, but not to service generate DMA cycles, and channel 0 of course takes TFIFO. This idiosyncracy is due to internal timing precedence. After the DMAO cycle, channel 0 must conflicts, and results in each individual DMA cycle to wait for an Instruction cycle before it can access TFIFO having to be immediately preceded by an In- TFIFO again. Channell, being in Burst mode, doesn't struction cycle. The logic disallows that there be two have that restriction, and is therefore granted a DMA1 DMAs to TFIFO in a row. cycle. After the first DMA1 cycle, channel 0 is still waiting for an Instruction cycle and channel 1 still does If the user is unaware of this idiosyncracy, it can cause not have that restriction. There follows another DMA1 problems in situations where one DMA channel is serv- cycle. icing TFIFO and the other is configured to a complete- ly different mode of operation. 7-58 inteL 83C152 HARDWARE DESCRIPTION ° The result is that in this particular case channel has to wait until channell completes its Burst mode DMA, Function Register (SFR). If DAS = 1 and IDA = 1, the destination is in Internal Data RAM. and then has to wait for an Instruction cycle to be gen- erated, before it can continue its own DMA to TFIFO. IDA (Increment Destination Address) If IDA = 1, the The delay in servicing TFIFO can cause an Underflow destination address is automatically incremented after condition in the GSC transmission. each byte transfer. If IDA = 0, it is not. The delay will not occur if channel 1 is configured to SAS specifies the Source Address Space. If SAS = 0, ° Alternate Cycles mode, since channel would then see the Instruction cycles it needs to complete its logic re- the source is in External Data Memory. If SAS = 1 and ISA = 0, the source is an SFR. If SAS = 1 and quirements for asserting its request. ISA = 1, the source is Internal Data RAM. ISA (Increment Source Address) If ISA = 1, the 4.4.1 DMA Arbitration with Hold/Hold Ack source address is automatically incremented after each The HoldlHold Acknowledge feature is invoked by set- byte transfer. If ISA = 0, it is not. ting either the ARB or REQ bit in PCON. Their effect is to add the requirements of the HoldlHold Ack pro- DM (Demand Mode) If DM = 1, the DMA Channel tocol to mode~ogic (). This amounts to replacing ev- operates in Demand Mode. In Demand Mode the ery expression "return 1" in Figure 4.13 with the ex- DMA is initiated either by an external signal or by a pression "return hld_hlda~ogic ( )", where Serial Port flag, depending on the value of the TM bit. If DM = 0, the DMA is requested by setting the GO ° hld_hlda_logic ( ) is a function which returns 1 if the HoldlHold Ack protocol is satisfied, and returns oth- erwise. A suitable definition for hld_hlda_logic ( ) is bit in software. shown in Figure 4.14. TM (Transfer Mode) If DM = 1 then TM selects whether a DMA is initiated by an external signal (TM = 1) or by a Serial Port flag (TM = 0). If DM = then TM selects whether the data transfers are to be in ° 4.5 Summary of DMA Control Bits bursts (TM = 1) or in alternate cycles (TM = 0). DeONn I DAS I IDA I SAS liSA I OM I TM I DONE I GO I DONE indicates the completion of a DMA operation DAS specifies the Destination Address Space. If DAS = 0, the destination is in External Data Memory. If DAS = 1 and IDA = 0, the destination is a Special ° and flags an interrupt. It is set to I by on-chip hardware when BCRn = 0, and is cleared to by on-chip hard- ware when the interrupt is vectored to. It can also be set or cleared by software. hold_holda( ): if (ARB=° .AND. REQ =0) return 1; if SARn =!RAM • OR. DARn =!RAM) if (ARB =1 .AND. HLDA =1) return 1; if (REQ = 1 .AND. HLDA = 0) return 1; else return 0; return 1; end hold_holda( ); Figure 4.14. Hold/Hold Acknowledge Logic as a Pseudo-HLL Function 7-59 intel~ 83C152 HARDWARE DESCRIPTION GO is the enable bit for the DMA Channel itself. The Note that setting the DMA bit does not itself configure DMA Channel is inactive if GO = 0, the DM. .A.. channels to service the esc. That job must be done by software writes to the DMA registers. The I peON SMOD I ARB I REO I GAR EN I XRCLK I GFIEN I PDN IIDL I DMA bit only selects whether the GSCRV and GSCTV interrupts are flagged by a FIFO needing serv- ARB enables the DMA logic to detect HLD and gener- ice or by an "operation done" signal. ate HLDA. After it has activated HLDA, the C152 will not begin a new DMA to or from External Data Mem- The Receive and Transmit Error interrupt flags are ory as long as HLD is seen to be active. This logic is generated by the logical OR of a number of error condi- disabled when ARB = 0, and enabled when ARB = 1. tions, which are described in Section 3.6.5. REQ enables the DMA logic to generate HLD and de- Each interrupt is assigned a fixed location in Program tect HLDA before performing a DMA to or from Ex- Memory, and the interrupt causes the CPU to jump to ternal Data Memory. After it has activated HLD, the that location. All the interrupt flags are sampled at C152 will not begin the DMA until HLDA is seen to be S5P2 of every machine cycle, and then the samples are active. This logic is disabled when REQ = 0, and en- sequentially polled during the next machine cycle. If abled when REQ = 1. more than one interrupt of the same priority is active, the one that is highest in the polling sequence is serv- iced first. The interrupts and their fixed locations in 5.0 INTERRUPT STRUCTURE Program Memory are listed below in the order of their polling sequence. The 8XC152 retains all five interrupts of the 80C51BH. Six new interrupts are added in the 8XC152, to support its GSC and the DMA features. They are as listed be- RFNE Dt.4A=O low, and the flags that generate them are shown in Fig- J ure 5.1. ~GSCRV GSCRV - GSC Receive Valid RON ---fDt.4A = 1 270427-43 GSCRE - GSC Receive Error GSCTV - GSC Transmit Valid GSCTE - GSC Transmit Error CRCE~ DMAO - DMA Channel 0 Done AE GSCRE RCABT DMAI - DMA Channell Done OVR 270427-44 As shown in Figure 5.1, the Receive Valid interrupt can be signaled either by the RFNE flag (Receive FIFO TFNF Dt.4A=O Not Empty), or by the RDN flag (Receive Done). Which one of these flags causes the interrupt depends J on the setting of the DMA bit in the SFR named ~GSCTV TSTAT. TON ---f Dt.4A = 1 270427-45 DMA = 0 means the DMA hardware is not config- ured to service the GSC, so the CPU will service it in TCDT~ software in response to the Receive FIFO not being UR GSCTE NOACK empty. In that case, RFNE generates the Receive Valid 270427-46 interrupt. DMA = 1 means the DMA hardware is configured to DONE _ _ _.~ Dt.4AO (DCONO.1) service the GSC, in which case the CPU need not be 270427-47 interrupted till the receive is complete. In that case, RDN generates the Receive Valid interrupt. DONE .. (DCON1.1) - - -... DMA1 270427-48 Similarly the Transmit Valid interrupt can be signaled either by the TFNF flag (Transmit FIFO Not Full), or Figure 5.1. Six New Interrupts in the 8XC152 by the TDN flag (Transmit Done), depending on whether the DMA bit is 0 or 1. 7-60 int:eL 83C152 HARDWARE DESCRIPTION The two Interrupt Priority registers in the 8XC152 are Interrupt Location Name as follows: lEO 0003H External Interrupt 0 76543210 GSCRV TFO 002BH OOOBH GSC Receive Valid Timer 0 Overflow IP: 1-1-1-1 PS I PT1 I PX1 I PTO I PXO I GSCRE 0033H GSC Receive Error DMAO 003BH Address of IP in SFR space = OB8H (bit-addressable) DMA Channel 0 Done IE1 0013H External Interrupt 1 7654321 0 GSCTV 0043H GSC Transmit Valid IPN1 :lddpGSTElpDMA11PGSTVlpDMAOlpGSRElpGSR9 DMA1 0053H DMA Channel 1 Done TF1 001BH Timer 1 Overflow GSCTE Address of IPNI in SFR space = OF8H (bit-address- 004BH GSC Transmit Error able) TI+RI 0023H UART Transmit/Receive The bits in IP are unchanged from the standard 8051 Note that the locations of the basic 8051 interrupts are IP register. The bits in IPNI are as follows: the same as in the rest of the MCS-51 Family. And PGSTE = 1 GSC Transmit Error Interrupt Priority relative to each other they retain their same positions in to High the polling sequence. = 0 Priority to Low PDMAI = 1 DMA Channell Done Interrupt Priori- The locations of the new interrupts all follow the loca- ty to High tions of the basic 8051 interrupts in Program Memory, = 0 Priority to Low but they are interleaved with them in the polling se- PGSTV = 1 GSC Transmit Valid Interrupt Priority quence. to High = 0 Priority to Low To support the new interrupts a second Interrupt En- PDMAO = 1 DMA Channel 0 Done Interrupt Priori- able register and a second Interrupt Priority register are ty to High implemented in bit-addressable SFR space. The two In- = 0 Priority to Low terrupt Enable registers in the 8XC152 are as follows: PGSRE = 1 GSC Receive Error Interrupt Priority to 76543210 High = 0 Priority to Low IE: I EA I- I-I ES I ET1 I EX1 I ETO I EXO I PGSRV = 1 GSC Receive Valid Interrupt Priority to High Address of IE in SFR space = OA8H (bit-addressable) = 0 Priority to Low 7654321 0 Note that these registers all have unimplemented bits IEN1 :lddEGSTEIEDMA 11EGsTVlEDMAOIEGSREIEGSR9 ("-"). If these bits are read, they will return unpredict- able values. If they are written to, the value written goes nowhere. Address pF lEI in SFR space = OC8H (bit-address- able) It is recommended that user software should never write Is to unimplemented bits in MCS-51 devices. Fu- The bits in IE are unchanged from the standard 8051 ture versions of the device may have new bits installed IE register. The bits in IENI are as follows: in these locations. If so, their reset value will be O. Old EGSTE = 1 Enable GSC Transmit Error Interrupt software that writes Is to newly implemented bits may = 0 Disable unexpectedly invoke new features. EDMAI = 1 Enable DMA Channel 1 Done Interrupt = 0 Disable The MCS-51 interrupt structure provides hardware EGSTV = 1 Enable GSC Transmit Valid Interrupt support for only two priority levels, High and Low. = 0 Disable With as many interrupt sources as the 8XC152 has, it EDMAO = 1 Enable DMA Channel 0 Done Interrupt may be helpful to know how to augment the priority = 0 Disable structure in software. Any number of priority levels can EGSRE = 1 Enable GSC Receive Error Interrupt be implemented in software by saving and redefining = 0 Disable the interrupt enable registers within the interrupt serv- EGSRV = 1 Enable GSC Receive Valid Interrupt ice routines. The technique is described in the "MCS- = 0 Disable 51" Architectural Overview" chapter in this handbook. 7-61 83C152 HARDWARE DESCRIPTION 5.1 GSC Transmitter Error Conditions The TCDT bit can get set only if the GSC is configured to CSMA!CD mock In that case, the GSC hardware The GSC Transmitter section reports three kinds of sets TCDT when a collision is detected during a trans- error conditions: mission, and the collision was detected after TFIFO has TCDT - Transmitter Collision Detector been accessed. Also, the GSC hardware sets TCDT when a detected collision causes the TCDCNT register UR - Underrun in Transmit FIFO to overflow. NOACK - No Acknowledge The UR bit can get set only if the DMA bit in TSTAT These bits reside in the TSTAT register. User software is set. The DMA bit being set informs the GSC hard- can read them, but only the GSC hardware can write to ware that TFIFO is being serviced by DMA. In that them. The GSC hardware will set them in response to case, if the GSC goes to fetch another byte from TFIFO the various error conditions that they represent. When and finds it empty, and the byte count register of the user software sets the TEN bit, the GSC hardware will DMA channel servicing TFIFO is not zero, it sets the at that time clear these flags. This is the only way these UR bit. flags can be cleared. If the DMA hardware is not being used to service The logical OR of these three bits flags the GSC Trans- TFIFO, the UR bit cannot get set. If the DMA bit is 0, mit Error interrupt (GSCTE) and clears the TEN bit, then when the GSC finds TFIFO empty, it assumes as shown in Figure 5.2. Thus any detected error condi- that the transmission of data is complete and the trans- tion aborts the transmission. No CRC bits are transmit- mission of CRC bits can begin. ted. In SDLC mode, no EOF flag is generated. In CSMA/CD mode, an EOF is generated by default, The NOACK bit is functional only in CSMAlCD since the GTXD pin is pulled to a logic 1 and held mode, and only when the HABEN bit in RSTAT is set. there. The HABEN bit turns on the Hardware Based Ac- knowledge feature, as described in Section 3.2.6. If this feature is not invoked, the NOACK bit will stay at o. N;:~:~ TEN ED: . GSCTE ~': _ Set TRANSMIT I -.....-----~ TON EOF' 270427-49 Figure 5.2. Transmit Error Flags (Logic for Clearing TEN, Setting TON) 7-62 iniaL 83C152 HARDWARE DESCRIPTION l-----t~-----. GSCRE \-...._ _ _ _ _.... Set RON 270427-50 Figure 5.3. Receive Error Flag (Logic for Clearing GREN, setting RON) If the NOACK bit gets set, it means the GSC has com- The logical OR of these four bits flags the GSC Receive pleted a transmission, and was expecting to receive a Error interrupt (GSCRE) and clears the GREN bit, as hardware based acknowledge from the receiver of the shown in Figure 5.3. Note in this figure that any error message, but did not receive the acknowledge, or at condition will prevent RDN from being set. least did not receive it cleanly. There are three ways the NOACK bit can get set: A CRC Error means the CRC generator did not come 1. The acknowledge signal (an unattached preamble) to its correct value after calculating the CRC of the was not received before the IFS was completed. message plus received CRC. An Alignment Error means the number of bits received between the BOF 2. A collision was detected during the IFS. and EOF was not a mUltiple of 8. 3. The line was active during the last bit-time of the IFS. In SOLC mode, the CRCE bit gets set at the end of any frame in which there is a CRC Error, and the AE bit The first condition is an obvious reason for setting the gets set at the end of any frame in which there is an NOACK bit, since that's what the hardware based ac- Alignment Error. knowledge is for. The other two ways the NOACK bit can get set are to guard against the possibility that the In CSMAICO mode, if there is no CRC Error, neither transmitting station might mistake an unrelated trans- CRCE nor AE will get set. If there is a CRC Error and mission or transmission fragment for an acknowledge no Alignment Error, the CRCE bit will get set, but not signal. the AE bit. If there is both a CRC Error and an Align- ment Error, the AE bit will get set, but not the CRCE bit. Thus in CSMAlCO mode, the CRCE and AE bits 5.2 GSC Receiver Error Conditions are mutually exclusive. The GSC Receiver section reports four kinds of error The Receive Abort flag, RCABT, gets set if an incom- conditions: ing frame was interrupted after received data had al- CRCE - CRC Error ready passed to the Receive FIFO. In SOLC mode, this can happen if a line idle condition is detected before an AE - Alignment Error EOF flag is. In CSMAlCO mode, it can happen if RCABT - Receive Abort there is a collision. In either case, the CPU will have to OVR - Overrun in Receive FIFO re-initialize whatever pointers and counters it might have been using. These bits reside in the RSTAT register. User software can read them, but only the GSC hardware can write to The Overrun Error flag, OVR, gets set if the GSC Re- them. The GSC hardware will set them in response to ceiver is ready to push a newly received byte onto the the various error conditions that they represent. When Receive FIFO, but the FIFO is full. user software sets the GREN bit, the GSC hardware will at that time clear these flags. This is the only way Up to 7 "dribble bits" can be received after the EOF these flags can be cleared. without causing an error condition. 7-63 83C152 HARDWARE DESCRIPTION 6.0 GLOSSARY DAS - Destination Address Space, see DCON. ADRO,I,2,3 (9SH, OASH, OBSH, OCSH) - Address DCJ - D.C. Jam, see MYSLOT. Match Registers 0,1,2,3 - The contents of these SFRs are compared against the address bits from the serial DCONO/l (092H,093H) data on the GSC. If the address matches the SFR, then 7654321 0 the C IS2 accepts that frame. If in 8 bit addressing mode, a match with any of the four registers will trigger acceptance. In 16 bit addressing mode, a match with ADRl:ADRO or ADR3:ADR2 will be accepted. Ad- The DCON registers control the operation of the DMA dress length is determined by GMOD (AL). channels by determining the source of data to be trans- ferred, the destination of the data to be transfer, and the AE - Alignment Error, see RSTAT. various modes of operation. AL - Address Length, see GMOD. DCON.O (GO) - Enables DMA Transfer - When set it AMSKO,1 (ODSH, OESH) - Address Match Mask 0,1 - enables a DMA channel. If block mode is set then Identifies which bits in ADRO,1 are "don't care" bits. DMA transfer starts as soon as possible under CPU Setting a bit to 1 in AMSKO,1 identifies the corre- control. If demand mode is set then DMA transfer sponding bit in ADDRO,1 as not to be examined when starts when a demand is asserted and recognized. comparing addresses. DCON.l (DONE) - DMA Transfer is Complete - BAUD - (94H) Contains the programmable value for When set the DMA transfer is complete. It is set when the baud rate generator for the GSC. The baud rate will BCR equals 0 and is automatically reset when the equal (fosc)/«BAUD+ 1) X 8). DMA vectors to its interrupt routine. If DMA inter- rupt is disabled and the user software executes a jump BCRLO,1 (OE2H, OF2H) - Byte Count Register Low on the DONE bit, then the user software must also 0,1 - Contains the lower byte of the byte count. Used reset the done bit. If DONE is not set, then the DMA during DMA transfers to identify to the DMA chan- transfer is not complete. nels when the transfer is complete. BCRHO,1 (OE3H, OF3H) - Byte Count Register High DCON.2 (TM) - Transfer Mode - When set, DMA 0,1 - Contains the upper byte of the byte count. burst transfers are used if the DMA channel is config- ured in block mode or external interrupts are used to BKOFF (OC4H) - BackoffTimer - The backofftimer is initiate a transfer if in Demand Mode. When TM is an eight bit count-down timer with a clock period equal cleared, Alternate Cycle Transfers are used if DMA is to one slot time. The backoff time is used in the in the Block Mode, or Local Serial channel/GSC inter- CSMAlCD collision resolution algorithm. rupts are used to initiate a transfer if in Demand Mode. BOF - Beginning of Frame flag - A term commonly DCON.3 (DM) - DMA Channel Mode - When set, used when dealing with packetized data. Signifies the Demand Mode is used and when cleared, Block Mode beginning of a frame. is used. CRC - Cyclic Redundancy Check - An error checking DCON.4 (lSA) - Increment Source Address - When routine that mathematically manipulates a value depen- set, the source address registers are automatically incre- dent on the incoming data. The purpose is to identify mented during each transfer. When cleared, the source when a frame has been received in error. address registers are not incremented. CRCE - CRC Error, see RSTAT. DCON.S (SAS) - Source Address Space - When set, the source of data for the DMA transfers is internal data CSMA/CD - Stands for Carrier Sense, Multiple Ac- memory if autoincrement is also set. If autoincrement is cess, with Collision Detection. not set but SAS is, then the source for data will be one of the Special Function Registers. When SAS is cleared, CT - CRC Type, see GMOD. the source for data is external data memory. DARLO/l (OC2H, OD2H) - Destination Address Reg- DCON.6 (IDA) - Increment Destination Address ister Low Oil - Contains the lower byte of the destina- Space - When set, destination address registers are in- tions' address when performing DMA transfers. cremented once after each byte is transferred. When cleared, the destination address registers are not auto- DARHO/l (OC3H, OD3H) - Destination Address Reg- matically incremented. ister Low Oil - Contains the upper byte of the destina- tions' address when performing DMA transfers. 7-64 infel~ 83C152 HARDWARE DESCRIPTION DCON.7 (DAS) - Destination Address Space - When GMOD(84H) set, destination of data to be transferred is internal data 7 6543210 memory if autoincrement mode is also set. If autoincre- ment is not set the destinationwill be one of the Special I XTCLK I M1 I MO I AL I CT I PL1 I PLO I PR I Function Registers. When DAS is cleared then the des- tination is external data memory. The bits in this SFR, perform most of the configuration on the type of data transfers to be used with the GSc. DCR - Deterministic Resolution, see MYSLOT. Determines the mode, address length, preamble length, protocol select, and enables the external clocking of the DEN - An alternate function of one of the port 1 pins transmit data. (Pt.2). Its purpose is to enable external drivers when the GSC is transmitting data. This function is always GMOD.O (PR) - Protocol - If set, SDLC protocols with active when using the GSC and if P 1.2 is programmed NRZI encoding, zero bit insertion, and SD LC flags are to a 1. used. If cleared, CSMAlCD link access with Manches- ter encoding is used. DM - DMA Mode, see DCONO. GMOD.l,2 (PLO,I) - Preamble length DMA - Direct Memory Access mode, see TSTAT. PLI PLO LENGTH (BITS) DONE - DMA done bit, see DCONO. o 0 0 018 DPH - Data Pointer High, an SFR that contains the 1 0 32 high order byte of a general purpose pointer called the 1 1 64 data pointer (DPTR). The length includes the two bit Begin Of frame (BOP) DPL - Data Pointer Low, an SFR that contains the low flag in CSMA/CD but does not include the SDLC flag. order byte of the data pointer. In SDLC mode, the BOF is an SDLC flag, otherwise it is two consecutive ones. Zero length is not compatible EDMAO - Enable DMA Channel 0 interrupt, see in CSMAlCD mode. lENt. GMOD.3 (CT) - CRC Type - If set, 32-bit AUTODIN- EDMAI - Enable DMA Channel 1 interrupt, see 11-32 is used. If cleared, 16-bit CRC-CCITT is used. lEN 1. GMOD.4 (AL) - Address Length - If set, 16-bit ad- EGSRE - Enable GSC Receive Error interrupt, see dressing is used. If cleared, 8-bit addressing is used. In IENl. 8-bit mode, a match with any of the 4 address registers will allow that frame to be accepted (ADRO, ADRl, EGSRV - Enable GSC Receive Valid interrupt, see ADR2, ADR3). "Don't Care" bits may be masked in IENl. ADRO and ADRI with AMSKO and AMSKl. In 16- bit mode, addresses are matched against EGSTE - Enable GSC Transmit Error interrupt, see "ADRl:ADRO" or "ADR3:ADR2". Again, "Don't IENl. Care" bits in ADRl:ADRO can be masked in AM- SKl:AMSKO. A received address of all ones will al- EGSTV - Enable GSC Transmit Valid interrupt, see ways be recognized in any mode. IENl. GMOD.5, 6 (MO,Ml) - Mode Select - Two test modes, EOF - A general term used in serial communications. an optional "alternate backotr' mode, or normal back- EOF stands for End Of Frame and signifies when the off can be enabled with these two bits. last bits of data are transmitted when using packetized data. Ml MO Mode o 0 Normal ES - Enable LSC Service interrupt, see IE. o 1 Raw Transmit 1 0 Raw Receive ETO - Enable Timer 0 interrupt, see IE. 1 1 Alternate Backoff ETI - Enable Timer 1 interrupt, see IE. GMOD.7 (XTCLK) - External Transmit Clock - If set an external IX clock is used for the transmitter. If EXO - Enable External interrupt 0, see IE. cleared the internal baud rate generator provides the EX 1 - Enable External interrupt 1, see IE. 7-65 infel~ 83C152 HARDWARE DESCRIPTION transmit clock. The input clock is applied to P1.3 IE.2 (EXl) - Enables the external interrupt INTI on (TxC). The user software is responsible for setting or P3.3. clearing this flag. External receive clock is enabled by setting PCON.3. IE.3 (ETl) - Enables the Timer 1 interrupt. GO - DMA Go bit, see DCONO. 1E.4 (ES) - Enables the Local Serial Channel interrupt. GRxD - GSC Receive Data input, an alternate function IE.7 (EA) - The global interrupt enable bit. This bit of one of the port 1 pins (pl.0). This pin is used as the must be set to a 1 for any other interrupt to be enabled. receive input for the GSC. P1.0 must be programmed to a 1 for this function to operate. IENI - (OCSH) 76 5 4 3 2 1 0 GSC - Global Serial Channel - A high-level, multi-pro- tocol, serial communication controller added to the IIIEGSTEIEDMA11EGSTVIEDMAOIEGSREIEGSRVI SOC51BH core to accomplish high-speed transfers of packetized serial data. Interrupt enable register for DMA and GSC interrupts. A 1 in any bit position enables that interrupt. GTxD - GSC Transmit Data output, an alternate func- tion of one of the port 1 pins (P 1.1). This pin is used as IEN1.0 (EGSRV) - Enables the GSC valid receive in- the transmit output for the GSC. PI. 1 must be pro- terrupt. grammed to a 1 for this function to operate. IEN1.l (EGSRE) - Enables the GSC receive error in- HBAEN - Hardware Based Acknowledge Enable, see terrupt. RSTAT. IEN1.2 (EDMAO) - Enables the DMA done interrupt HLDA - Hold Acknowledge, an alternate function of for Channel O. one of the port 1 pins (p1.6). This pin is used to per- fonn the "HOLD ACKNOWLEDGE" function for IEN1.3 (EGSTV) - Enables the GSC valid transmit in- DMA transfers. HLDA can be an input or an output, terrupt. depending on the configuration of the DMA channels. P1.6 must be programmed to a 1 for this function to IEN1.4 (EDMAl) - Enables the DMA done interrupt operate. for Channell. HOLD - Hold, an alternate function of one of the port IEN1.5 (EGSTE) - Enables the GSC transmit error in- 1 pins (p1.5). This pin is used to perform the "HOLD" terrupt function for DMA transfers. HOLD can be an input or an output, depending on the configuration of the DMA IFS - (OA4H) Interframe Space, determines the number channels. P1.5 must be programmed to a 1 for this of bit times separating transmitted frames in CSMAI function to operate. CDandSDLC. IDA - Increment Destination Address, see DCONO. IP(OBSH) IE (OASH) 76543 2 1 0 7 6 5 4 3 2 1 0 PS I PT1 I PX1 I PTO PXO EA I ES I ET1 I EX1 I ETO EXO Allows the user software two levels of prioritization to be assigned to each of the interrupts in IE. A 1 assigns Interrupt Enable SFR, used to individually enable the the corresponding interrupt in IE a higher interrupt Timer and Local Serial Channel interrupts. Also con- than an interrupt with a corresponding O. tains the global enable bit which must be set to a 1 to enable any interrupt to be automatically recognized by IP.O (PXO) - Assigns the priority of external interrupt, the CPU. INTO. IE.O (EXO) - Enables the external interrupt INTO on IP.l (PTO) - Assigns the priority of Timer 0 interrupt, P3.2. TO. IE. 1 (ETO) - Enables the Timer 0 interrupt. 7-66 intel~ 83C152 HARDWARE DESCRIPTION IP.2 (PX 1) - Assigns the priority of external interrupt, Determines which type of Jam is used, which backoff INTI. algorithm is used, and the DCR slot address for the GSC. IP.3 (PT1) - Assigns the priority of Timer 1 interrupt, T1. MYSLOT.0,1,2,3,4,5 (SAO,1,2,3,4,5) - These bits deter- mine which slot address is assigned to the C152 when IP.4 (PS) - Assigns the priority of the LSC interrupt, using deterministic backoff during CSMA/CD opera- SBUF. tions on the GSC. Maximum slots available is 63. An address of OOH prevents that station from participating IPN1- (OFSH) in the backoff process. 765 4 3 2 1 0 I I I PGSTE I PDMA1 I PGSTV I PDMAO I PGSRE I PGSRV I MYSLOT.6 (DCR) - Determines which collision reso- lution algorithm is used. If set to ai, then the determi- Allows the user software two levels of prioritization to nistic backoff is used. If cleared, then a random slot be assigned to each of the interrupts in IENl. A 1 as- assignment is used. signs the corresponding interrupt in IENl a higher in- terrupt than an interrupt with a corresponding O. MYSLOT.7 (DCJ) - Determines the type of Jam used during CSMA/CD operation when a collision occurs. IPN1.0 (PGSRV) - Assigns the priority of GSC receive If set to a 1 then a low D.C. level is used as the jam valid interrupt. signal. If cleared, then CRC is used as the jam signal. The jam is applied for a length of time equal to the IPNl.1 (pGSRE) - Assigns the priority of GSC error CRC length. receive interrupt. NOACK - No Acknowledgment error bit, see TSTAT. IPNl.2 (PDMAO) - Assigns the priority of DMA done interrupt for Channel O. NRZI - Non-Return to Zero inverted, a type of data encoding where a 0 is represented by a change in the IPN1.3 (pGSTV) - Assigns the priority of GSC trans- level of the seriallink.. A 1 is represented by no change. mit valid interrupt. OVR - Overrun error bit, see RSTAT. IPN1.4 (PDMA1) - Assigns the priority of DMA done interrupt for Channel 1. PR - Protocol select bit, see GMOD. PCON (S7H) 7654 3 210 IPN 1. 5 (pGSTE) - Assigns the priority of GSC trans- mit error interrupt. ISMoolARslREolGARENIXRCLKIGFIENlpolloLI ISA - Increment Source Address, see DCONO. PCON.O (IDL) - Idle bit, used to place the C152 into the idle power saving mode. LNI - Line Idle, see TSTAT. PCON.1 (PD) - Power Down bit, used to place the LSC - Local Serial Channel - The asynchronous serial C152 into the power down power saving mode. port found on all MCS-51 devices. Uses start/stop bits and can transfer only 1 byte at a time. PCON.2 (GFIEN) - GSC Flag Idle Enable bit, when set, enables idle flags (01111110) to be generated be- MO - One of two GSC mode bits, see TMOD. tween transmitted frames in SDLC mode. M1 - One of two GSC mode bits, see TMOD PCON.3 (XRCLK) - External Receive Clock bit, used to enable an external clock to be used for only the re- MYSLOT - (OFSH) ceiver portion of the GSC. 7 654 3 2 1 0 IOCJ IOCR ISA5 I SA41 SA3 ISA21 SA1 I SAO I PCON.4 (GAREN) - GSC Auxiliary Receive Enable bit, used to enable the GSC to receive back-to-back SDLC frames. This bit has no effect in CSMA/CD mode. 7-67 infelqp 83C152 HARDWARE DESCRIPTION PCON.5 (REQ) - Requester mode bit, set to a 1 when RI - LSC Receive Interrupt bit, see SCON. C152 is to be operated as the requester station during DMA transfers. RFIFO - (F4H) RFIFO is a 3-byte FIFO that contains the receive data from the GSC. PCON.6 (ARB) - Arbiter mode bit, set to a 1 when C152 is to be operated as the arbiter during DMA RSTAT (OE8H) - Receive Status Register transfers. 7654321 0 PCON.7 (SMOD) - LSC mode bit, used to double the baud rate on the LSC. RSTAT.O (HBAEN) - Hardware Based Acknowledge PDMAO - Priority bit for DMA Channel 0 interrupt, Enable - If set, enables the hardware based acknowl- see IPNI. edge feature. PDMA 1 - Priority bit for DMA Channel 1 interrupt, RSTAT.l (GREN) - Receiver Enable - When set, the see IPNI. receiver is enabled to accept incoming frames. The user must clear RFIFO with software before enabling the PGSRE - Priority bit for GSC Receive Error interrupt, receiver. RFIFO is cleared by reading the contents of see IPNI. RFIFO until RFNE = o. After each read of RFIFO, it takes one machine cycle for the status of RFNE to be PGSRV - Priority bit for GSC Receive Valid interrupt, updated. Setting GREN also clears RON, CRCE, AE, see IPNI. and RCABT. GREN is cleared by hardware at the end of a reception or if any receive errOrs are detected. The PGSTE - Priority bit for GSC Transmit Error inter- status of GREN has no effect on whether the receiver rupt, see IPN 1. detects a collision in CSMA/CD mode as the receiver input circuitry always monitors the receive pin. PGSTV - Priority bit for GSC Transmit Valid inter- rupt, see IPNI. RSTAT.2 (RFNE) - Receive FIFO Not Empty - If set, indicates that the receive FIFO contains data. The re- PLO - One of two bits that determines the Preamble ceive FIFO is a three byte buffer into which the receive Length, see GMOD. data is loaded. A CPU read of the FIFO retrieves the oldest data and automatically updates the FIFO point- PLI - One of two bits that determines the Preamble ers. Setting GREN to a one will clear the receive FIFO. Length, see GMOD. The status of this flag is controlled by the GSC. This bit is cleared if user software empties receive FIFO. PRBS - (OE4H) Pseudo-Random Binary Sequence, gen- erates the pseudo-random number to be used in RSTAT.3 (RON) - Receive Done - If set, indicates the CSMAlCD backoff algorithms. successful completion of a receiver operation. Will not be set if a CRC, alignment, abort, or FIFO overrun PS - Priority bit for the LSC service interrupt, see IP. error occurred. PTO - Priority bit for Timer 0 interrupt, see IP. RSTAT.4 (CRCE) - CRC Error - If set, indicates that a properly aligned frame was received with a mismatched PTI - Priority bit for Timer 1 interrupt, see IP. CRC. PXO - Priority bit for External interrupt 0, see IP. RSTAT.5 (AE) - Alignment Error - In CSMAlCD mode, AE is set if the receiver shift register (an internal PXl - Priority bit for External interrupt 1, see IP. serial-to-parallel converter) is not full and the CRC is bad when an EOF is detected. In CSMAlCD the EOF RCABT - GSC Receiver Abort error bit, see RSTAT. is a line idle condition (see LNI) for two bit times. If the CRC is correct while in CSMAlCD mode, AE is RON - GSC Receiver Done bit, see RSTAT. not set and any mis-alignment is assumed to be caused by dribble bits as the line went idle. In SDLC mode, GREN - GSC Receiver Enable bit, see RSTAT. AE is set if a non-byte-aligned flag is received. CRCE may also be set. The setting of this flag is controlled by RFNE - GSC Receive FIFO Not Empty bit, see the GSe. RSTAT. 7-68 intet 83C152 HARDWARE DESCRIPTION RSTAT.6 (RCABT) - Receiver Collision/Abort Detect SCON.7 (SM2) - LSC mode specifier. - If set, indicates that a collision was detected after data had been loaded into the receive FIFO in CSMA/CD SDLC - Stands for Synchronous Data Link Communi- mode. In SDLC mode, RCABT indicates that 7 consec- cation and is a protocol developed by IBM. utive ones were detected prior to the end flag but after data has been loaded into the receive FIFO. AE may SLOTI'M - (0B4H) Determines the length of the slot also be set if RCABT is set. time in CSMAlCD. RSTAT.7 (OVR) - Overrun - If set, indicates that the SP (OSIH) - Stack Pointer, an eight bit pointer register receive FIFO was full and new shift register data was used during a PUSH, POP, CALL, RET, or RET!. written into it. It is cleared by user software. AE and/or CRCE may also be set if OVR is set. TCDCNT - (OD4H) Contains the number of collisions in the current frame if using probabilistic CSMA/CD SARHO (OA3H) - Source Address Register High 0, and contains the maximum number of slots in the de- contains the high byte of the source address for DMA terministic mode. ChannelO. TCDT - Transmit Collision Detect, see TSTAT. SARHI (OB3H) - Source Address Register High 1, contains the high byte of the source address for DMA TCON(OSSH) ChannelL 76543210 SARLO (OA2H) - Source Address Register Low 0, con- ITF1 I TR1 I TFO I TRO IIE1 IIT1 I lEO liTO I tains the low byte of the source address for DMA ChannelO. TCON.O (ITO) - Interrupt 0 mode control bit. SARLI (OB2H) - Source Address Register Low 1, con- TCON.l (lEO) - External interrupt 0 edge flag. tains the low byte of the source address for DMA Channell. TCON.2 (ITl) - Interrupt 1 mode control bit. SAS - Source Address Space bit, see DCONO. TCON.3 (lEI) - External interrupt 1 edge flag. SBUF (099H) - Serial Buffer, both the receive and TCON.4 (TRO) - Timer 0 run control bit. transmit SFR location for the LSC. CON.S (TFO) - Timer 0 overflow flag. SCON(09SH) 76543210 TCON.6 (TRl) - Timer 1 run control bit. ISMO I SM1 15M2 I REN I Tes I RBS I TI I RI I TCON.7 (TFl) - Timer 1 overflow flag. SCON.O (RI) - Receive Interrupt flag. TDN - Transmit Done flag, see TSTAT. SCON.l (TI) - Transmit Interrupt flag. TEN - Transmit Enable bit, see TSTAT. SCON.2 (RBS) - Receive Bit S, contains the ninth bit TFNF - Transmit FIFO Not Full flag, see TSTAT. that was received in Modes 2 and 3 or the stop bit in Mode 1 if SM20. Not used in Mode O. TFIFO - (SSH) TFIFO is a 3-byte FIFO that contains the transmission data for the GSC. SCON.3 (TBS) - Transmit Bit S, the ninth bit to be transmitted in Modes 2 and 3. THO (OSCH) - Timer 0 High byte, contains the high byte for timer/counter O. SCON.4 (REN) - Receiver Enable, enables reception for the LSC. SCON.S (SM2) - Enables the multiprocessor communi- cation feature in Modes 2 and 3 for the LSC. SCON.6 (SMl) - LSC mode specifier. 7-69 intet 83C152 HARDWARE DESCRIPTION THI (OSDH) - Timer I High byte, contains the high ter a successful transmission, a collision during the byte for timer/counter 1. data, CRC, or end flag. If cleared during a transmission the GSC transmit pin goes to a steady state high level. TI . Transmit Interrupt, see SCON. This is the method used to send an abort character in SDLC. Also DEN is forced to a high level. The end of TLO (OSAR) - Timer 0 Low byte, contains the low byte transmission occurs whenever the TFIFO is emptied. for timer/counter o. TSTAT.2 (TFNF) - Transmit FIFO not full - When TLl (OSBH) - Timer I Low byte, contains the low byte set, indicates that new data may be written into the for timer/counter 1. transmit FIFO. The transmit FIFO is a three byte buff- er that loads the transmit shift register with data. TM - Transfer Mode, see, DCONO. TSTAT.3 (TDN) - Transmit Done - When set, indi- TMOD(OS9H) cates the successful completion of a frame transmission. 76543210 If HBAEN is set, TDN will not be set until the end of the IFS following the transmitted message, so that the IGATE Iclf I M1 I MO IGATE I c/f I M1 I MO I acknowledge can be checked. If an acknowledge is ex- pected and not received, TDN is not set. An acknowl- TMOD.O (MO) - Mode selector bit for Timer O. edge is not expected following a broadcast or multi-cast packet. TMOD.I (MI) - Mode selector bit for Timer o. TSTAT.4 (TCDT) - Transmit Collision Detect - If set, TMOD.2 (CIT) - Timer/Counter selector bit for indicates that the transmitter halted due to a collision. Timer o. It is set if a collision occurs during the data or CRC or if there are more than eight collisions. TMOD.3 (GATE) - Gating Mode bit for Timer O. TSTAT.5 (UR) - Underrun - If set, indicates that in TMOD.4 (MO) - Mode selector bit for Timer 1. DMA mode the last bit was shifted out of the transmit register and that the DMA byte count did not equal TMOD.5 (MI) - Mode selector bit for Timer 1. zero. When an underrun occurs, the transmitter halts without sending the CRC or the end flag. TMOD.6 (CIT) - Timer/Counter selector bit for Timer 1. TSTAT.6 (NOACK) - No Acknowledge - If set, indi- cates that no acknowledge was received for the previous TMOD.7 (GATE) - Gating Mode bit for Timer 1. frame. Will be set only if HBAEN is set and no ac- knowledge is received prior to the end of the IFS. TSTAT (ODS) - Transmit Status Register NOACK is not set following a broadcast or a multi- cast packet. 76543210 ILNII NOACK IUR ITCOT ITON ITFNF ITEN IOMA I TSTAT.7 (LNI) - Line Idle - If set, indicates the re- ceive line is idle. In SDLC protocol it is set if 15 consec- utive ones are received. In CSMA/CD protocol, line TSTAT.O (DMA) - DMA Select - If set, indicates that idle is set ifGRXD remains high for approximately 1.6 DMA channels are used to service the GSC FIFO's and bit times. LNI is cleared after a transition on GRXD. GSC interrupts occur on TDN and RDN, and also en- ables UR to become set. If cleared, indicates that the TxC - External Clock input for GSC transmitter. GSC is operating in it normal mode and interrupts oc- cur on TFNE and RFNE.For more information on UR - Underrun flag, see TSTAT. DMA servicing please refer to the DMA section on DMA serial demand mode (4.2.2.3). XRCLK - External GSC Receive Clock Enable bit, see PCON. TSTAT.I (TEN) - Transmit Enable - When set causes TDN, UR, TCDT, and NOACK flags to be reset and XTCLK - External GSC Transmit Clock Enable bit, the TFIFO cleared. The transmitter will clear TEN af- seeGMOD. 7-70 intel® UNITED STATES Intel Corporation 2200 Mission College Boulevard P.O. Box 58119 Santa Clara, CA 95052 .. 8119 JAPAN Intel Japan K.K. 5 ..6 Tokodai, Tsukuba.. shi Ibaraki, 300.. 26 FRANCE Intel Corporation S.A.R.L. 1, Rue Edison, BP 303 78054 Saint.. Quentin.. en..Yvelines Cedex UNITED KINGDOM Intel Corporation (U.K.) Ltd. Pipers Way Swindon Wiltshire, England SN3 lRJ GERMANY Intel GmbH Domacher Strasse 1 85622 Feldkirchen/Muenchen HONG KONG Intel Semiconductor Ltd. 32/F Two Pacific Place 88 Queensway Central CANADA Intel Semiconductor of Canada, Ltd. 190 Attwell Drive, Suite 500 Rexdale, Ontario M9W 6H8 Printed in USAlEBM-002l294 1KIlL OS CG/061193 Embedded Microcontrollers