Sei sulla pagina 1di 131

MICROCONTROLLER

LABORATORY MANUAL
V SEMESTER B.Tech (ECE & EIE)

AMRITA VISHWA VIDYAPEETHAM


UNIVERSITY

Prepared By,
Mr. Peeyush.K.P.
Assistant Professor
Department of Electronics & Communication Engineering
ASE Ettimadai

Department of Electronics & Communication Engineering


Amrita School of Engineering
Ettimadai

TABLE OF CONTENTS
1)
2)
3)
4)
5)
6)

Study of 8085 Simulator


Study of MPLAB IDE
Study of Proteus 7 Professional
8085 Architecture
8085 Instruction Set Summary
8085 Programs
a. Addition of two 8 bit numbers
b. Subtraction of two 8 bit numbers
c. Multiplication of two 8 bit numbers
d. Division of two 8 bit numbers
7) PIC16F877A Architecture
8) PIC16F877A Peripherals
9) PIC16F877A Instruction Set Summary
10) MPLAB Programs
a. Addition of two 8 bits numbers
b. Addition of two 8 bit numbers with carry
c. Subtraction of two 8 bit numbers with borrow
d. Multiplication of two 8 bit numbers
e. Division of two 8 bit numbers
f. Addition of 5 numbers using Indirect Addressing Mode
g. Blinking 8 LEDs connected to a PORT
h. Controlling an LED with a Switch
i. Controlling an LED with a Switch using External Interrupt (INT0)
j. Controlling an LED with a Switch using PORTB Pin Change Interrupt
k. Generation of 5mS square wave using Timer0
l. Generation of 5mS square wave using Timer0 in Interrupt Mode
m. Generation of 5mS square wave using Timer1 in Interrupt Mode
n. Generation of 5mS square wave using Timer2 in Interrupt Mode
o. Continuous Transmission of a character using USART in Asynchronous
p. Mode of operation
q. Reception and Transmission of the same character using USART in
Asynchronous Mode of operation
r. Analog to Digital Conversion
s. Analog to Digital Conversion in Interrupt Mode
t. Generation of PWM
u. EEPROM read and write
v. Compare mode of operation of CCP

1
7
23
49
50
54
54
55
56
57
58
59
60
61
61
62
63
64
65
67
68
71
73
76
79
84
87
93
98
105
109
113
116
121
125

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

8085 SIMULATOR
1) Click on 8085 Simulator icon from Desktop.

Register window
Memory Editor window
Assembler window

Flag Register window

Simulator window
Starting address of the actual program is 4000h, but by default the starting address will be from
C000h.
It is shown in Memory Range text box in 8085 Simulator.

Memory Range Text Box

The default starting address should be changed from C000h to 4000h.

Page 1 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

2) For this click on Settings Tab -> Set Memory Range.

3) Then the Memory Range Text Box can be edited with new value ie 4000h 4FFFh. So enter
these values in Memory Range text box.

Memory Range Text Box

4) Type the Assembly Language Program in the Assembler window.


5) Click on Assemble button on bottom of Assembler window.

Assembler window

Assemble button

Page 2 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

The program will be assembled and the corresponding Opcodes along with its address will be
displayed below the Assembler window.

Program with
Opcode & Address

If the program is wrong, the simulator will display the error message at the bottom of the
Simulator along with the line number and the will be anX mark on left of that particular
address where error is present.
This error should be corrected and click on Assemble again.

Error ie X Mark
Error Message with Line No.

As the next step, the simulator should be given with the starting address of the program.

Page 3 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

By default, the starting address will be C000h. It should be changed to 4000h as this program is
starting from 4000h onwards.
6) For this, change C000 to 4000 in the Start From text box in the Simulator window.

Start From text box

7) Click on Step By Step button in the Simulator window inorder to perform the execution of the
program step by step.

Step by Step button

8) Click on Forward button in the Simulator window, so first instruction will get executed.

Forward button

Second instruction of the program will be highlighted which shows that first instruction has
been executed and the second instruction is going to be executed when the Forward button is
pressed again.

Highlight

Page 4 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

In the first instruction 01 is moved to A register, check whether 01 is present in A register or in


the Accumulator.
This can be seen checking the value of Accumulator in Register window.

Accumulator

Value = 01

9) Click on Forward button until the whole program is executed. That means the last line of the
program will be highlighted in blue colour.

Last line Highlighted

Next step is that the output should be checked.

Page 5 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

10) Select Show only loaded memory location radio button in Memory Editor window.

Show only loaded memory location radio button


So the memory locations which are loaded with a value will be displayed

Page 6 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

MPLAB IDE
Project Creation
1) Open MPLAB IDE 7.52 icon from the desktop.

Menubar
Toolbar

Status bar

2) Click on Project Tab from Menubar.


3) Click on Project Wizard.

Welcome screen will appear.

Page 7 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

4) Click on Next button in the welcome screen.

5) Select Device as PIC16F877A from Device drop down menu.

Device Drop down menu

6) Select the Active Toolsuite as Microchip MPASM Toolsuite. This is done because programs are
to be written in Assembly Language.

Page 8 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

7) Click on Next button.

8) Select the folder in which New Project has to be stored. For this, Select Create New Project File
and then click on Browse button.

Browse button

Page 9 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

9) Open any Drive, say E Drive, create a folder directly in this Drive.
Folder should be created directly in the Drive specified because the path name for the project
should have only limited number of characters.

10) Name the folder as MP_Lab.


11) Type the Project Name as PIC_Project.
12) Click on Save.

Project Name

Page 10 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

13) Verify that the Path for the Project is displayed properly as E:\MP_Lab\PIC_Project.
14) Click on Next.

Project Path

15) Again Click on Next.

Page 11 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

16) Click on Finish.

17) Project Explorer window will be displayed.

Project Explorer Window

Page 12 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

18) Click on File -> New File or click on New File icon in the Toolbar.

New File

19) Type the whole assembly language program.

20) Click on File -> Save As.

Page 13 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

21) Open the Project Folder ie E:\MP_Lab in the Save In drop down menu. Otherwise the program
will not be assembled properly and the program files will be saved in some other location other
than Project Folder.
22) Type the file name as Add.asm. Don,t forget to type the extension of the file as .asm.
23) Click on Save.

24) After saving, the colour of the program will change into blue and red. If still the program is in
black colour, that means the program is not saved with proper extension ie .asm.

Page 14 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Adding Source File (Program File) into the New Project and Build the Project
1) Right click on Source Files in the Project Explorer window and click Add Files.

Source Files

2) Open the Project Folder in E Drive ie MP_Lab in Look In drop down menu.
3) Select Add.asm. Click on Open.

Page 15 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Add.asm will be displayed under Source Files in the Project Explorer window.

Add.asm

4) Click on Project -> Build All or click on Build All icon in the Toolbar to assemble the project.

Build All

5) If there is no errors in the program, Build Succeeded message will be displayed in the output
window.

Build Succeeded

Page 16 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

If there are any errors, Build Failed message will be displayed in the Output Window along with
the errors in the program.
6) Double click on the Error[108]: Message displayed in the output window.

Build Failed

7) The cursor will automatically go to the statement where the error is present.

Instruction with Error

Page 17 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Debug and Run the Project


1) Click on Debugger -> Select Tool -> MPLAB SIM.

MPLAB Simulator Debug Toolbar will be displayed.

Debug Toolbar
As the next step, output should be checked. To check the output, the output and input address
location should be monitored.
To monitor these address locations, Watch window is used.
2) Click on View -> Watch.

Page 18 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Watch window will be displayed.

3) Select FIRSTNO symbol from the Symbol drop down menu.


4) Click on Add Symbol button.

Add Symbol button

Symbol Drop down menu

FIRSTNO symbol is added to watch window, so that the value of FIRSTNO can be monitored.

Page 19 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

5) The address location of FIRSTNO also can be identified from the Watch window.

6) Similarly add SECNO and SUM to Watch window.

As the next step add WREG to Watch Window.


7) Select WREG from SFR drop down menu.
8) Click on Add SFR button in Watch Window.

SFR Drop down menu

Page 20 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

9) WREG is also added to Watch window.

10) To debug the program by executing step by step, click on Step Into icon in Debug Toolbar.

Green arrow will be displayed on the left side of the program. This shows the instruction to be
executed in the next click of Step Into icon.

Green Arrow

Green arrow is in the second line of the program, which shows that first line has been executed
and the result is displayed in the Watch window with value of WREG as 0x01.

Page 21 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

11) If there is any change in the value of a register, it will be shown in red colour in the Watch
window.

Red Colour
12) Similarly click on Step Into until the last line of the program ie Goto L1 is reached.
This shows that the whole program has been executed and debugged.

Page 22 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

PROTEUS 7 PROFESSIONAL

1) Click on Start -> Program Files -> Proteus 7 Professional -> ISIS 7 Professional.

ISIS 7 Professional

Page 23 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

ISIS screen will be displayed.

Menubar
Toolbar

Device List

Workspace

Toolbar

2) Click on Component Mode icon in the Toolbar.

Component Mode

Page 24 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

3) Click on Pick From Libraries button from Devices List.

Pick from Libraries

Pick Devices window will be displayed.

Pick Devices window

