Professional Documents
Culture Documents
Micro Controller 8051 - Soft
Micro Controller 8051 - Soft
Micro Controller 8051 - Soft
Hexadecimal Basis
Hexadecimal Digits: 1 2 3 4 5 6 7 8 9 A B C D E F A=10 B=11 C=12 D=13 E=14 F=15
Registers
SP
A B R0 R1 R2 R3 R4 R5 R6 R7 Some 8-bitt Registers of the 8051 Some 8051 16-bit Register PC PC DPTR DPH DPL
8k
0FFFH
8751 AT89C51
30H 2FH Bit-Addressable RAM 20H 1FH 18H 17H 10H 0FH 08H 07H 00H Register Bank 2 (Stack) Register Bank 1 Register Bank 3
Register Bank 0
Addressing Modes
Register Direct Register Indirect Immediate Relative Absolute Long Indexed
In other word, the content of register R0 or R1 is sources or target in MOV, ADD and SUBB insructions.
; dest = source
;A=72H ;R4=62H ;B=the content of F9th byte of RAM
Note 1:
MOV A,#72H MOV A,72H After instruction MOV A,72H the content of 72th byte of RAM will replace in Accumulator.
Note 2:
MOV A,R3 MOV A,3
ADDA, Source
ADDA,#6 ADDA,R6 ADD ADD A,6 A,0F3H ;A=A+6
;A=A+SOURCE
SUBB
SUBB SUBB
A, Source
A,#6 A,R6 ;A=A-6
;A=A-SOURCE-C
;A=A+R6
; bit=1 ; bit=0
; CY=1 ;bit 0 from port 0 =1 ;bit 7 from port 3 =1 ;bit 2 from ACCUMULATOR =1 ;set high D5 of RAM loc. 20h
CLR instruction is as same as SETB i.e.: CLR C ;CY=0 But following instruction is only for CLR: CLR A ;A=0
DEC INC
INC DEC DEC
byte byte
R7 A 40H
;byte=byte-1 ;byte=byte+1
; [40]=[40]-1
CPL
;1s complement
A,#55H ;A=01010101 B A P1,A DELAY L01
Example: MOV MOV MOV PUSH PUSH PUSH R6,#25H R1,#12H R4,#0F3H 6 1 4
SP=08H
SP=09H
SP=08H
DJNZ:
Write a program to clear ACC, then add 3 to the accumulator ten time Solution: MOV MOV ADD DJNZ MOV A,#0 R2,#10 A,#03 R2,AGAIN ;repeat until R2=0 (10 times) R5,A
AGAIN:
LJMP(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 00FFH is divided into forward and backward jumps, that is , within -128 to +127 bytes of memory relative to the address of the current PC.
CALL 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.
Example: Write a program to copy a block of 10 bytes from RAM location starting at 37h to RAM location starting at 59h. Solution: MOV R0,#37h MOV R1,#59h MOV R2,#10 L1: MOV A,@R0 MOV @R1,A INC R0 INC R1 DJNZ R2,L1
Decimal Addition
156 + 248
. . + = 100's 1 2 4 10's 5 4 0 1's 6 8 4
16 Bit Addition
1A44 + 22DB = 3D1F
. . + = 1 2 3 256's A 2 D 16s 4 D 1 1's 4 B F
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.
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
LOOP:
MOV TMOD,#02H MOV TH0,#-50 SETB TR0 JNB TF0, LOOP CLR TF0 CPL P1.0 SJMP LOOP END
;8-bit auto-reload mode ;-50 reload value in TH0 ;start timer0 ;wait for overflow ;clear timer0 overflow flag ;toggle port bit ;repeat
Interrupts
1. Enabling and Disabling Interrupts 2. Interrupt Priority 3. Writing the ISR (Interrupt Service Routine)
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.
Interrupt Vectors
Interrupt
System Reset External 0 Timer 0 External 1 Timer 1 Serial Port Timer 2
Vector Address
0000H 0003H 000BH 0013H 001BH 0023H 002BH
T0ISR:
MAIN:
OH PROGRAM Myfile.hex