Sei sulla pagina 1di 48

8051 PIN Diagram

Pins 1 to 8 − These pins are known as Port 1. This port doesn’t serve any
other functions. It is internally pulled up, bi-directional I/O port.
Pin 9 − It is a RESET pin, which is used to reset the microcontroller to its
initial values.
Pins 10 to 17 − These pins are known as Port 3. This port serves some
functions like interrupts, timer input, control signals, serial communication
signals RxD and TxD, etc.
Pins 18 & 19 − These pins are used for interfacing an external crystal to
get the system clock.
Pin 20 − This pin provides the power supply to the circuit.
Pins 21 to 28 − These pins are known as Port 2. It serves as I/O port.
Higher order address bus signals are also multiplexed using this port.
Pin 29 − This is PSEN pin which stands for Program Store Enable. It is
used to read a signal from the external program memory.
Pin 30 − This is ALE pin which stands for Address Latch Enable. It is
used to demultiplex the address-data signal of port.
Pin 31 − This is EA pin which stands for External Access input. It is used
to enable/disable the external memory interfacing.
Pins 32 to 39 − These pins are known as Port 0. It serves as I/O port.
Lower order address and data bus signals are multiplexed using this port.
Pin 40 − This pin is used to provide power supply to the circuit.
The 8051
Assembly Language
Assembly Language

In the early days of the computer, programmers coded


in machine language, consisting of 0s and 1s
Assembly languages, which provided mnemonics for
the machine code instructions, plus other features,
were developed.
Assembly language is referred to as a low level
language
Assembly Language
 Assembly language instruction includes
 a mnemonic (abbreviation easy to remember)
• The commands to the CPU, telling it what
those to do with those items
 optionally followed by one or two operands
• The data items being manipulated
 A given Assembly language program is a series
of statements, or lines
 Assembly language instructions
• Tell the CPU what to do
 Directives (or pseudo-instructions)
• Give directions to the assembler
Assembly Language
 An Assembly language instruction
consists of four fields:

[label:] Mnemonic [operands] [;comment]

MOV R5, #25H ;load 25H into R5

HERE: SJMP HERE


Assembly Language
The most widely used registers
• A (Accumulator)
• For all arithmetic and logic instructions
• B, R0, R1, R2, R3, R4, R5, R6, R7
• DPTR (data pointer), and PC (program counter)
Assembly Language
The step of Assembly language program are outlines as
follows:
1. First we use an editor to type a program,
many excellent editors or word processors are
available that can be used to create and/or
edit the program.
2. The “asm” source file containing the program
code created in step 1 is fed to an 8051
assembler.

3. Assembler require a third step called linking.

4. Next the “abs” file is fed into a program called


“OH” (object to hex converter) which creates
a file with extension “hex”that is ready to
burn into ROM.
Assembly Language
First we use an editor to type a program, many excellent editors
or word processors are available that can be used to create and/or
edit the program.

ORG 0H ;start (origin) at 0


MOV R5,#25H ;load 25H into R5
MOV R7,#34H ;load 34H into R7
MOV A,#0 ;load 0 into A
ADD A,R5 ;add contents of R5 to A ;now A = A +
R5
ADD A,R7 ;add contents of R7 to A ;now A = A +
R7
ADD A,#12H ;add to A value 12H ;now A = A + 12H
HERE: SJMP HERE ;stay in this loop
END ;end of asm source file
Assembly Language
Examine the list file and how the code is placed in ROM
Assembly Language
After the program is burned into ROM, the opcode and operand are placed
in ROM memory location starting at 0000.
Assembly Language
The program status word (PSW) register, also referred to as the flag register,
is an 8 bit register
The 8051 Instruction Set

12
Instruction Groups
• The 8051 has 255 instructions
– Every 8-bit opcode from 00 to FF is used except
for A5.

• The instructions are grouped into 5 groups


– Arithmetic
– Logic
– Data Transfer
– Boolean
– Branching

13
Arithmetic Instructions
• ADD
– 8-bit addition between the accumulator (A) and a
second operand.
• The result is always in the accumulator.
• The CY flag is set/reset appropriately.
• Example:
MOV A,#0F5H ;A=F5 hex
ADD A,#0BH ;A=F5+0B=00

14
Arithmetic Instructions
• ADDC
• When adding two 16-bit data operands, the propagation of
a carry from lower byte to higher byte is concerned

