SlideShare a Scribd company logo
Microcontroller (PIC)
PIC Microcontroller Programming
Dr. Nilesh Bhaskarrao Bahadure
nbahadure@gmail.com
https://www.sites.google.com/site/nileshbbahadure/home
July 1, 2021
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 1 / 118
Overview
1 Syllabus
2 Course Objectives & Outcomes
3 Objectives
4 Addressing Modes
Immediate addressing mode
Register operand addressing mode
Memory operand addressing mode
Direct addressing mode
Indirect addressing mode
5 Instruction Sets of PIC16F877
Guidelines from Microchip Technology
Instruction Set:
Summary of Instructions
Encoding of instruction:
Instruction Execution Period
Pipelining of instruction fetch successive addressing
Word list
Instruction set - Group Wise
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 2 / 118
Syllabus
Unit Heading No. of Lectures
PIC Microcontroller Program-
ming
No of lectures - 5
Addressing modes, Instruction set, Assembly Language and C pro-
gramming.
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 3 / 118
Course Objectives
1 To expose the students to the fundamentals of PIC Microcontroller
16F877 architecture and its Peripherals.
2 To introduce the advanced features in PIC Microcontroller 16F877.
3 To make student develop and practice assembly language and C
language programming techniques
4 To enable student demonstrate and perform hardware interfacing and
design.
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 4 / 118
Course Outcomes
At the end of the course, the student shall be able to,
1 Describe how PIC microcontroller and its peripherals function.
2 Interpret advanced features in PIC Microcontroller 16F877.
3 Program an embedded system in assembly and C.
4 Design, implement and test a single-processor embedded systems for
real-time applications
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 5 / 118
Objectives
Upon completion of this chapter, you will be able to:
1 Understanding of addressing modes and its importance in writing
programs
2 Manipulate data using the WREG and MOVE instructions
3 Perform simple operations such as ADD and MOVE using the file
register and access bank in the PIC microcontroller
4 Code simple PIC assembly language programming
5 Details the execution of PIC assembly language instructions
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 6 / 118
Addressing Modes
To know the working principal and data handling, we need to have clear
knowledge on addressing modes of pic microcontroller. Now we can see
that how we can categorise different addressing modes of pic
microcontroller. In PIC micro controller, it having mainly five addressing
modes. Those are
1 Immediate addressing mode
2 Register operand addressing mode
3 Memory operand addressing mode
4 Direct addressing
5 Indirect addressing.
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 7 / 118
Immediate addressing mode
In this addressing mode, the operand is a number or constant not an
address as MOVLW 43h, the operand here is data not address. So in this
addressing mode of pic microcontroller data is directly transfer. And data
is immediate after the opcode. That is why this type of addressing is
called immediate addressing. This way is fast in execution.
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 8 / 118
Register operand addressing mode
In this addressing mode, the operand is a Register which holds the data to
be execute. Register operand addressing mode deals with the registers like:
CLR W
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 9 / 118
Memory operand addressing mode
In this addressing mode, the operand is an address of Memory location
which holds the data to be execute. Again memory operand addressing
mode is under two category
Direct addressing like CLRF 13h. We deal with the address or the
memory location.
Indirect addressing. we use in it INDF and FSR registers.
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 10 / 118
Direct addressing mode
Direct Addressing is done through a 9-bit address. This address is
obtained by connecting 7th bit of direct address. By using an instruction
with two bits (RP1, RP0) from STATUS register. this is shown in Figure
1. Any access to SFR registers can be an example of direct addressing.
Figure : Direct addressing mode
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 11 / 118
Indirect addressing mode
It does not take an address from an instruction. But it derives from IRP
bit of STATUS and FSR registers. Addressed location is accessed through
INDF register. And INDF register in fact holds the address indicated by
the FSR. Indirect addressing is very convenient for manipulating data
arrays located in GPR registers. In this case, it is necessary to initialise
FSR register with a starting address of the array, and the rest of the data
can be accessed by increment the FSR register. Figure 2 shows the
indirect addressing concept.
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 12 / 118
Instruction Sets of PIC16F877
We have already mentioned that microcontroller is not like any other
integrated circuit. When they come out of production most integrated
circuits are ready to be built into devices which is not the case with
microcontrollers. In order to ”make” microcontroller perform a task, we
have to tell it exactly what to do, or in other words we must write the
program microcontroller will execute. We will describe in this chapter
instructions which make up the assembler, or lower-level program language
for PIC microcontrollers.
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 13 / 118
Instruction Sets of PIC16F877
Complete set which includes 35 instructions is given in the following table.
A reason for such a small number of instructions lies primarily in the fact
that we are talking about a RISC microcontroller whose instructions are
well optimized considering the speed of work, architectural simplicity and
code compactness. The only drawback is that programmer is expected to
master ”uncomfortable” technique of using a reduced set of 35
instructions.
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 14 / 118
Instruction Sets of PIC16F877
The instruction sets in PIC is developed by the basis of RISC structure.
The instruction sets can be classified into 5 separate categories (depends
on the basis of type of operation). They are
1 DATA TRANSFER GROUP
2 ARITHMETIC AND LOGIC OPERATIONS GROUP
3 BIT OPERATION GROUP INSTRUCTIONS
4 PROGRAM FLOW CONTROL
5 Other instructions (Explained along with Program Flow Control)
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 15 / 118
Guidelines from Microchip Technology
For writing assembly language program Microchip Technology has
suggested the following guidelines.
1 Write instruction mnemonics in lower case. (e.g., movwf)
2 Write the special register names, RAM variable names and bit names
in upper case. (e.g., PCL, RP0, etc.)
3 Write instructions and subroutine labels in mixed case. (e.g.,
Mainline, LoopTime)
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 16 / 118
Instruction Set: I
The instruction set for PIC16C74A consists of only 35 instructions. Some
of these instructions are byte oriented instructions and some are bit
oriented instructions.
The byte oriented instructions that require two parameters (For example,
movf f, F(W)) expect the f to be replaced by the name of a special
purpose register (e.g., PORTA) or the name of a RAM variable (e.g.,
NUM1), which serves as the source of the operand. ’f’ stands for file
register. The F(W) parameter is the destination of the result of the
operation. It should be replaced by:
F, if the destination is to be the source register.
W, if the destination is to be the working register (i.e., Accumulator or W
register).
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 17 / 118
Instruction Set: II
The bit oriented instructions also expect parameters (e.g., btfsc f, b). Here
’f’ is to be replaced by the name of a special purpose register or the name
of a RAM variable. The ’b’ parameter is to be replaced by a bit number
ranging from 0 to 7.
Example
For example: Z equ 2
btfsc STATUS, Z
Z has been equated to 2. Here, the instruction will test the Z bit of the
STATUS register and will skip the next instruction if Z bit is clear.
The literal instructions require an operand having a known value (e.g.,
0AH) or a label that represents a known value.
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 18 / 118
Instruction Set: III
Example
For example: NUM equ 0AH ; Assigns 0AH to the label NUM ( a constant
)
movlw NUM ; will move 0AH to the W register.
Every instruction fits in a single 14-bit word. In addition, every instruction
also executes in a single cycle, unless it changes the content of the
Program Counter. These features are due to the fact that PIC micro
controller has been designed on the principles of RISC (Reduced
Instruction Set Computer) architecture.
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 19 / 118
Summary of Instructions
Mnemonics Description Instruction
Cycles
bcf f, b
Clear bit b of register f 1
bsf f, b
Set bit b of register f 1
clrw
Clear working register W 1
clrf f
Clear f 1
movlw k
Move literal ’k’ to W 1
movwf f
Move W to f 1
movf f, F(W)
Move f to F or W 1
swapf f, F(W)
Swap nibbles of f, putting result in F or W 1
andlw k
And literal value into W 1
andwf f, F(W)
And W with F and put the result in W or F 1
andwf f, F(W)
And W with F and put the result in W or F 1
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 20 / 118
Summary of Instructions
Mnemonics Description Instruction
Cycles
iorlw k
inclusive-OR literal value into W 1
iorwf f, F(W)
inclusive-OR W with f and put the result in
F or W
1
xorlw k
Exclusive-OR literal value into W 1
xorwf f, F(W)
Exclusive-OR W with f and put the result in
F or W
1
addlw k
Add the literal value to W and store the result
in W
1
addwf f, F(W)
Add W to f and store the result in F or W 1
sublw k
Subtract the literal value from W and store
the result in W
1
subwf f, F(W)
Subtract f from W and store the result in F
or W
1
rlf f, F(W)
Copy f into F or W; rotate F or W left
through the carry bit
1
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 21 / 118
Summary of Instructions
Mnemonics Description Instruction
Cycles
rrf f, F(W)
Copy f into F or W; rotate F or W right
through the carry bit
1
btfsc f, b
Test ’b’ bit of the register f and skip the next
instruction if bit is clear
1 / 2
btfss f, b
Test ’b’ bit of the register f and skip the next
instruction if bit is set
1 / 2
decfsz f, F(W)
Decrement f and copy the result to F or W;
skip the next instruction if the result is zero
1 / 2
incfcz f, F(W)
Increment f and copy the result to F or W;
skip the next instruction if the result is zero
1 / 2
goto label
Go to the instruction with the label ”label” 2
call label
Go to the subroutine ”label”, push the Pro-
gram Counter in the stack
2
retrun
Return from the subroutine, POP the Pro-
gram Counter from the stack
2
retlw k
Retrun from the subroutine, POP the Pro-
gram Counter from the stack; put k in W
2
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 22 / 118
Summary of Instructions
Mnemonics Description Instruction
Cycles
retie
Return from Interrupt Service Routine and
re-enable interrupt
2
clrwdt
Clear Watch Dog Timer 1
sleep
Go into sleep/ stand by mode 1
nop No operation 1
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 23 / 118
Encoding of instruction:
As has been discussed, each instruction is of 14-bit long. These 14-bits
contain both op-code and the operand. Some examples of instruction
encoding are shown here.
Example
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 24 / 118
Encoding of instruction:
Example
Figure : Example 2
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 25 / 118
Instruction Execution Period
All instructions are executed in one cycle except for conditional branch
instructions if condition was true, or if the contents of program counter
was changed by some instruction. In that case, execution requires two
instruction cycles, and the second cycle is executed as NOP (No
Operation). Four oscillator clocks make up one instruction cycle. If we are
using an oscillator with 4MHz frequency, the normal time for executing an
instruction is 1 µs, and in case of conditional branching, execution period
is 2 µs.
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 26 / 118
Pipelining of instruction fetch successive addressing I
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 27 / 118
Pipelining of instruction fetch successive addressing II
Figure : Pipelining of instruction fetch successive addressing
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 28 / 118
Pipelining of instruction fetch successive addressing III
Figure : Introduction of extra cycle for a jump/goto instruction
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 29 / 118
Word list
f any memory location in a microcontroller
(0x00 to 0x7F)
W work register
b bit position in ’f’ register
d destination bit (d=0:store result in W,
d=1:store result in file register f. Default
is d=1)
label group of eight characters which marks the
beginning of a part of the program
TOS top of stack
[] option
<> bit position inside register
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 30 / 118
Instruction set - Group Wise I
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 31 / 118
Instruction set - Group Wise II
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 32 / 118
1. MOVLW
MOVLW
Syntax MOVLW k
Desciption 8 - bit contents of k is written in W register
Operation k ⇒ (W )
Operand 0 ≤ k ≤ 255
Flags –
Number of words 1
Number of cycles 1
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 33 / 118
MOVLW
Example
MOVLW 0x5A
After execution: W = 0x5A
Example
MOVLW REGISTER
Before execution: W = 0x10 and REGISTER = 0x40
After execution: W=0x40
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 34 / 118
2. MOVWF
MOVWF
Syntax MOVWF f
Desciption contents of W register is copied to f register
Operation W ⇒ (f )
Operand 0 ≤ f ≤ 127
Flags –
Number of words 1
Number of cycles 1
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 35 / 118
MOVWF
Example
MOVWF OPTION REG
Before execution: OPTION REG = 0x20
W=0x40
After execution: OPTION REG = 0x40
W=0x40
Example
MOVWF INDF
Before execution: W=0x17
FSR = 0xC2
Address contents 0xC2 = 0x00
After execution: W=0x17
FSR = 0xC2
Address contents 0xC2 = 0x17
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 36 / 118
3. MOVF f, d
MOVF f, d
Syntax MOVF f, d
Desciption Contents of f register is stored in location
determined by d operand.
If d = 0, destination is W register
If d = 1, destination is f register itself
Option d=1 is used for testing the contents
of f register because execution of this in-
struction affects Z flag in STATUS register.
Operation f ⇒ (d)
Operand 0 ≤ f ≤ 127
Flags Z
Number of words 1
Number of cycles 1
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 37 / 118
MOVF f, d
Example
MOVF FSR, 0
Before execution: FSR = 0xC2
W=0x00
After execution: W=0xC2
Z=0
Example
MOVF INDF, 0
Before execution: W=0x17
FSR=0xC2
Address contents 0xC2=0x00
After execution: W=0x17
FSR = 0xC2
Address contents 0xC2=0x00
Z=1
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 38 / 118
4. CLRW
CLRW
Syntax CLRW
Desciption Contents of W register evens out to zero,
and Z flag in STATUS register is set to one
Operation 0 ⇒ (W )
Operand –
Flags Z
Number of words 1
Number of cycles 1
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 39 / 118
CLRW
Example
CLRW
Before execution: W = 0x55
After execution: W=0x00
Z=1
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 40 / 118
5. CLRF
CLRF
Syntax CLRF
Desciption Contents of f register evens out to zero, and
Z flag in STATUS register is set to one
Operation 0 ⇒ (f )
Operand 0 ≤ f ≤ 127
Flags Z
Number of words 1
Number of cycles 1
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 41 / 118
CLRF
Example
CLRF STATUS
Before execution: STATUS=0xC2
After execution: STATUS=0x00
Z=1
Example
CLRF INDF
Before execution: FSR=0xC2
Address contents 0xC2=0x33
After execution: FSR=0xC2
Address contents 0xC2=0x00
Z=1
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 42 / 118
6. SWAPF f, d
SWAPF f, d
Syntax SWAPF f, d
Desciption Upper and lower half of f register exchange
places
If d = 0, result is stored in W register
If d = 1, result is stored in f register itself
Option d=1 is used for testing the contents
of f register because execution of this in-
struction affects Z flag in STATUS register.
Operation f < 0 : 3 >⇒ d < 4 : 7 >, f < 4 : 7 >⇒
d < 0 : 3 >
Operand 0 ≤ f ≤ 127
Flags –
Number of words 1
Number of cycles 1
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 43 / 118
SWAPF f, d
Example
SWAP REG, 0
Before execution: REG=0xF3
After execution: REG=0xF3
W=0x3F
Example
SWAP REG, 1
Before execution: REG = 0xF3
After execution: REG=0x3F
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 44 / 118
7. ADDLW k
ADDLW k
Syntax ADDLW k
Desciption Contents of W register are added to 8-bit
contents of k and result is stored in W reg-
ister.
Operation (W ) + k ⇒ (W )
Operand 0 ≤ k ≤ 255
Flags C, DC, and Z
Number of words 1
Number of cycles 1
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 45 / 118
ADDLW k
Example
ADDLW 0x15
Before execution: W=0x10
After execution: W=0x25
Example
ADDLW REG
Before execution: W=0x10
Register contents REG=0x37
After execution: W=0x47
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 46 / 118
8. ADDWF f, d
ADDWF f, d
Syntax ADDWF f, d
Desciption Add contents of register W to register f.
If d = 0, result is stored in W register
If d = 1, result is stored in f register itself
Operation (W ) + (f ) ⇒ d
Operand 0 ≤ f ≤ 127
Flags C, DC, and Z
Number of words 1
Number of cycles 1
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 47 / 118
ADDWF f, d
Example
ADDWF FSR, 0
Before execution: W=0x17
FSR=0xC2
After execution: W=0xD9
FSR=0xC2
Example
ADDWF INDF, 1
Before execution: W=0x17
FSR = 0xC2
Address contents 0xC2=0x20
After execution= w=0x17
FSR=0xC2
Address contents 0xC2=0x37
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 48 / 118
9. SUBLW k
SUBLW k
Syntax SUBLW k
Desciption Contents of W register are subtracted from
k and result is stored in W register.
Operation k − (W ) ⇒ (W )
Operand 0 ≤ k ≤ 255
Flags C, DC, and Z
Number of words 1
Number of cycles 1
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 49 / 118
SUBLW k
Example
SUBLW 0x03
Before execution: W=0x01, C=X, Z=X
After execution: W=0x02, C=1, Z=0
Result > 0
Before execution: W=0x03, C=X, Z=X
After execution: W=0x00, C=1, Z=1
Result = 0
Before execution: W=0x04, C=X, Z=X
After execution: W=0xFF, C=0, Z=0
Result < 0
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 50 / 118
10. SUBWF f, d
SUBWF f, d
Syntax SUBWF f, d
Desciption Contents of register W is subtracted from
register f.
If d = 0, result is stored in W register
If d = 1, result is stored in f register itself
Operation (f ) − (W ) ⇒ d
Operand 0 ≤ f ≤ 127
Flags C, DC, and Z
Number of words 1
Number of cycles 1
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 51 / 118
SUBWF f, d
Example
SUBWF REG, 1
Before execution: REG = 3, W=2, C=X, Z=X
After execution: REG = 1, W=2, C=1, Z=0
Result ¿ 0
Before execution: REG = 2, W=2, C=X, Z=X
After execution: REG = 0, W=2, C=1, Z=1
Result = 0
Before execution: REG = 1, W=2, C=X, Z=X
After execution: REG = 0xFF, W=2, C=0, Z=0
Result ¡ 0
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 52 / 118
11. ANDLW k
ANDLW k
Syntax ANDLW k
Desciption Perform operation logic AND over the con-
tents of W register and constant k and re-
sult is stored in W register.
Operation kAND(W ) ⇒ (W )
Operand 0 ≤ k ≤ 255
Flags Z
Number of words 1
Number of cycles 1
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 53 / 118
ANDLW k
Example
ANDLW 0x5F
Before execution: W=0xA3
After execution: W=0x03
Example
ANDLW REG
Before execution: W=0xA3
REG = 0x37
After execution: W=0x23
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 54 / 118
12. ANDWF f, d
ANDWF f, d
Syntax ANDWF f, d
Desciption Perfrom operation of logic AND over the
contents of register W and register f.
If d = 0, result is stored in W register
If d = 1, result is stored in f register itself
Operation (W )AND(f ) ⇒ d
Operand 0 ≤ f ≤ 127
Flags Z
Number of words 1
Number of cycles 1
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 55 / 118
ANDWF f, d
Example
ANDWF FSR, 1
Before execution: W=0x17, FSR = 0xC2
After execution: W=0x17, FSR = 02
Example
ANDWF FSR, 0
Before execution: W=0x17, FSR = 0xC2
After execution: W=0x02, FSR = 0xC2
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 56 / 118
13. IORLW k
IORLW k
Syntax IORLW k
Desciption Operation logic OR is performed over the
contents of W register and over 8 bit con-
stant k, and the result is stored in W regis-
ter.
Operation kOR(W ) ⇒ (W )
Operand 0 ≤ k ≤ 255
Flags Z
Number of words 1
Number of cycles 1
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 57 / 118
IORLW k
Example
IORLW 0x35
Before execution: W=0x9A
After execution: W=0xBF
Z = 0
Example
IORLW REG
Before execution: REG = 0x37, W=0x9A After execution: W=0x9F
Z = 0
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 58 / 118
14. IORWF f, d
IORWF f, d
Syntax IORWF f, d
Desciption Perfrom operation of logical OR over the
contents of register W and register f.
If d = 0, result is stored in W register
If d = 1, result is stored in f register itself
Operation (W )OR(f ) ⇒ d
Operand 0 ≤ f ≤ 127
Flags Z
Number of words 1
Number of cycles 1
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 59 / 118
IORWF f, d
Example
IORWF REG, 0
Before execution: W=0x13, REG = 0x13
After execution: W=0x93, REG = 0x13
Z=0
Example
IORWF REG, 1
Before execution: W=0x91, REG = 0x13
After execution: W=0x91, REG = 0x93
Z=0
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 60 / 118
15. XORLW k
XORLW k
Syntax XORLW k
Desciption Operation exclusive OR is performed over
the contents of W register and over 8 bit
constant k, and the result is stored in W
register.
Operation kXOR(W ) ⇒ (W )
Operand 0 ≤ k ≤ 255
Flags Z
Number of words 1
Number of cycles 1
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 61 / 118
XORLW k
Example
XORLW 0xAF
Before execution: W=0xB5
After execution: W=0x1A
Example
XORLW REG
Before execution: REG = 0x37, W=0xAF After execution: W=0x18
Z = 0
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 62 / 118
16. XORWF f, d
XORWF f, d
Syntax XORWF f, d
Desciption Perfrom operation of exclusive OR over the
contents of register W and register f.
If d = 0, result is stored in W register
If d = 1, result is stored in f register itself
Operation (W )OR(f ) ⇒ d
Operand 0 ≤ f ≤ 127
Flags Z
Number of words 1
Number of cycles 1
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 63 / 118
XORWF f, d
Example
XORWF REG, 0
Before execution: W=0xB5, REG = 0xAF
After execution: W=0x1A, REG = 0xAF
Z=0
Example
XORWF REG, 1
Before execution: W=0xB5, REG = 0xAF
After execution: W=0xB5, REG = 0x1A
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 64 / 118
17. INCF f, d
INCF f, d
Syntax INCF f, d
Desciption Increment register f by one.
If d = 0, result is stored in W register
If d = 1, result is stored in f register itself
Operation (f ) + 1 ⇒ d
Operand 0 ≤ f ≤ 127
Flags Z
Number of words 1
Number of cycles 1
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 65 / 118
INCF f, d
Example
INCF REG, 0
Before execution: W=X, REG = 0x10, Z=0
After execution: W=0x11, REG = 0x10, Z=0
Example
INCF REG, 1
Before execution: REG = 0xFF, Z=0
After execution: REG = 0x00, Z=1
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 66 / 118
18. DECF f, d
DECF f, d
Syntax DECF f, d
Desciption Decrement register f by one.
If d = 0, result is stored in W register
If d = 1, result is stored in f register itself
Operation (f ) − 1 ⇒ d
Operand 0 ≤ f ≤ 127
Flags Z
Number of words 1
Number of cycles 1
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 67 / 118
DECF f, d
Example
DECF REG, 0
Before execution: W=X, REG = 0x13, Z=0
After execution: W=0x12, REG = 0x13, Z=0
Example
DECF REG, 1
Before execution: REG = 0x01, Z=0
After execution: REG = 0x00, Z=1
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 68 / 118
19. RLF f, d
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 69 / 118
20. RRF f, d
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 70 / 118
21. COMF f, d
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 71 / 118
22. BCF f, d
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 72 / 118
23. BSF f, d
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 73 / 118
24. BTFSC f, b
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 74 / 118
25. BTFSS f, b
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 75 / 118
26. INCFSZ f, d
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 76 / 118
27. DECFSZ f, d
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 77 / 118
28. GOTO k
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 78 / 118
29. CALL k
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 79 / 118
30. RETURN
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 80 / 118
31. RETLW k
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 81 / 118
32. RETFIE
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 82 / 118
33. NOP
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 83 / 118
34. CLRWDT
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 84 / 118
35. SLEEP
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 85 / 118
Pseudo Instructions
It is the instruction used to inform the Assembler about conversion of
Assembly Program into Machine Language Program. It is also called
pseudo or dummy instruction because these instructions are not executed
by Microcontroller. Rather, they are executed by the Assembler. In other
words, we say that Assembler Directives is used for Assembling and they
are not consuming any Memory Location because they are not executed by
Microcontroller.
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 86 / 118
Pseudo Instructions... I
1 ORG: (ORIGIN)
ORG Assembler Directive is used to initialize starting Memory
Location of the program. There is no memory space allocated for the
ORG Assembler Directive. In the program, ORG is always initialized
at the beginning.
In some of the Assemblers, ORG is used instead of ORG Assembler
Directive. ORG Assembler Directive is always followed by a number
which may be represented in decimal or hexadecimal. If the given
number is in the decimal, then writing D at the end of the number is
optional. But when given number is in the hexadecimal, then put H
at the end of the number. Whenever the number is given in any
format, assembler is always converted into the hexadecimal.
FORMAT OF ORG:
ORG 100 i.e. ORG 0064H, ORG 100H, ORG $
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 87 / 118
Pseudo Instructions... II
2 DB: (Define Byte):
This Assembler Directive is used to allocate 1 Memory Location to
each byte. So one memory location is reserved. Along with DB
Assembler Directive, 8-bit number is given either in decimal, binary or
in hexadecimal. If the number is given in the decimal format, then it
is optional to end the number with ’D’. But when the number is given
in the binary format, then put B at the end of the number. If the
number is given in hexadecimal, then put H at the end of the
number. Regardless of this number system, Assembler always
converts the given number into the hexadecimal. Apart from this
number system, it is also possible to allocate or give ASCII numbers
or characters with this DB Assembler Directive. To give the ASCII
value, it always includes either in single or double Quotation mark.
Quotation mark indicates the given string represented in ASCII. Its
simple application is used to display program in LCD Screen. So we
are using DB, to save more number of bytes.
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 88 / 118
Pseudo Instructions... III
Format of DB:
DB 12((12) d means (0CH). So the starting Memory Location is
allocated.)
DB 1001 0101B (binary is represented as 95H in Hexadecimal.)
DB 45H(Given Number in Hexadecimal.)
DB ’BHEL’/ DB ”BHEL”, both are possible. Since the single
Inverted Comma is possible in Simulator and the Double Inverted
Comma is possible.
For example: ORG 0500H
0500H → DATA1: DB 45H, 67H, 01H.
Given the Data starting memory Location is 0500H.
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 89 / 118
Pseudo Instructions... IV
3 EQU: (EQUATE)
This Assembler Directive is used to assign constant value to any
value. For example: Label EQU 1000H, then after executing this, the
value of Label becomes 1000H.
Format:
Label EQU value or Expression which may be Register, Accumulator,
Jump.
Use of Equate:
Consider the example of Counter Value. Assume Counter value is 25,
then using EQU it may be defined as
COUNT EQU 25
MOV R2, #Count
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 90 / 118
Pseudo Instructions... V
4 END
END means the end of Assembly Process. So, no instruction either
belonging to Assembler or microcontroller is executed after END.
END gives only once in a program and that to be in the last
indicating end of Assembly Process. Some Assembler will Support
.END instead of END Assembler Directive.
5 SET
This directive is used to define a constant value or a fixed address. In
this regard, the SET and EQU directives are identical. The only
difference is the value assigned by the SET directive may be
reassigned later.
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 91 / 118
Pseudo Instructions... VI
6 LIST directives
Unline ORG and END, which are used by all assemblers, the LIST
directive is unique to the PIC assembler. It indicates to the assembler
the specific PIC chip for which the program should be assembled. It is
used as follows:
LIST P = 16F877
The above tells the PIC assembler to assemble the program
specifically for the PIC16F877 microcontroller. We use LIST to state
the target chip.
7 #include directive
The #include directive tells the PIC assembler to use the libraries
associated with the specific chip for which we are compiling the
program.
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 92 / 118
Pseudo Instructions... VII
8 config directive
The config directive tells the assembler the configuration bits for the
targeted PIC chip.
9 radix directive
we can use the radix directive to indicate whether the numbering
system is hexadecimal or decimal. The default is hex if we do not use
the radix directive.
Example: radix dec
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 93 / 118
Assembly and C Language Programming
Program 1
Write an assembly language programming to add two 8-bit numbers 28h
and 56h and store the result in register W.
Solution
Instructions Comments
movlw 0x28 ; copy data 28h to the W register
movwf 0x20 ; copy contents of W register to memory location/file reg-
ister 20h
movlw 0x56 ; copy data 56h to the W register
addwf 0x20, 0 ; add contents of W register with the contents of file register
20h and store the result in W register
goto $ ; stop execution
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 94 / 118
Simulation - Program 1
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 95 / 118
Assembly and C Language Programming
Program 2
Write a program to copy the value 55H into RAM locations 40h to 44h
using
(A) Direct addressing mode
(B) Register indirect addressing mode
(C) A loop
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 96 / 118
Solution - (A)
Instructions Comments
org 0x0004 ; interrupt vector location
main: ; start of program
MOVLW 0x55 ; copy data 55h to W register
MOVWF 0x40 ; copy contents of W register to memory location/FSR 40h
MOVWF 0x41
MOVWF 0x42
MOVWF 0x43
MOVWF 0x44
goto $
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 97 / 118
Solution - (B)
Instructions Comments
org 0x0004 ; interrupt vector location
main: ; start of program
MOVLW 0x55 ; copy data 55h to W register
MOVWF 0x40 ; copy contents of W register to memory location/FSR 40h
MOVWF 0x41
MOVWF 0x42
MOVWF 0x43
MOVWF 0x44
goto $
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 98 / 118
Simulation - Program 1
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 99 / 118
Assembly and C Language Programming
Program 3
Write an assembly language programming to add two 16-bit numbers
present in the memory location 21h-20h and 31h-30h and store the result
in memory location 31h-30h.
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 100 / 118
Assembly and C Language Programming
Solution
Instructions Comments
org 0x0000
x EQU 0x20 ; Define memory location 20h
y EQU 0x30 ; Define memory location 30h
nop ; nop required for icd
goto main ; go to beginning of program
org 0x0004 ; interrupt vector location
main:
movf x,W ; copy contents of 20h to reg W
addwf y,f ; Add contents of Reg W and Mem Loc 30h and store result in ML 30
btfsc STATUS,C ; Check for carry if C = 1 add 1 to the contents of ML 30h
incf y+1,f
movf x+1,W ; copy contents of ML 21h to Reg W
addwf y+1,f ; Add contents of Reg W and Mem Loc 31h and store result in ML 31
goto $ ; stop execution
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 101 / 118
Simulation - Program 3
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 102 / 118
Assembly and C Language Programming
Program 4
Write an assembly language programming to add two 32-bit numbers
present in the memory location 23h to 20h and 33h to 30h and store the
result in memory location 33h to 30h.
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 103 / 118
Assembly and C Language Programming
Solution
Instructions Comments
org 0x0000
x EQU 0x20 ; Define memory location 20h
y EQU 0x30 ; Define memory location 30h
nop ; nop required for icd
goto main ; go to beginning of program
org 0x0004 ; interrupt vector location
main:
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 104 / 118
Assembly and C Language Programming
Solution
Instructions Comments
movf x,W
addwf y,f ; add byte 0 (LSB)
movf x+1, W
btfsc STATUS, C
incfsz x+1, W
addwf y+1,f ; add byte 1
movf x+2, W
btfsc STATUS, C
incfsz x+2, W
addwf y+2,f ; add byte 2
movf x+3, W
btfsc STATUS, C
incfsz x+3, W
addwf y+3,f ; add byte 3
goto $ ; stop execution
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 105 / 118
Simulation - Program 4
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 106 / 118
Assembly and C Language Programming
Program 5
Write an assembly language programming to subtract two 16-bit numbers
present in the memory location 21h-20h (LSB) and 31h-30h (MSB) and
store the result in memory location 31h-30h.
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 107 / 118
Assembly and C Language Programming
Solution
Instructions Comments
org 0x0000
x EQU 0x20 ; Define memory location 20h
y EQU 0x30 ; Define memory location 30h
nop ; nop required for icd
goto main ; go to beginning of program
org 0x0004 ; interrupt vector location
main:
movf x, W ; copy contents of ML 20h to Reg W
subwf y, f ; subtract LSB y-w put result in y
movf x+1, W ; get MSB
btfss STATUS, C ; if borrow from LSB subtraction
incfsz x+1, W ; increment copy of MSB
subwf y+1, f ; subtract MSB
goto $ ; stop execution
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 108 / 118
Simulation - Program 5
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 109 / 118
Assembly and C Language Programming
Program 6
Write an assembly language programming to subtract two 32-bit numbers
present in the memory location 23h to 20h and 33h to 30h and store the
result in memory location 33h to 30h.
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 110 / 118
Assembly and C Language Programming
Solution
Instructions Comments
org 0x0000
x EQU 0x20 ; Define memory location 20h
y EQU 0x30 ; Define memory location 30h
nop ; nop required for icd
goto main ; go to beginning of program
org 0x0004 ; interrupt vector location
main:
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 111 / 118
Assembly and C Language Programming
Solution
Instructions Comments
movf x,W
subwf y,f ; subtract byte 0 (LSB)
movf x+1, W
btfss STATUS, C
incfsz x+1, W
subwf y+1,f ; subtract byte 1
movf x+2, W
btfss STATUS, C
incfsz x+2, W
subwf y+2,f ; subtract byte 2
movf x+3, W
btfss STATUS, C
incfsz x+3, W
subwf y+3,f ; subtract byte 3
goto $ ; stop execution
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 112 / 118
Que - 1
2017 (SLR - TJ-205) - Marks 1
The CALL and GOTO instruction provides . . . . . . bits of address to allow
branching within any . . . . . . program memory page.
(a) 13 bit, 8k
(b) 16 bit, 64k
(c) 11 bit, 2k
(d) none of these
Answer
(c) 11 bit, 2k
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 113 / 118
Que - 2
2017 (SLR-VB-179) - Marks 4
Explain the logical instructions named as XORLW, XORWF, IORLW AND
IORWF
Answer
Explain instructions 13, 14, 15 and 16
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 114 / 118
Que - 3
2016 (SLR-EP-142) - Marks 1
Before execution of ANDLW 0x5F the working register contents were
0xA3. The contents after execution will be
(a) 0x5F
(b) 0x03
(c) 0xFF
(d) none of these
Answer
(b) 0x03
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 115 / 118
Que - 4
2016 (SLR-EP-142) - Marks 5
Explain logical instructions related to PIC
Answer
Explain instructions 11, 12, 13, 14, 15 and 16
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 116 / 118
References
[1, 2, 3, 4]
Muhammad Ali Mazidi, Rolin D. McKinlay, and Danny Causey.
PIC Microcontroller and Embedded Systems - Using Assembly and C
for PIC18.
Pearson International Edition, Upper Saddle River, NJ, 2008.
Martin P. Bates.
Programming 8 - bit PIC Microcontrollers in C with Interactive
Hardware Simulation.
Newnes Press Private Limited, United Kingdom, 2008.
Ajay V. Deshmukh.
Microcontrollers Theory and Applications.
Tata McGraw Hill, New Delhi, India, 2008.
John B. Peatman.
Design with PIC Microcontroller.
Prentice Hall, New Delhi, India, 1997.
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 117 / 118
Thank you
Please send your feedback at nbahadure@gmail.com
For download [Click Here]
Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 118 / 118

