You are on page 1of 68

WELCOME

TO
TRAINING PROGRAMME
ON
8051 MICROCONTOLLER
&
APPLICATIONS
September 1-2,2008

DEPARTMENT OF ECE
MVGR COLLEGE OF ENGINEERING
CHINTALAVALASA,VIZIANAGARAM

Contents

Introduction to microcontrollers
8051 microcontroller architecture
Assembly language programming of 8051
8051 interfacing
KEIL-IDE: Overview
Programming in KEIL-IDE Vision 3
Applications
2

Objectives
Participants will be able to
Know about microcontrollers
Understand 8051 architecture
Program the 8051 Microcontroller
Develop software for embedded systems
with the help of KEIL-IDE
Design small scale Embedded systems
3

Plan?
Day 1

Session 1: Introduction to microcontrollers


Session 2: 8051 architecture: an overview
Session 3: KEIL-IDE Vision 3: an overview
Session 4: LAB1: Programming Basics

Day 2
Session 1:Programming 8051 internals
Session2 :Interfacing 8051
Session3&4 : LAB2: Programming 8051 in KEIL
4

DAY 1
SESSION 1

Contents:
Microcontrollers Microcontrollers
Why should we study them?
Where they are being used?
vs. Microprocessor
Need for them
What is it?
Blah .blahblah
6

Why we should study microprocessor or


microcontroller?
They are in curriculum
They are used in personal computers

If the above are only reasons, see the statistics


where they are being used

They are being used in Embedded systems


Embedded system means the processor is embedded
into that application.
An embedded product uses a microprocessor or
microcontroller to do one task only.
In an embedded system, there is only one application
software that is typically burned into ROM.
Example printer, keyboard, video game player

A short list of embedded systems


Anti-lock brakes
Auto-focus cameras
Automatic teller
machines
Automatic toll systems
Automatic transmission
Avionic systems
Battery chargers
Camcorders
Cell phones
Cell-phone base
stations
Cordless phones
Cruise control
Curbside check-in
systems
Disk drives

Digital cameras
Electronic card readers
Portable video games
Printers
Satellite phones
Scanners
Smart ovens/dishwashers
Speech recognizers
Stereo systems
Teleconferencing systems
Televisions
Temperature controllers
Theft tracking systems
TV set-top boxes
VCRs, DVD players
Video game consoles
Video phones

Electronic instruments
Electronic toys/games
Factory control
Fax machines
Fingerprint identifiers
Home security systems
Life-support systems
Medical testing systems
Modems
MPEG decoders
Network cards
Network
switches/routers
On-board navigation
Pagers
Photocopiers
Washers and dryers
9

And the list goes on and on

10

General-purpose microprocessor

CPU for Computers


No RAM, ROM, I/O on CPU chip itself
Example Intels x86, Motorolas 680x0

CPU
GeneralPurpose
Microprocessor

Many chips on mothers board

Data Bus

RAM

ROM

I/O
Port

Timer

Serial
COM
Port

Address Bus
General-Purpose Microprocessor System
11

Microcontroller :
A smaller computer
On-chip RAM, ROM, I/O ports...
Example Motorolas 6811, Intels 8051, Zilogs Z8 and PIC 16X

CPU
I/O
Port

RAM ROM
Serial
Timer COM
Port

A single chip
Microcontroller
12

Microprocessor vs. Microcontroller


Microprocessor
CPU is stand-alone, RAM,
ROM, I/O, timer are separate
designer can decide on the
amount of ROM, RAM and
I/O ports.
expensive
versatility
general-purpose

Microcontroller
CPU, RAM, ROM, I/O and
timer are all on a single chip
fix amount of on-chip ROM,
RAM, I/O ports
for applications in which cost,
power and space are critical
single-purpose

13

Need for microcontroller


for applications in which cost, power and
space are critical
Example : TV remote control

14

What is a Microcontroller?
A microcontroller is an entire computer
manufactured on a single chip
They have a high concentration of on-chip
facilities such as

serial ports,
parallel input/output ports,
Timers & counters,
interrupt control,
analog-to-digital converters,
random access memory, read only memory, etc.
15

What are its Advantages


Cost is very less
Around Rs.100

Consumes less power


Typically milliwatts while operating
Nanowatts in sleeping and idle mode
Making it ideal for low power embedded systems

