You are on page 1of 20

ĐH Bách Khoa TP.

HCM

Lê Chí Thông-www.tinyurl.com/thongchile

The 8051 Microcontroller

Lê Chí Thông Ref. I. Scott Mackenzie, The 8051 Microcontroller

Instructor
Lê Chí Thông Faculty of Electrical and Electronics Engineering Ho Chi Minh City University of Technology (Đại Học Bách Khoa TP.HCM) Email: chithong@gmail.com; chithong@hcmut.edu.vn Website: www.tinyurl.com/thongchile

Ref. I. Scott Mackenzie

Lê Chí Thông

2

1

ĐH Bách Khoa TP.HCM

Lê Chí Thông-www.tinyurl.com/thongchile

Objectives
• Introduction fundamentals and applications of microprocessors and microcomputers. • Architecture, the instruction set, and applications of 8051 microcontroller family • Basic applications of microprocessors, such as input/output, analog-digital conversion (ADC), and digital-analog conversion (DAC), and data acquisition.

Ref. I. Scott Mackenzie

Lê Chí Thông

3

Grading
• Quizzes and homework assignments: 20%
– Homework is due at the beginning of class

• Mid-term: 30% • Final: 50%

Ref. I. Scott Mackenzie

Lê Chí Thông

4

2

Scott MacKenzie.tinyurl. 1995 • The 8051 Microcontroller: Architecture.ĐH Bách Khoa TP. Scott Mackenzie Lê Chí Thông 5 Chapter 1 Introduction to Microcontrollers Ref. Ayala. Programming. Kenneth J. Prentice-Hall. Scott Mackenzie Lê Chí Thông 6 3 . 2nd Edition.HCM Lê Chí Thông-www. I. and Applications. I. I.com/thongchile Textbooks • The 8051 Microcontroller. West Publishing Company Ref.

Motorola’s 680x0 Control Unit (CU) Arithmetic Logical Unit (ALU) Central Processing Unit (CPU) Data Bus Many chips on mother’s board Serial Port CPU GeneralPurpose Microprocessor RAM ROM I/O Port Timer Address Bus Ref. Scott Mackenzie Lê 7 Microprocessors • Integrated ALU and CU • No RAM.HCM Lê Chí Thông-www. I/O on CPU chip itself • Example: Intel’s x86.tinyurl.com/thongchile These are … embedded systems An embedded system is a system in which a processor/microcontroller/computer is embedded to perform a specific task or Chí Thông tasks Ref.ĐH Bách Khoa TP. I. ROM. Scott Mackenzie General-Purpose Microprocessor System Lê Chí Thông 8 4 . I.

ĐH Bách Khoa TP. Scott Mackenzie Lê Chí Thông 9 FIGURE 1–3 The central processing unit (CPU) Architecture of CPU PCProgram counter Ref. Scott Mackenzie Lê Chí Thông 10 5 . I.HCM Lê Chí Thông-www.tinyurl.com/thongchile FIGURE 1–2 Block diagram of a microcomputer system Architecture of computers Ref. I.

tinyurl.com/thongchile FIGURE 1–4 Bus activity for an opcode fetch cycle Opcode fetch. I. Scott Mackenzie Lê Chí Thông 11 FIGURE 1–5 Levels of software Shell. BIOS Keyboard. execution Ref. Scott Mackenzie Lê Chí Thông 12 6 . GUI – user .ĐH Bách Khoa TP. decode.hardware Utilities Booster loader. monitor Other hardwires Ref. I.HCM Lê Chí Thông-www.

Intel’s 8051.HCM Lê Chí Thông-www. Zilog’s Z8 and PIC 16X CPU I/O Port RAM ROM Serial Timer COM Port Lê Chí Thông A single chip Ref. I. Scott Mackenzie Microcontroller 14 7 . I/O ports. RAM.tinyurl.ĐH Bách Khoa TP. Scott Mackenzie Lê Chí Thông 13 Microcontrollers • Integrates CPU. ROM.com/thongchile FIGURE 1–6 Detailed block diagram of a microcomputer system Ref. … on a single chip • Sometimes called a "computer on a chip" • Typically used in embedded applications • Example: Motorola’s 6811. I.

Scott Mackenzie Lê Chí Thông 16 8 . I.com/thongchile Microcontroller Architectures Memory Address Bus CPU Data Bus 0 Program + Data 2n Memory Von Neumann Architecture Address Bus CPU Fetch Bus 0 Program Address Bus 0 Data Bus Ref. I. there is only one application software that is typically burned into ROM Ref. Scott Mackenzie Harvard Architecture Data Lê Chí Thông 15 Embedded System • Embedded system: the processor is embedded into that application. • In an embedded system.HCM Lê Chí Thông-www.tinyurl. • An embedded product usually uses a microcontroller to do one task only.ĐH Bách Khoa TP.

