The steps are as follows:
Step 1 Start the mikroC IDE, making sure the program developed in Example 5.1 is displayed in the Code Editor window.
Step 2 From the drop-down menu select Debugger→Select Debugger→Software PIC Simulator.
Step 3 From the drop-down menu select Run→Start Debugger.
Step 4 Select variables Sum, i, and PORTC from the Watch window as described in Example 5.2.
Step 5 To set a breakpoint at the end of the program, click the mouse at the last closing bracket of the program, which is at line 27, and press F5. As shown in Figure 5.49, you should see a red line at the breakpoint and a little marker in the left column of the Code Editor window.
Figure 5.49: Setting a breakpoint at line 27
Step 6 Now, start the debugger, and press F6 key to run the program. The program will stop at the breakpoint, displaying variables as shown in Figure 5.48.
This is the end of the simulation. Select from drop-down menu Run→Stop Debugger.
To clear a breakpoint, move the cursor over the line where the breakpoint is and then press F5. To clear all breakpoints in a program, press the SHIFT+CTRL+F5 keys. To display the breakpoints in a program, press the SHIFT+F4 keys.
The following are some other useful debugger commands:
Step Into [F7] Executes the current instruction and then halts. If the instruction is a call to a routine, the program enters the routine and halts at the first instruction.
Step Over [F8] Executes the current instruction and then halts. If the instruction is a call to a routine, it skips it and halts at the first instruction following the call.
Step Out [CTRL+F8] Executes the current instruction and then halts. If the instruction is within a routine, it executes the instruction and halts at the first instruction following the call.
Run to Cursor [F4] Executes all instructions between the current instruction and the cursor position.
Jump to Interrupt [F2] Jumps to the interrupt service routine address (address 0x08 for PIC18 microcontrollers) and executes the procedure located at that address.
5.3.4 Using the mikroICD In-Circuit Debugger
This section discusses how to use the mikroICD in-circuit debugger (also called the PICFlash 2 programmer) to debug the program developed in Example 5.1. First of all, we have to build the hardware and then connect the in-circuit debugger device. In this example, the hardware is built on a breadboard, and a PICFlash 2 mikroICD in-circuit debugger is used to debug the system. Note that pins RB6 and RB7 are used by the mikroICD and are not available for I/O while mikroICD is active.
The project’s circuit diagram is shown in Figure 5.50. The mikroICD in-circuit debugger is connected to the development circuit using the following pins of the microcontroller:
• MCLR
• RB6
• RB7
• +5V
• GND
Figure 5.50: Circuit diagram of the project
The mikroICD has two modes of operation. In inactive mode all lines from the microcontroller used by the debugger device are connected to the development system. In active mode the MCLR, RB6, and RB7 pins are disconnected from the development system and used to program the microcontroller. After the programming, these lines are restored.
The mikroICD debugger device has a 10-way IDC connector and can be connected to the target system with a 10-way IDC header. Once the development is finished and the mikroICD debugger is removed, opposite pairs of the IDC header can be connected with jumpers. Figure 5.51 shows the system built on a breadboard.
Figure 5.51: System built on a breadboard
After building the hardware we are ready to program the microcontroller and test the system’s operation with the in-circuit debugger. The steps are as follows:
Step 1 Start the mikroC IDE, making sure the program developed in Example 5.1 is displayed in the Code Editor window.
Step 2 Click the Edit Project button (Figure 5.52) and set DEBUG_ON as shown in Figure 5.53.
Figure 5.52: Edit Project button
Figure 5.53: Set the DEBUG_ON
Step 3 Select ICD Debug in the Project Setup window as shown in Figure 5.54.
Figure 5.54: Select the ICD Debug
Step 4 Click the Build Project icon to compile the program with the debugger. After a successful compilation you should see the message Success (ICD Build) in the Message Window.
Step 5 Make sure the mikroICD debugger device is connected as in Figure 5.50, and select Tools→PicFlash Programmer from the drop-down menu to program the microcontroller.
Step 6 From the drop-down menu select Debugger→Select Debugger→mikroICD Debugger as shown in Figure 5.55.
Figure 5.55: Selecting the mikroICD debugger
Step 7 From the drop-down menu select Run→Start Debugger. The debugger form will pop up and select variables Sum, i, and PORTC as described in Example 5.2.
Step 8 Single-step through the program by pressing the F8 key. You should see the values of variables changing. At the end of the program, decimal value 55 will be sent to PORTC, and LEDs 0,1,2,4, and 5 should be turned ON, as shown in Figure 5.56, corresponding to this number.
Figure 5.56: Decimal number 55 shown in LEDs
Step 9 Stop the debugger.
In routines that contain delays, the Step Into [F7] and Step Over [F8] commands can take a long time. Run to Cursor [F4] and breakpoints should be used instead.
5.3.5 Using a Development Board
It is easy to develop microcontroller-based applications with the help of a development board. This section explains how to use the development board BIGPIC4, described earlier in this chapter. The program written in Example 5.1 is compiled and then loaded to the microcontroller using the on-board mikroICD in-circuit emulator. Then the program runs and displays the sum of the numbers 1 to 10 on the LEDs connected to PORTC.
However, before using the development board we need to know how the BIGPIC4 is organized and how to use the various devices on the board.
Figure 5.57 shows the BIGPIC4 development board with the functions of various devices identified with arrows. The board can be powered either from an external power supply (8-to 16-C AC/DC) or from the USB port of a computer, using a jumper. In this application, the board is powered from a USB port.