You are on page 1of 27

Introduction

General-purpose microprocessor
• CPU for Computers
• No RAM, ROM, I/O on CPU chip itself
• Example : Intel’s x86, Motorola’s 680x0

Data Bus
CPU
General-
Serial
Purpose RAM ROM I/O Timer COM
Micro- Port
Port
processor

Address Bus
Microcontroller :
• A smaller computer
• On-chip RAM, ROM, I/O ports...
• Example : Motorola’s 6811, Intel’s 8051, Zilog’s Z8 and
PIC 16X

CPU RAM ROM


A single chip
Serial
I/O Timer COM
Port
Port
Microprocessor vs. Microcontroller

Microprocessor Microcontroller
• CPU is stand-alone, RAM, • CPU, RAM, ROM, I/O
ROM, I/O, timer are and timer are all on a
separate single chip
• designer can decide on the • fix amount of on-chip
amount of ROM, RAM and ROM, RAM, I/O ports
I/O ports. • for applications in which
• expansive cost, power and space
• versatility are critical
• general-purpose • single-purpose
Embedded System

• 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
Block Diagram

External interrupts
On-chip Timer/Counter

Interrupt ROM for


On-chip Timer 1
Control program
RAM Timer 0
code

CPU

Bus Serial
4 I/O Ports
OSC Control Port

P0 P1 P2 P3 TxD RxD
Address/Data
Registers

. A

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
Some Simple Instructions
MOV dest,source ; dest = source

MOV A,#72H ;A=72H


MOV A, #’r’ ;A=‘r’ OR 72H
MOV R4,#62H ;R4=62H
MOV B,0F9H ;B=the content of F9’th byte of RAM

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

MOV P1,A ;mov A to port 1

Note 1:
MOV A,#72H ≠ MOV A,72H
After instruction “MOV A,72H ” the content of 72’th byte of RAM will replace in Accumulator.

8086 8051
MOV AL,72H MOV A,#72H
MOV AL,’r’ MOV A,#’r’
MOV BX,72H
MOV AL,[BX] MOV A,72H
Note 2:
MOV A,R3 ≡ MOV A,3
SETB bit ; bit=1
CLR bit ; bit=0

SETB C ; CY=1
SETB P0.0 ;bit 0 from port 0 =1
SETB P3.7 ;bit 7 from port 3 =1
SETB ACC.2 ;bit 2 from ACCUMULATOR =1
SETB 05 ;set high D5 of RAM loc. 20h

Note:

CLR instruction is as same as SETB


i.e:
CLR C ;CY=0

But following instruction is only for CLR:


CLR A ;A=0
SUBB A,source ;A=A-source-CY

SETB C ;CY=1
SUBB A,R5 ;A=A-R5-1

ADC A,source ;A=A+source+CY

SETB C ;CY=1
ADC A,R5 ;A=A+R5+1
DEC byte ;byte=byte-1
INC byte ;byte=byte+1

INC R7
DEC A
DEC 40H ; [40]=[40]-1

CPL A ;1’s complement


Example:
MOV A,#55H ;A=01010101 B
L01: CPL A
MOV P1,A
ACALL DELAY
SJMP L01

NOP & RET & RETI

All are like 8086 instructions.


ANL - ORL - XRL
EXAMPLE:
MOV R5,#89H
ANL R5,#08H

RR – RL – RRC – RLC A
EXAMPLE:
RR A
8051 Flag bits and the PSW register
CY AC F0 RS1 RS0 OV -- P

• PSW Register
Carry flag PSW.7 CY
Auxiliary carry flag PSW.6 AC
Available to the user for general purpose PSW.5 --
Register Bank selector bit 1 PSW.4 RS1
Register Bank selector bit 0 PSW.3 RS0
Overflow flag PSW.2 OV
User define bit PSW.1 --
Parity flag Set/Reset odd/even parity PSW.0 P

RS1 RS0 Register Bank Address

0 0 0 00H-07H

0 1 1 08H-0FH

1 0 2 10H-17H

1 1 3 18H-1FH
Example: Example:
MOV A,#88H MOV A,#9CH
ADD A,#93H ADD A,#64H
88 10001000
9C 10011100
+93 +10010011
---- -------------- +64 +01100100
11B 00011011 ---- --------------
CY=1 AC=0 P=0 100 00000000
CY=1 AC=1 P=0

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

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

