LAB PREVIEW: Introduction to PIC18F4550 MCU, …metalab.uniten.edu.my/~jamaludin/EEEB371/EEEB371...

15
EEEB371 E1- 1 LAB PREVIEW: Introduction to PIC18F4550 MCU, PTK40A Training Kit, MPLAB, PROTEUS and PICKit 2. Objectives: Provide an overview of the PIC18F4550 micro controller. Introduce students to MPLAB ® IDE, PTK40A Training Kit, Proteus and PICKit 2. Learn to make a project using MPLAB and generate the hex file of the program. Learn how to simulate the hex file using PROTEUS. Learn how to use PICKit 2 to download the hex file into PTK40A Training Kit and verify the simulation. 1) PIC18F4550 Introduction A micro controller unit (MCU) is an implementation of a computer on a single very large scale integrated circuit. It uses a microprocessor as its central processing unit (CPU) with additional peripheral components such as memory, timers, analog-to-digital converters, parallel I/O interface, asynchronous and synchronous serial communication interfaces. MCUs are used in almost every application that requires certain level of intelligence, such as controllers for printers, keyboards and home appliances. Microchip’s 8-bit PIC microcontroller is available in three product architecture categories, providing a variety of options for application requirement as needed by the user. The specific families include: Baseline – PIC10F and some PIC12F and PIC16F; Mid-Range– PIC12F and PIC16F; and High Performance – PIC18F with J and K-Series. The PIC18F4550 Micro Controller The PIC18 family utilizes a 16-bit program word architecture and incorporates an advanced RISC architecture with 32 level-deep stack, 8x8 hardware multiplier, and multiple internal and external interrupts. Table 1.1 below provides a summary of the features available for the PIC18F4550 chip. The pin-outs configuration of the PIC18F458 is as shown in Figure 1.2. Table 1.1: PIC18F4550

Transcript of LAB PREVIEW: Introduction to PIC18F4550 MCU, …metalab.uniten.edu.my/~jamaludin/EEEB371/EEEB371...

Page 1: LAB PREVIEW: Introduction to PIC18F4550 MCU, …metalab.uniten.edu.my/~jamaludin/EEEB371/EEEB371 1617S1 Lab_Pre… · LAB PREVIEW: Introduction to PIC18F4550 MCU, PTK40A Training

EEEB371 E1- 1

LAB PREVIEW: Introduction to PIC18F4550 MCU, PTK40A Training Kit, MPLAB, PROTEUS and PICKit 2. Objectives:

• Provide an overview of the PIC18F4550 micro controller. • Introduce students to MPLAB® IDE, PTK40A Training Kit, Proteus and

PICKit 2. • Learn to make a project using MPLAB and generate the hex file of the

program. • Learn how to simulate the hex file using PROTEUS. • Learn how to use PICKit 2 to download the hex file into PTK40A Training Kit

and verify the simulation. 1) PIC18F4550 Introduction

A micro controller unit (MCU) is an implementation of a computer on a single very large scale integrated circuit. It uses a microprocessor as its central processing unit (CPU) with additional peripheral components such as memory, timers, analog-to-digital converters, parallel I/O interface, asynchronous and synchronous serial communication interfaces. MCUs are used in almost every application that requires certain level of intelligence, such as controllers for printers, keyboards and home appliances. Microchip’s 8-bit PIC microcontroller is available in three product architecture categories, providing a variety of options for application requirement as needed by the user. The specific families include:

• Baseline – PIC10F and some PIC12F and PIC16F; • Mid-Range– PIC12F and PIC16F; and • High Performance – PIC18F with J and K-Series.

The PIC18F4550 Micro Controller The PIC18 family utilizes a 16-bit program word architecture and incorporates an advanced RISC architecture with 32 level-deep stack, 8x8 hardware multiplier, and multiple internal and external interrupts. Table 1.1 below provides a summary of the features available for the PIC18F4550 chip. The pin-outs configuration of the PIC18F458 is as shown in Figure 1.2.

Table 1.1: PIC18F4550

Page 2: LAB PREVIEW: Introduction to PIC18F4550 MCU, …metalab.uniten.edu.my/~jamaludin/EEEB371/EEEB371 1617S1 Lab_Pre… · LAB PREVIEW: Introduction to PIC18F4550 MCU, PTK40A Training

EEEB371 E1- 2

