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
Titles you can't find anywhere else
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Titles you can't find anywhere else
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Titles you can't find anywhere else
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Titles you can't find anywhere else
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
CHAPTER 1 INTRODUCTION TO MICROCONTROLLERS 1.
The differences between microprocessor and microcontrollers are given in the following table. Microprocessor Microcontroller Microprocessor is complete Microcontroller is complete functional functional CPU i.e. it contains ALU, microcomputer i.e. it contains the registers, stack pointer, program circuitry of microprocessor and in counter, instruction decode and addition it has built in memory (ROM, control unit and interrupt processing RAM), I/O circuits and peripherals circuits. necessary for an application. Microprocessor instruction sets are Microcontrollers have instruction sets data processing intensive, means that are related to the control of inputs powerful addressing modes and many and outputs, means they have many bit instructions to move data between handling instructions along with byte memory and CPU to handle large processing instructions. volumes of data. Microprocessor based products are Microcontroller based products are primarily designed to interact with primarily designed to interact with humans and are more flexible to machines; once a system is designed they design are less flexible. Access times for external memory Access times for on-chip memory and and I/O devices are more, resulting in I/O devices are less, resulting in a faster a slower system. system. Microprocessor based systems Microcontroller based systems require require support devices and are less external hardware, reducing PCB usually bulkier, costly, less reliable size and hence are compact, cheaper, and consume more power. more reliable and consume less power. Software protection is not possible Software protection is possible because because of the requirement of of on-chip code memory. external code memory.
2.
Various applications of microcontrollers are given below: •
•
Household appliances: appliances: Microwave oven, washing machine, 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
Titles you can't find anywhere else
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Titles you can't find anywhere else
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Titles you can't find anywhere else
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
•
•
•
•
•
•
•
•
•
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
Titles you can't find anywhere else
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Titles you can't find anywhere else
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Titles you can't find anywhere else
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
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
Titles you can't find anywhere else
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Titles you can't find anywhere else
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Titles you can't find anywhere else
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
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 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 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. 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
Titles you can't find anywhere else
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Titles you can't find anywhere else
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Titles you can't find anywhere else
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
•
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 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
Titles you can't find anywhere else
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Titles you can't find anywhere else
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Titles you can't find anywhere else
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Titles you can't find anywhere else
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
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.
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.
14.
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.
Titles you can't find anywhere else
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Titles you can't find anywhere else
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Titles you can't find anywhere else
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
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. Instruction Set Architecture: RISC and CISC
17.
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).
18.
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.
19.
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.
20.
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
Titles you can't find anywhere else
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Titles you can't find anywhere else
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Titles you can't find anywhere else
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
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 89C51RB2xx
89C51RC2xx/ 89C51RD2xx
89C662/ 89C664
89LPC916/ 89LPC917
89LPC980/ 89LPC985
On-chip program memory (Flash) On-chip RAM
16 Kbytes
32/64 Kbytes
32/64 Kbytes
2Kbytes
4K/8Kbytes
256 +256
256+256/768
1/2KBytes 1/2KBytes
256 bytes
256/512 bytes
I/O Pins
32
32
32
14
26
Timer/ Counters
3
3
3
2
7
UART
1
1
1
1
1
Interrupts
7
7
14/13
Other features
WDT, ISP, 2 DPTRs, IAP, PWM
WDT, ISP, 2 DPTRs, IAP, PWM
8 WDT, 2 DPTRs, IAP, ISP, I2C, PWM
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
On-chip program memory(Flash)
89C51/89L V51*
89C52/89L V52*
89C2051 (20 pin)
89C1051 (20 pin)
4KBytes
8KBytes
2KBytes
1KBytes
89S8252/8 9S8253 8/12 KBytes
89S53 12KBytes
Titles you can't find anywhere else
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Titles you can't find anywhere else
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Titles you can't find anywhere else
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
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 DS89C430
DS89C450
DS5000
DS87C550
DS8C7520
16Kbyte Flash
64Kbyte Flash
8Kbyte NV-RAM
8Kbyte EPROM
16Kbyte EPROM
256 Bytes + 1Kbyte 32
128 Bytes
256 Bytes + 1Kbyte
I/O Pins
256 Bytes + 1Kbyte 32
32
55
256 Bytes + 1Kbyte 32
Timer/ Counters
3
3
2
3
3
UART
2
2
1
2
2
Interrupt Sources
14
14
6
14
Other features
2 DPTRs, IAP, ISP, WDT
2 DPTRs, IAP, ISP, WDT
IAP, ISP
15 2 DPTRs, WDT, RTC, 10bit ADC (8ch), 8bit PWM (4ch)
On-chip memory
program
On-chip RAM
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
Titles you can't find anywhere else
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Titles you can't find anywhere else
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Titles you can't find anywhere else
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
C8051F020 (20MIPS)
C8051F060 (25 MIPS)
C8051F122* (100MIPS)
On-chip program memory (Flash) On-chip RAM
64K FLASH
64K FLASH
128K
256 bytes +4K
256 bytes +4K
256 bytes +8K
I/O Pins
64
59
64
Timer/ Counters
5
5
5
UART
2
Other features
2 WDT, PCA, SPI, I2C, CAN SPI, I2C, 12 and 8 bit ADCs 2.0B, 16 bit ADC (2ch), 10 (8ch), on-chip temperature bit ADC (8ch), 12 bit DAC sensor, (2ch), 3 Analog 12bit DAC (2ch), PCA Comparators, On-Chip Temperature sensor
2 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)
Titles you can't find anywhere else
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Titles you can't find anywhere else
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Titles you can't find anywhere else
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Titles you can't find anywhere else
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
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.
Titles you can't find anywhere else
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Titles you can't find anywhere else
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Titles you can't find anywhere else
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Titles you can't find anywhere else
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Titles you can't find anywhere else
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Titles you can't find anywhere else
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
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 8 051.
If EA pin is connected connected to to VCC (+5V), the 8051 will access first 4K bytes (0000H to 0FFFH) from internal ROM and any address above 0FFFH will be accessed from is connected to ground (0 V) then, only external memory from external ROM. If EA 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 MSB
AC
F0
RS1
RS0
OV
--
P LSB