Page 25 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Components required for the circuit should be added to the Device List.
4) Type PIC16F877A in the Keywords text space in Pick Devices window so as to add PIC into
Device List.
5) Press Enter key.
PIC16F877A will be displayed in the Result Space.

Keywords
Text Box

PIC16F877A

Result Space

Page 26 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

6) Double click on PIC16F877A in the Result Space.

Double Click Here

PIC16F877A will be added to the Devices List in Proteus.

PIC16F877A

Device List

Page 27 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

7) To add the second component ie 330 R resistance, type MINRES330R in Keywords text space
in Pick Devices window.
8) Press Enter key.
9) Double click on MINRES330R in the Result Space.

Keywords
Text Box

Double Click Here

MINRES330R ie 330 R resistor will be added to Devices List in Proteus.


10) Similarly Type Led-Yellow in Keywords text space in Pick Devices window inorder to add Led
to Devices List.

Page 28 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

11) Press Enter key.

12) Double click on Led-Yellow in Result Space.


Then Led-Yellow will be added to the Devices List.

Page 29 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Now all the devices required for drawing the circuits are added to the Devices List.

13) Click on OK button in Pick Devices window.

OK Button

Page 30 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Proteus workspace will be displayed with all the Devices listed in the Device List.

As the next step, desired circuit should be drawn in the workspace. So the devices should be
taken to the workspace from the Device List.

Page 31 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

14) Click on PIC16F877A in the Device List, and then click in the Workspace. So a skeleton of PIC
will be displayed in the workspace with the help of which the position of PIC can be desired.

Single Click Here

Single Click Here

15) Inorder to fix the PIC in the desired position, click ones more in the workspace.
16) Click on Zoom button in the Toolbar.

Single Click Here

Page 32 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

PIC IC in the workspace has been moved out of the viewable area, so it should be taken back to
the viewable area.
17) Click on Green Square (shows the viewable area) on the left top side of Proteus and move the
Green Square over the Black Rectangle (PIC) which is placed near the Green Square.
18) Click once more to fix the viewable area (Green Square).

Black Square (PIC)

Green Square (Viewable Area)

Viewable Area

Viewable Area

Page 33 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

After this step PIC has been brought back to the viewable area.

Page 34 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

19) Click on MINRES330R in the Device List.


20) Click twice in the Workspace. So the resistor will be added to the workspace.
21) Similarly add Led also to the workspace.

Double Click Here

Single Click Here

As the next step add Ground Terminal to the workspace.

Page 35 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

22) For this Click on Terminals Mode icon in the Toolbar.

Terminals Mode

23) Select Ground from Terminals List.

Ground

Page 36 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

24) Click twice in the workspace so that the Ground Terminal will be added to the workspace.

Double Click Here

Make the connection between the components.


25) Move the cursor to the starting point from which the connection has to be made. A small red
square will be displayed at the starting point.
This shows the point where the connection has to be made.

Red Square

Page 37 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

26) Click on this red square, move the cursor to the ending point.

Starting Point
Single Click

Ending Point
Single Click

27) Click at the ending point.


28) So that a connection has been made between starting and ending point.

Page 38 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Similarly make the connection between all the components including the Ground.

As the next step, program hex file should be loaded into the PIC and the Crystal Oscillator
Frequency should be specified for the operation of PIC.

Page 39 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

29) Right click on PIC IC in the workspace. Select Edit Properties of PIC.

Right Click
Edit Properties

Page 40 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Edit Component window will be displayed.

30) Click on Browse button, inorder to add the Program hex file.

Browse

Page 41 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

31) Select File Name window will be displayed.


32) Select the Led.hex file from the Project folder E:\MP_Lab.
33) Click on Open button.

Led.hex

Open Button

Page 42 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

34) Verify that the path displayed in the Program File text box is correct ie E:\MP_Lab\Led.hex.

Path

35) Enter the Crystal Oscillator Frequency as 4MHz in the Processor Clock Frequency text box.
Since for this circuit a 4MHz crystal is connected to the PIC for its clock generation.

4MHz

All the steps required for the simulation of the Led Blinking Circuit is completed.

Page 43 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

36) Click on Play button located at the bottom left corner to start the simulation.

Play Button

The Led connected to RB0 will start Blinking

Page 44 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Adding Virtual Instruments to Proteus Workspace


1) Click on Virtual Instruments Mode in Proteus Toolbar.

Virtual Instruments
Mode

Virtual Instruments List will be displayed.

Page 45 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

2) Click on Oscilloscope in Virtual Instruments List.

Oscilloscope

3) Add Oscilloscope to Workspace.

4) Click on Play button at the bottom of Proteus.

Page 46 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Oscilloscope window will be displayed.


5) Click on Cursors button inorder to measure the Time Period of Waveform.

Cursors button

6) Move the mouse pointer to Oscilloscope screen, then cursor will be displayed

Cursor

Page 47 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

7) Click on starting and ending point of waveform, so two cursors will be displayed at
starting and ending point.
From these two cursor Time Period can be directly measured.

Cursor 1

Cursor 2

Page 48 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

8085 ARCHITECTURE

Page 49 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

8085 Instruction Set


Data transfer (Copy)
Hex

Mnemonic

Hex

Mnemonic

Hex

Mnemonic

Hex

Mnemonic

40

MOV B,B

58

MOV E,B

70

MOV M,B

1A

LDAX D

41

MOV B,C

59

MOV E,C

71

MOV M,C

2A

LHLD

42

MOV B,D

5A

MOV E,D

72

MOV M,D

3A

LDA

43

MOV B,E

5B

MOV E,E

73

MOV M,E

02

STAX B

44

MOV B,H

5C

MOV E,H

74

MOV M,H

12

STAX D

45

MOV B,L

5D

MOV E,L

75

MOV M,L

22

SHLD

46

MOV B,M

5E

MOV E,M

77

MOV M,A

32

STA

47

MOV B,A

5F

MOV E,A

78

MOV A,B

01

LXI B

48

MOV C,B

60

MOV H,B

79

MOV A,C

11

LXI D

49

MOV C,C

61

MOV H,C

7A

MOV A,D

21

LXI H

4A

MOV C,D

62

MOV H,D

7B

MOV A,E

31

LXI SP

4B

MOV C,E

63

MOV H,E

7C

MOV A,H

F9

SPHL

4C

MOV C,H

64

MOV H,H

7D

MOV A,L

E3

XTHL

4D

MOV C,L

65

MOV H,L

7E

MOV A,M

EB

XCHG

4E

MOV C,M

66

MOV H,M

7F

MOV A,A

D3

OUT

4F

MOV C,A

67

MOV H,A

06

MVI B

DB

IN

50

MOV D,B

68

MOV L,B

0E

MVI C

C5

PUSH B

51

MOV D,C

69

MOV L,C

16

MVI D

D5

PUSH D

52

MOV D,D

6A

MOV L,D

1E

MVI E

E5

PUSH H

53

MOV D,E

6B

MOV L,E

26

MVI H

F5

PUSH PSW

Page 50 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

54

MOV D,H

6C

MOV L,H

2E

MVI L

C1

POP B

55

MOV D,L

6D

MOV L,L

36

MVI M

D1

POP D

56

MOV D,M

6E

MOV L,M

3E

MVI A

E1

POP H

57

MOV D,A

6F

MOV L,A

0A

LDAX B

F1

POP PSW

Arithmetic
Hex

Mnemonic

Hex

Mnemonic

Hex

Mnemonic

Hex

Mnemonic

80

ADD B

C6

ADI

9E

SBB M

3C

INR A

81

ADD C

CE

ACI

9F

SBB A

03

INX B

82

ADD D

90

SUB B

D6

SU1

13

INX D

83

ADD E

91

SUB C

DE

SB1

23

INX H

84

ADD H

92

SUB D

09

DAD B

33

INX SP

85

ADD L

93

SUB E

19

DAD C

05

DCR B

86

ADD M

94

SUB H

29

DAD H

0D

DCR C

87

ADD A

95

SUB L

39

DAD SP

15

DCR D

88

ADC B

96

SUB M

27

DAA

1D

DCR E

89

ADC C

97

SUB A

04

INR B

25

DCR H

8A

ADC D

98

SBB B

0C

INR C

2D

DCR L

8B

ADC E

99

SBB C

14

INR D

35

DCR M

8C

ADC H

9A

SBB D

1C

INR E

3D

DCR A

8D

ADC L

9B

SBB E

24

INR H

0B

DCX B

8E

ADC M

9C

SBB H

2C

INR L

1B

DCX D

8F

ADC A

9D

SBB L

34

INR M

2B

DCX H

3B

DCX SP

Page 51 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Logical
Hex

Mnemonic

Hex

Mnemonic

Hex

Mnemonic

Hex

Mnemonic

37

STC

A9

XRA C

B3

ORA E

BD

CMP L

A0

ANA B

AA

XRA D

B4

ORA H

BE

CMP M

A1

ANA C

AB

XRA E

B5

ORA L

BF

CMP A

A2

ANA D

AC

XRA H

B6

ORA M

FE

CPI

A3

ANA E

AD

XRA L

B7

ORA A

07

RLC

A4

ANA H

AE

XRA M

F6

ORI

