Relative, Absolute, & Long Addressing Used only with jump and call instructions: SJMP ACALL,AJMP LCALL,LJMP... Indexed Addressing Mode • This mode is widely used in accessing data eleme
Trang 2Numerical Bases Used in Programming
• Hexadecimal
• Binary
• BCD
Trang 4Decimal, Binary, BCD, & Hexadecimal Numbers
(43) 10 =
( 0010 1011 ) 2 =
( 2 B ) 16
Trang 5Register s
Some 8051 16-bit Register
Some 8-bitt Registers of
the 8051
SP
Trang 6AT89C51 8752
AT89C52
Trang 7• RAM memory space allocation in the 8051
7FH
30H 2FH
20H 1FH
17H 10H 0FH 07H 08H 18H
) Stack) Register Bank 1
Register Bank 2 Register Bank 3 Bit-Addressable RAM Scratch pad RAM
Trang 9Register Addressing Mode
Trang 10Direct 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.
Trang 11Register Indirect Addressing Mode
• In this mode, register is used as a pointer to the data.
MOV A,@Ri ; move content of RAM loc
where address is held by Ri into A ( i=0 or 1 )
In other word, the content of register R0 or R1 is
sources or target in MOV, ADD and SUBB insructions.
Trang 12Immediate Addressing Mode
MOV A,#65H
MOV R6,#65H
MOV DPTR,#2343H
MOV P1,#65H
Trang 13Relative, Absolute, & Long Addressing
Used only with jump and call instructions:
SJMP ACALL,AJMP LCALL,LJMP
Trang 14Indexed Addressing Mode
• This mode is widely used in accessing data
elements of look-up table entries located in the program (code) space ROM at the 8051
(A,@A+PC) 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.
Trang 15Some Simple Instructions
MOV A,#72H ;A=72H
MOV A,#72H ≠ MOV A,72H
After instruction “MOV A,72H ” the content of 72’th byte of RAM will replace in Accumulator
Note 2:
MOV A,R3 ≡ MOV A,3
Trang 16ADD A, Source ;A=A+SOURCE
ADD A,#6 ;A=A+6
ADD A,R6 ;A=A+R6
ADD A,6 ;A=A+[6] or A=A+R6 ADD A,0F3H ;A=A+[0F3H]
Trang 17MUL & DIV
• MUL AB ;B|A = A*B
Trang 18SETB bit ; bit=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.:
But following instruction is only for CLR:
Trang 19DEC byte ;byte=byte-1
Trang 21ANL - ORL – XRL
Bitwise Logical Operations:
AND, OR, XOR
Trang 22Stack in the 8051
• The register used to access
the stack is called SP
(stack pointer) register.
• The stack pointer in the
8051 is only 8 bits wide,
which means that it can
take value 00 to FFH
When 8051 powered up,
the SP register contains
value 07.
7FH
30H 2FH
20H 1FH
17H 10H 0FH 07H 08H 18H
00H Register Bank 0
) Stack) Register Bank 1
Register Bank 2 Register Bank 3 Bit-Addressable RAM Scratch pad RAM
Trang 23MOV R6,#25H MOV R1,#12H MOV R4,#0F3H PUSH 6
PUSH 1 PUSH 4
SP=08H
F3 12 25
0BH 0AH 09H 08H
SP=08H
12 25
0BH 0AH 09H 08H
SP=09H
Trang 24LOOP and JUMP Instructions
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
Trang 25Write a program to clear ACC, then
add 3 to the accumulator ten time
Solution:
MOV A,#0 MOV R2,#10
DJNZ R2,AGAIN ;repeat until R2=0 (10 times) MOV R5,A
Trang 26LJMP(long jump)
LJMP is an unconditional jump It is a 3-byte instruction It allows a jump to any memory location from 0000 to FFFFH.
AJMP(absolute jump)
In this 2-byte instruction, It allows a jump to any memory
location within the 2k block of program memory.
SJMP(short jump)
In this 2-byte instruction The relative address range of
00-FFH is divided into forward and backward jumps, that is ,
within -128 to +127 bytes of memory relative to the address of the current PC.
Trang 27CALL Instructions
Another control transfer instruction is the CALL
instruction, which is used to call a subroutine.
• LCALL(long call)
This 3-byte instruction 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 the target
address of the subroutine must be within 2K
byte range.
Trang 28Write a program to copy a block of 10 bytes from RAM location starting at 37h to RAM location starting at 59h.
Solution:
Trang 30Performing the Addition with 8051
. 65536's 256's 1's
1.Add the low bytes R7 and R5, leave the answer in R3
2.Add the high bytes R6 and R4, adding any carry from step 1, and leave the answer in R2 3.Put any carry from step 2 in the final byte, R1
Trang 31Steps 1, 2, 3
MOV A,R7 ;Move the low-byte into the accumulator
ADD A,R5 ;Add the second low-byte to the accumulator
MOV R3,A ;Move the answer to the low-byte of the result
MOV A,R6 ;Move the high-byte into the accumulator
ADDC A,R4 ;Add the second high-byte to the accumulator, plus carry MOV R2,A ;Move the answer to the high-byte of the result
MOV A,#00h ;By default, the highest byte will be zero
ADDC A,#00h ;Add zero, plus carry from step 2
MOV R1,A ;Move the answer to the highest byte of the result
Trang 32The Whole Program
;Load the first value into R6 and R7
;Step 1 of the process
MOV A,R7 ;Move the low-byte into the accumulator
ADD A,R5 ;Add the second low-byte to the accumulator
MOV R3,A ;Move the answer to the low-byte of the result
;Step 2 of the process
MOV A,R6 ;Move the high-byte into the accumulator
ADDC A,R4 ;Add the second high-byte to the accumulator, plus carry MOV R2,A ;Move the answer to the high-byte of the result
;Step 3 of the process
MOV A,#00h ;By default, the highest byte will be zero
ADDC A,#00h ;Add zero, plus carry from step 2
MOV MOV R1,A ;Move the answer to the highest byte of the result
;Return - answer now resides in R1, R2, and R3 RET
Trang 33Timer & Port Operations
• Example:
Write a program using Timer0 to create a 10khz square wave on P1.0
END
Trang 35Interrupt Enable (IE) Register :
• EA : Global enable/disable.
• - : Undefined.
• ET2 :Enable Timer 2 interrupt.
• ES :Enable Serial port interrupt.
• ET1 :Enable Timer 1 interrupt.
• EX1 :Enable External 1 interrupt.
• ET0 : Enable Timer 0 interrupt
• EX0 : Enable External 0 interrupt.
Trang 37Writing the ISR
Example:
Writing the ISR for Timer0 interrupt
LJMP MAIN
.
.
.
END
Trang 38Structure of Assembly language and Running an 8051 program
EDITOR PROGRAM
ASSEMBLER PROGRAM
LINKER PROGRAM
OH PROGRAM
Trang 39Examples of Our Program Instructions
• MOV C,P1.4
JC LINE1
• SETB P1.0
CLR P1.2
Trang 408051 Instruction Set
ACALL: Absolute Call
ADD, ADDC: Add Acc (With Carry)
AJMP: Absolute Jump
ANL: Bitwise AND
CJNE: Compare & Jump if Not Equal
CLR: Clear Register
CPL: Complement Register
DA: Decimal Adjust
DEC: Decrement Register
DIV: Divide Accumulator by B
DJNZ: Dec Reg & Jump if Not Zero
INC: Increment Register
JB: Jump if Bit Set
JBC: Jump if Bit Set and Clear Bit
JC: Jump if Carry Set JMP: Jump to Address JNB: Jump if Bit Not Set JNC: Jump if Carry Not Set JNZ: Jump if Acc Not Zero JZ: Jump if Accumulator Zero LCALL: Long Call
LJMP: Long Jump MOV: Move Memory MOVC: Move Code Memory MOVX: Move Extended Memory MUL: Multiply Accumulator by B NOP: No Operation
ORL: Bitwise OR POP: Pop Value From Stack
PUSH: Push Value Onto Stack RET: Return From Subroutine RETI: Return From Interrupt RL: Rotate Accumulator Left RLC: Rotate Acc Left Through Carry RR: Rotate Accumulator Right
RRC: Rotate Acc Right Through Carry SETB: Set Bit
SJMP: Short Jump SUBB: Sub From Acc With Borrow SWAP: Swap Accumulator Nibbles XCH: Exchange Bytes
XCHD: Exchange Digits XRL: Bitwise Exclusive OR Undefined: Undefined Instruction