Sie sind auf Seite 1von 69

8051 Microcontroller

The necessary tools for a


microprocessor/controller

CPU: Central Processing Unit


I/O: Input /Output
Bus: Address bus & Data bus
Memory: RAM & ROM
Timer
Interrupt
Serial Port
Parallel Port

Microprocessors
General-purpose microprocessor

CPU for Computers


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

CPU
GeneralPurpose
Microprocessor

Many chips on mothers board

Data Bus

RAM

ROM

Address Bus

General-Purpose Microprocessor System

I/O
Port

Timer

Serial
COM
Port

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

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.
Expansive
general-purpose
Used in
desktops,laptops,notebooks
etc..

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
Used in mobile phones, digital
cameras, microwave oven,
washing machines

Pin Description of the 8051


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

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

Vcc
P0.0(AD0)
P0.1(AD1)
P0.2(AD2)
P0.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)
P2.4(A12)
P2.3(A11)
P2.2(A10)
P2.1(A9)
P2.0(A8)

8051
Schematic
Pin out

8051 Architecture

IMPORTANT PINS
I/O Ports

One of the most useful features of the 8051 is that it contains four
I/O ports (P0 - P3)

Port 0 pins 32-39 P0 P0.0 P0.7


8-bit R/W - General Purpose I/O
Or acts as a multiplexed low byte address and data bus for external memory design

Port 1 pins 1-8 P1 P1.0 P1.7


Only 8-bit R/W - General Purpose I/O

Port 2 pins 21-28 P2 P2.0 P2.7


8-bit R/W - General Purpose I/O
Or high byte of the address bus for external memory design

Port 3 pins 10-17 P3 P3.0 P3.7


General Purpose I/O
Each bit in port 3 can be programmed for alternate use.

Each port can be used as input or output (bi-direction)

Port 3 Alternate Functions

Pins of 8051
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 clock pulses by which all internal
operations can be synchronized.
Quartz crystal oscillator is used
Typical range from 1MHz to 16 MHz.

Pins of 8051
RST pin 9 reset
Upon applying a high pulse to RST, the microcontroller will
reset and all values in registers will be lost.
Register bank 0 is selected and all bits in PSW are 0

RESET Value of Some 8051 Registers:


Register

Reset Value

PC

0000

ACC

0000

0000

PSW

0000

SP

0007

DPTR

0000

RAM are all zero

Pins of 8051
EA pin 31 external access
-- The EA (External Access) pin is used to control the
internal or external memory access.
The signal 0 is for external memory access and signal 1
for internal memory access
PSEN pin 29 program store enable
If the memory access is for a byte of program code in
the external ROM, the pin will go low to enable the ROM
to place a byte of program code on the data bus.

Pins of 8051
ALE pin 30 address latch enable
It is an output pin and is active high.
Activates the port 0 joined with port 2 to provide 16 bit external
address bus to access the external memory.
The ALE multiplexes the P0.
1 for latching address on P0 as A0-A7 in the 16 bit address bus, 0 for
latching P0 as data I/O.

On-Chip Memory
Internal RAM

Summary of the 8051 on-chip data memory


(RAM)

Summary of the 8051 on-chip data memory


(Special Function Registers)

TCON

TMOD
Both counters use the TMOD (Timer Mode) register for
configuration.

Stack in the 8051


The register used to access the stack is called SP (stack pointer)
register.
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.

Example:
MOV
MOV
MOV
PUSH
PUSH
PUSH

R6,#25H
R1,#12H
R4,#0F3H
R6
R1
R4

0BH

0BH

0BH

0BH

0AH

0AH

0AH

0AH

F3

09H

09H

09H

12

09H

12

08H

08H

08H

25

08H

25

SP=07H

25
SP=08H

SP=09H

SP=0AH

Example:
MOV
MOV
MOV
PUSH
PUSH
PUSH
POP
POP
POP

0BH

R6,#25H
R1,#12H
R4,#0F3H
R6
R1
R4
R4
R1
R6

0BH

0BH

0BH

0AH

0AH

0AH
09H

0AH

F3

09H