0F

RRC

A5

ANA L

AF

XRA A

B8

CMP B

17

RAL

A6

ANA M

EE

XRI

B9

CMP C

1F

RAR

A7

ANA A

B0

ORA B

BA

CMP D

2F

CMA

E6

ANI

B1

ORA C

BB

CMP E

3F

CMC

A8

XRA B

B2

ORA D

BC

CMP H

Branching & Control


Hex

Mnemonic

Hex

Mnemonic

Hex

Mnemonic

Hex

Mnemonic

C3

JMP

D7

RST 2

EC

CPE

00

NOP

C2

JNZ

DF

RST 3

F4

CP

76

HLT

CA

JZ

E7

RST 4

FC

CM

F3

DI

D2

JNC

EF

RST 5

C9

RET

FB

EI

DA

JC

F7

RST 6

C0

RNZ

20

RIM

E2

JPO

FF

RST 7

C8

RZ

30

SIM

Page 52 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

EA

JPE

CD

CALL

D0

RNC

F2

JP

C4

CNZ

D8

RC

FA

JM

CC

CZ

E0

RPO

E9

PCHL

D4

CNC

E8

RPE

C7

RST 0

DC

CC

F0

RP

CF

RST 1

E4

CPO

F8

RM

Page 53 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

8085 PROGRAMS
Addition of two 8 bit numbers
Aim
To perform addition of two 8 bit numbers using 8085.
Algorithm
1)
2)
3)
4)
5)
6)
7)

Start the program by loading the first data into Accumulator.


Move the data to a register (B register).
Get the second data and load into Accumulator.
Add the two register contents.
Check for carry.
Store the value of sum and carry in memory location.
Terminate the program.

Program
MVI
LDA
MOV
LDA
ADD
JNC
INR

C, 00
4150
B, A
4151
B
LOOP
C

Initialize C register to 00
Load the value to Accumulator.
Move the content of Accumulator to B register.
Load the value to Accumulator.
Add the value of register B to A
Jump on no carry.
Increment value of register C

LOOP:
STA 4152
MOV A, C
STA 4153
HLT

Store the value of Accumulator (SUM).


Move content of register C to Acc.
Store the value of Accumulator (CARRY)

Output

Input
Output

Address
4150
4151
4152
4153

Page 54 of 129

Data
FF
FF
FE
01

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Subtraction of two 8 bit numbers


Aim
To perform the subtraction of two 8 bit numbers using 8085.
Algorithm
1)
2)
3)
4)
5)
6)
7)
8)

Start the program by loading the first data into Accumulator.


Move the data to a register (B register).
Get the second data and load into Accumulator.
Subtract the two register contents.
Check for carry.
If carry is present take 2s complement of Accumulator.
Store the value of borrow in memory location.
Store the difference value (present in Accumulator) to a memory location and terminate
the program.

Program
MVI
LDA
MOV
LDA
SUB
JNC
CMA
INR
INR

C, 00
4150
B, A
4151
B
LOOP
A
C

Initialize C register to 00
Load the value to Accumulator.
Move the content of Accumulator to B register.
Load the value to Accumulator.
Subtract the value of register B from A
Jump on no carry.
Complement Accumulator contents.
Increment value of register A
Increment value of register C

LOOP:
STA 4152
MOV A, C
STA 4153
HLT

Store the value of Accumulator (SUM).


Move content of register C to Acc.
Store the value of Accumulator (CARRY)

Output

Input
Output

Address
4150
4151
4152
4153

Page 55 of 129

Data
05
03
02
01

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Multiplication of two 8 bit numbers


Aim
To perform multiplication of two 8 bit numbers using 8085.
Algorithm
1)
2)
3)
4)
5)
6)
7)

Start the program by loading HL register pair with address of memory location.
Move the data to a register (B register).
Get the second data and load into Accumulator.
Add the two register contents.
Check for carry.
Increment the value of carry.
Check whether repeated addition is over and store the value of product and carry in
memory location.
8) Terminate the program.
Program
MVI
MVI
LXI
MOV
INX
MOV

D, 00
A,00
H,4150
B,M
H
C,M

Initialize register D to 00
Initialize Accumulator content to 00
Get the first number in B - reg
Get the second number in C- reg

LOOP:
ADD B
JNC NEXT
INR D

Add content of A to register B.


Jump on no carry to NEXT.
Increment content of register D

DCR
JNZ
STA
MOV
STA
HLT

Decrement content of register C.


Jump on no zero to address
Store the result in Memory

NEXT:
C
LOOP
4152
A,D
4153

Store the MSB of result in Memory

Output

Input
Output

Address
4150
4151
4152
4153

Page 56 of 129

Data
FF
02
FE
01

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Division of two 8 bit numbers


Aim
To perform the division of two 8 bit numbers using 8085.
Algorithm
1)
2)
3)
4)
5)
6)
7)

Start the program by loading HL register pair with address of memory location.
Move the data to a register (B register).
Get the second data and load into Accumulator.
Compare the two numbers to check for carry.
Subtract the two numbers.
Increment the value of carry.
Check whether repeated subtraction is over and store the value of product and carry in
memory location.
8) Terminate the program
Program
LXI
MOV
MVI
INX
MOV

H,4150
B,M
C,00
H
A,M

CMP
JC
SUB
INR
JMP

B
LOOP
B
C
NEXT

Get the dividend in B reg.


Clear C reg for qoutient
Get the divisor in A reg.

NEXT:
Compare A - reg with register B.
Jump on carry to LOOP
Subtract A reg from B- reg.
Increment content of register C.
Jump to NEXT

LOOP:
STA 4152
MOV A,C
STA 4153
HLT

Store the remainder in Memory


Store the quotient in memory

Output

Input
Output

Address
4150
4151
4152
4153

Page 57 of 129

Data
02
05
01
02

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

PIC16F877A ARCHITECTURE

Page 58 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

PIC16F877A PERIPHERALS

Timer0: 8-bit timer/counter with 8-bit prescaler.


Timer1: 16-bit timer/counter with prescaler, can be incremented during SLEEP via
external crystal/clock.
Timer2: 8-bit timer/counter with 8-bit period register, prescaler and postscaler.
Two Capture, Compare, PWM modules.
o Capture is 16-bit, max. resolution is 12.5 ns.
o Compare is 16-bit, max. resolution is 200 ns.
o PWM max. resolution is 10-bit.
10-bit multi-channel Analog-to-Digital converter.
Synchronous Serial Port (SSP) with SPI (Master mode) and I2C (Master/Slave).
Universal Synchronous Asynchronous Receiver Transmitter (USART/SCI) with 9-bit
address Detection.
Parallel Slave Port (PSP) 8-bits wide, with external RD, WR and CS controls (40/44-pin
only).
Brown-out detection circuitry for Brown-out Reset (BOR).

Page 59 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

INSTRUCTION SET SUMMARY

Page 60 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

MPLAB PROGRAMS
Addition of two 8 bits numbers
Aim
To add two 8 bit numbers and store the result
Algorithm
1)
2)
3)
4)
5)
6)

Move first number to W Register.


Move content of W Register to 0x20 (FIRSTNO).
Move second number to W Register.
Move content of W Register to 0x21 (SECNO).
Add content of W Register (second No.) with content of 0x20 (first No.).
Move the result to 0x22 (SUM).

Program
#INCLUDE<P16F877A.INC>
FIRSTNO EQU 0X20
SECNO
EQU 0X21
SUM EQU 0X22
ORG 0X00
MOVLW 0X01
MOVWF FIRSTNO
MOVLW 0X02
MOVWF SECNO
ADDWF FIRSTNO,0
MOVWF SUM
L1
GOTO L1
END
Output
Two 8 bit numbers will be added and the result will be stored in 0x22.

Page 61 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Addition of two 8 bit numbers with carry


Aim
To add two 8 bit numbers and store the result along with carry.
Algorithm
1)
2)
3)
4)
5)
6)
7)
8)
9)

Move first number to W Register.


Move content of W Register to 0x20 (NUMBER_1).
Move second number to W Register.
Move content of W Register to 0x21 (NUMBER_2).
Add content of W Register (second No.) with content of 0x20 (first No.).
Move the result to 0x22 (SUM).
If Carry is there, Increment 0x23 (CARRY).
Else goto step 9.
End

Program
#INCLUDE<P16F877A.INC>
NUMBER_1 EQU 0x20
NUMBER_2 EQU 0X21
SUM
EQU 0X22
CARRY
EQU 0X23
ORG 0X00
GOTO START
ORG 0X05
START

; Define a Symbol Name NUMBER_1 to an Address 0x30

MOVLW
0XA2
MOVWF
NUMBER_1
MOVLW
0X62
MOVWF
NUMBER_2
ADDWF
NUMBER_1, 0
MOVWF
SUM
BTFSC
STATUS, C
INCF CARRY, 1

; Shows the Origin of the Program


; Bypassing Interrupt Vector Address

; Move First Number to W


; Move First Number NUMBER_1 (0x20)

; W = NUMBER_1 + W (Second No.)


; Move Result from W to SUM
; Check for Carry Flag
; If Carry is there, increment Carry Register.

L1
GOTO L1
END

; Physical end of the program

