Solution manual the 8051 microcontroller based embedded systems

Page 1

Solution manual for the The 8051 microcontroller based Embedded Systems http://www.mhhe.com/patel/mbes

Manish K Patel

The 8051 Microcontroller based Embedded Systems, First edition, McGraw‐Hill Education http://www.mhhe.com/patel/mbes


CHAPTER 1 INTRODUCTION TO MICROCONTROLLERS 1.

The differences between microprocessor and microcontrollers are given in the following table. Microprocessor Microprocessor is complete functional CPU i.e. it contains ALU, registers, stack pointer, program counter, instruction decode and control unit and interrupt processing circuits. Microprocessor instruction sets are data processing intensive, means powerful addressing modes and many instructions to move data between memory and CPU to handle large volumes of data. Microprocessor based products are primarily designed to interact with humans and are more flexible to design Access times for external memory and I/O devices are more, resulting in a slower system. Microprocessor based systems require support devices and are usually bulkier, costly, less reliable and consume more power. Software protection is not possible because of the requirement of external code memory.

2.

Microcontroller Microcontroller is complete functional microcomputer i.e. it contains the circuitry of microprocessor and in addition it has built in memory (ROM, RAM), I/O circuits and peripherals necessary for an application. Microcontrollers have instruction sets that are related to the control of inputs and outputs, means they have many bit handling instructions along with byte processing instructions. Microcontroller based products are primarily designed to interact with machines; once a system is designed they are less flexible. Access times for on-chip memory and I/O devices are less, resulting in a faster system. Microcontroller based systems require less external hardware, reducing PCB size and hence are compact, cheaper, more reliable and consume less power. Software protection is possible because of on-chip code memory.

Various applications of microcontrollers are given below: • •

Household appliances: Microwave oven, washing machine, coffee machines, refrigerators, digital cameras, alarm clocks, toys, home security systems, remote controllers, exercise machines, sewing machines, air conditioners etc. Office and commercial appliances: Fax machine, photocopier, scanner or printer machine, intercom, computer systems (discussed below), calculators, ATM

The 8051 Microcontroller based Embedded Systems, First edition, McGraw‐Hill Education http://www.mhhe.com/patel/mbes


machines, CCTV camera and surveillance systems, point of sale systems, weighing scales, elevators, lifts, and many products included in household appliances. • Telecommunication: Telephones, phone answering machines, mobile phones, satellites, etc. • Entertainment and gaming: Televisions, VCRs, music players, stereo systems, set top boxes, play stations-video games, musical instruments etc. • Automotive industry: Fuel injection, ABS, ignition, power windows and seats, climate control, air bags, brake control etc. • Industrial automation and manufacturing: Motor control systems, data acquisition and supervisory systems, industrial robots, electronic metering etc. • Electronic measurement instruments: Digital multimeters, frequency synthesizers and oscilloscopes, logic analyzers, spectrum analyzers, digital thermometers, tachometers etc. • Biomedical systems: ECG recorder, blood cell analyzers, glucose monitor, patient monitoring systems etc. • Computer systems: Keyboard controller, CD drive or hard disk controller, CRT controller, DRAM controller, printer controller, LAN controller etc. • Military weapons, guidance and positioning systems. • Aerospace industry. And any automatic or semiautomatic devices around us usually contain microcontrollers. 3.

Different on-chip resources available in microcontrollers are: 1. CPU 2. RAM 3. ROM 4. Interrupt Controller 5. I/O Ports 6. Timer/Counter 7. A/D or D/A Converter 8. UART

4.

Microcode is a group of instructions (usually referred as microinstructions) used to implement the instructions of a microcontroller/ processor. It resides in a ROM or a programmable logic array (PLA) that is part of the microcontroller chip. The microinstruction is group of bits (stored in ROM) used to represent the sequence of control signals to fetch, decode and execute the instruction i.e. control signals (in a sequence) for every instruction are generated using memory. The microinstructions are at an even more detailed level than machine language.

