Académique Documents
Professionnel Documents
Culture Documents
Syllabus of Unit VI
8051 instruction set, Interrupts, Programming exercises for interfaced with keyboard, LED Matrix, serial communication
Learning Objectives:
By the end of this unit, the students will be able to :
Understand the syntax & operation of various instructions of 8051 microcontroller Get an in-depth knowledge about interrupts of c 8051 Understand the details about interfacing of various peripherals like keyboard and LED matrix Acquire knowledge about how serial communication takes place in c 8051
XCH A, Source
Exchange data between register A & internal RAM source Eg: XCH A,R7 XCH A,@R1
6
Arithmetic instructions
INC Destination
-increment destination by 1 -Destination : 8/16 bit register / memory -Eg: INC A INC @R1 INC DPTR
DEC Destination
decrement destination by 1 - Destination : 8-bit register / memory -Eg: DEC A DEC @R0 Note: DPTR can only be incremented and cant 8 be decremented
Arithmetic instructions
ADD / ADDC D,S
-Add source to destination without / with carry -CF, AC & OV flag affected - Register A:-Destination; Source:- data/reg/memory -Eg: ADD A,R5 ADDC A,@R1 ADD A,#56H
SUBB D,S
Subtract source from destination with borrow -CF, AC & OV flag affected - Register A:-Destination; Source:- data/reg/memory 9 -Eg: SUBB A,R5 SUBB A,@R0 SUBB A,#99H
Arithmetic instructions
MUL AB
-Multiply contents of register A & B -CF =0, & OV flag affected according to result - Result : 16 bit copied in register B(MSB) & A(LSB)
DIV AB
-Divide contents of register A by contents of register B -CF =0, & OV flag affected according to result - Result : 16 bit copied in register B(remainder) & A(Quotient)
10
Arithmetic instructions
DA A
-Decimal adjust register A -Used for BCD number addition - Used after ADD / ADDC instructions - CF & AC flag affected according to result
11
Logical instructions
ANL D,S
-Performs logical ANDing of destination & source -Destination : A/memory; Source:data/register/memory - Eg: ANL A,#0FH ANL A,R5 ANL 10H,A
ORL D,S
Logical instructions
XRL D,S
-Performs logical X-ORing of destination & source -Destination : A/memory; Source:data/register/memory - Eg: XRL A,#0FH ANL A,R5 XRL A,@R1
CPL A
-Compliment accumulator
-gives 1s compliment of accumulator data SWAP A 13 -Exchange the upper & lower nibbles of accumulator
Logical instructions
RL A -Rotate data of accumulator towards left without
carry
RLC A - Rotate data of accumulator towards left with carry RR A -Rotate data of accumulator towards right without
carry
Branching instructions
Program branching instructions are used to control the flow of actions in a program Some instructions provide decision making capabilities and transfer control to other parts of the program. e.g. conditional and unconditional branches
15
Branching instructions
The 8051 provides 2 forms for the CALL instruction:
Branching instructions
The 8051 provides 2 forms for the return instruction:
Branching instructions
The 8051 provides four different types of unconditional jump instructions:
Branching instructions
Absolute Jump AJMP
Uses an 11-bit address substituted for the lower 11 bits of the PC to calculate the 16-bit address of the target.. 2 byte instruction The location referenced must be within 2K Bytes
Indirect Jump
8-bit unsigned value of the acc added to the 16-bit data pointer and resulting sum is returned to the PC Neither ACC nor DPTR is altered No flags are affected 19 JMP @A + DPTR
Branching instructions
The 8051 supports different conditional jump instructions. ALL conditional jump instructions use an 8-bit address.
JZ / JNZ label :
Jump if the A == 0 / A != 0
JC / JNC label :
Jump if the Carry flag is set / cleared.
Branching instructions
JB bit, Target:
Jump to target location if the specified bit = 1 For eg: JB P1.3,L1
Branching instructions
CJNE destination, source, relative address
Compare and Jump if Not Equal Destination : accumulator /any one of the Rn registers Source: register / memory / immediate data Only CF affected For Eg: CJNE A,#99H,L1 CJNE A,@R0,next CJNE R5,#87H,L2
22
SETB C :- Set the Carry flag SETB bit :- Set the specified bit
For eg: SETB PSW.4
CPL C :- Compliment the carry flag CPL bit :- Compliment the bit value
For eg: CPL P2.2
24
Interrupts Programming
An interrupt is an external or internal event that interrupts the microcontroller to inform it that a device needs its service. Interrupts vs. Polling A single microcontroller can serve several devices. There are two ways to do that: interrupts polling. The program which is associated with the interrupt is called the interrupt service routine (ISR) or interrupt handler.
Interrupt Sources
Original 8051 has 6 sources of interrupts
Reset Timer 0 overflow Timer 1 overflow External Interrupt 0 External Interrupt 1 Serial Port events (buffer full, buffer empty, etc)
Interrupt Vectors
Each interrupt has a specific place in code memory where program execution (interrupt service routine) begins.
RESET : 0000H External Interrupt 0 : 0003h Timer 0 overflow : 000Bh External Interrupt 1 : 0013h Timer 1 overflow : 001Bh Serial : 0023h
Timer ISR
Notice that There is no need for a CLR TFx instruction in timer ISR 8051 clears the TF internally upon jumping to ISR Notice that We must reload timer in mode 1 There is no need on mode 2 (timer auto reload) (MSB) TF1 TR1 Timer 1 TF0 TR0 Timer0 IE1 IT1 IE0 for Interrupt (LSB) IT0
Interrupt Priorities
All interrupts have a power on default priority order. 1. Reset
37
38
39
1
P2.0
R1
Key pressed
C1
1
P2.4
8051
P2.0
R1
P2.4
8051
42
43
44
45
46
47
Pins are TTL compatible A line driver is hence necessary to make them RS232 compatible Two such line drivers are:
MAX 232 MAX 233
48
49
50
51
52
SCON
Serial Control register 8 bit
PCON
Power control register -8 bit
53
54
55
D7 SMOD
--
--
--
GF1 GF0
D0 PD IDL
SMOD =1 ; Baud Rate doubled GF1-GF0 : General purpose Flag Bits PD- Power Down Mode IDL Idle Mode
56
57
Clock Gen.
CPU
PD IDL
58
Summary :
The instruction set of 8051 consists of simple instructions almost similar to that of p 8085 8051 consists of 6 interrupts ( 3 internal, 2 external & 1 RESET interrupt) IE, IP registers used for interrupts 4*4 matrix keyboard & LEDs can be interfaced with 8051 using simple routines 8051 consists of 1 serial port which performs fullduplex asynchronous transmission & reception of information SBUF, SCON & PCON registers are used for serial communication 59
References :
The 8051 Microcontroller Architecture, programming & Applications by Kenneth J. Ayala The 8051 Microcontroller & Embedded Systems
by Muhammad Ali Mazidi &Janice Gillispie Mazidi
60
61