Vous êtes sur la page 1sur 17

Faculty

of Electric and Electronic Engineering

Electronic Engineering Laboratory IV


BEE31101
Instruction Sheet

Lab No.
Lab Title
Semester
Session
Lab Durations
Independent Studies

2
Introduction to Assembly Language and
Simulations for PIC16F877A
02
2014/15
2 Hours
1 Hour

Electronic Engineering Laboratory IV (BEE31101)


Lab 2: Introduction to Assembly Language and Simulation for PIC16F877A

Table of Content

Table of Content

ii

1.0 Outcomes

2.0 Instructions

3.0 Pre-Lab (5%)

4.0 Guidelines

Overview

Example 1

Example 2

12

5.0 Lab Activities (40%)

14

Lab Activity 1

14

Lab Activity 2

14

5.0 Observations (15%)

14

6.0 Questions (15%)

15

7.0 References

15

FKEE, Semester II Session 2014/15

ii

Electronic Engineering Laboratory IV (BEE31101)


Lab 2: Introduction to Assembly Language and Simulation for PIC16F877A

1.0 Outcomes
After completing this module, student should be able to:
1. Apply an appropriate assembly language using MPLAB and simulators using Proteus for
microcontroller PIC16F877A. (C3, PS)
2. Organize time management in group effectively according task given. (P5, TS)
3. Adapt the current technology in microcontroller with morale and etiquette. (A4, ET)

2.0 Instructions
1. Grouping: Lab group is not predetermine and consists with at most two team members.
2. Pre-Lab: Must be handwritten and submitted to the instructor at the beginning of lab session.
Verified by the instructor and returned to the students at the end of lab session. The verified
pre-lab will be attached with the final report for submission.
3. Lab Activities: All lab activities such as sample code, examples and lab assignments must
be held in the respective lab location and completed within the given times.
4. Demonstration: Student must demonstrate the successful sample code, examples and lab
assignments to the respective instructor. Verification only will be given upon completion of all
lab activities and initialized by the instructor on the cover page.
5. Report Organization: Report must be organized according to given report template.
6. Appendix: Handwritten source code with detail description of each command for lab
activities. Marks for lab activities are given based on attachment of the appendix. Printed
source code is required only as an attachment of lab activities.
7. Report Submission: Report must be received by respective technical staff (at respective
lab) before 4.00pm; not later than three (3) days upon completion of lab session.

FKEE, Semester II Session 2014/15

Electronic Engineering Laboratory IV (BEE31101)


Lab 2: Introduction to Assembly Language and Simulation for PIC16F877A

3.0 Pre-Lab (5%)


1.

Briefly explain the following used assembly language commands:


i.

CLRW,

ii. BCF,
iii. BSF,
iv. MOVLW,
v. MOVF,
vi. INCFSZ.
(3 marks)
2. Describe each command line (use in PIC16F877A) of the programs bellow by using your own
words.
i.

BSF STATUS, RP0

ii. MOVLW
MOVWF
iii. MOVLW
MOVWF
iv. MOVLW
MOVWF

B00000110;
h85;
00;
h86;
01;
1FH;
(2 marks)

FKEE, Semester II Session 2014/15

Electronic Engineering Laboratory IV (BEE31101)


Lab 2: Introduction to Assembly Language and Simulation for PIC16F877A

4.0 Guidelines
Overview
A. MPLAB
MPLAB is comprehensive software that has editing, project management and design in it. It
used in development of embedded system application using microcontroller Microchip
PICmicro and PIC. Assembly language is a language that used for programming
microcontroller. Before we programmed these assembly language instructions into
microcontroller memory, it has to convert to machine language so that microcontroller easily
understands. This conversion is done by software so called assembler in MPLAB. This
process is described in Figure 1.2

Figure 1.1
In the MPLAB assembly language programming, numbers usually represent decimal, binary
and hexadecimal. For example, decimal number 240 is written by:
240 - decimal
0xF0 - hexadecimal
b11110000 - binary
Basic elements in assembly language are:
Label
Instruction
Operand
Comment

FKEE, Semester II Session 2014/15

Electronic Engineering Laboratory IV (BEE31101)


