Microcontroller Lab Mnual

131
7/23/2019 Microcontroller Lab Mnual http://slidepdf.com/reader/full/microcontroller-lab-mnual 1/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

Transcript of Microcontroller Lab Mnual

Page 1: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 1/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

Page 2: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 2/131

TABLE OF CONTENTS

1)  Study of 8085 Simulator 1

2)  Study of MPLAB IDE 7

3) 

Study of Proteus 7 Professional 234)  8085 Architecture 49

5)  8085 Instruction Set Summary 50

6)  8085 Programs 54

a.  Addition of two 8 bit numbers 54

 b.  Subtraction of two 8 bit numbers 55

c.  Multiplication of two 8 bit numbers 56

d.  Division of two 8 bit numbers 57

7)  PIC16F877A Architecture 58

8)  PIC16F877A Peripherals 59

9) 

PIC16F877A Instruction Set Summary 60

10) MPLAB Programs 61

a.  Addition of two 8 bits numbers 61

 b.  Addition of two 8 bit numbers with carry 62

c.  Subtraction of two 8 bit numbers with borrow 63

d.  Multiplication of two 8 bit numbers 64

e.  Division of two 8 bit numbers 65

f.  Addition of 5 numbers using Indirect Addressing Mode 67

g.  Blinking 8 LEDs connected to a PORT 68

h. 

Controlling an LED with a Switch 71i.  Controlling an LED with a Switch using External Interrupt (INT0) 73

 j.  Controlling an LED with a Switch using PORTB Pin Change Interrupt 76

k.  Generation of 5mS square wave using Timer0 79

l.  Generation of 5mS square wave using Timer0 in Interrupt Mode 84

m.  Generation of 5mS square wave using Timer1 in Interrupt Mode 87

n.  Generation of 5mS square wave using Timer2 in Interrupt Mode 93

o.  Continuous Transmission of a character using USART in Asynchronous

 p.  Mode of operation 98

q.  Reception and Transmission of the same character using USART in

Asynchronous Mode of operation 105r.  Analog to Digital Conversion 109

s.  Analog to Digital Conversion in Interrupt Mode 113

t.  Generation of PWM 116

u.  EEPROM read and write 121

v.  Compare mode of operation of CCP 125

Page 3: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 3/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 1 of 129 

8085 SIMULATOR

1)  Click on 8085 Simulator icon from Desktop.

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.

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

Register window

Assembler windowFlag Register window

Memory Editor window

Simulator window

Memory Range Text Box

Page 4: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 4/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 2 of 129 

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.

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

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

Memory Range Text Box

Assembler window

Assemble button

Page 5: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 5/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 3 of 129 

The program will be assembled and the corresponding Opcodes along with its address will be

displayed below the Assembler window.

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 an’X’ mark on left of that particular

address where error is present.

This error should be corrected and click on Assemble again.

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

Program with

Opcode & Address

Error ie ‘X’ Mark

Error Messa e with Line No.

Page 6: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 6/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 4 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.

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.

Start From text box

Step by Step button

Forward button

Highlight

Page 7: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 7/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 5 of 129 

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. 

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.

 Next step is that the output should be checked.

Accumulator Value = 01

Last line Highlighted

Page 8: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 8/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 6 of 129 

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

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

Show only loaded memory location radio button

Page 9: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 9/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 7 of 129 

MPLAB IDE

Project Creation

1)  Open MPLAB IDE 7.52 icon from the desktop.

2)  Click on Project Tab from Menubar.

3)  Click on Project Wizard.

Welcome screen will appear.

Toolbar

Menubar

Status bar

Page 10: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 10/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 8 of 129 

4)  Click on Next button in the welcome screen.

5)  Select Device as PIC16F877A from 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.

Device Drop down menu

Page 11: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 11/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 9 of 129 

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 12: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 12/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 10 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.

10) 

 Name the folder as MP_Lab.

11) Type the Project Name as PIC_Project.

12) Click on Save.

Project Name

Page 13: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 13/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 11 of 129 

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

14) Click on Next.

15) Again Click on Next.

Project Path

Page 14: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 14/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 12 of 129 

16) Click on Finish.

17) 

Project Explorer window will be displayed.

Project Explorer Window

Page 15: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 15/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 13 of 129 

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

19) Type the whole assembly language program.

20) Click on File -> Save As.

New File

Page 16: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 16/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 14 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 17: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 17/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 15 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.

2)  Open the Project Folder in E Drive ie MP_Lab in Look In drop down menu.

3)  Select Add.asm. Click on Open.

Source Files

Page 18: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 18/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 16 of 129 

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

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

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

window.

Add.asm

Build All

Build Succeeded

Page 19: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 19/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 17 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.

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

Build Failed

Instruction with Error

Page 20: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 20/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 18 of 129 

Debug and Run the Project

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

MPLAB Simulator Debug Toolbar will be displayed.

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.

Debug Toolbar

Page 21: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 21/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 19 of 129 

Watch window will be displayed.

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

4)  Click on Add Symbol button.

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

Symbol Drop down menuAdd Symbol button

Page 22: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 22/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 20 of 129 

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 23: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 23/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 21 of 129 

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 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.

Green Arrow

Page 24: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 24/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 22 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.

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.

Red Colour

Page 25: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 25/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 23 of 129 

PROTEUS 7 PROFESSIONAL

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

ISIS 7 Professional

Page 26: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 26/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 24 of 129 

ISIS screen will be displayed.

2) 

Click on Component Mode icon in the Toolbar. 

Works aceDevice List

Toolbar

ToolbarMenubar

Component Mode

Page 27: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 27/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 25 of 129 

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

Pick Devices window will be displayed.

Pick from Libraries

Pick Devices window

Page 28: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 28/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 26 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. 

PIC16F877A

Result Space

Keywords

Text Box

Page 29: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 29/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 27 of 129 

