19
FACULTY OF ELECTRICAL AND ELECTRONIC ENGINEERING COMPUTER ADDED DESIGN LABORATORY LABORATORY INSTRUCTION SHEET Subject Code and Name DAE32203 / DEK3133 MICROCONTROLLER Experiment Code 01 Experiment Title Introduction to MPLAB, PROTEUS and MikroC Course Code DET/DEE/DEX

Lab 1 microcontroller

  • Upload
    mkazree

  • View
    11.676

  • Download
    7

Embed Size (px)

Citation preview

Page 1: Lab 1 microcontroller

FACULTY OF ELECTRICAL AND ELECTRONIC ENGINEERING

COMPUTER ADDED DESIGN LABORATORY

LABORATORY INSTRUCTION SHEET

Subject Code and Name DAE32203 / DEK3133 MICROCONTROLLER

Experiment Code 01

Experiment Title Introduction to MPLAB, PROTEUS and MikroC

Course Code DET/DEE/DEX

Page 2: Lab 1 microcontroller

Document Reference No. RPP-05 Page Number 1 of 18

Document Title

LABORATORY PRACTICUM

Edition 1 Revision No. 1

Effective Date JULY 2010 Amendment Date JULY 2010

MAKLUMAT MATAPELAJARAN

SUBJECT : DAE32203 / DEK 3133 MICROCONTROLLER

TITLE : LAB 1 – Introduction to MPLAB, Proteus and MicroC.

GOAL : To expose student with MPLAB, Proteus and MicroC software

1. OBJECTIVES

At the end of this lab session, students will :

(i) Understand the operation and the basic use of MPLAB, Micro C and Proteus.

(ii) Use MPLAB and Micro C to write program and assembly language for PIC Microcontroller.

(iii) Use MPLAB SIM and Proteus to simulate assembly and C language for PIC Microcontroller.

2. SINOPSYS

MPLAB and Micro C is a software package easy writing process and development of assembly and C

language program for PIC Microcontroller. In this lab, student will have the exposure on MPLAB and

Micro C software and learn how to manage project by using MPLAB starting and Micro C from assembly

and C language program until simulation by Proteus.

Disediakan Oleh:

Disahkan Oleh:

Tandatangan :

Tandatangan :

Nama : Mohamad Bin Hj. Md. Som

Nama : Shamsul Bin Mohamad

Tarikh : July-2010

Tarikh : July-2010

Page 3: Lab 1 microcontroller

Document Reference No. RPP-05 Page Number 2 of 18

Document Title

LABORATORY PRACTICUM

Edition 1 Revision No. 1

Effective Date JULY 2010 Amendment Date JULY 2010

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

Figure 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

b’11110000’ - binary

Basic elements in assembly language are:

Label

Instruction

Operand

Comment

Page 4: Lab 1 microcontroller

Document Reference No. RPP-05 Page Number 3 of 18

Document Title

LABORATORY PRACTICUM

Edition 1 Revision No. 1

Effective Date JULY 2010 Amendment Date JULY 2010

B) PROTEUS

ISIS provides the development environment for PROTEUS VSM, our revolutionary interactive system

level simulator. This software combines mixed mode circuit simulation, micro-processor 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.

MikroC is a powerful, feature rich development tool for PICmicros. It is designed to provide the programmer with the easiest possible solution for developing applications for embedded systems, without compromising performance or control.

C) MikroC IDE

PIC and C fit together well: PIC is the most popular 8-bit chip in the world, used in a wide variety of applications, and C, prized for its efficiency, is the natural choice for developing embedded systems. mikroC provides a successful match featuring highly advanced IDE, ANSI compliant compiler, broad set of hardware libraries, comprehensive documentation, and plenty of ready-to-run examples.

Features :

mikroC allows you to quickly develop and deploy complex applications:

• Write your C source code using the built-in Code Editor (Code and Parameter Assistants, Syntax Highlighting, Auto Correct, Code Templates, and more…)

• Use the included mikroC libraries to dramatically speed up the development: data acquisition, memory, displays, conversions, communications… Practically all P12, P16,

Page 5: Lab 1 microcontroller