Low space
Needs no extra circuitry
Possible to build handy systems
16

Common Microcontrollers
Atmel
ARM
Intel
8-bit
8XC42
MCS48
MCS51
8xC251
16-bit
MCS96
MXS296
National Semiconductor
COP8
Microchip
12-bit instruction PIC
14-bit instruction PIC
PIC16F84
16-bit instruction PIC
NEC

Motorola
8-bit
68HC05
68HC08
68HC11
16-bit
68HC12
68HC16
32-bit
683xx
Texas Instruments
TMS370
MSP430
Zilog
Z8
Z86E02

17

Three criteria in Choosing a Microcontroller


1.

meeting the computing needs of the task efficiently and cost


effectively
speed, the amount of ROM and RAM, the number of I/O ports
and timers, size, packaging, power consumption
easy to upgrade
cost per unit
2. availability of software development tools
assemblers, debuggers, C compilers, emulator, simulator,
technical support
3. wide availability and reliable sources of the microcontrollers.

18

Microcontroller Architectures
Address Bus
CPU

Memory
Program
or Data

Data Bus
2n

Address Bus
CPU

Fetch Bus

Memory
Program

Address Bus 0
Data Bus

Von Neumann
Architecture

Harvard
Architecture

Data
19

Embedded Products Using


Microcontrollers
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
20

Embedded Products Using


Microcontrollers
Office
Telephones, computers, security systems, fax
machines, microwave, copier, laser printer,
color printer, paging

21

Embedded Products Using


Microcontrollers
Auto
Trip computer, engine control, air bag, ABS,
instrumentation, security system, transmission
control, entertainment, climate control, cellular
phone, keyless entry

22

Blahblahblah
History of microcontrollers
Early days:
Transistor to microprocessor

Medieval days:
First microcontroller was introduced by Intel in late 1970s.ie
8051

Present :
more than 100 companies are producing microcontrollers in
different variants

Future
64/128 bit microcontrollers with more on chip circuitry with
less power
Size may reduce to nano scale
23

DAY 1
End of session 1

24

DAY 1
Session 2

25

Contents

8051 architecture
Comparison of the 8051 Family Members
Pin Description
SFR
RAM
ROM
Assembly language basics
Instruction set
26

About 8051
It was the first microcontroller developed
by Intel in early 80s
It is an 8 bit microcontroller & a 40 pin IC
Now produced by many companies in
many variations
The most popular microcontroller about
50% of market share
27

More about 8051

Harvard architecture single chip microcontroller (physically


separate storage and signal pathways for their instructions and
data )
Typically contains
Processor (CPU).
4K Bytes ROM
128 Bytes RAM
two timer/counters (16 bit)
A serial port
4 general purpose parallel input/output port
Interrupt controller

The 8051 can address 64K of external data memory and 64K of
External program memory.
28

Block Diagram
External interrupts
Interrupt
Control

On-chip
ROM for
program
code

Timer/Counter

On-chip
RAM

Timer 1
Timer 0

Counter
Inputs

CPU

OSC

Bus
Control

4 I/O Ports

P0 P1 P2 P3

Serial
Port

TxD RxD

Address/Data
29

Comparison of the 8051 Family Members

Feature
8051
ROM (program space in bytes) 4K
RAM (bytes)
128
Timers
2
I/O pins
32
Serial port
1
Interrupt sources
6

8052
8K
256
3
32
1
8

8031
0K
128
2
32
1
6

30

31

Pin Description of the 8051


DIP
P1.0
P1.1
P1.2
P1.3
P1.4
P1.5
P1.6
P1.7
RST
(RXD)P3.0
(TXD)P3.1
(INT0)P3.2
(INT1)P3.3
(T0)P3.4
(T1)P3.5
(WR)P3.6
(RD)P3.7
XTAL2
XTAL1
GND

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

8051
(8031)

40
39
38
37
36
35
34
33
32
31
30
29
28
27
26
25
24
23
22
21

Vcc
P0.0(AD0
P
) 0.1(AD1)
P0.2(AD2
P
) 0.3(AD3)
P0.4(AD4)
P0.5(AD5)
P0.6(AD6)
P0.7(AD7)
EA/VPP
ALE/PROG
PSEN
P2.7(A15)
P2.6(A14
)P2.5(A13
P
) 2.4(A12
)P2.3(A11)
P2.2(A10)
P2.1(A9)
P2.0(A8)