Figure 1.2: Pin Diagram The PIC18 MCUs provide the following peripheral functions:

• Parallel I/O ports • Timer functions, including counters, input capture, output capture, output compare,

real-time interrupt, and watchdog timer • Pulse width modulation (PWM) • Serial peripheral interface (SPI)and inter-integrated circuit (I2C) serial interface • Universal Synchronous/Asynchronous Receiver Transmitter (USART) • A/D converter with 10-bit resolution • Analogue comparator • Low-power operation mode • SRAM and EEPROM • EPROM or flash memory • USB host connection • Controller Area Network (CAN)

2) PTK40A Training Kit The PIC18 family utilizes a 16-bit program word architecture and incorporates an advanced RISC architecture PTK40A Training Board is developed based on Microchip 8-bit 40 pins PIC MCU (MicroController Unit). It covers programming, interfacing, and applications of microcontroller and mechanical hardware to help students in learning and developing their project with PIC MCU. The system’s hardware is well configured so that it can be programmed by any compatible programming language such as assembly, BASIC and C (for PIC MCU). PTK40A provides several basic modules or projects for user to explore PIC MCU peripherals and applications such as keypad, LCD, DC brush motor & encoder, Stepper motor, UART and more. These basic modules are mainly the fundamental control and monitor of Mechatronic applications.

Page 3: LAB PREVIEW: Introduction to PIC18F4550 MCU, …metalab.uniten.edu.my/~jamaludin/EEEB371/EEEB371 1617S1 Lab_Pre… · LAB PREVIEW: Introduction to PIC18F4550 MCU, PTK40A Training

EEEB371 E1- 3

In this kit, the USB ICSP PIC Programmer, UIC00B is being integrated to be utilized for downloading program (hex code) into PIC MCU. Using UIC00B offers a convenient programming method that allow program to be loaded or updated while the PIC MCU stays on the board.

Figure 1.3: Board Layout and On Board Components

Table 1.2: On Board Components

Page 4: LAB PREVIEW: Introduction to PIC18F4550 MCU, …metalab.uniten.edu.my/~jamaludin/EEEB371/EEEB371 1617S1 Lab_Pre… · LAB PREVIEW: Introduction to PIC18F4550 MCU, PTK40A Training

EEEB371 E1- 4

Power Supply Power supply part is main power for PTK40A. User may supply power either using DC adaptor or battery. 7 Segment LED Display 7-segment is a component that consists seven segments of lights which would display the number from 0 to 9. RC Servo RC servo motor is a type of motor that is generally used in hobby RC toy. Due to its special feature that offers open loop position control, it has been employed in building robot arm and biped robot. Relay Relay is an electrical device that is used as a switch in electrical system with high current or voltage. UIC00B USB ICSP Programmer On board programmer (UIC00B) is utilize to download the program into PIC MCU. UIC00B offers a convenient programming method that allow program to be loaded or updated while the PIC MCU stays on the board.

UART There are 2 types of UART terminal on PTK40A which are UC00A and Cytron Starter Kit. UC00A used to communicate between PTK40A and Computer Desktop/Laptop.

Microcontroller PTK40A comes with PIC18F4550. It is being placed on the 40 pin ZIF (Zero Insert Force) socket. PTK40A support 40 pin PDIP PIC microcontroller, PIC16F and PIC18F.

16x2 Characters LCD The 16x2 characters LCD offers character display for embedded system. It can be used to display numerical information, text message and also special symbol. Stepper Motor Stepper motor is a very important actuator in automation system due to its feature which is able to rotate in steps. There are many types of stepper motor available in the market such as unipolar type, bipolar type, single-phase type, multi-phase type etc. PTK40A comes with hybrid type of stepper which can be configured as unipolar or bipolar. Motor Driver L293D IC which is motor driver for stepper and DC motor. External Driver Besides use L293D to drive stepper and DC motor, user may use external driver such as SD02B to drive stepper motor and MD10C to drive DC motor. LEDs The LEDs are function as general purpose indicator of digital output of PIC MCU. DC Motor & Encoder The DC brush motor included in PTK40A is a general purpose DC motor with 6V operating voltage. The slotted encoder disc is a 1/16" thick plastic, with 1.4" outer diameter. The slotted encoder disc has 8 spokes that enable it to provide up to 16 transitions or 8 pulses per rotation.