6)  Double click on PIC16F877A in the Result Space.

PIC16F877A will be added to the Devices List in Proteus.

Double Click Here

PIC16F877A

Device List

Page 30: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 30/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 28 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. 

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. 

Keywords

Text Box Double Click Here

Page 31: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 31/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 29 of 129 

11) Press Enter key.

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

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

Page 32: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 32/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 30 of 129 

 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 33: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 33/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 31 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 34: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 34/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 32 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.

Single Click Here

Single Click Here

Single Click Here

Page 35: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 35/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 33 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).

Green Square (Viewable Area)

Black Square (PIC)

Viewable Area

Viewable Area

Page 36: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 36/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 34 of 129 

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

Page 37: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 37/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 35 of 129 

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.  

As the next step add Ground Terminal to the workspace.

Single Click HereDouble Click Here

Page 38: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 38/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 36 of 129 

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

23) Select Ground from Terminals List. 

Terminals Mode

Ground

Page 39: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 39/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 37 of 129 

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

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.

Double Click Here

Red Square

Page 40: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 40/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 38 of 129 

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

27) 

Click at the ending point.

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

Ending Point

Single ClickStarting Point

Single Click

Page 41: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 41/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 39 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 42: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 42/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 40 of 129 

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

Right Click

Edit Properties

Page 43: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 43/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 41 of 129 

Edit Component window will be displayed.

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

Browse

Page 44: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 44/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 42 of 129 

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 45: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 45/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 43 of 129 

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

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.

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

Path

4MHz

Page 46: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 46/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 44 of 129 

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

The Led connected to RB0 will start Blinking

Play Button

Page 47: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 47/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 45 of 129 

Adding Virtual Instruments to Proteus Workspace

1)  Click on Virtual Instruments Mode in Proteus Toolbar.

Virtual Instruments List will be displayed.

Virtual Instruments

Mode

Page 48: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 48/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 46 of 129 

2)  Click on Oscilloscope in Virtual Instruments List. 

3)  Add Oscilloscope to Workspace. 

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

Oscilloscope

Page 49: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 49/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 47 of 129 

Oscilloscope window will be displayed.

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

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

Cursors button

Cursor

Page 50: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 50/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 48 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 1Cursor 2

Page 51: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 51/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 49 of 129 

8085 ARCHITECTURE

Page 52: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 52/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 50 of 129 

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 53: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 53/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 51 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 54: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 54/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 52 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

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 55: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 55/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 53 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 56: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 56/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 54 of 129 

8085 PROGRAMS

Addition of two 8 bit numbers

Aim

To perform addition of two 8 bit numbers using 8085.

Algorithm

1)  Start the program by loading the first data into Accumulator.

2)  Move the data to a register (B register).3)  Get the second data and load into Accumulator.

4)  Add the two register contents.5)  Check for carry.

6)  Store the value of sum and carry in memory location.

7) 

Terminate the program.

Program

MVI C, 00 Initialize C register to 00

LDA 4150 Load the value to Accumulator.MOV B, A Move the content of Accumulator to B register.

LDA 4151 Load the value to Accumulator.ADD B Add the value of register B to A

JNC LOOP Jump on no carry.INR C Increment value of register C

LOOP:STA 4152 Store the value of Accumulator (SUM).

MOV A, C Move content of register C to Acc.STA 4153 Store the value of Accumulator (CARRY)

HLT

Output

Address Data

Input4150 FF

4151 FF

Output4152 FE4153 01

Page 57: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 57/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 55 of 129 

Subtraction of two 8 bit numbers

Aim

To perform the subtraction of two 8 bit numbers using 8085.

Algorithm

1)  Start the program by loading the first data into Accumulator.2)  Move the data to a register (B register).

3)  Get the second data and load into Accumulator.4)  Subtract the two register contents.

5)  Check for carry.6)  If carry is present take 2’s complement of Accumulator.

7)  Store the value of borrow in memory location.

8) 

Store the difference value (present in Accumulator) to a memory location and terminatethe program.

Program

MVI C, 00 Initialize C register to 00LDA 4150 Load the value to Accumulator.

MOV B, A Move the content of Accumulator to B register.LDA 4151 Load the value to Accumulator.

SUB B Subtract the value of register B from AJNC LOOP Jump on no carry.

CMA Complement Accumulator contents.INR A Increment value of register A

INR C Increment value of register CLOOP:

STA 4152 Store the value of Accumulator (SUM).MOV A, C Move content of register C to Acc.

STA 4153 Store the value of Accumulator (CARRY)HLT

Output

Address Data

Input4150 05

4151 03

Output4152 02

4153 01

Page 58: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 58/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 56 of 129 

Multiplication of two 8 bit numbers

Aim

To perform multiplication of two 8 bit numbers using 8085.

Algorithm

1)  Start the program by loading HL register pair with address of memory location.

2)  Move the data to a register (B register).3)  Get the second data and load into Accumulator.

4)  Add the two register contents.5)  Check for carry.

6)  Increment the value of carry.7)  Check whether repeated addition is over and store the value of product and carry in

memory location.8)  Terminate the program.

Program

MVI D, 00 Initialize register D to 00

MVI A,00 Initialize Accumulator content to 00LXI H,4150

MOV B,M Get the first number in B - regINX H

MOV C,M Get the second number in C- reg

LOOP:ADD B Add content of A to register B.JNC NEXT Jump on no carry to NEXT.

INR D Increment content of register D NEXT:

DCR C Decrement content of register C.JNZ LOOP Jump on no zero to address

STA 4152 Store the result in MemoryMOV A,D

STA 4153 Store the MSB of result in MemoryHLT

Output

Address Data

Input4150 FF

4151 02

Output4152 FE

4153 01

Page 59: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 59/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 57 of 129 

Division of two 8 bit numbers

Aim

To perform the division of two 8 bit numbers using 8085.

