微處理機 microprocessors 課本: the 8051 microcontroller: a systems approach, m. a. mazidi,...

49
微微微微 微微微微 Microprocessors Microprocessors 微微The 8051 Microcontroller: A Systems Approach, M. A. Mazidi, J. G. Mazidi, R. D. McKinlay, Int‘l ed. Pearson Education, 2013 ( 微微微微微微 ) 微微微微 微微微微微 5 微 微微微微微 5, 6 微 微微微微EE204 微微微微PC 微微 微微微微 微微 10% 微微微 20% 微微微 35% 微微微 35% 微微微微http://ares.ee.nchu.edu.tw/course/ mpr102/

Upload: stuart-oconnor

Post on 26-Dec-2015

331 views

Category:

Documents


17 download

TRANSCRIPT

Page 1: 微處理機 Microprocessors 課本: The 8051 Microcontroller: A Systems Approach, M. A. Mazidi, J. G. Mazidi, R. D. McKinlay, Int‘l ed. Pearson Education, 2013 (

微處理機微處理機MicroprocessorsMicroprocessors

課本: The 8051 Microcontroller: A Systems Approach, M. A. Mazidi, J. G. Mazidi, R. D. McKinlay, Int‘l ed. Pearson Education, 2013 ( 全華圖書代理 )

上課時間:星期二下午 5 節 星期三下午 5, 6 節上課地點: EE204

實習地點: PC 教室評量方式 :實習 10% ,平時考 20% ,期中考 35% ,期末考 35%

課程網頁: http://ares.ee.nchu.edu.tw/course/mpr102/

Page 2: 微處理機 Microprocessors 課本: The 8051 Microcontroller: A Systems Approach, M. A. Mazidi, J. G. Mazidi, R. D. McKinlay, Int‘l ed. Pearson Education, 2013 (

大綱大綱1. INTRODUCTION TO COMPUTING2. THE 8051 MICROCONTROLLERS3. 8051 ASSEMBLY LANGUAGE PROGRAMMING4. I/O PORT PROGRAMMING5. 8051 HARDWARE CONNECTION6. 8051 TIMER PROGRAMMING

Page 3: 微處理機 Microprocessors 課本: The 8051 Microcontroller: A Systems Approach, M. A. Mazidi, J. G. Mazidi, R. D. McKinlay, Int‘l ed. Pearson Education, 2013 (

大綱大綱 (( 續續 ))

7. 8051 SERIAL PORT PROGRAMMING8. INTERRUPTS PROGRAMMING9. 8051 INTERFACING TO I/O10. 8051 INTERFACING TO EXTERNAL MEMORY

Page 4: 微處理機 Microprocessors 課本: The 8051 Microcontroller: A Systems Approach, M. A. Mazidi, J. G. Mazidi, R. D. McKinlay, Int‘l ed. Pearson Education, 2013 (

CHAPTER 0CHAPTER 0

INTRODUCTION TO INTRODUCTION TO COMPUTINGCOMPUTING

Page 5: 微處理機 Microprocessors 課本: The 8051 Microcontroller: A Systems Approach, M. A. Mazidi, J. G. Mazidi, R. D. McKinlay, Int‘l ed. Pearson Education, 2013 (

Decimal and Binary Number Decimal and Binary Number SystemsSystemsHuman beings use base 10

(decimal) arithmetic◦There are 10 distinct symbols, 0, 1,

2, …, 9Computers use base 2 (binary)

system◦There are only 0 and 1

These two binary digits are commonly referred to as bits

Page 6: 微處理機 Microprocessors 課本: The 8051 Microcontroller: A Systems Approach, M. A. Mazidi, J. G. Mazidi, R. D. McKinlay, Int‘l ed. Pearson Education, 2013 (

Converting from Decimal to Converting from Decimal to BinaryBinary

Divide the decimal number by 2 repeatedly

Keep track of the remaindersContinue this process until the

quotient becomes zeroWrite the remainders in reverse

order to obtain the binary number

Page 7: 微處理機 Microprocessors 課本: The 8051 Microcontroller: A Systems Approach, M. A. Mazidi, J. G. Mazidi, R. D. McKinlay, Int‘l ed. Pearson Education, 2013 (

Converting from Decimal to Converting from Decimal to Binary (cont.)Binary (cont.)

Page 8: 微處理機 Microprocessors 課本: The 8051 Microcontroller: A Systems Approach, M. A. Mazidi, J. G. Mazidi, R. D. McKinlay, Int‘l ed. Pearson Education, 2013 (

Converting from Binary to Converting from Binary to DecimalDecimal

Know the weight of each bit in a binary number

Add them together to get its decimal equivalent

Page 9: 微處理機 Microprocessors 課本: The 8051 Microcontroller: A Systems Approach, M. A. Mazidi, J. G. Mazidi, R. D. McKinlay, Int‘l ed. Pearson Education, 2013 (

Converting from Binary to Converting from Binary to Decimal (cont.)Decimal (cont.)Use the concept of weight to

convert a decimal number to a binary directly

Page 10: 微處理機 Microprocessors 課本: The 8051 Microcontroller: A Systems Approach, M. A. Mazidi, J. G. Mazidi, R. D. McKinlay, Int‘l ed. Pearson Education, 2013 (

HexadecimalHexadecimalSystemSystemBase 16, the

hexadecimal system, is used as a convenient representation of binary numbers◦It is much easier to

represent a string of 0s and 1s such as 100010010110 as its hexadecimal equivalent of 896H

Page 11: 微處理機 Microprocessors 課本: The 8051 Microcontroller: A Systems Approach, M. A. Mazidi, J. G. Mazidi, R. D. McKinlay, Int‘l ed. Pearson Education, 2013 (

Converting between Binary Converting between Binary and Hexand HexTo represent a binary number as

its equivalent hexadecimal number◦Start from the right and group 4 bits

at a time Replacing each 4-bit binary number with

its hex equivalent

Page 12: 微處理機 Microprocessors 課本: The 8051 Microcontroller: A Systems Approach, M. A. Mazidi, J. G. Mazidi, R. D. McKinlay, Int‘l ed. Pearson Education, 2013 (

Converting between Binary Converting between Binary and Hex (cont.)and Hex (cont.)To convert from hex to binary

◦Each hex digit is replaced with its 4-bit binary equivalent

Page 13: 微處理機 Microprocessors 課本: The 8051 Microcontroller: A Systems Approach, M. A. Mazidi, J. G. Mazidi, R. D. McKinlay, Int‘l ed. Pearson Education, 2013 (

Converting from Decimal to Converting from Decimal to HexHex

Convert to binary first and then convert to hex

Convert directly from decimal to hex by repeated division◦Keeping track of the remainders

Page 14: 微處理機 Microprocessors 課本: The 8051 Microcontroller: A Systems Approach, M. A. Mazidi, J. G. Mazidi, R. D. McKinlay, Int‘l ed. Pearson Education, 2013 (

Converting from Decimal to Converting from Decimal to Hex (cont.)Hex (cont.)

Page 15: 微處理機 Microprocessors 課本: The 8051 Microcontroller: A Systems Approach, M. A. Mazidi, J. G. Mazidi, R. D. McKinlay, Int‘l ed. Pearson Education, 2013 (

Converting from Hex to Converting from Hex to DecimalDecimalConvert from hex to binary and

then to decimal

Convert directly from hex to decimal by summing the weight of all digits◦6 × 256 + 11 (B) × 16 + 2 = 1714

Page 16: 微處理機 Microprocessors 課本: The 8051 Microcontroller: A Systems Approach, M. A. Mazidi, J. G. Mazidi, R. D. McKinlay, Int‘l ed. Pearson Education, 2013 (

Addition of Hex NumbersAddition of Hex Numbers

Adding the digits together from the least significant digits◦If the result is less than 16, write that

digit as the sum for that position◦If it is greater than 16, subtract 16 from

it to get the digit and carry 1 to the next digit

Page 17: 微處理機 Microprocessors 課本: The 8051 Microcontroller: A Systems Approach, M. A. Mazidi, J. G. Mazidi, R. D. McKinlay, Int‘l ed. Pearson Education, 2013 (

Subtraction of Hex NumbersSubtraction of Hex Numbers

If the second digit is greater than the first, borrow 16 from the preceding digit

Page 18: 微處理機 Microprocessors 課本: The 8051 Microcontroller: A Systems Approach, M. A. Mazidi, J. G. Mazidi, R. D. McKinlay, Int‘l ed. Pearson Education, 2013 (

ASCII CodeASCII Code

The ASCII (pronounced “ask-E”) code assigns binary patterns for◦Numbers 0 to 9◦All the letters of English alphabet,

uppercase and lowercase ◦Many control codes and punctuation

marksThe ASCII system uses 7 bits to

represent each code

Page 19: 微處理機 Microprocessors 課本: The 8051 Microcontroller: A Systems Approach, M. A. Mazidi, J. G. Mazidi, R. D. McKinlay, Int‘l ed. Pearson Education, 2013 (

ASCII Code (cont.)ASCII Code (cont.)

Page 20: 微處理機 Microprocessors 課本: The 8051 Microcontroller: A Systems Approach, M. A. Mazidi, J. G. Mazidi, R. D. McKinlay, Int‘l ed. Pearson Education, 2013 (

Binary LogicBinary Logic

Two voltage levels can be represented as the two digits 0 and 1◦Signals in digital electronics have

two distinct voltage levels with built-in tolerances for variations in the voltage

◦A valid digital signal should be within either of the two shaded areas

Page 21: 微處理機 Microprocessors 課本: The 8051 Microcontroller: A Systems Approach, M. A. Mazidi, J. G. Mazidi, R. D. McKinlay, Int‘l ed. Pearson Education, 2013 (

Logic GatesLogic Gates

AND gate OR gate

Page 22: 微處理機 Microprocessors 課本: The 8051 Microcontroller: A Systems Approach, M. A. Mazidi, J. G. Mazidi, R. D. McKinlay, Int‘l ed. Pearson Education, 2013 (

Logic Gates (cont.)Logic Gates (cont.)

Tri-state buffer Inverter

Page 23: 微處理機 Microprocessors 課本: The 8051 Microcontroller: A Systems Approach, M. A. Mazidi, J. G. Mazidi, R. D. McKinlay, Int‘l ed. Pearson Education, 2013 (

Logic Gates (cont.)Logic Gates (cont.)

XOR gate

Page 24: 微處理機 Microprocessors 課本: The 8051 Microcontroller: A Systems Approach, M. A. Mazidi, J. G. Mazidi, R. D. McKinlay, Int‘l ed. Pearson Education, 2013 (

Logic Gates (cont.)Logic Gates (cont.)

NOR gate NAND gate

Page 25: 微處理機 Microprocessors 課本: The 8051 Microcontroller: A Systems Approach, M. A. Mazidi, J. G. Mazidi, R. D. McKinlay, Int‘l ed. Pearson Education, 2013 (

Logic Design Using GatesLogic Design Using GatesFull adders

Page 26: 微處理機 Microprocessors 課本: The 8051 Microcontroller: A Systems Approach, M. A. Mazidi, J. G. Mazidi, R. D. McKinlay, Int‘l ed. Pearson Education, 2013 (

Logic Design Using Gates Logic Design Using Gates (cont.)(cont.)Decoders

◦Decoders are widely used for address decoding in computer design

Page 27: 微處理機 Microprocessors 課本: The 8051 Microcontroller: A Systems Approach, M. A. Mazidi, J. G. Mazidi, R. D. McKinlay, Int‘l ed. Pearson Education, 2013 (

Logic Design Using Gates Logic Design Using Gates (cont.)(cont.)Flip-flopsFlip-flops are frequently used to

store data

Page 28: 微處理機 Microprocessors 課本: The 8051 Microcontroller: A Systems Approach, M. A. Mazidi, J. G. Mazidi, R. D. McKinlay, Int‘l ed. Pearson Education, 2013 (

Important TerminologyImportant TerminologyThe unit of data size

◦Bit : a binary digit with the value 0 or 1 Byte : 8 bits Nibble : half of a bye, or 4

bits Word : two bytes, or 16 bits

The terms used to describe amounts of memory in IBM PCs and compatibles◦Kilobyte (K): 210 bytes◦Megabyte (M) : 220 bytes, over 1 million◦Gigabyte (G) : 230 bytes, over 1 billion◦Terabyte (T) : 240 bytes, over 1 trillion

Page 29: 微處理機 Microprocessors 課本: The 8051 Microcontroller: A Systems Approach, M. A. Mazidi, J. G. Mazidi, R. D. McKinlay, Int‘l ed. Pearson Education, 2013 (

Internal Organization of Internal Organization of ComputersComputersCPU (Central Processing Unit)

◦Execute information stored in memoryI/O (Input/output) devices

◦Provide a means of communicating with CPU

Memory◦RAM (Random Access Memory)

Temporary storage of programs that computer is running

The data is lost when computer is off

Page 30: 微處理機 Microprocessors 課本: The 8051 Microcontroller: A Systems Approach, M. A. Mazidi, J. G. Mazidi, R. D. McKinlay, Int‘l ed. Pearson Education, 2013 (

Internal Organization of Internal Organization of Computers (cont.)Computers (cont.)

◦ROM (Read Only Memory) It contains programs and information

essential to operation of the computer The information cannot be changed by

user, and is not lost when power is off It is called nonvolatile memory

Page 31: 微處理機 Microprocessors 課本: The 8051 Microcontroller: A Systems Approach, M. A. Mazidi, J. G. Mazidi, R. D. McKinlay, Int‘l ed. Pearson Education, 2013 (

Internal Organization of Internal Organization of Computers (cont.)Computers (cont.)The CPU is connected to memory

and I/O through strips of wire called a bus◦Carries information from place to

place Address bus, Data bus, and Control bus

Page 32: 微處理機 Microprocessors 課本: The 8051 Microcontroller: A Systems Approach, M. A. Mazidi, J. G. Mazidi, R. D. McKinlay, Int‘l ed. Pearson Education, 2013 (

Internal Organization of Internal Organization of Computers (cont.)Computers (cont.)Address bus

◦For a device (memory or I/O) to be recognized by the CPU, it must be assigned an address The address assigned to a given device

must be unique

◦The CPU puts the address on the address bus The decoding circuitry finds the device

Page 33: 微處理機 Microprocessors 課本: The 8051 Microcontroller: A Systems Approach, M. A. Mazidi, J. G. Mazidi, R. D. McKinlay, Int‘l ed. Pearson Education, 2013 (

Internal Organization of Internal Organization of Computers (cont.)Computers (cont.)Data bus

◦The CPU either gets data from the device or sends data to it

Control bus◦Provides read or write signals to the

device to indicate if the CPU is asking for information or sending it information

Page 34: 微處理機 Microprocessors 課本: The 8051 Microcontroller: A Systems Approach, M. A. Mazidi, J. G. Mazidi, R. D. McKinlay, Int‘l ed. Pearson Education, 2013 (

More about Data BusMore about Data Bus

The more data buses available, the better the CPU◦The processing power of a computer

is related to the size of its buses◦More data buses mean a more

expensive CPU and computerThe average size of data buses in

CPUs varies between 8 and 64Data buses are bidirectional

◦To receive or send data

Page 35: 微處理機 Microprocessors 課本: The 8051 Microcontroller: A Systems Approach, M. A. Mazidi, J. G. Mazidi, R. D. McKinlay, Int‘l ed. Pearson Education, 2013 (

More about Address BusMore about Address BusThe more address buses available,

the larger the number of devices that can be addressed◦The number of locations is always equal to

2x, where x is the number of address lines ex. a CPU with 24 address lines and 8 data

lines can provide a total of 224 or 16M bytes of addressable memory

Each location can have a maximum of 1 byte of data, All general-purpose CPUs are byte addressable

The address bus is unidirectional

Page 36: 微處理機 Microprocessors 課本: The 8051 Microcontroller: A Systems Approach, M. A. Mazidi, J. G. Mazidi, R. D. McKinlay, Int‘l ed. Pearson Education, 2013 (

CPU’s Relation to RAM and CPU’s Relation to RAM and ROMROMFor the CPU to process information,

the data must be stored in RAM or ROM◦Referred to as primary memory

ROM provides information that is fixed and permanent◦Tables or initialization program

RAM stores information that is not permanent and can change with time◦Various versions of OS and application

packages

Page 37: 微處理機 Microprocessors 課本: The 8051 Microcontroller: A Systems Approach, M. A. Mazidi, J. G. Mazidi, R. D. McKinlay, Int‘l ed. Pearson Education, 2013 (

CPU’s Relation to RAM and CPU’s Relation to RAM and ROM (cont.)ROM (cont.)CPU gets information to be

processed◦First from RAM (or ROM)◦If it is not there, then seeks it from a

mass storage device, called secondary memory

◦Then transfers the information to RAM

Page 38: 微處理機 Microprocessors 課本: The 8051 Microcontroller: A Systems Approach, M. A. Mazidi, J. G. Mazidi, R. D. McKinlay, Int‘l ed. Pearson Education, 2013 (

Inside CPUsInside CPUsThe CPU uses registers to store

information temporarily◦Values to be processed◦Address of value to be fetched from

memory◦In general, the more and bigger the

registers, the better the CPURegisters can be 8-, 16-, 32-, or 64-bit

◦The disadvantage of more and bigger registers is the increased cost of such a CPU

Page 39: 微處理機 Microprocessors 課本: The 8051 Microcontroller: A Systems Approach, M. A. Mazidi, J. G. Mazidi, R. D. McKinlay, Int‘l ed. Pearson Education, 2013 (

Inside CPUs (cont.)Inside CPUs (cont.)

Page 40: 微處理機 Microprocessors 課本: The 8051 Microcontroller: A Systems Approach, M. A. Mazidi, J. G. Mazidi, R. D. McKinlay, Int‘l ed. Pearson Education, 2013 (

Inside CPUs (cont.)Inside CPUs (cont.)ALU (arithmetic/logic unit)

◦Performs arithmetic functions Such as add, subtract, multiply, and divide

◦Logic functions Such as AND, OR, and NOT

Program counter◦Points to the address of the next

instruction to be executed As each instruction is executed, the program

counter is incremented to point to the address of the next instruction to be executed

Page 41: 微處理機 Microprocessors 課本: The 8051 Microcontroller: A Systems Approach, M. A. Mazidi, J. G. Mazidi, R. D. McKinlay, Int‘l ed. Pearson Education, 2013 (

Inside CPUs (cont.)Inside CPUs (cont.)

Instruction decoder◦Interprets the instruction fetched

into the CPUA CPU capable of understanding

more instructions requires more transistors to design

Page 42: 微處理機 Microprocessors 課本: The 8051 Microcontroller: A Systems Approach, M. A. Mazidi, J. G. Mazidi, R. D. McKinlay, Int‘l ed. Pearson Education, 2013 (

Assume that an imaginary CPU has registers Assume that an imaginary CPU has registers called A, B, C, and D.called A, B, C, and D.It has an 8-bit data bus and a 16-bit address It has an 8-bit data bus and a 16-bit address bus. bus. The CPU can access memory from addresses The CPU can access memory from addresses 0000 to FFFFH (for a total of 10000H locations). 0000 to FFFFH (for a total of 10000H locations). The action to be performed by the CPU is:The action to be performed by the CPU is:1. Put hexadecimal value 21 into register A1. Put hexadecimal value 21 into register A2. Add to register A values 42H and 12H2. Add to register A values 42H and 12HAssume that the code for the CPU to move a Assume that the code for the CPU to move a value to register A is 1011 0000 (B0H) and the value to register A is 1011 0000 (B0H) and the code for adding a value to register A is 0000 code for adding a value to register A is 0000 0100 (04H). 0100 (04H). The necessary steps and code to perform them The necessary steps and code to perform them are as follows:are as follows:

Page 43: 微處理機 Microprocessors 課本: The 8051 Microcontroller: A Systems Approach, M. A. Mazidi, J. G. Mazidi, R. D. McKinlay, Int‘l ed. Pearson Education, 2013 (

If the program to perform the actions listed If the program to perform the actions listed above is stored in memory locations starting at above is stored in memory locations starting at 1400H,1400H,

Page 44: 微處理機 Microprocessors 課本: The 8051 Microcontroller: A Systems Approach, M. A. Mazidi, J. G. Mazidi, R. D. McKinlay, Int‘l ed. Pearson Education, 2013 (

The actions performed by the CPU to run the The actions performed by the CPU to run the program above would be as follows:program above would be as follows:

1.The CPU’s program counter can have a 1.The CPU’s program counter can have a value between 0000 and FFFFH. value between 0000 and FFFFH. The program counter must be set to the The program counter must be set to the value 1400H, indicating the address of the value 1400H, indicating the address of the first instruction code to be executed.first instruction code to be executed.

Page 45: 微處理機 Microprocessors 課本: The 8051 Microcontroller: A Systems Approach, M. A. Mazidi, J. G. Mazidi, R. D. McKinlay, Int‘l ed. Pearson Education, 2013 (

2.The CPU puts 1400H on the address bus 2.The CPU puts 1400H on the address bus and sends it out. and sends it out. The memory circuitry finds the location The memory circuitry finds the location while the CPU activates the READ signal, while the CPU activates the READ signal, indicating to memory that it wants the byte indicating to memory that it wants the byte at location 1400H. at location 1400H. This causes the contents of memory location This causes the contents of memory location 1400H, which is B0, to be put on the data 1400H, which is B0, to be put on the data bus and brought into the CPU.bus and brought into the CPU.

Page 46: 微處理機 Microprocessors 課本: The 8051 Microcontroller: A Systems Approach, M. A. Mazidi, J. G. Mazidi, R. D. McKinlay, Int‘l ed. Pearson Education, 2013 (

3.The CPU decodes the instruction B0 with 3.The CPU decodes the instruction B0 with the help of its instruction decoder dictionary.the help of its instruction decoder dictionary.When it finds the definition for that When it finds the definition for that instruction it knows it must bring into instruction it knows it must bring into register A of the CPU the byte in the next register A of the CPU the byte in the next memory location.memory location.When value 21H comes into the CPU it will When value 21H comes into the CPU it will go directly into register A. go directly into register A. After completing one instruction, the After completing one instruction, the program counter points to the address of the program counter points to the address of the next instruction to be executed, which in this next instruction to be executed, which in this case is 1402H. case is 1402H. Address 1402 is sent out on the address bus Address 1402 is sent out on the address bus to fetch the next instruction.to fetch the next instruction.

Page 47: 微處理機 Microprocessors 課本: The 8051 Microcontroller: A Systems Approach, M. A. Mazidi, J. G. Mazidi, R. D. McKinlay, Int‘l ed. Pearson Education, 2013 (

4. From memory location 1402H it fetches 4. From memory location 1402H it fetches code 04H. After decoding, the CPU knows code 04H. After decoding, the CPU knows that it must add to the contents of register A that it must add to the contents of register A the byte sitting at the next address (1403). the byte sitting at the next address (1403). After it brings the value (in this case 42H) After it brings the value (in this case 42H) into the CPU, it provides the contents of into the CPU, it provides the contents of register A along with this value to the ALU to register A along with this value to the ALU to perform the addition. perform the addition. It then takes the result of the addition from It then takes the result of the addition from the ALU’s output and puts it in register A. the ALU’s output and puts it in register A. The program counter becomes 1404, the The program counter becomes 1404, the address of the next instruction.address of the next instruction.

Page 48: 微處理機 Microprocessors 課本: The 8051 Microcontroller: A Systems Approach, M. A. Mazidi, J. G. Mazidi, R. D. McKinlay, Int‘l ed. Pearson Education, 2013 (

5.Address 1404H is put on the address bus 5.Address 1404H is put on the address bus and the code is fetched into the CPU, and the code is fetched into the CPU, decoded, and executed. This code is again decoded, and executed. This code is again adding a value to register A. adding a value to register A. The program counter is updated to 1406H.The program counter is updated to 1406H.

6.Finally, the contents of address 1406 are 6.Finally, the contents of address 1406 are fetched in and executed. fetched in and executed. This HALT instruction tells the CPU to stop This HALT instruction tells the CPU to stop incrementing the program counter and incrementing the program counter and asking for the next instruction.asking for the next instruction.

Page 49: 微處理機 Microprocessors 課本: The 8051 Microcontroller: A Systems Approach, M. A. Mazidi, J. G. Mazidi, R. D. McKinlay, Int‘l ed. Pearson Education, 2013 (

Now suppose that address 1403H contained Now suppose that address 1403H contained value 04 instead of 42H. How would the CPU value 04 instead of 42H. How would the CPU distinguish between data 04 to be added and distinguish between data 04 to be added and code 04?code 04?Remember that code 04 for this CPU means Remember that code 04 for this CPU means move the next value into register A. Therefore, move the next value into register A. Therefore, the CPU will not try to decode the next value. It the CPU will not try to decode the next value. It simply moves the contents of the following simply moves the contents of the following memory location into register A, regardless of memory location into register A, regardless of its value.its value.