More Related Content

PPTX
Pic 18 microcontroller
PPTX
8051 Microcontroller PPT's By Er. Swapnil Kaware
PPTX
Microcontroller 8096
PPTX
INTRODUCTION TO MICROCONTROLLER
PPT
ARM Micro-controller
PPTX
Addressing modes of 8051
PPTX
Microprocessor Fundamentals
Pic 18 microcontroller
8051 Microcontroller PPT's By Er. Swapnil Kaware
Microcontroller 8096
INTRODUCTION TO MICROCONTROLLER
ARM Micro-controller
Addressing modes of 8051
Microprocessor Fundamentals

What's hot (20)

PPTX
Interfacing Stepper motor with 8051
PPTX
Architecture of 8051
PDF
Microcontroller pic 16 f877 registers memory ports
DOCX
8085 interfacing with memory chips
DOCX
ARM7-ARCHITECTURE
PPTX
Interrupts of microprocessor 8085
PPT
Memory organization of 8051
PPT
Architecture of 8086 Microprocessor
PPTX
8086 microprocessor-architecture
PDF
Unit II arm 7 Instruction Set
PDF
Unit II Study of Onchip Peripherals
PDF
DAC Interfacing with 8051.pdf
PPTX
ARM Exception and interrupts
PPTX
Pic microcontroller architecture
PPTX
LCD Interacing with 8051
PPTX
Introducion to MSP430 Microcontroller.pptx
PPTX
8051 MICROCONTROLLER ARCHITECTURE.pptx
PPTX
Interrupts in pic
PPTX
8255 PPI
PDF
Arm instruction set
Interfacing Stepper motor with 8051
Architecture of 8051
Microcontroller pic 16 f877 registers memory ports
8085 interfacing with memory chips
ARM7-ARCHITECTURE
Interrupts of microprocessor 8085
Memory organization of 8051
Architecture of 8086 Microprocessor
8086 microprocessor-architecture
Unit II arm 7 Instruction Set
Unit II Study of Onchip Peripherals
DAC Interfacing with 8051.pdf
ARM Exception and interrupts
Pic microcontroller architecture
LCD Interacing with 8051
Introducion to MSP430 Microcontroller.pptx
8051 MICROCONTROLLER ARCHITECTURE.pptx
Interrupts in pic
8255 PPI
Arm instruction set
Ad