Algorithm

1)  Start the program by loading HL register pair with address of memory location.2)  Move the data to a register (B register).

3)  Get the second data and load into Accumulator.4)  Compare the two numbers to check for carry.

5)  Subtract the two numbers.6)  Increment the value of carry.

7)  Check whether repeated subtraction is over and store the value of product and carry in

memory location.8)  Terminate the program

Program

LXI H,4150MOV B,M Get the dividend in B – reg.

MVI C,00 Clear C – reg for qoutientINX H

MOV A,M Get the divisor in A – reg. NEXT:

CMP B Compare A - reg with register B.JC LOOP Jump on carry to LOOP

SUB B Subtract A – reg from B- reg.INR C Increment content of register C.

JMP NEXT Jump to NEXTLOOP:

STA 4152 Store the remainder in MemoryMOV A,CSTA 4153 Store the quotient in memory

HLT

Output

Address Data

Input4150 02

4151 05

Output4152 01

4153 02

Page 60: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 60/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 58 of 129 

PIC16F877A ARCHITECTURE

Page 61: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 61/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 59 of 129 

PIC16F877A PERIPHERALS

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

 

Timer1: 16-bit timer/counter with prescaler, can be incremented during SLEEP viaexternal 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 62: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 62/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 60 of 129 

INSTRUCTION SET SUMMARY

Page 63: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 63/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 61 of 129 

MPLAB PROGRAMS

Addition of two 8 bits numbers

AimTo add two 8 bit numbers and store the result

Algorithm

1)  Move first number to W Register.

2)  Move content of W Register to 0x20 (FIRSTNO).3)  Move second number to W Register.

4)  Move content of W Register to 0x21 (SECNO).5)  Add content of W Register (second No.) with content of 0x20 (first No.).

6) 

Move the result to 0x22 (SUM).

Program

#INCLUDE<P16F877A.INC>FIRSTNO EQU 0X20

SECNO EQU 0X21SUM EQU 0X22

ORG 0X00MOVLW 0X01

MOVWF FIRSTNO

MOVLW 0X02MOVWF SECNOADDWF FIRSTNO,0

MOVWF SUML1

GOTO L1END

Output

Two 8 bit numbers will be added and the result will be stored in 0x22.

Page 64: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 64/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 62 of 129 

Addition of two 8 bit numbers with carry

AimTo add two 8 bit numbers and store the result along with carry.

Algorithm

1) 

Move first number to W Register.

2)  Move content of W Register to 0x20 (NUMBER_1).3)  Move second number to W Register.

4)  Move content of W Register to 0x21 (NUMBER_2).5)  Add content of W Register (second No.) with content of 0x20 (first No.).

6)  Move the result to 0x22 (SUM).7)  If Carry is there, Increment 0x23 (CARRY).

8)  Else goto step 9.

9) 

End

Program

#INCLUDE<P16F877A.INC>

 NUMBER_1 EQU 0x20 ; Define a Symbol Name NUMBER_1 to an Address 0x30 NUMBER_2 EQU 0X21

SUM EQU 0X22CARRY EQU 0X23

ORG 0X00 ; Shows the Origin of the ProgramGOTO START

ORG 0X05 ; Bypassing Interrupt Vector AddressSTART

MOVLW 0XA2 ; Move First Number to W

MOVWF NUMBER_1 ; Move First Number NUMBER_1 (0x20)MOVLW 0X62

MOVWF NUMBER_2ADDWF NUMBER_1, 0 ; W = NUMBER_1 + W (Second No.)MOVWF SUM ; Move Result from W to SUM

BTFSC STATUS, C ; Check for Carry FlagINCF CARRY, 1 ; If Carry is there, increment Carry Register.

L1GOTO 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 in0x23.

Page 65: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 65/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 63 of 129 

Subtraction of two 8 bit numbers with borrow

Aim

To subtract two 8 bit numbers and store the result along with borrow.

Algorithm

1)  Move first number to W Register.2)  Move content of W Register to 0x20 (NUMBER_1).

3)  Move second number to W Register.4)  Move content of W Register to 0x21 (NUMBER_2).

5)  Subtract content of 0x20 (first No.) with content of W Register (second No.).6)  Move the result to 0x22 (DIFF).

7)  If Borrow is there, Increment 0x23 (BORROW).8)  Else goto step 9.

9) 

End

Program

#INCLUDE<P16F877A.INC>

 NUMBER_1 EQU 0X20 NUMBER_2 EQU 0X21

DIFF EQU 0X22BORROW EQU 0X23

ORG 0X00

GOTO MAIN

ORG 0X05MAIN

MOVLW 0X62MOVWF NUMBER_1

MOVLW 0XA2MOVWF NUMBER_2

SUBWF NUMBER_1, 0 ; WREG = NUMBER_1 - WREGMOVWF DIFF ; DIFF = WREG

BTFSS STATUS, C ; If borrow is not there, goto end.

CALL COMPLIMENT ; If Borrow present, goto 2’s COMPLIMENT function.GOTO L1

COMPLIMENTCOMF DIFF, 1 ; DIFF = one's compliment of DIFF

INCF DIFF, 1 ; DIFF = DIFF + 1INCF BORROW, 1 ; BORROW = BORROW + 1

RETURN

Page 66: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 66/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 64 of 129 

L1GOTO L1

END

Output

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

Multiplication of two 8 bit numbers

Aim

To multiply two 8 bit numbers and store the result.

Algorithm

1) 

Clear 0x23 (Product MSB) & 0x24 (Product LSB).2)  Move first number to MULTIPLIER & COUNT.3)  Move second number to MULTIPLICANT.

4)  If Multiplier is zero, goto end.5)  Else if Multiplicant is Zero, goto end.

6)  Add Multiplicant (second number) with PRODUCT LSB (initially zero) and store theresult 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 0X00GOTO START

