You are currently viewing Create & Debug a project in Mplab using PIC16F877A

Create & Debug a project in Mplab using PIC16F877A

Spread the love

1. Introduction of MPLAB

The MPLAB X IDE is the new graphical, integrated debugging tool set for all of Microchip’s more than 800 8-bit, 16-bit and 32-bit MCUs and digital signal controllers, and memory devices.

It includes a feature-rich editor, source-level debugger, project manager, software simulator, and supports Microchip’s popular hardware tools, such as the MPLAB ICD 3 in-circuit debugger, PICkit™ 3, and MPLAB PM3 programmer.

MPLAB IDE is a Windows Operating System (OS) software program that runs on a PC to develop applications for Microchip microcontrollers and digital signal controllers. It is called an Integrated Development Environment, or IDE, because it provides a single integrated “environment” to develop code for embedded microcontrollers. Experienced embedded systems designers may want to skip ahead to Components of MPLAB IDE.

The Object-HEX Converter creates Intel HEX files from absolute object modules.

2. Development Tools

The Mplab development tools for PIC offer numerous features and advantages that help you quickly and successfully develop embedded applications. They are easy to use and are guaranteed to help you achieve your design goals.

The MPLAB IDE and Debugger is the central part of the Microchip PIC development tools. MPLAB offers a Build Mode and a Debug Mode.

In the MPLAB Build Mode you maintain the project files and generate the application. MPLAB uses either the Hi-Tech or PIC ASM development tools.

In the MPLAB Debug Mode you verify your program either with a powerful CPU and peripheral simulator that connects the debugger to the target system.

The ICD2 allows you also to download your application into Flash ROM of your target system.

3. Getting Started

The MPLAB IDE combines project management, make facilities, source code editing, program debugging, and complete simulation in one powerful environment. The MPLAB development platform is easy-to-use and helping you quickly create embedded programs that work.

The MPLAB editor and debugger are integrated in a single application that provides a seamless embedded project development environment.

Use MPLAB to create your source files and organize them into a project that defines your target application.

MPLAB automatically compiles, assembles, and links your embedded application and provides a single focal point for your development efforts.

When you use the MPLAB, the project development cycle is roughly the same as it is for any other software development project.

☞Create a project, select the target chip from the device database, and configure the tool settings.

☞Create source files in C or assembly.

☞Build your application with the project manager.

☞Correct errors in source files.

☞Test the linked application.

☞Debug the executable logic by watching program flow with the built-in simulator or in real time with in-circuit emulators or in-circuit debuggers.

☞Make timing measurements with the simulator or emulator.

☞View variables in Watch windows.

☞Program firmware into devices with device programmers.

3.1. Create a Project

MPLAB includes a project manager which makes it easy to design applications for a PIC based microcontroller. You need to perform the following steps to create a new project:

☞Start MPLAB and select the toolset

☞Create a project file and select a CPU from the device database.

☞Create a new source file and add this source file to the project.

☞Add and configure the startup code for the PIC.

☞Set tool options for target hardware.

☞Build project and create a HEX file for Flash programming.

3.2. Start MPLAB

MPLAB is a standard Windows application and started by clicking on the program icon.

Create & Debug a project in Mplab using PIC16F877A

3.2.1. Create a Project File

To create a new project file select from the MPLAB menu Project – Project Wizard…. This opens a standard Windows dialog that asks you for the new project file name.

Now we can select Next button then the window shows as shown in below. Then we can select the device. Devices are selected by using the drop-down button in the device option.

Create & Debug a project in Mplab using PIC16F877A

3.2.2. Select Language Tools

Step two of the Project Wizard sets up the language tools that are used with this project. Select Hi-Tech Universal Tool suite in the top pull down. Then you should see PICC, HLINK and LIBR show up in the Tool suite Contents box. You can click on each one to see its location. If you installed MPLAB IDE into the default directory, the Hi-Tech compiler executable will be:

C:\Program Files\HI-TECH Software\PICC\9.83\bin\picc.exeThe MPLINK linker executable will be:C:\Program Files\HI-TECH Software\PICC\9.83\bin\hlink.exeAnd the MPLIB librarian executable will be:C:\Program Files\HI-TECH Software\PICC\9.83\bin\libr.exe

If these do not show up correctly, use the browse button to set them to the proper files in the MPLAB IDE subfolders.