Page 5: LAB PREVIEW: Introduction to PIC18F4550 MCU, …metalab.uniten.edu.my/~jamaludin/EEEB371/EEEB371 1617S1 Lab_Pre… · LAB PREVIEW: Introduction to PIC18F4550 MCU, PTK40A Training

EEEB371 E1- 5

Buzzer or Piezo buzzer Buzzer is one of the digital output devices. External Digital Input Provide socket for external digital input such as limit switch, IR01A and CX422. The voltage for sensor can be selected through jumper, is either 5V or 12V.

Analogue Input There are 3 analogue inputs on PTK40A, Variable resistor or potentiometer act as voltage divider, LM35 temperature sensor and external socket for user to connect any suitable analogue sensor. Selection of analogue input is done through the jumper. 4x4 Keypad and Push Buttons The 4 x 4 keypad is mounted on PTK40A to ease user pressing numbers. Pin Selector Pin selector for PTK40A Training Kit. PTK40A shared several I/O pins for different functions. For example on JP10, buzzer pin is shared with PWM pin. To choose or to select buzzer, user need to use mini jumper on JP10. Same applied for SPI, I2C, USB, Relay and Servo.

Expansion Ports Expansion ports or JP4 is provided as optional expansion for user to develop other experiments.

USB B Type Socket The USB socket offer option for user to develop USB application using PIC18F family of PIC that support USB peripheral, one example is PIC18F4550. 3) MPLAB® IDE MPLAB IDE is a software program that runs on a PC to develop applications for Microchip microcontrollers. It is called an Integrated Development Environment (IDE), because it provides a single integrated “environment” to develop code for embedded microcontrollers. The Integrated Development Environment allows the embedded systems design engineer to progress through the design cycle without the distraction of switching among an array of tools. By using MPLAB IDE, all the functions are integrated, allowing the engineer to concentrate on completing the application without the interruption of separate tools and different modes of operation.

Figure 1.4: Design Cycle

Page 6: LAB PREVIEW: Introduction to PIC18F4550 MCU, …metalab.uniten.edu.my/~jamaludin/EEEB371/EEEB371 1617S1 Lab_Pre… · LAB PREVIEW: Introduction to PIC18F4550 MCU, PTK40A Training

EEEB371 E1- 6

The built-in components of MPLAB IDE consist of: • Project Manager - The project manager provides integration and communication

between the IDE and the language tools. • Editor - The editor is a full-featured programmer’s text editor that also serves as a

window into the debugger. • Assembler/Linker and Language Tools - The assembler can be used stand-alone to

assemble a single file, or can be used with the linker to build a project from separate source files, libraries and recompiled objects. The linker is responsible for positioning the compiled code into memory areas of the target microcontroller.

• Debugger - The Microchip debugger allows breakpoints, single stepping, watch windows and all the features of a modern debugger for the MPLAB IDE. It works in conjunction with the editor to reference information from the target being debugged back to the source code.

• Execution Engines - There are software simulators in MPLAB IDE for all PICmicro MCU and dsPIC DSC devices. These simulators use the PC to simulate the instructions and some peripheral functions of the PICmicro MCU and dsPIC DSC devices. Optional in-circuit emulators and in-circuit debuggers are also available to test code as it runs in the applications hardware.

Procedure in using MPLAB® IDE

1. In order to create a code that is executable by the target PICmicro MCU, source file needs to be put into a project and then (the code) built into executable code using selected language tools (assemblers, compilers, linkers, etc.). In MPLAB IDE, the project manager controls this process.

Invoke the MPLAB IDE by double-clicking the MPLAB shortcut at the desktop.

2. Select the device by choosing the Configure menu and choose Select Device. Then choose PIC18F4550 as your device.

Figure 1.5: Select Device Dialog

Page 7: LAB PREVIEW: Introduction to PIC18F4550 MCU, …metalab.uniten.edu.my/~jamaludin/EEEB371/EEEB371 1617S1 Lab_Pre… · LAB PREVIEW: Introduction to PIC18F4550 MCU, PTK40A Training

EEEB371 E1- 7

3. Select Configure>Configuration Bits…

4. Uncheck Configuration Bits set in code

5. Click Oscillator Selection bits at Category bar. At the Setting bar, you will have a

drop-down menu. Click that and choose ‘HS’ as setting for oscillator.

6. Then configure the following settings:

Watchdog Timer Enable bit: WDT disable PORTB A/D Enable bit: configure as digital Single-Supply ICSP Enable bit: disable

