8051 Microcontroller
8051 Microcontroller
8051 Microcontroller
MICROCONTROLLER
The necessary tools for a
Microprocessor/controller
A smaller computer
On-chip RAM, ROM, I/O ports...
Example : Motorola’s 6811, Intel’s 8051, Zilog’s Z8 and PIC 16X
Microcontroller
Microprocessor vs. Microcontroller
Microcontroller
Microprocessor
CPU, RAM, ROM, I/O and timer are all
CPU is stand-alone, RAM, ROM,
on a single chip
I/O, timer are separate
fixed amount of on-chip ROM, RAM,
designer can decide on the amount
I/O ports
of ROM, RAM and I/O ports.
Microcontrollers are found in small,
Commonly used as a CPU in
minimum component designs performing
computers
control oriented activities
Microprocessor instruction sets are
Microcontroller instruction sets cater to
processing intensive - Their
control of inputs and outputs - They have
instructions operate on nibbles,
instructions to set and clear individual
bytes, words, or even double words
bits and perform bit operations - They
- Addressing modes provide access
have instructions for input/output
to large arrays of data using pointers
operations, event timing, enabling and
and offsets
setting priority levels for interrupts
Processing power is more caused by external stimuli.
expansive Processing power is less
versatility for applications in which cost, power and
general-purpose space are critical
single-purpose
Embedded System
External interrupts
On-chip Timer/Counter
ROM for
Interrupt program On-chip Timer 1 Counter
Control code RAM Inputs
Timer 0
CPU
Bus Serial
4 I/O Ports
OSC Control Port
P0 P1 P2 P3 TxD RxD
Address/Data
8051
Today over fifty companies produce variations of the 8051.
RST: Reset input. A high on this pin for two machine cycles
resets the device.
ALE/PROG: Address Latch Enable output pulse for latching
the low byte of the address during accesses to
external memory. This pin is also the program pulse
input (PROG) during programming of the EPROM
parts.
8051 Pin Descriptions
Both code and data may be internal, however, both expand using
external components to a maximum of 64K code memory and 64K data
memory.
In the 8051, the registers and input/output ports are memory mapped
and accessible like any other memory location.
In the 8051, the stack resides within the internal RAM, rather than in
external RAM.
Program Memory
Program memory normally have 4K on chip (but not in the case of 8031).
ROM occupies code address space 0000h to 0FFFh.
The PC is used to address code bytes from addresses 0000h to FFFFh.
Program addresses higher than 0FFFh, which exceeds internal ROM
capacity, will cause 8051 to automatically fetch code bytes from external
program memory.
Code can also be fetched exclusively from an external memory , addresses
0000h to FFFFh, by connecting the external access pin to gnd.
Data Memory
For example, if output lines P0.0 through P0.7 are all clear (0) and
you want to turn on the P0.0 output line you may either execute:
MOV P0,#01h || SETB 80h || SETB P0.0
Both these instructions accomplish the same thing. However, using
the SETB command will turn on the P0.0 line without effecting the
status of any of the other P0 output lines
Special Function Registers
Special Function Registers (SFRs) are areas of memory that control
specific functionality of the 8051 processor. SFRs are used to control the
way the 8051 functions. Each SFR has a specific purpose and format.
Each SFR occupies internal RAM from 0x80 to 0xFF (but, some areas
are empty!)
Aside from the MUL and DIV instructions, the ‘B’ register
is often used as yet another temporary storage register
much like a ninth ‘R’ register.
B register is bit-addressable.
Stack Register
Stack pointer (SP) is an 8-bit register at address 81H
It contains the address of the data item currently on top of the stack.
Stack operations include pushing data on the stack and popping data off
the stack
Popping from the stack reads the data and decrements the SP
Depending on the initial value of the SP, stack can have different sizes
On 8051 this would limit the stack to 32 bytes since the uppermost
address of on chip RAM is 7FH.
Stack in 8051
The default value of SP (after system reset) is 07H.
PSW Register
Auxiliary carry flag (AC) is set if a carry was generated out of bit 3 into
bit 4 or if the result in the lower nibble is in the range 0AH to 0FH. AC is
useful in arithmetic operations on binary coded decimal (BCD) values.
Register Bank Select Bits (RS0 and RS1): determine the active register
bank
Example: The following instructions enable register bank 3 and move
the content of R7 (address 1FH) to the accumulator
SETB RS1
SETB RS0
MOV A,R7
Program Status Word (PSW)
Register
Overflow flag (OV) is set after an addition or subtraction if there was an
arithmetic overflow.
o When signed numbers are added or subtracted this bit determines if the
result is in the proper range. Results greater than 127 or less than –128
will set OV bit
o When unsigned numbers are added OV can be ignored
o Example: What is the OV and the content of accumulator after the
following instruction sequence?
MOV R7, #FFH
MOV A, #0FH
ADD A,R7
o Answer: OV=0, ACC=0EH
1. Immediate
2. Register
3. Direct
4. Indirect
5. Indexed
6. Relative
7. Absolute
8. Long
Immediate Addressing
Examples:
ADD A,R7 ; add the values of A and R7
INC DPTR ; adding 1 to the data pointer
MUL AB ; multiplying unsigned values in A & B
Direct Addressing
operand is specified by an 8-bit address field in the
instruction
Examples:
ADD A,55H ; adds the values of A and contents of loc 55h
MOV P1, A ; transfers the content of acc to Port 1 (address 90H)
Advantages:
Direct addressing is generally fast since, although the value to be
loaded isn’t included in the instruction, it is quickly accessible since
it is stored in the 8051’s Internal RAM.
It is also much more flexible than Immediate Addressing since the
value to be loaded is whatever is found at the given address--which
may be variable.
Direct Addressing
The obvious question that may arise is, "If direct addressing an
address from 80h through FFh refers to SFRs, how can I access the
upper 128 bytes of Internal RAM that are available on the 8052?“
The answer is: You can’t access them using direct addressing. As
stated, if you directly refer to an address of 80h through FFh you will
be referring to an SFR. However, you may access the 8052’s upper
128 bytes of RAM by using the next addressing mode, "indirect
addressing."
Register Indirect Addressing
instruction specifies a register which contains the address of the
operand.
Both internal and external RAM can be indirectly addressed.
The address register for 8-bit addresses can be R0 or R1 of the
selected register bank, or the Stack Pointer.
The address register for 16-bit addresses can only be the data
pointer register, DPTR.
indirect addressing is represented by an @ before R0 or R1.
Example:
MOV A, @R0 ; load the acc with the value from internal RAM which is found at
the address indicated by R0
MOVX A,@DPTR ; move the contents of the external memory address pointed to by
DPTR into the Accumulator
MOVX @DPTR,A ; write the value of the Accumulator to the external memory
address pointed to by DPTR.
Indirect addressing always refers to Internal RAM; it never refers to
an SFR
MOV R0,#99h ; Load the address of the serial port
MOV @R0,#01h ; Send 01 to the serial port -- WRONG!!
This is not valid
Indexed addressing
Indexed addressing uses a base register (either the program
counter or data pointer) and an offset (the accumulator) in
forming the effective address for a JMP or MOVC instruction.
Only Program Memory can be accessed with indexed
addressing, and it can only be read.
This addressing mode is intended for reading look-up tables
in Program Memory.
A 16-bit base register (either DPTR or the Program Counter)
points to the base of the table, and the Accumulator is set up
with the table entry number. The address of the table entry in
Program Memory is formed by adding the Accumulator data to
the base pointer.
Example:
MOVC A, @A+DPTR ; this instruction moves a byte of data from code
memory to the accumulator. The address in code
memory is found by adding the accumulator to the data
pointer
Relative Addressing
Example:
LJMP 8AF2H ; jumps to memory location 8AF2H
Instruction Types
The DA A opcode only works when used with ADD and ADDC
commands and does not give correct adjustments for SUBB, MUL
or DIV commands.
Example: If ACC contains BCD value of 59 then:
ADD A, #1 ; adds 1 to A, leaving 5A in A register
DA A ; then adjust the result to correct BCD value 60.
Arithmetic Instructions
Logical Instructions
8051 logical instructions perform Boolean operations on bytes
of data on a bit-by-bit basis .
Program ROM
Jump and Call Instructions
The jump and calls are decision codes that alter the flow of
the program by examining the results of the action codes and
changing the contents of the program counter.
A jump permanently changes the contents of the PC if certain
program condition exists.
A Call temporarily changes the contents of the PC to allow
another part of the program to run.
Jump and Calls may also be referred as ‘branches’.
Types of decision opcodes:
1. Jump on bit conditions
2. Compare bytes and jump if not equal
3. Decrement byte and jump if not equal
4. Jump unconditionally
5. Call a subroutine
6. Return from a subroutine
Jump and Call Instructions
Program Range
A jump or call instruction can replace
the contents of the PC with a new
program address that causes
program execution to begin at the
code located at the new address. The
difference, in bytes, of this new
address from the address in the
program where jump or call is located
is called the range of the jump or call.
Unconditional Jumps:
They do not test any bit or byte to determine whether the jump
should be taken. The jump is always taken.
These are the only jumps that can jump to any memory
location.
Unconditional