COUNT EQU 0X20

MULTIPLIER EQU 0X21MULTIPLICANT EQU 0X22

PRODUCT_MSB EQU 0X23

PRODUCT_LSB EQU 0X24

ORG 0X05

START

CLRF PRODUCT_MSB ; Clear the PRODUCT_MSBCLRF PRODUCT_LSB ; Clear the PRODUCT_LSB

Page 67: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 67/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 65 of 129 

MOVLW 0X03MOVWF COUNT ; COUNT = WREG

MOVWF MULTIPLIER ; MULTIPLIER = WREGMOVLW 0XFF ; WREG = 0XFF

MOVWF MULTIPLICANT ; MULTIPLICANT = WREG

MOVF MULTIPLIER,0 ; WREG = MULTIPLIERBTFSC STATUS,Z ; Check Multiplier is ZeroGOTO TERMINATE ; If Zero, TERMINATE

MOVF MULTIPLICANT,0 ; WREG = MULTIPLICANTBTFSC STATUS,Z ; Check MULTIPLICANT is Zero

GOTO TERMINATE ; If Zero, TERMINATE

L2ADDWF PRODUCT_LSB,1 ; PRODUCT_LSB = PRODUCT_LSB + WREG

BTFSC STATUS,C ; Check for CarryINCF PRODUCT_MSB,1 ; PRODUCT_MSB = PRODUCT_MSB + 1

DECFSZ COUNT,1 ; Decrement COUNT, check for ZeroGOTO L2 ; If not zero, GOTO L2

TERMINATE

GOTO TERMINATEEND

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)  Clear 0x22 (Quotient) & 0x23 (Remainder).2)  Move first number to DIVIDENT & REMAINDER.

3)  Move second number to DIVISOR.

4) 

If Divisor is zero (Division by Zero), set Quotient as 0xff and Remainder as 0x00.5)  Subtract WREG content (second number ie Divisor) from REMAINDER (initiallyDivident) and store the result in REMAINDER itself.

6)  If Borrow is there (Remainder < 0), add Divisor with Remainder and store it inREMAINDER itself, then goto end.

7)  Else Increment QUOTIENT.8)  Goto step 5.

Page 68: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 68/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 66 of 129 

Program

#INCLUDE<P16F877A.INC>DIVIDENT EQU 0X20

DIVISOR EQU 0X21

QUOTIENT EQU 0X22REMINDER EQU 0X23

ORG 0X00GOTO START

ORG 0X05

START

CLRF QUOTIENT ; Clear QUOTIENTCLRF REMINDER ; Clear REMINDER

MOVLW 0X05 ; WREG = 0X05MOVWF DIVIDENT ; DIVIDENT = WREG

MOVWF REMINDER ; REMINDER = WREGMOVLW 0X07 ; WREG = 0X07

MOVWF DIVISOR ; DIVISOR = WREGMOVF DIVISOR,0 ; WREG = DIVISOR

BTFSC STATUS,Z ; Check Divisor is zero.GOTO L1 ; If zero, goto L1

L2:SUBWF REMINDER,1 ; REMINDER = REMINDER - WREG

BTFSS STATUS,C ; Check for borrow.GOTO L3 ; If Borrow is there (Remainder < 0), goto L3

INCF QUOTIENT,1 ; Increment QUOTIENTGOTO L2

L3:ADDWF REMINDER, 1 ; REMINDER = REMINDER + WREG

GOTO TERMINATEL1:

MOVLW 0XFF ; WREG = 0XFFMOVWF QUOTIENT ; QUOTIENT = WREG

MOVWF REMINDER ; REMINDER = WREG

TERMINATE:GOTO TERMINATE

END ; Physical end of program

Output

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

Page 69: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 69/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 67 of 129 

Addition of 5 numbers using Indirect Addressing Mode

AimTo 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 0X31CARRY EQU 0X32

ORG 0X00GOTO START

ORG 0X05START

MOVLW 0XFFMOVWF 0X20

MOVLW 0XFFMOVWF 0X21

MOVLW 0XFFMOVWF 0X22MOVLW 0XFF

MOVWF 0X23MOVLW 0XFF

MOVWF 0X24

MOVLW 0X04MOVWF COUNT

MOVLW 0X20MOVWF FSR

MOVF INDF,0

Page 70: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 70/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 68 of 129 

L1INCF FSR

ADDWF INDF,0BTFSC STATUS,C

INCF CARRY,1

DECFSZ COUNT,1GOTO L1MOVWF SUM

L2GOTO L2

END

Blinking 8 LEDs connected to a PORT

Aim

To Blink 8 LEDs connected to PORTB.

Circuit Diagram

Algorithm

1)  Configure PORTB as output Port.

2)  Make all LEDs ON.3)  Delay.

4)  Make all LEDs OFF.5)  Delay.

6)  Goto Step 2.

Page 71: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 71/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 69 of 129 

Registers associated with PORTB

Program 

#INCLUDE<P16F877A.INC>ORG 0X00

GOTO STARTCOUNT EQU 0X20

COUNT1 EQU 0X21ORG 0X05

STARTBANKSEL TRISB

MOVLW 0X00MOVWF TRISB ; Configure PORTB Direction as Output

BANKSEL PORTBCLRF PORTB

LOOP

BANKSEL PORTBMOVLW 0XFFMOVWF PORTB ; 8 LEDs ON

CALL DELAYCALL DELAY

MOVLW 0X00MOVWF PORTB ; 8 LEDs OFF

CALL DELAYCALL DELAY

GOTO LOOPDELAY

MOVLW 0XFFMOVWF COUNT

L1MOVLW 0XFF

MOVWF COUNT1L2

DECFSZ COUNT1GOTO L2

Page 72: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 72/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 70 of 129 

DECFSZ COUNTGOTO L1

RETURN

END

Output