Output
Two 8 bit numbers are added and the result will be stored in 0x22 with Carry stored in
0x23.
Page 62 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Subtraction of two 8 bit numbers with borrow


Aim
To subtract two 8 bit numbers and store the result along with borrow.
Algorithm
1)
2)
3)
4)
5)
6)
7)
8)
9)

Move first number to W Register.


Move content of W Register to 0x20 (NUMBER_1).
Move second number to W Register.
Move content of W Register to 0x21 (NUMBER_2).
Subtract content of 0x20 (first No.) with content of W Register (second No.).
Move the result to 0x22 (DIFF).
If Borrow is there, Increment 0x23 (BORROW).
Else goto step 9.
End

Program
#INCLUDE<P16F877A.INC>
NUMBER_1 EQU 0X20
NUMBER_2 EQU 0X21
DIFF
EQU 0X22
BORROW
EQU 0X23
ORG 0X00
GOTO MAIN
ORG 0X05
MAIN
MOVLW
MOVWF
MOVLW
MOVWF
SUBWF
MOVWF
BTFSS
CALL
GOTO
COMPLIMENT
COMF
INCF
INCF
RETURN

0X62
NUMBER_1
0XA2
NUMBER_2
NUMBER_1, 0
DIFF
STATUS, C
COMPLIMENT
L1

DIFF, 1
DIFF, 1
BORROW, 1

; WREG = NUMBER_1 - WREG


; DIFF = WREG
; If borrow is not there, goto end.
; If Borrow present, goto 2s COMPLIMENT function.

; DIFF = one's compliment of DIFF


; DIFF = DIFF + 1
; BORROW = BORROW + 1

Page 63 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

L1
GOTO
END

L1

Output
Two 8 bit numbers are subtracted and the result will be stored in 0x22 with Borrow
stored in 0x23.

Multiplication of two 8 bit numbers


Aim
To multiply two 8 bit numbers and store the result.
Algorithm
1)
2)
3)
4)
5)
6)

Clear 0x23 (Product MSB) & 0x24 (Product LSB).


Move first number to MULTIPLIER & COUNT.
Move second number to MULTIPLICANT.
If Multiplier is zero, goto end.
Else if Multiplicant is Zero, goto end.
Add Multiplicant (second number) with PRODUCT LSB (initially zero) and store the
result in PRODUCT LSB itself.
7) If Carry is there, increment PRODUCT MSB.
8) Else Decrement COUNT by one.
9) If COUNT is not zero goto step 6.
10) Else goto end.
Program
#INCLUDE<P16F877A.INC>
ORG 0X00
GOTO START
COUNT
MULTIPLIER
MULTIPLICANT
PRODUCT_MSB
PRODUCT_LSB

EQU
EQU
EQU
EQU
EQU

0X20
0X21
0X22
0X23
0X24

ORG 0X05
START
CLRF PRODUCT_MSB
CLRF PRODUCT_LSB

; Clear the PRODUCT_MSB


; Clear the PRODUCT_LSB

Page 64 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

MOVLW
MOVWF
MOVWF
MOVLW
MOVWF
MOVF
BTFSC
GOTO
MOVF
BTFSC
GOTO

0X03
COUNT
MULTIPLIER
0XFF
MULTIPLICANT
MULTIPLIER,0
STATUS,Z
TERMINATE
MULTIPLICANT,0
STATUS,Z
TERMINATE

; COUNT = WREG
; MULTIPLIER = WREG
; WREG = 0XFF
; MULTIPLICANT = WREG
; WREG = MULTIPLIER
; Check Multiplier is Zero
; If Zero, TERMINATE
; WREG = MULTIPLICANT
; Check MULTIPLICANT is Zero
; If Zero, TERMINATE

ADDWF
BTFSC
INCF
DECFSZ
GOTO

PRODUCT_LSB,1
STATUS,C
PRODUCT_MSB,1
COUNT,1
L2

; PRODUCT_LSB = PRODUCT_LSB + WREG


; Check for Carry
; PRODUCT_MSB = PRODUCT_MSB + 1
; Decrement COUNT, check for Zero
; If not zero, GOTO L2

L2

TERMINATE
GOTO TERMINATE
END
Output
Two 8 bit numbers are multiplied and the result will be stored in 0x23 (Product MSB)
and 0x24 (Product LSB).

Division of two 8 bit numbers


Aim
To perform divisionof two 8 bit numbers and store the result.
Algorithm
1)
2)
3)
4)
5)

Clear 0x22 (Quotient) & 0x23 (Remainder).


Move first number to DIVIDENT & REMAINDER.
Move second number to DIVISOR.
If Divisor is zero (Division by Zero), set Quotient as 0xff and Remainder as 0x00.
Subtract WREG content (second number ie Divisor) from REMAINDER (initially
Divident) and store the result in REMAINDER itself.
6) If Borrow is there (Remainder < 0), add Divisor with Remainder and store it in
REMAINDER itself, then goto end.
7) Else Increment QUOTIENT.
8) Goto step 5.

Page 65 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Program
#INCLUDE<P16F877A.INC>
DIVIDENT EQU 0X20
DIVISOR
EQU 0X21
QUOTIENT EQU 0X22
REMINDER EQU 0X23
ORG 0X00
GOTO START
ORG 0X05
START
CLRF
CLRF
MOVLW
MOVWF
MOVWF
MOVLW
MOVWF
MOVF
BTFSC
GOTO

QUOTIENT
REMINDER
0X05
DIVIDENT
REMINDER
0X07
DIVISOR
DIVISOR,0
STATUS,Z
L1

; Clear QUOTIENT
; Clear REMINDER
; WREG = 0X05
; DIVIDENT = WREG
; REMINDER = WREG
; WREG = 0X07
; DIVISOR = WREG
; WREG = DIVISOR
; Check Divisor is zero.
; If zero, goto L1

SUBWF
BTFSS
GOTO
INCF
GOTO

REMINDER,1
STATUS,C
L3
QUOTIENT,1
L2

; REMINDER = REMINDER - WREG


; Check for borrow.
; If Borrow is there (Remainder < 0), goto L3
; Increment QUOTIENT

ADDWF
GOTO

REMINDER, 1
TERMINATE

; REMINDER = REMINDER + WREG

MOVLW
MOVWF
MOVWF

0XFF
QUOTIENT
REMINDER

; WREG = 0XFF
; QUOTIENT = WREG
; REMINDER = WREG

L2:

L3:

L1:

TERMINATE:
GOTO
END

TERMINATE
; Physical end of program

Output
Two 8 bit numbers are divided and the result will be stored in 0x22 (Quotient) and 0x23
(Remainder).
Page 66 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Addition of 5 numbers using Indirect Addressing Mode


Aim
To perform addition of 5 numbers using Indirect Addressing Mode and store the result.
Algorithm
1) Store 5 numbers in consecutive locations starting from 0x20.
2) Move 0x04 to COUNT.
3) Initialize FSR with address location 0x20.
4) Move the content of INDF to WREG.
5) Increment FSR.
6) Add the content of INDF with content of WREG & store the result in WREG.
7) If Carry is there, increment CARRY register.
8) Else Decrement COUNT & check if it is equal to zero.
9) If COUNT is not equal to zero, goto step 5.
10) Else store the result in SUM & terminate the program.
Program
#INCLUDE<P16F877A.INC>
COUNT EQU 0X30
SUM EQU 0X31
CARRY EQU 0X32
ORG 0X00
GOTO START
ORG 0X05
START
MOVLW 0XFF
MOVWF 0X20
MOVLW 0XFF
MOVWF 0X21
MOVLW 0XFF
MOVWF 0X22
MOVLW 0XFF
MOVWF 0X23
MOVLW 0XFF
MOVWF 0X24
MOVLW 0X04
MOVWF COUNT
MOVLW 0X20
MOVWF FSR
MOVF INDF,0

Page 67 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

L1
INCF FSR
ADDWF INDF,0
BTFSC STATUS,C
INCF CARRY,1
DECFSZ COUNT,1
GOTO L1
MOVWF SUM
L2
GOTO L2
END

Blinking 8 LEDs connected to a PORT


Aim
To Blink 8 LEDs connected to PORTB.
Circuit Diagram

Algorithm
1)
2)
3)
4)
5)
6)

Configure PORTB as output Port.


Make all LEDs ON.
Delay.
Make all LEDs OFF.
Delay.
Goto Step 2.
Page 68 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Registers associated with PORTB

Program
#INCLUDE<P16F877A.INC>
ORG 0X00
GOTO START
COUNT EQU 0X20
COUNT1 EQU 0X21
ORG 0X05
START
BANKSEL TRISB
MOVLW 0X00
MOVWF TRISB
; Configure PORTB Direction as Output
BANKSEL PORTB
CLRF PORTB
LOOP
BANKSEL PORTB
MOVLW 0XFF
MOVWF PORTB
; 8 LEDs ON
CALL DELAY
CALL DELAY
MOVLW 0X00
MOVWF PORTB
; 8 LEDs OFF
CALL DELAY
CALL DELAY
GOTO LOOP
DELAY
MOVLW 0XFF
MOVWF COUNT
L1
MOVLW 0XFF
MOVWF COUNT1
L2
DECFSZ COUNT1
GOTO L2
Page 69 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