positional number systems • A number with radix r is represented by a string of digits: An .HCM Lê Chí Thông-www. Scott Mackenzie Lê Chí Thông 17 Representation of Number Systems • Positive radix. A.1 A.tinyurl.2 … A1A0 .com/thongchile Embedded System Sensor conditioning Output interfaces sensor actuator sensor Microcontroller (uC) indicator sensor Ref.2 … A.ĐH Bách Khoa TP. • The string of digits represents the power series: (Number)r = (∑ i=n-1 Ai r i + + Lê Chí Thông ) (∑ j=-1 Aj r j ) 18 i=0 j=-m (Integer Portion) Ref. Scott Mackenzie (Fraction Portion) 9 . is the radix point. I.1An .m in which 0 < Ai < r and .m + 1 A. I.

com/thongchile Representation of Number Systems General Radix (Base) Digits 0 1 2 3 Powers of 4 Radix 5 -1 -2 -3 -4 -5 Ref.ĐH Bách Khoa TP.6 + 0.375 Ref. I.011 B = 4 + 0 + 1 + 0 + 0. Scott Mackenzie Lê Chí Thông 20 10 . . .1 0.625 Binary (Radix r = 2) 1 22 1x22 4 0 21 0x21 0 1 20 1x20 1 .25 + 0.125 101.25 1 2-3 1x2-3 0. 0 2-1 0x2-1 0 1 2-2 1x2-2 0. .03125 19 r 0 => r . .6 2 10-2 2x10-2 0. .125 0. .000 100.HCM Lê Chí Thông-www.0625 0.125 = 5.02 5 10-3 5x10-3 0.1 r0 r1 r2 r3 r4 r5 r -1 r -2 r -3 r -4 r -5 Decimal (Radix r = 10) 4 102 4x102 400 0 101 0x101 0 7 100 7x100 7 . I.000 0.001 0.02 + 0.005 = 407.01 0.tinyurl. 6 10-1 6x10-1 0. Scott Mackenzie Decimal 10 0 => 9 1 10 100 1000 10.25 0.00001 Lê Chí Thông Binary 2 0 => 1 1 2 4 8 16 32 0.0001 0.5 0.005 400 + 0 + 7 + 0.

1 0 1 B 0. I. Scott Mackenzie Lê Chí Thông 22 11 . .HCM Lê Chí Thông-www.25 D 16-2 13x16-2 0. .301 Ref.625 x 2 = 1. 4 16-1 4x16-1 0.25 x 2 = 0.5 save the integer digit 0 0.0002 5A0.25 save the integer digit 1 0.tinyurl. I.0 save the integer digit 1 Ref.5 x 2 = 1.com/thongchile Hexadecimal or Hex (Radix r = 16) Hexadecimal Decimal 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 Binary 0000 0001 0010 0011 0100 0101 0110 0111 Hexadecimal Decimal 8 9 A B C D E F 8 9 10 11 12 13 14 15 Binary 1000 1001 1010 1011 1100 1101 1110 1111 5 162 5x162 1280 A 161 10x161 160 0 160 0x160 0 .0508 + 0. Scott Mackenzie Chí Thông 21 Converting decimal to binary 8 .0508 1 16-3 1x16-3 0.25 + 0. . 625 D = ? B 8 4 2 1 :2 = :2 = :2 = :2= 4 remainder 2 remainder 1 remainder 0 remainder 0 0 0 1 1 0 0 0 .4D1 H = 1280 + 160 + 0 +Lê0.0002 = 1440.ĐH Bách Khoa TP.

com/thongchile Converting decimal to hex 1480.875 x 16 = 14. 0 1 1 0 1 0 10 B 3 B 5 D .875 save the integer digit 6 0.4296875D=?H 1480 : 16 = 92 remainder 8 92 : 16 = 5 remainder 12 5 : 16 = 0 remainder 5 5 C 8 .HCM Lê Chí Thông-www.tinyurl.0 save the integer digit 14 Ref. E 8 H 0 01011001001.ĐH Bách Khoa TP. Scott Mackenzie Lê Chí Thông 23 Converting binary to hex 0 0 1 1 1 0 1 1 0 1 0 1 1 1 0 1 . I. Scott Mackenzie Lê Chí Thông 24 12 . 6 A H Converting binary to octal 2 C 9 . I. 6 E H 0.4296875 x 16 = 6.11101000 B Ref.