Create & Debug a project in Mplab using PIC16F877A

When you are finished, click next >.

3.2.3. Naming the Project

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

This sample project will be called Mplab. Using the Browse button, place the project in a folder named Mplab. Click Next>

Create & Debug a project in Mplab using PIC16F877A

3.2.4. Adding files to Project

Step Four of the Project Wizard allows file selection for the project. A source file has not yet been selected, so we will use an MPLAB IDE template file. The template files are simple files that can be used to start a project. They have the essential sections for any source file, and contain information that will help you write and organize your code.

These files are in the MPLAB IDE folder, which by default is in the Program Files folder on the PC.

There is one template file for each Microchip PIC micro and dsPIC device.

Choose the file named led_delay.c. If MPLAB IDE is installed in the default location, the full path to the file will be:C:\Documents and Settings\Administrator\Desktop\Mplab\led_delay.c

Create & Debug a project in Mplab using PIC16F877A

Press Add>> to move the file name to the right panel, and the file name to enable this file to be copied to our project directory.

Make sure that your dialog looks like the picture above, with both check boxes checked, then press Next> to finish the Project Wizard.

Create & Debug a project in Mplab using PIC16F877A

The final screen of the Project Wizard is a summary showing the selected device, the tool suite and the new project file name.

After pressing the Finish button, review the Project Window on the MPLAB IDE desktop. If the Project Window is not open, then select View>Project.

Create & Debug a project in Mplab using PIC16F877A

Note: Files can be added and projects saved by using the right mouse button in the project window. In case of error, files can be manually deleted by selecting them and using the right mouse click menu.

3.2.5. Building the Project

From the Project menu, we can assemble and link the current files. They don’t have any of our code in them yet, but this assures that the project is set up correctly.

Create & Debug a project in Mplab using PIC16F877A

To build the project, select either:

☞Project>Build All

☞Right-click on the project name in the project window and select Build All

☞Click the Build All icon on the Project toolbar. However the mouse over icons to see pop-up text of what they represent.

The Output window shows the result of the build process. There should be no errors on any step.

Create & Debug a project in Mplab using PIC16F877A

Note:If these do not assemble and link successfully, check the following items and then build the project again:

☞Check the spelling and format of the code entered in the editor window. Make sure the new variables and the special function registers, TRISC and PORTC, are in upper case. If the assembler reported errors in the Output window, double click on the error and MPLAB IDE will open the corresponding line in the source code with a green arrow in the left margin of the source code window.

☞Check that the correct compiler (Microchip Hi-Tech Compiler) and linker for PIC micro devices are being used. Select Project>Set Language Tool Locations. Click on the plus boxes to expand the Microchip Hi-Tech tool suite and its executables. Click Microchip Hi-Tech Compiler (picc.exe) and review their location in the display. If the location is correct, click Cancel. If it is not, change it and then click OK. The default search paths can be empty.

Upon a successful build, the output file generated by the language tool will be loaded.

This file contains the object code that can be programmed into a PIC micro MCU and debugging information so that source code can be debugged and source variables can be viewed symbolically in Watch windows.

3.2.6. Testing Code with the Simulator

In order to test the code, software or hardware is needed that will execute the PIC micro instructions. A debug execution tool is a hardware or software tool that is used to inspect code as it executes a program (in this case led_delay.c). Hardware tools such as MPLAB ICE or MPLAB ICD 2 can execute code in real devices. If hardware is not available, the MPLAB simulator can be used to test the code. In this tutorial we can use MPLAB SIM simulator.

The simulator is a software program that runs on the PC to simulate the instructions of the PIC micro MCU.

It does not run in “real time,” since the simulator program is dependent upon the speed of the PC, the complexity of the code, overhead from the operating system and how many other tasks are running. However, the simulator accurately measures the time it would take to execute the code if it were operating in real time in an application.

Note: Other debug execution tools include MPLAB ICE 2000, MPLAB ICE 4000 and MPLAB ICD 2. These are optional hardware tools to test code on the application PC board. Most of the MPLAB IDE debugging operations are the same as the simulator, but unlike the simulator, these tools allow the target PIC micro MCU to run at full speed in the actual target application.

Additional menu items should now appear in the Debugger menu. Additional toolbar icons should appear in the Debug Tool Bar.

4. Getting Started with MPLAB SIM