DECFSZ COUNT
GOTO L1
RETURN
END
Output
LEDs connected to PORTB will start blinking.

Page 70 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Controlling an LED with a Switch


Aim
To control an LED connected to RC2 with a Switch connected to RB1.
1) When Switch is pressed LED should be ON.
2) When Switch is released LED should be OFF.
Circuit Diagram

Algorithm
1)
2)
3)
4)
5)
6)

Configure PORTC as Output.


Configure RB1 as input
Configure Weak Internal Pull-ups for PORTB.
If switch is pressed (RB1 = 0), make LED ON (RC2 = 1).
Else make LED OFF (RC2 = 0).
Goto step 4.

Registers associated with PORTB

Page 71 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Program
#INCLUDE<P16F877A.INC>
ORG 0X00
GOTO START
START
BSF STATUS,RP0
BCF STATUS,RP1
CLRF TRISC
BSF TRISB,1
BCF OPTION_REG,NOT_RBPU ; Enable weak Internal Pull Ups for PORTB
BCF STATUS,RP0
CLRF PORTC
LOOP
BTFSS PORTB,1
; Check whether switch is Pressed
GOTO L2
GOTO L3
L2
BSF PORTC,2
GOTO LOOP

; LED ON

BCF PORTC,2
GOTO LOOP

; LED OFF

L3

END

Page 72 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Output
On pressing the switch, LED will be ON, on releasing the switch, LED will be OFF.

Controlling an LED with a Switch using External Interrupt (INT0)


Aim
To control an LED connected to RC0 with a Switch connected to INT0 pin (RB0).
1) When Switch is pressed LED should be ON.
2) When Switch is released LED should be OFF.
Circuit

Page 73 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Algorithm
Main
1)
2)
3)
4)
5)
6)
7)
8)

Configure RB0/INT0 as input.


Configure PORTC as output.
Configure Weak Internal Pull-ups for PORTB.
Configure Interrupt on Falling Edge.
Enable INT0.
Enable Global Interrupt Enable.
If switch is pressed (RB0 = 0), goto step 7.
Else make LED OFF (RC0 = 0).

ISR
1) Disable GIE.
2) If INT0 Interrupt Flag Bit is set (INTF),
a. Clear INTF.
b. Make LED ON (RC0 = 1).
3) Else return to main program.
Register description of INTCON Register

Program
#INCLUDE<P16F877A.INC>
ORG 0X00
GOTO START
ORG 0X04
GOTO ISR
Page 74 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

START
BANKSEL TRISB
BSF
TRISB,0
CLRF TRISC
BCF OPTION_REG,NOT_RBPU
BCF OPTION_REG,INTEDG
; Configure Interrupt Edge as Falling Edge
BANKSEL PORTB
CLRF PORTC
BANKSEL TRISB
BSF INTCON,INTE
; Enable INT0 Interrupt
BSF INTCON,GIE
; Enable Global Interrupt
BCF INTCON,INTF
L1
BANKSEL PORTB
BTFSC PORTB,0
BCF PORTC,0
GOTO L1
ISR
BANKSEL INTCON
BCF INTCON,GIE
BTFSS INTCON,INTF
; Check INT0 Interrupt Flag
RETFIE
BCF INTCON,INTF
BANKSEL PORTB
BSF PORTC,0
RETFIE
; Return from ISR with Interrupt Enable (GIE = 1).
END
Output
On pressing the switch, LED will be ON, on releasing the switch, LED will be OFF.

Page 75 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Controlling an LED with a Switch using PORTB Pin Change Interrupt


Aim
To control an LED connected to RC0 with a Switch connected to RB4 pin.
1) When Switch is pressed LED should be ON.
2) When Switch is released LED should be OFF.
Circuit

Algorithm
Main
1)
2)
3)
4)
5)
6)
7)

Configure RB4 as input.


Configure PORTC as output.
Configure Weak Internal Pull-ups for PORTB.
Enable PORTB Pin Change Interrupt.
Enable Global Interrupt Enable (GIE).
If switch is pressed (RB4 = 0), goto step 7.
Else make LED OFF (RC0 = 0).

ISR
1)
2)
3)
4)
5)

Disable GIE.
If PORTB Pin Change Interrupt Flag Bit is set (RBIF),Clear RBIF.
Else return to main program.
If switch is pressed (RB4 = 0), Make LED ON (RC0 = 1).
Else return to main program.
Page 76 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Register description of INTCON Register

Program
#INCLUDE<P16F877A.INC>
ORG 0X00
GOTO START
ORG 0X04
GOTO ISR
START
BANKSEL TRISB
BSF
TRISB,4
CLRF TRISC
BANKSEL OPTION_REG
BCF
OPTION_REG,NOT_RBPU
BANKSEL PORTC
CLRF PORTC
BANKSEL INTCON
BSF INTCON,RBIE
; Enable PORTB Pin Change Interrupt
BSF INTCON,GIE
BCF INTCON,RBIF
L1
BANKSEL PORTB
BTFSC PORTB,4
BCF PORTC,0
GOTO L1

Page 77 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

ISR
BANKSEL INTCON
BSF INTCON,GIE
BTFSS INTCON,RBIF
RETFIE
BCF INTCON,RBIF
BTFSS PORTB,4
BSF PORTC,0
RETFIE
END
Output

; Check PORTB Pin Change Interrupt Flag

; Check whether switch is pressed

On pressing the switch, LED will be ON, on releasing the switch, LED will be OFF.

Page 78 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Generation of 5mS square wave using Timer0


Aim
To generate a square wave of 5mS On & Off time in PORTC using Timer0.
Circuit

Algorithm
1) Configure PORTC as output.
2) Configure Timer0 in Timer Mode.
3) Assign Prescalar to Timer0.
4) Configure Prescalar as 32.
5) Initialize Timer0 register with 0x63.
6) Wait until Timer0 Interrupt Flag (T0IF) is set.
7) Clear T0IF.
8) Compliment PORTC.
9) Re-initialize Timer0 register with 0x63.
10) Goto step 6.

Page 79 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Registers associated with Timer0

Register Description of OPTION_REG Register

Page 80 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Register description of INTCON Register

Description of TMR0 Register


TMR0 is the 8 bit Timer Register of Tmer0. Since it is an 8 bit Register, it can have
values from 0x00 to 0xFF ie 256 values. Since TMR0 is the Timer Register, it will be
incremented from initial value (0x00 default value) to final value (0xFF) in every clock cycle. If
4MHz is used as the Crystal Oscillator, each instruction will take 1 usec for its execution. So
TMR0 will take 256 usec in total to reach 0xFF from 0x00. Since there is a prescalar (1:256
max) in addition to TMR0 Register, Timer0 can generate upto maximum of 65 mS delay ie (256
* 256 usec) when using 1:256 as Prescalar.
Inorder to generate a delay lesser than 65 mS, the initial value that has to be loaded into
TMR0 register has to be changed.
Eg: Generate a delay of 5mS with Timer0 with 4MHz Crystal Oscillator.
Since Timer0 is used,
255 * 1 usec * Prescalar = 5000 usec
Prescalar = 5000/(255 * 1)
= 19 = 32 (Next higher Prescalar for Timer0)
Calculation of value that has to be loaded into TMR0 Register,

TMR0 = 255

= 255 (5000/32)
= 99 = 0x63

Page 81 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Program
#INCLUDE<P16F877A.INC>
ORG 0X00
GOTO START
START
BSF STATUS,RP0
BCF STATUS,RP1
CLRF TRISC
BCF STATUS,RP0
CLRF PORTC
BSF STATUS,RP0
BCF OPTION_REG,T0CS
BCF OPTION_REG,PSA
BSF OPTION_REG,PS2
BCF OPTION_REG,PS1
BCF OPTION_REG,PS0
BCF STATUS,RP0
MOVLW 0X63
MOVWF TMR0

; Select Timer Mode of operation for Timer0


; Assign Prescalar to Timer0
; Set Prescalar as 1:32

; Initialize TMR0 to generate 5mS delay with Timer0

L1
BTFSS INTCON,T0IF
GOTO L1

; Check Timer0 Interrupt Flag

BCF INTCON,T0IF
COMF PORTC,1

; Complement PORTC

MOVLW 0X63
MOVWF TMR0

; Re-initialize TMR0

GOTO L1
END

Page 82 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Output
Square wave on 5mS ON & OFF Time will be generated in PORTC which can be
verified from the Oscilloscope output.

Page 83 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Generation of 5mS square wave using Timer0 in Interrupt Mode


Aim
To generate a square wave of 5mS On & Off time in PORTC using Timer0 in Interrupt
Mode of operation.
Circuit

Algorithm
Main
1)
2)
3)
4)
5)
6)
7)
8)

Configure PORTC as output.


Configure Timer0 in Timer Mode.
Assign Prescalar to Timer0.
Configure Prescalar as 32.
Enable Timer0 Interrupt Enable.
Enable GIE.
Initialize Timer0 register with 0x63.
Goto step 8.

ISR
1) Disable GIE.
2) If Timer0 Interrupt Flag Bit is set (T0IF),
a. Clear T0IF.
b. Compliment PORTC.
c. Re-initialize Timer0 register with 0x63.
3) Else return to main program.
Page 84 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Register description of INTCON Register

