0% found this document useful (0 votes)
2 views90 pages

Microcontroller book

The document covers various topics related to numbering and coding systems, digital logic, and microcontrollers. It includes exercises on converting between decimal, binary, and hexadecimal systems, as well as truth tables for digital gates. Additionally, it discusses the differences between microprocessors and microcontrollers, their applications in embedded systems, and criteria for selecting a microcontroller.

Uploaded by

Yh Yasin Hasan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
2 views90 pages

Microcontroller book

The document covers various topics related to numbering and coding systems, digital logic, and microcontrollers. It includes exercises on converting between decimal, binary, and hexadecimal systems, as well as truth tables for digital gates. Additionally, it discusses the differences between microprocessors and microcontrollers, their applications in embedded systems, and criteria for selecting a microcontroller.

Uploaded by

Yh Yasin Hasan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
PROBLEMS SECTION 0.1: NUMBERING AND CODING SYSTEMS 1. Convert the following decimal numbers to binary. (a) 12 (b) 123 (c) 63. (a) 128 (e) 1000 2. Convert the following binary numbers to decimal. (a) 100100 (b) 1000001 (c) 11101 (d) 1010 ¢e) 00100010 Convert the values in Problem 2 to hexadecimal. 4, Convert the following hex numbers to binary and decimal. (a) 2B9H (b) F44H (c) 912H (d) 2BH (c) FFFFH Convert the values in Problem 1 to hex. 6. Find the 2’s complement of the following binary numbers. (a) 1001010 (b) 111001 (c) 10000010 (d) 111110001 7. Add the following hex values. (a) 2CH + 3FH (b) F34H + SD6H (c) 20000H + 12FFH (a) FFFFH + 2222H 8, Perform hex subtraction for the following. (a) 24FH — 129H (b) FE9H — SCH (c) 2FFFFH ~ FFFFFH (a) 9FF25H — 4DD99H 9. Show the ASCII codes for numbers 0, 1, 2,3. 10. Show the ASCII code (in hex) for the followit “U.S.A. is a country” CR, LF in North America” CR, LF CR is carriage return LF is line feed 9 in both hex and binary. string: SECTION 0.2: DIGITAL PRIMER 11, Draw a 3-input OR gate using a 2-input OR gate. 12, Show the truth table for a 3-input OR gate. 13, Draw a 3-input AND gate using a 2-input AND gate, 14, Show the truth table for a 3-input AND gate, 15, Design a 3-input XOR gate with a 2-input XOR gate. Show the truth table for a 3-input XOR. 16, List the truth table for a 3-input NAND. 17. List the truth table for a 3-input NOR, 18, Show the decoder for binary 1100. 19, Show the decoder for binary 11011 20, List the truth table for a D-FF. SECTION 0.3: INSIDE THE COMPUTER 21, Answer the following: (a) How many nibbles are 16 bits? (b) How many bytes are 32 bits? (c) Ifa word is defined as 16 bits, how many words is a 64-bit data item? (d) What is the exact value (in decimal) of | meg? 20 () How many K is 1 meg? (0) What is the exact value (in decimal) of 1 giga? (g) How many K is 1 giga? (a) How many meg is 1 giga? (i) Ifa given computer has a total of & megabytes of memory, how many bytes (in decimal) is this? How many kilobytes is this? 22. A given mass storage device such as a hard disk can store 2 gigabytes of infor- mation. Assuming that each page of text has 25 rows and cach row has 80 columns of ASCII characters (each character = | byte), approximately how many pages of information can this disk store? 23. Ina given byte-addressable computer, memory locations 10000H to 9FFFFH are available for user programs. The first location is 10000H and the last loca- tion is 9FFFFH. Calcalate the following: (a) The total number of bytes available (in decimal) (b) The total number of kilobytes (in decimal) 24. A given computer has a 32-bit data bus. What is the largest number that can be carried into the CPU at a time? 25, Below are listed several computers with their data bus widths. For each com- puter, list the maximum value that can be brought into the CPU at a time (in both hex and decimal). (a) Apple 2 with an 8-bit data bus (b) IBM PC with a 16-bit data bus (c) IBM PC with a 32-bit data bus (4) Cray supercomputer with a 64-bit data bus 26. Find the total amount of memory, in the units requested, for each of the fol- lowing CPUs, given the size of the address buses. (a) 16-bit address bus (in K) (b) 24-bit address bus (in megabytes) (c) 32-bit address bus (in megabytes and gigabytes) (d) 48-bit address bus (in megabytes, gigabytes, and terabytes) 27. Regarding the data bus and address bus, which is unidirectional and which is bidirectional? 28. Which register of the CPU holds the address of the instruction to be fetched? 29. Which section of the CPU is responsible for performing addition? 30. List the three bus types present in every CPU. ANSWERS TO REVIEW QUESTIONS SECTION 0.1: NUMBERING AND CODING SYSTEMS 1, Computers use the binary system because each bit can have one of two voltage levels: on and off, 34,9 = 100010 = 2246 M0101 = 35)6~ 5310 1110001, 10100 461 275 CHAPTER INTRODUCTION TO COMPUTING 2 8. 38 30.78 38 36 20 43 5055 73 SECTION 0.2: DIGITAL PRIMER AND oR XOR Buffer Storing data Decoder SECTION 0.2: INSIDE THE COMPUTER 1. 24576 2. Random access memory; it is used for temporary storage of programs that the CPU is run ning, such as the operating system, word processing programs. ele. 3. Read-only memory; its used for permanent programs such as those that control the keyboard, ate. 4, The contents of RAM are lost when the computer is powered off. 5. The CPU, memory, and 1/0 devices 6. Central processing unit; it can be considered the “brain” of the computer; it executes the pro- _grams and controls all other devices in the computer. 7. ‘The address bus carries the location (address) needed by the CPU; the data bus carries infor- ‘mation in arid out of the CPU; the control bus is used by the CPU to send signals controlling UO devices. 8. (a) bidirectional (b) unidirectional 9. 64K, or 65,536 bytes 10, Arithmetic/logic unit; it performs all arithmetic and logic operations. 11. Itis for temporary storage of information. 12. Itholds the address of the next instruction to be executed, 13. Ittells the CPU what steps to perform for each instruction. 22 CHAPTER 1 THE 8051 MICROCONTROLLERS OBJECTIVES Upon completion of this chapter, you will be able to: >> Compare and contrast microprocessors and microcontrollers >> Describe the advantages of microcontrollers for some applications >> Explain the concept of embedded systems >> Discuss criteria for considering a microcontroller >> Explain the variations of speed, packaging, memory, and cost per unit and how these affect choosing a microcontroller >> Compare and contrast the various members of the 8051 family >> Compare 8051 microcontrollers offered by various manufacturers 23 This chapter begins with a discussion of the role and importance of micro- controllers in everyday life. In Section 1.1 we also discuss criteria to consider in choosing a microcontroller, as well as the use of microcontrollers in the embedded market, Section 1.2 covers various members of the 8051 family such as the 8052 and 8031, and their features. In addition, we discuss various versions of the 8051 such as the 8751, AT89C51, and DS5000. SECTION 1.1: MICROCONTROLLERS AND EMBEDDED PROCESSORS In this section we discuss the need for microcontrollers and contrast them with general-purpose microprocessors such as the Pentium and other x86 micro- processors. We also look at the role of microcontrollers in the embedded market. In addition, we provide some criteria on how to choose a microcontroller. Microcontroller versus general-purpose microprocessor What is the difference between a microprocessor and microcontroller? By microprocessor is meant the general-purpose microprocessors such as Intel’s x86 family (8086, 80286, 80386, 80486, and the Pentium) or Motorola’s 680x0 fami- ly (68000, 68010, 68020, 68030, 68040, cte.). These microprocessors contain no RAM, no ROM, and no I/O ports on the chip itself. For this reason, they are com- monly referred to as general-purpose microprocessors. Data bus CPU cpu | RAM| ROM General- Serial Purpose | |RAM|}ROM| | V0 | |Timer| | CoM Micro- Port Port VO | Timer | Serial processor com Port Address bus (a) General-Purpose Microprocessor System (b) Microcontroller Figure 1-1. Microprocessor System Contrasted With Microcontroller System A system designer using a general-purpose microprocessor such as the Pentium or the 68040 must add RAM, ROM, I/O ports, and timers externally to make them functional. Although the addition of external RAM, ROM, and 1/0 ports makes these systems bulkier and much more expensive, they have the advan- tage of versatility such that the designer can decide on the amount of RAM, ROM, and I/O ports needed to fit the task at hand. This is not the case with mierocon- trollers, A microcontroller has a CPU (a microprocessor) in addition to a fixed amount of RAM, ROM, I/O ports, and a timer all on a single chip. In other words, the processor, RAM, ROM, I/O ports, and timer are all embedded together on one chip; therefore, the designer cannot add any external memory, V/O, or timer to it. The fixed amount of on-chip ROM, RAM, and number of I/O ports in microcon- trollers makes them ideal for many applications in which cost and space are criti- 24 Home ‘Appliances Intercom, Telephones Security systems Garage door openers Answering machines Fax machines Home computers TVs Cable TV tuner VCR Camcorder Remote controls Video games Cellular phones Musical instruments Sewing machines Lighting control Paging ‘Camera Pinball machines Toys Exercise equipment Office Telephones Computers Security systems Fax machine Microwave Copier Laser printer Color printer Paging oe ‘Trip computer Engine control Air bag ABS Instrumentation Security system ‘Transmission control Entertainment Climate control Cellular phone Keyless entry Table 1-1: Some Embedded Products Using Mierocontri lers cal. In many applications, for example a TV remote control, there is no need for the computing power of a 486 or even an 8086 microprocessor. In many applications, the space it takes, the power it consumes, and the price per unit are much more critical considerations than the computing power. These applications ‘most often require some 1/O operations to read signals and turn on and off certain bits. For this reason some call these processors IBP, “itty-bitty processors” (see “Good Things in Small Packages ‘Are Generating Big Product Opportunities” by Rick Grehan, BYTE magazine, September 1994; [Link], for an excel- lent discussion of microcontrollers). It is interesting to note that some microcontroller manu- facturers have gone as far as integrating an ADC (analog-to-digi- tal converter) and other peripherals into the microcontroller. Microcontrollers for embedded systems In the literature discussing microprocessors, we often see the term embedded system. Microprocessors and microcontrollers are widely used in embedded system products. An embedded product uses a microprocessor (or microcontroller) to do one task and one task only. A printer is an example of embedded system since the processor inside it performs only one task ; namely, get- ting the data and printing it. Contrast this with a Pentium-based PC (or any x86 IBM-compatible PC). A PC can be used for any number of applications such as word processor, print server, bank teller terminal, video game player, network server, or internet ter~ minal. Software for a variety of applications can be loaded and run. Of course the reason a PC can perform myriad tasks is that it has RAM memory and an operating system that loads the appli- cation software into RAM and lets the CPU run it. In an embed- ded system, there is only one application software that is typical- ly burned into ROM. An x86 PC contains or is connected to var- ious embedded products such as the keyboard, printer, modem, disk controller, sound card, CD-ROM driver, mouse, and so on. Each one of these peripherals has a microcontroller inside it that performs only one task. For example, inside every mouse there is a microcontroller that performs the task of finding the mouse position and sending it to the PC. Table 1-1 lists some embedded products. X86 PC embedded applications Although microcontrollers are the preferred choice for many embedded systems, there are times that a microcontroller is inadequate for the task, For this reason, in recent years many manufacturers of general-purpose microprocessors such as Intel, Freescale Semiconductor Inc. (formerly Motorola), AMD (Advanced Micro Devices, Inc.), and Cyrix (now a division of CHAPTER 1: THE 8051 MICROCONTROLLERS 25 National Semiconductor, Inc.) have targeted their microprocessor for the high end of the embedded market. While Intel and AMD push their x86 processors for both the embedded and desktop PC markets, Freescale is determined to keep the 68000 family alive by targeting it mainly for the high end of embedded systems now that Apple no longer uses the 680x0 in their Macintosh. In the early 1990s Apple com- puter began using Power PC microprocessors (604, 603, 620, etc.) in place of the 680x0 for the Macintosh. The Power PC microprocessor is a joint venture between IBM and Freescale, and is targeted for the high end of the embedded market as well as the PC market. It must be noted that when a company targets a general- purpose microprocessor for the embedded market it optimizes the processor used for embedded systems. For this reason these processors are often called high-end embedded processors. Nery ofen the terms embedded processor and microcon- troller are used interchangeably. One of the most critical nceds of an embedded system is to decrease power consumption and space. This can be achieved by integrating more functions into the CPU chip. All the embedded processors based on the x86 and 680x0 have low power consumption in addition to some forms of 1/0, COM port, and ROM all on a single chip. In high-performance embedded processors, the trend is to integrate more and more funetions on the CPU chip and let the designer decide which fea tures he/she wants to use, This trend is invading PC system design as well Normally, in designing the PC motherboard we need a CPU plus a chip-set con- taining 1/0, a cache controller, a flash ROM containing BIOS, and finally a sec- ondary cache memory. New designs are emerging in industry. For example, Cyrix has announced that it is working on a chip that contains the entire PC, except for DRAM. In other words, we are about to sce an entire computer on a chip. Currently, because of MS-DOS and Windows standardization many embedded systems are using x86 PCs. In many cases using x86 PCs for the high- end embedded applications not only saves money but also shortens development time since there is a vast library of software already written for the DOS and Windows platforms, The fact that Windows is a widely used and well understood platform means that developing a Windows-based embedded product reduces the cost and shortens the development time considerably. Choosing a microcontroller There are four major 8-bit microcontrollers. They are: Freescale’s 6811, Intel’s 8051, Zilog’s 28, and PIC 16X from Microchip Technology. Each of these microcontrollers has a unique instruction set and register set; therefore, they are not compatible with cach other. Programs written for one will not run on the oth- ers. There are also 16-bit and 32-bit microcontrollers made by various chip mak- ers, With all these different microcontrollers, what criteria do designers consider in choosing one? Three criteria in choosing microcontrollers are as follows: (1) meeting the computing needs of the task at hand efficiently and cost effectively, (2) availability of software development tools such as compilers, assemblers, and debuggers, and (3) wide availability and reliable sources of the microcontroller. Next we elaborate further on each of the above criteria, 26 Criteria for choosing a microcontroller The first and foremost criterion in choosing a microcontroller is that it must meet the task at hand efficiently and cost effectively. In analyzing the needs of a microcontroller-based project, we must first see whether an 8-bit, 16-bit, or 32-bit microcontroller can best handle the computing needs of the task most effectively. Among other considerations in this category are: (a) Speed. What is the highest speed that the microcontroller supports? (b) Packaging. Does it come in a 40-pin DIP (dual inline package) or a QFP (quad flat package), or some other packaging format? This is important in terms of space, assembling, and prototyping the end product. (c) Power consumption. This is especially critical for battery-powered prod- ucts, (d) The amount of RAM and ROM on chip. (c) The number of I/O pins and the timer on the chip. (®) How easy it is to upgrade to higher-performance or lower power-con- sumption versions. (g) Cost per unit. This is important in terms of the final cost of the product in which a microcontroller is used. For example, there are microcontrollers that cost 50 cents per unit when purchased 100,000 units at a time. The second criterion in choosing a inicrocontroller is how easy it is to devel- op products around it. Key considerations include the availability of an assem- bler, debugger, a code-efficient C language compiler, emulator, technical sup- port, and both in-house and outside expertise, In many cases, third-party ven- dor (that is, a supplier other than the chip manufacturer) support for the chip is as good as, if not better than, support from the chip manufacturer, The third criterion in choosing a microcontroller is its ready availability in needed quantities both now and in the future. For some designers this is even more important than the first two criteria. Currently, of the leading 8-bit microcontrollers, the 8051 family has the largest number of diversified (multi- ple source) suppliers. By supplier is meant a producer besides the originator of the microcontroller. In the case of the 8051, which was originated by Intel, several companies also currently produce (or have produced in the past) the 8051. These companies include: Intel, Atmel, Philips/Signetics, AMD, Infineon (formerly Siemens), Matra, and Dallas Semiconductor. See Table 1-2. Table 1-2: Some of the Companies Producing a It should be noted that Member of the 8051 Family Freescale, Zilog, and Microchip ‘Compaa; , Technology have all dedicated Web Site massive resources to ensure wide intel [Link]/designimesS! and timely availability of their Atmel [Link] product since their product is sta- Philips/Signeties __www.[Link] je, mature, and single sourced. Infineon Dallas Semi/Maxim __ [Link] In recent years they also have begun to sell the ASIC library cell of the microcontroller, CHAPTER 1: THE 8051 MICROCONTROLLERS 27 Review Questions 1, True or false. Microcontrollers are normally less expensive than microproces- ors 2. When comparing a system board based on a microcontroller and a general-pur- pose microprocessor, which one is cheaper? 3. A microcontroller normally has which of the following devices on-chip? (a) RAM (b) ROM (©) VO (4) all of the above 4. A general-purpose microprocessor normally needs which of the following devices to be attached to it? (a) RAM (b) ROM (©) VO (a) all of the above 5. An embedded system is also called a dedicated system. Why? What does the term embedded system mean? 7. Why does having multiple sources of a given product matter? 2 SECTION 1.2: OVERVIEW OF THE 8051 FAMILY In this section we first look at the various members of the 8051 family of microcontrollers and their internal features. Plus we see who are the different man- ufacturers of the 8051 and what kind of products they offer. A brief history of the 8051 In 1981, Intel Corporation introduced an 8-bit microcontroller called the 8051. This microcontroller had 128 bytes of RAM, 4K bytes of on-chip ROM, two timers, one serial port, and four ports (cach 8-bits wide) all on a single chip. At the time it was also referred to as a “system on a chip.” The 8051 is an 8-bit proces- sor, meaning that the CPU ean work on only & bits of data at a time. Data larger than 8 bits has to be broken into 8-bit picces © be processed by the CPU. The 8051 has a total of four I/O ports, each 8 bits wide. Sce Figure }-2. Although the 8051 can have a maximum of 64K bytes of on-chip ROM, many manufacturers have put only 4K bytes on the chip. This will be discussed in more detail later. The 8051 became widely popular afier Intel allowed other manufacturers to make and market any flavors of the 8051 they please with the condition that they remain code-compatibie with the 8051. This has led to many versions of the 8051 with different speeds and amounts of on-chip ROM marketed by more than half'a dozen manufacturers. Next we review some of them. It is important to note that although there are different flavors of the 8051 in Table 1-3: Features of the 8051 terms of speed and amount of on-chip ROM, they are all, compatible with the original 8051 as far as the instruc- Feature Quantity tions are concerned, This means that if you write your ROM 4K bytes: program for one, it will run on any of them regardless of RAM __ 128 bytes the manufacturer. Timer 2 VO pins 322 8051 microcontroller Serial port 1 The 8051 is the original member of the 8051 fam- ‘essrupt sources 6 ily. Intel refers to itas MCS-51. Table 1-3 shows the main "7" HOw tmount indiates on-chip features of the 8051 28 EXTERNAL INTERRUPTS ON-CHIP 2 y y ROM g INTERRUPT |< for ETC. | CONTROL. rogram ON-CHIP TIMER O |< g code RAM TERT |< } 3 3 , it a cpu AK bus 440) ‘SERIAL osc CONTROL PORTS PORT 7 r 0 7 YoY { | | { Y | i PO P2 PI PS XD RXD — ADDRESSIDATA Figure 1-2, Inside the 8051 Microcontroller Block Diagram Other members of the 8051 family There are two other members in the 8051 family of microcontrollers. They are the 8052 and the 8031 8052 microcontroller The 8052 is another member of the 8051 family. The 8052 has all the stan- dard features of the 8051 as well as an extra 128 bytes of RAM and aa extra timer. In other words, the 8052 has 256 bytes of RAM and 3 timers. It also has 8K bytes of on-chip program ROM instead of 4K bytes. See Table 1-4 Table 1-4: Comparison of 8051 Family Members Feature 8051___8052__—_8031 ROM (on-chip program space in bytes) 4K 3K 0K, RAM (bytes) 128 256 128 Timers 2 3 2 TO pins 32 32 3 Serial port 1 1 1 Interrupt sources 6 8 6 As can be seen from Table 1-4, the 8051 is a subset of the 8052; therefore, all programs written for the 8051 will run on the 8052, but the reverse is not true. CHAPTER 1: THE 8051 MICROCONTROLLERS 29 8031 microcontroller Another member of the 8051 family is the 8031 chip. This chip is often referred to as a ROM-less 8051 since it has 0K bytes of on-chip ROM. To use this chip you must add external ROM to it. This external ROM must contain the pro- gram that the 8031 will fetch and execute. Contrast that to the 8051 in which the ‘on-chip ROM contains the program to be fetched and executed but is limited to only 4K bytes of code. The ROM containing the program attached to the 8031 can be as large as 64K bytes. In the process of adding external ROM to the 8031, you lose two ports. That leaves only 2 ports (of the 4 ports) for I/O operations. To solve this problem, you can add extemal 1/O to the 8031. Interfacing the 8031 with memory and I/O ports such as the 8255 chip is discussed in Chapter 14. There are also various speed versions of the 8031 available from different companies. Various 8051 microcontrollers Although the 8051 is the most popular member of the 8051 family, you will not see “8051” in the part number. This is because the 8051 is available in differ- ent memory types, such as UV-EPROM, flash, and NV-RAM, all of which have different part numbers. A discussion of the various types of ROM will be given in Chapter 14, The UV-EPROM version of the 8051 is the 8751. The flash ROM ver- sion is marketed by many companies including Atmel Corp. and Dallas Semiconductor. The Atmel Flash 8051 is called AT89CS1, while Dallas Semiconductor calls theirs DS89C4x0 (DS89C420/430/440). The NV-RAM ver- sion of the 8051 made by Dallas Semiconductor is calied DSS000. There is also an OTP (one-time programmable) version of the 8051 made by various manufac- turers. Next we discuss briefly each of the above chips and describe applications where they are used. 8751 microcontroller This 8751 chip has only 4K bytes of on-chip UV-EPROM. Using this chip for development requires access to a PROM burner, as well as a UV-EPROM eras- er to erase the contents of UV-EPROM inside the 8751 chip before you can pro- gram it again. Because the on-chip ROM for the 8751 is UV-EPROM, it takes around 20 minutes to erase the 8751 before it can be programmed again. This has Jed many manufacturers to introduce flash and NV-RAM versions of the 8051, as we will discuss next. There are also various speed versions of the 8751 available from different companies. DS89C4x0 from Dallas Semiconductor (Maxim) Many popular 8051 chips have on-chip ROM in the form of flash memo- ry. The AT89CS1 from Atmel Corp. is one example of an 8051 with flash ROM. This is ideal for fast development since flash memory can be erased in seconds compared to the twenty minutes or more needed for the 8751. For this reason the AT89CS1 is used in place of the 8751 to eliminate the waiting time necded to erase the chip and thereby speed up the development time, Using the AT89CS1 to devel- op a microcontrolier-based system requires a ROM burner that supports flash ‘memory; however, a ROM eraser is not needed. Notice that in flash memory you 30 must erase the entire contents of ROM in order to program it again, This erasing of flash is done by the PROM bumer itself, which is why a separate eraser is not needed. To eliminate the need for a PROM burner, Dallas Semiconductor, now part of the Maxim Corp., has a version of the 8051/52 called DS89C4x0 (D$89C420/430/...) that can be programmed via the serial COM port of an IBM PC. Notice that the on-chip ROM for the DS89C4x0 is in the form of flash. The DS89C4x0 (420/430/440/450) comes with an on-chip loader, which allows the program to be loaded into the on-chip flash ROM while it is in the system. This can be done via the serial COM port of an IBM PC. This in-system program loading of the DS89C4x0 via a PC serial COM port makes it an ideal home devel- opment system. Dallas Semiconductor also has an NV-RAM version of the 8051 called DS5000. The advantage of NV-RAM is the ability to change the ROM con- tents one byte at a time. The DS5000 also comes with a loader, allowing it to be programmed via the PC’s COM port. See Table 1-5. From Table 1-5, notice that the DS89C4x0 is a really an 8052 chip since it has 256 bytes of RAM and 3 timers. More details of this chip are given throughout the book. Table 1-5: Versions of 8051/52 Microcontroller From Dallas Semiconductor (Maxim) Part Number ROM RAM _VO pins Timers Interrupts Vcc DS89C420/30_16K (Flash) 25633 3 é 3V DS89C440__32K (Flash) 25632 3 6 5V DS89C450__64K (Flash) 256 32 3 6 5V Ds5000 8K (NVRAM) 12832 2 6 3V DS80C320__ OK 25632 3 6 3V, DS87520___ 16K (UVROM) 25632 3 6 SV, ‘Source: [Link]/products/microcontrollers/8051_drop_in.cfim DS89C4x0 Trainer In Chapter 8, we discuss the design of DSS9C4x0 Trainer extensively. The MDER0S51 Trainer is available from [Link]. This Trainer allows you to program the DS89C4x0 chip from the COM port of the x86 IBM PC, with no need for a ROM burner. For a DS89C4x0-based trainer see [Link]. AT89C51 from Atmel Corporation The Atmel Corp. has a wide selection of 8051 chips, as shown in Tables 1- 6 and 1-7. For example, the AT89CS1 is a popular and inexpensive chip used in many small projects. It has 4K bytes of flash ROM. Notice the AT89C51-12PC, where “C” before the 51 stands for CMOS, which has a low power consumption, “[2” indicates 12 MHz, “P” is for plastic DIP package, “C” is for commercial. CHAPTER 1: THE 8051 MICROCONTROLLERS 31 ‘Table 1-6: Versions of 8051 From Atmel (All ROM Flash) Part Number ROM __RAM__V/O pins Timer _Interrupt Vcc __ Packaging AT89CSI aK 128) 32 2 6 SV. 40 ATS9LVS1__ 4K 128 32 2 6 3V. 40 ATS9ICIOST IK 64, 15 1 3 3V. 20 AT89C2051__—_ 2K 128 15 2 6 3V. 20 ATS9C52 8K. 128 32 3 8 SV. 40 ATS9LVS2__8K 128 32 3 8 3V. 40 Note: “C” in the part nuraber indicates CMOS. Table 1-7: Various Speeds of 8051 From Atmel. Part Number Speed Pins Packaging Use AT89C51-12PC. 12 MHz 40 DIP plastic commercial ‘AT89CS1-16PC 16MHz 40 DIP plastic commercial ‘AT89C51-20PC 20 MHz 40 DIP plastic commercial OTP version of the 8051 There are also OTP (one-time-programmable) versions of the 8051 avail- able from different sources. Flash and NV-RAM versions are typically used for product development. When a product is designed and absolutely finalized, the OTP version of the 8051 is used for mass production since it is much cheaper in terms of price per unit. 8051 family from Philips Another major producer of the 8051 family is Philips Corporation, Indeed, they have one of the largest selections of 8051 microcontrollers. Many of their products include features such as A-to-D converters, D-to-A converters, extended VO, and both OTP and flash. For the list of companies producing the 8051 fami- ly see the Web sites in the box below. See the following Web sites for 8051 products and their features from various companies: [Link]/[Link] [Link] 32 Review Questions Name three features of the 8051 2. What is the major difference between the 8051 and 8052 microcontrollers? 3. Give the size of RAM in each of the following. (a) 8051 (b) 8052 (¢) 8031 4, Give the size of the on-chip ROM in each of the following. (a) 8051 (b) 8052 (c) 8031 5. The 8051 is a(n) __-bit microprocessor. 6. State a major difference between the 8751, the AT89CSI, and the DS89C420/30. 7. True or false. The DS89C420/30 is realy an 8052 chip, 8. True or false. The DS89C420/30 has a loader embedded to the chip, therefore eliminating the need for ROM burner, 9. The DS89C420/30 chip has__bytes of on-chip ROM. 10, The DS89C420/30 chip has ___bytes of RAM. SUMMARY This chapter discussed the role and importance of microcontrollers in everyday life. Microprocessors and microcontrollers were contrasted and com- pared, We discussed the use of microcontrollers in the embedded market. We also discussed criteria to consider in choosing a microcontroller such as speed, memo- ry, VO, packaging, and cost per unit. The second section of this chapter described various family members of the 8051, such as the 8052 and 8031, and their features. In addition, we discussed various versions of the 8051 such as the AT89C51 and DS89C4x0, which are marketed by suppliers other than Intel. PROBLEMS SECTION 1.1: MICROCONTROLLERS AND EMBEDDED PROCESSORS, ‘True or False, A general-purpose microprocessor has on-chip ROM. Truc or False. A microcontroller has on-chip ROM. True or False. A microcontroller has on-chip I/O ports. True or False. A microcontroller has a fixed amount of RAM on the chip. What components are normally put together with the microcontroller into a single chip? Intel's Pentium chips used in Windows PCs need external and chips to store data and code. 7. List three embedded products attached to a PC. 8. Why would someone want to use an x86 as an embedded processor? 9. Give the name and the manufacturer of some of the most widely used 8-bit microcontrollers, 10. In Question 9, which microcontroller has the most manufacture sources? yaene 2 CHAPTER f: HE 8051 MICROCONTROLLERS 3 I. 12 13. 14. 15. In a battery-based embedded product, what is the most important factor in choosing a microcontroller? In an embedded controller with on-chip ROM, why does the size of the ROM matter? In choosing a microcontroller, how important is it to have multiple sources for that chip? What does the term "third-party support" mean? If a microcontroller architecture has both 8-bit and 16-bit versions, which of the following statements is true? (a) The 8-bit software will run on the 16-bit system. (b) The 16-bit software will run on the 8-bit system, SECTION 1.2: OVERVIEW OF THE 8051 FAMILY 16. 17. 18. 19. 20, 21 22. 23. 24, 25. 26. 27. 28. 29, 30. The 8751 has bytes of on-chip ROM. The AT89CS1 has bytes of on-chip RAM The 8051 has on-chip timer(s). The 8052 has___ bytes of on-chip RAM ‘The ROM-less version of the 8051 uses ___as the part number. The 8051 family has ___ pins for 1/O. The 8051 family has circuitry to support ___ serial ports, The 8751 on-chip ROM is of type The AT89C51 on-chip ROM is of type The DS5000 on-chip ROM is of type . The DS89C420/30 on-chip ROM is of type __ . Give the amount of ROM and RAM for the following chips. (a) AT89CS1_—_(b) DS89C420/30 (ce) DS89C440 Of the 8051 family, which memory type is the most cost effective if you are using a million of them in an embedded product? What is the difference between the 8031 and 8051? Of the 8051 microcontrollers, which one is the best for a home development environment? (You do not have access to a ROM burner.) ANSWERS TO REVIEW QUESTIONS SECTION 1.1: MICROCONTROLLERS AND EMBEDDED PROCESSORS i 2. 3 4. 5 6, 1 True A microcontroller-based system @ @ It's dedicated since it is dedicated to doing one type of job Embedded system means thatthe application and processor are combined into a single system, Having multiple sources for a given part means you are not hostage to one supplier. More importantly, competition among suppliers brings about lower cost for that product. 34 SECTION 1.2: OVERVIEW OF THE 8051 FAMILY 128 bytes of RAM, 4K bytes of on-chip ROM, four 8-bit I/O ports. ‘The 8052 has everything that the 8051 has, plus an extra timer, and the on-chip ROM is 8K bytes instead of 4K bytes. The RAM in the 8052 is 256 bytes instead of 128 bytes. 3. Both the 8051 and the 8031 have 128 bytes of RAM and the 8052 has 256 bytes. 4. @)AK bytes (b) 8K bytes (6) 0K bytes 5. 8 6. The main difference is the type of on-chip ROM. In the 8751 it is UV-EPROM; in the AT89CS1 itis flash; and in the DS89C420/30 itis flash with a loader on the chip. True True 16K 0. 256 CHAPTER 1: THE 8051 MICROCONTROLLERS. 35 36 CHAPTER 2 8051 ASSEMBLY LANGUAGE PROGRAMMING >> >> >> >> >> >> >> >> >> >> >> >> >> OBJECTIVES Upon completion of this chapter, you will be able to: List the registers of the 8051 microcontroller Manipulate data using the registers and MOV instructions Code simple 8051 Assembly language instructions Assemble and run an 8051 program Describe the sequence of events that occur upon 8051 power-up Examine programs in ROM code of the 8051 Explain the ROM memory map of the 8051 Detail the execution of 8051 Assembly language instructions Describe 8051 data types Explain the purpose of the PSW (program status word) register Discuss RAM memory space allocation in the 8051 Diagram the use of the stack in the 8051 Manipulate the register banks of the 8051 37 In Section 2,1 we look at the inside of the 8051. We demonstrate some of the widely used registers of the 8051 with simple instructions such as MOV and ADD. In Section 2.2 we examine Assembly language and machine language pro- ‘gramming and define terms such as mnemonics, opcode, operand, etc. The process of assembling and creating a ready-to-run program for the 8051 is discussed in Section 2.3, Step-by-step execution of an 8051 program and the role of the pro- ‘gram counter are examined in Section 2.4, In Section 2.5 we look at some widely used Assembly language directives, pseudocode, and data types related to the 8051. In Section 2.6 we discuss the flag bits and how they are affected by arith- metic instructions. Allocation of RAM memory inside the 8051 plus the stack and register banks of the 8051 are discussed in Section 2.7. SECTION 2.1: INSIDE THE 8051 In this section we examine the major registers of the 8051 and show their use with the simple instructions MOV and ADD. Registers D7 | D6 | bs | 4 [3 | 2 | oi | bo In the CPU, reg- isters are wsed to store information temporarily, That information could be a byte of data to be processed, or an address pointing to the data to be fetched. The vast majority of 8051 regis- ters are 8-bit registers. In the 8051 there is only one data type: 8 bits. The 8 bits of a register are shown in the diagram from the MSB (most significant bit) D7 to the LSB (least significant bit) DO. With an 8-bit data type, any data larger than 8 bits must be broken into 8-bit chunks before it is processed. Since there are a large number of registers in the 8051, we will concentrate on some of the widely used general-purpose registers and cover special registers in future chapters. See Appendix A.2 for a complete list of 8051 registers. A DPTR DPH DPL RO El Pc PC (program counter) RZ RD Figure 2-1 (b). Some 8051 16-bit Registers The most widely used registers of the 8051 are A (accuraulator), B, RO, RI, R2, R3, RA, RS, R6, R7, R6 DPTR (data pointer), and PC (program counter). All of the above registers are 8-bits, except DPTR and the program counter. The accumulator, register A, is used for all arithmetic and logic instructions. To understand Figure 2-1 (a). Some 8-bit the use of these registers, we will show them in the Registers of the 8051 context of two simple instructions, MOV and ADD, 38 RS RT MOV instruction ply stated, the MOV instruction copies data from one location to anoth- er. It has the following format: MOV destination, source ;copy source to dest. This instruction tells the CPU to move (in reality, copy) the source operand to the destination operand, For example, the instruction “MOV A, RO” copies the contents of register RO to register A. After this instruction is executed, register A will have the same value as register RO. The MOV instruction does not affect the source operand, The following program first loads register A with value SSH (that is 55 in hex), then moves this value around to various registers inside the CPU. Notice the “#” in the instruction. This signifies that it is a value. The importance of this will be discussed soon. MOV A, #55H ;load value 55H into reg. A MOV RO,A ;copy contents of A into RO ; (now A=RO=55H) MOV R1,A ;copy contents of A into R1 ; (Mow A=RO=R1=55H) MOV R2,A ;copy contents of A into R2 ynow A=RO=R1=R2=55H) MOV R3,#95H ; load value 95H into R3 ; (now R3=95H) MOV A,R3 jcopy contents of R3 into A jmow A=R3=95H) When programming the 8051 microcontroller, the following points should be noted: 1, Values can be loaded directly into any of registers A, B, or RO - R7. However, to indicate that it is an immediate value it must be preceded with a pound sign (#). This is shown next. MOV A, #23H MOV RO, #12H MOV R1,#1FH MOV R2,#2BH MOV B,#3CH MOV R7,#9DH MOV R5,#0F9H 23H into A (A=23H) 12H into RO (R0=12H) 1FH into Ri (R1=1FH) 2BH into R2 (R2=2BH) 3CH into B (B=3CH) 9DH into R7 (R7=9DH) F9H into R5 (R5S=F9H) MOV R6,#12 12 decimal (OCH) reg. R6 (R6=0CH) Notice in instruction “Mov RS, #0F9H" that a 0 is used between the # and F to indicate that F is a hex number and not a letter. In other words “MOV RS, #F9H” will cause an error. CHAPTER 2: 8051 ASSEMBLY LANGUAGE PROGRAMMING 39 2. If-values 0 to F are moved into an 8-bit register, the rest of the bits are assumed to be all zeros. For example, in “MOV A, #5” the result will be A = 05; that is, A = 00000101 in binary. 3. Moving a value that is too large into a register will cause an error. MOV A, #7F2H 7F2H > 8 bits (FH) MOV R2,#456 456 > 255 decimal (FFH) 4, A value to be loaded into a register must be preceded with a pound sign (#). Otherwise it means to load from a memory location. For example “MOY A, 17H” means to move into A the value held in memory location 17H, which could have any value. In order to load the value 17H into the accumulator we must write “MOV A, #17H" with the # preceding the number. Notice that the absence of the pound sign will not cause an error by the assembler since it is a valid instruction, However, the result would not be what the programmer intended. This is a common error for beginning programmers in the 8051. ADD instruction The ADD instruction has the following format: ADD A,source ADD the source operand © the accumulator The ADD instruction tells the CPU to add the source byte to register A and Pput the result in register A. To add two numbers such as 25H and 34H, each can be moved to a register and then added together: MOV A, #25H jload 25H into A MOV R2,#34H — ;load 34H into R2 ADD A,R2 jadd R2 to accumulator (A = A + R2) Executing the program above results in A = 59H (2SH + 34H = 59H) and R2 = 34H. Notice that the content of R2 does not change. The program above can be written in many ways, depending on the registers used. Another way might be: MOV R5,#25H ;load 25H into RS (R5=25H) MOV R7,#34H ;load 34H into R7 (R7=34H) MOV A, #0 ;load 0 inte A (A=0,clear A) ADD A,R5 jadd to A content of RS jwhere A = A + RS ADD A,R7 jadd to A content of R7 jwhere A = A + R7 The program above results in A = S9H. There are always many ways to write the same program. One question that might come to mind after looking at the program above, is whether it is necessary to move both data items into registers 40 before adding them together. The answer is no, it is not necessary. Look at the fol- lowing variation of the same program: MOV A,#25H ;load one operand into A (A=25H) ADD A,#34H ;add the second operand 34H to A In the above case, while one register contained one value, the second value foliowed the instruction as an operand. This is called an immediate operand. The examples shown so far for the ADD instruction indicate that the source operand can be cither a register or immediate data, but the destination must always be reg- ister A, the accumulator. In other words, an instruction such as “ADD R2, #12H” is invalid since register A (accumulator) must be involved in any arithmetic oper- ation, Notice that “ADD 4 , A” is also invalid for the reason that A must be the destination of any arithmetic operation. To put it simply: In the 8051, register A must be involved and be the destination for all arithmetic operations. The forego- ing discussion explains why register A is referred to as the accumulator. The for- mat for Assembly language instructions, descriptions of their use, and a listing of legal operand types are provided in Appendix A.1 There are two 16-bit registers in the 8051: PC (program counter) and DPTR (data pointer). The importance and use of the program counter are covered in Section 2.3. The DPTR register is used in accessing data and is discussed in Chapter 5 where addressing modes are covered. Review Questions 1. Write the instructions to move value 34H into register A and value 3FH into register B, then add them together. 2. Write the instructions to add the values 16H and CDH, Place the result in reg- ister R2. 3. True or false. No value can be moved directly into registers RO - R7. 4, What is the largest hex value that can be moved into an 8-bit register? What is the decimal equivalent of the hex value? 5. The vast majority of registers in 8051 are___ bits. SECTION 22: INTRODUCTION TO 8051 ASSEMBLY PROGRAMMING In this section we discuss Assembly language format and define some widely used terminology associated with Assembly language programming. While the CPU can work only in binary, it can do so at a very high speed. For humans, however, it is quite tedious and slow to deal with Os and 1s in order to program the computer. A program that consists of Os and Is is called machine language. In the early days of the computer, programmers coded programs in machine language. Although the hexadecimal system was used as a more efficient ‘way to represent binary numbers, the process of working in machine code was stil cumbersome for humans. Eventually, Assembly languages were developed that provided mnemonics for the machine code instructions, plus other features that made programming faster and less prone to error. The term mnemonic is frequent- ly used in computer science and engineering literature to refer to codes and abbre- CHAPTER 2: 8051 ASSEMBLY LANGUAGE PROGRAMMING 41 viations that are relatively easy to remember. Assembly language programs must be translated into machine code by a program called an assembler. Assembly lan- guage is referred to as a low-level language because it deals directly with the inter- nal structure of the CPU. To program in Assembly language, the programmer must know all the registers of the CPU and the size of each, as well as other details. Today, one can use many different programming languages, such as BASIC, Pascal, C, C+, Java, and numerous others. These languages are called high-level languages because the programmer does not have to be concerned with the intemal details of the CPU. Whereas an assembler is used to translate an Assembly language program into machine code (sometimes also called object code or opcode for operation code), high-level languages are translated into machine code by a program called a compiler, For instance, to write a program in C, one must use aC compiler to translais the program into machine language. Now we look at 8051 Assembly language format and use an 8051 assembler to create a ready-to-run program. Structure of Assembly language An Assembly language program consists of, among other things, a series of lines of Assembly language instructions. An Assembly language instruction consists of a mnemonic, optionally followed by one or two operands. The operands are the data items being manipulated, and the mnemonics are the com- mands to the CPU, telling it what to do with those items, ORG OH ystart (origin) at location 0 MOV R5,#25H ;load 25H into RS MOV R7,#34H ;load 34H into R7 Mov A, #0 ;load 0 into A ADD A,RS jadd contents of R5 to A mow A = A + RS ADD A,R7 jadd contents of R7 to A mow A = A + R7 ADD A,#12H = ;add to A value 12H mow A = A + 12H HERE: SUMP HERE, ;stay in this loop END jend of asm source file Program 2-1: Sample of an Assembly Language Program A given Assembly language program (see Program 2-1) is a series of state- ‘ments, or lines, which are either Assembly language instructions such as ADD and MOY, or statements called directives. White instructions tell the CPU what to do, directives (also called pseudo-instructions) give directions to the assembler. For example, in the above program while the MOV and ADD instructions are com- mands to the CPU, ORG and END are directives to the assembler. ORG tells the 42 assembler to place the opcode at memory location 0 while END indicates to the assembler the end of the source code. In other words, one is for the start of the pro- gram and the other one for the end of the program, An Assembly language instruction consists of four fields: ({label:] mnemonic [operands] _[;conment] Brackets indicate that a field is optional, and not all lines have them. Brackets should not be typed in. Regarding the above format, the following points should be noted. 1. The label field allows the program to refer to a line of code by name. The label field cannot exceed a certain number of characters. Check your assembler for the rule. 2. The Assembly language mnemonic (instruction) and operand(s) fields togeth- er perform the real work of the program and accomplish the tasks for which the program was written. In Assembly language statements such as ADD A,B MOV A, #67 ADD and MOV are the mnemonics, which produce opcodes; and “A, B” and “A, #67” are the operands. Instead of a mnemonic and an operand, these two fields could contain assembler pseudo-instructions, or directives. Remember that directives do not generate any machine code (opcode) and are used only by the assembler, as opposed to instructions that are translated into machine code (opcode) for the CPU to execute. In Program 2-1 the commands ORG (origin) and END are examples of directives (some 8051 assemblers use .ORG and .END). Check your assembler for the rules. More of these pseudo-instrue- tions are discussed in detail in Section 2.5. 3. The comment field begins with a semicolon comment indicator “;”. Comments may be at the end of a line or on a line by themselves. The assem- bler ignores comments, but they are indispensable to programmers. Although comments are optional, it is recommended that they be used to describe the program and make it easier for someone else to read and understand, or for the programmer to remember what they wrote, 4, Notice the label “HERE” in the label field in Program 2-1. Any label referring to an instruction must be followed by a colon symbol, “:". In the SIMP (short jump instruction), the 8051 is told to stay in this loop indefinitely. If your sys- tem has a monitor program you do not need this line and it should be deleted from your program. In the next section we will see how to create a ready-to- run program, CHAPTER : 8051 ASSEMBLY LANGUAGE PROGRAMMING 43 Review Questions 1, What is the purpose of pseudo-instruction: 2 are translated by the assembler into machine code, whereas are not. 3. True or false, Assembly language is a high-level language 4. Which of the following produces opcode? (a) ADD AR2 (b) MOV A,#12 (c) ORG 2000H (d) SIMP HERE 5. Pscudo-instructions are also called 6. True or false. Assembler directives are not used by the CPU itself. They are simply a guide to the assembler, 7. In question 4, which one is an assembler directive? SECTION 2.3: ASSEMBLING AND RUNNING AN 8051 PROGRAM Now that the basic form of an Assembly language program has been given, the next question is: How it is created, assembled, and made ready to run? The steps to ereate an executable Assembly language program are outlined as follows. EDITOR 1. First we use an editor to type in a PROGRAM program similar to Program 2-1. Many excellent editors or word iyfile,axm) processors are available that can be Y used to create and/or edit the pro- ASSEMBLER gram. A widely used editor is the PROGRAM MS-DOS EDIT program (or eel Notepad in Windows), which comes | ™*letst with all Microsoft operating systems. Notice that the editor must be able to moos) J gr sinerob les produce an ASCII file. For many TINRER assemblers, the file names follow the PROGRAM usual DOS conventions, but the source file has the extension “asm” 1 or “sre, depending on which assem- [Link] bler you are using. Check your assembler for the convention. The t “asm” extension for the soures file is On used by an assembler in the next PROGRAM step. 2. The “asm” source file containing the imyfile hex program code created in step 1 is fed to an 8051 assembler. The assembler Steps to Create a Program converts the instructions into 44 machine code. The assembler will produce an object file and a list file. The extension for the object file is “obj” while the extension for the list file is “Ist”. 3. Assemblers require a third step called inking. The link program takes one or more object files and produces an absolute object file with the extension “abs”. This abs file is used by 8051 trainers that have a monitor program, 4, Next, the “abs” file is fed into a program called “OH” (object to hex convert- er), which creates a file with extension “hex” that is ready to bum into ROM. This program comes with all 8051 assemblers. Recent Windows-based assem- blers combine steps 2 through 4 into one step. More about “asm” and “obj” files The “asm” file is also called the source file and for this reason some assem- blers require that this file have the “sre” extension. Check your 8051 assembler to see which extension it requires. As mentioned earlier, this file is created with an editor such as DOS EDIT or Windows Notepad. The 8051 assembler converts the asm file’s Assembly language instructions into machine language and provides the obj (object) file. In addition to creating the object file, the assembler also produces the Ist file (list file). Ist file The Ist (list) file, which is optional, is very useful to the programmer because it lists all the opcodes and addresses as well as errors that the assembler detected. Many assemblers assume that the list file is not wanted unless you indi- cate that you want to produce it. This file can be accessed by an editor such as DOS EDIT and displayed on the monitor or sent to the printer to produce a hard copy. The programmer uses the list file to find syntax errors. It is only after fixing all the errors indicated in the Ist file that the obj file is ready to be input to the link- er program. 1 0000 ORG OH ;start (origin) at 0 2 0000 7D25, MOV R5,#25H load 25H into R5 3 0002 7F34 MOV R7,#34H — ; load 34H into R7 4 0004 7400 MoV A, #0 load 0 into A 5 0006 2D ADD A,RS jadd contents of R5 to A mow A = A + RS 6 0007 2F ADD A,R7 jadd contents of R7 to A imow A = A + R7 7 0008 2412 ADD A,#12H jadd to A value 12H mow A = A + 12H 8 000A 80FE HERE: SUMP HERE stay in this loop 9 000c END yend of asm source file Program 2-1: List File CHAPTER 2: 8051 ASSEMBLY LANGUAGE PROGRAMMING 45 Review Questions True or false. The DOS program EDIT produces an ASCII file, Truc or false. Generally, the extension of the source file is “asm” or “sre”. Which of the following files can be produced by the DOS EDIT program? (a) [Link] (b) [Link] (c) [Link] (d) [Link] 4, Which of the following files is produced by an 8051 assembler? (a) [Link] (b) [Link] (c) [Link] (d) [Link] 5. Which of the following files lists syntax errors? (a) [Link] (b) [Link] (c) myprog-hex (4) myprog lst SECTION 2.4: THE PROGRAM COUNTER AND ROM SPACE IN THE 8051 In this section we examine the role of the program counter (PC) register in executing an 8051 program. We also discuss ROM memory space for various 8051 family members. Program counter in the 8051 Another important register in the 8051 is the PC (program counter). The program counter points to the address of the next instruction to be executed, As the CPU fetches the opcode from the program ROM, the program counter is incre- mented to point to the next instruction. The program counter in the 8051 is 16 bits wide. This means that the 805} can access program addresses 0000 to FFFFH, a total of 64K bytes of code. However, not all members of the 8051 have the entire 64K bytes of on-chip ROM installed, as we will see soon. Where does the 8051 wake up when it is powered? We will discuss this important topic next. Where the 8051 wakes up when it is powered up One question that we must ask about any microcontroller (or microproces- sor) is: At what address does the CPU wake up upon applying power to it? Each microprocessor is different. In the case of the 8051 family (that is, all members regardless of the maker and variation), the microcontrollers wakes up at memory address 0000 when it is powered up. By powering up we mean applying Vec to the RESET pin as discussed in Chapter 4. In other words, when the 8051 is pew- cred up, the PC (program counter) has the value of 0000 in it. This means that it expects the first opcode to be stored at ROM address 0000H, For this reason in the 8051 system, the first opcode must be burned into memory location 0000H of pro- gram ROM since this is where it looks for the first instruction when it is booted. We achieve this by the ORG statement in the source program as shown earlier, Next, we discuss the step-by-step action of the program counter in fetching and executing a sample program. Placing code in program ROM To get a better understanding of the role of the program counter in fetch- ing and executing a program, we examine the action of the program counter as cach instruction is fetched and executed. First, we examine once more the list file 46 of the sample program and how the code is placed in the ROM of an 8051 chip. ‘As we can see, the opcode and operand for cach instruction are listed on the left side of the list file, (0000 0000 0002 0004 0006 GeONE 6 0007 7 0008 8 000A 9 o00c 725 1E34 7400 2D 2F 2412 B0FE ‘ORG OH MOV R5, #25H MOV R7,#34H MOV A, #0 ADD A,R5 ADD A,R7 ADD A, #12H HERE: SUMP HERE END ;start at location 0 oad 25H into R5 oad 34H into R7 cad 0 into A jadd contents of RS to A mow A = A + RS jadd contents of R7 to A mnow A = A + R7 jadd to A value 12H wnow A A+ 12H jstay in this loop vend of asm source file Program 2-1: List File ROM Address Machine Language Assembly Language loc tains 7D, which is the opcode for moving a value into register R5, and address 0001 con- tains the operand (in this case 2SH) to be moved to RS, Therefore, the instruction “MOV R5,#25H” has a machine code of “7D25”, where 7D is the opcode and 25 is the operand. Similarly, the machine code “7F34” is located in memory locations 0002 and 0003 and rep- resents the opcode and the operand for the instruction “MOV R7,#34H”, In the same way, machine code “7400” is located in mem- ory locations 0004 and 0005 and represents the opcode and the operand for the instruetion “MOV A, #0”. The memory location 0006 has 0000 7D25 MOV RS, #254 0902 7E34 MOV _R7, #344 004 7400 MOV_A, #0 0006 2D ADD _A,R5 0007 2F ADD A,R7 008 2412 ADD A, #128 00a 80FE HERE: SJMP HERE After the program is bumed into ROM of an 8051 family member such as 8751 or AT8951 or DS5000, the opcode and operand are placed in ROM memory mns starting at 0000 as shown in the list below. The list shows that address 0000 con- Program 2-1: ROM Contents Address__Code 0000 7D. 0001 25 0002 TF. 0003 34 0004 74, 0005 00, (0006 2D. 0007 2F 0008) 24 0009 12 000A 80, 0008 FE the opcode of 2D, which is the opcode for the CHAPTER 2: 8051 ASSEMBLY LANGUAGE PROGRAMMING 4 a instruction “ADD A, R5” and memory location 0007 has the content 2F, which is the opcode for the “ADD A, R7” instruction. The opeode for the instruction “ADD A, #12H" is located at address 0008 and the operand 12H at address 0009. The memory location 000A has the opcode for the SIMP instruction and its target address is located in location 000B. The reason the target address is FE is explained in the next chapter. Executing a program byte by byte Assuming that the above program is burned into the ROM of an 8051 chip (or 8751, AT89S1, or DS5000), the following is a step-by-step description of the action of the 8051 upon applying power to it. 1, When the 8051 is powered up, the PC (program counter) has 0000 and starts to fetch the first opcode from location 0000 of the program ROM. In the case of the above program the first opcode is 7D, which is the code for moving an operand to RS, Upon executing the opcode, the CPU fetches the value 25 and places it in RS. Now one instruction is finished, Then the program counter is incremented to point to 0002 (PC = 0002), which contains opcode 7F, the opcode for the instruction “MOV RT...” 2. Upon executing the opcode 7F, the value 34H is moved into R7. Then the pro- gram counter is incremented to 0004, 3. ROM location 0004 has the opcode for the instruction “MOV A, #0". This, instruction is executed and now PC = 0006, Notice that all the above instruc- tions are 2-byte instructions; that is, each one takes two memory locations. 4, Now PC = 0006 points to the next instruction, which is “ADD A, RS”. This is a L-byte instruction, After the execution of this instruction, PC = 0007. 5. The location 0007 has the opcode 2F, which belongs to the instruction “ADD A, R7”, This also is a 1-by'e instruction, Upon execution of this instruction, PC is incremented to 0008. This process goes on until all the instructions are fetched and executed. The fact that the program counter points at the next instruction to be executed explains why some microprocessors (notably the x86) call the program counter the instruction pointer. ROM memory map in the 8051 family As we saw in the last chapter, some family members have only 4K bytes of on-chip ROM (e.g., 8751, AT8951) and some, such as the AT89C52, have 8K bytes of ROM. Dallas Semiconductor's DSS000-32 has 32K bytes of on-chip ROM. Dallas Semiconductor also has an 8051 with 64K bytes of on-chip ROM. The point to remember is that no member of the 8055 family can access more than 64K bytes of opcode since the program counter in the 8051 is a 16-bit register (0000 to FFFF address range). It must be noted that while the first location of pro- gram ROM inside the 8051 has the address of 0000, the last location can be dif ferent depending on the size of the ROM on the chip. Among the 8051 family members, the 8751 and AT8951 have 4K bytes of on-chip ROM. This 4K bytes of ROM memory has memory addresses of 0000 to OFFFH. Therefore, the first loca- tion of on-chip ROM of this 8051 has an address of 0000 and the last location has the address of OFFFH. Look at Example 2-1 to see how this is computed. 48 [Example 2-1 Find the ROM memory address of each of the following 8051 chips. (a) AT89C51 with 4KB (b) DS89C420 with 16KB (c) DSS000-32 with 32KB Solution: (a) With 4K bytes of on-chip ROM memory space, we have 4096 bytes (4 x 1024 4096). This maps to address locations of 0000 to OFFFH, Notice that 0 is always the first location. (b) With 16K bytes of on-chip ROM memory space, we have 16,384 bytes (16 x 1024 = 16,384), which gives 0000 - 3FFFH, (c) With 32K bytes we have 32,768 bytes (32 x 1024 = 32,768). Converting 32,768 to hex, we get 8000H; therefore, the memoty space is 0000 to 7FFFH. byte byte byte 000 000 0000 OFFF 8051 1 ATS9CS1 ei! Ds89¢420/30 FFF DS5000-32 Figure 2-3. 8051 On-Chip ROM Address Range Review Questions 1, In the 8051, the program counter is bits wide. 2. True or false. Every member of the 8051 family, regardless of the maker, wakes up at memory 0000H when it is powered up. 3. At what ROM Iocation do we store the first opcode of an 8051 program? 4. The instruction “Mov A,#44H” isa -byte instruction. 5. What is the ROM address space for the 8052 chip? SECTION 2.5: 8051 DATA TYPES AND DIRECTIVES In this section we look at some widely used data types and directives sup- ported by the 8051 assembler. 8051 data type and directives The 8051 microcontroller has only one data type. It is 8 bits, and the size of each register is also 8 bits. It is the job of the programmer to break down data CHAPTER 2: 8951 ASSEMBLY LANGUAGE PROGRAMMING. 49 larger than 8 bits (00 to FFH, or 0 to 255 in decimal) to be processed by the CPU. For examples of how to process data larger than 8 bits, see Chapter 6. The data types used by the 8051 can be positive or negative. A discussion of signed num- bers is given in Chapter 6. DB (define byte) The DB directive is the most widely used data directive in the assembler. It is used to define the 8-bit data. When DB is used to define data, the numbers can be in decimal, binary, hex, or ASCII formats. For decimal, the “D” after the deci- ‘mal number is optional, but using “B” (binary) and “H” (hexadecimal) for the oth- ers is required. Regardless of which is used, the assembler will convert the num- bers into hex. To indicate ASCII, simply place the characters in quotation marks (‘like this’), The assembler will assign the ASCII code for the numbers or charac- ters automatically. The DB directive is the only directive that can be used to define ASCII strings larger than two characters; therefore, it should be used for all ASCII data definitions. Following are some DB examples: ORG 500H DATAL: DB 28 ;DECIMAL(1C in hex) DATA2: DB 00110101B BINARY (35 in hex) DATA3: DB 39H j HEX ORG 510H DATAd: DB "2591" ;ASCII NUMBERS ORG 518H DATA6: DB "My name is Joe" =; ASCII CHARACTERS Either single or double quotes can be used around ASCII strings. This can be useful for strings, which contain a single quote such as “O'Leary”. DB is also used to allocate memory in byte-sized chunks. Assembler directives The following are some more widely used directives of the 8051 ORG (origin) The ORG directive is used to indicate the beginning of the address. The number that comes after ORG can be either in hex or in decimal. If the number is not followed by H, it is decimal and the assembler will convert it to hex. Some assemblers use “. ORG” (notice the dot) instead of “ORG” for the origin directive. Check your assembler. EQU (equate) This is used to define a constant without occupying a memory location. The EQU directive does not set aside storage for a data item but associates a con- stant value with a data label so that when the label appears in the program, its con- stant value will be substituted for the label. The following uses EQU for the count- er constant and then the constant is used to load the R3 register. 50 COUNT EQU 25 MOV R3,#COUNT When executing the instruction “MOV 3, #COUNT”, the register R3 will be loaded with the value 25 (notice the # sign). What is the advantage of using EQU? Assume that there is a constant (a fixed value) used in many different places in the program, and the programmer wants to change its value throughout, By the use of EQU, the programmer can change it once and the assembler will change all of its occurrences, rather than search the entire program trying to find every occur- rence, END directive Another important pseudocode is the END directive. This indicates to the assembler the end of the source (asm) file. The END directive is the last line of an 8051 program, meaning that in the source code anything after the END directive is ignored by the assembler. Some assemblers use “. END” (notice the dot) instead of “END”. Rules for tabeis in Assembly language By choosing label names that are meaningful, a programmer can make a program much easier to read and maintain, There are several rules that names must follow. First, each label name must be unique, The names used for labels in Assembly language programming consist of alphabetic letters in both uppercase and lowercase, the digits 0 through 9, and the special characters question mark (2), period (.), at (@), underline (_), and dollar sign ($). The first character of the label must be an alphabetic character. In other words it cannot be a number, Every assembler has some reserved words that must not be used as labels in the program. Foremost among the reserved words are the mnemonics for the instructions. For example, “MOV” and “ADD” are reserved since they are instruction mnemonics. In addition to the mnemonics there are some other reserved words. Check your assembler for the list of reserved words. Review Questions 1, The ___ directive is always used for ASCII strings. 2. How many bytes are used by the following? DATA_1: DB "AMERICA" 3. What is the advantage in using the EQU directive to define a constant value? 4, How many bytes are set aside by each of the following directives? (a) ASC_DATA: DB "2234" (b) MY_DATA: DB "ABC1234" 5, State the contents of memory locations 200H - 205H for the following: ORG 200H MYDATA: DB "ABC123" CHAPTER 2: 8051 ASSEMBLY LANGUAGE PROGRAMMING 51 SECTION 2.6: 8051 FLAG BITS AND THE PSW REGISTER Like any other microprocessor, the 805! has a flag register to indicate arithmetic conditions such as the carry bit. The flag register in the 8051 is called the program status word (PSW) register. In this section we discuss various bits of this register and provide some examples of how it is altered. PSW (program status word) register The program status word (PSW) register is an 8-bit register. It is also referred to as the flag register. Although the PSW register is 8 bits wide, only 6 Dits of it are used by the 8051, The two unused bits are user-definable flags. Four of the flags are called conditional flags, meaning that they indicate some condi- tions that result after an instruction is executed, These four are CY (carry), AC (auxiliary carry), P (parity), and OV (overflow), As seen from Figure 2-4, the bits PSW.3 and PSW.4 are designated as RSO and RSI, respectively, and are used to change the bank registers. They are explained in the next section. The PSW.5 and PSW.1 bits are general-purpose sta- tus flag bits and can be used by the programmer for any purpose. In other words, they are user definable, See Figure 2-4 for the bits of the PSW register. cy ] ac ro | Rs | rso [| ov B P cy Psw7 Carty flag. AC PSW6 Auxiliary cary fag, FO PSW.S Available 1 the user far general purpose, RSI PSW4 Register Bank selector bit | RO PSW3 Register Bank selector bit 0. OV PsWw2 Overflow flag. = PSW User-definable bit. P PSWwa Parity flag, Sev/cleared by hardware each instuction cycle to indicate an odd/even number of } bits in the accumulator, RSI__RSO Register Bank Address a) 0 (OOH - 07H 0 1 1 8H - OFH, 1 0 2 LOH = 17H U I 3 18H = 1FH Figure 2-4, Bits of the PSW Register The following is a brief explanation of four of the flag bits of the PSW reg- ister. The impact of instructions an these registers is then discussed. 52 CY, the carry flag This flag is set whenever there is a carry out from the D7 bit. This flag bit is affected after an $-bit addition or subtraction. It can also be set to 1 or 0 direct- ly by an instruction such as “SETB C” and “CLR Cc” where “SETB C” stands for “set bit carry” and “CLR C” for “clear carry”. More about these and other bit- addressable instructions will be given in Chapter 8. AG, the auxiliary carry flag If there is a carry from D3 to D4 during an ADD or SUB operation, this bit is set; otherwise, itis cieared. This flag is used by instructions that perform BCD (binary coded decimal) arithmetic. See Chapter 6 for more information. P, the parity flag The parity flag reflects the number of Is in the A (accumulator) register only. If the A register contains an odd number of Is, then P = 1. Therefore, P = 0 if A has an even number of Is. OV, the overfiow flag This flag is set whenever the result of a signed number operation is too large, causing the high-order bit to overftow into the sign bit, In general, the carry flag is used to detect errors in unsigned arithmetic operations. The overflow flag is only used to detect errors in signed arithmetic operations and is discussed in detail in Chapter 6. ADD instruction and PSW Table 2-1: Instructions That Affect Next we examine the impact Flag ofthe ADD instruction on the flag bits _ ot the PSW tacster Wstruction CY OV Ac CY, AC, and P of the PSW register. Some examples shouid clarify their x. status. Although the flag bits affected by the ADD instruction are CY (carry flag), P (parity flag), AC (ausitiary carry flag), and OV (overflow flag) we will focus on flags CY, AC, and P for now. A discussion of the overflow flag is given in Chapter 6, since it relates only to signed number arith- metic. How the various flag bits are usod in programming is discussed in future chapters in the context of many applications. See Examples 2-2 through 2-4 for the impact on selected flag bits as a result of the ADD instruction. Al x x x ><] ><] ><] ><] >< MOV, bit CINE Note: X can be 0 or 1 >A} ><] >a] >< | >4| >¢| 24] >] —|>< | >¢] 24] | >} ><] >< CHAPTER 2: 8651 ASSEMBLY LANGUAGE PROGRAMMING 3 Example 2-2 ‘Show the status of the CY, AC, and P flags after the addition of 38H and 2FH in the fol- lowing instructions. MOV A, #38H ADD A,#2FH jafter the addition A=67H, CY=0 Solution: 38 QoLtL000 +2E OO1OIIL 67 01100111 CY=0 since there is no carry beyond the D7 bit. AC= L since there is a carry from the D3 to the D4 bit. P= | since the accumulator has an odd number of \s (it has five 1s). Example 2-3 Show the status of the CY, AC, and P flags after the addition of 9CH and 644 in the fol- lowing instructions. MOV A, #9CH ADD A, #64 after addition A=00 and Cy=1 Solution: 9c 10011100 +64 91100100 100 00000000 CY = 1 since there is a carry beyond the D7 bit. AC = I since there is a carry from the D3 to the D4 bit. P= 0 since the accumulator has an even number of 1s (it has zero 1s). [Example 2-4 Show the status of the CY, AC, and P flags after the addition of 88H and 93H in the fol- lowing instructions. MOV A, #88H ADD A,#93H jafter the addition A=1BH,CY=1 Solution: 88 10001000 +93 10010011 MB 00011011 CY= 1 since there is a carry beyond the D7 bit. AC = 0 since there is no carry from the D3 to the D4 bit. P= 0 since the accumulator has an even number of Is (it has four 1s). 54 Review Questions The flag register in the 8051 is called. What is the size of the flag register in the 8051? Which bits of the PSW register are user-definable? Find the CY and AC flag bits for the following code. Bune MOV A, #0FFH ADD A,#01 MOV A, #0C2H ADD A, #3DH Find the CY and AC flag bits for the following code, SECTION 2.7: 8051 REGISTER BANKS AND STACK The 8051 microcontroller has a total of 128 bytes of RAM. In this section we discuss the allocation of these 128 bytes of RAM and examine their usage as registers and stack. RAM memory space allocation in the 8051 There are 128 bytes of RAM in the 80S1 (some members, notably the 8052, have 256 bytes of RAM). The 128 bytes of RAM inside the 8051 are assigned addresses 00 to 7FH. As we directly as memory locations. These gtoups as follows. 1, A total of 32 bytes from locations 00 to IF hex are set aside for reg- ister banks and the stack. A total of 16 bytes from locations 20H to 2FH are set aside for bit- addressable read/write memory. detailed discussion of bit-address- able memory and instructions is given in Chapter 8. A total of 80 bytes from locations 30H 10 7FH are used for read and write storage, or what is normally called a scratch pad. These 80 locations of RAM are widely used for the purpose of storing data and parameters by 8051 programmers. ‘We will use them in future chap- ters to store data brought into the CPU via VO ports. Register banks in the 8051 will see in Chapter 5, they can be accessed 128 bytes are divided into three different J | Scratch Pad RAM F Bit-Addressable RAM ‘egister Bank 3 Register Bank 2 Register Bank 1 (Stack) Register Bank 0 Figure 2-5. RAM Allocation in the 8051 As mentioned earlier, a total of 32 bytes of RAM are set aside for the reg- ister banks and stack. These 32 bytes are divided into 4 banks of registers in which CHAPTER 2: 8051 ASSEMBLY LANGUAGE. PROGRAMMING 55

You might also like