{"id":3289,"date":"2011-07-31T13:10:51","date_gmt":"2011-07-31T17:10:51","guid":{"rendered":"http:\/\/embedded-lab.com\/blog\/?p=3289"},"modified":"2012-07-30T09:29:16","modified_gmt":"2012-07-30T13:29:16","slug":"in-circuit-debugging-of-pic-microcontrollers","status":"publish","type":"post","link":"https:\/\/embedded-lab.com\/blog\/in-circuit-debugging-of-pic-microcontrollers\/","title":{"rendered":"In-Circuit Debugging of PIC microcontrollers"},"content":{"rendered":"<p>An In-Circuit Debugger (ICD) is a very powerful and effective tool for real-time debugging of a microcontroller-based system at hardware level.\u00a0It allows you\u00a0to run, halt and single step the\u00a0program while the target microcontroller is embedded in\u00a0the actual circuit. Once halted, the\u00a0program variables, Special Function Registers (SFRs), RAM and EEPROM locations can be examined and modified in real-time, thus assists the designer in debugging the firmware and hardware together. In this article, I am going to describe the In-Circuit Debugging technique in PIC microcontrollers, and demonstrate the debugging procedure with a test project using the PIC16F887 microcontroller. Although the operation of most ICDs are similar, here I will be using mikroElektronika&#8217;s <a href=\"http:\/\/www.mikroe.com\/eng\/products\/view\/18\/picflash-with-mikroicd-support\/\" target=\"_blank\">PICFlash with mikroICD<\/a> device in conjunction with the mikroC Pro for PIC compiler for illustrative purpose.<\/p>\n<div id=\"attachment_3297\" style=\"width: 570px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/ICD_title.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3297\" class=\"size-full wp-image-3297\" title=\"ICD_title\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/ICD_title.jpg\" alt=\"\" width=\"560\" height=\"338\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/ICD_title.jpg 560w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/ICD_title-550x331.jpg 550w\" sizes=\"auto, (max-width: 560px) 100vw, 560px\" \/><\/a><p id=\"caption-attachment-3297\" class=\"wp-caption-text\">Use of ICD in debugging PIC programming <\/p><\/div>\n<p><!--more--><\/p>\n<p><strong>Theory<\/strong><\/p>\n<p>An ICD is a hardware device that is connected between a PC and the target microcontroller to debug real-time applications quickly and easily. The ICD is controlled by a debugging application tool running on the PC. The debugging tool allows the designer to slow down the microcontroller operation by inserting breakpoints into the program 0r single-step the program execution. After the execution of a single or a group of instructions, the values of the program variables and internal registers can be examined, and modified if required. This helps to quickly identify bugs and errors in the source code and thus expedite the development process of the project.<\/p>\n<p><strong><em>ICD in PICMicro<\/em><\/strong><\/p>\n<p>In PIC microcontrollers, in-circuit debugging is achieved through the same pins that are used for In-Circuit Serial Programming (ICSP), i.e. ICSPDAT (or PGD), ICSPCLK (or PGC), V<sub>PP<\/sub>\/MCLR (programming mode voltage), V<sub>DD<\/sub> (power supply voltage), and V<sub>SS<\/sub> (Ground). This allows manufacturers to integrate ISCP programming and in-circuit debugging features into a single device.\u00a0During debugging, the ICD device adds an additional block of code (along with the user application) to the target microcontroller&#8217;s program memory, which interacts with the debugging application and sends variable values, internal register values and other details to the PC through\u00a0the same programming cable.<\/p>\n<p>For example, Microchip&#8217;s MPLAB ICD2 and ICD3 are both programmer and real-time debugger for selected PIC MCUs and dsPIC DSCs.\u00a0The functionality of the ICD is controlled by the MPLAB Integrated Development Environment (IDE) application software running on the PC. During debugging all\u00a0available features of the target microcontroller are accessible interactively through the MPLAB IDE interface. These functions allow you to run the program in single-step mode, insert breakpoints into the program, examine the registers and variables, and modify them if desired.<\/p>\n<p>Although an ICD is an invaluable\u00a0debugging tool, it does not come at no cost. The two main\u00a0disadvantages of ICD are: 1) it uses some memory of the\u00a0target microcontroller, and 2) it occupies a few I\/O pins of the target microcontroller to\u00a0communicate with the PC. Therefore, while debugging, the PGD, PGC, and MCLR pins of the target microcontroller cannot be used for any other purposes.<\/p>\n<p><strong><em>PICFlash with mikroICD <\/em><\/strong><\/p>\n<p>The <a href=\"http:\/\/www.mikroe.com\/eng\/products\/view\/18\/picflash-with-mikroicd-support\/\" target=\"_blank\">PICFlash with mikroICD<\/a> is a programmer and in-circuit debugger for PIC12, PIC16, and PIC18 series microcontrollers. It is manufactured by <a href=\"http:\/\/www.mikroe.com\" target=\"_blank\">mikroElektronika<\/a> and is supported by all of its PIC compilers (mikroC, mikroBasic, and mikroPascal). The\u00a0device communicates with PC through an USB interface, which also provides the necessary power for the ICD. ?It connects to the target PIC microcontroller through the PGC, PGD, V<sub>PP<\/sub>\/MCLR and power supply pins. When this device is used as an standalone programmer, the first three pins are released after the HEX file is loaded into the target PIC microcontroller. But if \u00a0it is used for in-circuit debugging, those three pins are used for communicating with the PC and hence cannot be used for any other purposes.<\/p>\n<div id=\"attachment_3313\" style=\"width: 570px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/mikroICD.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3313\" class=\"size-full wp-image-3313\" title=\"mikroICD\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/mikroICD.jpg\" alt=\"\" width=\"560\" height=\"306\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/mikroICD.jpg 560w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/mikroICD-550x300.jpg 550w\" sizes=\"auto, (max-width: 560px) 100vw, 560px\" \/><\/a><p id=\"caption-attachment-3313\" class=\"wp-caption-text\">PICFlash with mikroICD: Programmer + In-Circuit Debugger<\/p><\/div>\n<p><strong><em>Installing software for PICFlash with mikroICD<\/em><\/strong><\/p>\n<p>You can find the detail information about this in the<a href=\"http:\/\/www.mikroe.com\/eng\/downloads\/get\/9\/mikroicd_manual_v102.pdf\" target=\"_blank\"> mikroICD manual<\/a>. The easiest way to setup this device is to download the PIC compiler first. Note that this device does not work with any other party&#8217;s compiler, so you need one of mikroElektronika compilers to make it work. The following steps describes the software installation and setup of <a href=\"http:\/\/www.mikroe.com\/eng\/products\/view\/18\/picflash-with-mikroicd-support\/\" target=\"_blank\">PICFlash with mikroICD<\/a> for <a href=\"http:\/\/www.mikroe.com\/eng\/products\/view\/7\/mikroc-pro-for-pic\/\" target=\"_blank\">mikroC Pro for PIC<\/a> compiler.<\/p>\n<ol>\n<li><span style=\"color: #800000;\">First, download mikroC Pro for PIC compiler from\u00a0<a href=\"http:\/\/www.mikroe.com\/eng\/downloads\/get\/29\/mikroc_pro_pic_2011_v501.zip\" target=\"_blank\">HERE<\/a> and install it on your PC.<\/span><\/li>\n<li><span style=\"color: #800000;\">When the installation is complete, it will ask if you want to install mikroProg Suite for PIC. Say yes, and install that too.<\/span><\/li>\n<li><span style=\"color: #800000;\">Next, it will ask if you want to install the drivers for\u00a0mikroProg\u00a0programmer. Say yes, and select the driver file suitable to your operating system, and install it. You are done!<\/span><\/li>\n<\/ol>\n<p><strong><em>Using <a href=\"http:\/\/www.mikroe.com\/eng\/products\/view\/18\/picflash-with-mikroicd-support\/\" target=\"_blank\">PICFlash with mikroICD<\/a> for Debugging<\/em><\/strong><\/p>\n<p>The first step of using the mikroICD is writing an application program in the chosen compiler, say mikroC Pro for PIC. The compiler&#8217;s IDE provides debugging functions such as running the program step by step, pausing the program execution using breakpoints, examining the state of the internal registers, tracking the values of variables in the program, etc. Please read the<a href=\"http:\/\/www.mikroe.com\/eng\/downloads\/get\/9\/mikroicd_manual_v102.pdf\" target=\"_blank\"> mikroICD manual<\/a> for further detail. I will be illustrating the debugging procedure with our test project that uses PIC16F887 microcontroller and LM34DZ sensor to measure the ambient temperature and display it on LCD. The circuit diagram for this project is shown below and is self-explanatory. The way it works is every time the Read Temp. switch is pressed, the microcontroller reads the temperature sample from the LM34DZ and displays it on the LCD. You can see that under default condition, the switch output is High, and it goes Low when the switch is pressed. I am using mikroElektronika&#8217;s<a href=\"http:\/\/www.mikroe.com\/eng\/products\/view\/305\/pic-ready1-board\/\" target=\"_blank\"> PIC Ready 1 board<\/a> for this experimental setup. It is a quick prototyping board for 28 and 40 pin PIC microcontrollers. The board comes with a PIC16F887 microcontroller, preloaded with a serial bootloader, which is actually not applicable in our case. However, the board does contain connectors for\u00a0<a href=\"http:\/\/www.mikroe.com\/eng\/products\/view\/18\/picflash-with-mikroicd-support\/\">PICFlash with mikroICD<\/a> too. The <a href=\"http:\/\/www.mikroe.com\/eng\/downloads\/get\/1264\/pic_ready1_manual_v100.pdf\" target=\"_blank\">user&#8217;s manual for PIC Ready 1 board<\/a> describes more about the on-board connections.<\/p>\n<div id=\"attachment_3293\" style=\"width: 560px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/DebuggingmikroICD_Ckt.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3293\" class=\"size-medium wp-image-3293\" title=\"DebuggingmikroICD_Ckt\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/DebuggingmikroICD_Ckt-550x405.png\" alt=\"\" width=\"550\" height=\"405\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/DebuggingmikroICD_Ckt-550x405.png 550w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/DebuggingmikroICD_Ckt.png 769w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/a><p id=\"caption-attachment-3293\" class=\"wp-caption-text\">Circuit diagram for debugging a PIC16F887 microcontroller system<\/p><\/div>\n<div id=\"attachment_3299\" style=\"width: 570px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/ExperimentalSetup.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3299\" class=\"size-full wp-image-3299\" title=\"ExperimentalSetup\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/ExperimentalSetup.jpg\" alt=\"\" width=\"560\" height=\"433\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/ExperimentalSetup.jpg 560w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/ExperimentalSetup-550x425.jpg 550w\" sizes=\"auto, (max-width: 560px) 100vw, 560px\" \/><\/a><p id=\"caption-attachment-3299\" class=\"wp-caption-text\">Experimental setup: PIC-Ready1 board and PICFlash with mikroICD<\/p><\/div>\n<p>So let me briefly describe the whole\u00a0scenario\u00a0once again, so that it would be more clear what we are trying to do here. We have a PIC16F887 microcontroller circuit that is set up to read the analog output of LM34DZ sensor, convert it into temperature (\u00b0F), and display it on a LCD. The microcontroller is connected to a mikroICD (PIC programmer + ICD) through ICSP pins. The mikroICD is also connected to the PC through USB port. The mikroICD debugging tool is embedded into the mikroC Pro for PIC compiler and hence can be operated from there. We will use the debugging tool for single stepping of the program loaded into the microcontroller and watch the variable values. Meanwhile, with the execution of each instruction, its effect can also be observed in the real hardware circuit.<\/p>\n<p><strong>mikroICD Debugging procedure<\/strong><\/p>\n<p>First of all create a new project with the following source code to read the LM34DZ output and display the measured temperature on the LCD screen. If this is your first experience with mikroC compiler, please read <a href=\"http:\/\/www.mikroe.com\/eng\/downloads\/get\/34\/1st_project_c_pro_pic_v101.pdf\" target=\"_blank\">Creating first project in mikroC Pro for PIC<\/a> to learn how to create a new project in mikroC Pro for PIC. Before compiling, go to Edit Project window and select the build type as ICD Debug. This option will let the mikroICD to load the debug code along with the user application into the microcontroller.<\/p>\n<pre>\r\n<div id=\"_mcePaste\">\/*\r\n * Project name:\r\n     Testing mikroICD debugging tool with PIC16F887\r\n * Copyright:\r\n     (c) Rajendra Bhatt\r\n * Test configuration:\r\n     MCU:             PIC16F887\r\n     Oscillator:      HS, 10.0000 MHz\r\n*\/\r\n\r\n\/\/ LCD module connections\r\nsbit LCD_RS at RC3_bit;\r\nsbit LCD_EN at RC2_bit;\r\nsbit LCD_D4 at RC4_bit;\r\nsbit LCD_D5 at RC5_bit;\r\nsbit LCD_D6 at RC6_bit;\r\nsbit LCD_D7 at RC7_bit;\r\n\r\nsbit LCD_RS_Direction at TRISC3_bit;\r\nsbit LCD_EN_Direction at TRISC2_bit;\r\nsbit LCD_D4_Direction at TRISC4_bit;\r\nsbit LCD_D5_Direction at TRISC5_bit;\r\nsbit LCD_D6_Direction at TRISC6_bit;\r\nsbit LCD_D7_Direction at TRISC7_bit;\r\n\/\/ End LCD module connections\r\n\r\nchar txt[] = \"Temp =      F\";\r\nchar *temp = \"000.0\";\r\nsbit PressSwitch at RC0_bit;\r\nunsigned int ADC_Value;\r\nvoid main() {\r\n\r\n  ANSEL  = 0x01;                 \/\/ Configure RA0 as Analog input\r\n  ANSELH = 0x00;\r\n  TRISA  = 0xff;\r\n  C1ON_bit = 0;                  \/\/ Disable comparators\r\n  C2ON_bit = 0;\r\n  TRISC = 0x01;                  \/\/ RC0 is input\r\n  TRISD = 0x00;\r\n  TRISB = 0xFF;\r\n  txt[11] = 223;                 \/\/ ASCII value for degree symbol\r\n  Lcd_Init();                    \/\/ Initialize LCD\r\n  Lcd_Cmd(_LCD_CLEAR);           \/\/ Clear display\r\n  Lcd_Cmd(_LCD_CURSOR_OFF);      \/\/ Cursor off\r\n  Lcd_Out(1,1,txt);              \/\/ Write text in first row\r\n\r\n  do {\r\n\r\n    if (!PressSwitch) {               \/\/ Detect logical 0\r\n      Delay_ms(300);\r\n      ADC_Value = ADC_Read(0);\r\n      ADC_Value = ADC_Value*4.88;     \/\/ Convert to temperature in F\r\n      temp[0] = adc_value\/1000 + 48;\r\n      temp[1] = (adc_value\/100)%10 + 48;\r\n      temp[2] = (adc_value\/10)%10 + 48;\r\n      temp[4] = adc_value%10 + 48;\r\n      Lcd_Out(1,7,temp);\r\n    }\r\n\r\n  } while(1);                         \/\/ Endless loop\r\n}<\/div><\/pre>\n<p style=\"text-align: center;\"><a href=\"http:\/\/embedded-lab.com\/uploads\/HexFiles\/PIC16F887\/ICD_Test.zip\" target=\"_blank\">Download mikroC project files<\/a><\/p>\n<div id=\"attachment_3302\" style=\"width: 580px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/EditProjectWindow.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3302\" class=\"size-full wp-image-3302\" title=\"EditProjectWindow\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/EditProjectWindow.jpg\" alt=\"\" width=\"570\" height=\"378\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/EditProjectWindow.jpg 570w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/EditProjectWindow-550x364.jpg 550w\" sizes=\"auto, (max-width: 570px) 100vw, 570px\" \/><\/a><p id=\"caption-attachment-3302\" class=\"wp-caption-text\">Selecting ICD Debug for build type allows in-circuit debugging<\/p><\/div>\n<p>You save this scheme and then click on <strong>Build + Program<\/strong> (or hit F4) to compile the program and load it into the target microcontroller. The mikroICD must be connected to the PC&#8217;s USB port as well as to the target microcontroller. It will automatically launch <a href=\"http:\/\/www.mikroe.com\/eng\/downloads\/get\/1201\/mikroprog_suite_for_pic_v210.zip\"><strong>mikroProg Suite for PIC<\/strong><\/a>, which is the application software required on the PC to transfer the HEX file into the micrcontroller. Now the application and the debug code are both loaded, it is time to use the debugging functions and see how it works.<\/p>\n<p>To start debugging, select the <em>Start Debugger<\/em> option from the <em>Run<\/em> drop-down menu or use the function key [F9]. Now you can execute the program line by line by pressing [F8] key. During execution, the program line to be executed next is highlighted in blue by default (see figure below).<\/p>\n<div id=\"attachment_3306\" style=\"width: 539px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/StartWithMain.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3306\" class=\"size-full wp-image-3306 \" title=\"StartWithMain\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/StartWithMain.jpg\" alt=\"\" width=\"529\" height=\"266\" \/><\/a><p id=\"caption-attachment-3306\" class=\"wp-caption-text\">Next instruction to be executed is highlighted in blue<\/p><\/div>\n<p>The real-time execution of the program can be monitored on the <em>Watch Window<\/em>. You can select whatever registers and variables you want to monitor and add them to the <em>Watch Window<\/em>. Go to <em>View -&gt; Debug Windows -&gt; Watch Window<\/em> option to get the Watch window. Select the variables that you want to observe from the variable list and add them to the window by clicking on the + sign shown. In the figure below, I am monitoring four variables during debugging. They are <em>ADC_Value, PressSwitch, ADRESH<\/em>, and <em>ADRESL<\/em>. The <em>ADC_Value<\/em> is the user defined integer variable that stores the 10-bit A\/D result. <em>PressSwitch<\/em> is the input port RC0, which is high because the tact switch is open. <em>ADRESH<\/em> and <em>ADRESL<\/em> are 8-bit internal registers that store the 10-bit A\/D conversion result. The least significant 8-bits are stored in <em>ADRESL<\/em> and the remaining 2 most significant bits are stored in <em>ADRESH<\/em>.<\/p>\n<div id=\"attachment_3308\" style=\"width: 453px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/WatchWindow.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3308\" class=\"size-full wp-image-3308\" title=\"WatchWindow\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/WatchWindow.jpg\" alt=\"\" width=\"443\" height=\"295\" \/><\/a><p id=\"caption-attachment-3308\" class=\"wp-caption-text\">Adding registers and variables to watch window<\/p><\/div>\n<p>As I mentioned earlier, in in-circuit debugging whenever an instruction is executed, its effect is also observed in the real hardware circuit. For example, when the <em>LCD_Init()<\/em> routine is executed, a blinking cursor appears on the LCD screen. This tells you that the <em>LCD_Init()<\/em> routine and your LCD hardware interface are both working well.<\/p>\n<div id=\"attachment_3304\" style=\"width: 522px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/LCD_Init.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3304\" class=\"size-full wp-image-3304 \" title=\"LCD_Init\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/LCD_Init.jpg\" alt=\"\" width=\"512\" height=\"373\" \/><\/a><p id=\"caption-attachment-3304\" class=\"wp-caption-text\">A blinking cursor appears when LCD_Init routine is executed<\/p><\/div>\n<p>Keep hitting on the [F8] key until the<em> Lcd_Out(1, 1, txt)<\/em> is executed, and you will see the &#8216;txt&#8217; string displayed on the LCD. Carry on hitting the [F8] function key and the single-stepping will continue until the <em>if (!PressSwitch)<\/em> condition is arrived. By default, the <em>Read Temp<\/em> switch is open and <em>PressSwitch<\/em> is &#8216;1&#8217;, so the <em>If condition<\/em> is not met. Therefore, if you hit the [F8] key again, the execution will jump to the <em>while(1)<\/em> statement. It will continue doing back and forth between <em>if <\/em>and <em>while<\/em> statements until you press the Read Temp switch on the circuit board.<\/p>\n<div id=\"attachment_3301\" style=\"width: 580px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/DoWhile.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3301\" class=\"size-full wp-image-3301 \" title=\"DoWhile\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/DoWhile.jpg\" alt=\"\" width=\"570\" height=\"378\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/DoWhile.jpg 570w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/DoWhile-550x364.jpg 550w\" sizes=\"auto, (max-width: 570px) 100vw, 570px\" \/><\/a><p id=\"caption-attachment-3301\" class=\"wp-caption-text\">Waits until the Read Temp key is pressed<\/p><\/div>\n<p>When the<em> if(!PressSwitch)<\/em> instruction is highlighted in blue, press the <em>Read Temp<\/em> switch and hit [F8], and the execution gets inside the if statement. The execution will be single-stepping again with pressing of [F8] key. When the <em>ADC_Read(0)<\/em> is executed, the values of<em> ADC_Value<\/em>,<em> ADRESH<\/em>, and <em>ADRESL<\/em> also change in the <em>Watch Window<\/em>.<\/p>\n<div id=\"attachment_3303\" style=\"width: 570px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/InsideLoop.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3303\" class=\"size-full wp-image-3303 \" title=\"InsideLoop\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/InsideLoop.jpg\" alt=\"\" width=\"560\" height=\"136\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/InsideLoop.jpg 560w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/InsideLoop-550x133.jpg 550w\" sizes=\"auto, (max-width: 560px) 100vw, 560px\" \/><\/a><p id=\"caption-attachment-3303\" class=\"wp-caption-text\">Execution within the if statement begins upon pressing the Read Temp button (PressSwitch = 0)<\/p><\/div>\n<div id=\"attachment_3300\" style=\"width: 570px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/ADC_Value.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3300\" class=\"size-full wp-image-3300 \" title=\"ADC_Value\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/ADC_Value.jpg\" alt=\"\" width=\"560\" height=\"151\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/ADC_Value.jpg 560w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/ADC_Value-550x148.jpg 550w\" sizes=\"auto, (max-width: 560px) 100vw, 560px\" \/><\/a><p id=\"caption-attachment-3300\" class=\"wp-caption-text\">ADC_Value in Watch Window shows the ADC result<\/p><\/div>\n<p>Next, the <em>ADC_Value<\/em> and <em>temp<\/em> variables are updated and the temperature in \u00b0F is displayed on the LCD when <em>Lcd_Out(1, 7, temp)<\/em> is executed.<\/p>\n<div id=\"attachment_3305\" style=\"width: 570px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/LCDDisplay.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3305\" class=\"size-full wp-image-3305 \" title=\"LCDDisplay\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/LCDDisplay.jpg\" alt=\"\" width=\"560\" height=\"411\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/LCDDisplay.jpg 560w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/LCDDisplay-550x403.jpg 550w\" sizes=\"auto, (max-width: 560px) 100vw, 560px\" \/><\/a><p id=\"caption-attachment-3305\" class=\"wp-caption-text\">Displaying ADC_Value on LCD<\/p><\/div>\n<p>Thus, in this simple example, we saw how the PIC16F887 microcontroller was slowed down to single-step mode during in-circuit debugging, and the program variables are watched at the execution of each program line. In case the application program is too big, say hundreds of lines of instructions, single-step debugging will be very time-consuming and inconvenient. To deal with those circumstances, the mikroICD debugging tool offers<em> run to cursor <\/em>[F4] and <em>breakpoint<\/em> features to fasten the debugging process. With [F4] function key, you can let the execution of the program until reaching the cursor position. Similarly, the use of breakpoints allows to place intentional stops at desired program lines. The summary of various debug function keys in mikroICD are shown in the table below. Besides, during debugging you can also modify the values of the program variables from the <em>Watch Window<\/em>. The more advanced features of mikroICD debugging tool are described in the <strong><a href=\"http:\/\/www.mikroe.com\/eng\/downloads\/get\/9\/mikroicd_manual_v102.pdf\" target=\"_blank\">user&#8217;s manual<\/a><\/strong>.<\/p>\n<div id=\"attachment_3320\" style=\"width: 590px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/DebuggKeys.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3320\" class=\"size-full wp-image-3320 \" title=\"DebuggKeys\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/07\/DebuggKeys.png\" alt=\"\" width=\"580\" height=\"418\" \/><\/a><p id=\"caption-attachment-3320\" class=\"wp-caption-text\">Major function keys in mikroICD debugging tool<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>An In-Circuit Debugger (ICD) is a very powerful and effective tool for real-time debugging of a microcontroller-based system at hardware level.\u00a0It allows you\u00a0to run, halt and single step the\u00a0program while the target microcontroller is embedded in\u00a0the actual circuit. Once halted, the\u00a0program variables, Special Function Registers (SFRs), RAM and EEPROM locations can be examined and modified in real-time, thus assists the designer in debugging the firmware and hardware together. In this article, I am going to describe the In-Circuit Debugging technique in PIC microcontrollers, and demonstrate the debugging procedure with a test project using the PIC16F887 microcontroller. Although the operation of<\/p>\n","protected":false},"author":1,"featured_media":3293,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32,59,38,183],"tags":[313,314,315,316],"class_list":["post-3289","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-embedded-labs","category-microcontroller-programmers","category-pic-tutorials","category-tips-and-tricks","tag-icd","tag-in-circuit-debugging","tag-mikroicd","tag-pic16f887"],"_links":{"self":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/3289","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/comments?post=3289"}],"version-history":[{"count":53,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/3289\/revisions"}],"predecessor-version":[{"id":3355,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/3289\/revisions\/3355"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media\/3293"}],"wp:attachment":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media?parent=3289"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/categories?post=3289"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/tags?post=3289"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}