A hardwired microcontroller/processor uses the bit patterns of the instructions to select and activate specific circuits (may be unique to the each instruction) to execute The 8051 Microcontroller based Embedded Systems, First edition, McGraw‐Hill Education http://www.mhhe.com/patel/mbes


the instructions. All control signals (or sequence of steps) required to fetch, decode and execute the instructions are generated and controlled by combinatorial logic and state machine circuitry. A microcoded core is simpler to design, and can be implemented faster. A hardwired microcontroller is more complex to design because instruction decoder will be complex and difficult to debug and is less flexible. The advantage of hardwired design is that it will execute instructions faster. 5.

6.

7.

The CISC architecture require less the number of instructions per program at the cost of number of cycles per instruction. Hence, small program size and higher code density. The Boolean processor provides direct support for bit manipulation. This leads to more efficient programs that need to deal with binary operations frequently used in the machine control applications. Bit addressing can be used for test pin monitoring or user defined program control flags. The key features of 8051 microcontroller are: • • • • • • • • • • • • •

8 bit CPU with Boolean processing capabilities. 4K bytes on-chip *program memory. 128 bytes on-chip data memory. 64 Kbytes each program and external data address space. 32 bidirectional I/O lines organized as four 8-bit I/O ports. serial port – Full duplex UART. 2 16-bit timers/counters. Two-level prioritized interrupt structure. Direct byte and bit addressability. Four register banks. Binary or decimal arithmetic support. Hardware multiply and divide operations. 12 clock cycles per machine cycle Comparison of hardware resources of MCS 51 family.

Feature

8031

8051

8751

8032

8052

8752

Program memory

None ROM less

4K ROM

4K EPROM

None ROM less

8K ROM

8K EPROM

Data memory

128 Bytes

128 Bytes

128 RAM

256 Bytes

256 Bytes

256 Bytes

The 8051 Microcontroller based Embedded Systems, First edition, McGraw‐Hill Education http://www.mhhe.com/patel/mbes


Timers/counters (16-bit)

2

2

2

3

3

3

I/O pins

32

32

32

32

32

32

Serial port

1

1

1

1

1

1

Interrupt sources (Reset not 5 included)

5

5

6

6

6

Different family member exist to fulfill requirements of different types of applications. 8.

Required aspects are given below: 1. Amount of program memory 2. Amount of data Memory 3. Number of timers or counters 4. Number of I/O pins 5. Number of serial ports 6. Number of Interrupt sources

9.

RAM is widely used by programmers to store temporary data and intermediate results. The advantage of using this memory is that their access is faster compared to other off-chip RAM. Larger on-chip RAM means large storage area for temporary data and intermediate results.

10.

The use of 8051 family microcontrollers offers following advantages: •

• • • •

Availability and support: Easily and readily available and widely supported, free and commercial third party support is easily available because they are more popular. Hardware and software development tools and training are easily available and are inexpensive. High level language compilers are also available. Low cost: High level integration of many peripherals within single chip, only a few external components needed to create a working system. Effective architecture: Architecture optimized for the single bit operations, highly desirable for control applications. Single bit instructions require fewer bytes of code and hence faster execution. Multiple vendors: More than 20 manufacturers, more than thousand variants, something for everyone. Compatibility: Op-codes are same for all variants, therefore, easy to upgrade to newer variants.

The 8051 Microcontroller based Embedded Systems, First edition, McGraw‐Hill Education http://www.mhhe.com/patel/mbes


11.

Constant improvement: Constant improvement in implementation technology and reduction in power consumption.

The factors affecting choice of microcontroller for a given application are given below. •

• •

• •