MPLAB SIM is one of the debug engines that can be used with MPLAB. The other debug engines are hardware devices, while MPLAB SIM is a software program that runs on your PC. MPLAB SIM provides many of the same features as in-circuit emulators and in-circuit debuggers. The difference is that both in-circuit emulators and in-circuit debuggers allow the code to be run on actual silicon, and also allow target application hardware to be functional while being debugged.

MPLAB SIM has features to simulate hardware interaction with other signals and devices, and since it is running as software on the PC, it has complete information about the internal state of the simulated chip at each instruction. This is a little different from the hardware debuggers because, while they are running code at full speed, they typically cannot monitor all registers and all memory in real time.

Both MPLAB SIM and the hardware debuggers can do the traditional functions of debuggers, but due to their differences, they can have unique features of their own. This presentation will identify the functions and features of MPLAB SIM.

The debugger is a part of MPLAB IDE, and whether you are using MPLAB SIM, MPLAB ICE or MPLAB ICD 2, most operations are exactly the same. This allows you to develop code using the simulator, and then when your hardware is ready, you can use a hardware debugger to further test your code in practice without having to learn how to use a new tool.

These are the basic debug functions: Reset the target, in order to restart the application.

☞Execute the code so the program can be tested to verify it functions as designed.

☞Halt the code at breakpoints

☞While halted at breakpoints examine and modify memory and variables to analyze and debug the application code.

☞Single Step through code to closely inspect how it executes. This allows the engineer to go through code one instruction (or one C statement) at a time while monitoring affected variables, registers and flags. Single stepping essentially “zooms in” on code to ensure that it operates correctly in complex and critical sections with ranges of variable values and under various test conditions.

Most debuggers also have additional features to help analyze and debug the application.

Some of these are listed here:

☞Watch points group and monitor selected variables and memory locations into a convenient, custom readout.

☞Trace buffers capture the streams of instructions executed and reveal the contents of changing register values.

☞A Stopwatch can time a section of code. Routines can be optimized, and critical code timing can be accurately measured and adjusted.

☞Complex breakpoints offer a method for establishing breakpoints or for gathering data in the trace buffer based upon multiple conditions. Simple breakpoints allow setting breakpoints in the source code or anywhere in program memory. Complex breakpoints allow getting a breakpoint on a condition such as,   – After the main routine called “Refresh Display” executes then   – wait for subroutine “Read Temp” to execute. Then    – break if the variable named “Temperature” is greater than 20.

Complex events can be constructed to count events, so that a subroutine would have to be executed, for example, 15 times before it starts looking for a value on a pin or in a register. This kind of breakpoint allows you to describe the condition where your code misbehaves, to halt at a breakpoint or to trace code when that condition occurs. This is usually a faster way of finding bugs than simply setting simple breakpoints and stepping through your code.

MPLAB SIM is a simulator, and as a result it has certain characteristics that make it a unique debug engine. So the speed of the simulation is determined by,

☞How fast your PC executes,

☞The complexity of the current simulation, and

☞The number of other tasks executing on your PC.

Currently the maximum speed of MPLAB SIM is on the order of 10 MIPS, or 10 million instructions per second. This will be affected by how many other things are being done by your PC, by the code the simulator is running, and by the other tasks that the simulator is performing. The simulator simulates the operation of,

☞The core CPU and its internal registers,

☞Memory, and

☞Many of it peripherals.

In order to test the application on the simulator, stimulus signals can be applied to pins and registers.

To evaluate performance, the simulator can log changing registers to files for further analysis.

4.1. Starting Up MPLAB SIM

Create & Debug a project in Mplab using PIC16F877A

MPLAB SIM is selected as the debug engine from the Debugger menu. Note the other functions on the debug menu, such as Run, Step, and Breakpoints.

Once a debug engine is selected, the toolbar is appended with some new icons for running, halting, single stepping, and resetting the target.

The Status bar now shows some additional information.

☞MPLAB SIM shows as the current debug engine.

☞The simulated processor is listed, in this case the PIC16F877A,

☞Then the program counters,

☞The W register,

☞The current state of the internal CPU flags and

☞The current selected file register bank.

Create & Debug a project in Mplab using PIC16F877A

The toolbar icons, the menus, or the hot keys listed on the menus can be used to execute the debug functions. Note that some are a little more complex, such as Reset, which actually has four types of reset actions.

