of Computer Science and Information Engineering National Cheng Kung University, TAIWAN Chung-Ping Young 楊中平 INTRODUCTION TO COMPUTING The 8051 Microcontroller and Embedded Systems: Usi
Trang 1www.elsolucionario.org
Trang 2The 8051 Microcontroller and Embedded
Systems Using Assembly and C
Second Edition
Muhammad Ali Mazidi Janice Gillispie Mazidi Rolin D McKinlay
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
Trang 3Home 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
Trang 4OUTLINES Numbering and coding systems
www.elsolucionario.org
Trang 5Department of Computer Science and Information Engineering
¾ There are only 0 and 1
¾ These two binary digits are commonly referred to as bits
Trang 6becomes zero
to obtain the binary number
Ex Convert 2510 to binary
Quotient Remainder 25/2 = 12 1 LSB (least significant bit)
Trang 7Department of Computer Science and Information Engineering
decimal number to a binary directly
Ex Convert 110012 to decimal
www.elsolucionario.org
Trang 8¾ ex
It is much easier to represent a string of 0s and 1s such as
100010010110 as its hexadecimal equivalent of
C 1100
12
B 1011
11
A 1010
10
9 1001
9
8 1000
8
7 0111
7
6 0110
6
5 0101
5
4 0100
4
3 0011
3
2 0010
2
1 0001
1
0 0000
0
Hex Binary
Decimal
Trang 9Department of Computer Science and Information Engineering
equivalent hexadecimal number
¾ Start from the right and group 4 bits at a time, replacing each 4-bit binary number with its hex equivalent
¾ Each hex digit is replaced with its 4-bit binary equivalent
Ex Convert hex 29B to binary
Trang 10by repeated division, keeping track of the remainders
Ex Convert 4510 to hex
Trang 11Department of Computer Science and Information Engineering
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
Trang 12least 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
MSD: 2 + 9 = B
Trang 13Department of Computer Science and Information Engineering
first, borrow 16 from the preceding digit
Ex Perform hex subtraction: 59F – 2B8
59F LSD: 15 – 8 = 7 – 2B8 9 + 16 – 11 = 14 = E16
www.elsolucionario.org
Trang 14NUMBERING
AND CODING
SYSTEMS
ASCII Code
assigns binary patterns for
represent each code
d 64
D 44
c 63
C 43
b 62
B 42
a 61
A 41
Symbol Hex
Symbol Hex
Selected ASCII codes
Trang 15Department of Computer Science and Information Engineering
HANEL
DIGITAL
PRIMER
Binary Logic
as the two digits 0 and 1
distinct voltage levels with built-in tolerances for variations in the voltage
either of the two shaded areas
0 1 2 3 4 5
Logic 0 Logic 1
Trang 17Department of Computer Science and Information Engineering
Computer Science Illuminated, Dale and Lewis
Computer Science Illuminated, Dale and Lewis
Trang 19Department of Computer Science and Information Engineering
Trang 21Department of Computer Science and Information Engineering
Address decoder for 5 (01012)The output will be 1 if and only if the input is 01012
Address Decoders
Trang 22¾ Flip-flops are frequently used to store data
Digital Design, Mano
www.elsolucionario.org
Trang 23Department of Computer Science and Information Engineering
¾ Bit : a binary digit that can have the value
0 or 1
memory in IBM PCs and compatibles
Trang 24¾ Execute information stored in memory
¾ Provide a means of communicating with CPU
¾ RAM (Random Access Memory) – temporary storage of programs that computer is running
¾ ROM (Read Only Memory) – contains programs and information essential to operation of the computer
Trang 25Department of Computer Science and Information Engineering
Address bus
Data buswww.elsolucionario.org
Trang 26¾ Carries information from place to place
Trang 27Department of Computer Science and Information Engineering
be unique
and the decoding circuitry finds the device
Trang 28INSIDE THE
COMPUTER
More about
Data Bus
better the CPU
¾ Think of data buses as highway lanes
expensive CPU and computer
¾ The average size of data buses in CPUs varies between 8 and 64
¾ To receive or send data
related to the size of its buses
www.elsolucionario.org
Trang 29Department of Computer Science and Information Engineering
larger the number of devices that can
be addressed
CPU can communicate is always equal
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
Trang 30the data must be stored in RAM or
memory
and permanent
¾ Tables or initialization program
permanent and can change with time
¾ Various versions of OS and application packages
¾ CPU gets information to be processed
Trang 31Department of Computer Science and Information Engineering
¾ In general, the more and bigger the registers, the better the CPU
is the increased cost of such a CPU
www.elsolucionario.org
Trang 32Register A Register B
Trang 33Department of Computer Science and Information Engineering
¾ Performs arithmetic functions such as add, subtract, multiply, and divide, and logic
functions such as AND, OR, and NOT
¾ Points to the address of the next instruction to be executed
counter is incremented to point to the address
of the next instruction to be executed
¾ Interprets the instruction fetched into the CPU
instructions requires more transistors to design
Trang 34Assume 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
www.elsolucionario.org
Trang 35Department of Computer Science and Information Engineering
Mem addr Contents of memory address
1400 (B0) code for moving a value to register A
Trang 361. 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
以動畫表示
Trang 37Department of Computer Science and Information Engineering
¾ 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
www.elsolucionario.org
Trang 38¾ 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
Trang 39
Department of Computer Science and Information Engineering
¾ 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 1406H6.
¾ 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
Trang 40Chung-Ping Young
楊中平
8051 MICROCONTROLLERS
The 8051 Microcontroller and Embedded
Systems: Using Assembly and C
Mazidi, Mazidi and McKinlay
www.elsolucionario.org
Trang 41Department of Computer Science and Information Engineering
Trang 43Department of Computer Science and Information Engineering
TimerI/O
ROMRAM
Timer
purpose Micro- Processor
General-www.elsolucionario.org
Trang 44space are critical
¾ In many applications, the space it takes, the power it consumes, and the price per
Trang 45Department of Computer Science and Information Engineering
microprocessor (or microcontroller) to
do one task and one task only
¾ There is only one application software that
is typically burned into ROM
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
it that performs only one task
Trang 46garage 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
machines, microwave, copier, laser printer, color printer, paging
instrumentation, security system, transmission
www.elsolucionario.org
Trang 47Department of Computer Science and Information Engineering
microprocessors have targeted their microprocessor for the high end of the embedded market
¾ There are times that a microcontroller is inadequate for the task
general-purpose microprocessor for the embedded market, it optimizes the processor used for embedded systems
interchangeably
Trang 48embedded system is to decrease power consumption and space
processors, the trend is to integrate more functions on the CPU chip and let designer decide which features he/she wants to use
high-end embedded applications
¾ Saves money and shortens development time
Trang 49Department of Computer Science and Information Engineering
microcontrollers made by various chip makers
www.elsolucionario.org
Trang 50task 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 performance or lower power-consumption versions
Trang 51higher-Department of Computer Science and Information Engineering
tools, such as compilers, assemblers, and debuggers
Trang 52¾ The 8051 is an 8-bit processor
allowing other manufactures to make
www.elsolucionario.org
Trang 53Department of Computer Science and Information Engineering
Bus Control
CPU
Ports
Serial Port
Etc.
Timer 0 Timer 1
On-chip RAM
On-chip ROM for code
P0 P1 P2 P3Address/Data
TXD RXD
External Interrupts
Trang 54¾ Add external ROM to it
¾ You lose two ports, and leave only 2 ports for I/O operations
32 32
32 I/O pins
2 3
2 Timers
128 256
128 RAM (bytes)
0K
8K 4K
ROM (on-chip program space in bytes)
8031 8052
8051 Feature
Trang 55Department of Computer Science and Information Engineering
¾ Flash (erase before write)
¾ Flash
on-chip flash via PC COM port
www.elsolucionario.org
Trang 56¾ NV-RAM (changed one byte at a time), RTC (real-time clock)
of 8051
¾ ADC, DAC, extended I/O, and both OTP and flash
Trang 57Home 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
Trang 58INSIDE THE
8051
Registers
temporarily, while the information could be
¾ a byte of data to be processed, or
¾ an address pointing to the data to be fetched
8-bit registers
¾ There is only one data type, 8 bits
www.elsolucionario.org
Trang 59Department of Computer Science and Information Engineering
D0D1
D2D3
D4D5
D6D7
8 bit Registers
most significant bit
least significant bit
Trang 60PC PC (Program counter)
Trang 61Department of Computer Science and Information Engineering
operand
MOV A,#55H ;load value 55H into reg AMOV 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
www.elsolucionario.org
Trang 62 “MOV A, #5”, the result will be A=05; i.e., A
= 00000101 in binary
Moving a value that is too large into a
If it’s not preceded with #,
it means to load from a memory location
Add a 0 to indicate that
F is a hex number and not a letter
Trang 63Department of Computer Science and Information Engineering
;to the accumulator
byte to register A and put the result in register A
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
ADD A, R2 ;add R2 to Accumulator
There are always
many ways to write
the same program,
depending on the
registers used
Trang 64Structure of
Assembly
Language
consisting of 0s and 1s
Assembly languages , which provided mnemonics for the machine code instructions, plus other features, were developed
of lines of Assembly language instructions
Trang 65Department of Computer Science and Information Engineering
¾ a mnemonic (abbreviation easy to remember)
to do with those items
¾ optionally followed by one or two operands
a series of statements, or lines
¾ Assembly language instructions
¾ Directives (or pseudo-instructions)
Trang 668051
ASSEMBLY
PROGRAMMING
consists of four fields:
[label:] Mnemonic [operands] [;comment]
Mnemonics produce opcodes
Directives do not generate any machine code and are used only by the assembler
Trang 67Department of Computer Science and Information Engineering
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
an ASCII file
the usual DOS conventions, but the source file has the extension “asm“ or “src”, depending
on which assembly you are using
www.elsolucionario.org
Trang 682) The “asm” source file containing the
program code created in step 1 is fed to
the extension for the list file is “lst”
3) Assembler require a third step called
linking
code files and produce an absolute object file with the extension “abs”
Trang 69Department of Computer Science and Information Engineering
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
step 2 through 4 into one step
Trang 70ASSEMBLER PROGRAM
LINKER PROGRAM
Trang 71Department of Computer Science and Information Engineering
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
8 000A 80EF HERE: SJMP HERE;stay in this loop
address