Computational requirements: The microcontroller should have enough speed and processing capability to handle all operations of an application in a real time. The speed should be just sufficient to meet the computational needs of an application efficiently. Higher operational speeds than required will unnecessarily increase power consumption. The microcontroller word length i.e. 8, 16, or 32 bits should match most of the data types to be processed. Hence it is a major factor in evaluating computational capabilities and suitability of microcontroller for an application. Hardware resources requirement: The microcontroller should have sufficient program and data memory to store and execute an application program. An application program should take around 70-80% of the microcontroller’s program memory, this will provide space for future upgradations. Microcontroller should have enough I/O pins to provide user interface and connectivity to other modules of an application. It should also have all (or maximum) peripherals like timers/counters, ADC, DAC, serial port etc…on chip to make product as compact and as reliable as possible. Power requirements: Microcontroller should have low power consumption. It is critical factor for portable and battery powered products. Software and hardware development tools and Family: Cost and availability of the software development tools like compiler/assembler, debuggers, emulators or Integrated Development Environment (IDE) are important factors to choose a microcontroller. Availability of software libraries and software building blocks will ease the product design. The design team should be well versed with all this tools and family of microcontroller. For example if design team has experience and expertise in Intel 8051 family, then choosing the 8051 family microcontroller will led to perfect design of the product. This saves development efforts and reduces implementation time. The third party support for all tools is also an important factor to choose a microcontroller. Cost: It is a major factor in selection of a microcontroller, designer should select cheapest microcontroller that satisfy applications need. Availability: The microcontroller and support chips, if any, should be available easily in enough quantities now and in future throughout life cycle of a product. Future upgradeability and maintenance: The microcontroller’s ability to upgrade to higher performance or low power versions in future and ease of maintenance of the product also have to be considered.

The 8051 Microcontroller based Embedded Systems, First edition, McGraw‐Hill Education http://www.mhhe.com/patel/mbes


12.

The differences among various 8051 family members are given in following table.

Feature

8031

8051

8751

8032

8052

8752

Program memory

None ROM less

4K ROM

4K EPROM

None ROM less

8K ROM

8K EPROM

Data memory

128 Bytes

128 Bytes

128 RAM

256 Bytes

256 Bytes

256 Bytes

Timers/counters (16-bit)

2

2

2

3

3

3

I/O pins

32

32

32

32

32

32

Serial port

1

1

1

1

1

1

Interrupt sources (Reset not 5 included)

5

5

6

6

6

13.

14.

15.

Every microcontroller/processor has a native word size i.e. word length of 8, 16 or 32 etc. bits. The microcontroller works most efficiently with a data of native word size. The microcontroller can directly process the data which is having size equal to word length of microcontroller. Therefore the processing will be faster and efficient. The 8051 and its variants are all 8 bit microcontrollers, therefore they works more efficiently with 8 bit data. Von Neumann Architecture has single memory storage to hold both program instructions and data i.e. common program and data space. The CPU can either read an instruction or data from the memory one at a time (or write data to memory) because instructions and data are accessed using same bus system. The advantage of Von Neumann architecture is simple design of microcontroller chip because only one memory is to be implemented (for data as well as code) which in turn reduces required hardware (buses). The disadvantage is slower execution of a program. The RISC architecture has simple instructions. The simple instructions which perform a few operations at a time will provide high performance because of less hardware requirements for instruction decoder simple design, small die area, less power consumption). Thus, instructions require very less time to execute. The RISC instructions have few addressing modes for supported by all instructions. It reduces the cycles per instruction at the cost of the number of instructions per program. This will improve the performance of a microcontroller.

The 8051 Microcontroller based Embedded Systems, First edition, McGraw‐Hill Education http://www.mhhe.com/patel/mbes


16.

The microcontrollers/processors can be classified on the basis of word length, memory architecture, core (CPU) architecture and instruction set architecture. Word length: The 4, 8, 16, 32, 64 bit Microcontrollers. Memory Achitecture: Harvard Architecture or Von Neumann Architecture. Core Architecture: Microcoded or Hardwired Architecture.

17.

18.

19.

20.

Instruction Set Architecture: RISC and CISC RISC based microcontrollers has simple hardware and therefore the gap between high level language and actual hardware is more. To bridge this gap, the compiler design is more complex (emphasis is more on software therefore compiler design is more complex).