Once MPLAB SIM is established as the debug engine, whenever a project is built, it is automatically loaded into the simulator’s program memory to be run and tested.

One debug window is the source code window. This is actually the editor window, and breakpoints can be set by clicking on a line with the right mouse button. Single stepping with the source code window in focus will single step through the C source lines. Since you are in the editor, changes can be done quickly, and the project can be rebuilt.

Create & Debug a project in Mplab using PIC16F877A

The Program Memory window shows the machine code that will be executed by the simulator. Single stepping with this window in focus will allow you to step through each machine instruction.

Create & Debug a project in Mplab using PIC16F877A

Another window, called the Disassembly Listing window shows high level source code interspersed with machine code generated by each C statement.

Create & Debug a project in Mplab using PIC16F877A

You can also open up a watch window and drag the variables from your program to them to see the contents.

In order to see if the code is operating as intended, sending incrementing values out PORTC, watch the values being sent to PORTC.

Below are the menu items in the Disassembly window right mouse button menu.

☞Set or remove a breakpoint at the currently-selected line.

☞Enable/Disable a breakpoint at the currently- selected line.

☞Run the program to the current cursor location.

☞Set PC at Cursor Set the program counter (PC) to the cursor location.

☞Copy selected text to clipboard. Select text by (1) clicking and dragging mouse over text or (2) clicking at the beginning of text and shift-clicking at the end of text.

☞Select all text in the window.

☞Output to File.

Create & Debug a project in Mplab using PIC16F877A

Select View>Watch to bring up an empty Watch Window.

There are two pull downs on the top of the Watch Window. The one on the left labeled “Add SFR” can be used to add the Special Function Register, PORTC, into the watch. Select PORTC from the list and then click Add SFR to add it to the window.

Create & Debug a project in Mplab using PIC16F877A

While debugging, other windows are available to view,

☞register memory,

☞stack memory, and

☞Non-volatile data memory areas.

5. Sample Programs

The sample programs are ready for you to run. You can use the sample programs to learn how to use our tools. Additionally, you can copy the code from MPLAB samples for your own use.

The sample programs are found in…\ Hi-Tech software\ Samples (for Hi-Tech Universal toolset). The path is,C:\Program Files\HI-TECH Software\PICC\9.83\samples

Each sample program is stored in a separate folder along with project files that help you quickly build and evaluate each sample program.

5.1. LED_delay: Your first PIC C Program

In Mplab, we can generate a simple C program named LED_delay by using the Mplab Editor.

LED_delay program does nothing just high & low an I/O line within a certain time period. This program has a single source file LED_delay.C.

This small application helps you confirm that you can compile, link, and debug an application. You can perform these operations from the Mplab for Windows using the provided project file.

The only on-chip peripheral used is the I/O port line. You do not actually need a target CPU because Mplab lets you simulate the hardware required for this program.

5.2. Mplab Project File

In Mplab, applications are maintained in a project file. A project file has been created for LED_delay.c.

To load this project, select Open Project from the Project menu and open Mplab.mcp from the specific folder if we already generate the Project file. Otherwise you must generate a Project file (Refer Getting Started Chapter).

Create & Debug a project in Mplab using PIC16F877A

5.3. Editing LED_delay.C

You can now edit LED_delay.C. Double click on LED_delay.C in the Files page of the Project Workspace. µVision loads and displays the contents of LED_delay.C in an editor window.

5.4. Compiling and Linking LED_delay

When you are ready to compile and link your project, use the Build Target command from the Project menu.

Mplab begins to translate and link the source files and creates an absolute object module that you can load into the Mplab debugger for testing. The status of the build process is listed in the Build page of the Output Window.

Create & Debug a project in Mplab using PIC16F877A

5.5. Testing LED_delay

Once the LED_delay program is compiled and linked, you can test it with the Mplab SIM debugger. In Mplab, use the Run command from the Debug menu or toolbar. Mplab initializes the debugger and starts program execution till the main function.

During debugging Mplab will show the following output:

Create & Debug a project in Mplab using PIC16F877A

The PORTC registers enabling HIGH in a certain period,

Create & Debug a project in Mplab using PIC16F877A

The PORTC registers enabling LOW in a certain period,

Create & Debug a project in Mplab using PIC16F877A

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.