Microcontroller 8051
Microcontroller 8051
Microcontroller 8051
2
Module-1
3
MICROCONTROLLER-8051
4
Why do we need to learn Microcontrollers ?
5
Personal information Laptop components: Home appliances: door Industrial equipment:
products: Cell phone, mouse, keyboard, modem, lock, alarm clock, Temperature/pressure
pager, watch, pocket fax card, sound card, thermostat, air controllers, Counters,
recorder, calculator battery charger conditioner, TV remote, timers, RPM Controllers
VCR, small refrigerator,
exercise equipment,
washer/dryer, microwave
oven
6
So, A good designer
should always know
architecture, advantages ,
what type of controller
he/she is using ,their
ways to reduce
disadvantages , product reliability etc.
production costs and
O.K ????
7
Introduction
A decade back the process and control operations were totally implemented
by the Microprocessors only.
But now a days the situation is totally changed and it is occupied by the new
devices called Microcontroller.
The development is so drastic that we can’t find any electronic gadget without
the use of a microcontroller.
8
Then What is a Microcontroller ?
• A smaller computer
• On-chip RAM, ROM, I/O ports, timers etc
• Example:Motorola’s 6811, Intel’s 8051, Zilog’s
Z8 and PIC 16X
9
How is it different from a Microprocessor ??
• General-purpose microprocessor
Data Bus
CPU
General-Pur
pose Serial
RAM ROM I/O Timer
Micro-proc COM
Port
essor Port
Address Bus
10
Microprocessor
MP is General Purpose digital CPU Know as computer on chip.
Block Diagram of MP
12
Microprocessor vs. Microcontroller
Microprocessor Microcontroller
■ CPU is stand-alone, • CPU, RAM, ROM, I/O
RAM, ROM, I/O, timer and timer are all on a
are separate single chip
■ Designer can decide on • fix amount of on-chip
the amount of ROM, ROM, RAM, I/O ports
RAM and I/O ports.
■ expansive • for applications in
■ Versatility (Ability to which cost, power and
apodpt) space are critical
■ general-purpose • single-purpose
13
S. Microprocessor Microcontroller
N
o
1 A microprocessor is a general purpose A microcontroller is a dedicated chip which is also
device which is called a CPU called single chip computer.
2 A microprocessor do not contain onchip A microcontroller includes RAM, ROM, serial and
I/OPorts, Timers, Memories etc.. parallel interface, timers, interrupt
3 Microprocessors are most commonly used as Microcontrollers are used in small, minimum
the CPU in microcomputer systems component designs performing control-oriented
applications.
4 Microprocessor instructions are mainly Microcontroller instructions are both bit addressable
byte addressable as well as byte addressable.
5 Microprocessor instruction sets are mainly Microcontrollers have instruction sets catering to the
intended for catering to large volumes of control of inputs and outputs.
data.
6 Microprocessor based system design is Microcontroller based system design is rather simple
complex and expensive and cost effective
7 The Instruction set of microprocessor is The instruction set of a Microcontroller is very
complex with large number of instructions. simple with less number of instructions. For, ex: PIC
14
microcontrollers have only 35 instructions.
EVOLUTION
Flashback !!!!
In the year 1976, Motorola created a Microprocessor chip
called 6801 which replaced its brother 6800 with certain
add-on chips to make a computer. This paved the way for
the new revolution in the history of chip design and gave
birth to a new entity called
MICROCONTROLLER.
The INTEL bagged the credit of producing the first
Microcontroller 8048 with a CPU and 1K bytes of
EPROM, 64 Bytes of RAM an 8-Bit Timer and 27 I/O
pins in 1976.
15
contd
• Then followed the most popular controller 8051 in
the year 1980 with 4K bytes of ROM,128 Bytes of
RAM , a serial port, two 16-bit Timers , and 32 I/O
pins.
• The 8051 family has many additions and
improvements over the years and remains a most
soughtafter tool for todays circuit designers.
• The same INTEL introduced a 16 bit controller
8096 in the year 1982
16
• Later INTEL introduced 80c196 series of
16-bit microcontrollers for mainly industrial
applications
17
EVOLUTION OF MICROCONTROLLERS
• The first microcontroller TMS1000 was introduced by Texas Instrumentsin the
year 1974.
• In the year 1976, Motorola designed a Microprocessor chip called 6801 This
paved the way for the new revolution in the history of chip design and gave
birth to a new entity called “Microcontroller”.
• Later the Intel company produced its first Microcontroller 8048 with a CPU and
1K bytes of EPROM, 64 Bytes of RAM an 8-Bit Timer and 27 I/O pins in 1976.
• Then followed the most popular controller 8051 in the year 1980 with 4K bytes
of ROM,128 Bytes of RAM , a serial port, two 16-bit Timers , and 32 I/O pins.
• INTEL introduced a 16 bit microcontroller 8096 in the year 1982 .
• Later INTEL introduced 80c196 series of 16-bit Microcontrollers for mainly
industrial applications.
• Microchip, another company has introduced an 8-bit Microcontroller PIC 16C64
in the year 1985.
• The 32-bit microcontrollers have been developed by IBM and Motorola. MPC
505 is a 32-bit RISC controller of Motorola. The 403 GA is a 32 -bit RISC
embedded controller of IBM.
• In recent times ARM company (Advanced RISC machines) has developed and
introduced 32 bit controllers for high-end application devices like mobiles ,
Ipods etc...
18
Types of Microcontrollers
19
4-bit Microcontrollers:
THESE 4-BIT WHICH ARE WIDELY THE POPULAR 4-BIT WHICH IS A 20 PIN DIP
MICROCONTROLLERS USED FOR LOW END CONTROLLERS ARE CHIP WITH 4KB OF
ARE SMALL SIZE, APPLICATIONS LIKE RENASA M34501 ROM,256 BYTES OF RAM,
MINIMUM PIN COUNT LED & LCD DISPLAY 2-COUNTERS AND 14
AND LOW COST DRIVERS ,PORTABLE I/O PINS.
CONTROLLERS. BATTERY CHARGERS
ETC..
20
• These are the most popular and widely used
microcontrollers
• The 8-bit microcontroller has 8-bit internal bus and the ALU
performs all the arithmetic and logical operations on a byte
8-bit instruction.
• Features: UART
21
• The internal bus width of 16-bit microcontroller is
of 16-bit
22
• These microcontrollers used in
high end applications like
Automative control,
Communication networks,
Robotics,Cell phones ,GPRS &
PDAs etc..
32-Bit
Microcontrollers
: • EX:PIC32,ARM 7,ARM9
,SHARP LH79520 ,ATMEL 32
(AVR) ,Texas Instrument’s –.
TMS320F2802x/2803x etc..are
some of the popular 32-bit
microcontrollers.
23
INTEL MCS 51 Family
Microcontrol On chip On chip Timers/Count Interrupts Serial ports
ler RAM program ers
(Bytes) memory
8031 128 None 2 5 1
24
Development system for Microcontrollers
• An assembly language program consists of
Mnemonics (which are nothing but short
abbreviated English instructions given to the
controller.)
25
• 1. Editor : An Editor is a program which allows us to create a file
containing the assembly language statements for the program.
Examples of some editors are PC write Wordstar. After typing all
the program we have to save the program . This we call it as source
file. The next step is to process the source file with an assembler.
Ex: Sample.asm
(When you run the assembler it reads the source file of your program
from where you have saved it. The assembler generates a file with the
extension .hex. This file consists of hexadecimal values encoding a
sequence of data and their starting offset or absolute address. )
26
• 3.Compiler : A compiler is a program which converts the high level
language program like “C” into binary or machine code.
Compilers like Keil ,Ride and IAR workbench are very popular.
27
Memory Org for 8051
8051 MC support Harvard Architecture
28
The Register 32 byte
Bank 00H TO 1FH
Internal
Memory BIT/BYTE 16 byte
(Data Addressable
area 20 H TO 2FH
Memory
128B)
General 80 byte
Memory 30H TO 7FH
29
30
Register’s Bank
Internal Address
To access these Bank Special function register called PSW : RSO and RS1.
31
Starts Bit address 00 to 7Fh
Bit/Byte
Addressable Example: 20h Memory location : you can get
8bit memory
Memory
(16B) Like this 16X8=128 bits
32
General User/Designer can store
data depends on
Purpose requirement of application.
33
34
35
Program Memory
8051 has 4KB of internal RAM
37
SALIENT FEATURES
The salient features of 8051 Microcontroller are
Internal ROM
38
32 I/O pins arranges as four 8 bits ports P0-P3
Controller register:
TCON(Timer Control Register),
TMOD (Timer Mode Register),
SCON(Serial control),
PCON(Power Control register),
IP(interrupt priority register),
IE(Interrupt Enable)) 39
ARCHITECTURE & BLOCK DIAGRAM OF 8051 MICROCONTROLLER:
40
ALU AND A AND B REGISTER:
ALU 8bit
A= OEOH
B=OFOH 41
PC AND DPTR
PC: It was 16 bit register
43
ALU and A and B Register
ALU Max can process operation 8 bits at a time
8051 MUL and DIV are performed through A and B register Only.
44
A and B register has called General
purpose register
45
A and B Registers :
• The A and B registers are special function registers which hold the results
of many arithmetic and logical operations of 8051.The A register is also
called the Accumulator and as it’s name suggests, is used as a general
register to accumulate the results of a large number of instructions. By
default it is used for all mathematical operations and also data transfer
operations between CPU and any external memory.
46
• The R registers: The "R" registers are a set of eight registers that are
named R0, R1, to R7. These registers are used as auxiliary registers
in many operations. The "R" registers are also used to temporarily
store values.
48
49
50
51
52
SFR and RAM = 256B
RAM= 128B
SFR=128 B
53
Special Function Register (SFR)
Register FUNCTION
Name
A Primary Accumulator * 0E0H
B Secondary Accumulator * 0FOH
DPL Data pointer lower Byte 82H
DPH Data pointer Higher Byte 83H
SP Stack pointer 81H
TCON Timer control register * 88H
TMOD Timer Mode section 89H
register
SCON Serial control register * 98H
SBUF Serial Buffer Register 99H
54
Register Name FUNCTION
56
57
58
59
60
61
62
63
64
ARM Controllers
• In recent times ARM company (Advanced
Risc machines) has developed and introduced
32 bit controllers which are highend
application devices,especially communication
devices like mobiles , ipods etc..(Refer
www.arm.com)
65
Microcontrollers from different manufacturers
•Atmel •Motorola
•ARM •8-bit
•Intel •68HC05
•8-bit •68HC08
•8XC42 •68HC11
•MCS48 •16-bit
•MCS51 •68HC12
•8xC251 •68HC16
•16-bit •32-bit
•MCS96 •683xx
•MXS296 •Texas Instruments
•National Semiconductor •TMS370
•COP8 •MSP430
•Microchip •Zilog
•12-bit instruction PIC •Z8
•14-bit instruction PIC •Z86E02
•PIC16F84
•16-bit instruction PIC
•NEC 66
MCS-51 “Family” of Microcontollers
Feature 8031 8051 8052 8751
ROM NO 4kB 8kB 4kB UV Eprom
TIMERS 2 2 3 2
I/O PINS 32 32 32 32
SERIAL PORTS 1 1 1 1
INTERRUPT 6 6 8 6
SOURCES
67
Microcontroller Architectures
Memory
0
Address Bus
Program +
CPU Data Bus Data Von Neumann
2n
Architecture
Memory
0
Address Bus
Program
CPU Fetch Bus Harvard
Address Bus 0
Architecture
Data Bus Data
68
Important Features of 8051
• 4K bytes ROM
• 128 bytes RAM
• Four 8-bit I/O ports
• Two 16-bit timers
• Serial interface
• 64K external code memory space
• 64K data memory space
69
“Original” 8051 Microcontroller
Oscillator 4096 Bytes 128 Bytes Two 16 Bit
and timing Program Memory Data Memory Timer/Event
(ROM) (RAM) Counters
71
8051 CPU Registers
A (8-bit Accumulator)
B (8-bit register for Mul &Div)
PSW (8-bit Program Status Word)
SP (8-bit Stack Pointer)
PC (16-bit Program Counter)
DPTR (16-bit Data Pointer)
72
Special Function Registers
DATA registers
CONTROL registers
•Timers
•Serial ports
•Interrupt system
74
Contd…
75
PSW REGISTER
76
Memory mapping in 8051
0FFFH
DS5000-32
1FFFH
8051
8752 7FFFH
77
RAM memory space allocation in the 8051
7FH
30H
2FH
Bit-Addressable RAM
20H
1FH Register Bank 3
18H
17H
Register Bank 2
10H
0FH Register Bank 1 (Stack)
08H
07H
Register Bank 0
00H
78
PORTS OF 8051
• 8051 has 4 Ports. Port 0, Port1, Port2 , Port3
■ Port 0 is a dual purpose port, it is located from
pin 32 to pin 39 (8 pins). To use this port as both
input/output ports each pin must be connected
externally to a 10 k ohm pull-up resistor.This is
because Port 0 is an open drain.
Simple ex: MOV A, #22
BACK MOV P0 ,A
ACALL DELAY
CPL A
SJMP BACK
79
Port 0 with Pull-Up Resistors
80
Ports..
• Port 1 is a dedicated I/O port from pin 1 to pin
8.Upon reset it is configured as outport. It is
generally used for interfacing to external device
thus if you need to connect to switches or LEDs,
you could make use of these 8 pins,but it doesn’t
need any pull-up resistors as it is having
internally
• Like port 0, port 2 is a dual-purpose port.(Pins 21
through 28) It can be used for general I/O or as
the high byte of the address bus for designs with
external code memory.Like P1 ,Port2 also doesn’t
require any pull-up resistors
81
Ports contd…
83
I/O Port structure
• The internal circuitry for the I/O port is shown in
the next slide
• If you want to read in from a pin, you must first
give a logic ‘1’ to the port latch to turn off the
FET otherwise the data read in will always be
logic ‘0’.
• When you write to the port you are actually
writing to the latch e.g. a logic 0 given to the
latch will be inverted and turn on the FET which
cause the port pin to be connected to Gnd (logic
0).
84
I/O Port contd…
85
Timers /Counters
• The 8051 has 2 timers/counters:
• Timer/Counter 0
• Timer/Counter 1
They can be used as
1. The Timer :Uused as a time delay generator.
– The clock source is the internal crystal frequency of the
8051.
2. An event counter.
– External input from input pin to count the number of
events on registers.
– These clock pulses cold represent the number of people
passing through an entrance, or the number of wheel
rotations, or any other event that can be converted to
pulses.
86
Timer
• Set the initial value of registers
• Start the timer and then the 8051 counts up.
• Input from internal system clock (machine cycle)
• When the registers equal to 0 and the 8051 sets a
bit to denote time out
8051
P2 P1 to
Set LCD
Timer 0 TH0
TL0
87
Counter
TH0
P1 to
TL0
LCD
P3.4
a switch T0
88
Registers Used in Timer/Counter
• 8051 has two 16-bit Timer registers ,Timer 0 &
Timer 1.
• As 8051 has 8-bit architecture , each Timer
register is treated as two 8-bit registers namely
TH0, TL0, TH1, TL1.
• One 8-bit mode register -TMOD.
• One 8-bit control register-TCON.
89
TMOD Register
(MSB) (LSB)
GATE C/T M1 M0 GATE C/T M1 M0
Timer 1 Timer 0
M1 M0 Mode Operation
0 0 0 13-bit timer mode 8-bit THx + 5-bit TLx (x= 0 or 1)
93
Let us understand the working of Timer Mode 1
• For this , let us consider timer 0 as an example.
• 16-bit timer (TH0 and TL0)
• TH0-TL0 is incremented continuously when TR0 is set
to 1. And the 8051 stops to increment TH0-TL0 when
TR0 is cleared.
• The timer works with the internal system clock. In
other words, the timer counts up each machine cycle.
• When the timer (TH0-TL0) reaches its maximum of
FFFFH, it rolls over to 0000, and TF0 is raised.
• Programmer should check TF0 and stop the timer 0.
94
Steps of Mode 1
1. Choose mode 1 timer 0
– MOV TMOD,#01H
2. Set the original value to TH0 and TL0.
– MOV TH0,#FFH
– MOV TL0,#FCH
3. You better to clear the TF: TF0=0.
– CLR TF0
4. Start the timer.
– SETB TR0
95
Mode 1 contd…
TR0=1 TR0=0
Start timer TH0 TL0
Stop timer
TF = 0 TF = 0 TF = 0 TF = 0 TF = 1
TF Monitor TF until TF=1
96
Mode 1 contd…
6. When TH0-TL0 rolls over from FFFFH to 0000,
the 8051 set TF0=1.
TH0-TL0= FFFE H, FFFF H, 0000 H (Now
TF0=1)
7. Keep monitoring the timer flag (TF) to see if it is
raised.
AGAIN: JNB TF0, AGAIN
8. Clear TR0 to stop the process.
CLR TR0
9. Clear the TF flag for the next round.
CLR TF0
97
TCON Register
98
Tcon contd…
99
Equivalent Instructions for the Timer Control
Register
For timer 0
SETB TR0 = SETB TCON.4
CLR TR0 = CLR TCON.4
101
Square-wave of 66%duty cycle.
HERE : SETB P1.0 ( Make bit of Port 0 High)
LCALL DELAY
LCALL DELAY
CLR P1.0
LCALL DELAY
SJMP HERE : Keep doing it
102
Square-wave generation using Timer
❑ Square wave of 50% duty on P1.5
❑ Timer 0 is used
; each loop is a half clock
MOV TMOD,#01 ;Timer 0,mode 1(16-bit)
HERE: MOV TL0,#0F2H ;Timer value = FFF2H
MOV TH0,#0FFH
CPL P1.5
ACALL DELAY
SJMP HERE
DELAY:
SETB TR0 ;start the timer 0
AGAIN: JNB TF0,AGAIN
CLR TR0 ;stop timer 0
CLR TF0 ;clear timer 0 flag
RET
103
8051- SERIAL COMMUNICATION
104
Basics of serial communication
105
Types of Serial communications
106
RxD and TxD pins in the 8051
• The 8051 has two pins for transferring and
receiving data by serial communication. These
two pins are part of the Port3(P3.0 &P3.1)
• These pins are TTL compatible and hence they
require a line driver to make them RS232
compatible
• Max232 chip is one such line driver in use.
• Serial communication is controlled by an 8-bit
register called SCON register,it is a bit
addressable register.
107
Interfacing to PC
108
SCON (Serial control) register
109
SM0 , SM1
• These two bits of SCON register determine the framing
of data by specifying the number of bits per character
and start bit and stop bits. There are 4 serial modes.
SM0 SM1
0 0 Serial Mode 0
0 1 Serial Mode 1, 8 bit data,
1 stop bit, 1 start bit
1 0 Serial Mode 2
1 1 Serial Mode 3
110
REN, TI, RI
• REN (Receive Enable) also referred as SCON.4.
When it is high,it allows the 8051 to receive
data on the RxD pin. So to receive and transfer
data REN must be set to 1.When REN=0,the
receiver is disabled. This is achieved as below
SETB SCON.4
& CLR SCON.4
111
contd…
• TI (Transmit interrupt) is the D1 bit of SCON register.
When 8051 finishes the transfer of 8-bit character, it
raises the TI flag to indicate that it is ready to transfer
another byte. The TI bit is raised at the beginning of
the stop bit.
113
Steps in executing an interrupt
• Upon receiving the interrupt signal the
Microcontroller , finish current instruction
and saves the PC on stack.
• Jumps to a fixed location in memory
depending on type of interrupt
• Starts to execute the interrupt service routine
until RETI (return from interrupt)
• Upon executing the RETI the microcontroller
returns to the place where it was interrupted.
Get pop PC from stack
114
Interrupt Sources
• Original 8051 has 6 sources of interrupts
– Reset
– Timer 0 overflow
– Timer 1 overflow
– External Interrupt 0
– External Interrupt 1
– Serial Port events (buffer full, buffer empty, etc)
• Enhanced version has 22 sources
– More timers, programmable counter array, ADC,
more external interrupts, another serial port
(UART)
115
Interrupt Vectors
• Each interrupt has a specific place in code
memory where program execution (interrupt
service routine) begins.
• External Interrupt 0: 0003h
• Timer 0 overflow: 000Bh
• External Interrupt 1: 0013h
• Timer 1 overflow: 001Bh
• Serial : 0023h
• Timer 2 overflow(8052+) 002bh
116
Interrupt Enable Register
117
• EA : Global enable/disable.
• --- : Undefined.
• ET2 : Enable Timer 2 interrupt.
• ES : Enable Serial port interrupt.
• ET1 : Enable Timer 1 interrupt.
• EX1 :Enable External 1 interrupt.
• ET0 : Enable Timer 0 interrupt.
• EX0 : Enable External 0 interrupt.
118
Enabling and disabling an interrupt
❑By bit operation
119
Interrupt Priorities
• What if two interrupt sources interrupt at the same
time?
• The interrupt with the highest PRIORITY gets
serviced first.
• All interrupts have a power on default priority order.
1. External interrupt 0 (INT0)
2. Timer interrupt0 (TF0)
3. External interrupt 1 (INT1)
4. Timer interrupt1 (TF1)
5. Serial communication (RI+TI)
• Priority can also be set to “high” or “low” by IP reg.
120
Interrupt Priorities (IP) Register
--- --- PT2 PS PT1 PX1 PT0 PX0
IP.7: reserved
IP.6: reserved
IP.5: Timer 2 interrupt priority bit (8052 only)
IP.4: Serial port interrupt priority bit
IP.3: Timer 1 interrupt priority bit
IP.2: External interrupt 1 priority bit
IP.1: Timer 0 interrupt priority bit
IP.0: External interrupt 0 priority bit
121
Interrupt Priorities Example
--- --- PT2 PS PT1 PX1 PT0 PX0
123
Applications of Microcontrollers
124
Simple Interfacing Examples
125
Seven segment Interfacing
126
Traffic light controller
127
128
Closed loop control system-Temperature
control example
129
130
131
132
133
134
135
136
137
Recent Wonders
138
Recent Wonders contd ........
139
Recent Wonders contd ........
140
Books that have helped me to understand the
Microcontrollers & embedded systems
141
5) Sewart. J.W. The 8051 Microcontroller Hardware,
Software and Interfacing – Prentice Hall
6) Ayala Kenneth, The 8051 Microntroller –
Architecture, Programming and Applications –
Delmar Publ.
7) Ajay Deshmukh, Microcontrollers – TATA McGraw
Hill
8) Rajkamal, Microcontrollers - Architecture,
Programming – Pearson Publ.
9) Myke Predko, Programming the 8051 Microcontroller –
McGraw Hill
10) Michael J. Pont, Embedded C - Addison Wesely
Publ.
142
Useful websites
143
Useful websites contd…
1. http://www.eg3.com
2. http://www.ARM.MCU.com
3. http://www.mcjournal.com
4. http://www.iar.com
5. http://http://www.embedded.com
6. http://www.powersoftsystems.com
144
Epilogue
• The woods are lovely, dark and deep,
But I have promises to keep,
And miles to go before I sleep,
And miles to go before I sleep.
---- Robert Frost
• GOOD LUCK!
145