Professional Documents
Culture Documents
Bee31101 Lab2 v20142015
Bee31101 Lab2 v20142015
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
Table of Content
Table of Content
ii
1.0 Outcomes
2.0 Instructions
4.0 Guidelines
Overview
Example 1
Example 2
12
14
Lab Activity 1
14
Lab Activity 2
14
14
15
7.0 References
15
ii
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.
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.
ii. MOVLW
MOVWF
iii. MOVLW
MOVWF
iv. MOVLW
MOVWF
B00000110;
h85;
00;
h86;
01;
1FH;
(2 marks)
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
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
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
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
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.
Figure 1.7
Figure 1.8
3. A window in Figure 1.9 will appear.
Figure 1.9
FKEE, Semester II Session 2014/15
ii.
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
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
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
11
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
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
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
00h
MAIN
TIMELOW
COUNTLOW
COUNTHIGH
DELAYLOOP
STATUS,RP0
B'00000000'
TRISB
STATUS,RP0
PORTB
PORTB, LED
DELAY
PORTB,LED
DELAY
LOOP
13
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.
14
EQU
EQU
21h
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.
15