Page 8: LAB PREVIEW: Introduction to PIC18F4550 MCU, …metalab.uniten.edu.my/~jamaludin/EEEB371/EEEB371 1617S1 Lab_Pre… · LAB PREVIEW: Introduction to PIC18F4550 MCU, PTK40A Training

EEEB371 E1- 8

7. Creating Source Code with the Editor

Select File>New to open an empty editor window in which to type your source code. Before entering the following code, study it carefully. What does the program do? Hint: Please refer to Figure 1.8

#include<p18f4550.inc> ;program start template org 0x00 goto start org 0x08 retfie org 0x18 retfie ;main program start SETF TRISB,A CLRF TRISD,A CLRF PORTD,A AGAIN BTFSS PORTB,0 BRA EVEN BTFSC PORTB,1 BRA AGAIN MOVLW 0x55 MOVWF PORTD,A BRA AGAIN EVEN MOVLW 0xAA MOVWF PORTD,A BRA AGAIN NOP END

Page 9: LAB PREVIEW: Introduction to PIC18F4550 MCU, …metalab.uniten.edu.my/~jamaludin/EEEB371/EEEB371 1617S1 Lab_Pre… · LAB PREVIEW: Introduction to PIC18F4550 MCU, PTK40A Training

EEEB371 E1- 9

Once you have finished entering the code, select File>Save and save the file as LABPREV.asm. You may save it to any directory; you will move it, if necessary, into the project directory in the next step.

8. Next, create a project using the Project Wizard. A project is the way your files are organized to be compiled and assembled. We will use a single assembly file for this project. Choose the Project>Project Wizard. From the Welcome dialog, click on Next> to advance in the Project Wizard.

9. Step Two of the Project Wizard sets up the language tools that are used with this

project. Select Microchip MPASM Toolsuite in the top pulldown. Then you should see MPASM, MPLINK and MPLIB show up in the Toolsuite Contents box. You can click on each one to see its location. If you installed MPLAB IDE into the default directory, the MPASM assembler executable will be:

C:\Program Files\Microchip\MPASM Suite\mpasmwin.exe

the MPLINK linker executable will be: C:\Program Files\Microchip\MPASM Suite\mplink.exe

and the MPLIB librarian executable will be: C:\Program Files\Microchip\MPASM Suite\mplib.exe

10. Step Three of the wizard allows you to name the project and put it into a folder. This

sample project will be called LABPREV 11. Step Four of the Project Wizard allows us to select files for the project. Choose the

file named LABPREV.asm.

Figure 1.6: Project Wizard

Press the Add>> button to move the file name to the right panel, and click on the check box at the start of the line with the file name to enable this file to be copied to our project directory.

Page 10: LAB PREVIEW: Introduction to PIC18F4550 MCU, …metalab.uniten.edu.my/~jamaludin/EEEB371/EEEB371 1617S1 Lab_Pre… · LAB PREVIEW: Introduction to PIC18F4550 MCU, PTK40A Training

EEEB371 E1- 10

Make sure that your dialog looks like the picture above, with check box checked, then press the Next> button to finish the Project Wizard. The final screen of the Project Wizard is a summary showing the selected device, the toolsuite, and the new project file name. After pressing the Finish button, look at the Project Window on the MPLAB IDE desktop. It should look like this. If the Project Window is not open, select View>Project

Figure 1.7: Project Window

12. Build the Project: Project>Build All menu items. This will execute the MPASM

assembler in the background. Once the assembly process is complete, the Output Window will appear. The Output window will display “BUILD SUCCEEDED”. File .LST and .HEX will be generated.

13. To include the program configuration settings into your code: File>Export. Click OK and save your hex file in the same folder as your project file. Click on the existing hex file and overwrite it with this hex file. Without performing this procedure, your hardware might not work properly.

14. Next, you will design a circuit using Proteus and load the hex file to verify your program.

4) PROTEUS® Proteus contains everything you need to develop; test and virtually prototype your embedded system designs based around the Microchip Technologies™ PIC18 series of microcontrollers. The unique nature of schematic based microcontroller simulation with Proteus facilitates rapid, flexible and parallel development of both the system hardware and the system firmware. This design synergy allows engineers to evolve their projects more quickly, empowering them with the flexibility to make hardware or firmware changes at will and reducing the time to market. Proteus VSM models will fundamentally work with the exact same HEX file as you would program the physical device with, produced by any assembler or compiler.

