The 8051 Microcontroller and Embedded
Systems
Using Assembly and C
Second Edition
Muhammad Ali Mazidi
Janice Gillispie Maz...
Home Automation, Networking, and Entertainment Lab
Dept. of Computer Science and Information Engineering
National Cheng Ku...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 2
HANEL
OUTLINES
Numberi...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 3
HANEL
NUMBERING
AND CO...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 4
HANEL
NUMBERING
AND CO...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 5
HANEL
NUMBERING
AND CO...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 6
HANEL
NUMBERING
AND CO...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 7
HANEL
NUMBERING
AND CO...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 8
HANEL
NUMBERING
AND CO...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 9
HANEL
NUMBERING
AND CO...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 10
HANEL
NUMBERING
AND C...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 11
HANEL
NUMBERING
AND C...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 12
HANEL
NUMBERING
AND C...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 13
HANEL
DIGITAL
PRIMER
...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 14
HANEL
DIGITAL
PRIMER
...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 15
HANEL
DIGITAL
PRIMER
...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 16
HANEL
DIGITAL
PRIMER
...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 17
HANEL
DIGITAL
PRIMER
...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 18
HANEL
DIGITAL
PRIMER
...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 19
HANEL
DIGITAL
PRIMER
...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 20
HANEL
DIGITAL
PRIMER
...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 21
HANEL
INSIDE THE
COMP...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 22
HANEL
INSIDE THE
COMP...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 23
HANEL
INSIDE THE
COMP...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 24
HANEL
INSIDE THE
COMP...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 25
HANEL
INSIDE THE
COMP...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 26
HANEL
INSIDE THE
COMP...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 27
HANEL
INSIDE THE
COMP...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 28
HANEL
INSIDE THE
COMP...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 29
HANEL
INSIDE THE
COMP...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 30
HANEL
INSIDE THE
COMP...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 31
HANEL
INSIDE THE
COMP...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 32
HANEL
INSIDE THE
COMP...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 33
HANEL
INSIDE THE
COMP...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 34
HANEL
INSIDE THE
COMP...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 35
HANEL
INSIDE THE
COMP...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 36
HANEL
INSIDE THE
COMP...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 37
HANEL
INSIDE THE
COMP...
Home Automation, Networking, and Entertainment Lab
Dept. of Computer Science and Information Engineering
National Cheng Ku...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 2
HANEL
OUTLINES
Microco...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 3
HANEL
MICRO-
CONTROLLE...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 4
HANEL
MICRO-
CONTROLLE...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 5
HANEL
MICRO-
CONTROLLE...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 6
HANEL
MICRO-
CONTROLLE...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 7
HANEL
MICRO-
CONTROLLE...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 8
HANEL
MICRO-
CONTROLLE...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 9
HANEL
MICRO-
CONTROLLE...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 10
HANEL
MICRO-
CONTROLL...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 11
HANEL
MICRO-
CONTROLL...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 12
HANEL
MICRO-
CONTROLL...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 13
HANEL
OVERVIEW OF
805...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 14
HANEL
OVERVIEW OF
805...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 15
HANEL
OVERVIEW OF
805...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 16
HANEL
OVERVIEW OF
805...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 17
HANEL
OVERVIEW OF
805...
Home Automation, Networking, and Entertainment Lab
Dept. of Computer Science and Information Engineering
National Cheng Ku...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 2
HANEL
INSIDE THE
8051
...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 3
HANEL
INSIDE THE
8051
...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 4
HANEL
INSIDE THE
8051
...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 5
HANEL
INSIDE THE
8051
...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 6
HANEL
INSIDE THE
8051
...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 7
HANEL
INSIDE THE
8051
...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 8
HANEL
Structure of
Ass...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 9
HANEL
Structure of
Ass...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 10
HANEL
Structure of
As...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 11
HANEL
ASSEMBLING
AND ...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 12
HANEL
ASSEMBLING
AND ...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 13
HANEL
ASSEMBLING
AND ...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 14
HANEL
ASSEMBLING
AND ...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 15
HANEL
ASSEMBLING
AND ...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 16
HANEL
PROGRAM
COUNTER...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 17
HANEL
PROGRAM
COUNTER...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 18
HANEL
PROGRAM
COUNTER...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 19
HANEL
PROGRAM
COUNTER...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 20
HANEL
PROGRAM
COUNTER...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 21
HANEL
PROGRAM
COUNTER...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 22
HANEL
PROGRAM
COUNTER...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 23
HANEL
8051 DATA
TYPES...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 24
HANEL
8051 DATA
TYPES...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 25
HANEL
8051 DATA
TYPES...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 26
HANEL
8051 DATA
TYPES...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 27
HANEL
8051 DATA
TYPES...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 28
HANEL
FLAG BITS AND
P...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 29
HANEL
FLAG BITS AND
P...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 30
HANEL
FLAG BITS AND
P...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 31
HANEL
FLAG BITS AND
P...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 32
HANEL
FLAG BITS AND
P...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 33
HANEL
FLAG BITS AND
P...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 34
HANEL
REGISTER
BANKS ...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 35
HANEL
8051
REGISTER
B...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 36
HANEL
8051
REGISTER
B...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 37
HANEL
8051
REGISTER
B...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 38
HANEL
8051
REGISTER
B...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 39
HANEL
8051
REGISTER
B...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 40
HANEL
8051
REGISTER
B...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 41
HANEL
8051
REGISTER
B...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 42
HANEL
8051
REGISTER
B...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 43
HANEL
8051
REGISTER
B...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 44
HANEL
8051
REGISTER
B...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 45
HANEL
8051
REGISTER
B...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 46
HANEL
8051
REGISTER
B...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 47
HANEL
8051
REGISTER
B...
Home Automation, Networking, and Entertainment Lab
Dept. of Computer Science and Information Engineering
National Cheng Ku...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 2
HANEL
LOOP AND
JUMP
IN...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 3
HANEL
LOOP AND
JUMP
IN...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 4
HANEL
LOOP AND
JUMP
IN...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 5
HANEL
LOOP AND
JUMP
IN...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 6
HANEL
LOOP AND
JUMP
IN...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 7
HANEL
LOOP AND
JUMP
IN...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 8
HANEL
LOOP AND
JUMP
IN...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 9
HANEL
LOOP AND
JUMP
IN...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 10
HANEL
CALL
INSTRUCTIO...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 11
HANEL
CALL
INSTRUCTIO...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 12
HANEL
CALL
INSTRUCTIO...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 13
HANEL
CALL
INSTRUCTIO...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 14
HANEL
CALL
INSTRUCTIO...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 15
HANEL
CALL
INSTRUCTIO...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 16
HANEL
CALL
INSTRUCTIO...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 17
HANEL
CALL
INSTRUCTIO...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 18
HANEL
TIME DELAY
FOR ...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 19
HANEL
TIME DELAY
FOR ...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 20
HANEL
TIME DELAY
FOR ...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 21
HANEL
TIME DELAY
FOR ...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 22
HANEL
TIME DELAY
FOR ...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 23
HANEL
TIME DELAY
FOR ...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 24
HANEL
TIME DELAY
FOR ...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 25
HANEL
TIME DELAY
FOR ...
Home Automation, Networking, and Entertainment Lab
Dept. of Computer Science and Information Engineering
National Cheng Ku...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 2
HANEL
I/O
PROGRAMMING
...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 3
HANEL
I/O
PROGRAMMING
...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 4
HANEL
I/O
PROGRAMMING
...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 5
HANEL
I/O
PROGRAMMING
...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 6
HANEL
I/O
PROGRAMMING
...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 7
HANEL
I/O
PROGRAMMING
...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 8
HANEL
I/O
PROGRAMMING
...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 9
HANEL
I/O
PROGRAMMING
...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 10
HANEL
I/O
PROGRAMMING...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 11
HANEL
I/O
PROGRAMMING...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 12
HANEL
I/O
PROGRAMMING...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 13
HANEL
I/O
PROGRAMMING...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 14
HANEL
I/O
PROGRAMMING...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 15
HANEL
I/O
PROGRAMMING...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 16
HANEL
I/O BIT
MANIPUL...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 17
HANEL
I/O BIT
MANIPUL...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 18
HANEL
I/O BIT
MANIPUL...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 19
HANEL
I/O BIT
MANIPUL...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 20
HANEL
I/O BIT
MANIPUL...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 21
HANEL
I/O BIT
MANIPUL...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 22
HANEL
I/O BIT
MANIPUL...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 23
HANEL
I/O BIT
MANIPUL...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 24
HANEL
I/O BIT
MANIPUL...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 25
HANEL
I/O BIT
MANIPUL...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 26
HANEL
READING
INPUT P...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 27
HANEL
READING
INPUT P...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 28
HANEL
I/O BIT
MANIPUL...
Home Automation, Networking, and Entertainment Lab
Dept. of Computer Science and Information Engineering
National Cheng Ku...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 2
HANEL
ADDRESSING
MODES...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 3
HANEL
IMMEDIATE
ADDRES...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 4
HANEL
IMMEDIATE
ADDRES...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 5
HANEL
REGISTER
ADDRESS...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 6
HANEL
ACCESSING
MEMORY...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 7
HANEL
ACCESSING
MEMORY...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 8
HANEL
ACCESSING
MEMORY...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 9
HANEL
ACCESSING
MEMORY...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 10
HANEL
ACCESSING
MEMOR...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 11
HANEL
ACCESSING
MEMOR...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 12
HANEL
ACCESSING
MEMOR...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 13
HANEL
ACCESSING
MEMOR...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 14
HANEL
ACCESSING
MEMOR...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 15
HANEL
ACCESSING
MEMOR...
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 16
HANEL
ACCESSING
MEMOR...
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Mazidi
Upcoming SlideShare
Loading in...5
×

Mazidi

2,686

Published on

Published in: Technology