Document Reference No. RPP-05 Page Number 4 of 18

Document Title

LABORATORY PRACTICUM

Edition 1 Revision No. 1

Effective Date JULY 2010 Amendment Date JULY 2010

and P18 chips are supported.

• Monitor your program structure, variables, and functions in the Code Explorer. • Generate commented, human-readable assembly, and standard HEX compatible with all

programmers. • Inspect program flow and debug executable logic with the integrated Debugger.

4. EQUIPMENT LIST

(1) Personal Computer (PC)

(2) MPLAB Software

(3) Mikro C Software

(4) Proteus Software

5. PROCEDURE

5.1 WRITE ASSEMBLY CODE AND SIMULATE USING MPLAB.

(A) Starting New Project

1. Start MPLAB software. Click start > Microchip > MPLAB IDE

2. MPLAB work area will appear; shows in Figure 1-1.

Figure 1-1

3. To start a new project, click Project > new. New windows of New Project will appear; shows in

Figure 1-2.

Page 6: Lab 1 microcontroller

Document Reference No. RPP-05 Page Number 5 of 18

Document Title

LABORATORY PRACTICUM

Edition 1 Revision No. 1

Effective Date JULY 2010 Amendment Date JULY 2010

4. Fill in Project Name as Lab1 and Project directory with c:\lab1 or use Browse to choose existence

directory.

Figure 1-2

5. To write PIC assembly language program, click File > New. New windows editing will appear.

6. Write down the program 1.1 below and save it by name as lab1.asm ;**************************************************************************

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

Page 7: Lab 1 microcontroller

Document Reference No. RPP-05 Page Number 6 of 18

Document Title

LABORATORY PRACTICUM

Edition 1 Revision No. 1

Effective Date JULY 2010 Amendment Date JULY 2010

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.

7. Insert file programming into this project by clicking right button Source File which shows in Figure

1-3. Choose lab1.asm file that has written.

Figure 1-3

Page 8: Lab 1 microcontroller

Document Reference No. RPP-05 Page Number 7 of 18

Document Title

LABORATORY PRACTICUM

Edition 1 Revision No. 1

Effective Date JULY 2010 Amendment Date JULY 2010

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

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

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-4) will appear. First, from pull down menu, choose

PORTA and click Add SFR button. Then choose PORTB and click Add SFR button again.

Figure 1-4

Page 9: Lab 1 microcontroller

Document Reference No. RPP-05 Page Number 8 of 18

Document Title

LABORATORY PRACTICUM

Edition 1 Revision No. 1

Effective Date JULY 2010 Amendment Date JULY 2010

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 to reset the

program.

5. Click Debugger > Stimulus Controller > New Workbook >Asynch. A window in Figure 1-5 will

appear. At Pin/SFR column, choose RA4 pin and at Action column choose Set Low. Observe any

changes at Watch window after Animate button and Fire button is pressed. Observe any changes.

Figure 1-5

Page 10: Lab 1 microcontroller

Document Reference No. RPP-05 Page Number 9 of 18

Document Title

LABORATORY PRACTICUM

Edition 1 Revision No. 1

Effective Date JULY 2010 Amendment Date JULY 2010

5.2 SIMULATION THE HEX FILE USING PROTEUS

1. Open Proteus by clicking START > ALL PROGRAM > PROTEUS 7 PROFESIONAL > ISIS 7 PROFESIONAL .

Figure 1-6 2. Proteus work area will appear; shows in Figure 1-7. Select button “ P” and follow the next instructions.

Figure 1-7

Page 11: Lab 1 microcontroller

Document Reference No. RPP-05 Page Number 10 of 18

Document Title

LABORATORY PRACTICUM

Edition 1 Revision No. 1

Effective Date JULY 2010 Amendment Date JULY 2010

3. A window in Figure 1-8 will appear.

Figure 1-8 4. Select instruction bellows and doudle click your left mouse to input components inside category window as shown in figure 1-9. a) MICROPROCESSORS > PIC16F877A b) RESISTORS > 3 WATT1K c) OPTOELECTRONICS > LED-BIBY d) SWITCHES & RELAYS > BUTTON

Figure 1-9