12

09H

12

09H

08H

25

08H

25

08H

SP=0AH

SP=09H

25
SP=08H

08H
SP=07H

Data Pointer Register


(DPTR)

Interrupt
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.

Interrupts
Interrupt

Program

time t

Interrupts
Interrupt

Program

Program
Interrupt Service Routine

time t

Interrupts
Interrupt

Program

Save
Context

Program
Interrupt
Service
Routine

Restore
Context

time t

34

Interrupt Enable(IE) Register

Interrupt Priority (IP) Register

The 8051
Programming

INTRODUCTION
An instruction is an order or command given
to a processor by a computer program. All
commands are known as instruction set and set
of instructions is known as program.
8051 have in total 111 instructions, i.e. 111
different words available for program writing.

Instruction Format
Where first part describes WHAT should be
done (Opcode), while other explains HOW to
do it(operand).
Ex: MOV
R6, #25H
Opcode Operands
The latter part can be a data (binary number)
or the address at which the data is stored.

Addressing Modes
Addressing modes specifies where the data
(operand) is. They specify the source or
destination of data (operand) in several
different ways, depending upon the situation.
Addressing modes are used to know where the
operand located is.

Addressing Modes
There are 4 types of addressing modes:
1.
2.
3.
4.

Immediate addressing.
Register addressing.
Direct addressing.
Register indirect addressing.

Immediate Addressing Mode


In immediate addressing mode, the data is given
with the instruction itself.
In this case; the data to be stored in memory
immediately follows the opcode.
MOV Rr, #n: copy the 8 bit number n into register Rr.
MOV A, #n: copy the 8 bit number n into accumulator.
MOV DPTR, #nn: copy the 16 bit number nn into DPTR.

Immediate Addressing Mode


For example;
1. MOV A, #25H :This instruction will move the data 25H to
accumulator.

2. MOV R4, #04H: Put the immediate 8 bit number 04H in


register R4

3. MOV DPTR, #1234H: Put the immediate 16 bit number


1234H into DPTR register.

Register Addressing Mode


In register addressing mode; the source and/or destination is
a register.
In this case data is placed in any of the 8 registers (R0-R7);
in instructions it is specified with letter Rn (where n
indicates 0 to 7).

Register Addressing Mode


Examples:
1. MOV A,R0 : Copy the data in register R0 to
Accumulator.

2. ADD R5,A : This instruction will add the contents of


register R5 with the accumulator contents.

Direct Addressing Mode


In direct addressing mode the address of memory
location containing data to be read is specified in
instruction.
In this case; address of the data is given with the
instruction itself.

Direct Addressing Mode


Examples:

Register Indirect Addressing Mode


In register indirect addressing mode; the contents of the
designated register are used as a pointer to memory.
In this case; data is placed in memory, but address of
memory location is not given directly with instruction.

Register Indirect Addressing Mode


Examples

Data Transfer Instructions


MOV dest, source
Stack instructions
PUSH byte
POP byte

dest source

;increment stack pointer,


;move byte on stack
;move from stack to byte,
;decrement stack pointer

Exchange instructions
XCH a, byte
XCHD a, byte

;exchange accumulator and byte


;exchange low nibbles of
;accumulator and byte

Data Processing Instructions


Arithmetic Instructions
Logic Instructions

Arithmetic Instructions

Add
Subtract
Increment
Decrement
Multiply
Divide

Arithmetic Instructions
Mnemonic

Description

ADD A, byte

add A to byte, put result in A

ADDC A, byte

add with carry

SUBB A, byte

subtract with borrow

INC A

increment A

INC byte

increment byte in memory

INC DPTR

increment data pointer

DEC A

decrement accumulator

DEC byte

decrement byte

MUL AB

multiply accumulator by b register

DIV AB

divide accumulator by b register

DA A

decimal adjust the accumulator

ADD

Subtract

Multiply

Division

Logic Instructions
ANL AND
ORL OR
XRL XOR
CPL Complement
CLR - clear
RL rotate left
RLC rotate left through Carry
RR rotate right
RRC rotate right through Carry
SWAP swap accumulator nibbles

