Micro Controller 8051- Soft
-
Upload
dinesh-antony -
Category
Documents
-
view
244 -
download
0
Transcript of Micro Controller 8051- Soft
-
8/2/2019 Micro Controller 8051- Soft
1/40
-
8/2/2019 Micro Controller 8051- Soft
2/40
Numerical Bases Used in Programming
Hexadecimal
Binary
BCD
-
8/2/2019 Micro Controller 8051- Soft
3/40
Hexadecimal Basis
Hexadecimal Digits:
1 2 3 4 5 6 7 8 9 A B C D E F
A=10
B=11
C=12D=13
E=14
F=15
-
8/2/2019 Micro Controller 8051- Soft
4/40
Decimal, Binary, BCD, & Hexadecimal Numbers
(43)10=
(0100 0011)BCD=
( 0010 1011 )2 =
( 2 B )16
-
8/2/2019 Micro Controller 8051- Soft
5/40
Registers
A
B
R0
R1
R3
R4
R2
R5
R7
R6
DPH DPL
PC
DPTR
PC
Some 8051 16-bit Register
Some 8-bitt Registers of
the 8051
SP
-
8/2/2019 Micro Controller 8051- Soft
6/40
Memory mapping in 8051
ROM memory map in 8051 family
0000H
0FFFH
0000H
1FFFH8751
AT89C518752
AT89C52
4k 8k
-
8/2/2019 Micro Controller 8051- Soft
7/40
RAM memory space allocation in the 8051
7FH
30H
2FH
20H
1FH
17H
10H
0FH
07H
08H
18H
00HRegister Bank 0
(Stack) Register Bank 1
Register Bank 2
Register Bank 3
Bit-Addressable RAM
Scratch pad RAM
-
8/2/2019 Micro Controller 8051- Soft
8/40
Addressing Modes
Register
Direct
Register Indirect
Immediate
Relative
Absolute
Long Indexed
-
8/2/2019 Micro Controller 8051- Soft
9/40
Register Addressing Mode
MOVRn, A ;n=0,..,7
ADD A, Rn
MOVDPL, R6
MOVDPTR, A
MOVRm, Rn
-
8/2/2019 Micro Controller 8051- Soft
10/40
Direct Addressing Mode
Although the entire of 128 bytes of RAM can beaccessed using direct addressing mode, it is mostoften used to access RAM loc. 30 7FH.
MOVR0, 40H
MOV56H, A
MOVA, 4 ; MOV A, R4
MOV6, 2 ; copy R2 to R6
; MOV R6,R2 is invalid !
-
8/2/2019 Micro Controller 8051- Soft
11/40
Register Indirect Addressing Mode
In this mode, register is used as a pointer to thedata.
MOV A,@Ri ; move content of RAM loc.where address is held by Ri into A ( i=0 or 1 )
MOV @R1,B
In other word, the content of register R0 or R1 issources or target in MOV, ADD and SUBBinsructions.
-
8/2/2019 Micro Controller 8051- Soft
12/40
Immediate Addressing Mode
MOVA,#65H
MOVR6,#65H
MOVDPTR,#2343H
MOVP1,#65H
-
8/2/2019 Micro Controller 8051- Soft
13/40
Relative, Absolute, & Long Addressing
Used only with jump and call instructions:
SJMP
ACALL,AJMP
LCALL,LJMP
-
8/2/2019 Micro Controller 8051- Soft
14/40
Indexed Addressing Mode
This mode is widely used in accessing dataelements of look-up table entries located in theprogram (code) space ROM at the 8051
MOVC A,@A+DPTR (A,@A+PC)
A= content of address A +DPTR from ROM
Note:
Because the data elements are stored in theprogram (code ) space ROM of the 8051, it usesthe instruction MOVC instead of MOV. TheC means code.
-
8/2/2019 Micro Controller 8051- Soft
15/40
SomeSimple Instructions
MOV dest,source ; dest = source
MOV A,#72H ;A=72H
MOV R4,#62H ;R4=62H
MOV B,0F9H ;B=the content of F9th byte of RAM
MOV DPTR,#7634H
MOV DPL,#34HMOV DPH,#76H
MOV P1,A ;mov A to port 1
Note 1:
MOV A,#72H MOV A,72HAfter instruction MOV A,72H the content of 72th byte of RAM willreplace in Accumulator.
Note 2:MOV A,R3 MOV A,3
-
8/2/2019 Micro Controller 8051- Soft
16/40
ADDA, Source ;A=A+SOURCE
ADDA,#6 ;A=A+6
ADDA,R6 ;A=A+R6
ADD A,6 ;A=A+[6] or A=A+R6
ADD A,0F3H ;A=A+[0F3H]
SUBB A, Source ;A=A-SOURCE-C
SUBB A,#6 ;A=A-6
SUBB A,R6 ;A=A+R6
-
8/2/2019 Micro Controller 8051- Soft
17/40
MUL & DIV
MUL AB ;B|A = A*BMOV A,#25H
MOV B,#65H
MUL AB ;25H*65H=0E99
;B=0EH, A=99H
DIV AB ;A = A/B, B = A mod B
MOV A,#25MOV B,#10
DIV AB ;A=2, B=5
-
8/2/2019 Micro Controller 8051- Soft
18/40
SETB bit ; bit=1
CLR bit ; bit=0
SETB C ; CY=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.:
CLR C ;CY=0
But following instruction is only for CLR:
CLR A ;A=0
-
8/2/2019 Micro Controller 8051- Soft
19/40
DEC byte ;byte=byte-1
INC byte ;byte=byte+1
INC R7
DEC A
DEC 40H ; [40]=[40]-1
-
8/2/2019 Micro Controller 8051- Soft
20/40
RR RL RRC RLC A
EXAMPLE:RR A
RR:
RRC:
RL:
RLC:
C
C
-
8/2/2019 Micro Controller 8051- Soft
21/40
ANL - ORL XRL
Bitwise Logical Operations:AND, OR, XOREXAMPLE:
MOV R5,#89H
ANLR5,#08H
CPL A ;1s complementExample:
MOV A,#55H ;A=01010101 B
L01: CPL A
MOV P1,A
ACALL DELAY
SJMP L01
-
8/2/2019 Micro Controller 8051- Soft
22/40
Stack in the 8051 The register used to
access the stack is calledSP (stack pointer)register.
The stack pointer in the8051 is only 8 bits wide,
which means that it cantake value 00 to FFH.When 8051 powered up,the SP register containsvalue 07.
7FH
30H
2FH
20H
1FH
17H
10H
0FH
07H
08H
18H
00HRegister Bank 0
(Stack) Register Bank 1
Register Bank 2
Register Bank 3
Bit-Addressable RAM
Scratch pad RAM
-
8/2/2019 Micro Controller 8051- Soft
23/40
Example:
MOV R6,#25H
MOV R1,#12H
MOV R4,#0F3H
PUSH 6
PUSH 1
PUSH 4
0BH
0AH
09H
08H
Start SP=07H
25
0BH
0AH
09H
08H
SP=08H
F3
12
25
0BH
0AH
09H
08H
SP=08H
12
25
0BH
0AH
09H
08H
SP=09H
-
8/2/2019 Micro Controller 8051- Soft
24/40
LOOP and JUMP Instructions
JZ Jump if A=0
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
JC Jump if CY=1
JNC Jump if CY=0
JB Jump if bit=1
JNB Jump if bit=0
JBC Jump if bit=1 and clear bit
Conditional Jumps :
-
8/2/2019 Micro Controller 8051- Soft
25/40
DJNZ:
Write a program to clear ACC, thenadd 3 to the accumulator ten time
Solution:
MOV A,#0
MOV R2,#10
AGAIN: ADD A,#03
DJNZ R2,AGAIN ;repeat until R2=0 (10 times)
MOV R5,A
-
8/2/2019 Micro Controller 8051- Soft
26/40
LJMP(long jump)
LJMP is an unconditional jump. It is a 3-byte instruction.
It allows a jump to any memory location from 0000 toFFFFH.
AJMP(absolute jump)
In this 2-byte instruction, It allows a jump to any memorylocation 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 addressof the current PC.
-
8/2/2019 Micro Controller 8051- Soft
27/40
CALL InstructionsAnother 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 targetaddress of the subroutine must be within 2Kbyte range.
-
8/2/2019 Micro Controller 8051- Soft
28/40
Example:
Write a program to copy a block of 10 bytes from RAMlocation starting at 37h to RAM location starting at 59h.
Solution:
MOV R0,#37h ; source pointer
MOV R1,#59h ; dest pointer
MOV R2,#10 ; counter
L1: MOV A,@R0
MOV @R1,A
INC R0INC R1
DJNZ R2,L1
-
8/2/2019 Micro Controller 8051- Soft
29/40
-
8/2/2019 Micro Controller 8051- Soft
30/40
Performing the Addition with 8051
. 65536's 256's 1's
. R6 R7
+ R4 R5
= R1 R2 R3
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.
-
8/2/2019 Micro Controller 8051- Soft
31/40
Steps 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
-
8/2/2019 Micro Controller 8051- Soft
32/40
The Whole Program;Load the first value into R6 and R7
MOV R6,#1Ah
MOV R7,#44h;Load the first value into R4 and R5
MOV R4,#22h
MOV R5,#0DBh
;Call the 16-bit addition routine LCALL ADD16_16
ADD16_16:
;Step 1 of the processMOV 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
-
8/2/2019 Micro Controller 8051- Soft
33/40
Timer & Port Operations
Example:
Write a program using Timer0 to create a 10khz squarewave on P1.0
MOV TMOD,#02H ;8-bit auto-reload modeMOV TH0,#-50 ;-50 reload value in TH0
SETB TR0 ;start timer0
LOOP: JNB TF0, LOOP ;wait for overflow
CLR TF0 ;clear timer0 overflow flag
CPL P1.0 ;toggle port bit
SJMP LOOP ;repeat
END
-
8/2/2019 Micro Controller 8051- Soft
34/40
Interrupts
1. Enabling and Disabling Interrupts
2. Interrupt Priority
3. Writing the ISR (Interrupt Service
Routine)
-
8/2/2019 Micro Controller 8051- Soft
35/40
Interrupt 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.
-
8/2/2019 Micro Controller 8051- Soft
36/40
Interrupt Vectors
Interrupt Vector Address
System Reset 0000H
External 0 0003H
Timer 0 000BH
External 1 0013H
Timer 1 001BH
Serial Port 0023HTimer 2 002BH
-
8/2/2019 Micro Controller 8051- Soft
37/40
Writing the ISRExample:
Writing the ISR for Timer0 interrupt
ORG 0000H ;reset
LJMP MAIN
ORG 000BH ;Timer0 entry point
T0ISR: . ;Timer0 ISR begins
.
RETI ;return to main program
MAIN: . ;main program
.
.
END
-
8/2/2019 Micro Controller 8051- Soft
38/40
Structure of Assembly language and
Running an 8051 program
EDITOR
PROGRAM
ASSEMBLER
PROGRAM
LINKER
PROGRAM
OH
PROGRAM
Myfile.asm
Myfile.obj
Other obj fileMyfile.lst
Myfile.hex
-
8/2/2019 Micro Controller 8051- Soft
39/40
Examples of Our Program Instructions
MOV C,P1.4
JC LINE1
SETB P1.0
CLR P1.2
-
8/2/2019 Micro Controller 8051- Soft
40/40
8051 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