Lab 2: Introduction to Assembly Language and Simulation for PIC16F877A
B. PROTEUS
ISIS provides the development environment for PROTEUS VSM, our revolutionary interactive
system level simulator. This software combines mixed mode circuit simulation, microprocessor models and interactive component models to allow the simulation of complete
micro-controller based designs. ISIS provides the means to enter the design in the first place,
the architecture for real time interactive simulation and a system for managing the source and
object code associated with each project. In addition, a number of graph objects can be
placed on the schematic to enable conventional time, frequency and swept variable
simulation to be performed.
Major features of PROTEUS VSM include:
True Mixed Mode simulation based on Berkeley SPICE3F5 with extensions for digital
simulation and true mixed mode operation.
Support for both interactive and graph based simulation.
CPU Models available for popular microcontrollers such as the PIC and 8051 series.
Interactive peripheral models include LED and LCD displays, a universal matrix
keypad, an RS232 terminal and a whole library of switches, pots, lamps, LEDs etc.

Example 1
(A) Starting New Project in MPLAB
1. Start MPLAB software. Click start > Microchip > MPLAB IDE
2. MPLAB work area will appear; shows in Figure 1.2

Figure 1.2
3. To start a new project, click Project > new. New windows of New Project will appear;
shows in Figure 1.3.
4. Fill in Project Name as Lab1 and Project directory with c:\Lab1 or use Browse to choose
existence directory.
FKEE, Semester II Session 2014/15

Electronic Engineering Laboratory IV (BEE31101)


Lab 2: Introduction to Assembly Language and Simulation for PIC16F877A

Figure 1.3
5. To write PIC assembly language program, click File > New. New windows editing will
appear.
6. Write down the program 1 below and save it by name as L1_buttonLED.asm
;****************************** Program 1 **********************************
PORTA
EQU 05h
;This tells the assembler where the address of PORTA,
PORTB
EQU 06h
;PORTB, STATUS and ADCON1
TRISA
EQU 85h
;This Tell the assembler where the address of ports direction of,
TRISB
EQU 86h
;TRISA and TRISB.
STATUS EQU 03h
ADCON1 EQU 9Fh
;Address of Type of data whether Digital/Analog at PORTA
RP1
EQU 6
;Bit 6 and 5 for bank selection in STATUS register.
RP0
EQU 5
;To select bank 0, RP1 = 0 and RP0 = 0 {PORTA and PORTB}
;To select bank 1, RP1 = 0 and RP0 = 1 {TRISA,TRISB & ADCON1}
;To select bank 2, RP1 = 1 and RP0 = 0
;To select bank 3, RP1 = 1 and RP0 = 1
;***************************************************************************
ORG 00h
;Setting of address 0 where the main program is placed
;*********************************PIC SETUP********************************
BSF
STATUS,RP0
;Select bank 1(To use TRISA,B and ADCON1)
MOVLW
B'00000110'
;Load literal value=6 into W register
MOVWF
ADCON1
;Move the literal value into ADCON1 register.
;Now PORTA is configured as digital input
MOVLW
B'00000000'
;0 is loaded into W register
MOVWF
TRISB
;Now all PORTB bits are set as OUTPUT
MOVLW
B'11111111'
;move binary value 11111111 to register W
MOVWF
TRISA
;Now All PORTA bits are set as INPUT

;*************************START OF MAIN PROGRAM*************************


BCF
STATUS,RP0
;Back to bank 0;(To use PORTA and PORTB)
CLRF
PORTB
;Set PORTB output is 0
LOOP
BTFSS
PORTA,4
;Bit test PORTA bit 4, if SET skip next line
GOTO
ON
;Goto ON if button is pressed
OFF
BCF
PORTB,0
;LED is OFF when button at PORTA is not pressed.
GOTO
LOOP
;Go back and test the button again
ON
BSF
PORTB,0
;As PORTA input is 0, mean button is pressed so
;LED at PORTB bit 0 goes ON
GOTO
LOOP
;Go back to LOOP and test the button again
END
;End of code.
;*****************************END PROGRAM*********************************

FKEE, Semester II Session 2014/15

Electronic Engineering Laboratory IV (BEE31101)