Add 1E44H to 56CAH

CLR C ; Clear the CY flag


MOV A, 44H ; The lower 8-bits of the 1st number
ADD A, CAH ; The lower 8-bits of the 2nd number
MOV R1, A ; The result 0EH will be in R1. CY = 1.
MOV A, 1EH ; The upper 8-bits of the 1st number
ADDC A, 56H ; The upper 8-bits of the 2nd number
MOV R2, A ; The result of the addition is 75H

The overall result: 750EH will be in R2:R1. CY = 0.

15
Arithmetic Instructions
• DA
– Decimal adjust the accumulator.
• Format the accumulator into a proper 2 digit packed BCD
number.
• Operates only on the accumulator.
• Works only after the ADD instruction.

Add 34 to 49 BCD

CLR C ; Clear the CY flag


MOV A, #34H ; Place 1st number in A
ADD A, #49H ; Add the 2nd number.
; A = 7DH
DA A ; A = 83H

16
Arithmetic Instructions
• SUBB(Subtract with Borrow)
• Subtract an operand and the previous value of the
borrow (carry) flag from the accumulator.
– The result is always saved in the accumulator.
– The CY flag is set/reset appropriately.

SUBB when CY = 0

17
Arithmetic Instructions
• SUBB(Subtract with Borrow)
SUBB when CY = 1

18
Arithmetic Instructions

• MUL AB
 The 8051 supports byte by byte multiplication only
• The byte are assumed to be unsigned data
MUL AB ;AxB, 16-bit result in B, A

19
Arithmetic Instructions
• DIV AB
• The 8051 supports byte over byte division only
• The byte are assumed to be unsigned data
DIV AB ;divide A by B, A/B

20
Arithmetic Instructions

• INC
– Increment the operand by one.
• The operand can be a register, a direct address, an
indirect address, the data pointer.

• DEC
– Decrement the operand by one.
• The operand can be a register, a direct address, an
indirect address.

21
Logical Instructions
• ANL
• This instruction will perform a logic AND on the two
operands and place the result in the destination .
ANL destination, source
• The destination is normally the accumulator
• The source operand can be a register, in memory, or
immediate

22
Logical Instructions
• ORL
• The destination and source operands are ORed and the
result is placed in the destination.
ORL destination, source
• The destination is normally the accumulator
• The source operand can be a register, in memory, or
immediate

23
Logical Instructions
• XRL
• This instruction will perform XOR operation on the two operands
and place the result in the destination.
XRL destination, source
• The destination is normally the accumulator
• The source operand can be a register, in memory, or
immediate

24
Logical Instructions
• CPL
– CPL A ;complements the register A
– This is called 1’s complement

25
Logical Instructions
• RL / RLC / RR / RRC
– Rotate the accumulator.
• RL and RR without the carry
• RLC and RRC rotate through the carry.
• In rotate right(RR)
• The 8 bits of the accumulator are rotated right one bit, and
• Bit D0 exits from the LSB and enters into MSB, D7.

26
Logical Instructions
• In rotate left(RL)
• The 8 bits of the accumulator are rotated left one bit,
and
• Bit D7 exits from the MSB and enters into LSB, D0

27
Logical Instructions
• In RRC A
• Bits are rotated from left to right
• They exit the LSB to the carry flag, and the carry flag
enters the MSB

28
Logical Instructions
• In RLC A
• Bits are shifted from right to left
• They exit the MSB and enter the carry flag, and the
carry flag enters the LSB

29
Logical Instructions

• SWAP A
• It swaps the lower nibble and the higher nibble
• In other words, the lower 4 bits are put into the higher 4
bits and the higher 4 bits are put into the lower 4 bits
• SWAP works only on the accumulator (A).

30
Data Transfer Instructions
• MOV
– 1-bit data transfer involving the CY flag
• MOV C, bit
• MOV bit, C

• MOV
– 16-bit data transfer involving the DPTR
• MOV DPTR, #data

31
Data Transfer Instructions
• MOVC
– Move Code Byte
• Load the accumulator with a byte from program memory.
• Must use indexed addressing

• MOVC A, @A+DPTR
• MOVC A, @A+PC

Microprocessors 1 Msc. Ivan A. Escobar 32


Broitman
Data Transfer Instructions
• MOVX
– Data transfer between the accumulator and a byte
from external data memory.
• MOVX A, @Ri
• MOVX A, @DPTR
• MOVX @Ri, A
• MOVX @DPTR, A