Mazidi

  1. 1. 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
  2. 2. Home Automation, Networking, and Entertainment Lab Dept. of Computer Science and Information Engineering National Cheng Kung University, TAIWAN Chung-Ping Young 楊中平 INTRODUCTION TO COMPUTING The 8051 Microcontroller and Embedded Systems: Using Assembly and C Mazidi, Mazidi and McKinlay
  3. 3. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 2 HANEL OUTLINES Numbering and coding systems Digital primer Inside the computer
  4. 4. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 3 HANEL NUMBERING AND CODING SYSTEMS Decimal and Binary Number Systems Human beings use base 10 (decimal) arithmetic There are 10 distinct symbols, 0, 1, 2, …, 9 Computers use base 2 (binary) system There are only 0 and 1 These two binary digits are commonly referred to as bits
  5. 5. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 4 HANEL NUMBERING AND CODING SYSTEMS Converting from Decimal to Binary Divide the decimal number by 2 repeatedly Keep track of the remainders Continue this process until the quotient becomes zero Write the remainders in reverse order 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
  6. 6. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 5 HANEL NUMBERING AND CODING SYSTEMS Converting from Binary to Decimal Know the weight of each bit in a binary number Add them together to get its decimal equivalent Use the concept of weight to convert a decimal number to a binary directly Ex. Convert 110012 to decimal Weight: 24 23 22 21 20 Digits: 1 1 0 0 1 Sum: 16 + 8 + 0 + 0 + 1 = 2510 Ex. Convert 3910 to binary 32 + 0 + 0 + 4 + 2 + 1 = 39 Therefore, 3910 = 1001112
  7. 7. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 6 HANEL NUMBERING AND CODING SYSTEMS Hexadecimal System Base 16, the hexadecimal system, is used as a convenient representation of binary numbers ex. It is much easier to represent a string of 0s and 1s such as 100010010110 as its hexadecimal equivalent of 896H F111115 E111014 D110113 C110012 B101111 A101010 910019 810008 701117 601106 501015 401004 300113 200102 100011 000000 HexBinaryDecimal
  8. 8. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 7 HANEL NUMBERING AND CODING SYSTEMS Converting between Binary and Hex To 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 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 Ex. Represent binary 100111110101 in hex 1001 1111 0101 = 9 F 5
  9. 9. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 8 HANEL NUMBERING AND CODING SYSTEMS Converting from Decimal to Hex Convert to binary first and then convert to hex Convert directly from decimal to hex by repeated division, keeping track of the remainders 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
  10. 10. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 9 HANEL NUMBERING AND CODING SYSTEMS Converting from Hex to Decimal Convert from hex to binary and then to decimal Convert directly from hex to decimal by summing the weight of all digits Ex. 6B216 = 0110 1011 00102 1024 512 256 128 64 32 16 8 4 2 1 1 1 0 1 0 1 1 0 0 1 0 1024 + 512 + 128 + 32 + 16 + 2 = 171410
  11. 11. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 10 HANEL NUMBERING AND CODING SYSTEMS Addition 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 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
  12. 12. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 11 HANEL NUMBERING AND CODING SYSTEMS Subtraction of Hex Numbers If the second digit is greater than the first, borrow 16 from the preceding digit Ex. Perform hex subtraction: 59F – 2B8 59F LSD: 15 – 8 = 7 – 2B8 9 + 16 – 11 = 14 = E16 2E7 5 – 1 – 2 = 2
  13. 13. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 12 HANEL NUMBERING AND CODING SYSTEMS ASCII 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 marks The ASCII system uses 7 bits to represent each code z7AZ5A y79Y59 …......... d64D44 c63C43 b62B42 a61A41 SymbolHexSymbolHex Selected ASCII codes
  14. 14. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 13 HANEL DIGITAL PRIMER Binary 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 0 1 2 3 4 5 Logic 0 Logic 1
  15. 15. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 14 HANEL DIGITAL PRIMER Logic Gates AND gate OR gate Computer Science Illuminated, Dale and Lewis Computer Science Illuminated, Dale and Lewis
  16. 16. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 15 HANEL DIGITAL PRIMER Logic Gates (cont’) Tri-state buffer Inverter XOR gate Computer Science Illuminated, Dale and Lewis Computer Science Illuminated, Dale and Lewis
  17. 17. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 16 HANEL DIGITAL PRIMER Logic Gates (cont’) NAND gate NOR gate Computer Science Illuminated, Dale and Lewis Computer Science Illuminated, Dale and Lewis
  18. 18. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 17 HANEL DIGITAL PRIMER Logic Design Using Gates Half adder Full adder Digital Design, Mano
  19. 19. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 18 HANEL DIGITAL PRIMER Logic Design Using Gates (cont’) 4-bit adder Digital Design, Mano
  20. 20. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 19 HANEL DIGITAL PRIMER Logic Design Using Gates (cont’) Decoders Decoders are widely used for address decoding in computer design Address decoder for 9 (10012) The output will be 1 if and only if the input is 10012 Address decoder for 5 (01012) The output will be 1 if and only if the input is 01012 Address Decoders
  21. 21. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 20 HANEL DIGITAL PRIMER Logic Design Using Gates (cont’) Flip-flops Flip-flops are frequently used to store data Digital Design, Mano
  22. 22. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 21 HANEL INSIDE THE COMPUTER Important Terminology The unit of data size Bit : a binary digit that can have 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
  23. 23. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 22 HANEL INSIDE THE COMPUTER Internal Organization of Computers CPU (Central Processing Unit) Execute information stored in memory I/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 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
  24. 24. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 23 HANEL INSIDE THE COMPUTER Internal Organization of Computers (cont’) CPU Memory (RAM, ROM) Peripherals (monitor, printer, etc.) Address bus Data bus
  25. 25. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 24 HANEL INSIDE THE COMPUTER Internal Organization of 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 Control bus CPU Read/ Write RAM Address bus Data bus ROM Printer Disk Monitor Keyboard Control bus
  26. 26. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 25 HANEL INSIDE THE COMPUTER Internal Organization of 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, and the decoding circuitry finds the device 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
  27. 27. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 26 HANEL INSIDE THE COMPUTER More about Data Bus The more data buses available, the better the CPU Think of data buses as highway lanes More data buses mean a more expensive CPU and computer The average size of data buses in CPUs varies between 8 and 64 Data buses are bidirectional To receive or send data The processing power of a computer is related to the size of its buses
  28. 28. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 27 HANEL INSIDE THE COMPUTER More about Address Bus The more address buses available, the larger the number of devices that can be addressed The number of locations with which a CPU can communicate is always equal to 2x, where x is the address lines, regardless of the size of the data bus ex. a CPU with 24 address lines and 16 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, since all general-purpose CPUs are byte addressable The address bus is unidirectional
  29. 29. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 28 HANEL INSIDE THE COMPUTER CPU’s Relation to RAM and ROM For the CPU to process information, the data must be stored in RAM or ROM, which are 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 CPU gets information to be processed first form RAM (or ROM) if it is not there, then seeks it from a mass storage device, called secondary memory, and transfers the information to RAM
  30. 30. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 29 HANEL INSIDE THE COMPUTER Inside CPUs Registers The 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 CPU Registers can be 8-, 16-, 32-, or 64-bit The disadvantage of more and bigger registers is the increased cost of such a CPU
  31. 31. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 30 HANEL INSIDE THE COMPUTER Inside CPUs (cont’) Flags ALU Program Counter Instruction Register Instruction decoder, timing, and control Register A Register B Register C Register D AddressBusControlBusDataBus Internal buses
  32. 32. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 31 HANEL INSIDE THE COMPUTER Inside CPUs (cont’) ALU (arithmetic/logic unit) Performs arithmetic functions such as add, subtract, multiply, and divide, and 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 Instruction decoder Interprets the instruction fetched into the CPU A CPU capable of understanding more instructions requires more transistors to design
  33. 33. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 32 HANEL INSIDE THE COMPUTER Internal Working of Computers Ex. A CPU has registers A, B, C, and D and it has an 8-bit data bus and a 16-bit address bus. The CPU can access memory from addresses 0000 to FFFFH Assume that the code for the CPU to move a value to register A is B0H and the code for adding a value to register A is 04H The action to be performed by the CPU is to put 21H into register A, and then add to register A values 42H and 12H ...
  34. 34. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 33 HANEL INSIDE THE COMPUTER Internal Working of Computers (cont’) Ex. (cont’) Action Code Data Move value 21H into reg. A B0H 21H Add value 42H to reg. A 04H 42H Add value 12H to reg. A 04H 12H Mem. addr. Contents of memory address 1400 (B0) code for moving a value to register A 1401 (21) value to be moved 1402 (04) code for adding a value to register A 1403 (42) value to be added 1404 (04) code for adding a value to register A 1405 (12) value to be added 1406 (F4) code for halt ...
  35. 35. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 34 HANEL INSIDE THE COMPUTER Internal Working of Computers (cont’) Ex. (cont’) The actions performed by CPU are as follows: 1. The program counter is set to the value 1400H, indicating the address of the first instruction code to be executed 2. The CPU puts 1400H on address bus and sends it out The memory circuitry finds the location The CPU activates the READ signal, indicating to memory that it wants the byte at location 1400H This causes the contents of memory location 1400H, which is B0, to be put on the data bus and brought into the CPU ... 以動畫表示
  36. 36. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 35 HANEL INSIDE THE COMPUTER Internal Working of Computers (cont’) Ex. (cont’) 3. The CPU decodes the instruction B0 The CPU commands its controller circuitry to bring into register A of the CPU the byte in the next memory location The value 21H goes into register A The program counter points to the address of the next instruction to be executed, which is 1402H Address 1402 is sent out on the address bus to fetch the next instruction ...
  37. 37. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 36 HANEL INSIDE THE COMPUTER Internal Working of Computers (cont’) Ex. (cont’) 4. From memory location 1402H it fetches code 04H After decoding, the CPU knows that it must add to the contents of register A the byte sitting at the next address (1403) After the CPU brings the value (42H), it provides the contents of register A along with this value to the ALU to perform the addition It then takes the result of the addition from the ALU’s output and puts it in register A The program counter becomes 1404, the address of the next instruction ...
  38. 38. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 37 HANEL INSIDE THE COMPUTER Internal Working of Computers (cont’) Ex. (cont’) 5. Address 1404H is put on the address bus and the code is fetched into the CPU, decoded, and executed This code is again adding a value to register A The program counter is updated to 1406H 6. The contents of address 1406 are fetched in and executed This HALT instruction tells the CPU to stop incrementing the program counter and asking for the next instruction
  39. 39. Home Automation, Networking, and Entertainment Lab Dept. of Computer Science and Information Engineering National Cheng Kung University, TAIWAN Chung-Ping Young 楊中平 8051 MICROCONTROLLERS The 8051 Microcontroller and Embedded Systems: Using Assembly and C Mazidi, Mazidi and McKinlay
  40. 40. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 2 HANEL OUTLINES Microcontrollers and embedded processors Overview of the 8051 family
  41. 41. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 3 HANEL MICRO- CONTROLLERS AND EMBEDDED PROCESSORS Microcontroller vs. General- Purpose Microprocessor General-purpose microprocessors contains No RAM No ROM No I/O ports Microcontroller has CPU (microprocessor) RAM ROM I/O ports Timer ADC and other peripherals
  42. 42. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 4 HANEL MICRO- CONTROLLERS AND EMBEDDED PROCESSORS Microcontroller vs. General- Purpose Microprocessor (cont’) Serial COM Port TimerI/O ROMRAMCPU Microcontroller CPU RAM Address bus Data bus ROM I/O Port Serial COM Port Timer General- purpose Micro- Processor
  43. 43. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 5 HANEL MICRO- CONTROLLERS AND EMBEDDED PROCESSORS Microcontroller vs. General- Purpose Microprocessor (cont’) General-purpose microprocessors Must add RAM, ROM, I/O ports, and timers externally to make them functional Make the system bulkier and much more expensive Have the advantage of versatility on the amount of RAM, ROM, and I/O ports Microcontroller The fixed amount of on-chip ROM, RAM, and number of I/O ports makes them ideal for many applications in which cost and space are critical In many applications, the space it takes, the power it consumes, and the price per unit are much more critical considerations than the computing power
  44. 44. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 6 HANEL MICRO- CONTROLLERS AND EMBEDDED PROCESSORS Microcontrollers for Embedded Systems An embedded product uses a microprocessor (or microcontroller) to do one task and one task only There is only one application software that is typically burned into ROM A PC, in contrast with the embedded system, can be used for any number of applications It has RAM memory and an operating system that loads a variety of applications into RAM and lets the CPU run them A PC contains or is connected to various embedded products Each one peripheral has a microcontroller inside it that performs only one task
  45. 45. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 7 HANEL MICRO- CONTROLLERS AND EMBEDDED PROCESSORS Microcontrollers for Embedded Systems (cont’) Home Appliances, intercom, telephones, security systems, garage door openers, answering machines, fax machines, home computers, TVs, cable TV tuner, VCR, camcorder, remote controls, video games, cellular phones, musical instruments, sewing machines, lighting control, paging, camera, pinball machines, toys, exercise equipment Office Telephones, computers, security systems, fax machines, microwave, copier, laser printer, color printer, paging Auto Trip computer, engine control, air bag, ABS, instrumentation, security system, transmission control, entertainment, climate control, cellular phone, keyless entry
  46. 46. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 8 HANEL MICRO- CONTROLLERS AND EMBEDDED PROCESSORS x86 PC Embedded Applications Many manufactures of general-purpose microprocessors have targeted their microprocessor for the high end of the embedded market There are times that a microcontroller is inadequate for the task When a company targets a general- purpose microprocessor for the embedded market, it optimizes the processor used for embedded systems Very often the terms embedded processor and microcontroller are used interchangeably
  47. 47. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 9 HANEL MICRO- CONTROLLERS AND EMBEDDED PROCESSORS x86 PC Embedded Applications (cont’) One of the most critical needs of an embedded system is to decrease power consumption and space In high-performance embedded processors, the trend is to integrate more functions on the CPU chip and let designer decide which features he/she wants to use In many cases using x86 PCs for the high-end embedded applications Saves money and shortens development time A vast library of software already written Windows is a widely used and well understood platform
  48. 48. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 10 HANEL MICRO- CONTROLLERS AND EMBEDDED PROCESSORS Choosing a Microcontroller 8-bit microcontrollers Motorola’s 6811 Intel’s 8051 Zilog’s Z8 Microchip’s PIC There are also 16-bit and 32-bit microcontrollers made by various chip makers
  49. 49. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 11 HANEL MICRO- CONTROLLERS AND EMBEDDED PROCESSORS Criteria for Choosing a Microcontroller Meeting the computing needs of the task at hand efficiently and cost effectively Speed Packaging Power consumption The amount of RAM and ROM on chip The number of I/O pins and the timer on chip How easy to upgrade to higher- performance or lower power-consumption versions Cost per unit
  50. 50. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 12 HANEL MICRO- CONTROLLERS AND EMBEDDED PROCESSORS Criteria for Choosing a Microcontroller (cont’) Availability of software development tools, such as compilers, assemblers, and debuggers Wide availability and reliable sources of the microcontroller The 8051 family has the largest number of diversified (multiple source) suppliers Intel (original) Atmel Philips/Signetics AMD Infineon (formerly Siemens) Matra Dallas Semiconductor/Maxim
  51. 51. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 13 HANEL OVERVIEW OF 8051 FAMILY 8051 Microcontroller Intel introduced 8051, referred as MCS- 51, in 1981 The 8051 is an 8-bit processor The CPU can work on only 8 bits of data at a time The 8051 had 128 bytes of RAM 4K bytes of on-chip ROM Two timers One serial port Four I/O ports, each 8 bits wide 6 interrupt sources The 8051 became widely popular after allowing other manufactures to make and market any flavor of the 8051, but remaining code-compatible
  52. 52. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 14 HANEL OVERVIEW OF 8051 FAMILY 8051 Microcontroller (cont’) Interrupt Control Bus Control CPU OSC I/O Ports Serial Port Etc. Timer 0 Timer 1 On-chip RAM On-chip ROM for code P0 P1 P2 P3 Address/Data TXD RXD CounterInputs External Interrupts
  53. 53. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 15 HANEL OVERVIEW OF 8051 FAMILY 8051 Family The 8051 is a subset of the 8052 The 8031 is a ROM-less 8051 Add external ROM to it You lose two ports, and leave only 2 ports for I/O operations 686Interrupt sources 111Serial port 323232I/O pins 232Timers 128256128RAM (bytes) 0K8K4K ROM (on-chip program space in bytes) 803180528051Feature
  54. 54. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 16 HANEL OVERVIEW OF 8051 FAMILY Various 8051 Microcontrollers 8751 microcontroller UV-EPROM PROM burner UV-EPROM eraser takes 20 min to erase AT89C51 from Atmel Corporation Flash (erase before write) ROM burner that supports flash A separate eraser is not needed DS89C4x0 from Dallas Semiconductor, now part of Maxim Corp. Flash Comes with on-chip loader, loading program to on-chip flash via PC COM port
  55. 55. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 17 HANEL OVERVIEW OF 8051 FAMILY Various 8051 Microcontrollers (cont’) DS5000 from Dallas Semiconductor NV-RAM (changed one byte at a time), RTC (real-time clock) Also comes with on-chip loader OTP (one-time-programmable) version of 8051 8051 family from Philips ADC, DAC, extended I/O, and both OTP and flash
  56. 56. Home Automation, Networking, and Entertainment Lab Dept. of Computer Science and Information Engineering National Cheng Kung University, TAIWAN Chung-Ping Young 楊中平 8051 ASSEMBLY LANGUAGE PROGRAMMING The 8051 Microcontroller and Embedded Systems: Using Assembly and C Mazidi, Mazidi and McKinlay
  57. 57. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 2 HANEL INSIDE THE 8051 Registers Register are used to store information temporarily, while the information could be a byte of data to be processed, or an address pointing to the data to be fetched The vast majority of 8051 register are 8-bit registers There is only one data type, 8 bits
  58. 58. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 3 HANEL INSIDE THE 8051 Registers (cont’) The 8 bits of a register are shown from MSB D7 to the LSB D0 With an 8-bit data type, any data larger than 8 bits must be broken into 8-bit chunks before it is processed D0D1D2D3D4D5D6D7 8 bit Registers most significant bit least significant bit
  59. 59. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 4 HANEL INSIDE THE 8051 Registers (cont’) The most widely used registers A (Accumulator) For all arithmetic and logic instructions B, R0, R1, R2, R3, R4, R5, R6, R7 DPTR (data pointer), and PC (program counter) R6 R5 R4 R3 R2 R1 R0 B A R7 DPTR PC PC (Program counter) DPH DPL
  60. 60. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 5 HANEL INSIDE THE 8051 MOV Instruction MOV destination, source ;copy source to dest. The instruction tells the CPU to move (in reality, COPY) the source operand to the destination operand MOV A,#55H ;load value 55H into reg. A MOV R0,A ;copy contents of A into R0 ;(now A=R0=55H) MOV R1,A ;copy contents of A into R1 ;(now A=R0=R1=55H) MOV R2,A ;copy contents of A into R2 ;(now A=R0=R1=R2=55H) MOV R3,#95H ;load value 95H into R3 ;(now R3=95H) MOV A,R3 ;copy contents of R3 into A ;now A=R3=95H “#” signifies that it is a value
  61. 61. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 6 HANEL INSIDE THE 8051 MOV Instruction (cont’) Notes on programming Value (proceeded with #) can be loaded directly to registers A, B, or R0 – R7 MOV A, #23H MOV R5, #0F9H If values 0 to F moved into an 8-bit register, the rest of the bits are assumed all zeros “MOV A, #5”, the result will be A=05; i.e., A = 00000101 in binary Moving a value that is too large into a register will cause an error MOV A, #7F2H ; ILLEGAL: 7F2H>8 bits (FFH) If it’s not preceded with #, it means to load from a memory locationAdd a 0 to indicate that F is a hex number and not a letter
  62. 62. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 7 HANEL INSIDE THE 8051 ADD Instruction ADD A, source ;ADD the source operand ;to the accumulator The ADD instruction tells the CPU to add the source byte to register A and put the result in register A Source operand can be either a register or immediate data, but the destination must always be register A “ADD R4, A” and “ADD R2, #12H” are invalid since A must be the destination of any arithmetic operation MOV A, #25H ;load 25H into A MOV R2, #34H ;load 34H into R2 ADD A, R2 ;add R2 to Accumulator ;(A = A + R2) MOV A, #25H ;load one operand ;into A (A=25H) ADD A, #34H ;add the second ;operand 34H to A There are always many ways to write the same program, depending on the registers used
  63. 63. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 8 HANEL Structure of Assembly Language In the early days of the computer, programmers coded in machine language, consisting of 0s and 1s Tedious, slow and prone to error Assembly languages, which provided mnemonics for the machine code instructions, plus other features, were developed An Assembly language program consist of a series of lines of Assembly language instructions Assembly language is referred to as a low- level language It deals directly with the internal structure of the CPU 8051 ASSEMBLY PROGRAMMING
  64. 64. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 9 HANEL Structure of Assembly Language 8051 ASSEMBLY PROGRAMMING Assembly language instruction includes a mnemonic (abbreviation easy to remember) the commands to the CPU, telling it what those to do with those items optionally followed by one or two operands the data items being manipulated A given Assembly language program is a series of statements, or lines Assembly language instructions Tell the CPU what to do Directives (or pseudo-instructions) Give directions to the assembler
  65. 65. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 10 HANEL Structure of Assembly Language ORG 0H ;start(origin) at location 0 MOV R5, #25H ;load 25H into R5 MOV R7, #34H ;load 34H into R7 MOV A, #0 ;load 0 into A ADD A, R5 ;add contents of R5 to A ;now A = A + R5 ADD A, R7 ;add contents of R7 to A ;now A = A + R7 ADD A, #12H ;add to A value 12H ;now A = A + 12H HERE: SJMP HERE ;stay in this loop END ;end of asm source file 8051 ASSEMBLY PROGRAMMING An Assembly language instruction consists of four fields: [label:] Mnemonic [operands] [;comment] Mnemonics produce opcodes The label field allows the program to refer to a line of code by name Comments may be at the end of a line or on a line by themselves The assembler ignores comments Directives do not generate any machine code and are used only by the assembler
  66. 66. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 11 HANEL ASSEMBLING AND RUNNING AN 8051 PROGRAM The step of Assembly language program are outlines as follows: 1) First we use an editor to type a program, many excellent editors or word processors are available that can be used to create and/or edit the program Notice that the editor must be able to produce an ASCII file For many assemblers, the file names follow the usual DOS conventions, but the source file has the extension “asm“ or “src”, depending on which assembly you are using
  67. 67. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 12 HANEL ASSEMBLING AND RUNNING AN 8051 PROGRAM (cont’) 2) The “asm” source file containing the program code created in step 1 is fed to an 8051 assembler The assembler converts the instructions into machine code The assembler will produce an object file and a list file The extension for the object file is “obj” while the extension for the list file is “lst” 3) Assembler require a third step called linking The linker program takes one or more object code files and produce an absolute object file with the extension “abs” This abs file is used by 8051 trainers that have a monitor program
  68. 68. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 13 HANEL ASSEMBLING AND RUNNING AN 8051 PROGRAM (cont’) 4) Next the “abs” file is fed into a program called “OH” (object to hex converter) which creates a file with extension “hex” that is ready to burn into ROM This program comes with all 8051 assemblers Recent Windows-based assemblers combine step 2 through 4 into one step
  69. 69. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 14 HANEL ASSEMBLING AND RUNNING AN 8051 PROGRAM Steps to Create a Program EDITOR PROGRAM ASSEMBLER PROGRAM LINKER PROGRAM OH PROGRAM myfile.asm myfile.obj myfile.abs myfile.lst Other obj files myfile.hex
  70. 70. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 15 HANEL ASSEMBLING AND RUNNING AN 8051 PROGRAM lst File The lst (list) file, which is optional, is very useful to the programmer It lists all the opcodes and addresses as well as errors that the assembler detected The programmer uses the lst file to find the syntax errors or debug 1 0000 ORG 0H ;start (origin) at 0 2 0000 7D25 MOV R5,#25H ;load 25H into R5 3 0002 7F34 MOV R7,#34H ;load 34H into R7 4 0004 7400 MOV A,#0 ;load 0 into A 5 0006 2D ADD A,R5 ;add contents of R5 to A ;now A = A + R5 6 0007 2F ADD A,R7 ;add contents of R7 to A ;now A = A + R7 7 0008 2412 ADD A,#12H ;add to A value 12H ;now A = A + 12H 8 000A 80EF HERE: SJMP HERE;stay in this loop 9 000C END ;end of asm source file address
  71. 71. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 16 HANEL PROGRAM COUNTER AND ROM SPACE Program Counter The program counter points to the address of the next instruction to be executed As the CPU fetches the opcode from the program ROM, the program counter is increasing to point to the next instruction The program counter is 16 bits wide This means that it can access program addresses 0000 to FFFFH, a total of 64K bytes of code
  72. 72. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 17 HANEL PROGRAM COUNTER AND ROM SPACE Power up All 8051 members start at memory address 0000 when they’re powered up Program Counter has the value of 0000 The first opcode is burned into ROM address 0000H, since this is where the 8051 looks for the first instruction when it is booted We achieve this by the ORG statement in the source program
  73. 73. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 18 HANEL PROGRAM COUNTER AND ROM SPACE Placing Code in ROM Examine the list file and how the code is placed in ROM 1 0000 ORG 0H ;start (origin) at 0 2 0000 7D25 MOV R5,#25H ;load 25H into R5 3 0002 7F34 MOV R7,#34H ;load 34H into R7 4 0004 7400 MOV A,#0 ;load 0 into A 5 0006 2D ADD A,R5 ;add contents of R5 to A ;now A = A + R5 6 0007 2F ADD A,R7 ;add contents of R7 to A ;now A = A + R7 7 0008 2412 ADD A,#12H ;add to A value 12H ;now A = A + 12H 8 000A 80EF HERE: SJMP HERE ;stay in this loop 9 000C END ;end of asm source file HERE: SJMP HERE80EF000A ADD A, #12H24120008 ADD A, R72F0007 ADD A, R52D0006 MOV A, #074000004 MOV R7, #34H7F340002 MOV R5, #25H7D250000 Assembly LanguageMachine LanguageROM Address
  74. 74. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 19 HANEL PROGRAM COUNTER AND ROM SPACE Placing Code in ROM (cont’) After the program is burned into ROM, the opcode and operand are placed in ROM memory location starting at 0000 FE000B 80000A 120009 240008 2F0007 2D0006 000005 740004 340003 7F0002 250001 7D0000 CodeAddress ROM contents
  75. 75. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 20 HANEL PROGRAM COUNTER AND ROM SPACE Executing Program A step-by-step description of the action of the 8051 upon applying power on it 1. When 8051 is powered up, the PC has 0000 and starts to fetch the first opcode from location 0000 of program ROM Upon executing the opcode 7D, the CPU fetches the value 25 and places it in R5 Now one instruction is finished, and then the PC is incremented to point to 0002, containing opcode 7F 2. Upon executing the opcode 7F, the value 34H is moved into R7 The PC is incremented to 0004
  76. 76. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 21 HANEL PROGRAM COUNTER AND ROM SPACE Executing Program (cont’) (cont’) 3. The instruction at location 0004 is executed and now PC = 0006 4. After the execution of the 1-byte instruction at location 0006, PC = 0007 5. Upon execution of this 1-byte instruction at 0007, PC is incremented to 0008 This process goes on until all the instructions are fetched and executed The fact that program counter points at the next instruction to be executed explains some microprocessors call it the instruction pointer
  77. 77. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 22 HANEL PROGRAM COUNTER AND ROM SPACE ROM Memory Map in 8051 Family No member of 8051 family can access more than 64K bytes of opcode The program counter is a 16-bit register Byte Byte Byte 0000 0FFF 0000 0000 3FFF 7FFF 8751 AT89C51 DS89C420/30 DS5000-32
  78. 78. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 23 HANEL 8051 DATA TYPES AND DIRECTIVES Data Type 8051 microcontroller has only one data type - 8 bits The size of each register is also 8 bits It is the job of the programmer to break down data larger than 8 bits (00 to FFH, or 0 to 255 in decimal) The data types can be positive or negative
  79. 79. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 24 HANEL 8051 DATA TYPES AND DIRECTIVES Assembler Directives The DB directive is the most widely used data directive in the assembler It is used to define the 8-bit data When DB is used to define data, the numbers can be in decimal, binary, hex, ASCII formats ORG 500H DATA1: DB 28 ;DECIMAL (1C in Hex) DATA2: DB 00110101B ;BINARY (35 in Hex) DATA3: DB 39H ;HEX ORG 510H DATA4: DB “2591” ;ASCII NUMBERS ORG 518H DATA6: DB “My name is Joe” ;ASCII CHARACTERS The “D” after the decimal number is optional, but using “B” (binary) and “H” (hexadecimal) for the others is required The Assembler will convert the numbers into hex Place ASCII in quotation marks The Assembler will assign ASCII code for the numbers or characters Define ASCII strings larger than two characters
  80. 80. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 25 HANEL 8051 DATA TYPES AND DIRECTIVES Assembler Directives (cont’) ORG (origin) The ORG directive is used to indicate the beginning of the address The number that comes after ORG can be either in hex and decimal If the number is not followed by H, it is decimal and the assembler will convert it to hex END This indicates to the assembler the end of the source (asm) file The END directive is the last line of an 8051 program Mean that in the code anything after the END directive is ignored by the assembler
  81. 81. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 26 HANEL 8051 DATA TYPES AND DIRECTIVES Assembler directives (cont’) EQU (equate) This is used to define a constant without occupying a memory location The EQU directive does not set aside storage for a data item but associates a constant value with a data label When the label appears in the program, its constant value will be substituted for the label
  82. 82. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 27 HANEL 8051 DATA TYPES AND DIRECTIVES Assembler directives (cont’) EQU (equate) (cont’) Assume that there is a constant used in many different places in the program, and the programmer wants to change its value throughout By the use of EQU, one can change it once and the assembler will change all of its occurrences COUNT EQU 25 ... .... MOV R3, #COUNT Use EQU for the counter constant The constant is used to load the R3 register
  83. 83. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 28 HANEL FLAG BITS AND PSW REGISTER Program Status Word The program status word (PSW) register, also referred to as the flag register, is an 8 bit register Only 6 bits are used These four are CY (carry), AC (auxiliary carry), P (parity), and OV (overflow) – They are called conditional flags, meaning that they indicate some conditions that resulted after an instruction was executed The PSW3 and PSW4 are designed as RS0 and RS1, and are used to change the bank The two unused bits are user-definable
  84. 84. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 29 HANEL FLAG BITS AND PSW REGISTER Program Status Word (cont’) P--OVRS0RS1F0ACCY CY PSW.7 Carry flag. AC PSW.6 Auxiliary carry flag. -- PSW.5 Available to the user for general purpose RS1 PSW.4 Register Bank selector bit 1. RS0 PSW.3 Register Bank selector bit 0. OV PSW.2 Overflow flag. -- PSW.1 User definable bit. P PSW.0 Parity flag. Set/cleared by hardware each instruction cycle to indicate an odd/even number of 1 bits in the accumulator. 18H – 1FH311 10H – 17H201 08H – 0FH110 00H – 07H000 AddressRegister BankRS0RS1 Carry out from the d7 bit A carry from D3 to D4 Reflect the number of 1s in register AThe result of signed number operation is too large, causing the high-order bit to overflow into the sign bit
  85. 85. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 30 HANEL FLAG BITS AND PSW REGISTER ADD Instruction And PSW XCJNE XMOV C, bit XORL C, /bit XORL C, bit XANL C, /bit XANL C, bit XCPL C 0CLR C 1SETB C XPLC XRPC XDA X0DIV X0MUL XXXSUBB XXXADDC XXXADD ACOVCYInstruction Instructions that affect flag bits
  86. 86. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 31 HANEL FLAG BITS AND PSW REGISTER ADD Instruction And PSW (cont’) Example 2-2 Show the status of the CY, AC and P flag after the addition of 38H and 2FH in the following instructions. MOV A, #38H ADD A, #2FH ;after the addition A=67H, CY=0 Solution: 38 00111000 + 2F 00101111 67 01100111 CY = 0 since there is no carry beyond the D7 bit AC = 1 since there is a carry from the D3 to the D4 bi P = 1 since the accumulator has an odd number of 1s (it has five 1s) The flag bits affected by the ADD instruction are CY, P, AC, and OV
  87. 87. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 32 HANEL FLAG BITS AND PSW REGISTER ADD Instruction And PSW (cont’) Example 2-3 Show the status of the CY, AC and P flag after the addition of 9CH and 64H in the following instructions. MOV A, #9CH ADD A, #64H ;after the addition A=00H, CY=1 Solution: 9C 10011100 + 64 01100100 100 00000000 CY = 1 since there is a carry beyond the D7 bit AC = 1 since there is a carry from the D3 to the D4 bi P = 0 since the accumulator has an even number of 1s (it has zero 1s)
  88. 88. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 33 HANEL FLAG BITS AND PSW REGISTER ADD Instruction And PSW (cont’) Example 2-4 Show the status of the CY, AC and P flag after the addition of 88H and 93H in the following instructions. MOV A, #88H ADD A, #93H ;after the addition A=1BH, CY=1 Solution: 88 10001000 + 93 10010011 11B 00011011 CY = 1 since there is a carry beyond the D7 bit AC = 0 since there is no carry from the D3 to the D4 bi P = 0 since the accumulator has an even number of 1s (it has four 1s)
  89. 89. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 34 HANEL REGISTER BANKS AND STACK RAM Memory Space Allocation There are 128 bytes of RAM in the 8051 Assigned addresses 00 to 7FH The 128 bytes are divided into three different groups as follows: 1) A total of 32 bytes from locations 00 to 1F hex are set aside for register banks and the stack 2) A total of 16 bytes from locations 20H to 2FH are set aside for bit-addressable read/write memory 3) A total of 80 bytes from locations 30H to 7FH are used for read and write storage, called scratch pad
  90. 90. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 35 HANEL 8051 REGISTER BANKS AND STACK RAM Memory Space Allocation (cont’) Scratch pad RAM Bit-Addressable RAM Register Bank 3 Register Bank 2 Register Bank 1 (stack) Register Bank 0 00 07 08 0F 10 17 18 1F 20 2F 30 7F RAM Allocation in 8051
  91. 91. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 36 HANEL 8051 REGISTER BANKS AND STACK Register Banks These 32 bytes are divided into 4 banks of registers in which each bank has 8 registers, R0-R7 RAM location from 0 to 7 are set aside for bank 0 of R0-R7 where R0 is RAM location 0, R1 is RAM location 1, R2 is RAM location 2, and so on, until memory location 7 which belongs to R7 of bank 0 It is much easier to refer to these RAM locations with names such as R0, R1, and so on, than by their memory locations Register bank 0 is the default when 8051 is powered up
  92. 92. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 37 HANEL 8051 REGISTER BANKS AND STACK Register Banks (cont’) R7 R6 R5 R4 R3 R2 R1 R0 Bank 0 Bank 1 Bank 2 Bank 3 7 6 5 4 3 2 0 1 F E D C B A 8 9 1F 1E 1D 1C 1B 1A 18 19 17 16 15 14 13 12 10 11 R7 R6 R5 R4 R3 R2 R1 R0 R7 R6 R5 R4 R3 R2 R1 R0 R7 R6 R5 R4 R3 R2 R1 R0 Register banks and their RAM address
  93. 93. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 38 HANEL 8051 REGISTER BANKS AND STACK Register Banks (cont’) We can switch to other banks by use of the PSW register Bits D4 and D3 of the PSW are used to select the desired register bank Use the bit-addressable instructions SETB and CLR to access PSW.4 and PSW.3 11Bank 3 01Bank 2 10Bank 1 00Bank 0 RS0(PSW.3)RS1(PSW.4) PSW bank selection
  94. 94. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 39 HANEL 8051 REGISTER BANKS AND STACK Register Banks (cont’) Example 2-5 MOV R0, #99H ;load R0 with 99H MOV R1, #85H ;load R1 with 85H Example 2-6 MOV 00, #99H ;RAM location 00H has 99H MOV 01, #85H ;RAM location 01H has 85H Example 2-7 SETB PSW.4 ;select bank 2 MOV R0, #99H ;RAM location 10H has 99H MOV R1, #85H ;RAM location 11H has 85H
  95. 95. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 40 HANEL 8051 REGISTER BANKS AND STACK Stack The stack is a section of RAM used by the CPU to store information temporarily This information could be data or an address The register used to access the stack is called the SP (stack pointer) register The stack pointer in the 8051 is only 8 bit wide, which means that it can take value of 00 to FFH When the 8051 is powered up, the SP register contains value 07 RAM location 08 is the first location begin used for the stack by the 8051
  96. 96. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 41 HANEL 8051 REGISTER BANKS AND STACK Stack (cont’) The storing of a CPU register in the stack is called a PUSH SP is pointing to the last used location of the stack As we push data onto the stack, the SP is incremented by one This is different from many microprocessors Loading the contents of the stack back into a CPU register is called a POP With every pop, the top byte of the stack is copied to the register specified by the instruction and the stack pointer is decremented once
  97. 97. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 42 HANEL 8051 REGISTER BANKS AND STACK Pushing onto Stack Example 2-8 Show the stack and stack pointer from the following. Assume the default stack area. MOV R6, #25H MOV R1, #12H MOV R4, #0F3H PUSH 6 PUSH 1 PUSH 4 Solution: 25 12 F3 After PUSH 4 SP = 0A 08 09 0A 0B SP = 09SP = 08Start SP = 07 2508250808 12090909 0A0A0A 0B0B0B After PUSH 1After PUSH 6
  98. 98. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 43 HANEL 8051 REGISTER BANKS AND STACK Popping From Stack Example 2-9 Examining the stack, show the contents of the register and SP after execution of the following instructions. All value are in hex. POP 3 ; POP stack into R3 POP 5 ; POP stack into R5 POP 2 ; POP stack into R2 Solution: 6C After POP 2 SP = 08 08 09 0A 0B SP = 09SP = 0AStart SP = 0B 6C086C086C08 760976097609 0AF90AF90A 0B0B540B After POP 5After POP 3 Because locations 20-2FH of RAM are reserved for bit-addressable memory, so we can change the SP to other RAM location by using the instruction “MOV SP, #XX”
  99. 99. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 44 HANEL 8051 REGISTER BANKS AND STACK CALL Instruction And Stack The CPU also uses the stack to save the address of the instruction just below the CALL instruction This is how the CPU knows where to resume when it returns from the called subroutine
  100. 100. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 45 HANEL 8051 REGISTER BANKS AND STACK Incrementing Stack Pointer The reason of incrementing SP after push is Make sure that the stack is growing toward RAM location 7FH, from lower to upper addresses Ensure that the stack will not reach the bottom of RAM and consequently run out of stack space If the stack pointer were decremented after push We would be using RAM locations 7, 6, 5, etc. which belong to R7 to R0 of bank 0, the default register bank
  101. 101. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 46 HANEL 8051 REGISTER BANKS AND STACK Stack and Bank 1 Conflict When 8051 is powered up, register bank 1 and the stack are using the same memory space We can reallocate another section of RAM to the stack
  102. 102. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 47 HANEL 8051 REGISTER BANKS AND STACK Stack And Bank 1 Conflict (cont’) Example 2-10 Examining the stack, show the contents of the register and SP after execution of the following instructions. All value are in hex. MOV SP, #5FH ;make RAM location 60H ;first stack location MOV R2, #25H MOV R1, #12H MOV R4, #0F3H PUSH 2 PUSH 1 PUSH 4 Solution: 25 12 F3 After PUSH 4 SP = 62 60 61 62 63 SP = 61SP = 60Start SP = 5F 2560256060 12616161 626262 636363 After PUSH 1After PUSH 2
  103. 103. Home Automation, Networking, and Entertainment Lab Dept. of Computer Science and Information Engineering National Cheng Kung University, TAIWAN Chung-Ping Young 楊中平 JUMP, LOOP AND CALL INSTRUCTIONS The 8051 Microcontroller and Embedded Systems: Using Assembly and C Mazidi, Mazidi and McKinlay
  104. 104. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 2 HANEL LOOP AND JUMP INSTRUCTIONS Looping Repeating a sequence of instructions a certain number of times is called a loop Loop action is performed by DJNZ reg, Label The register is decremented If it is not zero, it jumps to the target address referred to by the label Prior to the start of loop the register is loaded with the counter for the number of repetitions Counter can be R0 – R7 or RAM location ;This program adds value 3 to the ACC ten times MOV A,#0 ;A=0, clear ACC MOV R2,#10 ;load counter R2=10 AGAIN: ADD A,#03 ;add 03 to ACC DJNZ R2,AGAIN ;repeat until R2=0,10 times MOV R5,A ;save A in R5 A loop can be repeated a maximum of 255 times, if R2 is FFH
  105. 105. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 3 HANEL LOOP AND JUMP INSTRUCTIONS Nested Loop If we want to repeat an action more times than 256, we use a loop inside a loop, which is called nested loop We use multiple registers to hold the count Write a program to (a) load the accumulator with the value 55H, and (b) complement the ACC 700 times MOV A,#55H ;A=55H MOV R3,#10 ;R3=10, outer loop count NEXT: MOV R2,#70 ;R2=70, inner loop count AGAIN: CPL A ;complement A register DJNZ R2,AGAIN ;repeat it 70 times DJNZ R3,NEXT
  106. 106. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 4 HANEL LOOP AND JUMP INSTRUCTIONS Conditional Jumps MOV A,R0 ;A=R0 JZ OVER ;jump if A = 0 MOV A,R1 ;A=R1 JZ OVER ;jump if A = 0 ... OVER: Jump only if a certain condition is met JZ label ;jump if A=0 Determine if R5 contains the value 0. If so, put 55H in it. MOV A,R5 ;copy R5 to A JNZ NEXT ;jump if A is not zero MOV R5,#55H NEXT: ... Can be used only for register A, not any other register
  107. 107. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 5 HANEL LOOP AND JUMP INSTRUCTIONS Conditional Jumps (cont’) (cont’) JNC label ;jump if no carry, CY=0 If CY = 0, the CPU starts to fetch and execute instruction from the address of the label If CY = 1, it will not jump but will execute the next instruction below JNC Find the sum of the values 79H, F5H, E2H. Put the sum in registers R0 (low byte) and R5 (high byte). MOV A,#0 ;A=0 MOV R5,A ;clear R5 ADD A,#79H ;A=0+79H=79H ; JNC N_1 ;if CY=0, add next number ; INC R5 ;if CY=1, increment R5 N_1: ADD A,#0F5H ;A=79+F5=6E and CY=1 JNC N_2 ;jump if CY=0 INC R5 ;if CY=1,increment R5 (R5=1) N_2: ADD A,#0E2H ;A=6E+E2=50 and CY=1 JNC OVER ;jump if CY=0 INC R5 ;if CY=1, increment 5 OVER: MOV R0,A ;now R0=50H, and R5=02 MOV R5,#0
  108. 108. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 6 HANEL LOOP AND JUMP INSTRUCTIONS Conditional Jumps (cont’) All conditional jumps are short jumps The address of the target must within -128 to +127 bytes of the contents of PC Jump if bit = 1 and clear bitJBC Jump if bit = 0JNB Jump if bit = 1JB Jump if CY = 0JNC Jump if CY = 1JC Jump if byte ≠ #dataCJNE reg,#data Jump if A ≠ byteCJNE A,byte Decrement and Jump if A ≠ 0DJNZ Jump if A ≠ 0JNZ Jump if A = 0JZ ActionsInstructions 8051 conditional jump instructions
  109. 109. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 7 HANEL LOOP AND JUMP INSTRUCTIONS Unconditional Jumps The unconditional jump is a jump in which control is transferred unconditionally to the target location LJMP (long jump) 3-byte instruction First byte is the opcode Second and third bytes represent the 16-bit target address – Any memory location from 0000 to FFFFH SJMP (short jump) 2-byte instruction First byte is the opcode Second byte is the relative target address – 00 to FFH (forward +127 and backward -128 bytes from the current PC)
  110. 110. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 8 HANEL LOOP AND JUMP INSTRUCTIONS Calculating Short Jump Address To calculate the target address of a short jump (SJMP, JNC, JZ, DJNZ, etc.) The second byte is added to the PC of the instruction immediately below the jump If the target address is more than -128 to +127 bytes from the address below the short jump instruction The assembler will generate an error stating the jump is out of range
  111. 111. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 9 HANEL LOOP AND JUMP INSTRUCTIONS Calculating Short Jump Address (cont’) Line PC Opcode Mnemonic Operand 01 0000 ORG 0000 02 0000 7800 MOV R0,#0 03 0002 7455 MOV A,#55H 04 0004 6003 JZ NEXT 05 0006 08 INC R0 06 0007 04 AGAIN: INC A 07 0008 04 INC A 08 0009 2477 NEXT: ADD A,#77H 09 000B 5005 JNC OVER 10 000D E4 CLR A 11 000E F8 MOV R0,A 12 000F F9 MOV R1,A 13 0010 FA MOV R2,A 14 0011 FB MOV R3,A 15 0012 2B OVER: ADD A,R3 16 0013 50F2 JNC AGAIN 17 0015 80FE HERE: SJMP HERE 18 0017 END + + +
  112. 112. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 10 HANEL CALL INSTRUCTIONS Call instruction is used to call subroutine Subroutines are often used to perform tasks that need to be performed frequently This makes a program more structured in addition to saving memory space LCALL (long call) 3-byte instruction First byte is the opcode Second and third bytes are used for address of target subroutine – Subroutine is located anywhere within 64K byte address space ACALL (absolute call) 2-byte instruction 11 bits are used for address within 2K-byte range
  113. 113. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 11 HANEL CALL INSTRUCTIONS LCALL When a subroutine is called, control is transferred to that subroutine, the processor Saves on the stack the the address of the instruction immediately below the LCALL Begins to fetch instructions form the new location After finishing execution of the subroutine The instruction RET transfers control back to the caller Every subroutine needs RET as the last instruction
  114. 114. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 12 HANEL CALL INSTRUCTIONS LCALL (cont’) ORG 0 BACK: MOV A,#55H ;load A with 55H MOV P1,A ;send 55H to port 1 LCALL DELAY ;time delay MOV A,#0AAH ;load A with AA (in hex) MOV P1,A ;send AAH to port 1 LCALL DELAY SJMP BACK ;keep doing this indefinitely ;---------- this is delay subroutine ------------ ORG 300H ;put DELAY at address 300H DELAY: MOV R5,#0FFH ;R5=255 (FF in hex), counter AGAIN: DJNZ R5,AGAIN ;stay here until R5 become 0 RET ;return to caller (when R5 =0) END ;end of asm file Upon executing “LCALL DELAY”, the address of instruction below it, “MOV A,#0AAH” is pushed onto stack, and the 8051 starts to execute at 300H. The counter R5 is set to FFH; so loop is repeated 255 times. When R5 becomes 0, control falls to the RET which pops the address from the stack into the PC and resumes executing the instructions after the CALL. The amount of time delay depends on the frequency of the 8051
  115. 115. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 13 HANEL CALL INSTRUCTIONS CALL Instruction and Stack 001 0000 ORG 0 002 0000 7455 BACK: MOV A,#55H ;load A with 55H 003 0002 F590 MOV P1,A ;send 55H to p1 004 0004 120300 LCALL DELAY ;time delay 005 0007 74AA MOV A,#0AAH ;load A with AAH 006 0009 F590 MOV P1,A ;send AAH to p1 007 000B 120300 LCALL DELAY 008 000E 80F0 SJMP BACK ;keep doing this 009 0010 010 0010 ;-------this is the delay subroutine------ 011 0300 ORG 300H 012 0300 DELAY: 013 0300 7DFF MOV R5,#0FFH ;R5=255 014 0302 DDFE AGAIN: DJNZ R5,AGAIN ;stay here 015 0304 22 RET ;return to caller 016 0305 END ;end of asm file 0708 SP = 09 0009 0A Stack frame after the first LCALL Low byte goes first and high byte is last
  116. 116. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 14 HANEL CALL INSTRUCTIONS Use PUSH/POP in Subroutine 01 0000 ORG 0 02 0000 7455 BACK: MOV A,#55H ;load A with 55H 03 0002 F590 MOV P1,A ;send 55H to p1 04 0004 7C99 MOV R4,#99H 05 0006 7D67 MOV R5,#67H 06 0008 120300 LCALL DELAY ;time delay 07 000B 74AA MOV A,#0AAH ;load A with AA 08 000D F590 MOV P1,A ;send AAH to p1 09 000F 120300 LCALL DELAY 10 0012 80EC SJMP BACK ;keeping doing this 11 0014 ;-------this is the delay subroutine------ 12 0300 ORG 300H 13 0300 C004 DELAY: PUSH 4 ;push R4 14 0302 C005 PUSH 5 ;push R5 15 0304 7CFF MOV R4,#0FFH;R4=FFH 16 0306 7DFF NEXT: MOV R5,#0FFH;R5=FFH 17 0308 DDFE AGAIN: DJNZ R5,AGAIN 18 030A DCFA DJNZ R4,NEXT 19 030C D005 POP 5 ;POP into R5 20 030E D004 POP 4 ;POP into R4 21 0310 22 RET ;return to caller 22 0311 END ;end of asm file Normally, the number of PUSH and POP instructions must always match in any called subroutine PCL0B08PCL0B08PCL0B08 PCH0009PCH0009PCH0009 R4990AR4990A0A R5670B0B0B After PUSH 5After PUSH 4After first LCALL
  117. 117. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 15 HANEL CALL INSTRUCTIONS Calling Subroutines ;MAIN program calling subroutines ORG 0 MAIN: LCALL SUBR_1 LCALL SUBR_2 LCALL SUBR_3 HERE: SJMP HERE ;-----------end of MAIN SUBR_1: ... ... RET ;-----------end of subroutine1 SUBR_2: ... ... RET ;-----------end of subroutine2 SUBR_3: ... ... RET ;-----------end of subroutine3 END ;end of the asm file It is common to have one main program and many subroutines that are called from the main program This allows you to make each subroutine into a separate module - Each module can be tested separately and then brought together with main program - In a large program, the module can be assigned to different programmers
  118. 118. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 16 HANEL CALL INSTRUCTIONS ACALL The only difference between ACALL and LCALL is The target address for LCALL can be anywhere within the 64K byte address The target address of ACALL must be within a 2K-byte range The use of ACALL instead of LCALL can save a number of bytes of program ROM space
  119. 119. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 17 HANEL CALL INSTRUCTIONS ACALL (cont’) ORG 0 MOV A,#55H ;load A with 55H BACK: MOV P1,A ;send 55H to port 1 ACALL DELAY ;time delay CPL A ;complement reg A SJMP BACK ;keep doing this indefinitely ... END ;end of asm file ORG 0 BACK: MOV A,#55H ;load A with 55H MOV P1,A ;send 55H to port 1 LCALL DELAY ;time delay MOV A,#0AAH ;load A with AA (in hex) MOV P1,A ;send AAH to port 1 LCALL DELAY SJMP BACK ;keep doing this indefinitely ... END ;end of asm file A rewritten program which is more efficiently
  120. 120. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 18 HANEL TIME DELAY FOR VARIOUS 8051 CHIPS CPU executing an instruction takes a certain number of clock cycles These are referred as to as machine cycles The length of machine cycle depends on the frequency of the crystal oscillator connected to 8051 In original 8051, one machine cycle lasts 12 oscillator periods Find the period of the machine cycle for 11.0592 MHz crystal frequency Solution: 11.0592/12 = 921.6 kHz; machine cycle is 1/921.6 kHz = 1.085μs
  121. 121. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 19 HANEL TIME DELAY FOR VARIOUS 8051 CHIPS (cont’) For 8051 system of 11.0592 MHz, find how long it takes to execute each instruction. (a) MOV R3,#55 (b) DEC R3 (c) DJNZ R2 target (d) LJMP (e) SJMP (f) NOP (g) MUL AB Solution: Machine cycles Time to execute (a) 1 1x1.085μs = 1.085μs (b) 1 1x1.085μs = 1.085μs (c) 2 2x1.085μs = 2.17μs (d) 2 2x1.085μs = 2.17μs (e) 2 2x1.085μs = 2.17μs (f) 1 1x1.085μs = 1.085μs (g) 4 4x1.085μs = 4.34μs
  122. 122. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 20 HANEL TIME DELAY FOR VARIOUS 8051 CHIPS Delay Calculation Find the size of the delay in following program, if the crystal frequency is 11.0592MHz. MOV A,#55H AGAIN: MOV P1,A ACALL DELAY CPL A SJMP AGAIN ;---time delay------- DELAY: MOV R3,#200 HERE: DJNZ R3,HERE RET Solution: Machine cycle DELAY: MOV R3,#200 1 HERE: DJNZ R3,HERE 2 RET 2 Therefore, [(200x2)+1+2]x1.085μs = 436.255μs. A simple way to short jump to itself in order to keep the microcontroller busy HERE: SJMP HERE We can use the following: SJMP $
  123. 123. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 21 HANEL TIME DELAY FOR VARIOUS 8051 CHIPS Increasing Delay Using NOP Find the size of the delay in following program, if the crystal frequency is 11.0592MHz. Machine Cycle DELAY: MOV R3,#250 1 HERE: NOP 1 NOP 1 NOP 1 NOP 1 DJNZ R3,HERE 2 RET 2 Solution: The time delay inside HERE loop is [250(1+1+1+1+2)]x1.085μs = 1627.5μs. Adding the two instructions outside loop we have 1627.5μs + 3 x 1.085μs = 1630.755μs
  124. 124. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 22 HANEL TIME DELAY FOR VARIOUS 8051 CHIPS Large Delay Using Nested Loop Find the size of the delay in following program, if the crystal frequency is 11.0592MHz. Machine Cycle DELAY: MOV R2,#200 1 AGAIN: MOV R3,#250 1 HERE: NOP 1 NOP 1 DJNZ R3,HERE 2 DJNZ R2,AGAIN 2 RET 2 Solution: For HERE loop, we have (4x250)x1.085μs=1085μs. For AGAIN loop repeats HERE loop 200 times, so we have 200x1085μs=217000μs. But “MOV R3,#250” and “DJNZ R2,AGAIN” at the start and end of the AGAIN loop add (3x200x1.805)=651μs. As a result we have 217000+651=217651μs. Notice in nested loop, as in all other time delay loops, the time is approximate since we have ignored the first and last instructions in the subroutine.
  125. 125. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 23 HANEL TIME DELAY FOR VARIOUS 8051 CHIPS Delay Calculation for Other 8051 Two factors can affect the accuracy of the delay Crystal frequency The duration of the clock period of the machine cycle is a function of this crystal frequency 8051 design The original machine cycle duration was set at 12 clocks Advances in both IC technology and CPU design in recent years have made the 1-clock machine cycle a common feature 1DS89C420/30/40/50 Dallas Semi 4DS5000 Dallas Semi 6P89C54X2 Philips 12AT89C51 Atmel Clocks per Machine CycleChip/Maker Clocks per machine cycle for various 8051 versions
  126. 126. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 24 HANEL TIME DELAY FOR VARIOUS 8051 CHIPS Delay Calculation for Other 8051 (cont’) Find the period of the machine cycle (MC) for various versions of 8051, if XTAL=11.0592 MHz. (a) AT89C51 (b) P89C54X2 (c) DS5000 (d) DS89C4x0 Solution: (a) 11.0592MHz/12 = 921.6kHz; MC is 1/921.6kHz = 1.085μs = 1085ns (b) 11.0592MHz/6 = 1.8432MHz; MC is 1/1.8432MHz = 0.5425μs = 542ns (c) 11.0592MHz/4 = 2.7648MHz ; MC is 1/2.7648MHz = 0.36μs = 360ns (d) 11.0592MHz/1 = 11.0592MHz; MC is 1/11.0592MHz = 0.0904μs = 90ns
  127. 127. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 25 HANEL TIME DELAY FOR VARIOUS 8051 CHIPS Delay Calculation for Other 8051 (cont’) For an AT8051 and DSC89C4x0 system of 11.0592 MHz, find how long it takes to execute each instruction. (a) MOV R3,#55 (b) DEC R3 (c) DJNZ R2 target (d) LJMP (e) SJMP (f) NOP (g) MUL AB Solution: AT8051 DS89C4x0 (a) 1 1085ns = 1085ns 2 90ns = 180ns (b) 1 1085ns = 1085ns 1 90ns = 90ns (c) 2 1085ns = 2170ns 4 90ns = 360ns (d) 2 1085ns = 2170ns 3 90ns = 270ns (e) 2 1085ns = 2170ns 3 90ns = 270ns (f) 1 1085ns = 1085ns 1 90ns = 90ns (g) 4 1085ns = 4340ns 9 90ns = 810ns 11NOP 32SJMP 32LJMP 4 2 1 1 8051 9MUL AB 4DJNZ R2 target 1DEC R3 2MOV R3,#55 DSC89C4x0Instruction
  128. 128. Home Automation, Networking, and Entertainment Lab Dept. of Computer Science and Information Engineering National Cheng Kung University, TAIWAN Chung-Ping Young 楊中平 I/O PORT PROGRAMMING The 8051 Microcontroller and Embedded Systems: Using Assembly and C Mazidi, Mazidi and McKinlay
  129. 129. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 2 HANEL I/O PROGRAMMING 8051 (8031) (89420) P1.0 P1.1 P1.2 P1.3 P1.4 P1.5 P1.6 P1.7 RST (RXD) P3.0 (TXD) P3.1 (-INT0) P3.2 (-INT1) P3.3 (T0) P3.4 (T1) P3.5 (-WR) P3.6 (-RD )P3.7 XTAL2 XTAL1 GND Vcc P0.0 (AD0) P0.1 (AD1) P0.2 (AD2) P0.3 (AD3) P0.4 (AD4) P0.5 (AD5) P0.6 (AD6) P0.7 (AD7) -EA/VPP ALE/PROG -PSEN P2.7 (A15) P2.6 (A14) P2.5 (A13) P2.4 (A12) P2.3 (A11) P2.2 (A10) P2.1 (A9) P2.0 (A8) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 Provides +5V supply voltage to the chip Grond P1 P3 8051 Pin Diagram A total of 32 pins are set aside for the four ports P0, P1, P2, P3, where each port takes 8 pins P0 P2
  130. 130. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 3 HANEL I/O PROGRAMMING I/O Port Pins The four 8-bit I/O ports P0, P1, P2 and P3 each uses 8 pins All the ports upon RESET are configured as input, ready to be used as input ports When the first 0 is written to a port, it becomes an output To reconfigure it as an input, a 1 must be sent to the port To use any of these ports as an input port, it must be programmed P1.0 P1.1 P1.2 P1.3 P1.4 P1.5 P1.6 P1.7 RST (RXD)P3.0 (TXD)P3.1 (INT0)P3.2 (INT1)P3.3 (T0)P3.4 (T1)P3.5 (WR)P3.6 (RD)P3.7 XTAL2 XTAL1 GND Vcc P0.0(AD0) P0.1(AD1) P0.2(AD2) P0.3(AD3) P0.4(AD4) P0.5(AD5) P0.6(AD6) P0.7(AD7) -EA/VPP ALE/PROG -PSEN P2.7(A15) P2.6(A14) P2.5(A13) P2.4(A12) P2.3(A11) P2.2(A10) P2.1(A9) P2.0(A8) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 8051 (8031)
  131. 131. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 4 HANEL I/O PROGRAMMING Port 0 It can be used for input or output, each pin must be connected externally to a 10K ohm pull-up resistor This is due to the fact that P0 is an open drain, unlike P1, P2, and P3 Open drain is a term used for MOS chips in the same way that open collector is used for TTL chips P0.0 P0.1 P0.2 P0.3 P0.4 P0.5 P0.6 P0.7 Vcc 10 K 8051 Port0 P1.0 P1.1 P1.2 P1.3 P1.4 P1.5 P1.6 P1.7 RST (RXD)P3.0 (TXD)P3.1 (INT0)P3.2 (INT1)P3.3 (T0)P3.4 (T1)P3.5 (WR)P3.6 (RD)P3.7 XTAL2 XTAL1 GND Vcc P0.0(AD0) P0.1(AD1) P0.2(AD2) P0.3(AD3) P0.4(AD4) P0.5(AD5) P0.6(AD6) P0.7(AD7) -EA/VPP ALE/PROG -PSEN P2.7(A15) P2.6(A14) P2.5(A13) P2.4(A12) P2.3(A11) P2.2(A10) P2.1(A9) P2.0(A8) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 8051 (8031) P0.X
  132. 132. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 5 HANEL I/O PROGRAMMING Port 0 (cont’) The following code will continuously send out to port 0 the alternating value 55H and AAH BACK: MOV A,#55H MOV P0,A ACALL DELAY MOV A,#0AAH MOV P0,A ACALL DELAY SJMP BACK P1.0 P1.1 P1.2 P1.3 P1.4 P1.5 P1.6 P1.7 RST (RXD)P3.0 (TXD)P3.1 (INT0)P3.2 (INT1)P3.3 (T0)P3.4 (T1)P3.5 (WR)P3.6 (RD)P3.7 XTAL2 XTAL1 GND Vcc P0.0(AD0) P0.1(AD1) P0.2(AD2) P0.3(AD3) P0.4(AD4) P0.5(AD5) P0.6(AD6) P0.7(AD7) -EA/VPP ALE/PROG -PSEN P2.7(A15) P2.6(A14) P2.5(A13) P2.4(A12) P2.3(A11) P2.2(A10) P2.1(A9) P2.0(A8) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 8051 (8031)
  133. 133. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 6 HANEL I/O PROGRAMMING Port 0 as Input In order to make port 0 an input, the port must be programmed by writing 1 to all the bits Port 0 is configured first as an input port by writing 1s to it, and then data is received from that port and sent to P1 MOV A,#0FFH ;A=FF hex MOV P0,A ;make P0 an i/p port ;by writing it all 1s BACK: MOV A,P0 ;get data from P0 MOV P1,A ;send it to port 1 SJMP BACK ;keep doing it P1.0 P1.1 P1.2 P1.3 P1.4 P1.5 P1.6 P1.7 RST (RXD)P3.0 (TXD)P3.1 (INT0)P3.2 (INT1)P3.3 (T0)P3.4 (T1)P3.5 (WR)P3.6 (RD)P3.7 XTAL2 XTAL1 GND Vcc P0.0(AD0) P0.1(AD1) P0.2(AD2) P0.3(AD3) P0.4(AD4) P0.5(AD5) P0.6(AD6) P0.7(AD7) -EA/VPP ALE/PROG -PSEN P2.7(A15) P2.6(A14) P2.5(A13) P2.4(A12) P2.3(A11) P2.2(A10) P2.1(A9) P2.0(A8) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 8051 (8031)
  134. 134. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 7 HANEL I/O PROGRAMMING Dual Role of Port 0 Port 0 is also designated as AD0-AD7, allowing it to be used for both address and data When connecting an 8051/31 to an external memory, port 0 provides both address and data P1.0 P1.1 P1.2 P1.3 P1.4 P1.5 P1.6 P1.7 RST (RXD)P3.0 (TXD)P3.1 (INT0)P3.2 (INT1)P3.3 (T0)P3.4 (T1)P3.5 (WR)P3.6 (RD)P3.7 XTAL2 XTAL1 GND Vcc P0.0(AD0) P0.1(AD1) P0.2(AD2) P0.3(AD3) P0.4(AD4) P0.5(AD5) P0.6(AD6) P0.7(AD7) -EA/VPP ALE/PROG -PSEN P2.7(A15) P2.6(A14) P2.5(A13) P2.4(A12) P2.3(A11) P2.2(A10) P2.1(A9) P2.0(A8) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 8051 (8031)
  135. 135. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 8 HANEL I/O PROGRAMMING Port 1 Port 1 can be used as input or output In contrast to port 0, this port does not need any pull-up resistors since it already has pull-up resistors internally Upon reset, port 1 is configured as an input port P1.0 P1.1 P1.2 P1.3 P1.4 P1.5 P1.6 P1.7 RST (RXD)P3.0 (TXD)P3.1 (INT0)P3.2 (INT1)P3.3 (T0)P3.4 (T1)P3.5 (WR)P3.6 (RD)P3.7 XTAL2 XTAL1 GND Vcc P0.0(AD0) P0.1(AD1) P0.2(AD2) P0.3(AD3) P0.4(AD4) P0.5(AD5) P0.6(AD6) P0.7(AD7) -EA/VPP ALE/PROG -PSEN P2.7(A15) P2.6(A14) P2.5(A13) P2.4(A12) P2.3(A11) P2.2(A10) P2.1(A9) P2.0(A8) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 8051 (8031) The following code will continuously send out to port 0 the alternating value 55H and AAH MOV A,#55H BACK: MOV P1,A ACALL DELAY CPL A SJMP BACK
  136. 136. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 9 HANEL I/O PROGRAMMING Port 1 as Input To make port 1 an input port, it must be programmed as such by writing 1 to all its bits P1.0 P1.1 P1.2 P1.3 P1.4 P1.5 P1.6 P1.7 RST (RXD)P3.0 (TXD)P3.1 (INT0)P3.2 (INT1)P3.3 (T0)P3.4 (T1)P3.5 (WR)P3.6 (RD)P3.7 XTAL2 XTAL1 GND Vcc P0.0(AD0) P0.1(AD1) P0.2(AD2) P0.3(AD3) P0.4(AD4) P0.5(AD5) P0.6(AD6) P0.7(AD7) -EA/VPP ALE/PROG -PSEN P2.7(A15) P2.6(A14) P2.5(A13) P2.4(A12) P2.3(A11) P2.2(A10) P2.1(A9) P2.0(A8) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 8051 (8031) Port 1 is configured first as an input port by writing 1s to it, then data is received from that port and saved in R7 and R5 MOV A,#0FFH ;A=FF hex MOV P1,A ;make P1 an input port ;by writing it all 1s MOV A,P1 ;get data from P1 MOV R7,A ;save it to in reg R7 ACALL DELAY ;wait MOV A,P1 ;another data from P1 MOV R5,A ;save it to in reg R5
  137. 137. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 10 HANEL I/O PROGRAMMING Port 2 Port 2 can be used as input or output Just like P1, port 2 does not need any pull- up resistors since it already has pull-up resistors internally Upon reset, port 2 is configured as an input port P1.0 P1.1 P1.2 P1.3 P1.4 P1.5 P1.6 P1.7 RST (RXD)P3.0 (TXD)P3.1 (INT0)P3.2 (INT1)P3.3 (T0)P3.4 (T1)P3.5 (WR)P3.6 (RD)P3.7 XTAL2 XTAL1 GND Vcc P0.0(AD0) P0.1(AD1) P0.2(AD2) P0.3(AD3) P0.4(AD4) P0.5(AD5) P0.6(AD6) P0.7(AD7) -EA/VPP ALE/PROG -PSEN P2.7(A15) P2.6(A14) P2.5(A13) P2.4(A12) P2.3(A11) P2.2(A10) P2.1(A9) P2.0(A8) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 8051 (8031)
  138. 138. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 11 HANEL I/O PROGRAMMING Port 2 as Input or Dual Role To make port 2 an input port, it must be programmed as such by writing 1 to all its bits In many 8051-based system, P2 is used as simple I/O In 8031-based systems, port 2 must be used along with P0 to provide the 16- bit address for the external memory Port 2 is also designated as A8 – A15, indicating its dual function Port 0 provides the lower 8 bits via A0 – A7 P1.0 P1.1 P1.2 P1.3 P1.4 P1.5 P1.6 P1.7 RST (RXD)P3.0 (TXD)P3.1 (INT0)P3.2 (INT1)P3.3 (T0)P3.4 (T1)P3.5 (WR)P3.6 (RD)P3.7 XTAL2 XTAL1 GND Vcc P0.0(AD0) P0.1(AD1) P0.2(AD2) P0.3(AD3) P0.4(AD4) P0.5(AD5) P0.6(AD6) P0.7(AD7) -EA/VPP ALE/PROG -PSEN P2.7(A15) P2.6(A14) P2.5(A13) P2.4(A12) P2.3(A11) P2.2(A10) P2.1(A9) P2.0(A8) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 8051 (8031)
  139. 139. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 12 HANEL I/O PROGRAMMING Port 3 Port 3 can be used as input or output Port 3 does not need any pull-up resistors Port 3 is configured as an input port upon reset, this is not the way it is most commonly used P1.0 P1.1 P1.2 P1.3 P1.4 P1.5 P1.6 P1.7 RST (RXD)P3.0 (TXD)P3.1 (INT0)P3.2 (INT1)P3.3 (T0)P3.4 (T1)P3.5 (WR)P3.6 (RD)P3.7 XTAL2 XTAL1 GND Vcc P0.0(AD0) P0.1(AD1) P0.2(AD2) P0.3(AD3) P0.4(AD4) P0.5(AD5) P0.6(AD6) P0.7(AD7) -EA/VPP ALE/PROG -PSEN P2.7(A15) P2.6(A14) P2.5(A13) P2.4(A12) P2.3(A11) P2.2(A10) P2.1(A9) P2.0(A8) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 8051 (8031)
  140. 140. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 13 HANEL I/O PROGRAMMING Port 3 (cont’) Port 3 has the additional function of providing some extremely important signals P1.0 P1.1 P1.2 P1.3 P1.4 P1.5 P1.6 P1.7 RST (RXD)P3.0 (TXD)P3.1 (INT0)P3.2 (INT1)P3.3 (T0)P3.4 (T1)P3.5 (WR)P3.6 (RD)P3.7 XTAL2 XTAL1 GND Vcc P0.0(AD0) P0.1(AD1) P0.2(AD2) P0.3(AD3) P0.4(AD4) P0.5(AD5) P0.6(AD6) P0.7(AD7) -EA/VPP ALE/PROG -PSEN P2.7(A15) P2.6(A14) P2.5(A13) P2.4(A12) P2.3(A11) P2.2(A10) P2.1(A9) P2.0(A8) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 8051 (8031) P3 Bit Function P3.0 RxD TxD INT0 INT1 T0 T1 WR RD P3.1 P3.2 P3.3 P3.4 P3.5 P3.6 P3.7 Pin 10 11 12 13 14 15 16 17 Serial communications External interrupts Timers Read/Write signals of external memories In systems based on 8751, 89C51 or DS89C4x0, pins 3.6 and 3.7 are used for I/O while the rest of the pins in port 3 are normally used in the alternate function role
  141. 141. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 14 HANEL I/O PROGRAMMING Port 3 (cont’) P1.0 P1.1 P1.2 P1.3 P1.4 P1.5 P1.6 P1.7 RST (RXD)P3.0 (TXD)P3.1 (INT0)P3.2 (INT1)P3.3 (T0)P3.4 (T1)P3.5 (WR)P3.6 (RD)P3.7 XTAL2 XTAL1 GND Vcc P0.0(AD0) P0.1(AD1) P0.2(AD2) P0.3(AD3) P0.4(AD4) P0.5(AD5) P0.6(AD6) P0.7(AD7) -EA/VPP ALE/PROG -PSEN P2.7(A15) P2.6(A14) P2.5(A13) P2.4(A12) P2.3(A11) P2.2(A10) P2.1(A9) P2.0(A8) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 8051 (8031) Write a program for the DS89C420 to toggle all the bits of P0, P1, and P2 every 1/4 of a second ORG 0 BACK: MOV A,#55H MOV P0,A MOV P1,A MOV P2,A ACALL QSDELAY ;Quarter of a second MOV A,#0AAH MOV P0,A MOV P1,A MOV P2,A ACALL QSDELAY SJMP BACK QSDELAY: MOV R5,#11 H3: MOV R4,#248 H2: MOV R3,#255 H1: DJNZ R3,H1 ;4 MC for DS89C4x0 DJNZ R4,H2 DJNZ R5,H3 RET END Delay = 11 × 248 × 255 × 4 MC × 90 ns = 250,430 µs
  142. 142. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 15 HANEL I/O PROGRAMMING Different ways of Accessing Entire 8 Bits The entire 8 bits of Port 1 are accessed BACK: MOV A,#55H MOV P1,A ACALL DELAY MOV A,#0AAH MOV P1,A ACALL DELAY SJMP BACK Rewrite the code in a more efficient manner by accessing the port directly without going through the accumulator BACK: MOV P1,#55H ACALL DELAY MOV P1,#0AAH ACALL DELAY SJMP BACK Another way of doing the same thing MOV A,#55H BACK: MOV P1,A ACALL DELAY CPL A SJMP BACK
  143. 143. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 16 HANEL I/O BIT MANIPULATION PROGRAMMING I/O Ports and Bit Addressability Sometimes we need to access only 1 or 2 bits of the port BACK: CPL P1.2 ;complement P1.2 ACALL DELAY SJMP BACK ;another variation of the above program AGAIN: SETB P1.2 ;set only P1.2 ACALL DELAY CLR P1.2 ;clear only P1.2 ACALL DELAY SJMP AGAIN P0 P1 P2 P3 P1.0 P2.0 P2.1 P2.2 P2.3 P2.4 P2.5 P2.6 P2.7 P1.1 P3.0 P3.1 P3.2 P3.3 P3.4 P3.5 P3.6 P1.2 P1.3 P1.4 P1.5 P1.6 P1.7 P3.7 P0.0 P0.1 P0.2 P0.3 P0.4 P0.5 P0.6 P0.7 Port Bit D0 D1 D2 D3 D4 D5 D6 D7
  144. 144. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 17 HANEL I/O BIT MANIPULATION PROGRAMMING I/O Ports and Bit Addressability (cont’) Example 4-2 Write the following programs. Create a square wave of 50% duty cycle on bit 0 of port 1. Solution: The 50% duty cycle means that the “on” and “off” state (or the high and low portion of the pulse) have the same length. Therefore, we toggle P1.0 with a time delay in between each state. HERE: SETB P1.0 ;set to high bit 0 of port 1 LCALL DELAY ;call the delay subroutine CLR P1.0 ;P1.0=0 LCALL DELAY SJMP HERE ;keep doing it Another way to write the above program is: HERE: CPL P1.0 ;set to high bit 0 of port 1 LCALL DELAY ;call the delay subroutine SJMP HERE ;keep doing it 8051 P1.0
  145. 145. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 18 HANEL I/O BIT MANIPULATION PROGRAMMING I/O Ports and Bit Addressability (cont’) Instructions that are used for signal-bit operations are as following Instruction Function SETB bit Set the bit (bit = 1) CLR bit Clear the bit (bit = 0) CPL bit Complement the bit (bit = NOT bit) JB bit, target Jump to target if bit = 1 (jump if bit) JNB bit, target Jump to target if bit = 0 (jump if no bit) JBC bit, target Jump to target if bit = 1, clear bit (jump if bit, then clear) Single-Bit Instructions
  146. 146. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 19 HANEL I/O BIT MANIPULATION PROGRAMMING Checking an Input Bit The JNB and JB instructions are widely used single-bit operations They allow you to monitor a bit and make a decision depending on whether it’s 0 or 1 These two instructions can be used for any bits of I/O ports 0, 1, 2, and 3 Port 3 is typically not used for any I/O, either single-bit or byte-wise Mnemonic Examples Description MOV A,PX MOV A,P2 Bring into A the data at P2 pins JNB PX.Y, .. JNB P2.1,TARGET Jump if pin P2.1 is low JB PX.Y, .. JB P1.3,TARGET Jump if pin P1.3 is high MOV C,PX.Y MOV C,P2.4 Copy status of pin P2.4 to CY Instructions for Reading an Input Port
  147. 147. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 20 HANEL I/O BIT MANIPULATION PROGRAMMING Checking an Input Bit (cont’) Example 4-3 Write a program to perform the following: (a) Keep monitoring the P1.2 bit until it becomes high (b) When P1.2 becomes high, write value 45H to port 0 (c) Send a high-to-low (H-to-L) pulse to P2.3 Solution: SETB P1.2 ;make P1.2 an input MOV A,#45H ;A=45H AGAIN: JNB P1.2,AGAIN ; get out when P1.2=1 MOV P0,A ;issue A to P0 SETB P2.3 ;make P2.3 high CLR P2.3 ;make P2.3 low for H-to-L
  148. 148. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 21 HANEL I/O BIT MANIPULATION PROGRAMMING Checking an Input Bit (cont’) Example 4-4 Assume that bit P2.3 is an input and represents the condition of an oven. If it goes high, it means that the oven is hot. Monitor the bit continuously. Whenever it goes high, send a high-to-low pulse to port P1.5 to turn on a buzzer. Solution: HERE: JNB P2.3,HERE ;keep monitoring for high SETB P1.5 ;set bit P1.5=1 CLR P1.5 ;make high-to-low SJMP HERE ;keep repeating
  149. 149. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 22 HANEL I/O BIT MANIPULATION PROGRAMMING Checking an Input Bit (cont’) Example 4-5 A switch is connected to pin P1.7. Write a program to check the status of SW and perform the following: (a) If SW=0, send letter ‘N’ to P2 (b) If SW=1, send letter ‘Y’ to P2 Solution: SETB P1.7 ;make P1.7 an input AGAIN: JB P1.2,OVER ;jump if P1.7=1 MOV P2,#’N’ ;SW=0, issue ‘N’ to P2 SJMP AGAIN ;keep monitoring OVER: MOV P2,#’Y’ ;SW=1, issue ‘Y’ to P2 SJMP AGAIN ;keep monitoring
  150. 150. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 23 HANEL I/O BIT MANIPULATION PROGRAMMING Reading Single Bit into Carry Flag Example 4-6 A switch is connected to pin P1.7. Write a program to check the status of SW and perform the following: (a) If SW=0, send letter ‘N’ to P2 (b) If SW=1, send letter ‘Y’ to P2 Use the carry flag to check the switch status. Solution: SETB P1.7 ;make P1.7 an input AGAIN: MOV C,P1.2 ;read SW status into CF JC OVER ;jump if SW=1 MOV P2,#’N’ ;SW=0, issue ‘N’ to P2 SJMP AGAIN ;keep monitoring OVER: MOV P2,#’Y’ ;SW=1, issue ‘Y’ to P2 SJMP AGAIN ;keep monitoring
  151. 151. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 24 HANEL I/O BIT MANIPULATION PROGRAMMING Reading Single Bit into Carry Flag (cont’) Example 4-7 A switch is connected to pin P1.0 and an LED to pin P2.7. Write a program to get the status of the switch and send it to the LED Solution: SETB P1.7 ;make P1.7 an input AGAIN: MOV C,P1.0 ;read SW status into CF MOV P2.7,C ;send SW status to LED SJMP AGAIN ;keep repeating The instruction ‘MOV P2.7,P1.0’ is wrong , since such an instruction does not exist However ‘MOV P2,P1’ is a valid instruction
  152. 152. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 25 HANEL I/O BIT MANIPULATION PROGRAMMING Reading Input Pins vs. Port Latch In reading a port Some instructions read the status of port pins Others read the status of an internal port latch Therefore, when reading ports there are two possibilities: Read the status of the input pin Read the internal latch of the output port Confusion between them is a major source of errors in 8051 programming Especially where external hardware is concerned
  153. 153. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 26 HANEL READING INPUT PINS VS. PORT LATCH Reading Latch for Output Port Some instructions read the contents of an internal port latch instead of reading the status of an external pin For example, look at the ANL P1,A instruction and the sequence of actions is executed as follow 1. It reads the internal latch of the port and brings that data into the CPU 2. This data is ANDed with the contents of register A 3. The result is rewritten back to the port latch 4. The port pin data is changed and now has the same value as port latch
  154. 154. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 27 HANEL READING INPUT PINS VS. PORT LATCH Reading Latch for Output Port (cont’) Read-Modify-Write The instructions read the port latch normally read a value, perform an operation then rewrite it back to the port latch Mnemonics Example ANL PX ANL P1,A ORL PX ORL P2,A XRL PX XRL P0,A JBC PX.Y,TARGET JBC P1.1,TARGET CPL PX.Y CPL P1.2 INC PX INC P1 DEC PX DEC P2 DJNZ PX.Y,TARGET DJNZ P1,TARGET MOV PX.Y,C MOV P1.2,C CLR PX.Y CLR P2.3 SETB PX.Y SETB P2.3 Instructions Reading a latch (Read-Modify-Write) Note: x is 0, 1, 2, or 3 for P0 – P3
  155. 155. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 28 HANEL I/O BIT MANIPULATION PROGRAMMING Read-modify- write Feature The ports in 8051 can be accessed by the Read-modify-write technique This feature saves many lines of code by combining in a single instruction all three actions 1. Reading the port 2. Modifying it 3. Writing to the port MOV P1,#55H ;P1=01010101 AGAIN: XRL P1,#0FFH ;EX-OR P1 with 1111 1111 ACALL DELAY SJMP BACK
  156. 156. Home Automation, Networking, and Entertainment Lab Dept. of Computer Science and Information Engineering National Cheng Kung University, TAIWAN Chung-Ping Young 楊中平 ADDRESSING MODES The 8051 Microcontroller and Embedded Systems: Using Assembly and C Mazidi, Mazidi and McKinlay
  157. 157. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 2 HANEL ADDRESSING MODES The CPU can access data in various ways, which are called addressing modes Immediate Register Direct Register indirect Indexed Accessing memories
  158. 158. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 3 HANEL IMMEDIATE ADDRESSING MODE The source operand is a constant The immediate data must be preceded by the pound sign, “#” Can load information into any registers, including 16-bit DPTR register DPTR can also be accessed as two 8-bit registers, the high byte DPH and low byte DPL MOV A,#25H ;load 25H into A MOV R4,#62 ;load 62 into R4 MOV B,#40H ;load 40H into B MOV DPTR,#4521H ;DPTR=4512H MOV DPL,#21H ;This is the same MOV DPH,#45H ;as above ;illegal!! Value > 65535 (FFFFH) MOV DPTR,#68975
  159. 159. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 4 HANEL IMMEDIATE ADDRESSING MODE (cont’) We can use EQU directive to access immediate data We can also use immediate addressing mode to send data to 8051 ports Count EQU 30 ... ... MOV R4,#COUNT ;R4=1EH MOV DPTR,#MYDATA ;DPTR=200H ORG 200H MYDATA: DB “America” MOV P1,#55H
  160. 160. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 5 HANEL REGISTER ADDRESSING MODE Use registers to hold the data to be manipulated The source and destination registers must match in size MOV DPTR,A will give an error The movement of data between Rn registers is not allowed MOV R4,R7 is invalid MOV A,R0 ;copy contents of R0 into A MOV R2,A ;copy contents of A into R2 ADD A,R5 ;add contents of R5 to A ADD A,R7 ;add contents of R7 to A MOV R6,A ;save accumulator in R6 MOV DPTR,#25F5H MOV R7,DPL MOV R6,DPH
  161. 161. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 6 HANEL ACCESSING MEMORY Direct Addressing Mode It is most often used the direct addressing mode to access RAM locations 30 – 7FH The entire 128 bytes of RAM can be accessed The register bank locations are accessed by the register names Contrast this with immediate addressing mode There is no “#” sign in the operand MOV R0,40H ;save content of 40H in R0 MOV 56H,A ;save content of A in 56H MOV A,4 ;is same as MOV A,R4 ;which means copy R4 into A Register addressing mode Direct addressing mode
  162. 162. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 7 HANEL ACCESSING MEMORY SFR Registers and Their Addresses The SFR (Special Function Register) can be accessed by their names or by their addresses The SFR registers have addresses between 80H and FFH Not all the address space of 80 to FF is used by SFR The unused locations 80H to FFH are reserved and must not be used by the 8051 programmer MOV 0E0H,#55H ;is the same as MOV A,#55h ;load 55H into A MOV 0F0H,R0 ;is the same as MOV B,R0 ;copy R0 into B
  163. 163. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 8 HANEL ACCESSING MEMORY SFR Registers and Their Addresses (cont’) 0A8HInterrupt enable controlIE* ……… 0B8HInterrupt priority controlIP* 0B0HPort 3P3* 0A0HPort 2P2* 90HPort 1P1* 80HPort 0P0* 83HHigh byteDPH 82HLow byteDPL Data pointer 2 bytesDPTR 81HStack pointerSP 0D0HProgram status wordPSW* 0F0HB registerB* 0E0HAccumulatorACC* AddressNameSymbol Special Function Register (SFR) Addresses
  164. 164. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 9 HANEL ACCESSING MEMORY SFR Registers and Their Addresses (cont’) 89HTimer/counter mode controlTMOD 88HTimer/counter controlTCON* 0C8HTimer/counter 2 controlT2CON* 87HPower ontrolPCON * Bit addressable 99HSerial data bufferSBUF 98HSerial controlSCON* 0CAHT/C 2 capture register low byteRCAP2L 0CBHT/C 2 capture register high byteRCAP2H 0CCHTimer/counter 2 low byteTL2 0CDHTimer/counter 2 high byteTH2 8BHTimer/counter 1 low byteTL1 8DHTimer/counter 1 high byteTH1 8AHTimer/counter 0 low byteTL0 8CHTimer/counter 0 high byteTH0 OC9HTimer/counter mode controlT2MOD AddressNameSymbol Special Function Register (SFR) Addresses
  165. 165. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 10 HANEL ACCESSING MEMORY SFR Registers and Their Addresses (cont’) Example 5-1 Write code to send 55H to ports P1 and P2, using (a) their names (b) their addresses Solution : (a) MOV A,#55H ;A=55H MOV P1,A ;P1=55H MOV P2,A ;P2=55H (b) From Table 5-1, P1 address=80H; P2 address=A0H MOV A,#55H ;A=55H MOV 80H,A ;P1=55H MOV 0A0H,A ;P2=55H
  166. 166. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 11 HANEL ACCESSING MEMORY Stack and Direct Addressing Mode Only direct addressing mode is allowed for pushing or popping the stack PUSH A is invalid Pushing the accumulator onto the stack must be coded as PUSH 0E0H Example 5-2 Show the code to push R5 and A onto the stack and then pop them back them into R2 and B, where B = A and R2 = R5 Solution: PUSH 05 ;push R5 onto stack PUSH 0E0H ;push register A onto stack POP 0F0H ;pop top of stack into B ;now register B = register A POP 02 ;pop top of stack into R2 ;now R2=R6
  167. 167. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 12 HANEL ACCESSING MEMORY Register Indirect Addressing Mode A register is used as a pointer to the data Only register R0 and R1 are used for this purpose R2 – R7 cannot be used to hold the address of an operand located in RAM When R0 and R1 hold the addresses of RAM locations, they must be preceded by the “@” sign MOV A,@R0 ;move contents of RAM whose ;address is held by R0 into A MOV @R1,B ;move contents of B into RAM ;whose address is held by R1
  168. 168. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 13 HANEL ACCESSING MEMORY Register Indirect Addressing Mode (cont’) Example 5-3 Write a program to copy the value 55H into RAM memory locations 40H to 41H using (a) direct addressing mode, (b) register indirect addressing mode without a loop, and (c) with a loop Solution: (a) MOV A,#55H ;load A with value 55H MOV 40H,A ;copy A to RAM location 40H MOV 41H.A ;copy A to RAM location 41H (b) MOV A,#55H ;load A with value 55H MOV R0,#40H ;load the pointer. R0=40H MOV @R0,A ;copy A to RAM R0 points to INC R0 ;increment pointer. Now R0=41h MOV @R0,A ;copy A to RAM R0 points to (c) MOV A,#55H ;A=55H MOV R0,#40H ;load pointer.R0=40H, MOV R2,#02 ;load counter, R2=3 AGAIN: MOV @R0,A ;copy 55 to RAM R0 points to INC R0 ;increment R0 pointer DJNZ R2,AGAIN ;loop until counter = zero
  169. 169. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 14 HANEL ACCESSING MEMORY Register Indirect Addressing Mode (cont’) The advantage is that it makes accessing data dynamic rather than static as in direct addressing mode Looping is not possible in direct addressing mode Example 5-4 Write a program to clear 16 RAM locations starting at RAM address 60H Solution: CLR A ;A=0 MOV R1,#60H ;load pointer. R1=60H MOV R7,#16 ;load counter, R7=16 AGAIN: MOV @R1,A ;clear RAM R1 points to INC R1 ;increment R1 pointer DJNZ R7,AGAIN ;loop until counter=zero
  170. 170. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 15 HANEL ACCESSING MEMORY Register Indirect Addressing Mode (cont’) Example 5-5 Write a program to copy a block of 10 bytes of data from 35H to 60H Solution: MOV R0,#35H ;source pointer MOV R1,#60H ;destination pointer MOV R3,#10 ;counter BACK: MOV A,@R0 ;get a byte from source MOV @R1,A ;copy it to destination INC R0 ;increment source pointer INC R1 ;increment destination pointer DJNZ R3,BACK ;keep doing for ten bytes
  171. 171. Department of Computer Science and Information Engineering National Cheng Kung University, TAIWAN 16 HANEL ACCESSING MEMORY Register Indirect Addressing Mode (cont’) R0 and R1 are the only registers that can be used for pointers in register indirect addressing mode Since R0 and R1 are 8 bits wide, their use is limited to access any information in the internal RAM Whether accessing externally connected RAM or on-chip ROM, we need 16-bit pointer In such case, the DPTR register is used
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×