Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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
12
Instruction Groups
• The 8051 has 255 instructions
– Every 8-bit opcode from 00 to FF is used except
for A5.
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
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
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
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
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.
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.
42
Branching Instructions
– Jump on Bit – JB / JNB
• Jump if the specified bit is set / cleared.
• Any addressable bit can be specified.
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.
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.
• 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.