33
Data Transfer Instructions
• PUSH / POP
– Push and Pop a data byte onto the stack.
– The data byte is identified by a direct address from
the internal RAM locations.

• PUSH DPL
• POP 40H

34
Data Transfer Instructions
• XCH
– Exchange accumulator and a byte variable
• XCH A, Rn
• XCH A, direct
• XCH A, @Ri

• XCHD
– Exchange lower digit of accumulator with the lower digit of
the memory location specified.
• XCHD A, @Ri

• The lower 4-bits of the accumulator are exchanged with the


lower 4-bits of the internal memory location identified indirectly
by the index register.
• The upper 4-bits of each are not modified.

35
Boolean Instructions
• This group of instructions is associated with the
single-bit operations of the 8051.
• This group allows manipulating the individual bits
of bit addressable registers and memory
locations as well as the CY flag.
– The P, OV, and AC flags cannot be directly
altered.
• This group includes:
– Set, clear, and, or complement, move.
– Conditional jumps.

36
Boolean Instructions
• CLR
– Clear a bit or the CY flag.
• CLR P1.1
• CLR C

• SETB
– Set a bit or the CY flag.
• SETB A.2
• SETB C

• CPL
– Complement a bit or the CY flag.
• CPL 40H ; Complement bit 40 of the bit
addressable memory

37
Boolean Instructions
• JC / JNC
– Jump to a relative address if CY is set / cleared.

• JB / JNB
– Jump to a relative address if a bit is set / cleared.
• JB ACC.2, <label>

• JBC
– Jump to a relative address if a bit is set and clear
the bit.

38
Branching Instructions
• The 8051 provides four different types of
unconditional jump instructions:
– Short Jump – SJMP
• Uses an 8-bit signed offset relative to the 1st byte of the next
instruction.
– Long Jump – LJMP
• Uses a 16-bit address.
• 3 byte instruction capable of referencing any location in the
entire 64K of program memory.

39
Branching Instructions
• The 8051 provides 2 forms for the CALL
instruction:
– Absolute Call – ACALL
• Uses an 11-bit address similar to AJMP
• The subroutine must be within the same 2K page.
– Long Call – LCALL
• Uses a 16-bit address similar to LJMP
• The subroutine can be anywhere.

– Both forms push the 16-bit address of the next


instruction on the stack and update the stack
pointer.

40
Branching Instructions
• The 8051 provides 2 forms for the return
instruction:
– Return from subroutine – RET
• Pop the return address from the stack and continue
execution there.
– Return from ISV – RETI
• The RETI instruction is used to end an interrupt
service routine

41
Branching Instructions
• The 8051 supports 5 different conditional jump
instructions.
– ALL conditional jump instructions use an 8-bit
signed offset.

– Jump on Zero – JZ / JNZ


• Jump if the A == 0 / A != 0
– The check is done at the time of the instruction execution.

– Jump on Carry – JC / JNC


• Jump if the C flag is set / cleared.

42
Branching Instructions
– Jump on Bit – JB / JNB
• Jump if the specified bit is set / cleared.
• Any addressable bit can be specified.

– Jump if the Bit is set then Clear the bit – JBC


• Jump if the specified bit is set.
• Then clear the bit.

43
Branching Instructions
• Compare and Jump if Not Equal – CJNE
– Compare the magnitude of the two operands and
jump if they are not equal.
• The values are considered to be unsigned.
• The Carry flag is set / cleared appropriately.

• CJNE A, direct, rel


• CJNE A, #data, rel
• CJNE Rn, #data, rel
• CJNE @Ri, #data, rel

44
Branching Instructions
• Decrement and Jump if Not Zero – DJNZ
– Decrement the first operand by 1 and jump to the
location identified by the second operand if the
resulting value is not zero.

• DJNZ Rn, rel


• DJNZ direct, rel

• No Operation
– NOP

45
Examples
1: Write a program to bring in a byte of data serially one
bit at a time via pin P2.7 and save it in register R2. The
byte comes in with the LSB first.

46
Examples
2. Assume that bit P2.2 is used to control an outdoor light and bit P2.5
a light inside a building. Show how to turn on the outside light and
turn off the inside one.
Examples
3. Write a program that finds the number of 1s in a given byte.

Potrebbero piacerti anche