Increment & Decrement

Example

8 bit addition
Start

Initialize DPTR
Bring data to registers
A&B
Add data
Transfer result to external
memory location
Stop

Addition program
ORG 8400H
MOV DPTR, #8500H
MOV A, #14H
ADD A, #23H
MOVX @DPTR, A
END

: Starting location of program


: Initialize the data pointer
: Data1 is copied into accumulator
: Data1 and data2 are added
: Result is moved into memory location

8 bit substraction
Start

Initialize DPTR
Bring data to registers
A&B
Subtract data
Transfer result to external
memory location
Stop

Subtraction program
ORG 8400H
MOV DPTR #8500H
MOV A, #23H
SUBB A, #14H
MOVX @DPTR, A
END

: Starting location of program


: Initialize the data pointer
: Data1 is copied into accumulator
: Data1 and data2 are substracted.
: Result is moved into memory location

8 bit Multiplication
Start

Initialize DPTR
Initialize accumulator with data1 & B
register with data 2
Multiply the data
Transfer result to external
memory location
Stop

Multiplication program

ORG 8400 H
MOV DPTR,#8800H
MOV 0F0H, #02H
MOV A, #05H
MUL AB
MOVX @DPTR, A
INC DPTR
MOV R0,0F0H
MOV A, R0
MOVX @DPTR, A
END

: Starting location of the program


: Initializing of the data pointer
: Data 1 is transferred into B(0F0H)
: Data 2 is transferred into A register
: Multiplying the Data 1 and Data 2
: Copy low order byte of result into 8800H
: Update the pointer to 8801H
: Copy the high order byte of result into R0
: Get high order byte of result into A register
: Copy high order byte of result into 8801H

8 bit Division
Start

Initialize DPTR
Initialize accumulator with dividend & B
register with divisor
Divide the data
Transfer result to external
memory location
Stop

Division program

ORG 8400H
MOV DPTR,#8800H
MOV A,#64H
MOV OFOH,#05
DIV AB
MOVX @DPTR,A
INC DPTR
MOV R0,0F0H
MOV A,R0
MOVX @DPTR,A
END

: Starting location of program


: Initialization the data pointer
: Dividend is loaded into register A
: Divisor is loaded into register B
: Divided the 64H by 05H
: Quotient is moved into 8800H location
: Update the pointer
: Copy the remainder into R0 register
: Copy the remainder into register A
: Remainder is copied into 8801H

Applications of 8051

Analog to Digital converters (ADC)

8051

CS:Active low input (P3.2-INT0 pin) used to activate the ADC chip.
WR(startconversion):Active low input used to inform the ADC to
start the conversion process. If CS = 0 when WR makes a low-to-high
transition(P3.3-INT1 pin), the ADC starts converting the analog input
value of Vin to an 8-bit digital number.
RD(dataenable):Active low input used to get converted data out of the
ADC chip. When CS = 0, if a high-to-low pulse is applied to the RD pin
(from T0 pin of port 3), the 8-bit digital output shows up at the D0-D7 data
pins.
.

Vref(+)andVref(-):These are the differential analog inputs where Vin =


Vref (+) - Vref (-). Often the Vref (-) pin is connected to ground and the
Vref (+) pin is used as the analog input to be converted to digital.
Vin:This is the analog input to be converted.

Digital to Analog converters


(DAC)

CS:Active low input (P3.2-INT0 pin) used to activate the DAC chip.

WR:Active low input used to inform the DAC to get data from data pins and
start the conversion process. If CS = 0 when WR makes a low-to-high
transition(P3.3-INT1 pin), the DAC starts converting the digital input to an analog
voltage.

Temperature control system


Temperature
sensor

Relay

Heater

Relay

Fan

8051

Preset temperature=23
T>23
T<23

Temperature control system


Temperature sensor is used for sensing the ambient
temperature .
The temperature is compared with a preset value
stored by the user.
If the room temperature goes beyond the preset
temperature then the fan will be ON
If the temperature goes below the preset temperature
then heater will be ON

Das könnte Ihnen auch gefallen