SlideShare a Scribd company logo
METU-EEE                                                     EE 494-Engineering Design II

               Tutorial 3: Firmware Debugging with PICKit2


I.   INTRODUCTION

It is a fact that, it does not matter whether you are an engineer or not, everyone does
mistakes in life ☺. We as engineers can skip small details in developing a system for
example. The important thing is that we should figure out and correct those mistakes in
the application we implement. However, sometimes it becomes really frustrating when we
try to find out what the problem really is. We need a procedure more than a “reprogram
and try again” style. That is why “debugging” becomes necessary in our engineering tools.

As you may possibly be familiar from MATLAB programming, you can pause the execution
of your code and investigate the states of each variable you use inside. The debugging
utility you use in MATLAB is a highly featured tool in which you can visually inspect every
item by processing them within functions in the command window or array inspector. With
a step-by-step trace, you can observe and locate the problems in your code and hence fix
them easily. However, you should note that you are using your computer’s resources to
perform this task which includes a highly organized structure when compared to the
hardware limitations of a microcontroller. Therefore, debugging a microcontroller firmware
is usually harder than the software you develop in PC. You need additional components to
perform debugging PICs which is the main subject of this tutorial.

Here, the debugging utility of PICKit2 will be demonstrated with an example using MPLAB
IDE and CCS C compiler. CCS C is a commercial PIC C compiler with very limited 30 day
trial versions; you can buy it if you like. The reason why this compiler is used to
demonstrate the debugging example is to explain also the integration of another compiler
(other than C18 as discussed in tutorial 1) with MPLAB IDE. You can, for sure, use C18 or
other compilers to perform this debugging task.

II. The firmware example

Now let’s implement a simple blinking using PIC16F877. Open MPLAB IDE. Select
Project -> Project Wizard




                                                                                       4-1
METU-EEE                                                   EE 494-Engineering Design II




            Figure 1: Creating a Project in MPLAB with Project Wizard

Click on Next and Select PIC16F877 from the device list.




                 Figure 2: Device selection in creating the Project

Select CCS C toolsuite as the compiler. Check for the directory of the compiler provided
that MPLAB can not locate the toolsuite. Click Next.

                                                                                    4-2
METU-EEE                                                          EE 494-Engineering Design II




                     Figure 3: Selection of the Compiler toolsuite


Create your project directory and the project file. Click Next.




                       Figure 4: Specifying the Project Location



                                                                                          4-3
METU-EEE                                                   EE 494-Engineering Design II



For now, do not add any files to your project; let’s just create an empty project. Click
Next.




                          Figure 5: Addition of existing files

Check your project configuration and finish the wizard.




                                                                                    4-4
METU-EEE                                                      EE 494-Engineering Design II




         Figure 6: Creating an empty project - end of the Project Wizard


Dock your project and output windows. Click on the blank sheet (New File).




            Figure 7: Formatting the Workspace and adding a new file

In the untitled document, copy and paste the following code

                                                                                      4-5
METU-EEE                                                       EE 494-Engineering Design II

#include <16F877.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)

void main()
{
  int k;
  int j;
  double t;

    for (k = 1;k<10;k++)
    {
            output_high(PIN_D2);
            delay_ms(500);
            output_low(PIN_D2);
            delay_ms(500);
            j = 2*k+1;
            t = j/2;
    }
}

Save your file as main.c in your project directory.




               Figure 8: Saving the main program in the project directory


Now, right click on the Source Files->Add Files. Select the file “main.c” you just created.




                                                                                          4-6
METU-EEE                                                       EE 494-Engineering Design II




           Figure 9: Inclusion of the created “main.c” file in the project


Click on “Build All”, and you will see that new files are created in your project window.




                   Figure 10: Building the code with CCS compiler




                                                                                            4-7
METU-EEE                                                     EE 494-Engineering Design II
Now, let’s program this code to our PIC16F877 device. First of all, you should construct
the programming hardware (with PICKit2) discussed in tutorial 2 (Figure 5 of ICSP
tutorial). You should use the PIC16F877 device as the target microcontroller and place a
resistor (10k) and a LED pair to pin D2 of the microcontroller.