Lab 2: Introduction to Assembly Language and Simulation for PIC16F877A
7. Insert file programming into this project by clicking right button Source File which shows
in Figure 1.4. Choose lab1.asm file that has written.

Right Click to
Add file into
project source
Select file

1
2

Figure 1.4
8. To assemble the program, firstly we have to choose types of microcontroller by clicking
Configure > Select Device, choose 16F877A.
9. Select Configure > Configuration Bits and set as shown in a Figure 1.5 below.

Figure 1.5

FKEE, Semester II Session 2014/15

Electronic Engineering Laboratory IV (BEE31101)


Lab 2: Introduction to Assembly Language and Simulation for PIC16F877A
10. Click Project > Built All to assemble the program. If there is no error, then BUILD
SUCCEEDED message will appear. If there is an error, then Error message and
Location will appear.

(B) Project Simulation using MPLAB


1. To start Simulation, click Debugger > Select Tool > MPLAB SIM. Observe the
changes at the MPLAB interface. List down the changes.
2. Click View > Watch. Watch windows (Figure 1.6) will appear. First, from pull down
menu, choose PORTA and click Add SFR button. Then choose PORTB and click Add
SFR button again.
Right Click, select
Properties and set
to binary format

Figure 1.6
3. Click Debugger > Animate. Observe Watch window and also Lab1.asm program
window. Explain your observation.
4. Click Debugger > Halt to stop the simulation process. Click Debugger > Reset >
MCLR Reset to reset the program.
5. Click Debugger > Stimulus Controller > New Workbook >Asynch. A window in
Figure 1.7 will appear. At Pin/SFR column, choose RA4 pin and at Action column
choose toggle. Observe any changes at Watch window after Animate button and Fire
button is pressed. Observe any changes.

Click fire to toggle a


value at PORTA bit 4

Figure 1.7

FKEE, Semester II Session 2014/15

Electronic Engineering Laboratory IV (BEE31101)


Lab 2: Introduction to Assembly Language and Simulation for PIC16F877A
(C) Project Simulation Using PROTEUS.
1. Open Proteus by clicking Start > All program > Proteus 7 Professional > ISIS 7
Professional.
2. Proteus work area will appear, shows in Figure 1.8 Select button P and follow the next
instructions.

Figure 1.8
3. A window in Figure 1.9 will appear.

Figure 1.9
FKEE, Semester II Session 2014/15

Electronic Engineering Laboratory IV (BEE31101)


Lab 2: Introduction to Assembly Language and Simulation for PIC16F877A
4. Select components as stated bellows and double click your left mouse to input
components inside category window.
i.

MICROPROCESSORS > PIC16F877A

ii.

RESISTORS > 0.6WATT > MINRES1K

iii. OPTOELECTRONICS > LED-BIBY


iv. SWITCHES & RELAYS > BUTTON

5. Select the component and positioned the components accordingly.

Click to rotate
component clockwise

Figure 1.10
6. Drag and drop components by clicking at DEVICES window to working window as
shown at Figure 1.11.

Figure 1.11
7. Connect and wiring all components.
8. Click Terminals Mode button to input POWER and GROUND to your working space
as shown at Figure 1.12
FKEE, Semester II Session 2014/15

Electronic Engineering Laboratory IV (BEE31101)


Lab 2: Introduction to Assembly Language and Simulation for PIC16F877A

Figure 1.12
9. Arrange your components to suitable position and completed your work with the correct
connections as Figure 1.13.

Figure 1.13
10. Save your project at different folder and named it Lab1_Eg1.
11. From your working space, double click your PIC16F877A. Component setting is as
shown in Figure 1.14.

2
1
Figure 1.14
FKEE, Semester II Session 2014/15

10

Electronic Engineering Laboratory IV (BEE31101)


Lab 2: Introduction to Assembly Language and Simulation for PIC16F877A

12. Click DEBUG > EXECUTE to simulate your program and click DEBUG > STOP
ANIMATION to stop the program. From your window project, you can animate your
program by clicking suitable button as shown in Figure 1.15.

Figure 1.15
13. The complete circuit as shown as Figure 1.16.