Programming the microcontroller chip (burning a program in to ROM) while the microcontroller chip is within the system (within the board) is known as ISP. Program download through serial port eliminating need of EPROM programmer and need of removing chip from the system, this feature is known as In System Programming (ISP).This results in faster and easier downloading of program in ROM. We know that power consumption of a chip is directly proportional to the speed at which it operates (system clock frequency). Therefore operating the microcontroller at higher speeds (than required) will unnecessarily increase the power consumption without providing any improvement in system performance. Analog devices, Atmel, Dallas Semiconductors, Infenion, Intel, NXP semiconductors, Silicon laboratories, SI Microclectronics, Texas instruments, Microchip are the leading manufacturers of the microcontrollers. Features and on-chip resources of the microcontrollers from some leading manufacturers are given below. •

8051 variants from NXP (Philips)

The major manufacturer of the 8051 variants is NXP (around50 variants). With the basic 8051 core, the variants have high capacity on-chip program memory (upto32K/64K), with peripherals like I2C bus, 8/10 bit ADCs, CAN Bus, Capture and The 8051 Microcontroller based Embedded Systems, First edition, McGraw‐Hill Education http://www.mhhe.com/patel/mbes


Compare registers, WDT, PWM, IAP,ISP, additional timer/counter.NXP has few high speed variants, for example, P89C54X2 microcontroller requires only 6 clocks per machine cycle and P89LPC916/917 requires two to four clocks per machine cycles. The following Table shows some of the variants from NXP. 8051 variants from NXP

On-chip program memory (Flash) On-chip RAM I/O Pins Timer/ Counters UART Interrupts Other features

89C51RB2xx

89C51RC2xx/ 89C51RD2xx

89C662/ 89C664

89LPC916/ 89LPC917

89LPC980/ 89LPC985

16 Kbytes

32/64 Kbytes

32/64 Kbytes

2Kbytes

4K/8Kbytes

256 +256 32 3 1 7

256+256/768 32 3 1 7

256 bytes 14 2 1 14/13

WDT, ISP, 2 DPTRs, IAP, PWM

WDT, ISP, 2 DPTRs, IAP, PWM

1/2KBytes 32 3 1 8 WDT, 2 DPTRs, IAP, ISP, I2C, PWM

256/512 bytes 26 7 1 13 WDT, 2 DPTRs, SPI, IAP, ISP, ADC, PWM, RTC, I2C, Analog comparators

WDT, 2 DPTRs, SPI, IAP, ISP, ADC/DAC

8051 variants from Atmel Corporation Atmel Corporation is another major manufacturer of the 8051 variants. It has introduced flash memory based variants at a low cost. The devices have peripherals like ISP, WDT and SPI. The variants are available in 20/40 pins and varying operating voltages from 2.7 to 6 V. The following Table shows some of the variants from Atmel. 8051 variants from Atmel 89C51/89L V51*

89C52/89L V52*

89C2051 (20 pin)

89C1051 (20 pin)

4KBytes

8KBytes

2KBytes

1KBytes

128 bytes

256 bytes

128 bytes

64bytes

89S8252/8 9S8253 8/12 KBytes 256 bytes

On-chip EEPROM

-

-

-

-

2KBytes

-

I/O Pins

32

32

15

15

32

32

On-chip program memory(Flash) On-chip RAM

89S53 12KBytes 256 bytes

Timer/Counters

2

3

2

1

3

3

UART Interrupt Sources Lock Bits

1 6 3

1 8 3

1 6 2

3 2

1 9 3

1 9 3

The 8051 Microcontroller based Embedded Systems, First edition, McGraw‐Hill Education http://www.mhhe.com/patel/mbes


Analog comparator * 89LV51/52 are low voltage devices i.e. operating voltage range is 2.7V to 6V Others

2 DPTR, SPI,WDT

2DPTR, SPI,WDT