LEDs connected to PORTB will start blinking.

Page 73: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 73/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 71 of 129 

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)  Configure PORTC as Output.2)  Configure RB1 as input

3)  Configure Weak Internal Pull-ups for PORTB.4)  If switch is pressed (RB1 = 0), make LED ON (RC2 = 1).

5)  Else make LED OFF (RC2 = 0).6)  Goto step 4.

Registers associated with PORTB

Page 74: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 74/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 72 of 129 

Program

#INCLUDE<P16F877A.INC>

ORG 0X00GOTO START

START

BSF STATUS,RP0BCF STATUS,RP1

CLRF TRISC

BSF TRISB,1BCF OPTION_REG,NOT_RBPU ; Enable weak Internal Pull Ups for PORTBBCF STATUS,RP0

CLRF PORTCLOOP

BTFSS PORTB,1 ; Check whether switch is PressedGOTO L2

GOTO L3

L2

BSF PORTC,2 ; LED ONGOTO LOOP

L3

BCF PORTC,2 ; LED OFFGOTO LOOP

END

Page 75: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 75/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 73 of 129 

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 76: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 76/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 74 of 129 

Algorithm

Main

1)  Configure RB0/INT0 as input.

2) 

Configure PORTC as output.3) 

Configure Weak Internal Pull-ups for PORTB.4)  Configure Interrupt on Falling Edge.

5)  Enable INT0.6)  Enable Global Interrupt Enable.

7)  If switch is pressed (RB0 = 0), goto step 7.8)  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 0X00GOTO START

ORG 0X04GOTO ISR

Page 77: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 77/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 75 of 129 

STARTBANKSEL TRISB

BSF TRISB,0CLRF TRISC

BCF OPTION_REG,NOT_RBPU

BCF OPTION_REG,INTEDG ; Configure Interrupt Edge as Falling EdgeBANKSEL PORTBCLRF PORTC

BANKSEL TRISBBSF INTCON,INTE ; Enable INT0 Interrupt

BSF INTCON,GIE ; Enable Global InterruptBCF INTCON,INTF

L1BANKSEL PORTB

BTFSC PORTB,0BCF PORTC,0

GOTO L1ISR

BANKSEL INTCONBCF INTCON,GIE

BTFSS INTCON,INTF ; Check INT0 Interrupt FlagRETFIE

BCF INTCON,INTFBANKSEL PORTB

BSF PORTC,0RETFIE ; 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 78: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 78/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 76 of 129 

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)  Configure RB4 as input.

2)  Configure PORTC as output.3)  Configure Weak Internal Pull-ups for PORTB.

4)  Enable PORTB Pin Change Interrupt.5)  Enable Global Interrupt Enable (GIE).

6)  If switch is pressed (RB4 = 0), goto step 7.7)  Else make LED OFF (RC0 = 0).

ISR

1)  Disable GIE.

2)  If PORTB Pin Change Interrupt Flag Bit is set (RBIF),Clear RBIF.3)  Else return to main program.

4)  If switch is pressed (RB4 = 0), Make LED ON (RC0 = 1).5)

 

Else return to main program.

Page 79: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 79/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 77 of 129 

Register description of INTCON Register

Program

#INCLUDE<P16F877A.INC>

ORG 0X00GOTO START

ORG 0X04

GOTO ISRSTART

BANKSEL TRISB

BSF TRISB,4CLRF TRISC

BANKSEL OPTION_REGBCF OPTION_REG,NOT_RBPU

BANKSEL PORTCCLRF PORTC

BANKSEL INTCONBSF INTCON,RBIE ; Enable PORTB Pin Change Interrupt

BSF INTCON,GIEBCF INTCON,RBIF

L1BANKSEL PORTB

BTFSC PORTB,4BCF PORTC,0

GOTO L1

Page 80: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 80/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 78 of 129 

ISRBANKSEL INTCON

BSF INTCON,GIEBTFSS INTCON,RBIF ; Check PORTB Pin Change Interrupt Flag

RETFIE

BCF INTCON,RBIFBTFSS PORTB,4 ; Check whether switch is pressedBSF PORTC,0

RETFIEEND

Output

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

Page 81: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 81/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 79 of 129 

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 82: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 82/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 80 of 129 

Registers associated with Timer0

Register Description of OPTION_REG Register

Page 83: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 83/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 81 of 129 

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. If4MHz 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 84: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 84/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 82 of 129 

Program

#INCLUDE<P16F877A.INC>ORG 0X00

GOTO START

STARTBSF STATUS,RP0BCF STATUS,RP1

CLRF TRISCBCF STATUS,RP0

CLRF PORTC

BSF STATUS,RP0BCF OPTION_REG,T0CS ; Select Timer Mode of operation for Timer0

BCF OPTION_REG,PSA ; Assign Prescalar to Timer0BSF OPTION_REG,PS2 ; Set Prescalar as 1:32

BCF OPTION_REG,PS1BCF OPTION_REG,PS0

BCF STATUS,RP0

MOVLW 0X63MOVWF TMR0 ; Initialize TMR0 to generate 5mS delay with Timer0

L1

BTFSS INTCON,T0IF ; Check Timer0 Interrupt FlagGOTO L1

BCF INTCON,T0IF

COMF PORTC,1 ; Complement PORTC

MOVLW 0X63 ; Re-initialize TMR0

MOVWF TMR0

GOTO L1

END

Page 85: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 85/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 83 of 129 

Output

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

Page 86: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 86/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 84 of 129 

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 InterruptMode of operation.

Circuit

Algorithm

Main1)  Configure PORTC as output.

2)  Configure Timer0 in Timer Mode.3)  Assign Prescalar to Timer0.

4)  Configure Prescalar as 32.5)  Enable Timer0 Interrupt Enable.

6)  Enable GIE.7)  Initialize Timer0 register with 0x63.