Program
#INCLUDE<P16F877A.INC>
ORG 0X00
GOTO START
ORG 0X04
GOTO ISR
START
BANKSEL TRISC
CLRF TRISC
BANKSEL PORTC
CLRF PORTC
BANKSEL OPTION_REG
BCF OPTION_REG,T0CS
BCF OPTION_REG,PSA
BSF OPTION_REG,PS2
BCF OPTION_REG,PS1
BCF OPTION_REG,PS0
BANKSEL INTCON
BSF INTCON,T0IE
BSF INTCON,GIE

; Enable Timer0 Interrupt

BANKSEL TMR0
MOVLW 0X63
MOVWF TMR0
L1
GOTO L1
ISR
BCF INTCON,GIE
Page 85 of 129

Department of ECE, ASE Ettimadai

BTFSS INTCON,T0IF
RETFIE
BCF INTCON,T0IF
BANKSEL PORTC
COMF PORTC,1
BANKSEL TMR0
MOVLW 0X63
MOVWF TMR0
RETFIE
END

Microcontroller Laboratory Manual

; Check Timer0 Interrupt Flag

Output
Square wave on 5mS ON & OFF Time will be generated in PORTC which can be
verified from the Oscilloscope output.

Page 86 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Generation of 5mS square wave using Timer1 in Interrupt Mode


Aim
To generate a square wave of 5mS On & Off time in PORTC using Timer1 in Interrupt
Mode of operation.
Circuit

Algorithm
Main
1) Configure PORTC as output.
2) Configure Timer1 in Timer Mode.
3) Configure Prescalar as 1:1.
4) Initialize TMR1L with 0x77.
5) Initialize TMRIH with 0xEC.
6) Enable Timer1 Interrupt Enable.
7) Enable Peripheral Interrupt Enable (PEIE).
8) Enable GIE.
9) Switch on Timer1.
10) Goto step 10.
ISR
1) Disable GIE.
2) If Timer1 Interrupt Flag Bit is set (TMR1IF),
a. Clear TMR1IF.
b. Compliment PORTC.
c. Re-initialize TMR1L with 0x77.
Page 87 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

d. Re-initialize TMRIH with 0xEC.


3) Else return to main program.
Registers associated with Timer1

Register description of T1CON Register

Page 88 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Register description of PIE1 Register

Register description of PIR1 Register

Register description of INTCON Register

Description of TMR1H & TMR1L Registers


Timer1 is a 16 bit Timer. TMR1H is the 8 bit Timer Register which holds the 8 bit MSB
and TMR1L is an 8 bit Timer Register which holds the remaining 8 bit LSB of Timer1. Since it
is a 16 bit Timer, it can have values from 0x00 to 0xFFFF ie 65536 values. Since TMR1H &
Page 89 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

TMR1L is the 16 bit Timer Register, it will be incremented from initial value (0x00 default
value) to final value (0xFFFF) in every clock cycle. If 4MHz is used as the Crystal Oscillator,
each instruction will take 1 usec for its execution. So TMR1H & TMR1L together will take
65536 usec in total to reach 0xFFFF from 0x00. Since there is a prescalar (1:8 max) in addition
to TMR1H & TMR1L Register, Timer1 can generate upto maximum of 524 mS delay ie (65536
* 8 usec) when using 1:8 as Prescalar.
Inorder to generate a delay lesser than 524 mS, the initial value that has to be loaded into
TMR1H & TMR1L registers has to be changed.
Eg: Generate a delay of 5mS with Timer1 with 4MHz Crystal Oscillator.
Since Timer1 is used,
65535 * 1 usec * Prescalar = 5000 usec
Prescalar = 5000/(65535 * 1)
= 0.08 = 1 (Next higher Prescalar for Timer1)
Calculation of value that has to be loaded into TMR1H & TMR1L Register,

TMR1H: TMR1L = 65535

= 65535 (5000/1)
= 60535 = 0 x EC 77

TMR1H = 0xEC

TMR1L = 0x77

Program
#INCLUDE<P16F877A.INC>
ORG 0X00
GOTO START
ORG 0X04
GOTO ISR
ORG 0X05
START
BANKSEL TRISC
CLRF TRISC
BANKSEL T1CON
BCF T1CON,TMR1CS
BCF T1CON,T1CKPS1
BCF T1CON,T1CKPS0

; Select Timer Mode of Operation for Timer1


; Set Timer1 Prescalar as 1:1

Page 90 of 129

Department of ECE, ASE Ettimadai

BANKSEL TMR1L
MOVLW 0X77
MOVWF TMR1L
BANKSEL TMR1H
MOVLW 0XEC
MOVWF TMR1H
BANKSEL PIE1
BSF PIE1,TMR1IE
BSF INTCON,PEIE
BSF INTCON,GIE
BANKSEL T1CON
BSF T1CON,TMR1ON

Microcontroller Laboratory Manual

; Initialize TMR1L with 0x77

; Initialize TMR1H with 0xEC


; Enable Timer1 Interrupt
; Enable Peripheral Interrupts

; Switch on Timer1

L1
GOTO L1
ISR
BCF INTCON,GIE
BANKSEL PIR1
BTFSS PIR1,TMR1IF
RETFIE
BCF PIR1,TMR1IF
BANKSEL PORTC
COMF PORTC,1
BANKSEL TMR1L
MOVLW 0X77
MOVWF TMR1L
BANKSEL TMR1H
MOVLW 0XEC
MOVWF TMR1H
RETFIE

; Check Timer1 Interrupt Flag

END

Page 91 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Output
Square wave on 5mS ON & OFF Time will be generated in PORTC which can be
verified from the Oscilloscope output.

Page 92 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Generation of 5mS square wave using Timer2 in Interrupt Mode


Aim
To generate a square wave of 5mS On & Off time in PORTC using Timer2 in Interrupt
Mode of operation.
Circuit

Algorithm
Main
1) Configure PORTC as output.
2) Configure Prescalar as 1:16.
3) Configure Postscalar as 1:2.
4) Initialize PR2 with 0x9C.
5) Initialize Timer2 register with 0x00.
6) Enable Timer2 Interrupt Enable.
7) Enable PEIE.
8) Enable GIE.
9) Switch on Timer2.
10) Goto step 10.
ISR
1) Disable GIE.
2) If Timer2 Interrupt Flag Bit is set (TMR2IF),
a. Clear TMR2IF.
b. Compliment PORTC.
c. Re-initialize Timer2 register with 0x00.
3) Else return to main program.
Page 93 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Registers associated with Timer2

Register description of T2CON Register

Register description of PIE1 Register

Page 94 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Register description of PIR1 Register

Description of TMR2 Register


TMR2 is the 8 bit Timer Register of Tmer2. Since it is an 8 bit Register, it can have
values from 0x00 to 0xFF ie 256 values. Since TMR2 is the Timer Register, it will be
incremented from initial value (0x00) to final value (0xFF default value) in every clock cycle. If
4MHz is used as the Crystal Oscillator, each instruction will take 1 usec for its execution. So
TMR2 will take 256 usec in total to reach 0xFF from 0x00. Since there is a prescalar (1:16 max)
and postscalar (1:16 max) in addition to TMR2 Register, Timer2 can generate upto maximum of
65 mS delay ie (256 * 16 * 16 usec) when using 1:16 as Prescalar & 1:16 as Postscalar.
Inorder to generate a delay lesser than 65 mS, the final value has to be changed. Final
value till which the Timer2 register (TMR2) has to be incremented is specified in PR2 Register.
So that Timer2 can increment from 0x00 to final value that is specified in PR2 Register.
Eg: Generate a delay of 5mS with Timer2 with 4MHz Crystal Oscillator.
Since Timer0 is used,
255 * 1 usec * Prescalar * Postscalar = 5000 usec
Prescalar * Postscalar = 5000/(255 * 1)
= 19 = 32
Therefore,
Prescalar = 16 & Postscalar = 2

Page 95 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Calculation of value that has to be loaded into PR2 Register,


(

PR2 =

= (5000/(16 * 2))
= 156 = 0x9C
Program
#INCLUDE<P16F877A.INC>
ORG 0X00
GOTO START
ORG 0X04
GOTO ISR
ORG 0X05
START
BANKSEL TRISC
CLRF TRISC
BANKSEL T2CON
BSF T2CON,T2CKPS1
BSF T2CON,T2CKPS0
BCF T2CON,TOUTPS3
BCF T2CON,TOUTPS2
BCF T2CON,TOUTPS1
BSF T2CON,TOUTPS0
BANKSEL PR2
MOVLW 0X9C
MOVWF PR2
BANKSEL TMR2
CLRF TMR2
BANKSEL PIE1
BSF PIE1,TMR2IE
BSF INTCON,PEIE
BSF INTCON,GIE
BANKSEL T2CON
BSF T2CON,TMR2ON
L1
GOTO L1
ISR
BCF INTCON,GIE
BANKSEL PIR1
BTFSS PIR1,TMR2IF
RETFIE
BCF PIR1,TMR2IF
BANKSEL PORTC
COMF PORTC,1

; Set Prescalar as 1:16


; Set Postscalar as 1:2

; Set final value of Timer2 with 0x9C

; Enable Timer2 Interrupt

; Switch ON Timer2

; Check Timer2 Interrupt Flag

Page 96 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

BANKSEL TMR2
CLRF TMR2
RETFIE
END
Output
Square wave on 5mS ON & OFF Time will be generated in PORTC which can be
verified from the Oscilloscope output.

Page 97 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Continuous Transmission of a character using USART in Asynchronous


Mode of operation
Aim
To transmit a character A continuously from PIC using USART in Asynchronous Mode
of Operation at 9600bps.
Circuit

Algorithm
1)
2)
3)
4)
5)
6)
7)
8)

