MICROPROCESSORS
&
MICROCONTROLLERS
Khalil Zeineddine
2021
KAFAZ
Microprocessors & Microcontrollers KAFAZ- 7
Version 2021 Khalil Zeineddine
P
I
C
1
6
F
8
7
7
A
Chapter_2:
To know:
1. Programming of PIC16F877a
2. Addressing modes
3. Instructions set
4. Directives of assembly
5. Exercises
Contents_2:
PROGRAMMING OF PIC 16F877A
INTRODUCTION
The microcontroller executes the program loaded in its FLASH memory.
Binary words (on 14 bits for the PIC16F877A) are considered by the CPU as a
command.
But programming in binary or even in hexadecimal is impossible.
Programmers use languages like assembler or C, which uses abbreviations.
Transforming this more readable code into binary code ready for storage is
called compilation.
Microprocessors & Microcontrollers KAFAZ- 8
Version 2021 Khalil Zeineddine
P
I
C
1
6
F
8
7
7
A
Note: Assembler is a low-level language closely related to the type of
microcontroller.
C is a higher level language than assembler and in theory allows it to be
portable, therefore adapting to any microcontroller.
Addressing modes
1. Immediate (literal)
MOVLW 0x55; load the value 0x55 into the W register
2. Direct
MOVF 0x55, W; load the contents of address 0x55 into W
Use the STATUS register (RP0, RP1)
3. Indirect
Uses FSR, INDF and STATUS (IRP) registers.
Microprocessors & Microcontrollers KAFAZ- 9
Version 2021 Khalil Zeineddine
P
I
C
1
6
F
8
7
7
A
INSTRUCTIONS SET
All instructions understandable by microcontrollers form what is called
the instruction set
 Instructions on registers
Registers: W  the accumulator W and F  a RAM register.
d = 0  W is therefore the destination,
d = 1  F is therefore the destination.
ADDWF 70h,1 or ADDWF 70h,f
Means: add the content of W with the content of the address memory box
70h and place the result in the memory box 70h
XORWF 35h,0 or XORWF 35h,w
Means: make an exclusive or between W and the content of the address
memory box 35h and place the result in the accumulator W
Microprocessors & Microcontrollers KAFAZ- 10
Version 2021 Khalil Zeineddine
P
I
C
1
6
F
8
7
7
A
 Instructions on bits
F  RAM register ; b  position (7 – 0) of bit in the register F ;
For instructions acting on a bit, the parameter F indicates the register
which contains the bit to be modified and the parameter b indicates the
number of the bit to be modified; we count from zero, starting on the
right.
BSF STATUS, 2;
Means: set bit 2 (3rd bit from the right) of the STATUS register to 1
BCF 45h, 6;
Means: set bit 6 (7th bit from the right) to 0 of the register of the 45h
address memory slot
 immediate Instructions (W)
K  immediate data (value) ;
 Control Instructions (W)
K  immediate data (value) ; L  address (or label)
Microprocessors & Microcontrollers KAFAZ- 11
Version 2021 Khalil Zeineddine
P
I
C
1
6
F
8
7
7
A
The directives of the assembly MPASM
The assembly directives are instructions that are added to the program and
which will be interpreted by the MPASM assembler. These are not
instructions for the PIC.
 LIST: Example:
LIST p=16F876, r=dec
 INCLUDE :
INCLUDE "p16f876.inc"
 EQU : defines a constant or variable:
XX EQU 0x20
 CBLOCK/ENDC : allows you to define a set of constants
 ORG : defines the position in the program memory from which the
following instructions will be written.
 #DEFINE : works a bit like the EQU directive but is a little more
general, because it allows a whole string to be assigned to an
abbreviation
 DE : to declare data which will be stored in the data EEPROM
when the program is installed on the PIC
 DT : to declare a RETLW array
 END : indicates the end of the program

MICROPROCESSORS & MICROCONTROLLERS

  • 1.
  • 2.
    Microprocessors & MicrocontrollersKAFAZ- 7 Version 2021 Khalil Zeineddine P I C 1 6 F 8 7 7 A Chapter_2: To know: 1. Programming of PIC16F877a 2. Addressing modes 3. Instructions set 4. Directives of assembly 5. Exercises Contents_2: PROGRAMMING OF PIC 16F877A INTRODUCTION The microcontroller executes the program loaded in its FLASH memory. Binary words (on 14 bits for the PIC16F877A) are considered by the CPU as a command. But programming in binary or even in hexadecimal is impossible. Programmers use languages like assembler or C, which uses abbreviations. Transforming this more readable code into binary code ready for storage is called compilation.
  • 3.
    Microprocessors & MicrocontrollersKAFAZ- 8 Version 2021 Khalil Zeineddine P I C 1 6 F 8 7 7 A Note: Assembler is a low-level language closely related to the type of microcontroller. C is a higher level language than assembler and in theory allows it to be portable, therefore adapting to any microcontroller. Addressing modes 1. Immediate (literal) MOVLW 0x55; load the value 0x55 into the W register 2. Direct MOVF 0x55, W; load the contents of address 0x55 into W Use the STATUS register (RP0, RP1) 3. Indirect Uses FSR, INDF and STATUS (IRP) registers.
  • 4.
    Microprocessors & MicrocontrollersKAFAZ- 9 Version 2021 Khalil Zeineddine P I C 1 6 F 8 7 7 A INSTRUCTIONS SET All instructions understandable by microcontrollers form what is called the instruction set  Instructions on registers Registers: W  the accumulator W and F  a RAM register. d = 0  W is therefore the destination, d = 1  F is therefore the destination. ADDWF 70h,1 or ADDWF 70h,f Means: add the content of W with the content of the address memory box 70h and place the result in the memory box 70h XORWF 35h,0 or XORWF 35h,w Means: make an exclusive or between W and the content of the address memory box 35h and place the result in the accumulator W
  • 5.
    Microprocessors & MicrocontrollersKAFAZ- 10 Version 2021 Khalil Zeineddine P I C 1 6 F 8 7 7 A  Instructions on bits F  RAM register ; b  position (7 – 0) of bit in the register F ; For instructions acting on a bit, the parameter F indicates the register which contains the bit to be modified and the parameter b indicates the number of the bit to be modified; we count from zero, starting on the right. BSF STATUS, 2; Means: set bit 2 (3rd bit from the right) of the STATUS register to 1 BCF 45h, 6; Means: set bit 6 (7th bit from the right) to 0 of the register of the 45h address memory slot  immediate Instructions (W) K  immediate data (value) ;  Control Instructions (W) K  immediate data (value) ; L  address (or label)
  • 6.
    Microprocessors & MicrocontrollersKAFAZ- 11 Version 2021 Khalil Zeineddine P I C 1 6 F 8 7 7 A The directives of the assembly MPASM The assembly directives are instructions that are added to the program and which will be interpreted by the MPASM assembler. These are not instructions for the PIC.  LIST: Example: LIST p=16F876, r=dec  INCLUDE : INCLUDE "p16f876.inc"  EQU : defines a constant or variable: XX EQU 0x20  CBLOCK/ENDC : allows you to define a set of constants  ORG : defines the position in the program memory from which the following instructions will be written.  #DEFINE : works a bit like the EQU directive but is a little more general, because it allows a whole string to be assigned to an abbreviation  DE : to declare data which will be stored in the data EEPROM when the program is installed on the PIC  DT : to declare a RETLW array  END : indicates the end of the program