PowerElectronics Class6

Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

Instruction Set

Classifications
• Bit Addressable Instructions
• Data Movement Instructions
• Arithmetic Instructions
• Logic and Shift Instructions
• Control Instructions
• Subroutine Instructions
• Stack Instructions
128 Bit addressable RAM memory
Locations
Ex: to set bit 67H
Solution: (in Microcontroller)

SETB 67H

Solution: (in Microprocessor)

MOV A, @2CH
ORL A, #10000000B
MOV @2CH, A
21 special function registers
Boolean Variable Instructions
Instructions Functions Example Instructions
SETB bit Set the bit (bit=1) SETB 42H
CLR bit Clear the bit (bit=0) CLR 67H
CPL bit Complement the bit (bit= NOT bit) CPL P0.0
JB bit, target Jump to target if bit=1 JB PSW.2, TARGET
JNB bit, target Jump to target if bit=0 JNB P2.3, TARGET
JBC bit, target Jump to target if bit =1, clear bit JBC P2.3, TARGET

Program: Write a program to save the Accumulator in R7 of bank 2


CLR PSW.3
SETB PSW.4
MOV R7,A
Addressing Modes
• Immediate Addressing Modes
MOV A, #25
MOV R4, #62
MOV DPTR, #4521H
• Register Addressing Modes
MOV A, R0
MOV R2, A
Source and Destination Registers size should match
MOV R2, R3 is invalid
If data (operand) is in RAM how to access them??
Addressing Modes
• Direct Addressing Mode
To access other RAM memory locations, i.e., other than
register banks, we can give direct address in the instruction
MOV R0, 40H; save the content of RAM location 40H in R0
MOV 56H, A; save content of A in RAM location 56H
The ‘#’ sign distinguishes direct addressing mode from
immediate addressing mode.
MOV R4,#62
MOV A,#25H
MOV A, R0 and MOV A, 0 are same

Address value is limited to one byte, 00-FFH, which means this


addressing mode is limited to accessing RAM locations and
Registers located inside 8051.
• Direct Addressing for stack
Only direct addressing mode is allowed to
accessing the stack.
PUSH 0E0H is valid
PUSH A is invalid
Program: Transfer the contents of the registers
A, R0, and R1, respectively, of bank 0 to the
registers B, R0, and R1, of bank 1.
• Register Indirect Addressing Mode
A register is used as pointer to the data.
MOV A, @R0; move contents of RAM location
; whose address is held by R0 into A
Program: Write a program to copy the value 55H into
RAM memory locations 40H to 45H using
(a) Direct addressing mode
(b) Register indirect addressing mode without loop
(c) With a loop
Program: Take 10 bytes of data from data RAM
locations 45H to 54H, add 02 to each of them, and
save the result in data RAM locations 79H down to
70H.
• Indexed Addressing Mode
Indexed addressing mode is widely used in
accessing data elements of look up table in RAM
or ROM
MOVC A, @A+DPTR;
Program: The common cathode ‘seven segment’
codes for the numerals from 0 to 9 are stored in
ROM space from 400H. Write a program to get x
value from P1 and display using seven segment
display connected in P2.
Ans:
ORG 0000H
MOV DPTR, #400H
MOV A, #0FFH
MOV P1, A; Configure P1 as input port
BACK: MOV A, P1
MOVC A, @A+DPTR ; Get seven segment code from Look- up table
MOV P2, A
SJMP BACK

ORG 0400H
DB 3FH, 06H, 5BH, 4FH, 66H, 6DH, 7DH, 07H, 7FH, 6FH
END
Loop and JUMP instructions (Control
Transfer)
• Repeating a sequence of instructions a certain
number of times is called a loop.
• Example: DJNZ reg, label ; reg is decremented, if it is not zero, it
jumps to the target address referred to by the label

• Program: Multiply 25 by 10 using the


technique of repeated addition and save the
result in RAM 45H location.
MOV A, #0
MOV R2, #10
AGAIN: ADD A, #25
DJNZ R2, AGAIN
MOV 45H, A ; Save the result
Conditional Jump Instructions
Instruction Function
JZ Target Jump if A=0
JNZ Target Jump if A not equal to 0
JC Target Jump if carry flag in PSW is set
JNC Target Jump if carry flag in PSW is clear
DJNZ R2, Target Decrement and jump if register is 0

In short jumps , the address of the target must be within -128 to