8)  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 87: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 87/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 85 of 129 

Register description of INTCON Register

Program

#INCLUDE<P16F877A.INC>

ORG 0X00GOTO START

ORG 0X04GOTO ISR

STARTBANKSEL TRISC

CLRF TRISCBANKSEL PORTC

CLRF PORTC

BANKSEL OPTION_REGBCF OPTION_REG,T0CSBCF OPTION_REG,PSA

BSF OPTION_REG,PS2BCF OPTION_REG,PS1

BCF OPTION_REG,PS0

BANKSEL INTCONBSF INTCON,T0IE ; Enable Timer0 Interrupt

BSF INTCON,GIE

BANKSEL TMR0MOVLW 0X63MOVWF TMR0

L1

GOTO L1ISR

BCF INTCON,GIE

Page 88: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 88/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 86 of 129 

BTFSS INTCON,T0IF ; Check Timer0 Interrupt FlagRETFIE

BCF INTCON,T0IFBANKSEL PORTC

COMF PORTC,1

BANKSEL TMR0MOVLW 0X63MOVWF TMR0

RETFIEEND

Output

Square wave on 5mS ON & OFF Time will be generated in PORTC which can be

verified from the Oscilloscope output.

Page 89: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 89/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 87 of 129 

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 InterruptMode of operation.

Circuit

Algorithm

Main1)  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.

ISR1)  Disable GIE.

2)  If Timer1 Interrupt Flag Bit is set (TMR1IF),a.  Clear TMR1IF.

 b.  Compliment PORTC.c.  Re-initialize TMR1L with 0x77.

Page 90: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 90/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 88 of 129 

d.  Re-initialize TMRIH with 0xEC.3)  Else return to main program.

Registers associated with Timer1

Register description of T1CON Register

Page 91: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 91/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 89 of 129 

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 92: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 92/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 90 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,

 TMR1H:TMR1L = 65535− ()

 

= 65535 – (5000/1)

= 60535 = 0 x EC 77

TMR1H = 0xEC TMR1L = 0x77

Program

#INCLUDE<P16F877A.INC>

ORG 0X00GOTO START

ORG 0X04GOTO ISR

ORG 0X05START

BANKSEL TRISCCLRF TRISC

BANKSEL T1CONBCF T1CON,TMR1CS ; Select Timer Mode of Operation for Timer1

BCF T1CON,T1CKPS1 ; Set Timer1 Prescalar as 1:1BCF T1CON,T1CKPS0

Page 93: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 93/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 91 of 129 

BANKSEL TMR1LMOVLW 0X77

MOVWF TMR1L ; Initialize TMR1L with 0x77BANKSEL TMR1H

MOVLW 0XEC

MOVWF TMR1H ; Initialize TMR1H with 0xECBANKSEL PIE1BSF PIE1,TMR1IE ; Enable Timer1 Interrupt

BSF INTCON,PEIE ; Enable Peripheral InterruptsBSF INTCON,GIE

BANKSEL T1CONBSF T1CON,TMR1ON ; Switch on Timer1

L1GOTO L1

ISRBCF INTCON,GIE

BANKSEL PIR1BTFSS PIR1,TMR1IF ; Check Timer1 Interrupt Flag

RETFIEBCF PIR1,TMR1IF

BANKSEL PORTCCOMF PORTC,1

BANKSEL TMR1LMOVLW 0X77

MOVWF TMR1LBANKSEL TMR1H

MOVLW 0XECMOVWF TMR1H

RETFIE

END

Page 94: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 94/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 92 of 129 

Output

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

Page 95: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 95/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 93 of 129 

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 InterruptMode of operation.

Circuit

Algorithm

Main1)  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 96: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 96/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 94 of 129 

Registers associated with Timer2

Register description of T2CON Register

Register description of PIE1 Register

Page 97: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 97/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 95 of 129 

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 98: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 98/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 96 of 129 

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

PR2=  ()

∗  

= (5000/(16 * 2))

= 156 = 0x9C

Program

#INCLUDE<P16F877A.INC>ORG 0X00

GOTO STARTORG 0X04

GOTO ISRORG 0X05

START

BANKSEL TRISCCLRF TRISCBANKSEL T2CON

BSF T2CON,T2CKPS1 ; Set Prescalar as 1:16BSF T2CON,T2CKPS0

BCF T2CON,TOUTPS3 ; Set Postscalar as 1:2BCF T2CON,TOUTPS2

BCF T2CON,TOUTPS1BSF T2CON,TOUTPS0

BANKSEL PR2MOVLW 0X9C

MOVWF PR2 ; Set final value of Timer2 with 0x9CBANKSEL TMR2

CLRF TMR2BANKSEL PIE1

BSF PIE1,TMR2IE ; Enable Timer2 InterruptBSF INTCON,PEIE

BSF INTCON,GIEBANKSEL T2CON

BSF T2CON,TMR2ON ; Switch ON Timer2L1

GOTO L1

ISRBCF INTCON,GIEBANKSEL PIR1

BTFSS PIR1,TMR2IF ; Check Timer2 Interrupt FlagRETFIE

BCF PIR1,TMR2IFBANKSEL PORTC

COMF PORTC,1

Page 99: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 99/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 97 of 129 

BANKSEL TMR2CLRF TMR2

RETFIE

END

Output

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

Page 100: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 100/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 98 of 129 

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 Modeof Operation at 9600bps.

Circuit

Algorithm

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)  Configure RC6 & RC7 as TX & RX pin of USART (Set SPEN =1).6)  Wait until Transmission Interrupt Flag is set (TXIF).

7)  Move ‘A’ to Transmit Register.8)  Goto step 6.

Page 101: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 101/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 99 of 129 

Register associated with Asynchronous Transmitter

Register description of TXSTA Register

Page 102: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 102/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 100 of 129 

Register description of RCSTA Register

Register description of PIE1 Register