Page 12: Lab 1 microcontroller

Document Reference No. RPP-05 Page Number 11 of 18

Document Title

LABORATORY PRACTICUM

Edition 1 Revision No. 1

Effective Date JULY 2010 Amendment Date JULY 2010

5. Drag and drop components by clicking at “DEVICES” window to working window as shown at

figure 1-10.

Figure 1-10 6. Right click mouse and select properties for rotate the components to your suitable positions 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.

Figure 1-12

Page 13: Lab 1 microcontroller

Document Reference No. RPP-05 Page Number 12 of 18

Document Title

LABORATORY PRACTICUM

Edition 1 Revision No. 1

Effective Date JULY 2010 Amendment Date JULY 2010

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

figure 1-13.

Figure 1-12

10. Save your project at different folder and named it Projek1_circuit as figure 1-14.

Figure 1-13

Page 14: Lab 1 microcontroller

Document Reference No. RPP-05 Page Number 13 of 18

Document Title

LABORATORY PRACTICUM

Edition 1 Revision No. 1

Effective Date JULY 2010 Amendment Date JULY 2010

Figure 1-14

11. From your working space, double click your PIC16F877A and follow the instructions number inside figure 1-15. Setting your Edit Component window by following the step given. 1st setting frequency to 4 MHz, 2nd click at browse button and select the hex file from the project folder created by compiler (MPLAB or Micro C ) . 3rd click “OK”.

Figure 1-15

Page 15: Lab 1 microcontroller

Document Reference No. RPP-05 Page Number 14 of 18

Document Title

LABORATORY PRACTICUM

Edition 1 Revision No. 1

Effective Date JULY 2010 Amendment Date JULY 2010

11. 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.16. Explain your observation.

Figure 1-16

12. The complete circuit as shown as Figure 1-17.

Figure 1-17

13. To see detail what happen to PORTA, PORTB and others register, go to Debug>watch window and

add the register that you want to watch like a figure below.

Figure 1-18

Page 16: Lab 1 microcontroller

Document Reference No. RPP-05 Page Number 15 of 18

Document Title

LABORATORY PRACTICUM

Edition 1 Revision No. 1

Effective Date JULY 2010 Amendment Date JULY 2010

5.3 PROGRAMMING USING C LANGUAGE

1. Open Mikro C compiler as shown in figure 1-18.

Figure 1-18

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.

Page 17: Lab 1 microcontroller

Document Reference No. RPP-05 Page Number 16 of 18

Document Title

LABORATORY PRACTICUM

Edition 1 Revision No. 1

Effective Date JULY 2010 Amendment Date JULY 2010

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

Page 18: Lab 1 microcontroller

Document Reference No. RPP-05 Page Number 17 of 18

Document Title

LABORATORY PRACTICUM

Edition 1 Revision No. 1

Effective Date JULY 2010 Amendment Date JULY 2010

4. Type the source code below into the code editor:

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.

6. Compiler will generate output files in the project folder. There should be 4 new files: project1.asm, project1.lst, project1.mcl and project1.hex. The file name with *.hex file will be used to burn into PIC flash memory or can be load into simulation software (Proteus) to simulate the output.

Page 19: Lab 1 microcontroller

Document Reference No. RPP-05 Page Number 18 of 18

Document Title

LABORATORY PRACTICUM

Edition 1 Revision No. 1

Effective Date JULY 2010 Amendment Date JULY 2010

6. 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) ( 5 Mark )

• Procedure lab sheet . (30)

1. Program 1.1 by MPLAB. ( 5 Mark )

2. Proteus - Circuit diagram ( 10 Mark )

3. Program 1.2 by Micro C. ( 5 Mark )

4. Flow chart of Mikro C code. ( 10 Mark )

• Observations. (35)

1. Observations when using MPLAB software. ( 15 Mark )

2. Observations when using Proteus software. ( 10 Mark )

3. Observations when using Micro C software. ( 10 Mark )

• Discussions. (20)

1. Result by using MPLAB software ( 10 Mark )

2. Result by using Proteus software ( 5 Mark )

3. Result by using Micro C software ( 5 Mark )

• Conclusions. (10) ( 10 Mark )