Page 11: LAB PREVIEW: Introduction to PIC18F4550 MCU, …metalab.uniten.edu.my/~jamaludin/EEEB371/EEEB371 1617S1 Lab_Pre… · LAB PREVIEW: Introduction to PIC18F4550 MCU, PTK40A Training

EEEB371 E1- 11

Procedure in using PROTEUS

1. Go to Start>All Programs>Proteus 7 Professional>ISIS 7 Professional.

2. For this lab preview, your lab instructor will guide you step by step to design the circuit below. Additional Task: Draw another switch and connect it to RB1.

Figure 1.8: Basic Proteus circuit

3. To run the simulation, you need to load the hex file first by right-clicking on the

microcontroller and then click edit properties.

4. At the Edit Component menu: a) Under the Program File tab, select your hex file by clicking on the browse

icon. b) Under the Processor Clock Frequency, select your desired clock frequency. c) Under Advanced Properties, select Enable Watchdog Timer and then select

No. d) Finally, click OK.

Page 12: LAB PREVIEW: Introduction to PIC18F4550 MCU, …metalab.uniten.edu.my/~jamaludin/EEEB371/EEEB371 1617S1 Lab_Pre… · LAB PREVIEW: Introduction to PIC18F4550 MCU, PTK40A Training

EEEB371 E1- 12

5. Finally, click on the Play button at the bottom of the workspace to run the simulation.

6. Observe the simulation. Does the circuit work as expected according to your program in MPLAB?

7. Next, you will load the same hex file into PIC18F4550 mounted on the PTK40A Training Kit. You will require PICKit 2 programmer software to load the hex file into the hardware.

Page 13: LAB PREVIEW: Introduction to PIC18F4550 MCU, …metalab.uniten.edu.my/~jamaludin/EEEB371/EEEB371 1617S1 Lab_Pre… · LAB PREVIEW: Introduction to PIC18F4550 MCU, PTK40A Training

EEEB371 E1- 13

5) PICKit 2® The required software to load program into PIC MCU is PICkit 2 Programmer. The PICkit 2 Programmer application allows user to program all supported devices. The programming interface appears as shown in figure below.

Figure 1.9: PICKit2 Programming Interface

Using PICKit 2 Programmer 1. To load program into PIC MCU:

• Connect DC adaptor to adaptor socket. • Connect USB mini cable to UIC00B (left top corner) on PTK40A.

Page 14: LAB PREVIEW: Introduction to PIC18F4550 MCU, …metalab.uniten.edu.my/~jamaludin/EEEB371/EEEB371 1617S1 Lab_Pre… · LAB PREVIEW: Introduction to PIC18F4550 MCU, PTK40A Training

EEEB371 E1- 14

2. Switch on power supply for PTK40A.

3. Launch PICkit 2 Programmer by selecting Start>Programs>Microchip>PICkit 2.

4. When the PICkit 2 programmer application is first opened, it will attempt to identify the connected device by the device ID and display it in the Configuration window as shown in figure below.

5. To connect to a device once the application is already running, select Tools>Check Communication.

6. To import hex file to be programmed into the PTK80A, select File>Import HEX as shown in figure below.

Page 15: LAB PREVIEW: Introduction to PIC18F4550 MCU, …metalab.uniten.edu.my/~jamaludin/EEEB371/EEEB371 1617S1 Lab_Pre… · LAB PREVIEW: Introduction to PIC18F4550 MCU, PTK40A Training

EEEB371 E1- 15

7. Browse for your hex file and click Open. The code is displayed in the Program Memory and EEPROM Data windows. The name of the hex file is displayed in the Source Block under Program Memory. Hex file generated from MPLAB IDE will be named according to project name.

8. After a device family has been selected and a hex file has been imported, the target device can be programmed by clicking Write. The device will be erased and programmed with the hex code previously imported.

9. The status of the write operation is displayed in the status bar located under the device Configuration window. If the write is successful, the status bar turns green and displays “Programming Successful” as shown in figure below.

10. Observe what happens when you press Switch 1 and Switch 2 on the PTK40A

Training Kit. Is the output as same as Proteus simulation?

11. Turn off the PTK40A power supply, rearrange the USB cable back into the Training Kit. Exit from MPLAB, Proteus, PICKit 2 programmer and shutdown your PC.