












Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!













After studying this chapter, you will be able to understand the following:
The microprocessor is a programmable chip that forms the CPU of a computer. Nowadays, many microprocessor chips are available in the market for users to select from depending on the application. In general, processor chips can be classified as general-purpose microprocessors, microcontrollers, and DSP processors. A general-purpose microprocessor is the CPU of a digital computer and needs external components such as memory, input devices, output devices, and decoders to function as a microcomputer system. These chips can be used to suit any general-purpose application and can be configured by the user. Examples of 8-bit processors are Intel’s 8085, Zilog 80, and Motorola 6800. Examples of 16- bit processors are Intel’s 8086 and 8088 and Motorola’s 68000 and examples of 32-bit processors are Intel’s 80186, 80286, and 80386, and Motorola’s 68030. In general, these microprocessor-based systems get data from mass storage devices, perform calculations, and store the results in storage devices. General-purpose microprocessors use external memory and a lot of processor time is involved in data transfer between the external memory and the processor. Microcontrollers are processor chips that generally have memory, input ports, and output ports within the chip itself. Therefore, they can also be called single chip computers, computer-on-a-chip, or system-on-a-chip. Microcontrollers are used in machine control applications, where there is no need to change the program. Equipments that use microcontrollers include computer printers, plotters, fax machines, Xerox machines, telephones, automotive engine control mechanisms, and electronic instruments such as oscilloscopes, multimeters, planimeters, IC testers, etc. The major difference between microprocessors and microcontrollers is that microcontrollers are comparatively faster because of reduced external memory accessing. Intel’s 8031, 8051, and 8096 and Motorola’s 68HC1 1 are examples of microcontrollers.
304 MICROPROCESSORS AND MICROCONTROLLERS DSP processors are processing chips that have flexibility in hardware and software, to implement signal-processing algorithms. These processors have an extended arithmetic and logic unit for operation on floating or fixed point number formats. Like microcontrollers, DSP processors also have on-chip memory, I/O ports, A/D converters, and serial ports. They are used in mobile phones, digital cameras, PBX systems, and smart card readers. Chapters 9-12 give a complete idea about the Intel 8-bit microcontroller’s architecture, instruction set, programming, and hardware interfacing. Readers of these chapters are expected to know the fundamentals of microprocessors, memory structures, and assembly language programming.
Intel Corporation has many microcontrollers in both 8-bit and 1 6-bit configurations. The 8-bit microcontrollers are available in many part numbers, with MCS-51 as the family name. Figure 9.1 shows the various microcontrollers in the MCS- series, with their constituent differences. For example, 8XC51RD comes with an internal ROM of 64 KB, while 8XC51FC comes with only a 32 KB ROM. Fig. 9.1 Microcontrollers in Intel’s MCS-51 series 8 KB ROM 16 KB ROM 32 KB ROM 64 KB ROM 4 KB ROM 8XC51 8XC 8XC51FA 8XC51RA 8XC51FB 8XC51RB 8XC51FC 8XC51RC 8XC51RD Table 9.1 lists the features of the 8051 family of microcontrollers. Table 9.1 Features of the 8051 family of microcontrollers Device number Data bus width (bits) (^) RAM capacity (bytes) ROM capacity (^8031 8 128) Nil 8051 8 128 4 KB 8751H (^8 128 4) KB EPROM 8052AH (^8 256 8) KB 8752BH (^8 256 8) KB EPROM All the microcontroller chips listed in Table 9.1 have the same basic ar chitecture. The Intel 8051 is considered for further discussion in the topics that deal with instruction set, and software and hardware interfacing in Chapters 9-12.
The main features available in the 8051 chips are as follows: (i) 8-bit CPU (ii) 4 KB of on-chip program memory
306 MICROPROCESSORS AND MICROCONTROLLERS PO 0-P0 7 AAAAAAAA AAAAAAAA B register PSEN RST Port 0 drivers Port 2 drivers P2 0-P2 7 RAM (^) Port 0 latch Port 2 latch ROM TMP P1 0-P1 7 P3 Q-P3 7 Fig. 9.2(b) 8051 internal block diagram ACC TMP PCON SCON T2CON THO TL SBUF Stack pointer TLO TMOD IE TH TCON IP Interrupt serial ports and timer blocks Buffer PC incrementer PSW Timing and control -XtAL1 XTAL Port 1 latch Port 3 latch Port 1 drivers 24 clock periods. PSEN, ALE, and EA are the signals used in conjunction with the external memory access of the 8051. They are discussed in detail in Chapters 11 and 12.
In the 8051, the memory is organized logically into program memory and data memory separately. The program memory is read-only type; the data memory is organized as read-write memory. Again, both program and data memories can be within the chip or outside. Figure 9.4 shows the various options available for memory organization in the 8051. The Intel 8051 has 128 bytes of RAM and 4 KB of ROM within the chip. The address bus of the 8051 is 16 bits wide. So it can access 64 KB of memory. As the memory is organized separately as program memory and data memory, the 8051
INTRODUCTION TO 8051 MICROCONTROLLERS 307 Fig. 9.3 Pin details of 8051 DIP IC T2/P1.0 (^) e^1 40 □ Vcc T2EX/P1.1 (^) □ 2 39 □ P0.0/AD ECI/P1.2 (^) e 3 38 □ P0.1/AD CEX0/P1.3 (^) e 4 37 □ P0.2/AD CEX1/P1.4 (^) □ 5 36 □ P0.3/AD CEX2/P1.5 (^) □ 6 35 □^ P0.4/AD CEX3/P1.6 (^) □ 7 34 □^ P0.5/AD CEX4/P1.7 (^) □ 8'^33 □^ P0.6/AD RST □ 9'^32 □ P0.7/AD RXD/P3.0 (^) □ 10 8061 Dual in-line 31^3 EA/VPP TXD/P3.1 (^) □ 11 package 30 □ ALE INT0/P3.2 (^) E 12 29 □^ PSEN iNT1/P3.3 (^) E 13 28 □ P2.7/A T0/P3.4 (^) E 14 27 □ P2.6/A T1/P3.5 (^) E 15 26 □ P2.5/A WR/P3.6 (^) E 16 25 □^ P2.4/A RD/P3.7 (^) E 17 24 □^ P2.3/A XTAL2 (^) E 18 23 □^ P2.2/A XTAL1 (^) E 19 22 □^ P2.1/A Vss (^) E^20 21 □^ P2.0/A Memory I-------------- J I • Program memory Data memory I I ■ I 1 Internal (4 KB) External (64 KB) Internal (128 bytes) External (64 KB)
. t ... • ’ '. ,. - ' t Fig. 9.4 Memory organization in the 8051 microcontroller can access 64 KB of program memory and 64 KB of data memory. The user can configure the entire program memory outside the chip or use 4 KB inside and 60 KB outside the chip. The internal data memory is accessed with 8-bit addresses and the external data memory with 16-bit addresses. So the maximum data memory that can be connected to the 8051 system is 64 KB.
The 8051 has 128 bytes of internal data RAM, which is accessible as bytes or sometimes as bits. The mapping of the internal RAM is shown in Fig. 9.5. The address of the internal RAM starts at OOH and occupies space up to 7FH. The RAM space is divided into three blocks—the register banks, the bit- addressable memory, and the scratch pad memory. The 8051-has four register banks of eight registers each, with addresses from OOH to 1FH. In assembly language, they are addressed by the names R0-R7. The
INTRODUCTION TO 8051 MICROCONTROLLERS 309 memory, are the registers that control the entire processor. They can be accessed only by direct addressing. The common SFRs are listed in Table 9.2. The registers available in the 8051 are as follows: (i) Accumulators—A and B (ii) Processor status word—PSW (iii) I/O port registers—PO, Pl, P2, and P (iv) Data pointers—DPH and DPL (v) Serial data buffer register—SBUF (vi) Stack pointer—SP (vii) Timer registers—THO, TH1 and TLO, TL (viii) Timer control registers—TCON and TMOD (ix) Power and port control—PCON and SCON (x) Interrupt control registers—IP and IE Programmers should not use the addresses in the range 80H-FFH (other than the SFRs), as they are used by Intel Corporation for expanding the functions of the
The PSW contains all the flags of the 8051 and is eight bits wide. The bit pattern of this flag register is given in Table 9.3. From the table, it can be seen that there are seven flag bits in the PSW of the 8051. The PSW is accessible fully as an 8- bit register, with the address DOH. Meanwhile, individual bits of the PSW can be
310 MICROPROCESSORS AND MICROCONTROLLERS accessed with the bit addresses given in Table 9.3. The contents of the PSW upon reset are given in the third row. Table 9.3 PSW^ format^ of^8051 PSW CY AC^ FO^ RSI^ RSO^ OV^ P Bit address D7H^ D6H^ D5H^ D4H^ D3H^ D2H^ D1H^ DOH Contents upon reset 0 0 0 0 0 0 X^0
an arithmetic or logical operation.
in the result. It is set to 1 if there is a carry out of either the D7 bit or the D6 bit of the accumulator. Overflow flag is set when arithmetic operations such as add and subtract result in sign conflict. The conditions under which the OV flag is set are as follows: Positive + Positive = Negative Negative + Negative = Positive Positive - Negative = Negative Negative - Positive = Positive
They can be set by the programmer to point to the correct register banks. The register bank selection in the programs can be changed using these two bits. Table 9.4 explains how these bits can be changed to select the appropriate register bank. Table 9.4 Register bank selection using PSW RS1 RSO^ Selected^ bank^ Address^ range 0 0 BankO^ 00H-07H 0 1 Bank^1 08H-OFH 1 0 Bank^2 10H-17H 1 1 Bank^3 18H-1FH
program and store any bit of his/her choice in this flag, using the bit address.
flag is set when there is a carry out of the D3 bit of the accumulator.
operations. It can also be used as an accumulator, to store one of the data bits for bit-related Boolean instructions’. The 8051 supports bit manipulation instructions. This means that in addition to the byte operations, bit operations can also be done using bit data. For this purpose, the contents of the PSW are bit-addressable. Similarly, the contents of
312 MICROPROCESSORS AND MICROCONTROLLERS actions will reset PCON.O and processor execution will resume at the instruction following the instruction that set the idle mode.
The power down mode is initiated by making PCON.l bit 1. In this mode, the clock generator is switched off and only the internal memory is active. The supply voltage Vcc can be reduced to 2 V and the power consumption can be reduced. The only way to revoke the processor from power down mode is to reset the system.
In the 8051, the stack is configured as a series of memory locations following the Last-In First-Out (LIFO) pattern. In general, the stack is initialized in the internal RAM area. Any 8-bit data can be stored and retrieved from the stack using PUSH and POP instructions, with the help of the stack pointer. The stack pointer (SP) is an 8-bit register within the SFR area, with the address 81H. This register can hold one 8-bit address at a time, which is actually the memory location at top of the stack. A push operation in the 8051 is used to store an 8-bit data in the stack. The PUSH instruction first increments the value of SP and then stores the data mentioned in the instruction in the memory location pointed to by SP. Similarly, the POP instruction stores the value from the top of the stack in the register mentioned in the instruction and then decrements the value of SP. The stack pointer is initialized to the value 07H when the 8051 microcontroller is reset. The other instructions of the 8051 that affect the stack and the stack pointer are ACALL, LCALL, RET, and RETI. The stack pointer can be initialized to any internal RAM address by the programmer, by writing the required address in the SP SFR address 81H. POINTS TO REMEMBER
INTRODUCTION TO 8051 MICROCONTROLLERS 313 KEY TERMS Data memory It is a read and write memory, which is used to store data. Flags These are the bits used to indicate the status of the results after arithmetic and logical operations. Processor Status Word (PSW) It is the register containing the various flags that are used to indicate the status of the results after arithmetic and logical operations. Program memory It is a read only memory, which is used to store programs. Special Function Registers (SFRs) These are part of the internal memory and are used for specific functions and control of various functional blocks in the 8051. REVIEW QUESTIONS
8051 INSTRUCTION SET AND PROGRAMMING 315 specified in the instruction. Example: ADD A, RO This instruction adds the contents stored in register RO to the accumulator contents and stores the result in the accumulator. The registers A, DPTR (data pointer register), and R0-R7 are used in register direct addressing. This addressing mode uses temporary registers, which hold the data for the operation.
The memory address that contains the data to be operated on is specified in the instruction. Example: ADD A, 74H This instruction adds the data in the accumulator to that stored in the memory address 74H. All internal RAM addresses, including that of the special function registers, can be used in memory direct addressing instructions. This addressing mode is used when the data stored in memory is to be used in arithmetic and logical instructions. The data in the memory, which is used in memory direct addressing, can be changed at any point in the program.
The register that contains the memory address of the data is specified in the instruction. The register specified is preceded by the ‘ @ ’ symbol in the assembly language format. Example: ADD A. @R The value stored in the register RO is now the address of the memory location of the data to be fetched. From this memory location, the data is fetched and the instruction is executed. The DPTR is used to access the data in the external memory with 16-bit addresses. The indirect addressing mode is very useful for accessing data that are stored in consecutive memory locations and accessed serially in the program.
In this type of addressing, the instruction consists of two parts — a base address and an offset. This type of addressing is useful in relative memory accessing and relative jumping. The base address is stored in the DPTR or any other register. The offset value is stored in the accumulator. Example: MOVC A. @A + DPTR This instruction adds the contents of the accumulator to the contents of the data pointer and the result forms the actual address from where data is to be fetched. This data is moved to the accumulator.
316 MICROPROCESSORS AND MICROCONTROLLERS The indexed addressing mode is useful in accessing data structures similar to look-up tables. The base address holds the address of the starting point of the table and the offset points to a particular entry in the table.
The instructions supported by the 8051 can be classified into different types depending on their operational functions. The complete instruction set is given in Appendix B. The different types of 8051 instructions are discussed in Sections 10.3.1-10.3.5.
As the name indicates, instructions in this set are used to transfer data. Data can be transferred from or to the external RAM or within the internal memory itself. The list of data transfer instructions is given in Table 10.1. Table 10.1 Data transfer instructions Mnemonic Operation^ Addressing^ modes Direct Indirect Register Immediate MOV A, A^ =^ ^ yf^ yf^ yf^ yf MOV , A ^ =^ A^ yf^ yf^ yf MOV ,
= yf yf yf MOV DPTR, # data 16 DPTR = 16-bit immediate data PUSH INC SP: MOV ‘@SP’, yf POP MOV , ‘@SP’: DEC SP yf XCH A, ACC and exchange data yf^ yf^ yf XCHD A, @Ri ACC and @Ri exchange lower- order nibbles yf MOVX A, @Ri Copy 8-bit data from the external RAM location pointed to by Ri to register A Only indirect addressing mode MOVX @Ri, A Copy 8-bit data from register A to the external RAM location pointed to by Ri Only indirect addressing mode MOVX A, @ DPTR Copy 8-bit data from the external RAM location pointed to by the 1 6-bit DPTR to register A Only indirect addressing mode MOVX @DPTR, A Copy 8-bit data from register A to the external RAM location pointed to by the 16-bit DPTR Only indirect addressing mode MOVC A, @A
318 MICROPROCESSORS AND MICROCONTROLLERS The ADD instruction is used to add 8-bit data with the accumulator and the result is stored in the accumulator (A) register. The carry generated (if any) is stored in the carry flag of the processor status word PSW. The ADDC instruction is used to add 8-bit data with the accumulator, along with the carry bit. The SUBB instruction is used to subtract the contents of a register and the carry bit from the contents of the accumulator. For the ADD and SUBB instructions, one of the data must be in accumulator; the other data can be immediate or in any direct addressed or indirect addressed internal memory location. In addition to the ADD, ADDC, and SUBB instructions, there are two more instructions—MUL and DIV. The register B is used exclusively for these two instructions. The operands should be stored in the registers A and B. The MUL instruction multiplies the contents of registers A and B and stores the 16-bit result in both the registers. The lower-order byte of the result is stored in register A and the higher-order byte in register B. The DIV instruction upon execution divides the contents of register A by the contents of register B. The quotient of the result is stored in register A and the remainder in register B. Division by 0, i.e., a ‘ 0 ’ in register B before the execution of the instruction DIV AB sets the overflow flag (OV) to 1. The DAA instruction is used to convert the binary sum obtained after adding two BCD numbers into a BCD number.
In addition to the logical AND, OR, and XRL operations, the 8051 has instructions such as CLR and CPL, as shown in Table 10.3. All the data for the logical instructions must be available in the internal RAM. The instruction CLR A is used to clear the contents of register A, CPL is used to complement or logically invert the contents of register A, and SWAP is used to swap the nibbles of register A. The 8051 supports four rotate operations, with options to rotate left or right and through the carry. Table 10.3 Logical instructions Mnemonics Operation Addressing modes Direct Indirect Register Immediate ANL A, A = A AND y[^ yj^ yf^ yf ANL , A = AND A y/ ANL , #data = AND data yf ORL A, A = A OR y/ yf yf yf ORL , A (^) = OR A yf ORL , #data (^) = OR data yf XRL A, A = A XOR yf yf yf yf XRL , A = XOR A yf XRL , #data = XOR data yf (Contd)
8051 INSTRUCTION SET AND PROGRAMMING 319 Table 10.3 Logical instructions (Contd) Mnemonics (^) Operation Addressing modes Direct Indirect Register Immediate CLR A A = 00H (^) Accumulator only CPLA A = NOT A (^) Accumulator only RL A Rotate ACC left one bit Accumulator only RLC A Rotate ACC left through carry Accumulator only RR A (^) Rotate ACC right one bit Accumulator only RRC A (^) Rotate right through carry Accumulator only
The 8051 supports unconditional jumping and subroutine calling in three ways—absolute jump (AJMP and ACALL), long jump (LJMP and LCALL), and short jump (SJMP). These unconditional branching instructions are listed in Table 10.4. Table 10.4 Unconditional branching instructions Mnemonics Operation SJMP rel_addr (^) Jump to (PC) + 8-bit rel_addr. AJMP 11 -bit addr Jump to PC:addr. LJMP addr Jump to addr. JMP @A + DPTR (^) Jump to A + DPTR. ACALL 11 -bit addr Call subroutine at PC:addr. LCALL addr Call subroutine at addr. RET Return from subroutine. RETI Return from interrupt. NOP No operation Table 10.5 lists the conditional branching instructions of the 8051. Table 10.5 Conditional branching instructions Mnemonics Operation Addressing modes Direct Indirect Register Immediate CJNE A, , rel CJNE , #data, rel DJNZ , rel Jump if A yf yf Jump if #data yf yf Decrement byte and jump yf yf if not zero JZrel JNZ rel Jump if A = 0 Accumulator only Jump if A 0 Accumulator only
8051 INSTRUCTION SET AND PROGRAMMING 321 Table 10.6 (^) Bit manipulation instructions Mnemonics (^) Operation ANL C, bit ANL C, /bit ORL C, bit ORL C, /bit MOV C, bit MOV bit, C CLR C C = C AND bit C = C AND (NOT bit) C = C OR bit C = C OR (NOT bit) C = bit bit = C C = 0 CLR bit (^) bit = 0 SETB C C = 1 SETB bit bit = 1 CPLC C = NOT C CPL bit bit = NOT bit JC rel Jump if C = 1 JNCrel Jump if C = 0 JB bit, rel JNB bit, rel JBC bit, rel Jump if bit = 1 Jump if bit = 0 Jump if bit = 1; CLR bit The logical instructions include ANL and ORL. Conditional branching includes JC, JNC, JB, JNB, and JBC. The other instructions available in this category include CLR, SETB, CPL, and MOV. Note that there are no instructions for halting machine execution. Table 10.7 shows the flag bits affected by the various instructions. Note that the increment and decrement instructions do not affect the flag register. Table 10.7 Instructions that affect flag bits Instruction C Flags affected OV AC ADD yf^ yf ADDC yf yf^ yf SUB B yf^ yf^ yf MUL 0 yf DIV 0 yf DA yf RRC yf RLC yf SETB C 1 CLR C 0 CPLC yj (Contd)
322 MICROPROCESSORS AND MICROCONTROLLERS Table 10.7 Instructions that affect flag bits (Contd) Instruction Flags affected C OV AC ANL C, /bit ORL C, bit ORL C, /bit MOV C, bit CJNE
Assembler directives are special instructions to the assembler program and are used to define specific operations. These directives are not part of the executable program. Some of the most frequently used assembler directives are as follows: (i) ORG—ORiGinate; defines the starting address for the program in program (code) memory (ii) EQU—EQUate; assigns a numeric value to a symbol identifier so as to make the program more readable (iii) DB—Define a Byte; puts a byte constant (8-bit number) at the memory location specified (iv) DW—Define a Word; puts a word constant (16-bit number) at the memory location specified (v) DBIT—Define a BIT; defines a bit constant, which is stored in the bit- addressable section of the internal RAM (vi) END—This is the last statement in the source file and is used to advise the assembler to stop the assembly process.
Example 10.1: Fill a block of memory in the internal RAM with a specific data. The following assembly language program is used to fill a block of internal data memory with a particular data. The number of memory locations to be filled is given by the variable COUNT in the program. This program uses indirect addressing. Program: START: MOV RI.tfCOUNT ; Load number count. MOV RO, #30 ; Load the starting address of the memory. LOOP: MOV @R0, #DATA : Load data in the memory location pointed to by R0. INC R0 ; Point to the next memory location. DJNZ RI, LOOP ; Check for count after decrementing and loop if not 0. The following program uses direct addressing for achieving the same programming objective, using the memory locations 30H-34H.