Register description of PIR1 Register

Page 103: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 103/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 101 of 129 

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 104: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 104/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 102 of 129 

Page 105: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 105/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 103 of 129 

Program

#INCLUDE<P16F877A.INC>ORG 0X00

GOTO START

ORG 0X05STARTBANKSEL TXSTA

BSF TXSTA,BRGH ; Set High Speed TransmissionBANKSEL SPBRG

MOVLW .25MOVWF SPBRG ; Set Baud Rate as 9600bps

BANKSEL TXSTABCF TXSTA,SYNC ; Set Asynchronous Transmission

BSF TXSTA,TXEN ; Enable TransmissionBANKSEL RCSTA

BSF RCSTA,SPEN ; Configure RC6 & RC7 as USART PinsLOOP

BANKSEL PIR1BTFSS PIR1,TXIF ; Check Transmission Interrupt Flag

GOTO LOOPBANKSEL TXREG

MOVLW 'A'MOVWF TXREG ; Transmit ‘A’

GOTO LOOPEND

Output

The character ‘A’ will be continuously transmitted from PIC to PC which can be seen on

Virtual Terminal window of Proteus.

Page 106: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 106/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 104 of 129 

Note: For adding Virtual Terminal  to Workspace  click on Virtual Instruments Mode  in

Proteus Toolbar. Then Select Virtual Instruments from Virtual Instruments List.

Page 107: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 107/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 105 of 129 

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 backto 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 108: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 108/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 106 of 129 

Register associated with Asynchronous Transmitter

Register description of RCSTA Register

Page 109: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 109/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 107 of 129 

Register description of PIE1 Register

Register description of PIR1 Register

Program

PROCESSOR P16F877#INCLUDE<P16F877.INC>ORG 0X00

GOTO STARTORG 0X05

STARTBANKSEL TXSTA

BSF TXSTA,BRGH

Page 110: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 110/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 108 of 129 

BANKSEL SPBRGMOVLW .25

MOVWF SPBRGBANKSEL TXSTA

BCF TXSTA,SYNC

BSF TXSTA,TXENBANKSEL RCSTABSF RCSTA,CREN ; Enable Continous Receive

BSF RCSTA,SPENLOOP

BANKSEL PIR1BTFSS PIR1,RCIF ; Check Receive Interrupt Flag

GOTO LOOPBANKSEL RCREG

MOVF RCREG,0 ; Move received character to WREGBANKSEL PIR1

LOOP1BTFSS PIR1,TXIF ; Check Transmit Interrupt Flag

GOTO LOOP1BANKSEL TXREG

MOVWF TXREG ; Transmit received character to PCGOTO LOOP

END

Output

The characters which have been entered through the PC’s keyboard, will be received byPIC 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 VirtualTerminal window.

Page 111: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 111/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 109 of 129 

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 112: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 112/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 110 of 129 

Registers associated with ADC

Register description for ADCON0 Register

Page 113: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 113/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 111 of 129 

Register description for ADCON1 Register

Program

PROCESSOR P16F877#INCLUDE<P16F877.INC>

ORG 0X00MOVLW 0X00

MOVWF PCLATHCLRF STATUS

GOTO STARTSTART

BANKSEL TRISABSF TRISA,0 ; Configure RA0 as input

BANKSEL TRISB

Page 114: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 114/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 112 of 129 

CLRF TRISBBANKSEL PORTB

CLRF PORTBBANKSEL ADCON1

CLRF ADCON1

BANKSEL ADCON0BSF ADCON0,ADCS1 ; Configure ADC Clock as Fosc/32BCF ADCON0,ADCS0

BCF ADCON0,CHS2 ; Select ADC input channel as Channel 0BCF ADCON0,CHS1

BCF ADCON0,CHS0BANKSEL ADCON1

BCF ADCON1,ADFM ; Select ADC Result Format as Left JustifiedBANKSEL ADCON0

BSF ADCON0,ADON ; Switch ON ADCBSF ADCON0,GO_DONE ; Start ADC Conversion

LOOP BANKSEL ADCON0

BSF ADCON0,GO_DONEL1 BTFSC ADCON0,GO_DONE ; Check for End of Conversion of ADC

GOTO L1BANKSEL ADRESH

MOVF ADRESH,0 ; Move MSB 8 bit of ADC Result to WREGMOVWF PORTB ; Move ADC Result to PORTB

GOTO LOOPEND

Output

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

LEDs.

Page 115: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 115/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 113 of 129 

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 116: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 116/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 114 of 129 

Register description of PIR1 Register

Program

PROCESSOR P16F877#INCLUDE<P16F877.INC>

ORG 0X00GOTO START

ORG 0X04GOTO ISR

STARTBANKSEL TRISA

BSF TRISA,0BANKSEL TRISB

CLRF TRISBBANKSEL PORTB

CLRF PORTBBANKSEL ADCON1

CLRF ADCON1BANKSEL ADCON0

BSF ADCON0,ADCS1BSF ADCON0,ADCS0

BCF ADCON0,CHS2BCF ADCON0,CHS1

BCF ADCON0,CHS0BANKSEL ADCON1

BCF ADCON1,ADFM

BANKSEL PIE1BSF PIE1,ADIE ; Enable ADC Interrupt

BANKSEL INTCONBSF INTCON,PEIE

BSF INTCON,GIEBANKSEL ADCON0

BSF ADCON0,ADONBSF ADCON0,GO_DONE

Page 117: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 117/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 115 of 129 

L1 NOP

 NOP NOP

BSF ADCON0,GO_DONE

GOTO L1

ISRBSF INTCON,GIE

BANKSEL PIR1BTFSS PIR1,ADIF ; Check for ADC Interrupt Flag

RETFIEBCF PIR1,ADIF

BANKSEL ADRESH

MOVF ADRESH,0MOVWF PORTB