Configure High Speed Transmission (Set BRGH).


Configure Baud Rate as 9600 (Move 25 to SPBRG).
Configure Asynchronous Mode of Transmission.
Enable Transmission.
Configure RC6 & RC7 as TX & RX pin of USART (Set SPEN =1).
Wait until Transmission Interrupt Flag is set (TXIF).
Move A to Transmit Register.
Goto step 6.

Page 98 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Register associated with Asynchronous Transmitter

Register description of TXSTA Register

Page 99 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Register description of RCSTA Register

Register description of PIE1 Register

Register description of PIR1 Register

Page 100 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Description of SPBRG Register


SPBRG Register is the register used for setting the Baud Rate (Bits Per Second) at which
Transmission or Reception has to be taken place.
For setting a Baud Rate as say 9600bps, a particular 8 bit Value has to be moved to SPBRG.
For finding this value there are separate equations when BRGH = 1 & BRGH = 0,

By substituting the value of Fosc (Crystal Oscillator Frequency) and the Desired Baud Rate, the
value of X can be found out. This value has to be moved to SPBRG inorder to set the Desired
Baud Rate.
Eg:

Fosc = 4MHz & Baud Rate = 9600bps whwn BRGH = 1,


9600bps = 4MHz/(16(X+1))
Therefore,
X = 25 = 0x19.

So SPBRG = 0x19 will generate Baud Rate of 9600bps when using 4MHz Crsytal Oscillator.
Usually instead performing this calculations, the SPBRG values corresponding to popular Baud
Rates are given in the form of a Table in the Data sheet.
There are Tables corresponding to BRGH = 1 & BRGH = 0.

Page 101 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Page 102 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Program
#INCLUDE<P16F877A.INC>
ORG 0X00
GOTO START
ORG 0X05
START
BANKSEL TXSTA
BSF TXSTA,BRGH
BANKSEL SPBRG
MOVLW .25
MOVWF SPBRG
BANKSEL TXSTA
BCF TXSTA,SYNC
BSF TXSTA,TXEN
BANKSEL RCSTA
BSF RCSTA,SPEN
LOOP
BANKSEL PIR1
BTFSS PIR1,TXIF
GOTO LOOP
BANKSEL TXREG
MOVLW 'A'
MOVWF TXREG
GOTO LOOP
END

; Set High Speed Transmission

; Set Baud Rate as 9600bps


; Set Asynchronous Transmission
; Enable Transmission
; Configure RC6 & RC7 as USART Pins

; Check Transmission Interrupt Flag

; Transmit A

Output
The character A will be continuously transmitted from PIC to PC which can be seen on
Virtual Terminal window of Proteus.

Page 103 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Note: For adding Virtual Terminal to Workspace click on Virtual Instruments Mode in
Proteus Toolbar. Then Select Virtual Instruments from Virtual Instruments List.

Page 104 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Reception and Transmission of the same character using USART in


Asynchronous Mode of operation
Aim
To receive a character that is transmitted from PC and transmit this same character back
to the PC using USART in Asynchronous Mode of Operation at 9600bps.
Circuit

Algorithm
Main
1) Configure High Speed Transmission (Set BRGH).
2) Configure Baud Rate as 9600 (Move 25 to SPBRG).
3) Configure Asynchronous Mode of Transmission.
4) Enable Transmission.
5) Enable Continuous Receive.
6) Configure RC6 & RC7 as TX & RX pin of USART (Set SPEN =1).
7) Wait until Receive Interrupt Flag is set (RCIF).
8) Read Receive Register to W register.
9) Wait until Transmit Interrupt Flag is set (TXIF).
10) Move read character from W register to Transmit Register.
11) Goto step 7

Page 105 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Register associated with Asynchronous Transmitter

Register description of RCSTA Register

Page 106 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Register description of PIE1 Register

Register description of PIR1 Register

Program
PROCESSOR P16F877
#INCLUDE<P16F877.INC>
ORG 0X00
GOTO START
ORG 0X05
START
BANKSEL TXSTA
BSF TXSTA,BRGH
Page 107 of 129

Department of ECE, ASE Ettimadai

BANKSEL SPBRG
MOVLW .25
MOVWF SPBRG
BANKSEL TXSTA
BCF TXSTA,SYNC
BSF TXSTA,TXEN
BANKSEL RCSTA
BSF RCSTA,CREN
BSF RCSTA,SPEN

Microcontroller Laboratory Manual

; Enable Continous Receive

LOOP
BANKSEL PIR1
BTFSS PIR1,RCIF
GOTO LOOP
BANKSEL RCREG
MOVF RCREG,0
BANKSEL PIR1
LOOP1
BTFSS PIR1,TXIF
GOTO LOOP1
BANKSEL TXREG
MOVWF TXREG
GOTO LOOP
END

; Check Receive Interrupt Flag

; Move received character to WREG

; Check Transmit Interrupt Flag

; Transmit received character to PC

Output
The characters which have been entered through the PCs keyboard, will be received by
PIC and then it will be retransmitted back to the PC.
So whatever characters that are typed in the Virtual Terminal will be echoed back in Virtual
Terminal window.

Page 108 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Analog to Digital Conversion


Aim
To convert the analog signal that is given by a Potentiometer connected to Channel 0
(RA0) and display the digital data on 8 LEDs connected to PORTB.
Circuit

Algorithm
1) Configure RA0 as input.
2) Configure PORTB as output.
3) Configure PORTA & PORTE as Analog Input Pins.
4) Configure ADC Clock Source as FOSC/32.
5) Select Analog Input Channel 0 (RA0) as ADC input Channel.
6) Select Left Justification for ADC Result Format.
7) Switch ON ADC.
8) Start ADC Conversion.
9) Wait until ADC Conversion is completed.
10) Move ADC Result from ADRESH to W register.
11) Move ADC Result from W register to PORTB.
12) Goto step 8.

Page 109 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Registers associated with ADC

Register description for ADCON0 Register

Page 110 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Register description for ADCON1 Register

Program
PROCESSOR P16F877
#INCLUDE<P16F877.INC>
ORG 0X00
MOVLW 0X00
MOVWF PCLATH
CLRF STATUS
GOTO START
START
BANKSEL TRISA
BSF TRISA,0
BANKSEL TRISB

; Configure RA0 as input


Page 111 of 129

Department of ECE, ASE Ettimadai

CLRF TRISB
BANKSEL PORTB
CLRF PORTB
BANKSEL ADCON1
CLRF ADCON1
BANKSEL ADCON0
BSF ADCON0,ADCS1
BCF ADCON0,ADCS0
BCF ADCON0,CHS2
BCF ADCON0,CHS1
BCF ADCON0,CHS0
BANKSEL ADCON1
BCF ADCON1,ADFM
BANKSEL ADCON0
BSF ADCON0,ADON
BSF ADCON0,GO_DONE

Microcontroller Laboratory Manual

; Configure ADC Clock as Fosc/32


; Select ADC input channel as Channel 0

; Select ADC Result Format as Left Justified


; Switch ON ADC
; Start ADC Conversion

LOOP

L1

BANKSEL ADCON0
BSF ADCON0,GO_DONE
BTFSC
ADCON0,GO_DONE
; Check for End of Conversion of ADC
GOTO L1
BANKSEL ADRESH
MOVF ADRESH,0
; Move MSB 8 bit of ADC Result to WREG
MOVWF PORTB
; Move ADC Result to PORTB
GOTO LOOP
END

Output
On varying the potentiometer, the corresponding digital value will be displayed on the
LEDs.

Page 112 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Analog to Digital Conversion in Interrupt Mode


Aim
To convert the analog signal that is given by a Potentiometer connected to Channel 0
(RA0) and display the digital data on 8 LEDs connected to PORTB in Interrupt Mode.
Circuit

Register description of PIE1 Register

Page 113 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Register description of PIR1 Register

Program

PROCESSOR P16F877
#INCLUDE<P16F877.INC>
ORG 0X00
GOTO START
ORG 0X04
GOTO ISR
START
BANKSEL TRISA
BSF TRISA,0
BANKSEL TRISB
CLRF TRISB
BANKSEL PORTB
CLRF PORTB
BANKSEL ADCON1
CLRF ADCON1
BANKSEL ADCON0
BSF ADCON0,ADCS1
BSF ADCON0,ADCS0
BCF ADCON0,CHS2
BCF ADCON0,CHS1
BCF ADCON0,CHS0
BANKSEL ADCON1
BCF ADCON1,ADFM
BANKSEL PIE1
BSF PIE1,ADIE
; Enable ADC Interrupt
BANKSEL INTCON
BSF INTCON,PEIE
BSF INTCON,GIE
BANKSEL ADCON0
BSF ADCON0,ADON
BSF ADCON0,GO_DONE
Page 114 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