• Immediate
• Register
• Direct
• Register Indirect
• Indexed
Immediate Addressing Mode

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

Example :

Num EQU 30

MOV R0,Num
MOV DPTR,#data1

ORG 100H
data1: db “IRAN”
Register Addressing Mode

MOV Rn, A ;n=0,..,7


ADD A, Rn
MOV DPL, R6

MOV DPTR, A
MOV Rm, Rn
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 R0, 40H


MOV 56H, A
MOV A, 4 ; ≡ MOV A, R4
MOV 6, 2 ; copy R2 to R6
; MOV R6,R2 is invalid !

SFR register and their address

MOV 0E0H, #66H ; ≡ MOV A,#66H


MOV 0F0H, R2 ; ≡ MOV B, R2
MOV 80H,A ; ≡ MOV P1,A
Register Indirect Addressing Mode
MOV A,@Ri ; move content of RAM loc.Where address is held by Ri into A
( i=0 or 1 )
MOV @R1,B

In other word, the content of register R0 or R1 is sources or target in MOV, ADD and
SUBB insructions.
Example:
Write a program to copy a block of 10 bytes from RAM location sterting at 37h to RAM
location starting at 59h.

Solution:
MOV R0,37h ; source pointer
MOV R1,59h ; dest pointer
MOV R2,10 ; counter
L1: MOV A,@R0
MOV @R1,A
INC R0
INC R1
DJNZ R2,L1
Indexed Addressing Mode And On-Chip ROM
Access

• This mode is widely used in accessing data elements


of look-up table entries located in the program (code)
space ROM at the 8051

MOVC A,@A+DPTR
A= content of address A +DPTR 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.
MUL & DIV
• MUL AB ;B|A = A*B
MOV A,#25H
MOV B,#65H
MUL AB ;25H*65H=0E99
;B=0EH, A=99H
• MUL AB ;A = A/B, B = A mod B
MOV A,#25
MOV B,#10
MUL AB ;A=2, B=5
Other conditional jumps :

JZ Jump if A=0

JNZ Jump if A/=0

DJNZ Decrement and jump if A/=0

CJNE A,byte Jump if A/=byte

CJNE reg,#data Jump if byte/=#data

JC Jump if CY=1

JNC Jump if CY=0

JB Jump if bit=1

JNB Jump if bit=0

JBC Jump if bit=1 and clear bit


CALL Instructions

Another control transfer instruction is the CALL


instruction, which is used to call a subroutine.

• LCALL(long call)
In this 3-byte instruction, the first byte is the opcode
an the second and third bytes are used for the address
of target subroutine. Therefore, LCALL can be used
to call subroutines located anywhere within the 64K
byte address space of the 8051.
• ACALL (absolute call)

ACALL is 2-byte instruction in contrast to LCALL,


which is 13 bytes. Since ACALL is a 2-byte
instruction, the target address of the subroutine must be
within 2K bytes address because only 11 bits of the 2
bytes are used for the address. There is no difference
between ACALL and LCALL in terms of saving the
program counter on the stack or the function of the
RET instruction. The only difference is that the target
address for LCALL can be anywhere within the 64K
byte address space of the 8051 while the target address
of ACALL must be within a 2K-byte range.
Instructions For Reading an Input Port
• Following are instructions for
reading external pins of ports:

Mnemonics Examples Description

Bring into A the data at P2


MOV A,PX MOV A,P2
pins

JNB PX.Y,.. JNB P2.1,TARGET Jump if pin P2.1 is low

JB PX.Y,.. JB P1.3,TARGET Jump if pin P1.3 is high

Copy status of pin P2.4 to


MOV C,PX.Y MOV C,P2.4
CY
Read-Modify-Write Instructions
Mnemonics Example

ANL ANL P1,A


ORL ORL P1,A
XRL XRL P1,A
JBC PX.Y, TARGET JBC P1.1, TARGET
CPL CPL P1.2
INC INC P1
DEC DEC P1
DJNZ PX, TARGET DJNZ P1,TARGET
MOV PX.Y,C MOV P1.2,C
CLR PX.Y CLR P1.3
SETB PX.Y SETB P1.4
Port 3 Alternate Functions

P3 Bit Function Pin

P3.0 RxD 10
P3.1 TxD 11
P3.2 INT0 12
P3.3 INT1 13
P3.4 T0 14
P3.5 T1 15
P3.6 WR 16
P3.7 RD 17

You might also like