32

Pins of 8051 1/4


Vcc pin 40
Vcc provides supply voltage to the chip.
The voltage source is +5V.
GND pin 20 ground
XTAL1 and XTAL2 pins 19,18
These 2 pins provide external clock.
Way 1 using a quartz crystal oscillator
Way 2 using a TTL oscillator
Example 4-1 shows the relationship between XTAL and the
machine cycle.

33

Pins of 8051 2/4


RST pin 9 reset
It is an input pin and is active high normally low .
The high pulse must be high at least 2 machine cycles.
It is a power-on reset.
Upon applying a high pulse to RST, the microcontroller will
reset and all values in registers will be lost.
Reset values of some 8051 registers
Way 1 Power-on reset circuit
Way 2 Power-on reset with debounce

34

Pins of 8051 3/4

EA pin 31 external access


There is no on-chip ROM in 8031 and 8032 .
The EA pin is connected to GND to indicate
the code is stored externally.
PSEN ALE are used for external ROM.
For 8051, /EA pin is connected to Vcc.
PSEN pin 29 program store enable
This is an output pin and is connected to the
OE pin of the ROM.
35

Pins of 8051 4/4


ALE pin 30 address latch enable
It is an output pin and is active high.
8051 port 0 provides both address and data.
The ALE pin is used for de-multiplexing the address and data by
connecting to the G pin of the 74LS373 latch.
I/O port pins
The four ports P0, P1, P2, and P3.
Each port uses 8 pins.
All I/O pins are bi-directional.

36

Figure 4-2 (a). XTAL Connection to 8051

Using a quartz crystal oscillator


We can observe the frequency on the XTAL2 pin.
C2
XTAL2
30pF
C1
XTAL1
30pF
GND

37

Figure 4-2 (b). XTAL Connection to an External Clock Source

N
C

Using a TTL oscillator


XTAL2 is unconnected.

EXTERNAL
OSCILLATOR
SIGNAL

XTAL2

XTAL1

GND

38

Example :
Find the machine cycle for
(a) XTAL = 11.0592 MHz
(b) XTAL = 16 MHz.
Solution:
(a) 11.0592 MHz / 12 = 921.6 kHz;
machine cycle = 1 / 921.6 kHz = 1.085 s
(b) 16 MHz / 12 = 1.333 MHz;
machine cycle = 1 / 1.333 MHz = 0.75 s

39

RESET Value of Some 8051 Registers:

Register
PC
ACC
B
PSW
SP
DPTR
RAM are all zero.

Reset Value
0000
0000
0000
0000
0007
0000

40

Figure 4-3 (a). Power-On RESET Circuit


Vcc

+
10 uF

31
30 pF

8.2 K
30 pF

11.0592 MHz

19
18

EA/VPP
X1
X2

9 RST

41

Figure 4-3 (b). Power-On RESET with Debounce


Vcc

31
10 uF

30 pF

EA/VPP
X1

X2
RST

8.2 K

42

Pins of I/O Port


The 8051 has four I/O ports
Port 0 pins 32-39 P0 P0.0 P0.7
Port 1 pins 1-8 P1 P1.0 P1.7
Port 2 pins 21-28 P2 P2.0 P2.7
Port 3 pins 10-17 P3 P3.0 P3.7
Each port has 8 pins.
Named P0.X X=0,1,...,7 , P1.X, P2.X, P3.X
Ex P0.0 is the bit 0 LSB of P0
Ex P0.7 is the bit 7 MSB of P0
These 8 bits form a byte.
Each port can be used as input or output (bi-direction).

43

Registers
A
B
R0

DPTR

DPH

DPL

R1
R2

PC

PC

R3
R4

Some 8051 16-bit Register

R5
R6
R7
Some 8-bitt Registers of
the 8051

44

Memory mapping in 8051


ROM memory map in 8051 family
4k
0000H

8k

32k
0000H

0000H

0FFFH
DS5000-32
8751
AT89C51

1FFFH

from Atmel Corporation

8752
AT89C52

7FFFH

from Dallas Semiconductor

45

RAM memory space allocation in the 8051