Similar to Microcontroller pic 16f877 addressing modes instructions and programming (20)

PPT
Microprocessor and Microcontroller Based Systems.ppt
PPT
Microchip's PIC Micro Controller
PPT
My seminar new 28
PPTX
Instruction Set Architecture – II
PDF
UNIT-V (PIC16F877-Microcontrollers).pdf
PPTX
Instruction Set Architecture
PPTX
Pic16cxx instruction set
PPTX
2. Instruction Set.pptx huishiuhfuidhiuhdfuhdu
PPTX
Module -4_microprocessor (1).pptx
PDF
Introduction to Microcontrollers
PDF
Instruction execution cycle _
PPTX
PIC introduction + mapping
PPTX
MODULE-3.pptx cnjndviuseiuhfviushfiuhiafuhiushfiuhfs
PPTX
R.S.Gaonker Chapter3(all questions)_23U01054_Mukul Bhardwaj.pptx
PDF
Embedded system (Chapter 2) part 2
DOCX
digital communication,micro processor,pulse and digital circuits
PDF
CO Unit 3.pdf (Important chapter of coa)
PDF
Ch2 microcontroller architecture
PPTX
instruction format.pptx
PPTX
8085 architecture
Microprocessor and Microcontroller Based Systems.ppt
Microchip's PIC Micro Controller
My seminar new 28
Instruction Set Architecture – II
UNIT-V (PIC16F877-Microcontrollers).pdf
Instruction Set Architecture
Pic16cxx instruction set
2. Instruction Set.pptx huishiuhfuidhiuhdfuhdu
Module -4_microprocessor (1).pptx
Introduction to Microcontrollers
Instruction execution cycle _
PIC introduction + mapping
MODULE-3.pptx cnjndviuseiuhfviushfiuhiafuhiushfiuhfs
R.S.Gaonker Chapter3(all questions)_23U01054_Mukul Bhardwaj.pptx
Embedded system (Chapter 2) part 2
digital communication,micro processor,pulse and digital circuits
CO Unit 3.pdf (Important chapter of coa)
Ch2 microcontroller architecture
instruction format.pptx
8085 architecture
Ad

