1. CENTRE OF DIPLOMA STUDIES
COMPUTER ADDED DESIGN LABORATORY
LABORATORY INSTRUCTION SHEET
DEK 3133
Subject Code and Name
MICROCONTROLLER
Experiment Code 02
Introduction to PIC Development Board &
Experiment Title Program Delay
Course Code DET/DEE/DEX
2. Document
RPP-05
Reference No. Page Number 01
Edition 1
Document LABORATORY Revision No. 4
Title PRACTICUM Effective Date 21/7/2010
Amendment 21/7/2010
Date
a)
SUBJECT INFORMATION
SUBJECT : DEK 3133 Microcontroller
TOPIC : Lab 2 – Introduction to PIC Development Board & Program Delay
AIM : Familiarizing with PIC Development Board and programming
using software delay.
1 OBJECTIVES
1.1 To understand the development concept and testing the program by using PIC Development
Board - PICDEV
1.2 To learn how to download the program into the PIC chip
2 EQUIPMENT
2.1 PIC Development Board – PICDEV
2.2 MPLAB IDE Program
2.3 MikroC.
2.4 The PIC Development Board User manual
2.5 Power supply 9V
3 PART 1: Understand the development concept and testing the program by using
PIC Development Board PICDEV
THEORY AND EQUATIONS
3.1 The program development
PIC instructions set is very simple and it has only 35 instructions. To develop a system by using PIC,
a user must have the suitable equipment.
Prepared by: Approved by:
Signature : Signature :
Name : Mohamad Bin Hj. Md. Som Name : Shamsul Bin Mohamad
Date : 21-July-2010 Date : 21-July-2010
3. Document
RPP-05
Reference No. Page Number 02
Edition 1
Document LABORATORY Revision No. 4
Title PRACTICUM Effective Date 21/7/2010
Amendment 21/7/2010
Date
a)
4 ATTENTION
4.1.1
4.1 Do not move any IC or device inside the board without the order from your instructor.
4.2 When you want to download and test a PIC 16F877A, make sure the PIC is removed,
otherwise it will damage the board.
5 EXPERIMENT PROCEDURE
5.1 Communicating with the PIC Development Board – PICDEV
5.1.1 Connect the serial cable from the PICDEV PIC programmer serial port to PC’s COM1
port.
5.1.2 Then connect the 9V 1A power adaptor to the programmer, switch on the
programmer and run the MPLAB‐IDE software.
5.1.3 At the MPLAB‐IDE software, select Programmer > Select Programmer > PICSTART
Plus. Figure 1 shows the steps.
Figure 1
5.1.4 Enable the programmer as shown in Figure 2. It will detect the PIC programmer. If
MPLAB does not detect your programmer, check the cable and the COM port and try
again. After completing this step, you can write your own program and blow into PIC
chip. Figure 3 shows the box that indicates the MPLAB has detected the programmer.
4. Document
RPP-05
Reference No. Page Number 03
Edition 1
Document LABORATORY Revision No. 4
Title PRACTICUM Effective Date 21/7/2010
Amendment 21/7/2010
Date
a)
Figure 2
Figure 3
5. Document
RPP-05
Reference No. Page Number 04
Edition 1
Document LABORATORY Revision No. 4
Title PRACTICUM Effective Date 21/7/2010
Amendment 21/7/2010
Date
a)
5.2 Start the program and download the program into the PIC chip
5.2.1 Select Project > New to make a new directory to your program. New Project box will
appear and you need to give a name to your project and also the directory that you
want to use as shown in Figure 4.
Figure 4
5.2.2 Select View > Project and a box that contain the file needed in your directory will
appear (see figure 5):
-Source Files
-Header Files
-Object Files
-Library Files
-Linker Scripts
6. Document
RPP-05
Reference No. Page Number 05
Edition 1
Document LABORATORY Revision No. 4
Title PRACTICUM Effective Date 21/7/2010
Amendment 21/7/2010
Date
a)
Figure 5
5.2.3 Create a new file and start writing the program as shown below and save your
program as lab2asm.as. (The circuit related to this code is at attachment: Circuit
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
7. Document
RPP-05
Reference No. Page Number 06
Edition 1
Document LABORATORY Revision No. 4
Title PRACTICUM Effective Date 21/7/2010
Amendment 21/7/2010
Date
a)
;**************************************************************************
ORG 00h ;Assembler is now set to 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.
5.2.4 Right click at Source Files and click Add Files. The Insert Files into Project box
will appear and choose the lab2asm.asm as shown in Figure 6.
8. Document
RPP-05
Reference No. Page Number 07
Edition 1
Document LABORATORY Revision No. 4
Title PRACTICUM Effective Date 21/7/2010
Amendment 21/7/2010
Date
a)
Figure 6
5.2.5 Double click at lab2asm.asm and go through the program and check the types of PIC
chip that have been using in the program. Click Configure > Select Device then select
the device you want to program a shown in Figure 7.
9. Document
RPP-05
Reference No. Page Number 08
Edition 1
Document LABORATORY Revision No. 4
Title PRACTICUM Effective Date 21/7/2010
Amendment 21/7/2010
Date
a)
Figure 7
5.2.6 Then set the Configuration Bits according to the Microchip IC that you choose as
shown in Figure 8. To set, go Configure > Configuration Bits
10. Document
RPP-05
Reference No. Page Number 09
Edition 1
Document LABORATORY Revision No. 4
Title PRACTICUM Effective Date 21/7/2010
Amendment 21/7/2010
Date
a)
Figure 8
5.2.7 Look at the Link Setting section in the program. Then configure the PIC Development
with the correct jumpers setting and jumper leads. Make sure the power is off at this
state.
5.2.8 After finish setting the jumper, assemble it. Click Project > Built All. This step will
convert the assembly language to hex file. In no error, you can place your blank PIC
chip at the programmer.
5.2.9 Before program the chip, please do a blank check. Click on
programmer>Blank Check. This is to make sure that the
program memory inside the PIC is empty.
5.2.10 If the device is not blank, select Programmer>Erase. The
programmer will erase all the contents of program
memory.
5.2.11 When the device is blank. Click Programmer > Program
to blow your PIC chip. Wait until this message appears
“Programming/Verification completed successfully!”
5.2.12 After that, you can place your PIC chip at the development
board and test the programming running correctly.
5.2.13 Write your result in the report.
11. Document
RPP-05
Reference No. Page Number 010
Edition 1
Document LABORATORY Revision No. 4
Title PRACTICUM Effective Date 21/7/2010
Amendment 21/7/2010
Date
a)
5.3 Programming the PIC using MikroC and burn into PIC using PICDEV
5.3.1 Open Mikro C compiler as shown in figure below.
5.3.2 Create a new project
‐ Project wizard dialog will appear – fill the dialog
with appropriate settings for your project
‐ Enter a name for your new project
‐ Choose project path,
‐ Enter short text which describes your project (this
is optional)
‐ Choose the microcontroller from drop-down menu
‐ Set the device clock by entering the value in edit
box (set to 4Mhz)
‐ Set configuration bits (device flags) by clicking default
‐ After you have set up your project, click OK to continue.
12. Document
RPP-05
Reference No. Page Number 011
Edition 1
Document LABORATORY Revision No. 4
Title PRACTICUM Effective Date 21/7/2010
Amendment 21/7/2010
Date
a)
5.3.3 Write the code:
‐ Compiler will create the project file and an
accompanying source file, named same as your project.
This source file will be automatically opened in the
code editor, so you can write the source code. Picture
below is the window of code editor.
13. Document
RPP-05
Reference No. Page Number 012
Edition 1
Document LABORATORY Revision No. 4
Title PRACTICUM Effective Date 21/7/2010
Amendment 21/7/2010
Date
a)
5.3.4 Type the source code below into the code editor:
5.3.5 Save the file by clicking on the save icon. To compile the source code select Project
>Build from the drop‐down menu, or click the Build Icon . When compiling the
“lab2.c” is success, the “lab2.hex” file is created. This file will be use by the MPLAB to
burn it into PIC device.
14. Document
RPP-05
Reference No. Page Number 013
Edition 1
Document LABORATORY Revision No. 4
Title PRACTICUM Effective Date 21/7/2010
Amendment 21/7/2010
Date
a)
5.3.6 By using MPLAB, check the PIC whether it empty or not. If not empty, select
Programmer>erase and select Programmer>blank check to make sure the PIC is
blank.
5.3.7 Import the lab2.hex file into MPLAB by selecting
File>Import.
5.3.8 When the device is blank. Click Programmer > Program to blow your PIC chip. Wait
until this message appears “Programming/Verification completed successfully!”
5.3.9 After that, you can place your PIC chip on the development board and test the
programming running correctly.
6 Assignment 1 (See Attachment : Circuit 2)
6.1 By using MikroC, modify the lab2.c to SWITCH ON all the LED at PORTB when the
button at PORTA.F4 is pressed. When the button is not pressed all the LED at PORTB is
SWITCH OFF.
6.2 Draw a flowchart for the modified code in your report.
6.3 Before MPLAB burn the file into the PIC, You need to erase your PIC chip by clicking
Programmer > Erase Flash Device > Black Check All.
6.4 Then you need to blow your PIC chip by doing the same step showing before. (Pls refer
to item 5.2.9)
15. Document
RPP-05
Reference No. Page Number 014
Edition 1
Document LABORATORY Revision No. 4
Title PRACTICUM Effective Date 21/7/2010
Amendment 21/7/2010
Date
a)
7 Program the PIC using Delay
Theory and Equations
7.1 Introduction
CALL
1. 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.
2. The RETURN tells the PIC to go back to where it came from and execute the next
instruction.
3. The CALL routine is placed at the front of the program as 16F84 devices require
CALL to be within the first area of memory.
Timing Delay (Software Delay)
1. The crystal fitted to the PIC DEVELOPMENT BOARD run at 4 MHz and each
instruction takes 4 cycles to complete.
2. Therefore
¼ MHz * 4 = 1μs per instruction
3. The CALL routine delay has the number of cycles for each section commented in and
the total delay stated as:
DELAY
movlw TIMELOW ;1 cycle
movwf countlow ;1 cycle 4 cycles
movlw TIMEHIGH ;1 cycle
movwf counthigh ;1 cycle
delayloop
decfsz countlow ;1 cycle 3 cycles x countlow
goto delayloop ;2 cycles
number of times
movlw TIMELOW ;1 cycle
movwf countlow ;1 cycle
decfsz counthigh ;1 cycle
goto delayloop ;2 cycles
return ;1 cycle
;total delay = 4 + ((3 cycles * countlow) + 5 Cycles)) * counthigh + 1
16. Document
RPP-05
Reference No. Page Number 015
Edition 1
Document LABORATORY Revision No. 4
Title PRACTICUM Effective Date 21/7/2010
Amendment 21/7/2010
Date
a)
4. 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.
5. 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.
6. When it has reaches 0, it will break the cycle and go to movlw TIMELOW and again
countlow will load with TIMELOW.
7. 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.
8 ASSIGNMENT 2 (See Attachment : Circuit 3)
8.1 Using MPLAB, write the program as shown below and use the same step before. Burn the
program into the device and see what the result is. Then write the report about it.
;****************************************************************************
;* Assignment 2 Program to flash a LED using delays *
;* *
;* Operation:- This program flashes a LED with 50/50 duty cycle *
;* at intervals set by TIMEHIGH and TIMELOW *
;*************** Hardware Definitions - Specific to the PIC *****************
PORTB EQU 06h ;This tells the assembler where the address of port B
;********************** Constants (Preset Values) ***************************
TIMELOW EQU H'FF'
TIMEHIGH EQU H'FF'
;************************* Variables (Registers) ****************************
countlow EQU 20h ;Address of General Purpose Register
counthigh EQU 21h
;*************************** Start of the Program ***************************
ORG 0h
goto start
;############################## Call Routines ##############################
;This call routine generates a delay
;*********Delay for 200ms******************
DELAY
movlw TIMELOW ;1 cycle
movwf countlow ;1 cycle | 4 cycles
movlw TIMEHIGH ;1 cycle |
movwf counthigh ;1 cycle /
delayloop
decfsz countlow ;1 cycle 3 cycles x countlow number of times
goto delayloop ;2 cycles /
movlw TIMELOW ;1 cycle
17. Document
RPP-05
Reference No. Page Number 016
Edition 1
Document LABORATORY Revision No. 4
Title PRACTICUM Effective Date 21/7/2010
Amendment 21/7/2010
Date
a)
movwf countlow ;1 cycle | 5 cycles
decfsz counthigh ;1 cycle |
goto delayloop ;2 cycles /
return ;1 cycle
;total delay = 4 + ((3 cycles * countlow) + 5 Cycles)) * counthigh + 1
;total delay = 4 + ((3 * 255 + 5)) * 255 + 1 = 196355 * 1us = 196ms ~ 200ms
;############################ End of Call Routines #########################
start
;********************************** Setup **********************************
clrf PORTB ;Clear the file PORTB (All outputs set to 0)
movlw B'00000000' ;The value 0 is loaded into w register bits 0-7
tris PORTB ;This loads w into PORTB, PORTB is now configured
; as all outputs, each output is low as set above
;************************ Start of the Main Program ************************
loop
bsf PORTB,0
call DELAY
bcf PORTB,0
call DELAY
goto loop
END
8.2 By using Mikro C, create a new project and type the program as shown in figure below.
8.2 Draw a flowchart in your report for the above C code.
8.3 Compile the program and burn the hex file into the PIC.
18. Document
RPP-05
Reference No. Page Number 017
Edition 1
Document LABORATORY Revision No. 4
Title PRACTICUM Effective Date 21/7/2010
Amendment 21/7/2010
Date
a)
9 ASSIGNMENT 3 (See Attachment: Circuit 4)
9.1 By using Mikro C, modify the program to ON the LED with the sequence from LED1
until LED8 and then from LED8 until LED1 continuously. The delay for each LED to ON
and OFF is 1 second.
9.2 Draw a flowchart in your report.
9.3 Compile the program and burn the hex file to the PIC and write your observation.
10 REPORT PREPARATION AND SCHEMA.
(1) 2 persons for 1 report.
(2) Due date to send report is 1 weeks after lab date.
(3) Report schema following below requirements:
• Lab report cover sheet for 1st page.
• Objective, theory, equipments for the 2nd page. (5) (5M)
• Observations. (20)
1. Observations on writing the code using assembly language and burn it into PIC (10M)
2. Observations on writing the code using C and burn it into PIC. (10M )
• Discussions. (50)
1. Flow chart of assignment 1 (C code) (10M )
2. The C code of assignment 1 (10M )
3. Flow chart of Assignment 2 (C code) (10M )
4. Flow chart of Assignment 3 (C code) (10M )
5. The C code of assignment 3 (10M )
• Questions. (10)
1. What is the maximum size (Kbyte) of program memory for PIC16F877A (5M)
2. Draw a diagram of program memory for PIC16F877A (5M)
• Conclusions. (15M)
19. Document
RPP-05
Reference No. Page Number 018
Edition 1
Document LABORATORY Revision No. 4
Title PRACTICUM Effective Date 21/7/2010
Amendment 21/7/2010
Date
a)
Attachments:
Circuit 1: 1 Button (PORTA bit 4 : Input) and 1 LED (PORTB bit 0 : Output)
Circuit 2: 1 Button (PORTA bit 4 : Input) and 8 LEDs (PORTB : Output)
20. Document
RPP-05
Reference No. Page Number 019
Edition 1
Document LABORATORY Revision No. 4
Title PRACTICUM Effective Date 21/7/2010
Amendment 21/7/2010
Date
a)
Circuit 3: PIC with 1 LED (PORTB bit 0 : Output)
Circuit 4: PIC with 8 LEDs (PORTB : Output)