7FH
Scratch pad RAM

30H
2FH
Bit-Addressable RAM
20H
1FH
18H
17H
10H
0FH
08H
07H
00H

Register Bank 3
Register Bank 2
Register Bank 1( Stack)
Register Bank 0

46

8051 Flag bits and the PSW register


PSW Register
CY

AC

F0

RS1

RS0

OV

Carry flag
Auxiliary carry flag
Available to the user for general purpose
Register Bank selector bit 1
Register Bank selector bit 0
Overflow flag
User define bit
Parity flag Set/Reset odd/even parity

RS1

RS0

Register Bank

--

PSW.7
PSW.6
PSW.5
PSW.4
PSW.3
PSW.2
PSW.1
PSW.0

CY
AC
-RS1
RS0
OV
-P

Address

00H-07H

08H-0FH

10H-17H

18H-1FH

47

Review Binary/Hexadecimal
Decima
l

Binary

Hexadecimal

0000

0001

0010

0011

0100

0101

0110

0111

1000

1001

10

1010

11

1011

12

1100

13

1101

14

1110

15

1111

Conversions:
1000 1110 (binary)
8 E (hex)
Notations for hex:
0x8E
8Eh
8E16
0A2H

48

Some Simple Instructions


MOV dest,source
MOV
MOV
MOV
MOV

A,#72H
A, #r
R4,#62H
B,0F9H

MOV
MOV
MOV

DPTR,#7634H
DPL,#34H
DPH,#76H

MOV

P1,A

; dest = source
;A=72H
;A=r OR 72H
;R4=62H
;B=the content of F9th byte of RAM

;mov A to port 1
49

Bit Addressable Memory


2F

7F

78

2E
2D
2C

20h 2Fh (16 locations X


8-bits = 128 bits)
Bit addressing:
mov C, 1Ah
or
mov C, 23h.2

2B
2A
29
28
27
26
25
24
23

1A

22
21
20

10
0F
07

08
06

05

04

03

02

01

00
50

Addressing Modes

Immediate
Register
Direct
Register Indirect
Indexed

51

Immediate Addressing Mode


MOV
MOV
MOV
MOV
MOV

A,#65H
A,#A
R6,#65H
DPTR,#2343H
P1,#65H

52

Register Addressing Mode


MOV
ADD
MOV

Rn, A
A, Rn
DPL, R6

MOV
MOV

DPTR, A
Rm, Rn

;n=0,..,7

53

Direct Addressing Mode


Although the entire of 128 bytes of RAM can be accessed using direct
addressing mode, it is most often used to access RAM loc. 30 7FH.
MOV
MOV
MOV
MOV

R0, 40H
56H, A
A, 4
6, 2

; MOV A, R4
; copy R2 to R6
; MOV R6,R2 is invalid !

SFR register and their address


MOV
MOV
MOV

0E0H, #66H
0F0H, R2
80H,A

; MOV A,#66H
; MOV B, R2
; MOV P1,A

Bit Addressable
Page 359,360

54

Register Indirect Addressing Mode

In this mode, register is used as a pointer to the data.

MOV

A,@Ri

MOV

@R1,B

; move content of RAM loc. Where address is held by Ri into A


( i=0 or 1 )

In other word, the content of register R0 or R1 is sources or target in MOV, ADD and SUBB
instructions.
Example:
Write a program to copy a block of 10 bytes from RAM location starting at 37h to RAM
location starting at 59h.
Solution:
MOV R0,37h
MOV R1,59h
MOV R2,10
L1: MOV A,@R0
MOV @R1,A
INC R0
INC R1
DJNZ R2,L1

; source pointer
; dest pointer
; counter

55

Indexed Addressing Mode


This mode is widely used in accessing data elements of lookup table entries located in the program (code) space ROM at
the 8051
MOVCA,@A+DPTR
MOVC
A,@A+PC
A= content of address A +DPTR (or A+PC) from ROM
Note:
Because the data elements are stored in the program (code )
space ROM of the 8051, it uses the instruction MOVC instead
of MOV. The C means code.
MOVC is normally used with internal or external ROM and
can address 4K of internal or 64K of external code

56

Accessing external RAM

It is possible to expand RAM by connecting it externally


Opcodes that access this external memory always use indirect
addressing to specify the external memory
MOVX
MOVX
MOVX
MOVX