LED Light
up
BUTTON
pressed

Figure 1.16
14. To see the detail what happen to PORTA, PORTB and others register, go to Debug >
Watch Window and Add the register that you want to monitor.
Right Click and
Add Item (By
Name)

Figure 1.17

FKEE, Semester II Session 2014/15

11

Electronic Engineering Laboratory IV (BEE31101)


Lab 2: Introduction to Assembly Language and Simulation for PIC16F877A

Example 2
DELAY Program
(A) CALL
A CALL command instruct the PIC to store the current location of the program counter, then goes
off to the address where the desired call routine exists, and executes instructions until it
encounters a RETURN command. The RETURN tells the PIC to go back to where it came from
and execute the next instruction. The CALL routine is placed at the front of the program as
16F877A devices require CALL to be within the first area of memory.
(B) Timing Delay
The PIC will take 4 cycles to complete each instruction. A PIC with 4 MHz crystal oscillator will
execute 1us per instruction as illustrated in calculation below
1
4 1s per instruction
4Mhz
The CALL routine delay has the number of cycles for each section commented in and the total
delay stated as Figure 1.18:
DELAY

MOVLW
MOVWF
MOVLW
MOVWF

TIMELOW
countlow
TIMEHIGH
counthigh

;
;
;
;

delayloop

decfsz
goto

countlow ; 1 cycle
delayloop ; 2 cycles

3 cycles * TIMELOW

MOVLW
MOVWF
decfsz
goto
return

TIMELOW
countlow
counthigh
delayloop

5 cycles

;
;
;
;

1
1
1
2

cycle
cycle
cycle
cycles

total delay = ((3 cycles * TIMELOW) + 5 cycles)* TIMEHIGH

Figure 1.18
There are two routine, one is the inner routine where a register countlow is decremented to zero
and the outer routine where counthigh is decremented.
Two register; countlow and counthigh are loaded with the starting value (TIMELOW and
TIMEHIGH). Countlow is decremented by 1, tested to see if it has reached 0; and if not,
decrement again. When it has reaches 0, it will break the cycle and go to movlw TIMELOW and
again countlow will load with TIMELOW. And then counthigh is decremented by 1, tested to see
if it has reached 0; and if not, go to delayloop and do the step as above until counthigh reaches
0.
FKEE, Semester II Session 2014/15

12

Electronic Engineering Laboratory IV (BEE31101)


Lab 2: Introduction to Assembly Language and Simulation for PIC16F877A

The following source code demonstrates delay function to create blinking LED at RB0. Simulate
the example source code using the previous circuit from example 1..
;************************* Program 2 ********************************************
PORTB EQU
06h
;This tells the assembler where the address of PORTB,
TRISB EQU
86h
;TRISB at address 86h (bank1).
STATUS EQU
03h
;STATUS register at address 03h (bank0).
RP1
EQU
6
;Bit 6 and 5 for bank selection in STATUS register.
RP0
EQU
5
;To select bank 0, RP1 = 0 and RP0 = 0 {PORTA and PORTB}
;To select bank 1, RP1 = 0 and RP0 = 1 {TRISA,TRISB & ADCON1}
;To select bank 2, RP1 = 1 and RP0 = 0
;To select bank 3, RP1 = 1 and RP0 = 1
LED
EQU
0
;This tells the assembler that LED represent a 0
;*************************
TIMELOW
EQU
0xFF
TIMEHIGH
EQU
0xFF
COUNTLOW
EQU
21h
COUNTHIGH
EQU
22h

Constant presets value *******************************


;value = 256 or binary = 11111111
;value = 256 or binary = 11111111
;bank 0 at address 21h
;bank 0 at address 22h

;**************************** START *********************************************


ORG
GOTO

00h
MAIN

;Assembler is now set to address 0 where the main program is placed


;jump to MAIN

;***************************** Call Routines ************************************


DELAY MOVLW TIMELOW
;value = 256 or b = 11111111 sent to working register
MOVWF COUNTLOW
;From working register sent data to address 21h (GPR) at Bank 0
MOVLW TIMEHIGH
;value = 256 or b = 11111111 sent to working register
MOVWF COUNTHIGH
;From working register sent data to address 22h (GPR) at Bank 0
DELAYLOOP
DECFSZ COUNTLOW
GOTO
DELAYLOOP
MOVLW
MOVWF
DECFSZ
GOTO
RETURN