More from Nilesh Bhaskarrao Bahadure (20)

PDF
Biomedical Signal Origin and Dynamics
PDF
Introduction to Medical Image Processing
PDF
Timing diagram of microprocessor 8085
PDF
Timers and counters of microcontroller 8051
PDF
Serial communication of microcontroller 8051
PDF
Peripherals of Microprocessor 8085
PDF
Microprocessor 8085 Basics
PDF
Microcontroller 8051 instruction set and assemble directives
PDF
Microcontroller 8051 basics (part I)
PDF
Memory interfacing of microprocessor 8085
PDF
Memory interfacing of microcontroller 8051
PDF
Interrupts of microprocessor 8085
PDF
Interrupts of microcontroller 8051
PDF
Instruction sets of microprocessor 8085
PDF
Embedded Systems
PDF
Basic Electronics Semiconductor Diodes
PDF
Basic Electronics Electrical Transducers
PDF
Basic Electronics BJT
PDF
Applications of Microcontroller 8051
PDF
Question Bank Programmable Logic Controller
Biomedical Signal Origin and Dynamics
Introduction to Medical Image Processing
Timing diagram of microprocessor 8085
Timers and counters of microcontroller 8051
Serial communication of microcontroller 8051
Peripherals of Microprocessor 8085
Microprocessor 8085 Basics
Microcontroller 8051 instruction set and assemble directives
Microcontroller 8051 basics (part I)
Memory interfacing of microprocessor 8085
Memory interfacing of microcontroller 8051
Interrupts of microprocessor 8085
Interrupts of microcontroller 8051
Instruction sets of microprocessor 8085
Embedded Systems
Basic Electronics Semiconductor Diodes
Basic Electronics Electrical Transducers
Basic Electronics BJT
Applications of Microcontroller 8051
Question Bank Programmable Logic Controller