Plug your PICKit2 device to one of the USB ports of your PC and connect your application
circuit and PICKit2 with the ICSP cable (an ordinary cable with 6 independent lines). Now,
we will first program the device from MPLAB. In MPLAB, click on “Programmer”, choose
“Select Programmer” and select PICKit2 (Figure 11).




      Figure 11: Programming the target device from MPLAB with PICKit2

Note that your PICKit2 device is identified by MPLAB (in PICKit2 tab, see Figure 12




                                                                                      4-8
METU-EEE                                                       EE 494-Engineering Design II




                   Figure 12: Identification of PICKit2 in MPLAB

Click on the “program the target device” icon (in Figure 13)




                 Figure 13: Using programming toolbar for MPLAB

Once the programming is verified, you can use your PICKit2 as a debugger.



                                                                                       4-9
METU-EEE                                                        EE 494-Engineering Design II




                 Figure 14: Verification of the programmed device


Select “Debugger”, click on “Select Tool” and select PICKit2.




                     Figure 15: Switching PICKit2 to a debugger




                                                                                       4-10
METU-EEE                                                   EE 494-Engineering Design II
You will be reminded that “a programmer and a debugger can not be used at the same
time” and asked to switch to the debugger. Click OK.




                Figure 16: Configuration of PICKit2 as a debugger

Now, your PICKit2 serves as a debugger. Place a breakpoint on line 19 by double clicking
on the gray bar including the line numbers (Figure 17)




                                                                                   4-11
METU-EEE                                                EE 494-Engineering Design II




                         Figure 17: Placing a breakpoint

Click on the “Run” (play) icon on the debugging toolbar to go to line 19. You may be
asked to recompile the code for the debugger as in Figure 18. Select “Yes” for that
message.




           Figure 18: Configuring the firmware for debugging utilities.



                                                                               4-12
METU-EEE                                                EE 494-Engineering Design II
Now, you can see that the operation is halted at line 19 and you can analyze every
variable by placing your mouse on the variables.




                Figure 19: Investigation of variables in debugger

You can also see every variable and physical memory values (including stack, EEPROM
etc.) from “View”->”Program Memory”, “View”->”Locals” etc.




                                                                               4-13
METU-EEE                                         EE 494-Engineering Design II




   Figure 20: Using “Program Memory”, “Locals” and “Watch” windows in
                               debugger




                                                                        4-14
Ad

Recommended

PDF
Keil tutorial
anishgoel
 
PDF
15EE51 - Microcontrollers Laboratory
Jabez Winston
 
PPT
Getting started with code composer studio v4 for tms320 f2812
Pantech ProLabs India Pvt Ltd
 
PDF
Larson and toubro
anoopc1998
 
PPT
Getting started with code composer studio v3.3 for tms320 f2812
Pantech ProLabs India Pvt Ltd
 
DOC
Introduction to C++
rohassanie
 
PDF
174085193 pic-prgm-manual
Arun Shan
 
PDF
Microcontroladores pic lingc unicamp-150206140414-conversion-gate02
Cláudio Alves
 
PPTX
Programmation pic 16F877
Mouna Souissi
 
PDF
Chapitre 3 robotique
Mouna Souissi
 
PDF
Pic microcontroller step by step your complete guide
Ashraf Said AlMadhoun - Educational Engineering Team
 
PPT
Microchip's PIC Micro Controller
Midhu S V Unnithan
 
PDF
Question paper with solution the 8051 microcontroller based embedded systems...
manishpatel_79
 
PPT
PIC 16F877A by PARTHIBAN. S.
parthi_arjun
 
DOC
PIC MICROCONTROLLERS -CLASS NOTES
Dr.YNM
 
PDF
Embedded C programming based on 8051 microcontroller
Gaurav Verma
 
PDF
Esp with thepic16f877
ravi shankar ambati
 
PDF
Advanced View Pic Microcontroller Projects List _ PIC Microcontroller.pdf
Ismailkhan77481
 
PPT
generate IP CORES
guest296013
 
PDF
Micro Processor & Micro Controller Practical Notes_Electrical Engineering GTU...
Parth Gajjar
 
PDF
Advanced View Pic Microcontroller Projects List _ PIC Microcontroller.pdf
WiseNaeem
 
PDF
Altera up1
Emery Laura Ttito
 
PDF
Cp e 214_appendix_c
basavaraju ramaduragam
 
PDF
Embedded c lab and keil c manual
Hari K
 
DOCX
Practical work 1
wkhairil80
 
PDF
ARM-ES MANUAL.pdf
vidhya1806
 
PDF
Pic16f877a microcontroller based projects list _ PIC Microcontroller.pdf
Ismailkhan77481
 
PDF
xapp744-HIL-Zynq-7000
Umang Parekh
 
PDF
Creating first project in mikroC PRO for 8051
inovabrasil
 
PDF
Advanced view pic microcontroller projects list pic microcontroller
WiseNaeem
 

More Related Content

Viewers also liked (9)

PPTX
Programmation pic 16F877
Mouna Souissi
 
PDF
Chapitre 3 robotique
Mouna Souissi
 
PDF
Pic microcontroller step by step your complete guide
Ashraf Said AlMadhoun - Educational Engineering Team
 
PPT
Microchip's PIC Micro Controller
Midhu S V Unnithan
 
PDF
Question paper with solution the 8051 microcontroller based embedded systems...
manishpatel_79
 
PPT
PIC 16F877A by PARTHIBAN. S.
parthi_arjun
 
DOC
PIC MICROCONTROLLERS -CLASS NOTES
Dr.YNM
 
PDF
Embedded C programming based on 8051 microcontroller
Gaurav Verma
 
PDF
Esp with thepic16f877
ravi shankar ambati
 
Programmation pic 16F877
Mouna Souissi
 
Chapitre 3 robotique
Mouna Souissi
 
Pic microcontroller step by step your complete guide
Ashraf Said AlMadhoun - Educational Engineering Team
 
Microchip's PIC Micro Controller
Midhu S V Unnithan
 
Question paper with solution the 8051 microcontroller based embedded systems...
manishpatel_79
 
PIC 16F877A by PARTHIBAN. S.
parthi_arjun
 
PIC MICROCONTROLLERS -CLASS NOTES
Dr.YNM
 
Embedded C programming based on 8051 microcontroller
Gaurav Verma
 
Esp with thepic16f877
ravi shankar ambati
 

Similar to Firmware Debugging with PICkit2 (20)

PDF
Advanced View Pic Microcontroller Projects List _ PIC Microcontroller.pdf
Ismailkhan77481
 
PPT
generate IP CORES
guest296013
 
PDF
Micro Processor & Micro Controller Practical Notes_Electrical Engineering GTU...
Parth Gajjar
 
PDF
Advanced View Pic Microcontroller Projects List _ PIC Microcontroller.pdf
WiseNaeem
 
PDF
Altera up1
Emery Laura Ttito
 
PDF
Cp e 214_appendix_c
basavaraju ramaduragam
 
PDF
Embedded c lab and keil c manual
Hari K
 
DOCX
Practical work 1
wkhairil80
 
PDF
ARM-ES MANUAL.pdf
vidhya1806
 
PDF
Pic16f877a microcontroller based projects list _ PIC Microcontroller.pdf
Ismailkhan77481
 
PDF
xapp744-HIL-Zynq-7000
Umang Parekh
 
PDF
Creating first project in mikroC PRO for 8051
inovabrasil
 
PDF
Advanced view pic microcontroller projects list pic microcontroller
WiseNaeem
 
PDF
Pic programming gettingstarted
Ajit Padmarajan
 
PDF
Advanced View Pic Microcontroller Projects List _ PIC Microcontroller.pdf
WiseNaeem
 
PDF
Programming the ARM CORTEX M3 based STM32F100RBT6 Value Line Discovery Board
Gaurav Verma
 
PDF
Tutorial dec0604(print24) Programming a PIC
Muhammad Khan
 
PDF
Advanced View Pic Microcontroller Projects List _ PIC Microcontroller.pdf
WiseNaeem
 
PDF
Impl installation manual
Alkis Vazacopoulos
 
DOCX
ABC Consolidated Financial InfoABC Companys current financial inf.docx
ransayo
 
Advanced View Pic Microcontroller Projects List _ PIC Microcontroller.pdf
Ismailkhan77481
 
generate IP CORES
guest296013
 
Micro Processor & Micro Controller Practical Notes_Electrical Engineering GTU...
Parth Gajjar
 
Advanced View Pic Microcontroller Projects List _ PIC Microcontroller.pdf
WiseNaeem
 
Altera up1
Emery Laura Ttito
 
Cp e 214_appendix_c
basavaraju ramaduragam
 
Embedded c lab and keil c manual
Hari K
 
Practical work 1
wkhairil80
 
ARM-ES MANUAL.pdf
vidhya1806
 
Pic16f877a microcontroller based projects list _ PIC Microcontroller.pdf
Ismailkhan77481
 
xapp744-HIL-Zynq-7000
Umang Parekh
 
Creating first project in mikroC PRO for 8051
inovabrasil
 
Advanced view pic microcontroller projects list pic microcontroller
WiseNaeem
 
Pic programming gettingstarted
Ajit Padmarajan
 
Advanced View Pic Microcontroller Projects List _ PIC Microcontroller.pdf
WiseNaeem
 
Programming the ARM CORTEX M3 based STM32F100RBT6 Value Line Discovery Board
Gaurav Verma
 
Tutorial dec0604(print24) Programming a PIC
Muhammad Khan
 
Advanced View Pic Microcontroller Projects List _ PIC Microcontroller.pdf
WiseNaeem
 
Impl installation manual
Alkis Vazacopoulos
 
ABC Consolidated Financial InfoABC Companys current financial inf.docx
ransayo
 
Ad

Recently uploaded (20)

PPTX
How payment terms are configured in Odoo 18
Celine George
 
PPTX
IIT KGP Quiz Week 2024 Sports Quiz (Prelims + Finals)
IIT Kharagpur Quiz Club
 
PPTX
Wage and Salary Computation.ppt.......,x
JosalitoPalacio
 
PPTX
CRYPTO TRADING COURSE BY FINANCEWORLD.IO
AndrewBorisenko3
 
PPTX
Birnagar High School Platinum Jubilee Quiz.pptx
Sourav Kr Podder
 
PPTX
June 2025 Progress Update With Board Call_In process.pptx
International Society of Service Innovation Professionals
 
PPTX
How to use search fetch method in Odoo 18
Celine George
 
PPTX
F-BLOCK ELEMENTS POWER POINT PRESENTATIONS
mprpgcwa2024
 
PPTX
Filipino 9 Maikling Kwento Ang Ama Panitikang Asiyano
sumadsadjelly121997
 
PPTX
How to Manage Different Customer Addresses in Odoo 18 Accounting
Celine George
 
PPTX
ENGLISH-5 Q1 Lesson 1.pptx - Story Elements
Mayvel Nadal
 
PPTX
2025 Completing the Pre-SET Plan Form.pptx
mansk2
 
PPTX
List View Components in Odoo 18 - Odoo Slides
Celine George
 
PDF
Romanticism in Love and Sacrifice An Analysis of Oscar Wilde’s The Nightingal...
KaryanaTantri21
 
PDF
English 3 Quarter 1_LEwithLAS_Week 1.pdf
DeAsisAlyanajaneH
 
PPTX
Values Education 10 Quarter 1 Module .pptx
JBPafin
 
PDF
Public Health For The 21st Century 1st Edition Judy Orme Jane Powell
trjnesjnqg7801
 
PPTX
Peer Teaching Observations During School Internship
AjayaMohanty7
 
PDF
Aprendendo Arquitetura Framework Salesforce - Dia 02
Mauricio Alexandre Silva
 
PPTX
Tanja Vujicic - PISA for Schools contact Info
EduSkills OECD
 
How payment terms are configured in Odoo 18
Celine George
 
IIT KGP Quiz Week 2024 Sports Quiz (Prelims + Finals)
IIT Kharagpur Quiz Club
 
Wage and Salary Computation.ppt.......,x
JosalitoPalacio
 
CRYPTO TRADING COURSE BY FINANCEWORLD.IO
AndrewBorisenko3
 
Birnagar High School Platinum Jubilee Quiz.pptx
Sourav Kr Podder
 
June 2025 Progress Update With Board Call_In process.pptx
International Society of Service Innovation Professionals
 
How to use search fetch method in Odoo 18
Celine George
 
F-BLOCK ELEMENTS POWER POINT PRESENTATIONS
mprpgcwa2024
 
Filipino 9 Maikling Kwento Ang Ama Panitikang Asiyano
sumadsadjelly121997
 
How to Manage Different Customer Addresses in Odoo 18 Accounting
Celine George
 
ENGLISH-5 Q1 Lesson 1.pptx - Story Elements
Mayvel Nadal
 
2025 Completing the Pre-SET Plan Form.pptx
mansk2
 
List View Components in Odoo 18 - Odoo Slides
Celine George
 
Romanticism in Love and Sacrifice An Analysis of Oscar Wilde’s The Nightingal...
KaryanaTantri21
 
English 3 Quarter 1_LEwithLAS_Week 1.pdf
DeAsisAlyanajaneH
 
Values Education 10 Quarter 1 Module .pptx
JBPafin
 
Public Health For The 21st Century 1st Edition Judy Orme Jane Powell
trjnesjnqg7801
 
Peer Teaching Observations During School Internship
AjayaMohanty7
 
Aprendendo Arquitetura Framework Salesforce - Dia 02
Mauricio Alexandre Silva
 
Tanja Vujicic - PISA for Schools contact Info
EduSkills OECD
 
Ad

Firmware Debugging with PICkit2

  • 1. METU-EEE EE 494-Engineering Design II Tutorial 3: Firmware Debugging with PICKit2 I. INTRODUCTION It is a fact that, it does not matter whether you are an engineer or not, everyone does mistakes in life ☺. We as engineers can skip small details in developing a system for example. The important thing is that we should figure out and correct those mistakes in the application we implement. However, sometimes it becomes really frustrating when we try to find out what the problem really is. We need a procedure more than a “reprogram and try again” style. That is why “debugging” becomes necessary in our engineering tools. As you may possibly be familiar from MATLAB programming, you can pause the execution of your code and investigate the states of each variable you use inside. The debugging utility you use in MATLAB is a highly featured tool in which you can visually inspect every item by processing them within functions in the command window or array inspector. With a step-by-step trace, you can observe and locate the problems in your code and hence fix them easily. However, you should note that you are using your computer’s resources to perform this task which includes a highly organized structure when compared to the hardware limitations of a microcontroller. Therefore, debugging a microcontroller firmware is usually harder than the software you develop in PC. You need additional components to perform debugging PICs which is the main subject of this tutorial. Here, the debugging utility of PICKit2 will be demonstrated with an example using MPLAB IDE and CCS C compiler. CCS C is a commercial PIC C compiler with very limited 30 day trial versions; you can buy it if you like. The reason why this compiler is used to demonstrate the debugging example is to explain also the integration of another compiler (other than C18 as discussed in tutorial 1) with MPLAB IDE. You can, for sure, use C18 or other compilers to perform this debugging task. II. The firmware example Now let’s implement a simple blinking using PIC16F877. Open MPLAB IDE. Select Project -> Project Wizard 4-1
  • 2. METU-EEE EE 494-Engineering Design II Figure 1: Creating a Project in MPLAB with Project Wizard Click on Next and Select PIC16F877 from the device list. Figure 2: Device selection in creating the Project Select CCS C toolsuite as the compiler. Check for the directory of the compiler provided that MPLAB can not locate the toolsuite. Click Next. 4-2
  • 3. METU-EEE EE 494-Engineering Design II Figure 3: Selection of the Compiler toolsuite Create your project directory and the project file. Click Next. Figure 4: Specifying the Project Location 4-3
  • 4. METU-EEE EE 494-Engineering Design II For now, do not add any files to your project; let’s just create an empty project. Click Next. Figure 5: Addition of existing files Check your project configuration and finish the wizard. 4-4
  • 5. METU-EEE EE 494-Engineering Design II Figure 6: Creating an empty project - end of the Project Wizard Dock your project and output windows. Click on the blank sheet (New File). Figure 7: Formatting the Workspace and adding a new file In the untitled document, copy and paste the following code 4-5
  • 6. METU-EEE EE 494-Engineering Design II #include <16F877.h> #fuses HS,NOWDT,NOPROTECT,NOLVP #use delay(clock=20000000) void main() { int k; int j; double t; for (k = 1;k<10;k++) { output_high(PIN_D2); delay_ms(500); output_low(PIN_D2); delay_ms(500); j = 2*k+1; t = j/2; } } Save your file as main.c in your project directory. Figure 8: Saving the main program in the project directory Now, right click on the Source Files->Add Files. Select the file “main.c” you just created. 4-6
  • 7. METU-EEE EE 494-Engineering Design II Figure 9: Inclusion of the created “main.c” file in the project Click on “Build All”, and you will see that new files are created in your project window. Figure 10: Building the code with CCS compiler 4-7
  • 8. METU-EEE EE 494-Engineering Design II Now, let’s program this code to our PIC16F877 device. First of all, you should construct the programming hardware (with PICKit2) discussed in tutorial 2 (Figure 5 of ICSP tutorial). You should use the PIC16F877 device as the target microcontroller and place a resistor (10k) and a LED pair to pin D2 of the microcontroller. Plug your PICKit2 device to one of the USB ports of your PC and connect your application circuit and PICKit2 with the ICSP cable (an ordinary cable with 6 independent lines). Now, we will first program the device from MPLAB. In MPLAB, click on “Programmer”, choose “Select Programmer” and select PICKit2 (Figure 11). Figure 11: Programming the target device from MPLAB with PICKit2 Note that your PICKit2 device is identified by MPLAB (in PICKit2 tab, see Figure 12 4-8
  • 9. METU-EEE EE 494-Engineering Design II Figure 12: Identification of PICKit2 in MPLAB Click on the “program the target device” icon (in Figure 13) Figure 13: Using programming toolbar for MPLAB Once the programming is verified, you can use your PICKit2 as a debugger. 4-9
  • 10. METU-EEE EE 494-Engineering Design II Figure 14: Verification of the programmed device Select “Debugger”, click on “Select Tool” and select PICKit2. Figure 15: Switching PICKit2 to a debugger 4-10
  • 11. METU-EEE EE 494-Engineering Design II You will be reminded that “a programmer and a debugger can not be used at the same time” and asked to switch to the debugger. Click OK. Figure 16: Configuration of PICKit2 as a debugger Now, your PICKit2 serves as a debugger. Place a breakpoint on line 19 by double clicking on the gray bar including the line numbers (Figure 17) 4-11
  • 12. METU-EEE EE 494-Engineering Design II Figure 17: Placing a breakpoint Click on the “Run” (play) icon on the debugging toolbar to go to line 19. You may be asked to recompile the code for the debugger as in Figure 18. Select “Yes” for that message. Figure 18: Configuring the firmware for debugging utilities. 4-12
  • 13. METU-EEE EE 494-Engineering Design II Now, you can see that the operation is halted at line 19 and you can analyze every variable by placing your mouse on the variables. Figure 19: Investigation of variables in debugger You can also see every variable and physical memory values (including stack, EEPROM etc.) from “View”->”Program Memory”, “View”->”Locals” etc. 4-13
  • 14. METU-EEE EE 494-Engineering Design II Figure 20: Using “Program Memory”, “Locals” and “Watch” windows in debugger 4-14