com/thongchile BCD (Binary Coded Decimal) Decimal 0 1 2 3 4 5 6 7 8 9 BCD (8 4 2 1) 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 Ex: 12 = 0001 0010 (BCD) 39 = 0011 1001 (BCD) Ref. Scott Mackenzie 1111 0 1 2 3 4 5 6 7 8 9 A B C D E F 0 NUL SOH STX ETX EOT ENQ ACK BEL BS HT LF VT FF CR SO SI 001 1 DLE DC1 DC2 DC3 DC4 NAK SYN ETB CAN EM SUB ESC FS GS RS US 010 2 011 3 100 4 @ A B C D E F G H I J K L M N O 101 5 P Q R S T U V W X Y Z [ \ ] ^ _ 110 6 ` a b c d e f g h i j k l m n o 111 7 p q r s t u v w x y z { | } ~ 26 DEL SP 0 ! 1 ” 2 # 3 $ 4 % 5 & 6 ’ 7 ( 8 ) 9 * : + .tinyurl. < = . Scott Mackenzie Lê Chí Thông 25 ASCII Code b6 b5 b4 000 b3b2b1b0 Hex 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 Ref.ĐH Bách Khoa TP. I. Chí Thông > Lê / ? 13 . . I.HCM Lê Chí Thông-www.

HCM Lê Chí Thông-www. I.tinyurl. Scott Mackenzie Lê Chí Thông 28 14 . Scott Mackenzie b 0 1 0 1 b 0 1 0 1 a AND b 0 0 0 1 a XOR b 0 1 1 0 27 b 0 1 0 1 a OR b 0 1 1 1 Lê Chí Thông a 0 0 1 1 Applying Boolean Operations • Force a bit or bits to zero – Sometimes called "masking out bits" • Use a "mask" byte with – 0's in positions to be cleared (forced to 0) – 1's in positions to be unchanged • Perform an AND with the mask – x AND 0 = 0 (domination) – x AND 1 = x (identity) Ref.com/thongchile Boolean (Logical) Operations a 0 1 NOT a 1 0 a 0 0 1 1 a 0 0 1 1 Ref.ĐH Bách Khoa TP. I.

I.HCM Lê Chí Thông-www. Scott Mackenzie Lê Chí Thông 29 Applying Boolean Operations • Toggle a bit or bits – Sometimes called "flipping or complementing bits" • Use a "mask" byte with – 1's in positions to be toggled (complemented) – 0's in positions to be unchanged • Perform an XOR with the mask – x XOR 0 = x (identity) – x XOR 1 = NOT x Ref. Scott Mackenzie Lê Chí Thông 30 15 .ĐH Bách Khoa TP.com/thongchile Applying Boolean Operations • Force a bit or bits to one – Sometimes called "setting bits" • Use a "mask" byte with – 1's in positions to be set (forced to 1) – 0's in positions to be unchanged • Perform an OR with the mask – x OR 0 = x (identity) – x OR 1 = 1 (domination) Ref.tinyurl. I.

I. Scott Mackenzie Lê Chí Thông 32 16 .com/thongchile Combining Bits in Separate Bytes • Bit patterns from two bytes are to be combined – The important bits of each byte must be aligned with 0 (and unimportant) bits in the other byte • The OR of the two bytes. Scott Mackenzie Lê Chí Thông 31 Shifting • Move bits in a byte to the left or right – 11011100 shifted left is 10111000 – 11011100 shifted right is 01101110 • In these examples. combines them into one – The bold bits are the important ones: 00110000 OR 00001010 00111010 Ref. a zero (0) was brought in to fill the vacated position – Variations on the basic shift fill this position differently Ref.tinyurl.HCM Lê Chí Thông-www. I.ĐH Bách Khoa TP.

I. Scott Mackenzie Lê Chí Thông 33 Application of Shifts • When a byte (or word) is interpreted numerically (as a binary representation of a number)… – Left shift is equivalent to multiplication by 2 – Right shift is equivalent to division by 2 – 5CH shifted left becomes B8H • 92 * 2 = 184 – 5CH shifted right is 2EH • 92 / 2 = 46 (remainder if any is thrown away) Ref.tinyurl. Scott Mackenzie Lê Chí Thông 34 17 .HCM Lê Chí Thông-www.com/thongchile Rotating • Move bits in a byte to the left or right in a circular pattern – 11011100 rotated left is 10111001 – 11011100 rotated right is 01101110 • In these examples. I. the bit shifted out is used to fill the vacated position – There are some variations of this behavior as well Ref.ĐH Bách Khoa TP.

Scott Mackenzie Lê Chí Thông 35 Memory • A computer system component that allows the storage and retrieval of data • Main memory is usually called RAM • Memory is usually organized as a table of Addresses bytes or words Ref. I. I.tinyurl.com/thongchile Multiplication Tricks • Every multiplication can be accomplished by shifting and adding – Just regroup using only multiplications by powers of 2 and additions • 10 * n = (8 + 2) * n • =8*n+2*n – or • 10 * n = (2 * (4 + 1)) n • = 2 * (4 * n + n) Ref. Scott Mackenzie Lê Chí Thông 0000 0001 0002 0003 0004 0005 3F 2C 41 FF 00 1E Data values are usually bytes 36 are usually shown in hexadecimal 18 .ĐH Bách Khoa TP.HCM Lê Chí Thông-www.

tinyurl.HCM Lê Chí Thông-www. Scott Mackenzie Lê Chí Thông 37 RAM and ROM RAM ROM Ref.com/thongchile RAM and ROM • RAM: Random Access Memory – Access time does not vary by address – Read & write memory – Typically volatile • ROM: Read Only Memory – Also a random access memory – Retains data even when power is removed Ref. I. I.ĐH Bách Khoa TP. Scott Mackenzie Lê Chí Thông 38 19 .

Scott Mackenzie Lê Chí Thông 39 20 .ĐH Bách Khoa TP.tinyurl. Scott Mackenzie.com/thongchile References • I.HCM Lê Chí Thông-www. The 8051 Microcontroller • Nguy n Tr ng Lu t. Bài gi ng K Thu t S • Các tài li u trên Internet không trích d n ho c không ghi tác gi Ref. I.