RETFIEEND

Output

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

LEDs.

Page 118: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 118/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 116 of 129 

Generation of PWM

Aim

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

Circuit

Registers associated with PWM Operation

Page 119: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 119/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 117 of 129 

Register description for CCPxCON Register

Register description of PIE1 Register

Page 120: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 120/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 118 of 129 

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 121: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 121/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 119 of 129 

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

Program

#INCLUDE<P16F877A.INC>ORG 0X00

GOTO STARTORG 0X05

STARTBANKSEL TRISC

BCF TRISC,2

On Time

Page 122: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 122/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 120 of 129 

BANKSEL T2CONCLRF T2CON

BCF T2CON,T2CKPS1 ; Set Prescalar as 1:4BSF T2CON,T2CKPS0

BANKSEL PR2

MOVLW 0XF9MOVWF PR2 ; Initialize PWM Period as 0xF9BANKSEL CCPR1L

MOVLW 0XC8MOVWF CCPR1L ; Initialize 8 bits of On Time as 0xC8

BANKSEL CCP1CONBCF CCP1CON,CCP1X ; Initialize 2 bit LSBs of On Time as 00

BCF CCP1CON,CCP1YMOVLW 0X0F

MOVWF CCP1CONBANKSEL T2CON

BSF T2CON,TMR2ON ; Switch ON Timer2L1

GOTO L1END

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 123: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 123/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 121 of 129 

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 124: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 124/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 122 of 129 

Register description for EECON1 Register

Register description of PIE2 Register

Register description of PIR2 Register

Page 125: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 125/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 123 of 129 

Programs

#INCLUDE<P16F877A.INC>ORG 0X00GOTO START

ORG 0X05STARTBANKSEL TRISBCLRF TRISBBANKSEL PORTBCLRF PORTBBANKSEL EECON1BTFSC EECON1, WR ; Wait for write to finishGOTO $-1BANKSEL EEADRMOVLW 0X01MOVWF EEADR ; Write Address (0x01) to EEADR RegisterBANKSEL EEDATAMOVLW 0X06MOVWF EEDATA ; Write Data (0x06) to EEDATA RegisterBANKSEL EECON1BCF EECON1, EEPGD ; Set EEPROM as Data memoryBSF EECON1, WREN ; Enable Write to EEPROMBCF INTCON, GIEBANKSEL EECON2MOVLW 0x55 ; Write control word 55h to EECON2MOVWF EECON2MOVLW 0xAA ; Write control word AAh to EECON2MOVWF EECON2BANKSEL EECON1BSF EECON1, WR ; Start write operationBSF INTCON, GIE ;BCF EECON1, WREN ; Disable Write operation to EEPROM

BANKSEL EEADR

Page 126: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 126/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 124 of 129 

MOVLW 0X01MOVWF EEADR

BANKSEL EECON1BCF EECON1, EEPGD

BSF EECON1, RD

BANKSEL EEDATAMOVF EEDATA, W ; Move the contents of EEPROM to WREGMOVWF PORTB ; Move EEPROM contents to PORTB

L1GOTO L1

END

Output

EEPROM data (0x06) will be displayed in 8 LEDs.

Page 127: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 127/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 125 of 129 

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 128: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 128/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 126 of 129 

Register description for CCPxCON Register

Register description of PIE1 Register

Page 129: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 129/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 127 of 129 

Register description of PIR1 Register

Program

#INCLUDE<P16F877A.INC>

ORG 0X00GOTO START

ORG 0X04GOTO ISR

ORG 0X05START

BANKSEL TRISBBCF TRISC,2

CLRF TRISBBANKSEL PORTC

CLRF PORTBBANKSEL T1CON

BSF T1CON,T1CKPS1 ; Set Prescalar as 1:8BSF T1CON,T1CKPS0

BCF T1CON,TMR1CS ; Set Timer Mode of operation for Timer1

BANKSEL TMR1LMOVLW 0X00MOVWF TMR1L

BANKSEL TMR1HMOVLW 0X00

MOVWF TMR1H

BANKSEL CCP1CON

Page 130: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 130/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

Page 128 of 129 

CLRF CCP1CONMOVLW 0X08 ; Set Compare Mode of operation for CCP1,

MOVWF CCP1CON ; set CCP1 (RC2) pin on Compare eventBANKSEL PIR1

BCF PIR1,CCP1IF

BANKSEL CCPR1HMOVLW 0XF1

MOVWF CCPR1HBANKSEL CCPR1L

MOVLW 0XFFMOVWF CCPR1L

BANKSEL PIE1BSF PIE1,TMR1IE

BSF PIE1,CCP1IE ; Enable CCP1 InterruptBSF INTCON,PEIE

BSF INTCON,GIEBANKSEL T1CON

BSF T1CON,TMR1ON

L1

GOTO L1ISR

BCF INTCON,GIEBANKSEL PIR1

BTFSS PIR1,CCP1IF ; Check CCP1 Interrupt FlagGOTO TMR_ISR

BCF PIR1,CCP1IFBANKSEL CCPR1H

BANKSEL PORTBCOMF PORTB,1

RETFIETMR_ISR

BCF INTCON,GIEBANKSEL PIR1

BTFSS PIR1,TMR1IF ; Check Timer1 Interrupt FlagRETFIE

BCF PIR1,TMR1IFBANKSEL TMR1L

MOVLW 0X00MOVWF TMR1L

BANKSEL TMR1HMOVLW 0X00

Page 131: Microcontroller Lab Mnual

7/23/2019 Microcontroller Lab Mnual

http://slidepdf.com/reader/full/microcontroller-lab-mnual 131/131

Department of ECE, ASE Ettimadai Microcontroller Laboratory Manual

MOVWF TMR1HRETFIE

END

Output

PORTB will get complemented after each Compare event. CCP1 (RC2) pin will be set

after the first Compare operation.