Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
LABORATORY MANUAL
V SEMESTER B.Tech (ECE & EIE)
Prepared By,
Mr. Peeyush.K.P.
Assistant Professor
Department of Electronics & Communication Engineering
ASE Ettimadai
TABLE OF CONTENTS
1)
2)
3)
4)
5)
6)
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
8085 SIMULATOR
1) Click on 8085 Simulator icon from Desktop.
Register window
Memory Editor window
Assembler 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.
Page 1 of 129
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.
Assembler window
Assemble button
Page 2 of 129
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
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.
7) Click on Step By Step button in the Simulator window inorder to perform the execution of the
program step by step.
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
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.
Page 5 of 129
10) Select Show only loaded memory location radio button in Memory Editor window.
Page 6 of 129
MPLAB IDE
Project Creation
1) Open MPLAB IDE 7.52 icon from the desktop.
Menubar
Toolbar
Status bar
Page 7 of 129
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
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
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.
Project Name
Page 10 of 129
13) Verify that the Path for the Project is displayed properly as E:\MP_Lab\PIC_Project.
14) Click on Next.
Project Path
Page 11 of 129
Page 12 of 129
18) Click on File -> New File or click on New File icon in the Toolbar.
New File
Page 13 of 129
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
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
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
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.
Page 17 of 129
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
FIRSTNO symbol is added to watch window, so that the value of FIRSTNO can be monitored.
Page 19 of 129
5) The address location of FIRSTNO also can be identified from the Watch window.
Page 20 of 129
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
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
PROTEUS 7 PROFESSIONAL
1) Click on Start -> Program Files -> Proteus 7 Professional -> ISIS 7 Professional.
ISIS 7 Professional
Page 23 of 129
Menubar
Toolbar
Device List
Workspace
Toolbar
Component Mode
Page 24 of 129
Page 25 of 129
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
PIC16F877A
Device List
Page 27 of 129
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
Page 28 of 129
Page 29 of 129
Now all the devices required for drawing the circuits are added to the Devices List.
OK Button
Page 30 of 129
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
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.
15) Inorder to fix the PIC in the desired position, click ones more in the workspace.
16) Click on Zoom button in the Toolbar.
Page 32 of 129
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).
Viewable Area
Viewable Area
Page 33 of 129
After this step PIC has been brought back to the viewable area.
Page 34 of 129
Page 35 of 129
Terminals Mode
Ground
Page 36 of 129
24) Click twice in the workspace so that the Ground Terminal will be added to the workspace.
Red Square
Page 37 of 129
26) Click on this red square, move the cursor to the ending point.
Starting Point
Single Click
Ending Point
Single Click
Page 38 of 129
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
29) Right click on PIC IC in the workspace. Select Edit Properties of PIC.
Right Click
Edit Properties
Page 40 of 129
30) Click on Browse button, inorder to add the Program hex file.
Browse
Page 41 of 129
Led.hex
Open Button
Page 42 of 129
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
36) Click on Play button located at the bottom left corner to start the simulation.
Play Button
Page 44 of 129
Virtual Instruments
Mode
Page 45 of 129
Oscilloscope
Page 46 of 129
Cursors button
6) Move the mouse pointer to Oscilloscope screen, then cursor will be displayed
Cursor
Page 47 of 129
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
8085 ARCHITECTURE
Page 49 of 129
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
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
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
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
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
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)
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
Output
Input
Output
Address
4150
4151
4152
4153
Page 54 of 129
Data
FF
FF
FE
01
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
Output
Input
Output
Address
4150
4151
4152
4153
Page 55 of 129
Data
05
03
02
01
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
DCR
JNZ
STA
MOV
STA
HLT
NEXT:
C
LOOP
4152
A,D
4153
Output
Input
Output
Address
4150
4151
4152
4153
Page 56 of 129
Data
FF
02
FE
01
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
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
Output
Input
Output
Address
4150
4151
4152
4153
Page 57 of 129
Data
02
05
01
02
PIC16F877A ARCHITECTURE
Page 58 of 129
PIC16F877A PERIPHERALS
Page 59 of 129
Page 60 of 129
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)
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
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
MOVLW
0XA2
MOVWF
NUMBER_1
MOVLW
0X62
MOVWF
NUMBER_2
ADDWF
NUMBER_1, 0
MOVWF
SUM
BTFSC
STATUS, C
INCF CARRY, 1
L1
GOTO L1
END
Output
Two 8 bit numbers are added and the result will be stored in 0x22 with Carry stored in
0x23.
Page 62 of 129
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
Page 63 of 129
L1
GOTO
END
L1
Output
Two 8 bit numbers are subtracted and the result will be stored in 0x22 with Borrow
stored in 0x23.
EQU
EQU
EQU
EQU
EQU
0X20
0X21
0X22
0X23
0X24
ORG 0X05
START
CLRF PRODUCT_MSB
CLRF PRODUCT_LSB
Page 64 of 129
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
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).
Page 65 of 129
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
ADDWF
GOTO
REMINDER, 1
TERMINATE
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
Page 67 of 129
L1
INCF FSR
ADDWF INDF,0
BTFSC STATUS,C
INCF CARRY,1
DECFSZ COUNT,1
GOTO L1
MOVWF SUM
L2
GOTO L2
END
Algorithm
1)
2)
3)
4)
5)
6)
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
DECFSZ COUNT
GOTO L1
RETURN
END
Output
LEDs connected to PORTB will start blinking.
Page 70 of 129
Algorithm
1)
2)
3)
4)
5)
6)
Page 71 of 129
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
Output
On pressing the switch, LED will be ON, on releasing the switch, LED will be OFF.
Page 73 of 129
Algorithm
Main
1)
2)
3)
4)
5)
6)
7)
8)
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
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
Algorithm
Main
1)
2)
3)
4)
5)
6)
7)
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
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
ISR
BANKSEL INTCON
BSF INTCON,GIE
BTFSS INTCON,RBIF
RETFIE
BCF INTCON,RBIF
BTFSS PORTB,4
BSF PORTC,0
RETFIE
END
Output
On pressing the switch, LED will be ON, on releasing the switch, LED will be OFF.
Page 78 of 129
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
Page 80 of 129
TMR0 = 255
= 255 (5000/32)
= 99 = 0x63
Page 81 of 129
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
L1
BTFSS INTCON,T0IF
GOTO L1
BCF INTCON,T0IF
COMF PORTC,1
; Complement PORTC
MOVLW 0X63
MOVWF TMR0
; Re-initialize TMR0
GOTO L1
END
Page 82 of 129
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
Algorithm
Main
1)
2)
3)
4)
5)
6)
7)
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
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
BANKSEL TMR0
MOVLW 0X63
MOVWF TMR0
L1
GOTO L1
ISR
BCF INTCON,GIE
Page 85 of 129
BTFSS INTCON,T0IF
RETFIE
BCF INTCON,T0IF
BANKSEL PORTC
COMF PORTC,1
BANKSEL TMR0
MOVLW 0X63
MOVWF TMR0
RETFIE
END
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
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
Page 88 of 129
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,
= 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
Page 90 of 129
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
; 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
END
Page 91 of 129
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
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
Page 94 of 129
Page 95 of 129
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
; Switch ON Timer2
Page 96 of 129
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
Algorithm
1)
2)
3)
4)
5)
6)
7)
8)
Page 98 of 129
Page 99 of 129
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:
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.
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
; Transmit A
Output
The character A will be continuously transmitted from PIC to PC which can be seen on
Virtual Terminal window of Proteus.
Note: For adding Virtual Terminal to Workspace click on Virtual Instruments Mode in
Proteus Toolbar. Then Select Virtual Instruments from Virtual Instruments List.
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
Program
PROCESSOR P16F877
#INCLUDE<P16F877.INC>
ORG 0X00
GOTO START
ORG 0X05
START
BANKSEL TXSTA
BSF TXSTA,BRGH
Page 107 of 129
BANKSEL SPBRG
MOVLW .25
MOVWF SPBRG
BANKSEL TXSTA
BCF TXSTA,SYNC
BSF TXSTA,TXEN
BANKSEL RCSTA
BSF RCSTA,CREN
BSF RCSTA,SPEN
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
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.
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.
Program
PROCESSOR P16F877
#INCLUDE<P16F877.INC>
ORG 0X00
MOVLW 0X00
MOVWF PCLATH
CLRF STATUS
GOTO START
START
BANKSEL TRISA
BSF TRISA,0
BANKSEL TRISB
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
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.
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
L1
NOP
NOP
NOP
BSF ADCON0,GO_DONE
GOTO L1
ISR
BSF INTCON,GIE
BANKSEL PIR1
BTFSS PIR1,ADIF
RETFIE
BCF PIR1,ADIF
BANKSEL ADRESH
MOVF ADRESH,0
MOVWF PORTB
RETFIE
END
Output
On varying the potentiometer, the corresponding digital value will be displayed on the
LEDs.
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
PR2 =
-1
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)
On Time
Program
#INCLUDE<P16F877A.INC>
ORG 0X00
GOTO START
ORG 0X05
START
BANKSEL TRISC
BCF TRISC,2
Page 119 of 129
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
; 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.
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
BANKSEL EEADR
Page 123 of 129
MOVLW 0X01
MOVWF EEADR
BANKSEL EECON1
BCF EECON1, EEPGD
BSF EECON1, RD
BANKSEL EEDATA
MOVF EEDATA, W
MOVWF PORTB
L1
GOTO L1
END
Output
EEPROM data (0x06) will be displayed in 8 LEDs.
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
BANKSEL CCP1CON
Page 127 of 129
CLRF CCP1CON
MOVLW 0X08
MOVWF CCP1CON
BANKSEL PIR1
BCF PIR1,CCP1IF
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
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
MOVWF TMR1H
RETFIE
END
Output
PORTB will get complemented after each Compare event. CCP1 (RC2) pin will be set
after the first Compare operation.