Recently uploaded (20)

PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PDF
Structs to JSON How Go Powers REST APIs.pdf
PPT
Drone Technology Electronics components_1
PPTX
The-Looming-Shadow-How-AI-Poses-Dangers-to-Humanity.pptx
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
ANIMAL INTERVENTION WARNING SYSTEM (4).pptx
PPTX
Internship_Presentation_Final engineering.pptx
PPTX
bas. eng. economics group 4 presentation 1.pptx
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
Fluid Mechanics, Module 3: Basics of Fluid Mechanics
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPTX
Lecture Notes Electrical Wiring System Components
PDF
오픈소스 LLM, vLLM으로 Production까지 (Instruct.KR Summer Meetup, 2025)
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
web development for engineering and engineering
DOCX
573137875-Attendance-Management-System-original
PDF
ETO & MEO Certificate of Competency Questions and Answers
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
Sustainable Sites - Green Building Construction
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
Structs to JSON How Go Powers REST APIs.pdf
Drone Technology Electronics components_1
The-Looming-Shadow-How-AI-Poses-Dangers-to-Humanity.pptx
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
ANIMAL INTERVENTION WARNING SYSTEM (4).pptx
Internship_Presentation_Final engineering.pptx
bas. eng. economics group 4 presentation 1.pptx
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Fluid Mechanics, Module 3: Basics of Fluid Mechanics
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Lecture Notes Electrical Wiring System Components
오픈소스 LLM, vLLM으로 Production까지 (Instruct.KR Summer Meetup, 2025)
Embodied AI: Ushering in the Next Era of Intelligent Systems
web development for engineering and engineering
573137875-Attendance-Management-System-original
ETO & MEO Certificate of Competency Questions and Answers
Internet of Things (IOT) - A guide to understanding
Sustainable Sites - Green Building Construction