8051 variants from Dallas semiconductor Dallas has redesigned the 8051 architecture (hardwired in place of microcoded) and introduced high speed microcontrollers (HSMs). All instructions are executed in a single clock cycles (4 clock cycles in some variants) which require 12 clock cycles in traditional 8051. Moreover, the devices have additional serial port, WDT, RTC second data pointer, IAP, ISP and NV RAM (battery backed).The following Table shows some of the variants from Dallas semiconductor. 8051 variants from Dallas semiconductor

On-chip memory

program

On-chip RAM I/O Pins Timer/ Counters UART Interrupt Sources Other features

DS89C430

DS89C450

DS5000

DS87C550

DS8C7520

16Kbyte Flash

64Kbyte Flash

8Kbyte NV-RAM

8Kbyte EPROM

16Kbyte EPROM

256 Bytes + 1Kbyte 32 3 2 14

256 Bytes + 1Kbyte 32 3 2 14

128 Bytes

256 Bytes + 1Kbyte

32 2 1 6

2 DPTRs, IAP, ISP, WDT

2 DPTRs, IAP, ISP, WDT

55 3 2 15 2 DPTRs, WDT, RTC, 10bit ADC (8ch), 8bit PWM (4ch)

IAP, ISP

256 Bytes + 1Kbyte 32 3 2 14 2 DPTRs, WDT

8051 variants from Silicon Laboratories Silicon Laboratories (SiLab) 8 bit mixed signal microcontrollers utilizes Silicon Labs' proprietary CIP 51 Hardwired microcontroller core. The CIP 51 is fully compatible with the MCS-51 instruction set and have pipelined architecture which greatly improves its instruction throughput. It executes most instructions (around 70%) in one or two system clock cycles. These devices deliver up to 100 MIPS (million instructions per second) peak throughput. They have an on-board JTAG debug capability which supports read/write access to memory/registers, breakpoints, watch points, single stepping and free running commands. The following Table shows some of the 8051 variants from Silicon Laboratories. Table 8051 variants from Silicon Laboratories

The 8051 Microcontroller based Embedded Systems, First edition, McGraw‐Hill Education http://www.mhhe.com/patel/mbes


On-chip program memory (Flash) On-chip RAM I/O Pins Timer/ Counters UART

Other features

C8051F020 (20MIPS)

C8051F060 (25 MIPS)

C8051F122* (100MIPS)

64K FLASH

64K FLASH

128K

256 bytes +4K 64 5 2

256 bytes +4K 59 5 2 WDT, PCA, SPI, I2C, CAN 2.0B, 16 bit ADC (2ch), 10 bit ADC (8ch), 12 bit DAC (2ch), 3 Analog Comparators, On-Chip Temperature sensor

256 bytes +8K 64 5 2

SPI, I2C, 12 and 8 bit ADCs (8ch), on-chip temperature sensor, 12bit DAC (2ch), PCA

Capture/Compare, SPI, I2C, RTC, 10 and 8 bit ADCs (8ch), 12 bit DAC (2ch), OnChip Temperature sensor

*C8051F120/21/22/23/30/31/32/33 and C8051F360/61/62/63/64/65 are the fastest microcontrollers (100 MIPS)

The 8051 Microcontroller based Embedded Systems, First edition, McGraw‐Hill Education http://www.mhhe.com/patel/mbes


CHAPTER 2 PROGRAMMING MODEL AND ACHITECTURE OF THE 8051 1.

Accumulator register is used to store (collect or accumulate) the result of all arithmetic and majority of logical operations, because of this reason it is named as Accumulator.

2.

Program counter (PC) is a 16-bit register. It always contains the memory address of the next instruction to be executed i.e. it points to the instruction that is to be executed next.

3.

The memory in which temporary data are stored is called data memory. RAM is referred as data memory.

4.

The memory in which program instructions are stored is called program memory. ROM (PROM/EPROM/EEPROM/Flash) is referred as program memory.

5.

