Register Addressing ModeMOV Rn, A ;n=0,..,7ADD A, RnMOV DPL, R6MOV DPTR, AMOV Rm, Rn
Direct 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.MOV R0, 40HMOV 56H, AMOV A, 4 ; ≡ MOV A, R4MOV 6, 2 ; copy R2 to R6 ; MOV R6,R2 is invalid !
Register 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 )MOV @R1,B In other word, the content of register R0 or R1 is sources or target in MOV, ADD and SUBB insructions.
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 elements of look-up table entries located in the program (code) space ROM at the 8051 MOVC A,@A+DPTR (A,@A+PC) A= content of address A +DPTR from ROMNote: 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.
Some Simple InstructionsMOV dest,source ; dest = source MOV A,#72H ;A=72H MOV R4,#62H ;R4=62H MOV B,0F9H ;B=the content of F9’th byte of RAM MOV DPTR,#7634H MOV DPL,#34H MOV DPH,#76H MOV P1,A ;mov A to port 1Note 1: 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
MUL & DIV• MUL AB ;B|A = A*B MOV 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,#25 MOV B,#10 DIV AB ;A=2, B=5
SETB bit ; bit=1CLR bit ; bit=0SETB C ; CY=1SETB P0.0 ;bit 0 from port 0 =1SETB P3.7 ;bit 7 from port 3 =1SETB ACC.2 ;bit 2 from ACCUMULATOR =1SETB 05 ;set high D5 of RAM loc. 20hNote:CLR instruction is as same as SETBi.e.: CLR C ;CY=0But following instruction is only for CLR: CLR A ;A=0
Stack in the 8051• The register used to access the stack is called SP (stack pointer) register. 7FH Scratch pad RAM• The stack pointer in the 30H 8051 is only 8 bits wide, which means that it can 2FH Bit-Addressable RAM take value 00 to FFH. 20H When 8051 powered up, 1FH Register Bank 3 the SP register contains 18H 17H value 07. 10H Register Bank 2 0FH Stack) Register Bank 1) 08H 07H Register Bank 0 00H
LOOP and JUMP Instructions Conditional Jumps : 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
DJNZ:Write a program to clear ACC, thenadd 3 to the accumulator ten timeSolution: MOV A,#0 MOV R2,#10AGAIN: ADD A,#03 DJNZ R2,AGAIN ;repeat until R2=0 (10 times) MOV R5,A
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 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.
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 ; source pointer MOV R1,#59h ; dest pointer MOV R2,#10 ; counterL1: MOV A,@R0 MOV @R1,A INC R0 INC R1 DJNZ R2,L1
Decimal Addition 156 + 248. 100s 10s 1s. 1 5 6+ 2 4 8= 4 0 4 16 Bit Addition 1A44 + 22DB = 3D1F . 256s 16’s 1s . 1 A 4 4 + 2 2 D B = 3 D 1 F
Performing the Addition with 8051 . 65536s 256s 1s . R6 R7 + R4 R5 = R1 R2 R31.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, 3MOV A,R7 ;Move the low-byte into the accumulatorADD A,R5 ;Add the second low-byte to the accumulatorMOV R3,A ;Move the answer to the low-byte of the resultMOV A,R6 ;Move the high-byte into the accumulatorADDC A,R4 ;Add the second high-byte to the accumulator, plus carry.MOV R2,A ;Move the answer to the high-byte of the resultMOV 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
The Whole Program;Load the first value into R6 and R7MOV R6,#1AhMOV R7,#44h;Load the first value into R4 and R5MOV R4,#22hMOV R5,#0DBh;Call the 16-bit addition routine LCALL ADD16_16ADD16_16:;Step 1 of the processMOV A,R7 ;Move the low-byte into the accumulatorADD A,R5 ;Add the second low-byte to the accumulatorMOV R3,A ;Move the answer to the low-byte of the result;Step 2 of the processMOV A,R6 ;Move the high-byte into the accumulatorADDC 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 processMOV 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
Timer & Port Operations• Example:Write a program using Timer0 to create a 10khz square wave on P1.0 MOV TMOD,#02H ;8-bit auto-reload mode MOV TH0,#-50 ;-50 reload value in TH0 SETB TR0 ;start timer0LOOP: JNB TF0, LOOP ;wait for overflow CLR TF0 ;clear timer0 overflow flag CPL P1.0 ;toggle port bit SJMP LOOP ;repeat END
Interrupts1. Enabling and Disabling Interrupts2. Interrupt Priority3. Writing the ISR (Interrupt Service Routine)
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.
Writing the ISR Example:Writing the ISR for Timer0 interrupt ORG 0000H ;reset LJMP MAIN ORG 000BH ;Timer0 entry pointT0ISR: . ;Timer0 ISR begins . RETI ;return to main programMAIN: . ;main program . . END
Structure of Assembly language and Running an 8051 program EDITOR PROGRAM Myfile.asm ASSEMBLER PROGRAM Myfile.lst Other obj file Myfile.obj LINKER PROGRAM OH PROGRAM Myfile.hex
Examples of Our Program Instructions• MOV C,P1.4 JC LINE1• SETB P1.0 CLR P1.2
8051 Instruction SetACALL: Absolute Call JC: Jump if Carry Set PUSH: Push Value Onto StackADD, ADDC: Add Acc. (With Carry) JMP: Jump to Address RET: Return From SubroutineAJMP: Absolute Jump JNB: Jump if Bit Not Set RETI: Return From InterruptANL: Bitwise AND JNC: Jump if Carry Not Set RL: Rotate Accumulator LeftCJNE: Compare & Jump if Not Equal JNZ: Jump if Acc. Not Zero RLC: Rotate Acc. Left Through CarryCLR: Clear Register JZ: Jump if Accumulator Zero RR: Rotate Accumulator RightCPL: Complement Register LCALL: Long Call RRC: Rotate Acc. Right Through CarryDA: Decimal Adjust LJMP: Long Jump SETB: Set BitDEC: Decrement Register MOV: Move Memory SJMP: Short JumpDIV: Divide Accumulator by B MOVC: Move Code Memory SUBB: Sub. From Acc. With BorrowDJNZ: Dec. Reg. & Jump if Not Zero MOVX: Move Extended Memory SWAP: Swap Accumulator NibblesINC: Increment Register MUL: Multiply Accumulator by B XCH: Exchange BytesJB: Jump if Bit Set NOP: No Operation XCHD: Exchange DigitsJBC: Jump if Bit Set and Clear Bit ORL: Bitwise OR XRL: Bitwise Exclusive OR POP: Pop Value From Stack Undefined: Undefined Instruction