TIMELOW
COUNTLOW
COUNTHIGH
DELAYLOOP

;decrement 1 continuously form 256 and skip if zero


;jump to DELAYLOOP
;value = 256 or b = 11111111 sent to working register
;From working register sent data to address 21h (GPR) at Bank 0
;decrement 1 continuously form 256 and skip if zero
;jump to DELAYLOOP
;return to previous program line

;******************************** PIC SETUP **************************************


MAIN BSF
MOVLW
MOVWF

STATUS,RP0
B'00000000'
TRISB

;Select bank 1(To use TRISA,B and ADCON1)


;0 is loaded into W register
;Now all PORTB bits are set as OUTPUT

;******************************** START OF MAIN PROGRAM ***************************


BCF
CLRF
LOOP BSF
CALL
BCF
CALL
GOTO
END

STATUS,RP0
PORTB
PORTB, LED
DELAY
PORTB,LED
DELAY
LOOP

;Back to bank 0;(To use PORTA and PORTB)


;Clear PORTB output is 0
;set PortB 1 / ON LED
;jump to DELAY
;set PortB 0 / OFF LED
;jump to DELAY
;Go back to LOOP and test the button again
;End of code.

;********************************** END PROGRAM ************************************

FKEE, Semester II Session 2014/15

13

Electronic Engineering Laboratory IV (BEE31101)


Lab 2: Introduction to Assembly Language and Simulation for PIC16F877A

5.0 Lab Activities (40%)


Lab Activity 1
1. Create an algorithm (flow chart) that will light 7 LEDs which are connected to PORTB (use all
port from RB0 to RB7) when RA4 (PORTA bit 4) is HIGH and turn OFF otherwise.
(5 marks)
2. Modify the source code from Example1 to create the application as stated in (1).
(10 marks)
3. Execute your program and observe the simulation output. Please use the simulation circuit
provided (Example1).
4. Demonstrate your solution to the respective instructor.

Lab Activity 2
1. Create an algorithm that will display a running light sequence of 8 LEDs connected to
PORTB. The first LED will light up for a moment before moving on to the next LED. This
sequence continues until the 8th LED; and this cycle repeated continuously. Use circuit
diagram Example1 as circuit reference.
(5 marks)
2. Modify the source code from Example2 to create the application as stated in (1).
(20 marks)
3. Execute your program and observe the simulation output. Please use the simulation circuit
provided (Example1).
4. Demonstrate your solution to the respective instructor.

5.0 Observations (15%)


1. From Example 2, discuss how 0.1s delay can be implemented using assembly language
programming using 4MHz oscillator.
(10 marks)
2. From Lab Activity 2, write down simple command line to toggle/turning ON and OFF one
LED?
(3 marks)

FKEE, Semester II Session 2014/15

14

Electronic Engineering Laboratory IV (BEE31101)


Lab 2: Introduction to Assembly Language and Simulation for PIC16F877A
3. From Lab Activity 2 propose a better method of creating a running light sequence using
assembly language.
(2 marks)

6.0 Questions (15%)


COUNTLOW
COUNTHIGH

EQU
EQU

21h
22h

;bank 0 at address 21h


;bank 0 at address 22h

1. Describe the main purpose of using address 21h & 22h in Example2 programming as shown
on the above statement.
(5 marks)
2.

Write a program that produces running light sequence that will turn ON odd, turn OFF even
LEDs using bit shifting in assembly language.
(10 marks)

7.0 References
1. MICROCHIP. (2003) PIC16f877A Datasheet: Microchip Technology Inc.
2. Laboratory Instruction Sheet. (2012). Microcontroller. Introduction of MPLAB and PROTEUS:
Centre of Diploma UTHM.
3. Laboratory Instruction Sheet. (2011). Microcontroller. Introduction to PIC Development
System: Faculty of Electrical and Electronic Engineering UTHM.

FKEE, Semester II Session 2014/15

15

Vous aimerez peut-être aussi