A,@Rp
A,@DPTR
@DPTR,A
@Rp,A

Rp can address 256 bytes;DPTR can address 64K bytes


NOTE:
Rp can be either R0 or R1

57

ADD A, Source

;A=A+SOURCE

ADD

A,#6

;A=A+6

ADD

A,R6

;A=A+R6

ADD

A,6

;A=A+[6] or A=A+R6

ADD

A,0F3H

;A=A+[0F3H]

58

Example:
MOV
A,#88H
ADD
A,#93H
88
+93
---11B
CY=1
AC=0

10001000
+10010011
-------------00011011
P=0

Example:
MOV
A,#38H
ADD
A,#2FH

Example:
MOV
A,#9CH
ADD
A,#64H
9C
+64
---100
CY=1
AC=1

10011100
+01100100
-------------00000000
P=0

38
00111000
+2F +00101111
---- -------------67
01100111
CY=0
AC=1
P=1
59

SUBB

A,source ;A=A-source-CY

SETB C
SUBB A,R5

;CY=1
;A=A-R5-1

ADDC

A,source ;A=A+source+CY

SETB
ADDC

C
A,R5

;CY=1
;A=A+R5+1

60

MUL & DIV


MUL
MOV
MOV
MUL

AB
A,#25H
B,#65H
AB

DIV
MOV
MOV
DIV

AB
A,#25
B,#10
AB

;B|A = A*B

;25H*65H=0E99
;B=0EH, A=99H
;A = A/B, B = A mod B

;A=2, B=5
61

Stack in the 8051


The register used to access
the stack is called SP (stack
pointer) register.

7FH
Scratch pad RAM
30H

The stack pointer in the


8051 is only 8 bits wide,
which means that it can take
value 00 to FFH. When
8051 powered up, the SP
register contains value 07.

2FH
Bit-Addressable RAM
20H
1FH
18H
17H
10H
0FH
08H
07H
00H

Register Bank 3
Register Bank 2
Register Bank 1( Stack)
Register Bank 0

62

Example:
MOV
MOV
MOV
PUSH
PUSH
PUSH

R6,#25H
R1,#12H
R4,#0F3H
6
1
4

0BH

0BH

0BH

0BH

0AH

0AH

0AH

0AH

F3

09H

09H

09H

12

09H

12

08H

08H

08H

25

08H

25

Start SP=07H

25

SP=08H

SP=09H

SP=08H

63

SETB
CLR
SETB
SETB
SETB
SETB
SETB

bit
bit
C
P0.0
P3.7
ACC.2
05

; bit=1
; bit=0
; CY=1
;bit 0 from port 0 =1
;bit 7 from port 3 =1
;bit 2 from ACCUMULATOR =1
;set high D5 of RAM loc. 20h

Note:

Bit Addressable
Page 359,360

CLR instruction is as same as SETB


i.e:
CLR
C
;CY=0
But following instruction is only for CLR:
CLR
A
;A=0
64

DEC
INC

byte
byte

;byte=byte-1
;byte=byte+1

INC
R7
INC
40H
INC
DPTR
DEC
A
DEC
40H
NOTE: DEC DPTR is not possible

CPL

; [40]=[40]-1

;1s complement

Example:
L01:

MOV
CPL
MOV
ACALL
SJMP

A,#55H ;A=01010101 B
A
P1,A
DELAY
L01

CALL

NOP
65

Assembler Directives
ORG (origin)
Is to indicate beginning of the address
ORG 0000H or ORG 8000H

END
This indicates the assembler the end of the source file
It is last line of program

EQU (equate)
This is used to define a constant without occupying
memory location
COUNTEQU 25h
. .
MOV R3,#COUNT

66

Structure of Assembly language


and Running an 8051 program
EDITOR
PROGRAM

ORG
MOV
MOV
MOV
ADD
ADD
HERE: SJMP
END

0H
R5,#25H
R7,#34H
Myfile.lst
A,#0
A,R5
A,#12H
HERE

Myfile.asm
ASSEMBLER
PROGRAM
Other obj file
Myfile.obj
LINKER
PROGRAM

Myfile.abs
OH
PROGRAM
Myfile.hex

67

DAY1
END OF SESSION 2
THANK YOU

68

You might also like