The 8051 Microcontroller and Embedded Systems Using Assembly and C Second Edition Muhammad Ali Mazidi Janice Gillispie Mazidi Rolin D. McKinlay CONTENTS Introduction to Computing The 8051 Microcontrollers 8051 Assembly Language Programming Branch Instructions I/O Port Programming 8051 Addressing Modes Arithmetic & Logic Instructions And Programs 8051 Programming in C 8051 Hardware Connection and Hex File 8051 Timer/Counter Programming in Assembly and C 8051 Serial Port Programming in Assembly and C Interrupts Programming in Assembly and C 8051 Interfacing to External Memory 8051 Real World Interfacing I: LCD,ADC AND SENSORS LCD and Keyboard Interfacing 8051 Interfacing with 8255 INTRODUCTION TO COMPUTING The 8051 Microcontroller and Embedded Systems: Using Assembly and C Mazidi, Mazidi and McKinlay Chung-Ping Young 楊中平 Home Automation, Networking, and Entertainment Lab Dept. of Computer Science and Information Engineering National Cheng Kung University, TAIWAN Numbering and coding systems OUTLINES Digital primer Inside the computer Department of Computer Science and Information Engineering HANEL National Cheng Kung University, TAIWAN 2 Human beings use base 10 (decimal) NUMBERING AND CODING arithmetic SYSTEMS ¾ There are 10 distinct symbols, 0, 1, 2, …, 9 Decimal and Computers use base 2 (binary) system Binary Number ¾ There are only 0 and 1 Systems ¾ These two binary digits are commonly referred to as bits Department of Computer Science and Information Engineering HANEL National Cheng Kung University, TAIWAN 3 Divide the decimal number by 2 NUMBERING AND CODING repeatedly SYSTEMS Keep track of the remainders Continue this process until the quotient Converting becomes zero from Decimal Write the remainders in reverse order to Binary to obtain the binary number Ex. Convert 2510 to binary Quotient Remainder 25/2 = 12 1 LSB (least significant bit) 12/2 = 6 0 6/2 = 3 0 3/2 = 1 1 1/2 = 0 1 MSB (most significant bit) Therefore 2510 = 110012 Department of Computer Science and Information Engineering HANEL National Cheng Kung University, TAIWAN 4 Know the weight of each bit in a binary NUMBERING number AND CODING Add them together to get its decimal SYSTEMS equivalent Converting Ex.
Convert 110012 to decimal from Binary to Weight: 24 23 22 21 20 Decimal Digits: 1 1 0 0 1 Sum: 16 + 8+ 0+ 0+ 1 = 2510 Use the concept of weight to convert a decimal number to a binary directly Ex. Convert 3910 to binary 32 + 0 + 0 + 4 + 2 + 1 = 39 Therefore, 3910 = 1001112 Department of Computer Science and Information Engineering HANEL National Cheng Kung University, TAIWAN 5 Base 16, the NUMBERING AND CODING hexadecimal system, Decimal Binary Hex SYSTEMS is used as a 0 0000 0 1 0001 1 convenient 2 0010 2 Hexadecimal representation of 3 0011 3 4 0100 4 System binary numbers 5 0101 5 ¾ ex. 6 0110 6 7 0111 7 It is much easier to 8 1000 8 represent a string of 0s 9 1001 9 and 1s such as 10 1010 A 100010010110 as its 11 1011 B hexadecimal equivalent of 12 1100 C 896H 13 1101 D 14 1110 E 15 1111 F Department of Computer Science and Information Engineering HANEL National Cheng Kung University, TAIWAN 6 To represent a binary number as its NUMBERING equivalent hexadecimal number AND CODING ¾ Start from the right and group 4 bits at a SYSTEMS time, replacing each 4-bit binary number with its hex equivalent Converting between Binary Ex. Represent binary 100111110101 in hex and Hex 1001 1111 0101 = 9 F 5 To convert from hex to binary ¾ Each hex digit is replaced with its 4-bit binary equivalent Ex.
Convert hex 29B to binary 2 9 B = 0010 1001 1011 Department of Computer Science and Information Engineering HANEL National Cheng Kung University, TAIWAN 7 Convert to binary first and then NUMBERING AND CODING convert to hex SYSTEMS Convert directly from decimal to hex by repeated division, keeping track of Converting the remainders from Decimal to Hex Ex. Convert 4510 to hex 32 16 8 4 2 1 1 0 1 1 0 1 32 + 8 + 4 + 1 = 45 4510 = 0010 11012 = 2D16 Ex. Convert 62910 to hex 512 256 128 64 32 16 8 4 2 1 1 0 0 1 1 1 0 1 0 1 62910 = 512+64+32+16+4+1 = 0010 0111 01012 = 27516 Department of Computer Science and Information Engineering HANEL National Cheng Kung University, TAIWAN 8 Convert from hex to binary and then to NUMBERING AND CODING decimal SYSTEMS Convert directly from hex to decimal by summing the weight of all digits Converting from Hex to Ex. 6B216 = 0110 1011 00102 1024 512 256 128 64 32 16 8 4 2 1 Decimal 1 1 0 1 0 1 1 0 0 1 0 1024 + 512 + 128 + 32 + 16 + 2 = 171410 Department of Computer Science and Information Engineering HANEL National Cheng Kung University, TAIWAN 9 Adding the digits together from the NUMBERING AND CODING least significant digits SYSTEMS ¾ If the result is less than 16, write that digit as the sum for that position Addition of Hex ¾ If it is greater than 16, subtract 16 from it Numbers to get the digit and carry 1 to the next digit Ex.
Perform hex addition: 23D9 + 94BE 23D9 LSD: 9 + 14 = 23 23 – 16 = 7 w/ carry + 94BE 1 + 13 + 11 = 25 25 – 16 = 9 w/ carry B897 1+3+4=8 MSD: 2 + 9 = B Department of Computer Science and Information Engineering HANEL National Cheng Kung University, TAIWAN 10 If the second digit is greater than the NUMBERING AND CODING first, borrow 16 from the preceding SYSTEMS digit Ex. Perform hex subtraction: 59F – 2B8 Subtraction of Hex Numbers 59F LSD: 15 – 8 = 7 – 2B8 9 + 16 – 11 = 14 = E16 2E7 5–1–2=2 Department of Computer Science and Information Engineering HANEL National Cheng Kung University, TAIWAN 11 The ASCII (pronounced “ask-E”) code NUMBERING assigns binary patterns for AND CODING ¾ Numbers 0 to 9 SYSTEMS ¾ All the letters of English alphabet, uppercase and lowercase ASCII Code ¾ Many control codes and punctuation marks The ASCII system uses 7 bits to represent each code Hex Symbol Hex Symbol Selected ASCII codes 41 A 61 a 42 B 62 b 43 C 63 c 44 D 64 d. … 59 Y 79 y 5A Z 7A z Department of Computer Science and Information Engineering HANEL National Cheng Kung University, TAIWAN 12 Two voltage levels can be represented DIGITAL PRIMER as the two digits 0 and 1 Signals in digital electronics have two Binary Logic 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 5 4 Logic 1 3 2 1 Logic 0 0 Department of Computer Science and Information Engineering HANEL National Cheng Kung University, TAIWAN 13 AND gate DIGITAL PRIMER Logic Gates Computer Science Illuminated, Dale and Lewis OR gate Computer Science Illuminated, Dale and Lewis Department of Computer Science and Information Engineering HANEL National Cheng Kung University, TAIWAN 14 Tri-state buffer DIGITAL PRIMER Inverter Logic Gates (cont’) Computer Science Illuminated, Dale and Lewis XOR gate Computer Science Illuminated, Dale and Lewis Department of Computer Science and Information Engineering HANEL National Cheng Kung University, TAIWAN 15 NAND gate DIGITAL PRIMER Logic Gates (cont’) Computer Science Illuminated, Dale and Lewis NOR gate Computer Science Illuminated, Dale and Lewis Department of Computer Science and Information Engineering HANEL National Cheng Kung University, TAIWAN 16 DIGITAL PRIMER Half adder Logic Design Using Gates Full adder Digital Design, Mano Department of Computer Science and Information Engineering HANEL National Cheng Kung University, TAIWAN 17 DIGITAL PRIMER 4-bit adder Logic Design Using Gates (cont’) Digital Design, Mano Department of Computer Science and Information Engineering HANEL National Cheng Kung University, TAIWAN 18 Decoders DIGITAL ¾ Decoders are widely used for address PRIMER decoding in computer design Logic Design Address Decoders Using Gates (cont’) Address decoder for 9 (10012) Address decoder for 5 (01012) The output will be 1 if and The output will be 1 if and only if the input is 10012 only if the input is 01012 Department of Computer Science and Information Engineering HANEL National Cheng Kung University, TAIWAN 19 Flip-flops DIGITAL ¾ Flip-flops are frequently used to store data PRIMER Logic Design Using Gates (cont’) Digital Design, Mano Department of Computer Science and Information Engineering HANEL National Cheng Kung University, TAIWAN 20 The unit of data size INSIDE THE ¾ Bit : a binary digit that can have the value COMPUTER 0 or 1 Important ¾ Byte : 8 bits Terminology ¾ 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 Department of Computer Science and Information Engineering HANEL National Cheng Kung University, TAIWAN 21 CPU (Central Processing Unit) INSIDE THE ¾ Execute information stored in memory COMPUTER I/O (Input/output) devices ¾ Provide a means of communicating with Internal CPU Organization of Memory Computers ¾ RAM (Random Access Memory) – temporary storage of programs that computer is running The data is lost when computer is off ¾ ROM (Read Only Memory) – contains programs and information essential to operation of the computer The information cannot be changed by use, and is not lost when power is off – It is called nonvolatile memory Department of Computer Science and Information Engineering HANEL National Cheng Kung University, TAIWAN 22 INSIDE THE COMPUTER Internal Address bus Organization of Computers Memory Peripherals (cont’) CPU (monitor, (RAM, ROM) printer, etc.