+127
All Conditional Jumps are short jumps.
Unconditional Jumps: in which control is transferred unconditionally
to the target location.
Instruction: SJMP Label HERE: SJMP HERE
Instruction: LJMP Label (3-byte Instruction)
The 2-byte target address allows a jump to any memory location
from 0000H to FFFFH
Subroutine Instructions
• LCALL Label, 3-byte instruction, first byte is
opcode and other two bytes are target
address (anywhere in 64 kB).
• ACALL Label, is 2 byte instruction, 11 bits are
used for target address (only 2kB)
• RET, after finishing execution of the
subroutine, RET instruction transfers control
back to the caller.
Program: Analyse the stack contents after the execution of the
first LCALL and RET instructions (Processor Environment)
Address OPCODE Label Instructions Comment
0000 ORG 0000H
0000 758145 MOV SP, #45H ;initialize stack
0003 7455 BACK: MOV A, #55
0005 F590 MOV P1,A ;send 55 to port1
0007 120300 LCALL DELAY ; call delay subroutine
000A 74AA MOV A, #0AAH
000C F590 MOV P1, A ;send AAH to port1
000E 120300 LCALL DELAY
0010 80F0 SJMP BACK ; infinite loop
ORG 300H
0300 7DFF DELAY: MOV R5, #0FFH
0302 DDFE AGAIN: DJNZ R5, AGAIN
0304 22 RET ;return to caller
END ;end of asm file
Time Delay Calculation
• CPU takes certain number of clock cycles to
execute an instruction
• These cycles are called machine cycles. Total
number of machine cycles required to execute
an instruction is instruction cycle
• Assume that a 8051 system clock cycle period
is 1.085 microseconds. Find the time delay of
the following subroutine.
• Program
Label Instruction Machine Cycle
DELAY: MOV R3, #250 1
HERE: NOP 1
NOP 1
NOP 1
NOP 1
DJNZ R3, HERE 2
RET 2

Note: Generally machine cycles for each instruction of a specific microcontroller can
be found in data sheet.
Answer: 1630.755 microseconds
I/O Ports
• 4 8-bit ports P0, P1, P2, and P3
• Upon RESET all ports are configured as INPUT
• PORT 0 requires external pull-up resistors
• PORT 1,2, and 3 have inbuilt internal pull up
resisters
• By programming, each pin of the port can be
configured as either input or output
Structure of P1,P2, and P3
Pin Configuration
• By default all pins are configured as input
• When the first ‘0’ is written to a port, it becomes
an output
• To configure any port as input we have to write
‘1’ to it.
MOV A, #0FFH
MOV P1, A ; make P0 as input
BACK: MOV A, P0 ; get data from P0
MOV P1, A ; send it to P1
SJMP BACK
PORT Operation
• One has to be careful in connecting external
Vcc to the input pin
• Reading the Pin status
• Reading the Latch
• Port 0 does not have internal pull-up resister
• One should not leave a pin to float
Instructions to read the status of Input
Port
Mnemonics Examples
MOV A, PX MOV A, P1
JNB PX.Y, Target JNB P1.2, Target
JB PX.Y JB P1.3, Target
MOV C, PX.Y MOV C, P1.4
CJNE A, PX, Target CJNE A, P2, Target
Read-Modify-Write Instructions
Mnemonics Example
ANL ANL P1, A
ORL ORL P1, A
XRL XRL P1, A
JBC PX.Y, Target JBC P1.3, Target
CPL CPL P1.2
INC INR P1
DEC DEC P1
DJNZ DJNZ P1, Target
MOV PX.Y, C MOV P1.2, C
CLR PX.Y CLR P1.3
SETB PX.Y SETB P1.4
• Program: Create a square wave of 50% duty
cycle on bit 0 of port 1
HERE: SETB P1.0
LCALL DELAY
CLR P1.0
LCALL DELAY
SJMP HERE
• Another way
HERE: CPL P1.0
LCALL DELAY
SJMP HERE
• Hardware Schematic Diagram
• Write a program to perform the following;
(a) keep monitoring pin P0.1 until it becomes high
(b) when P0.1 becomes high, read in the data from P1
(c) send a low-to-high pulse on P0.2 to indicate that the data has
been read
Solution:
SETB P0.1
MOV P1, #0FFH
AGAIN: JNB P0.1, AGAIN
MOV A, P1
CLR P0.2
SETB P0.2

Modify the program to do this continuously and draw the hardware


schematic diagram
• Exercise: A switch is connected to pin P1.0 and
an LED to pin P2.7. Draw the hardware
schematic diagram and write a program to get
the status of the switch and send it to the LED.

You might also like