The power on default value of the SP register is 07H. The stack on the 8051 grows upwards in memory therefore SP is incremented before data is stored as a result of special instructions (PUSH and CALL). This means that location 08H is the first location being used for the stack. As data is retrieved from the stack (using POP and RET), the byte is read from the stack and then SP is decremented by one. So in conclusion, the address held in the SP register is the location where the last byte was stored by a stack operation. The SP register can be initialized with any address within internal RAM i.e. 00H to 7FH. The stack is usually defined (and thus located) at higher addresses in RAM by loading new address into SP before performing any stack operation.

6.

Four ports are available in 8051.Yes, all ports are bit addressable.

7.

Programming model is programmer’s view of a microcontroller/processor. It shows (memory or registers) which can be accessed (read/write) by a programmer along with their internal organization. It is a collection of internal registers (and memory locations) that can be used by a programmer to develop any software (i.e. control and application programs) and to use several features of a particular microcontroller. As these registers and memory locations are used by software instructions, it is necessary to have knowledge of the programming model before we start developing any program. Programming model of 8051 is given below:

The 8051 Microcontroller based Embedded Systems, First edition, McGraw‐Hill Education http://www.mhhe.com/patel/mbes


8.

Peripheral data registers: TL0, TH0, TL1, TH1, and SBUF. Peripheral control registers: IP, IE, TMOD, TCON, SCON, and PCON.

9.

When we switch the bank, complete new set of memory locations with names R0 to R7 are made available for use by instructions. So without saving the contents of current bank’s R0 to R7 we can directly use new set of R0 to R7. This will save time to preserve the registers contents when we call a subroutine i.e. save context switching time.

10.

The stack is a section of memory locations in the internal RAM that is used for temporary storage and retrieval of data (or addresses) while the execution of a program. It is the last in first out (LIFO) type memory. This section of memory is accessed by certain instructions or events (like interrupts). The 8051 Microcontroller based Embedded Systems, First edition, McGraw‐Hill Education http://www.mhhe.com/patel/mbes


The register used to access contents of the stack is called stack pointer. It is an 8 bit register. The power on default value of the SP register is 07H. The stack on the 8051 grows upwards in memory therefore SP is incremented before data is stored as a result of special instructions (PUSH and CALL). This means that location 08H is the first location being used for the stack. As data is retrieved from the stack (using POP and RET), the byte is read from the stack and then SP is decremented by one. So in conclusion, the address held in the SP register is the location where the last byte was stored by a stack operation 11.

Yes, we can interface of both ROM and RAM at the same time with the 8051. തതതത pin is connected to VCC (+5V), the 8051 will access first 4K bytes (0000H to If EA 0FFFH) from internal ROM and any address above 0FFFH will be accessed from external ROM. If തതതത EA is connected to ground (0 V) then, only external memory from 0000H to FFFFH will be accessed by the 8051.

12.

Program status word is an 8 bit register. It is also referred as flag register or processor status word. Flag is a flip flop (1 bit storage element) used to store and indicate the nature of result produced by execution of certain instructions. The state of flags (0 or 1) is tested by other instructions (program flow control or branch instructions) to make decisions. PSW structure is explained below: PSW.7 PSW.6

PSW.5

PSW.4

PSW.3

PSW.2

PSW.1

PSW.0

CY AC F0 RS1 RS0 OV -P MSB LSB CY: Carry flag It is a carry (or borrow) used in addition and subtraction operations. It is set to 1 when there is carry out from MSB (D7 bit) after an addition (or a borrow into D7 bit during a subtraction). It is also used as the ‘Accumulator’ for the Boolean operations. It can be directly modified by bit level instructions. OV: Overflow flag It is set to 1 to indicate that result of signed arithmetic is erroneous (out of range) 13.

True

14.

Yes, we can use program memory to store data by loading the data into the ROM while burning it with the program.

The 8051 Microcontroller based Embedded Systems, First edition, McGraw‐Hill Education http://www.mhhe.com/patel/mbes


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.