Microcontroller pic 16f877 addressing modes instructions and programming

  • 1. Microcontroller (PIC) PIC Microcontroller Programming Dr. Nilesh Bhaskarrao Bahadure [email protected] https://www.sites.google.com/site/nileshbbahadure/home July 1, 2021 Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 1 / 118
  • 2. Overview 1 Syllabus 2 Course Objectives & Outcomes 3 Objectives 4 Addressing Modes Immediate addressing mode Register operand addressing mode Memory operand addressing mode Direct addressing mode Indirect addressing mode 5 Instruction Sets of PIC16F877 Guidelines from Microchip Technology Instruction Set: Summary of Instructions Encoding of instruction: Instruction Execution Period Pipelining of instruction fetch successive addressing Word list Instruction set - Group Wise Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 2 / 118
  • 3. Syllabus Unit Heading No. of Lectures PIC Microcontroller Program- ming No of lectures - 5 Addressing modes, Instruction set, Assembly Language and C pro- gramming. Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 3 / 118
  • 4. Course Objectives 1 To expose the students to the fundamentals of PIC Microcontroller 16F877 architecture and its Peripherals. 2 To introduce the advanced features in PIC Microcontroller 16F877. 3 To make student develop and practice assembly language and C language programming techniques 4 To enable student demonstrate and perform hardware interfacing and design. Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 4 / 118
  • 5. Course Outcomes At the end of the course, the student shall be able to, 1 Describe how PIC microcontroller and its peripherals function. 2 Interpret advanced features in PIC Microcontroller 16F877. 3 Program an embedded system in assembly and C. 4 Design, implement and test a single-processor embedded systems for real-time applications Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 5 / 118
  • 6. Objectives Upon completion of this chapter, you will be able to: 1 Understanding of addressing modes and its importance in writing programs 2 Manipulate data using the WREG and MOVE instructions 3 Perform simple operations such as ADD and MOVE using the file register and access bank in the PIC microcontroller 4 Code simple PIC assembly language programming 5 Details the execution of PIC assembly language instructions Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 6 / 118
  • 7. Addressing Modes To know the working principal and data handling, we need to have clear knowledge on addressing modes of pic microcontroller. Now we can see that how we can categorise different addressing modes of pic microcontroller. In PIC micro controller, it having mainly five addressing modes. Those are 1 Immediate addressing mode 2 Register operand addressing mode 3 Memory operand addressing mode 4 Direct addressing 5 Indirect addressing. Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 7 / 118
  • 8. Immediate addressing mode In this addressing mode, the operand is a number or constant not an address as MOVLW 43h, the operand here is data not address. So in this addressing mode of pic microcontroller data is directly transfer. And data is immediate after the opcode. That is why this type of addressing is called immediate addressing. This way is fast in execution. Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 8 / 118
  • 9. Register operand addressing mode In this addressing mode, the operand is a Register which holds the data to be execute. Register operand addressing mode deals with the registers like: CLR W Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 9 / 118
  • 10. Memory operand addressing mode In this addressing mode, the operand is an address of Memory location which holds the data to be execute. Again memory operand addressing mode is under two category Direct addressing like CLRF 13h. We deal with the address or the memory location. Indirect addressing. we use in it INDF and FSR registers. Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 10 / 118
  • 11. Direct addressing mode Direct Addressing is done through a 9-bit address. This address is obtained by connecting 7th bit of direct address. By using an instruction with two bits (RP1, RP0) from STATUS register. this is shown in Figure 1. Any access to SFR registers can be an example of direct addressing. Figure : Direct addressing mode Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 11 / 118
  • 12. Indirect addressing mode It does not take an address from an instruction. But it derives from IRP bit of STATUS and FSR registers. Addressed location is accessed through INDF register. And INDF register in fact holds the address indicated by the FSR. Indirect addressing is very convenient for manipulating data arrays located in GPR registers. In this case, it is necessary to initialise FSR register with a starting address of the array, and the rest of the data can be accessed by increment the FSR register. Figure 2 shows the indirect addressing concept. Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 12 / 118
  • 13. Instruction Sets of PIC16F877 We have already mentioned that microcontroller is not like any other integrated circuit. When they come out of production most integrated circuits are ready to be built into devices which is not the case with microcontrollers. In order to ”make” microcontroller perform a task, we have to tell it exactly what to do, or in other words we must write the program microcontroller will execute. We will describe in this chapter instructions which make up the assembler, or lower-level program language for PIC microcontrollers. Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 13 / 118
  • 14. Instruction Sets of PIC16F877 Complete set which includes 35 instructions is given in the following table. A reason for such a small number of instructions lies primarily in the fact that we are talking about a RISC microcontroller whose instructions are well optimized considering the speed of work, architectural simplicity and code compactness. The only drawback is that programmer is expected to master ”uncomfortable” technique of using a reduced set of 35 instructions. Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 14 / 118
  • 15. Instruction Sets of PIC16F877 The instruction sets in PIC is developed by the basis of RISC structure. The instruction sets can be classified into 5 separate categories (depends on the basis of type of operation). They are 1 DATA TRANSFER GROUP 2 ARITHMETIC AND LOGIC OPERATIONS GROUP 3 BIT OPERATION GROUP INSTRUCTIONS 4 PROGRAM FLOW CONTROL 5 Other instructions (Explained along with Program Flow Control) Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 15 / 118
  • 16. Guidelines from Microchip Technology For writing assembly language program Microchip Technology has suggested the following guidelines. 1 Write instruction mnemonics in lower case. (e.g., movwf) 2 Write the special register names, RAM variable names and bit names in upper case. (e.g., PCL, RP0, etc.) 3 Write instructions and subroutine labels in mixed case. (e.g., Mainline, LoopTime) Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 16 / 118
  • 17. Instruction Set: I The instruction set for PIC16C74A consists of only 35 instructions. Some of these instructions are byte oriented instructions and some are bit oriented instructions. The byte oriented instructions that require two parameters (For example, movf f, F(W)) expect the f to be replaced by the name of a special purpose register (e.g., PORTA) or the name of a RAM variable (e.g., NUM1), which serves as the source of the operand. ’f’ stands for file register. The F(W) parameter is the destination of the result of the operation. It should be replaced by: F, if the destination is to be the source register. W, if the destination is to be the working register (i.e., Accumulator or W register). Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 17 / 118
  • 18. Instruction Set: II The bit oriented instructions also expect parameters (e.g., btfsc f, b). Here ’f’ is to be replaced by the name of a special purpose register or the name of a RAM variable. The ’b’ parameter is to be replaced by a bit number ranging from 0 to 7. Example For example: Z equ 2 btfsc STATUS, Z Z has been equated to 2. Here, the instruction will test the Z bit of the STATUS register and will skip the next instruction if Z bit is clear. The literal instructions require an operand having a known value (e.g., 0AH) or a label that represents a known value. Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 18 / 118
  • 19. Instruction Set: III Example For example: NUM equ 0AH ; Assigns 0AH to the label NUM ( a constant ) movlw NUM ; will move 0AH to the W register. Every instruction fits in a single 14-bit word. In addition, every instruction also executes in a single cycle, unless it changes the content of the Program Counter. These features are due to the fact that PIC micro controller has been designed on the principles of RISC (Reduced Instruction Set Computer) architecture. Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 19 / 118
  • 20. Summary of Instructions Mnemonics Description Instruction Cycles bcf f, b Clear bit b of register f 1 bsf f, b Set bit b of register f 1 clrw Clear working register W 1 clrf f Clear f 1 movlw k Move literal ’k’ to W 1 movwf f Move W to f 1 movf f, F(W) Move f to F or W 1 swapf f, F(W) Swap nibbles of f, putting result in F or W 1 andlw k And literal value into W 1 andwf f, F(W) And W with F and put the result in W or F 1 andwf f, F(W) And W with F and put the result in W or F 1 Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 20 / 118
  • 21. Summary of Instructions Mnemonics Description Instruction Cycles iorlw k inclusive-OR literal value into W 1 iorwf f, F(W) inclusive-OR W with f and put the result in F or W 1 xorlw k Exclusive-OR literal value into W 1 xorwf f, F(W) Exclusive-OR W with f and put the result in F or W 1 addlw k Add the literal value to W and store the result in W 1 addwf f, F(W) Add W to f and store the result in F or W 1 sublw k Subtract the literal value from W and store the result in W 1 subwf f, F(W) Subtract f from W and store the result in F or W 1 rlf f, F(W) Copy f into F or W; rotate F or W left through the carry bit 1 Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 21 / 118
  • 22. Summary of Instructions Mnemonics Description Instruction Cycles rrf f, F(W) Copy f into F or W; rotate F or W right through the carry bit 1 btfsc f, b Test ’b’ bit of the register f and skip the next instruction if bit is clear 1 / 2 btfss f, b Test ’b’ bit of the register f and skip the next instruction if bit is set 1 / 2 decfsz f, F(W) Decrement f and copy the result to F or W; skip the next instruction if the result is zero 1 / 2 incfcz f, F(W) Increment f and copy the result to F or W; skip the next instruction if the result is zero 1 / 2 goto label Go to the instruction with the label ”label” 2 call label Go to the subroutine ”label”, push the Pro- gram Counter in the stack 2 retrun Return from the subroutine, POP the Pro- gram Counter from the stack 2 retlw k Retrun from the subroutine, POP the Pro- gram Counter from the stack; put k in W 2 Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 22 / 118
  • 23. Summary of Instructions Mnemonics Description Instruction Cycles retie Return from Interrupt Service Routine and re-enable interrupt 2 clrwdt Clear Watch Dog Timer 1 sleep Go into sleep/ stand by mode 1 nop No operation 1 Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 23 / 118
  • 24. Encoding of instruction: As has been discussed, each instruction is of 14-bit long. These 14-bits contain both op-code and the operand. Some examples of instruction encoding are shown here. Example Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 24 / 118
  • 25. Encoding of instruction: Example Figure : Example 2 Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 25 / 118
  • 26. Instruction Execution Period All instructions are executed in one cycle except for conditional branch instructions if condition was true, or if the contents of program counter was changed by some instruction. In that case, execution requires two instruction cycles, and the second cycle is executed as NOP (No Operation). Four oscillator clocks make up one instruction cycle. If we are using an oscillator with 4MHz frequency, the normal time for executing an instruction is 1 µs, and in case of conditional branching, execution period is 2 µs. Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 26 / 118
  • 27. Pipelining of instruction fetch successive addressing I Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 27 / 118
  • 28. Pipelining of instruction fetch successive addressing II Figure : Pipelining of instruction fetch successive addressing Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 28 / 118
  • 29. Pipelining of instruction fetch successive addressing III Figure : Introduction of extra cycle for a jump/goto instruction Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 29 / 118
  • 30. Word list f any memory location in a microcontroller (0x00 to 0x7F) W work register b bit position in ’f’ register d destination bit (d=0:store result in W, d=1:store result in file register f. Default is d=1) label group of eight characters which marks the beginning of a part of the program TOS top of stack [] option <> bit position inside register Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 30 / 118
  • 31. Instruction set - Group Wise I Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 31 / 118
  • 32. Instruction set - Group Wise II Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 32 / 118
  • 33. 1. MOVLW MOVLW Syntax MOVLW k Desciption 8 - bit contents of k is written in W register Operation k ⇒ (W ) Operand 0 ≤ k ≤ 255 Flags – Number of words 1 Number of cycles 1 Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 33 / 118
  • 34. MOVLW Example MOVLW 0x5A After execution: W = 0x5A Example MOVLW REGISTER Before execution: W = 0x10 and REGISTER = 0x40 After execution: W=0x40 Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 34 / 118
  • 35. 2. MOVWF MOVWF Syntax MOVWF f Desciption contents of W register is copied to f register Operation W ⇒ (f ) Operand 0 ≤ f ≤ 127 Flags – Number of words 1 Number of cycles 1 Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 35 / 118
  • 36. MOVWF Example MOVWF OPTION REG Before execution: OPTION REG = 0x20 W=0x40 After execution: OPTION REG = 0x40 W=0x40 Example MOVWF INDF Before execution: W=0x17 FSR = 0xC2 Address contents 0xC2 = 0x00 After execution: W=0x17 FSR = 0xC2 Address contents 0xC2 = 0x17 Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 36 / 118
  • 37. 3. MOVF f, d MOVF f, d Syntax MOVF f, d Desciption Contents of f register is stored in location determined by d operand. If d = 0, destination is W register If d = 1, destination is f register itself Option d=1 is used for testing the contents of f register because execution of this in- struction affects Z flag in STATUS register. Operation f ⇒ (d) Operand 0 ≤ f ≤ 127 Flags Z Number of words 1 Number of cycles 1 Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 37 / 118
  • 38. MOVF f, d Example MOVF FSR, 0 Before execution: FSR = 0xC2 W=0x00 After execution: W=0xC2 Z=0 Example MOVF INDF, 0 Before execution: W=0x17 FSR=0xC2 Address contents 0xC2=0x00 After execution: W=0x17 FSR = 0xC2 Address contents 0xC2=0x00 Z=1 Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 38 / 118
  • 39. 4. CLRW CLRW Syntax CLRW Desciption Contents of W register evens out to zero, and Z flag in STATUS register is set to one Operation 0 ⇒ (W ) Operand – Flags Z Number of words 1 Number of cycles 1 Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 39 / 118
  • 40. CLRW Example CLRW Before execution: W = 0x55 After execution: W=0x00 Z=1 Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 40 / 118
  • 41. 5. CLRF CLRF Syntax CLRF Desciption Contents of f register evens out to zero, and Z flag in STATUS register is set to one Operation 0 ⇒ (f ) Operand 0 ≤ f ≤ 127 Flags Z Number of words 1 Number of cycles 1 Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 41 / 118
  • 42. CLRF Example CLRF STATUS Before execution: STATUS=0xC2 After execution: STATUS=0x00 Z=1 Example CLRF INDF Before execution: FSR=0xC2 Address contents 0xC2=0x33 After execution: FSR=0xC2 Address contents 0xC2=0x00 Z=1 Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 42 / 118
  • 43. 6. SWAPF f, d SWAPF f, d Syntax SWAPF f, d Desciption Upper and lower half of f register exchange places If d = 0, result is stored in W register If d = 1, result is stored in f register itself Option d=1 is used for testing the contents of f register because execution of this in- struction affects Z flag in STATUS register. Operation f < 0 : 3 >⇒ d < 4 : 7 >, f < 4 : 7 >⇒ d < 0 : 3 > Operand 0 ≤ f ≤ 127 Flags – Number of words 1 Number of cycles 1 Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 43 / 118
  • 44. SWAPF f, d Example SWAP REG, 0 Before execution: REG=0xF3 After execution: REG=0xF3 W=0x3F Example SWAP REG, 1 Before execution: REG = 0xF3 After execution: REG=0x3F Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 44 / 118
  • 45. 7. ADDLW k ADDLW k Syntax ADDLW k Desciption Contents of W register are added to 8-bit contents of k and result is stored in W reg- ister. Operation (W ) + k ⇒ (W ) Operand 0 ≤ k ≤ 255 Flags C, DC, and Z Number of words 1 Number of cycles 1 Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 45 / 118
  • 46. ADDLW k Example ADDLW 0x15 Before execution: W=0x10 After execution: W=0x25 Example ADDLW REG Before execution: W=0x10 Register contents REG=0x37 After execution: W=0x47 Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 46 / 118
  • 47. 8. ADDWF f, d ADDWF f, d Syntax ADDWF f, d Desciption Add contents of register W to register f. If d = 0, result is stored in W register If d = 1, result is stored in f register itself Operation (W ) + (f ) ⇒ d Operand 0 ≤ f ≤ 127 Flags C, DC, and Z Number of words 1 Number of cycles 1 Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 47 / 118
  • 48. ADDWF f, d Example ADDWF FSR, 0 Before execution: W=0x17 FSR=0xC2 After execution: W=0xD9 FSR=0xC2 Example ADDWF INDF, 1 Before execution: W=0x17 FSR = 0xC2 Address contents 0xC2=0x20 After execution= w=0x17 FSR=0xC2 Address contents 0xC2=0x37 Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 48 / 118
  • 49. 9. SUBLW k SUBLW k Syntax SUBLW k Desciption Contents of W register are subtracted from k and result is stored in W register. Operation k − (W ) ⇒ (W ) Operand 0 ≤ k ≤ 255 Flags C, DC, and Z Number of words 1 Number of cycles 1 Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 49 / 118
  • 50. SUBLW k Example SUBLW 0x03 Before execution: W=0x01, C=X, Z=X After execution: W=0x02, C=1, Z=0 Result > 0 Before execution: W=0x03, C=X, Z=X After execution: W=0x00, C=1, Z=1 Result = 0 Before execution: W=0x04, C=X, Z=X After execution: W=0xFF, C=0, Z=0 Result < 0 Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 50 / 118
  • 51. 10. SUBWF f, d SUBWF f, d Syntax SUBWF f, d Desciption Contents of register W is subtracted from register f. If d = 0, result is stored in W register If d = 1, result is stored in f register itself Operation (f ) − (W ) ⇒ d Operand 0 ≤ f ≤ 127 Flags C, DC, and Z Number of words 1 Number of cycles 1 Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 51 / 118
  • 52. SUBWF f, d Example SUBWF REG, 1 Before execution: REG = 3, W=2, C=X, Z=X After execution: REG = 1, W=2, C=1, Z=0 Result ¿ 0 Before execution: REG = 2, W=2, C=X, Z=X After execution: REG = 0, W=2, C=1, Z=1 Result = 0 Before execution: REG = 1, W=2, C=X, Z=X After execution: REG = 0xFF, W=2, C=0, Z=0 Result ¡ 0 Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 52 / 118
  • 53. 11. ANDLW k ANDLW k Syntax ANDLW k Desciption Perform operation logic AND over the con- tents of W register and constant k and re- sult is stored in W register. Operation kAND(W ) ⇒ (W ) Operand 0 ≤ k ≤ 255 Flags Z Number of words 1 Number of cycles 1 Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 53 / 118
  • 54. ANDLW k Example ANDLW 0x5F Before execution: W=0xA3 After execution: W=0x03 Example ANDLW REG Before execution: W=0xA3 REG = 0x37 After execution: W=0x23 Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 54 / 118
  • 55. 12. ANDWF f, d ANDWF f, d Syntax ANDWF f, d Desciption Perfrom operation of logic AND over the contents of register W and register f. If d = 0, result is stored in W register If d = 1, result is stored in f register itself Operation (W )AND(f ) ⇒ d Operand 0 ≤ f ≤ 127 Flags Z Number of words 1 Number of cycles 1 Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 55 / 118
  • 56. ANDWF f, d Example ANDWF FSR, 1 Before execution: W=0x17, FSR = 0xC2 After execution: W=0x17, FSR = 02 Example ANDWF FSR, 0 Before execution: W=0x17, FSR = 0xC2 After execution: W=0x02, FSR = 0xC2 Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 56 / 118
  • 57. 13. IORLW k IORLW k Syntax IORLW k Desciption Operation logic OR is performed over the contents of W register and over 8 bit con- stant k, and the result is stored in W regis- ter. Operation kOR(W ) ⇒ (W ) Operand 0 ≤ k ≤ 255 Flags Z Number of words 1 Number of cycles 1 Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 57 / 118
  • 58. IORLW k Example IORLW 0x35 Before execution: W=0x9A After execution: W=0xBF Z = 0 Example IORLW REG Before execution: REG = 0x37, W=0x9A After execution: W=0x9F Z = 0 Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 58 / 118
  • 59. 14. IORWF f, d IORWF f, d Syntax IORWF f, d Desciption Perfrom operation of logical OR over the contents of register W and register f. If d = 0, result is stored in W register If d = 1, result is stored in f register itself Operation (W )OR(f ) ⇒ d Operand 0 ≤ f ≤ 127 Flags Z Number of words 1 Number of cycles 1 Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 59 / 118
  • 60. IORWF f, d Example IORWF REG, 0 Before execution: W=0x13, REG = 0x13 After execution: W=0x93, REG = 0x13 Z=0 Example IORWF REG, 1 Before execution: W=0x91, REG = 0x13 After execution: W=0x91, REG = 0x93 Z=0 Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 60 / 118
  • 61. 15. XORLW k XORLW k Syntax XORLW k Desciption Operation exclusive OR is performed over the contents of W register and over 8 bit constant k, and the result is stored in W register. Operation kXOR(W ) ⇒ (W ) Operand 0 ≤ k ≤ 255 Flags Z Number of words 1 Number of cycles 1 Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 61 / 118
  • 62. XORLW k Example XORLW 0xAF Before execution: W=0xB5 After execution: W=0x1A Example XORLW REG Before execution: REG = 0x37, W=0xAF After execution: W=0x18 Z = 0 Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 62 / 118
  • 63. 16. XORWF f, d XORWF f, d Syntax XORWF f, d Desciption Perfrom operation of exclusive OR over the contents of register W and register f. If d = 0, result is stored in W register If d = 1, result is stored in f register itself Operation (W )OR(f ) ⇒ d Operand 0 ≤ f ≤ 127 Flags Z Number of words 1 Number of cycles 1 Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 63 / 118
  • 64. XORWF f, d Example XORWF REG, 0 Before execution: W=0xB5, REG = 0xAF After execution: W=0x1A, REG = 0xAF Z=0 Example XORWF REG, 1 Before execution: W=0xB5, REG = 0xAF After execution: W=0xB5, REG = 0x1A Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 64 / 118
  • 65. 17. INCF f, d INCF f, d Syntax INCF f, d Desciption Increment register f by one. If d = 0, result is stored in W register If d = 1, result is stored in f register itself Operation (f ) + 1 ⇒ d Operand 0 ≤ f ≤ 127 Flags Z Number of words 1 Number of cycles 1 Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 65 / 118
  • 66. INCF f, d Example INCF REG, 0 Before execution: W=X, REG = 0x10, Z=0 After execution: W=0x11, REG = 0x10, Z=0 Example INCF REG, 1 Before execution: REG = 0xFF, Z=0 After execution: REG = 0x00, Z=1 Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 66 / 118
  • 67. 18. DECF f, d DECF f, d Syntax DECF f, d Desciption Decrement register f by one. If d = 0, result is stored in W register If d = 1, result is stored in f register itself Operation (f ) − 1 ⇒ d Operand 0 ≤ f ≤ 127 Flags Z Number of words 1 Number of cycles 1 Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 67 / 118
  • 68. DECF f, d Example DECF REG, 0 Before execution: W=X, REG = 0x13, Z=0 After execution: W=0x12, REG = 0x13, Z=0 Example DECF REG, 1 Before execution: REG = 0x01, Z=0 After execution: REG = 0x00, Z=1 Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 68 / 118
  • 69. 19. RLF f, d Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 69 / 118
  • 70. 20. RRF f, d Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 70 / 118
  • 71. 21. COMF f, d Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 71 / 118
  • 72. 22. BCF f, d Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 72 / 118
  • 73. 23. BSF f, d Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 73 / 118
  • 74. 24. BTFSC f, b Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 74 / 118
  • 75. 25. BTFSS f, b Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 75 / 118
  • 76. 26. INCFSZ f, d Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 76 / 118
  • 77. 27. DECFSZ f, d Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 77 / 118
  • 78. 28. GOTO k Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 78 / 118
  • 79. 29. CALL k Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 79 / 118
  • 80. 30. RETURN Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 80 / 118
  • 81. 31. RETLW k Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 81 / 118
  • 82. 32. RETFIE Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 82 / 118
  • 83. 33. NOP Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 83 / 118
  • 84. 34. CLRWDT Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 84 / 118
  • 85. 35. SLEEP Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 85 / 118
  • 86. Pseudo Instructions It is the instruction used to inform the Assembler about conversion of Assembly Program into Machine Language Program. It is also called pseudo or dummy instruction because these instructions are not executed by Microcontroller. Rather, they are executed by the Assembler. In other words, we say that Assembler Directives is used for Assembling and they are not consuming any Memory Location because they are not executed by Microcontroller. Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 86 / 118
  • 87. Pseudo Instructions... I 1 ORG: (ORIGIN) ORG Assembler Directive is used to initialize starting Memory Location of the program. There is no memory space allocated for the ORG Assembler Directive. In the program, ORG is always initialized at the beginning. In some of the Assemblers, ORG is used instead of ORG Assembler Directive. ORG Assembler Directive is always followed by a number which may be represented in decimal or hexadecimal. If the given number is in the decimal, then writing D at the end of the number is optional. But when given number is in the hexadecimal, then put H at the end of the number. Whenever the number is given in any format, assembler is always converted into the hexadecimal. FORMAT OF ORG: ORG 100 i.e. ORG 0064H, ORG 100H, ORG $ Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 87 / 118
  • 88. Pseudo Instructions... II 2 DB: (Define Byte): This Assembler Directive is used to allocate 1 Memory Location to each byte. So one memory location is reserved. Along with DB Assembler Directive, 8-bit number is given either in decimal, binary or in hexadecimal. If the number is given in the decimal format, then it is optional to end the number with ’D’. But when the number is given in the binary format, then put B at the end of the number. If the number is given in hexadecimal, then put H at the end of the number. Regardless of this number system, Assembler always converts the given number into the hexadecimal. Apart from this number system, it is also possible to allocate or give ASCII numbers or characters with this DB Assembler Directive. To give the ASCII value, it always includes either in single or double Quotation mark. Quotation mark indicates the given string represented in ASCII. Its simple application is used to display program in LCD Screen. So we are using DB, to save more number of bytes. Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 88 / 118
  • 89. Pseudo Instructions... III Format of DB: DB 12((12) d means (0CH). So the starting Memory Location is allocated.) DB 1001 0101B (binary is represented as 95H in Hexadecimal.) DB 45H(Given Number in Hexadecimal.) DB ’BHEL’/ DB ”BHEL”, both are possible. Since the single Inverted Comma is possible in Simulator and the Double Inverted Comma is possible. For example: ORG 0500H 0500H → DATA1: DB 45H, 67H, 01H. Given the Data starting memory Location is 0500H. Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 89 / 118
  • 90. Pseudo Instructions... IV 3 EQU: (EQUATE) This Assembler Directive is used to assign constant value to any value. For example: Label EQU 1000H, then after executing this, the value of Label becomes 1000H. Format: Label EQU value or Expression which may be Register, Accumulator, Jump. Use of Equate: Consider the example of Counter Value. Assume Counter value is 25, then using EQU it may be defined as COUNT EQU 25 MOV R2, #Count Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 90 / 118
  • 91. Pseudo Instructions... V 4 END END means the end of Assembly Process. So, no instruction either belonging to Assembler or microcontroller is executed after END. END gives only once in a program and that to be in the last indicating end of Assembly Process. Some Assembler will Support .END instead of END Assembler Directive. 5 SET This directive is used to define a constant value or a fixed address. In this regard, the SET and EQU directives are identical. The only difference is the value assigned by the SET directive may be reassigned later. Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 91 / 118
  • 92. Pseudo Instructions... VI 6 LIST directives Unline ORG and END, which are used by all assemblers, the LIST directive is unique to the PIC assembler. It indicates to the assembler the specific PIC chip for which the program should be assembled. It is used as follows: LIST P = 16F877 The above tells the PIC assembler to assemble the program specifically for the PIC16F877 microcontroller. We use LIST to state the target chip. 7 #include directive The #include directive tells the PIC assembler to use the libraries associated with the specific chip for which we are compiling the program. Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 92 / 118
  • 93. Pseudo Instructions... VII 8 config directive The config directive tells the assembler the configuration bits for the targeted PIC chip. 9 radix directive we can use the radix directive to indicate whether the numbering system is hexadecimal or decimal. The default is hex if we do not use the radix directive. Example: radix dec Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 93 / 118
  • 94. Assembly and C Language Programming Program 1 Write an assembly language programming to add two 8-bit numbers 28h and 56h and store the result in register W. Solution Instructions Comments movlw 0x28 ; copy data 28h to the W register movwf 0x20 ; copy contents of W register to memory location/file reg- ister 20h movlw 0x56 ; copy data 56h to the W register addwf 0x20, 0 ; add contents of W register with the contents of file register 20h and store the result in W register goto $ ; stop execution Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 94 / 118
  • 95. Simulation - Program 1 Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 95 / 118
  • 96. Assembly and C Language Programming Program 2 Write a program to copy the value 55H into RAM locations 40h to 44h using (A) Direct addressing mode (B) Register indirect addressing mode (C) A loop Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 96 / 118
  • 97. Solution - (A) Instructions Comments org 0x0004 ; interrupt vector location main: ; start of program MOVLW 0x55 ; copy data 55h to W register MOVWF 0x40 ; copy contents of W register to memory location/FSR 40h MOVWF 0x41 MOVWF 0x42 MOVWF 0x43 MOVWF 0x44 goto $ Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 97 / 118
  • 98. Solution - (B) Instructions Comments org 0x0004 ; interrupt vector location main: ; start of program MOVLW 0x55 ; copy data 55h to W register MOVWF 0x40 ; copy contents of W register to memory location/FSR 40h MOVWF 0x41 MOVWF 0x42 MOVWF 0x43 MOVWF 0x44 goto $ Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 98 / 118
  • 99. Simulation - Program 1 Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 99 / 118
  • 100. Assembly and C Language Programming Program 3 Write an assembly language programming to add two 16-bit numbers present in the memory location 21h-20h and 31h-30h and store the result in memory location 31h-30h. Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 100 / 118
  • 101. Assembly and C Language Programming Solution Instructions Comments org 0x0000 x EQU 0x20 ; Define memory location 20h y EQU 0x30 ; Define memory location 30h nop ; nop required for icd goto main ; go to beginning of program org 0x0004 ; interrupt vector location main: movf x,W ; copy contents of 20h to reg W addwf y,f ; Add contents of Reg W and Mem Loc 30h and store result in ML 30 btfsc STATUS,C ; Check for carry if C = 1 add 1 to the contents of ML 30h incf y+1,f movf x+1,W ; copy contents of ML 21h to Reg W addwf y+1,f ; Add contents of Reg W and Mem Loc 31h and store result in ML 31 goto $ ; stop execution Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 101 / 118
  • 102. Simulation - Program 3 Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 102 / 118
  • 103. Assembly and C Language Programming Program 4 Write an assembly language programming to add two 32-bit numbers present in the memory location 23h to 20h and 33h to 30h and store the result in memory location 33h to 30h. Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 103 / 118
  • 104. Assembly and C Language Programming Solution Instructions Comments org 0x0000 x EQU 0x20 ; Define memory location 20h y EQU 0x30 ; Define memory location 30h nop ; nop required for icd goto main ; go to beginning of program org 0x0004 ; interrupt vector location main: Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 104 / 118
  • 105. Assembly and C Language Programming Solution Instructions Comments movf x,W addwf y,f ; add byte 0 (LSB) movf x+1, W btfsc STATUS, C incfsz x+1, W addwf y+1,f ; add byte 1 movf x+2, W btfsc STATUS, C incfsz x+2, W addwf y+2,f ; add byte 2 movf x+3, W btfsc STATUS, C incfsz x+3, W addwf y+3,f ; add byte 3 goto $ ; stop execution Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 105 / 118
  • 106. Simulation - Program 4 Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 106 / 118
  • 107. Assembly and C Language Programming Program 5 Write an assembly language programming to subtract two 16-bit numbers present in the memory location 21h-20h (LSB) and 31h-30h (MSB) and store the result in memory location 31h-30h. Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 107 / 118
  • 108. Assembly and C Language Programming Solution Instructions Comments org 0x0000 x EQU 0x20 ; Define memory location 20h y EQU 0x30 ; Define memory location 30h nop ; nop required for icd goto main ; go to beginning of program org 0x0004 ; interrupt vector location main: movf x, W ; copy contents of ML 20h to Reg W subwf y, f ; subtract LSB y-w put result in y movf x+1, W ; get MSB btfss STATUS, C ; if borrow from LSB subtraction incfsz x+1, W ; increment copy of MSB subwf y+1, f ; subtract MSB goto $ ; stop execution Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 108 / 118
  • 109. Simulation - Program 5 Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 109 / 118
  • 110. Assembly and C Language Programming Program 6 Write an assembly language programming to subtract two 32-bit numbers present in the memory location 23h to 20h and 33h to 30h and store the result in memory location 33h to 30h. Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 110 / 118
  • 111. Assembly and C Language Programming Solution Instructions Comments org 0x0000 x EQU 0x20 ; Define memory location 20h y EQU 0x30 ; Define memory location 30h nop ; nop required for icd goto main ; go to beginning of program org 0x0004 ; interrupt vector location main: Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 111 / 118
  • 112. Assembly and C Language Programming Solution Instructions Comments movf x,W subwf y,f ; subtract byte 0 (LSB) movf x+1, W btfss STATUS, C incfsz x+1, W subwf y+1,f ; subtract byte 1 movf x+2, W btfss STATUS, C incfsz x+2, W subwf y+2,f ; subtract byte 2 movf x+3, W btfss STATUS, C incfsz x+3, W subwf y+3,f ; subtract byte 3 goto $ ; stop execution Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 112 / 118
  • 113. Que - 1 2017 (SLR - TJ-205) - Marks 1 The CALL and GOTO instruction provides . . . . . . bits of address to allow branching within any . . . . . . program memory page. (a) 13 bit, 8k (b) 16 bit, 64k (c) 11 bit, 2k (d) none of these Answer (c) 11 bit, 2k Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 113 / 118
  • 114. Que - 2 2017 (SLR-VB-179) - Marks 4 Explain the logical instructions named as XORLW, XORWF, IORLW AND IORWF Answer Explain instructions 13, 14, 15 and 16 Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 114 / 118
  • 115. Que - 3 2016 (SLR-EP-142) - Marks 1 Before execution of ANDLW 0x5F the working register contents were 0xA3. The contents after execution will be (a) 0x5F (b) 0x03 (c) 0xFF (d) none of these Answer (b) 0x03 Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 115 / 118
  • 116. Que - 4 2016 (SLR-EP-142) - Marks 5 Explain logical instructions related to PIC Answer Explain instructions 11, 12, 13, 14, 15 and 16 Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 116 / 118
  • 117. References [1, 2, 3, 4] Muhammad Ali Mazidi, Rolin D. McKinlay, and Danny Causey. PIC Microcontroller and Embedded Systems - Using Assembly and C for PIC18. Pearson International Edition, Upper Saddle River, NJ, 2008. Martin P. Bates. Programming 8 - bit PIC Microcontrollers in C with Interactive Hardware Simulation. Newnes Press Private Limited, United Kingdom, 2008. Ajay V. Deshmukh. Microcontrollers Theory and Applications. Tata McGraw Hill, New Delhi, India, 2008. John B. Peatman. Design with PIC Microcontroller. Prentice Hall, New Delhi, India, 1997. Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 117 / 118
  • 118. Thank you Please send your feedback at [email protected] For download [Click Here] Dr. Nilesh Bhaskarrao Bahadure (PhD) Microcontroller (PIC) July 1, 2021 118 / 118