L1
NOP
NOP
NOP
BSF ADCON0,GO_DONE
GOTO L1

ISR
BSF INTCON,GIE
BANKSEL PIR1
BTFSS PIR1,ADIF
RETFIE
BCF PIR1,ADIF

; Check for ADC Interrupt Flag

BANKSEL ADRESH
MOVF ADRESH,0
MOVWF PORTB
RETFIE
END
Output
On varying the potentiometer, the corresponding digital value will be displayed on the
LEDs.

Page 115 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Generation of PWM
Aim
To generate a PWM signal of 5kHz frequency and 80% duty cycle using 20MHz as the
Crystal Oscillator Frequency in CCP1 pin (RC2).
Circuit

Registers associated with PWM Operation

Page 116 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Register description for CCPxCON Register

Register description of PIE1 Register

Page 117 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Register description of PIR1 Register

Description of PR2 Register


PR2 register is used for setting the Period of PWM. It is an 8 bit register.
Period of PWM signal can be set by moving an 8 bit value to PR2 register. For finding out this
value, there is an equation,

PR2 =

-1

Description of CCPRxL Register and CCPxX & CCPxY Bits


In PIC16F877A, PWM Module with 8 bit PWM Period (PR2 Register) and 10 bit On
Time is present. 8 bits of On Time can be given in CCPRxL Register and the remaining 2 bits
can be given in CCPxX & CCPxY bits of CCPxCON Register ie 4 th and 5 th bits.
CCPRxL: CCPxCON < 5: 4 > =

Duty Cycle Period


Tosc TMR2 Prescalar

Page 118 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Eg: Generate a PWM of 5kHz frequency and 80% duty cycle using 20MHz as the Crystal
Oscillator Frequency.
Calculation of Period value that has to be written to PR2 Register,
Tpclk (Time for 1 instr) = 1/(20MHz/4) = 0.2 usec. Since, 1Pclk = 20MHz/4
PWM Period = 1/5kHz = 200 usec.
Tosc = 1/20MHz = 0.05 usec.
Since Timer2 is used,
255*0.2 usec*Prescalar = 200 usec
Prescalar = 200/(255*0.2) = 3.92 = 4
PR2 = ( ( PWM Period) / ( Tosc x 4 x Prescaler ) ) - 1
= ( ( 200 usec ) / ( 0.05 usec x 4 x 4 )) - 1
= 250 1
= 249 = 0xF9
Calculation of On Time that has to be written to CCPRxL:CCPxCON<5:4> Registers,
(CCPRxL:CCPxCON<5:4>) = (Duty Cycle*Period)/(Tosc*(Prescalar))

= (0.8x(1/5,000))/((1/20,000,000)*4)
= (0.8x200 usec)/(0.05 usec*4)
= 800 = 0x320
= 11001000 00 (binary)

CCPRxL = 11001000 = 0xC8

CCPxCON<5:4> ie CCPxX = 0 CCPxY = 0

On Time

Program
#INCLUDE<P16F877A.INC>
ORG 0X00
GOTO START
ORG 0X05
START
BANKSEL TRISC
BCF TRISC,2
Page 119 of 129

Department of ECE, ASE Ettimadai

BANKSEL T2CON
CLRF T2CON
BCF T2CON,T2CKPS1
BSF T2CON,T2CKPS0
BANKSEL PR2
MOVLW 0XF9
MOVWF PR2
BANKSEL CCPR1L
MOVLW 0XC8
MOVWF CCPR1L
BANKSEL CCP1CON
BCF CCP1CON,CCP1X
BCF CCP1CON,CCP1Y
MOVLW 0X0F
MOVWF CCP1CON
BANKSEL T2CON
BSF T2CON,TMR2ON

Microcontroller Laboratory Manual

; Set Prescalar as 1:4

; Initialize PWM Period as 0xF9

; Initialize 8 bits of On Time as 0xC8


; Initialize 2 bit LSBs of On Time as 00

; Switch ON Timer2

L1
GOTO L1
END
Output
PWM with 5kHz frequency and 80% duty cycle will be generated in CCP1 (RC2)pin.
Note:
Crystal Oscillator frequency should be set as 20MHz in Proteus.

Page 120 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

EEPROM Read and Write


Aim
1) To write data (0x06) to address 0x01 of EEPROM.
2) Read the data back from the same address and display it on 8 LEDs connected to
PORTB.
Circuit

Registers associated with EEPROM

Page 121 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Register description for EECON1 Register

Register description of PIE2 Register

Register description of PIR2 Register

Page 122 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Programs
#INCLUDE<P16F877A.INC>
ORG 0X00
GOTO START
ORG 0X05
START
BANKSEL TRISB
CLRF TRISB
BANKSEL PORTB
CLRF PORTB
BANKSEL EECON1
BTFSC EECON1, WR
GOTO $-1
BANKSEL EEADR
MOVLW 0X01
MOVWF EEADR
BANKSEL EEDATA
MOVLW 0X06
MOVWF EEDATA
BANKSEL EECON1
BCF EECON1, EEPGD
BSF EECON1, WREN
BCF INTCON, GIE
BANKSEL EECON2
MOVLW 0x55
MOVWF EECON2
MOVLW 0xAA
MOVWF EECON2
BANKSEL EECON1
BSF EECON1, WR
BSF INTCON, GIE ;
BCF EECON1, WREN

; Wait for write to finish

; Write Address (0x01) to EEADR Register

; Write Data (0x06) to EEDATA Register


; Set EEPROM as Data memory
; Enable Write to EEPROM

; Write control word 55h to EECON2


; Write control word AAh to EECON2

; Start write operation


; Disable Write operation to EEPROM

BANKSEL EEADR
Page 123 of 129

Department of ECE, ASE Ettimadai

MOVLW 0X01
MOVWF EEADR
BANKSEL EECON1
BCF EECON1, EEPGD
BSF EECON1, RD
BANKSEL EEDATA
MOVF EEDATA, W
MOVWF PORTB

Microcontroller Laboratory Manual

; Move the contents of EEPROM to WREG


; Move EEPROM contents to PORTB

L1
GOTO L1
END
Output
EEPROM data (0x06) will be displayed in 8 LEDs.

Page 124 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Compare Mode of Operation of CCP


Aim
To perform the compare operation,
1) Set CCP1 pin as High.
2) Complement PORTB after each Compare event.
Circuit

Registers associated with Compare Mode of Operation

Page 125 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Register description for CCPxCON Register

Register description of PIE1 Register

Page 126 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

Register description of PIR1 Register

Program
#INCLUDE<P16F877A.INC>
ORG 0X00
GOTO START
ORG 0X04
GOTO ISR
ORG 0X05
START
BANKSEL TRISB
BCF TRISC,2
CLRF TRISB
BANKSEL PORTC
CLRF PORTB
BANKSEL T1CON
BSF T1CON,T1CKPS1
BSF T1CON,T1CKPS0
BCF T1CON,TMR1CS
BANKSEL TMR1L
MOVLW 0X00
MOVWF TMR1L
BANKSEL TMR1H
MOVLW 0X00
MOVWF TMR1H

; Set Prescalar as 1:8


; Set Timer Mode of operation for Timer1

BANKSEL CCP1CON
Page 127 of 129

Department of ECE, ASE Ettimadai

CLRF CCP1CON
MOVLW 0X08
MOVWF CCP1CON
BANKSEL PIR1
BCF PIR1,CCP1IF

Microcontroller Laboratory Manual

; Set Compare Mode of operation for CCP1,


; set CCP1 (RC2) pin on Compare event

BANKSEL CCPR1H
MOVLW 0XF1
MOVWF CCPR1H
BANKSEL CCPR1L
MOVLW 0XFF
MOVWF CCPR1L

BANKSEL PIE1
BSF PIE1,TMR1IE
BSF PIE1,CCP1IE
BSF INTCON,PEIE
BSF INTCON,GIE
BANKSEL T1CON
BSF T1CON,TMR1ON

; Enable CCP1 Interrupt

L1
GOTO L1
ISR
BCF INTCON,GIE
BANKSEL PIR1
BTFSS PIR1,CCP1IF
GOTO TMR_ISR
BCF PIR1,CCP1IF
BANKSEL CCPR1H
BANKSEL PORTB
COMF PORTB,1
RETFIE
TMR_ISR
BCF INTCON,GIE
BANKSEL PIR1
BTFSS PIR1,TMR1IF
RETFIE
BCF PIR1,TMR1IF
BANKSEL TMR1L
MOVLW 0X00
MOVWF TMR1L
BANKSEL TMR1H
MOVLW 0X00

; Check CCP1 Interrupt Flag

; Check Timer1 Interrupt Flag

Page 128 of 129

Department of ECE, ASE Ettimadai

Microcontroller Laboratory Manual

MOVWF TMR1H
RETFIE
END
Output
PORTB will get complemented after each Compare event. CCP1 (RC2) pin will be set
after the first Compare operation.

Page 129 of 129

Potrebbero piacerti anche