Basic Terms: - Bit - Nibble - Double Word - Data - Address - Clock - Memory Capacity
Basic Terms: - Bit - Nibble - Double Word - Data - Address - Clock - Memory Capacity
Basic Terms: - Bit - Nibble - Double Word - Data - Address - Clock - Memory Capacity
3
Microprocessor
• Microprocessor (µP) is the “brain” of a computer
that has been implemented on one
semiconductor chip.
• The word comes from the combination micro and
processor.
• Processor means a device that processes
whatever(binary numbers, 0’s and 1’s)
To process means to manipulate. It describes all
manipulation.
Micro - > extremely small
4
Definition of a Microprocessor.
The microprocessor is a
programmable device that takes in numbers,
performs on them arithmetic or logical
operations according to the program stored in
memory and then produces other numbers as
a result.
5
Microprocessor ?
A microprocessor is multi
programmable clock driven
register based semiconductor
device that is used to fetch ,
process & execute a data
within fraction of seconds.
6
Applications
• Calculators
• Accounting system
• Games machine
• Instrumentation
• Traffic light Control
• Multi user, multi-function environments
• Military applications
• Communication systems
8
Microprocessors
• CPU for Computers
• No RAM, ROM, I/O on CPU chip itself
• Example: Intel's x86, Motorola’s 680x0
9
Microcontroller
• A smaller computer
• On-chip RAM, ROM, I/O ports...
• Example: Motorola’s 6811, Intel’s 8051, Zilog’s Z8 and
PIC
10
Microprocessor vs. Microcontroller
Microprocessor Microcontroller
• CPU is stand-alone, RAM, • CPU, RAM, ROM, I/O and timer
ROM, I/O, timer are separate are all on a single chip
• Single-purpose
• Access times for memory and
I/O devices are more. • Less access times for built-in
memory and I/O devices.
• Microprocessor based • Microcontroller based system
system requires more requires less hardware reducing PCB
size and increasing the reliability
11
hardware.
Microcontrollers Applications
• 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 etc.
Office
– Telephones, computers, security systems, fax machines,
microwave, copier, laser printer, color printer, paging etc.
• Auto
– Trip computer, engine control, air bag, ABS, instrumentation,
security system, transmission control, entertainment, climate
control, cellular phone, keyless entry 12
13
8051 CPU Operation
1. Pin Diagram
2. Block Diagram
14
Pin Diagram of the 8051
15
General Block Diagram of 8051
Interrupt 4K 128 B
Control ROM RAM
CPU
Bus Serial
OSC 4 I/O Ports
Control Port
TXD RXD
P0 P1 P2 P3 16
Detailed Block Diagram
17
Program Status Word [PSW]
C AC F0 RS1 RS0 OV F1 P
Carry Parity
Auxiliary Carry User Flag 1
18
19
BEFORE EXECUTION AFTER
EXECUTION
BEFORE AFTER
EXECUTION EXECUTION
20
BEFORE EXECUTION AFTER
EXECUTION
A 0CH ADD A, B A 04H
B F8H B F8H
AC
1
A = 0000 1100
B = 1111 1000 A = 0CH
0000 0100 B = F8H
1
1 04H
CY
CY
21
8051 instructions that affects flag
22
Special
Function
Register [SFR]
23
Special Function Registers [SFR]
24
SFR Registers & their Addresses
MOV 0E0H,#55H ;is the same as
MOV A,#55H ;which means load 55H into A (A=55H)
25
8051
Memory Space
26
8051 Memory Structure
External
External
60K
64K 64K
SFR
EXT INT 4K
128
EA = 0 EA = 1
29
8051 Register Bank Structure
Bank 3 R0 R1 R2 R3 R4 R5 R6 R7
Bank 2 R0 R1 R2 R3 R4 R5 R6 R7
Bank 1 R0 R1 R2 R3 R4 R5 R6 R7
Bank 0 R0 R1 R2 R3 R4 R5 R6 R7
30
8051 Register Banks with address
31
8051 Programming Model
32
8051 Stack
• The stack is a section of RAM used by the CPU to
store information temporarily.
– This information could be data or an address
33
8051 Stack
• 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
(1) STACK
POINTER
INCREMENTS SP & STORES DPL
35
BEFORE EXECUTION
SP 07H
09H
08H
DPH 10 DPL 50
07H
PUSH DPL
PUSH DPH
AFTER EXECUTION
09H 10
SP 09H
08H 50
DPH 10 DPL 50
07H
36
POP
DPL
POP INITIAL POSITION AND READS DATA 1
DPH
(1) STACK DATA 1
POINTER
DECREMENTS SP
DATA 1
DATA 2
(3) STACK
POINTER 37
BEFORE EXECUTION
09H 30
SP 09H
08H 50
DPH DP
L 07H
POP DPL
POP DPH
AFTER EXECUTION
09H 30
SP 07H 08H 50
DP 5 DP 30 07H
H 0 L 38
External memory
8051
Addressing
Modes
40
8051 Addressing Modes
41
1. Immediate Addressing Mode
• The source operand is a constant
• The immediate data must be preceded by the hash 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
42
2. Register Addressing Mode
• Use registers to hold the data to be manipulated.
44
4. Stack Addressing Mode
• Only direct addressing mode is allowed for pushing or popping the
stack.
• PUSH A is invalid.
45
5. 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.
46
Examples
• 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.
47
6. External Direct
• External Memory is accessed.
51
Programs - 8051
Examples - University Question
UNIT-5
INTERFACING
MICROCONTROLLER
62
8051
TIMERS
63
TIMER REGISTERS
TIMER REGISTERS
TH I TL I TH 0 TL 0
(8 bit) (8 bit) (8 bit) (8 bit)
16 bit 16 bit
TIMER/COUNTER CONTROL
REGISTER (TCON)
8 bit
TIMER/COUNTER MODE
REGISTER (TMOD)
8 bit
TMOD Register
GATE:
When set, counter 0/1 is enabled, if INTx pin is high and
TRx is set.
When cleared, timer 0/1 is enabled, if TRx bit set.
C/T*:
When set, counter operation (input from Tx input pin).
When cleared, timer operation (input from internal clock).
65
TMOD Register
66
TCON Register
67
8051 Timer/Counter
OSC ÷12
C /T 0 TLx THx TFx
(8 Bit) (8 Bit) (1 Bit)
C /T 1
T PIN
INTERRUPT
TR
Gate
INT PIN
68
8051 TIMERS
8051 –Timer
OPERATING
Modes MODES
Timer 0 Timer 1
Mode 0 Mode 0
Mode 1 Mode 1
Mode 2 Mode 2
Mode 3
69
TIMER 0 / 1
OSC ÷12
C /T 0
TL 0/1 TH 0/1 TF0
C /T 1
T 0 PIN
TR 0 INTERRUPT
Gate
INT 0 PIN
70
TIMER 0 – Mode 0
13 Bit Timer / Counter
OSC ÷12
C /T 0 TL0 TH0 INTERRUPT
C /T 1 TF0
T 0 PIN (5 Bit) (8 Bit)
TR 0
Gate
INT 0 PIN
OSC ÷12
C /T 0 TL0 TH0 INTERRUPT
C /T 1 TF0
T 0 PIN (8 Bit) (8 Bit)
TR 0
Gate
INT 0 PIN
OSC ÷12
C /T 0 TL0 TH0 INTERRUPT
C /T 1 TF0
T 0 PIN (8 Bit) (8 Bit)
TR 0
Gate Reload
INT 0 PIN
TH0
(8 Bit)
OSC ÷12
C /T 0 TL0 INTERRUPT
C /T 1 TF0
T 0 PIN (8 Bit)
TR 0
Gate
INT 0 PIN
TR1
74
TIMER 1
OSC ÷12
C /T 0
TL1 TH1 TF1
C /T 1
T 1PIN
INTERRUPT
TR1
Gate
INT 1 PIN
75
TIMER 1 – Mode 0
13 Bit Timer / Counter
OSC ÷12
C /T 0 TL1 TH1 INTERRUPT
C /T 1 TF1
T 1PIN (5 Bit) (8 Bit)
TR1
Gate
INT 1 PIN
OSC ÷12
C /T 0 TL1 TH1 INTERRUPT
C /T 1 TF1
T 1PIN (8 Bit) (8 Bit)
TR1
Gate
INT 1 PIN
OSC ÷12
C /T 0 TL1 TH1 INTERRUPT
C /T 1 TF1
T 1PIN (8 Bit) (8 Bit)
TR1
Gate Reload
INT 1 PIN
TH1
(8 Bit)
• Solution:
80
8051
Serial
Port 84
Basics of Serial Communication
• Computers transfer data in two ways:
– Parallel: Often 8 or more lines (wire conductors) are used to
transfer data to a device that is only a few feet away.
– Serial: To transfer to a device located many meters away,
the serial method is used. The data is sent one bit at a time.
85
Basics of Serial Communication
• Serial data communication uses two methods
– Synchronous method transfers a block of data at a
time
• The start bit is always one bit, but the stop bit can be
one or two bits
88
Data Transfer Rate
• The rate of data transfer in serial data communication is
stated in bps (bits per second).
1. SBUF Register
2. SCON Register
3. PCON Register
90
SBUF Register
• SBUF is an 8-bit register used solely for serial communication.
• For a byte data to be transferred via the TxD line, it must be
placed in the SBUF register.
• The moment a byte is written into SBUF, it is framed with the
start and stop bits and transferred serially via the TxD line.
• SBUF holds the byte of data when it is received by 8051 RxD
line.
• When the bits are received serially via RxD, the 8051 deframes
it by eliminating the stop and start bits, making a byte out of
the data received, and then placing it in SBUF.
91
SCON Register
93
Programming Serial Data Transmission
1. TMOD register is loaded with the value 20H, indicating the use of timer 1
in mode 2 (8-bit auto-reload) to set baud rate.
2. The TH1 is loaded with one of the values to set baud rate for serial data
transfer.
3. The SCON register is loaded with the value 50H, indicating serial mode 1,
where an 8- bit data is framed with start and stop bits.
4. TR1 is set to 1 to start timer 1
5. TI is cleared by CLR TI instruction
6. The character byte to be transferred serially is written into SBUF register.
7. The TI flag bit is monitored with the use of instruction JNB TI, xx to see if
the character has been transferred completely.
8. To transfer the next byte, go to step 5
94
Programming Serial Data Transmission
1. TMOD register is loaded with the value 20H, indicating the use of timer 1
in mode 2 (8-bit auto-reload) to set baud rate.
2. The TH1 is loaded with one of the values to set baud rate for serial data
transfer.
3. The SCON register is loaded with the value 50H, indicating serial mode 1,
where an 8- bit data is framed with start and stop bits.
4. TR1 is set to 1 to start timer 1
5. TI is cleared by CLR TI instruction
6. The character byte to be transferred serially is written into SBUF register.
7. The TI flag bit is monitored with the use of instruction JNB TI, xx to see if
the character has been transferred completely.
8. To transfer the next byte, go to step 5
95
Programming examples
Programming Serial Data Reception
1. TMOD register is loaded with the value 20H, indicating the use of timer 1
in mode 2 (8-bit auto-reload) to set baud rate.
2. TH1 is loaded to set baud rate
3. The SCON register is loaded with the value 50H, indicating serial mode 1,
where an 8- bit data is framed with start and stop bits.
4. TR1 is set to 1 to start timer 1
5. RI is cleared by CLR RI instruction
6. The RI flag bit is monitored with the use of instruction JNB RI, xx to see if
an entire character has been received yet
7. When RI is raised, SBUF has the byte, its contents are moved into a safe
place.
8. To receive the next character, go to step 5.
98
Programming examples
Doubling Baud Rate
• There are two ways to increase the baud rate of data
transfer
1. By using a higher frequency crystal
2. By changing a bit in the PCON register
•We can set it to high by software and thereby double the baud rate.
101
Doubling Baud Rate (cont…)
102
8051
Interrupts
103
INTERRUPTS
104
Interrupt Vs Polling
1. Interrupts
– Whenever any device needs its service, the device notifies the
microcontroller by sending it an interrupt signal.
– Upon receiving an interrupt signal, the microcontroller interrupts
whatever it is doing and serves the device.
– The program which is associated with the interrupt is called the
interrupt service routine (ISR) or interrupt handler.
2. Polling
– The microcontroller continuously monitors the status of a given
device.
– When the conditions met, it performs the service.
– After that, it moves on to monitor the next device until every one is
serviced.
105
Interrupt Vs Polling
• The polling method is not efficient, since it wastes much of the
microcontroller’s time by polling devices that do not need
service.
• The advantage of interrupts is that the microcontroller can serve
many devices (not all at the same time).
• Each devices can get the attention of the microcontroller based
on the assigned priority.
• For the polling method, it is not possible to assign priority since it
checks all devices in a round-robin fashion.
107
Six Interrupts in 8051
Six interrupts are allocated as follows:
1.Reset – power-up reset.
– Timer 0 Overflow.
– Timer 1 Overflow.
– Reception/Transmission of Serial Character.
– External Event 0.
– External Event 1.
110
Presented by C.GOKUL,AP/EEE , Velalar College of Engg & Tech, Erode
8051 Interrupt related Registers
• The various registers associated with the use of
interrupts are:
– TCON - Edge and Type bits for External Interrupts 0/1
--
• EA : Global enable/disable.
• --- : Reserved for additional interrupt hardware.
MOV IE,#08h
• ES : Enable Serial port interrupt.
or • ET1 : Enable Timer 1 control bit.
SETB ET1
• EX1 : Enable External 1 interrupt.
• ET0 : Enable Timer 0 control bit.
• EX0 : Enable External 0 interrupt.
113
Enabling and Disabling an Interrupt
• Example: Show the instructions to (a) enable the serial interrupt,
timer 0 interrupt, and external hardware interrupt 1 and (b) disable
(mask) the timer 0 interrupt, then (c) show how to disable all the
interrupts with a single instruction.
• Solution:
115
Interrupt Priority
• We can alter the sequence of interrupt priority by assigning a
higher priority to any one of the interrupts by programming a
register called IP (interrupt priority).
• To give a higher priority to any of the interrupts, we make the
corresponding bit in the IP register high.
116
Interrupt Priority (IP) Register
Serial Port
INT 0 Pin
Timer 1 Pin
123