Documente Academic
Documente Profesional
Documente Cultură
M. Usman Rafique
Course Outline
Week 1 2 3 4 5 6 7 8, 9 10 11 12 13 14 15, 16 17 Topic Introduction 8051Microcontroller Architecture and Hardware Assembly Language Assembly Language Contd. Timers and Counters Serial Port Interrupt Design and Interface Examples
Midterm Exam PIC 18 F Microcontroller Introduction, Architecture, I/O Pins Revision Programming in C Timers / Counters Using Internal ADC of PIC Peripherals of PIC 18F Microcontrollers
M. Usman Rafique
Assembly Language
2.
3. 4. 5.
M. Usman Rafique
Addressing Modes
M. Usman Rafique
1. Immediate Addressing
Value to be stored in memory immediately follows the operation code in memory Or The instruction itself dictates the value to be stored in memory Example MOV A, #20h Accumulator will be loaded with the value that immediately follows; in this case 20h
M. Usman Rafique
1. Immediate Addressing
The value to be loaded is fixed at compile time its not very flexible
M. Usman Rafique
1. Immediate Addressing
Examples
Important
MOV A, #FH
; gives error
M. Usman Rafique
2 . Direct Addressing
Value to be stored in memory is obtained by directly retrieving it from another memory location Example MOV A, 30H
The above instruction will read data from RAM address 30H and put that data in A.
10
M. Usman Rafique
2 . Direct Addressing
Generally fast, although the value to be stored is not included in the instruction Its quickly accessible since value is stored in 8051s Internal RAM Much more flexible than immediate addressing. Value to be loaded is whatever it is found at that location which may be variable Entire 128 bytes of RAM (0 to FF) can be accessed
11 M. Usman Rafique Air University, Spring 2014
2 . Direct Addressing
Examples
12
M. Usman Rafique
3. Indirect Addressing
This instruction analyzes the value of R0. A will be loaded with value from RAM whose address is found in R0 For example if R0 holds value 40H and RAM address 40h holds value of 67H. Then A will have value of 67H
13 M. Usman Rafique Air University, Spring 2014
3. Indirect Addressing
MOV A,@R0 MOV @R1, B Note: @ sign is compulsory in order to use Indirect addressing
14
M. Usman Rafique
3. Indirect Addressing
This mode makes data access dynamic instead of static (as in case of direct addressing) Only R0 and R1 can be used
Since these registers are 8 bit, use is restricted to internal RAM only
15
M. Usman Rafique
4. Register Addressing
In register addressing mode, we directly use the value stored in register 8051 has 8 working registers (of the currently active register bank) R0 to R7
Examples
MOV A, R7 MOV A, R5 Contents of R7 and R5 will be moved in above instructions
16 M. Usman Rafique Air University, Spring 2014
4. Register Addressing
17
M. Usman Rafique
8 bit signed offset is used, range for jumping is -128 to 127 location SJMP [label]
18
M. Usman Rafique
6. Absolute Addressing
Used only with ACALL and AJMP instruction It allows branching within 2K limit (11 bit)
19
M. Usman Rafique
7. Long Addressing
Used only with LCALL and LJMP instruction Allows branching within 64K limit (16 bit)
20
M. Usman Rafique
8. Index Addressing
Used with JMP and MOVC instruction Use base register (either PC or DPTR) and an offset (the accumulator) in forming the effective address MOVC A, @A + DPTR JMP @A + DPTR It is used for look up table
Previously we studied how its stored Well cover its usage later on
M. Usman Rafique Air University, Spring 2014
21
Instruction Types
22
M. Usman Rafique
Types of Instructions
1.
2.
3. 4.
5.
23
M. Usman Rafique
MOV Summary
MOV [Destination],[Source]
Examples
24
M. Usman Rafique
1. Arithmetic Instructions
25
M. Usman Rafique
Example
26
High byte and Low Byte are added separately using 2 instructions If there is a Carry out from addition of low bytes, it has to be added in sum of high bytes
27
M. Usman Rafique
28
M. Usman Rafique
;clear carry flag. CY = 0 ; Load lower byte of 1st number ; Add lower byte of 2nd number ; now A = 74H and CY = 1; ; Move lower byte of sum in R0 ; Load higher byte of 1st number ; Add with carry ; 2C + 55 + 1 (carry) = 82 H ; Move higher byte of sum in R1
M. Usman Rafique Air University, Spring 2014
Note: There is no subtraction instruction without borrow in 8051 (many microprocessors have it)
30 M. Usman Rafique Air University, Spring 2014
31
M. Usman Rafique
15h Carry = 0
M. Usman Rafique Air University, Spring 2014
33
14h Carry = 0
M. Usman Rafique Air University, Spring 2014
35
F0h = 1111 0000. Taking 2s complement (by inverting and adding 1) = 0000 1111 + 1 = 0001 0000 = 10h Carry = 1. Showing it is a negative number
M. Usman Rafique Air University, Spring 2014
37
38
M. Usman Rafique
39
M. Usman Rafique
A = 40h = 64 B=0
M. Usman Rafique Air University, Spring 2014
40
41
M. Usman Rafique
42
M. Usman Rafique
A = 4h B=0
M. Usman Rafique Air University, Spring 2014
43
44
45
Lower 4 Bits represent decimal number (0 to 9) Remaining bits are zero Example: 9 will be represented as 0000 1001 1 byte can represent 1 number only Each 4 bits represent a decimal number (0 to 9) 1 byte can store more 2 digits Example: 79 will be represented as 0111 1001 Utilizes space more efficiently than unpacked BCD
M. Usman Rafique Air University, Spring 2014
Packed BCD
46
1. Arithmetic Instructions
Important
49
M. Usman Rafique
2. Logical Instructions
Left Right
SWAP
50 M. Usman Rafique Air University, Spring 2014
51
M. Usman Rafique
52
M. Usman Rafique
53
M. Usman Rafique
2. Logical Instructions - OR
OR Format
54
M. Usman Rafique
2. Logical Instructions - OR
OR Example mov a,#74h orl a,#0Fh
55
M. Usman Rafique
2. Logical Instructions - OR
OR Example mov a,#74h orl a,#0Fh
56
M. Usman Rafique
57
M. Usman Rafique
58
M. Usman Rafique
59
M. Usman Rafique
CLR operand
Result operand = 0
The operand may be Accumulator, carry flag (C), any IO Port Bit etc
60
M. Usman Rafique
61
M. Usman Rafique
CPL operand
The operand may be Accumulator, carry flag (C), any IO Port Bit etc
62
M. Usman Rafique
63
M. Usman Rafique
64
M. Usman Rafique
RL A
65
M. Usman Rafique
66
M. Usman Rafique
RR A
67
M. Usman Rafique
CY
68
M. Usman Rafique
Rotate Left used as Multiplication Rotate Right used as Division Less memory required Much faster
69
M. Usman Rafique
SWAP A
70
M. Usman Rafique
71
M. Usman Rafique
72
M. Usman Rafique
3. Data Transfer
MOV
Addressing Modes
(Already Covered)
73
M. Usman Rafique
75
M. Usman Rafique
77
Stack Pointer (SP) stores the address of current stack location. At power up, SP = 07
RAM locations 08h to 1Fh can be used for stack RAM locations 20h to 2Fh are bit-addressable locations More space if SP is set to point to 30h to 7Fh i.e. general purpose RAM
79 M. Usman Rafique Air University, Spring 2014
Number of a register
PUSH expression
Example
; Push contents of R3 ; Push contents of R1 ; Push value stored at memory location 55h
81
M. Usman Rafique
POP expression
expression must be
Number of a register
Retrieves the value the last value stored by PUSH Automatically decrements the SP
82 M. Usman Rafique Air University, Spring 2014
mov a,75h
What is this program doing?
83 M. Usman Rafique Air University, Spring 2014
mov a,75h
84
CLR
CLR bit Clears the bit i.e. makes the bit = 0 Examples
SETB
SETB bit Sets the bit i.e. makes the bit = 1 Examples
85
ANL
anl c,P1^7
ORL
orl c,P3^5
CPL
CPL P0^0
M. Usman Rafique Air University, Spring 2014
86
5. Program Branching
Conditional
87
fn2:
89
M. Usman Rafique
All required registers are stored before calling SR and retrieved after the SR has finished
90
PSW A B R0 R7
M. Usman Rafique Air University, Spring 2014
2.
PUSH them in stack before calling SR POP them from stack after returning from SR
91
M. Usman Rafique
JB bit, Label
JNB Jump if Bit is Not set JNB bit, label JC Jump if Carry is set JC Label
expression2 may be
96
M. Usman Rafique
Jmp $
What is this program doing?
97 M. Usman Rafique Air University, Spring 2014
R0 to R7
First it decrements the value of register and then checks if the value is zero or not
98
M. Usman Rafique
main:
DJNZ r7, main
jmp $
What is this program doing?
99 M. Usman Rafique Air University, Spring 2014
expression may be
100
M. Usman Rafique
Example
mov a,#1h
main:
jmp $
DEC a JZ main
Expression may be
102
M. Usman Rafique
jmp $
JB bit, Label
Example
Example
JC Label
JNC Jump if Carry Not set JNC Label
106
M. Usman Rafique
Acknowledgement
Material used with permission of
I am extremely thankful to Dr. Javaid who has been a great teacher and still helps and supports me
107
M. Usman Rafique