Professional Documents
Culture Documents
8086 Lecture Notes 15
8086 Lecture Notes 15
1 Introduction
Comparisons between microprocessors and
microcontrollers:
THE 8051
MICROCONTROLLER
15.1 Introduction
Comparisons between microprocessors and
microcontrollers:
15.1
15.2
15.3
15.4
15.5
15.6
15.7
15.8
Introduction
The 8051 Architecture
Interfacing to External Memory
The 8051 Instruction Set
Timer Operations
Serial Port Operations
Interrupts
8051 Applications
15.1 Introduction
15.1 Introduction
General purpose
Special purpose
purpose devices.
Microcontroller is a device similar to microprocessor
but includes more circuitry in the same chip.
Program & Data Memory
I/O Ports
Serial Communication
Counters/Timers
Interrupt Control logic
A/D and D/A converters
611 37100 Lecture 15-3
Device
number
Internal memory
Program
Data
Timers/
Event Counter
Interrupt
Sources
8051
4K X 8 ROM
128 X 8 RAM
2 X 16-bit
8751H
4K X 8 EPROM
128 X 8 RAM
2 X 16-bit
8031
None
128 X 8 RAM
2 X 16-bit
8052H
8K X 8 ROM
256 X 8 RAM
3 X 16-bit
8752BH
8K X 8 EPROM
256 X 8 RAM
3 X 16-bit
8032AH
None
256 X 8 RAM
3 X 16-bit
I/O Ports
Four 8-bit I/O ports.
Port 0, Port 1, Port 2, Port 3
Most have alternate functions.
Bi-directional.
Port 2 (pin 21 ~ 28 )
Dual purpose I/O port.
As an I/O port:
Standard bi-directional general purpose I/O port.
Alternate functions:
High byte of address bus for external program
and data memory accesses.
Port 0 (pin 32 ~ 39 )
Dual purpose I/O port.
In minimum component design, it is used as a
general purpose I/O port.
In larger designs with external memory, it
becomes a multiplexed data bus:
Low byte of address bus, strobed by ALE.
8-bit instruction bus, strobed by PSEN.
8-bit data bus, strobed by WR and RD.
Port 3 (pin 10 ~ 17 )
Dual purpose I/O port.
As an I/O port:
Alternate functions:
Port pin
Alternative function
P3.0
P3.1
P3.2
P3.3
P3.4
P3.5
P3.6
P3.7
Port 1 (pin 1 ~ 8 )
As an I/O port:
Standard bi-directional port for interfacing to
external devices as required for I/O.
Alternate functions:
Only on some derivatives.
Addressing space
64K x 8 ROM - External Program Memory.
Addressing space
Addressing mode
microcontroller
Addressing mode
Addressing modes are an integral part of each
computers instruction set. They allow different ways
of specifying source/destination operand addresses
depending on the programming situation. There are 8
addressing modes:
EXAMPLE
1. Immediate
2. Register
3. Direct
4. Indirect
5. Relative
6. Absolute
7. Long
8. Indexed
611 37100 Lecture 15-37
Solution:
READ: MOV A, P1
MOV P0, A
JNZ READ
NOP
etc.
; A P1
; P0A
; Repeat until A = 0
; Remainder of program
EXAMPLE
EXAMPLE
Solution:
(a)
(b)
(c)
(d)
(e)
MOV R5, A
MOV P1, 42H
MOV R3, P2
MOV P0, #0FFH
MOV P3, @R1
Repeat the previous example, except stop the looping when the
number 77H is read
Solution:
READ: MOV A, P1
MOV P0, A
CJNE A, #77, READ
NOP
etc.
; A P1
; P0A
; Repeat until A = 77H
; Remainder of program
Solution:
READ: MOV A, P1
MOV P0, A
JNB P2.3, READ
NOP
etc.
; A P1
; P0A
; Repeat until P2.3 = 1
; Remainder of program
EXAMPLE
EXAMPLE
Solution:
MOV R0, #80H
COUNT: MOV P0, R0
DJNZ R0, COUNT
NOP
etc.
; R0 80H
; P0 R0
; Decrement R0, jump to
; COUNT if not 0
; Remainder of program
Solution:
A = 0011 1100
R4 = 0110 0110
A AND R4 = 0010 0100 = 24H
Solution:
Assume carry = 0 initially
A = 1100 0011
Rotate left through carry
A = 1000 0110, carry = 1
Solution:
(a) CLR ACC.7
(b) SETB P3.0
(c) CPL PSW.0
EXAMPLE
EXAMPLE
Multiply the value being input at port 0 times the value at port 1
and send the result to ports 3 and 2 (high order, low order).
Solution:
MOV A, P0
MOV B, P1
MUL AB
MOV P2, A
MOV P3, B
Solution:
Register 7 is used as loop counter with the initial value of 10
(0AH). Each time that the complement instruction (CPL) is
executed, bit 7 will toggle to its opposite state. Toggling bit 7 ten
times will create a waveform with five positive pulses.
; AP0
; BP1
;AXB
; P2A (low order)
; P3B (high order)
Counter/Timer
EXAMPLE
Add the value being input at port 1 to the value at port 2 and
send the result to port 3.
Solution:
MOV R0, P1
MOV A, P2
ADD A, R0
MOV P3, A
; R0P1
; AP2
; AA+R0
; P3A
00 : Mode
01 : Mode
10 : Mode
11 : Mode
0
1
2
3
Timer 0 splits into two 8-bit counter/timers. TL0 and TH0 act
as two separate timers with overflows setting the TF0 and
TF1, respectively.
Timer 1 (when timer 0 is in mode 3):
Counter stopped if in mode 3
Can be used in mode 0, 1, or 2
May be used as a baud rate generator.
10
Control of timers
; (-100)10 = FF9CH
; load Timer 1 registers by FF9CH
The timer is then started by setting the run control bit i.e.,
SETB TR1
Control of timers
TRx bit in bit addressable register TCON is
responsible for starting and stopping the counters
to-serial conversion for output data, and serial-toparallel conversion for input data.
Transmission bit is P3.1 on pin 11 (TXD) and
reception bit is P3.0 on pin 10 (RXD).
Features full duplex (simultaneous reception and
transmission).
Receive buffering allowing one character to be
received and held in a buffer while a second
character is received. If the CPU reads the first
character before the second is fully received, data
are not lost.
611 37100 Lecture 15-65
Control of timers
Timers are usually initialized once at the beginning
of the program to set the correct operating mode.
Then, within the body of a program, the timers are
started, stopped, flag bits are tested and cleared,
timer registers read or updated, and so on, as
required in the application.
First register to be initialized is TMOD to set the
mode of operation e.g.,
to serial port.
Writing to SBUF loads data to be transmitted and reading
SBUF accesses received data.
SCON is a bit addressable register containing status bits and
control bits. Control bits set the operating mode and status
bits indicate the end of a character transmission or reception.
The status bits are tested in software or programmed to
cause an interrupt.
fixed or variable.
Fixed baud rate is derived from on-chip oscillator and
11
Mode
00 = Mode 0 : Shift register I/O
01 = Mode 1 : 8-bit UART
10 = Mode 2 : 9-bit UART
11 = Mode 3 : 9-bit UART
Baud Rate
Fixed (oscillator frequency/12)
Variable (set by timer)
Fixed (osc frq/32 or osc frq/64 )
Variable (set by timer)
Serial interface
12
Summary:
Baud rate: Fixed in mode 2, variable in modes 1 & 3
Data Bits: Eight in mode 1, nine in modes 2 & 3
Interrupt flags
RI & TI in SCON play an important role in serial
communications. Both bits are set by hardware but must be
cleared by software.
RI is set at the end of character reception and indicates
receive buffer full.
This condition is tested in software or programmed to cause
an interrupt.
If software wishes to input a character from the device
connected to the serial port, it must wait until RI is set, then
clear RI and read the character from SBUF.
WAIT: JNB RI, WAIT
CLR RI
MOV A, SBUF
Interrupt flags
TI is set at the end of character transmission and
indicates transmit buffer empty.
If software wishes to send a character to the
device connected to the serial port, it must wait
until TI is set (means previous character was sent,
wait until transmission is finished before sending
the next character), then clear TI and send the
character.
13
15.7 Interrupts
Baud rates
Baud rate is also affected by a bit in the PCON
register. PCON.7 is SMOD bit. If SMOD = 1, baud
rate will be doubled in modes 1, 2 and 3.
Mode 2 baud rate is 1/64 of the oscillator
frequency (SMOD = 0) and can be doubled to 1/32
of the oscillator frequency (SMOD = 1).
PCON is not bit-addressable, setting SMOD
without altering the other bits requires a readmodify-write operation as follows:
MOV A, PCON
SETB ACC.7
MOV PCON, A
15.7 Interrupts
Baud rates
Usually the timer is used in auto-reload mode and TH1 is
loaded with a proper reload value.
Formula for the baud rate in modes 1 and 3 is
Baud Rate = Timer 1 Overflow Rate / 32
e.g., For 1200 baud
1200 = Timer 1 Overflow Rate / 32
Timer 1 Overflow Rate = 38400 Hz
15.7 Interrupts
Baud rates
To initialize the serial port to operate as an 8-bit
UART at 2400 baud.
ORG 0000H
MOV SCON,#52H
MOV TMOD,#20H
MOV TH1, #-13
SETB TR1
END
sensitivity.
Each interrupt type has a separate vector address.
All interrupts are disabled after a system reset and enabled
individually by software.
611 37100 Lecture 15-84
14
15.7 Interrupts
15.7 Interrupts
Interrupt vectors
When an interrupt is accepted, the value loaded
into the PC is called the interrupt vector. It is the
address of the start of the ISR for the interrupting
source.
When an interrupt is vectored, the flag that
caused the interrupt is automatically cleared by
hardware.
Timer interrupts occur when the timer registers
(TLx/THx) overflow and set the overflow flag
(TFx).
15.7 Interrupts
15.7 Interrupts
Interrupt address
15.7 Interrupts
15.7 Interrupts
External Interrupts
External interrupt occurs as a result of a low-level
or negative-edge on the INT0 or INT1 pin of 8051.
Flags that generate these interrupts are bits IE0
and IE1 in TCON. These are automatically
cleared when the CPU vectors to the interrupt.
Low-level or negative-edge activated interrupts
can be programmed through IT0 and IT1 bits in
TCON, i.e., ITx = 0 means low-level and ITx = 1
means negative-edge triggered.
15
EXAMPLE
Solution:
READ:
ON:
OFF:
STOP:
MOV A, P0
; Keep reading port 0 switches
JZ READ
; into A until A0
MOV R0, A
; Transfer A to register 0
MOV P1, #0FFH ; Turn ON port 1 LEDs
CALL DELAY ; Delay 1 second
MOV P1, #00H ; Turn OFF port 1 LEDs
CALL DELAY ; Delay 1 second
DJNZ R0, ON ; Loop back number of times on switches
JMP STOP
; Suspend operation
KEYSCAN:
STOP:
ROWRD:
RET1:
LOOP2:
LOOP1:
DELAY:
RET2:
CONVRT:
EXAMPLE
EXAMPLE
16
; Write 1s to port 1
; Write 1s to port 0
; Output LOW-then-HIGH
; on SC
; Wait here until EOC goes LOW
; Transfer ADC result to register 0
http://www.qsl.net/ok2xdx/Vyskomer/Vyskomer.html
611 37100 Lecture 15-98
17