20
© 2007 Microchip Technology Inc. Page 1 of 18 PICkit™ 2 Debug Express Getting Started HI-TECH PICC-Lite C Compiler Overview This document provides a guide to creating a C language project in MPLAB ® IDE using HI- TECH’s PICC-Lite C Compiler for PIC ® MCUs. Programming the project and debugging in C are also covered. Please note that this document is not intended as an introduction to or tutorial on the C programming language, and assumes that the reader is already familiar with C programming. Creating a PICC-Lite C project in MPLAB IDE 1. Ensure all necessary software and files are installed. This includes the MPLAB IDE, The HI-TECH PICC-Lite Compiler, and the lessons for the Debug Express 44-Pin Demo Board. See the Quick Start guide for Debug Express on the PICkit Starter Kit CDROM, and click the link for the section titled “Quick Start in C with the HI-TECH PICC-Lite Compiler”. 2. Connect the PICkit™ 2 Microcontroller Programmer to the PC with the included USB cable. Plug the 6-pin header on the demo board into the PICkit 2 ICSP™ connector. 3. Start MPLAB IDE from the shortcut icon on the desktop.

DBE Getting Started With PICkit 2 App

Embed Size (px)

Citation preview

Page 1: DBE Getting Started With PICkit 2 App

© 2007 Microchip Technology Inc. Page 1 of 18

PICkit™ 2 Debug ExpressGetting Started

HI-TECH PICC-Lite C Compiler

Overview

This document provides a guide to creating a C language project in MPLAB® IDE using HI-TECH’s PICC-Lite C Compiler for PIC® MCUs. Programming the project and debugging in C are also covered.

Please note that this document is not intended as an introduction to or tutorial on the C programming language, and assumes that the reader is already familiar with C programming.

Creating a PICC-Lite C project in MPLAB IDE

1. Ensure all necessary software and files are installed. This includes the MPLAB IDE, The HI-TECH PICC-Lite Compiler, and the lessons for the Debug Express 44-Pin Demo Board. See the Quick Start guide for Debug Express on the PICkit Starter Kit CDROM, and click the link for the section titled “Quick Start in C with the HI-TECH PICC-Lite Compiler”.

2. Connect the PICkit™ 2 Microcontroller Programmer to the PC with the included USB cable. Plug the 6-pin header on the demo board into the PICkit 2 ICSP™ connector.

3. Start MPLAB IDE from the shortcut icon on the desktop.

Page 2: DBE Getting Started With PICkit 2 App

PICkit™ 2 Getting Started with the PICC-Lite C Compiler

© 2007 Microchip Technology Inc. Page 2 of 20

4. From the MPLAB IDE menu bar, select Project > Project Wizard…

5. This opens up the Project Wizard. Click Next to continue.

6. Wizard Step One: The project target device is the PIC16F887 included on the 44-Pin Demo Board. In the wizard, select the “PIC16F887” from the drop-down box and click Next.

Page 3: DBE Getting Started With PICkit 2 App

PICkit™ 2 Getting Started with the PICC-Lite C Compiler

© 2007 Microchip Technology Inc. Page 3 of 20

7. Wizard Step Two: Here we select that we want to use the “HI-TECH Universal ToolSuite” for the project language toolsuite from the Active Toolsuite drop-down box. Click Next and continue.

Note:If you receive the error “One or more of your tool paths do not point to existing files…”:The red “X” next to the “HI-TECH C Compiler” in the Toolsuite Contents box indicates thatMPLAB IDE needs the location of the tool executable. Next to the blank “Location” box, click the Browse… button. Navigate to the PICC-Lite install directory C:\Program Files\HI-TECH Software\PICC\LITE\9.60\bin\ and select the compiler executable picl.exe. Click Open.The tool executable path C:\Program Files\HI-TECH Software\PICC\LITE\9.60\bin\picl.exe will appear in the “Location” box and the red “X” will disappear. Click Next to continue.

Page 4: DBE Getting Started With PICkit 2 App

PICkit™ 2 Getting Started with the PICC-Lite C Compiler

© 2007 Microchip Technology Inc. Page 4 of 20

8. Wizard Step Three: Select a directory for the project. The C code file that we will use for this project is installed with the Debug Express 44-Pin Demo Board Lesson Files. Select the Browse… button and navigate to the folder C:\Pk2 Lessons\44Pin Demo Board\C Projects\HI-TECH as the project directory. Give the project file a name, such as “HITECH_C_Demo” and Click Save. Click Next.

9. Wizard Step Four: Add the existing C code demo file to the project. The file tree view box on the left should already be expanded to the project directory, where the demo file HITECH_44P_Demo.C was installed as part of the Debug Express Lessons. Select the file and click the Add >> button to add it to the project. Click Next when done.

Page 5: DBE Getting Started With PICkit 2 App

PICkit™ 2 Getting Started with the PICC-Lite C Compiler

© 2007 Microchip Technology Inc. Page 5 of 20

10. Wizard Summary: Click the Finish button. A new workspace and project are created in the MPLAB IDE. A “workspace” contains information on the selected PIC MCU device, the active programmer and/or debugger, open windows and their location, and other IDE configuration settings. The workspace is also associated with a “project”, which contains the files needed to build an application (source code, include files, linker scripts, etc.) along with associated language (compiler) tools and build options.

Select View > Project to open the project window. The project window displays the projectfiles and is shown below on the left with the source file we added in the Project Wizard, HITECH_44P_Demo.C.Select menu item View > Output to open the output window, shown below on the left. The output window displays the results from various associated tools, including the PICC-LiteCompiler under the “Build” tab and the PICkit 2 under the “PICkit 2” tab (not yet shown).

Page 6: DBE Getting Started With PICkit 2 App

PICkit™ 2 Getting Started with the PICC-Lite C Compiler

© 2007 Microchip Technology Inc. Page 6 of 20

11. One more file needs to be added to the project, the PICC-Lite PIC16F887 header file which contains device specific declarations. In the project window, right-click on the “Header Files” folder and select “Add Files…”

Navigate to the HI-TECH Compiler installation folder C:\Program Files\HI-TECH Software\PICC\LITE\9.60\include and select the pic16f887.h file. Click Open.

12. Project setup is now complete! Select File > Save Workspace to save the workspace and project.

Page 7: DBE Getting Started With PICkit 2 App

PICkit™ 2 Getting Started with the PICC-Lite C Compiler

© 2007 Microchip Technology Inc. Page 7 of 20

Exploring the Demo C Code

The demonstration C program HITECH_44P_Demo.C creates a “bar graph” display of the 8 LEDs on the 44-Pin Demo Board, with the number of lighted LEDs corresponding to the position of the potentiometer RP1. Pressing the switch SW1 reverses the LED display direction.

The program demonstrates configuring and using IO port pins, converting an analog voltage from the potentiometer with the PIC16F887 ADC peripheral, using the Timer0 module, and enabling and servicing Interrupts.

The schematic for the 44-Pin Demo Board may be found in the Appendix of the 44-Pin Demo Board User’s Guide. It will also be helpful to reference the PIC16F88x Data Sheet for PIC16F887 operation, peripheral, and register definitions.

1. To open the program code in an MPLAB IDE editor window, double-click on the file name in the Project window.

Page 8: DBE Getting Started With PICkit 2 App

PICkit™ 2 Getting Started with the PICC-Lite C Compiler

© 2007 Microchip Technology Inc. Page 8 of 20

2. Documentation is provided with the HI-TECH PICC-Lite Compiler. To view the Help documentation, select the Windows Start menu > All Programs > HI-TECH Software > PICC LITE v9.60LP2 > User manual.pdf

3. The PICC-Lite compiler contains some general macro functions for accessing features of PIC microcontroller. These include the __CONFIG() macro for specifying the PIC Configuration Word(s), the ei() and di() functions for enabling and disabling global interrupts, and the interrupt keyword for declaring the interrupt service function. See the manual referenced above for more information. The variables declared for access to the PIC16F887 Special Function Registers (SFRs) and the __CONFIG() arguments are contained in the pic16f887.h header file that was added to the project. Open the header file by double-clicking it in the MPLAB IDE Project Window. Variables used in the demo C program to access SFRs and SFR bits are named with ALL CAPS.

For the functions of the various Special Function Registers, please refer to the PIC16F882/883/884/886/887 Data Sheet.

Information on data types, pre-processor directives, and C library functions may be found in the PICC-Lite User Manual under the “Library Functions” heading.

Page 9: DBE Getting Started With PICkit 2 App

PICkit™ 2 Getting Started with the PICC-Lite C Compiler

© 2007 Microchip Technology Inc. Page 9 of 20

4. Note the macro __CONFIG() is used in our demo C program to set the PIC16F887 configuration word bits. This requires including the PICC-Lite header file <htc.c>. Once we build the project, the configuration settings may also be viewed by selecting Configure > Configuration Bits…. IMPORTANT: Configuration word settings may be changed in the Configuration Bits dialog, but any changes will revert to the settings in the code whenever the project is built.

The 2 basic data types used in the demo project are ‘char’ which is an 8-bit number that fits in a single File Register and a 1-bit bitfield used in the adc_result struct.

5. The demo C Program first sets up the IO pins using the function init_io() which writes to the TRISx Special Function Registers. This sets up which pins are inputs and outputs. Unused pins are set to outputs so they are not “floating”. The IO ports are covered in detail in Section 3 of the PIC16F88x Data Sheet.

6. The PIC16F887 internal Analog to Digital Converter (ADC) is used to read the analog voltage from the potentiometer RP1. The demo C program function init_adc() sets up the ADC and starts the first conversion. The ADC module function is covered in detail in Section 9 of the PIC16F88x Data Sheet.

7. The PIC16F887 Timer 0 module is set up to so that it counts up to 255 and rolls over back to 0 (zero) about every 65.5ms. An interrupt is enabled for the timer which will be generated every time it overflows. This interrupt will cause another reading of the ADC to be made. To specify to the compiler that the function interrupt_service() is called on an interrupt, the keyword interrupt is used in the function definition.

void interrupt interrupt_service(void)

To find out which PIC peripheral generated the interrupt, it is necessary to test the peripheral interrupt flags and enables in the interrupt function. For Timer0, we test its enable flag TOIEand interrupt flag T0IF, which are in the INTCON SFR. If there were more than one interrupt source, the flags for the other possible sources would be tested as well to determine the source of the interrupt and execute the proper statements or functions to handle it. Note

Page 10: DBE Getting Started With PICkit 2 App

PICkit™ 2 Getting Started with the PICC-Lite C Compiler

© 2007 Microchip Technology Inc. Page 10 of 20

that after acting on the interrupt from Timer0, the timer interrupt flag T0IF is cleared. Many PIC microcontroller interrupt flags must be cleared in software before exiting the interrupt function. For further information on PIC microcontroller interrupts see Section 14.3 of the PIC16F88x Data Sheet.

8. Finally, the main() body of code sits in an infinite while loop, where it continually checks for a new ADC conversion result from the interrupt service routine, checks for the switch to be pressed, and updates the 8 bit LED display.

Compiling and Programming the Demo C Program

1. To compile and build project code, select Project > Build from the MPLAB IDE menu bar.

The results of the Compile will appear under the “Build” tab of the Output Window, and the compiled code is now ready to program in the PIC16F887.

2. Ensure the PICkit 2 Microcontroller Programmer and 44-Pin Demo Board are connected as shown on Page 1 of this document. From the MPLAB IDE menu bar select Programmer > Select Programmer > PICkit 2.

The Output Window shows connection to the PICkit 2 Microcontroller Programmer, and that the target microcontroller (a PIC16F887 on the 44-Pin Demo Board) was found.

Page 11: DBE Getting Started With PICkit 2 App

PICkit™ 2 Getting Started with the PICC-Lite C Compiler

© 2007 Microchip Technology Inc. Page 11 of 20

If the PICkit 2 or demo board was not plugged in, connect them and select the MPLAB IDE menu Programmer > Connect to try connecting again.

3. Select menu Programmer > Program to program the compiled code into the PIC16F887.

The results of the programming operation will appear in the Output Window under the PICkit 2 tab.

4. The demo code is now executing in the PIC MCU on the 44-Pin Demo Board! The potentiometer RP1 may be turned to change the length of the LED bar display, and the button SW1 pressed to reverse the bar display.

Page 12: DBE Getting Started With PICkit 2 App

PICkit™ 2 Getting Started with the PICC-Lite C Compiler

© 2007 Microchip Technology Inc. Page 12 of 20

Debugging the Demo C Program

1. First, select PICkit 2 as the debugger tool, Debugger > Select Tool > PICkit 2.

This will automatically disable PICkit 2 as the Programmer since it is not able to act as both a programmer and debugger at the same time.

The output window will again show the connection to the PICkit 2 and the target device as when it was selected as a programmer.

2. To compile and build the project, select Project > Build from the MPLAB IDE menu bar.

3. Program the compiled code to the target MCU by selecting menu Debugger > Program.

PICkit 2 cannot a debug a target when the /MCLR pin is disabled. In the C program file, the configuration bits are set for /MCLR disabled (__CONFIG(INTCLK & WDTDIS & PWRTDIS & MCLRDIS &…) so we will get a warning from the MPLAB IDE as shown below. Click OK to enable the MCLR pin.

4. The Output Window will show the results of programming and indicate that Debug mode on the PIC16F887 was successfully entered.

Page 13: DBE Getting Started With PICkit 2 App

PICkit™ 2 Getting Started with the PICC-Lite C Compiler

© 2007 Microchip Technology Inc. Page 13 of 20

5. To run the program in Debug Mode, select Debugger > Run.

The Output window will display “Running Target”, and the code will begin executing. On the demo board, RP1 may be turned to change the LED display and SW1 pressed to reverse it. Turn RP1 until 4 LEDs are lighted.

6. Select Debugger > Halt to stop the program. The program will stop at the line of code it was executing when it was halted.

A green arrow on the right side of the editor window points to this line of code. Note that you may see it stop at a different location than shown below.

Page 14: DBE Getting Started With PICkit 2 App

PICkit™ 2 Getting Started with the PICC-Lite C Compiler

© 2007 Microchip Technology Inc. Page 14 of 20

7. If “Run” is selected again, the program will resume executing from the point at which itwas stopped. For this step, however, select Debugger > Reset > Processor Reset. This will reset the processor so that code execution will start back at the beginning of the program.

8. A breakpoint may be set on a specific line of code to automatically halt program execution when the program reaches the breakpoint. We’ll set a breakpoint on the interrupt service function “interrupt_service()” by clicking to place the cursor on the code line “adc_conversion.value = ADRESH;”. Right click on the code line to bring up the context menu and select “Set Breakpoint.”

Page 15: DBE Getting Started With PICkit 2 App

PICkit™ 2 Getting Started with the PICC-Lite C Compiler

© 2007 Microchip Technology Inc. Page 15 of 20

A red “stop sign” symbol with a “B” will appear to next to the code line, indicating that a break point has been set.

9. Select Debugger > Run to start program execution. The first time Timer0 overflows,generating an interrupt, the program will halt at the breakpoint. The green arrow indicates that the program is stopped at the code line with the breakpoint.

Page 16: DBE Getting Started With PICkit 2 App

PICkit™ 2 Getting Started with the PICC-Lite C Compiler

© 2007 Microchip Technology Inc. Page 16 of 20

10. Now that we are debugging code, we may wish to see what is happening to variables that the code is acting on. A “Watch Window” may be used to see the contents of variables as well as the Special Function Registers that control the PIC16F887 peripherals.

To open a Watch Window, select View > Watch. The Watch Window will appear in the MPLAB IDE workspace. To watch the members of the struct variable adc_conversion we’ll add it to the Watch. Click on the right drop-down box in the Watch Window and select “adc_conversion.” Click the Add Symbolbutton to add the variable symbol to the Watch Window.

Since adc_conversion is a complex structure with 2 member variables, it must be expanded to view the members. Click the expansion box next to the Symbol Name to see the member variables, “value” and “new_flag”.

To add the Special Function Register (SFR) of the ADC module where the upper 8 bits of the conversion result is stored, select “ADRESH” from the left drop-down box and click Add SFR.

Page 17: DBE Getting Started With PICkit 2 App

PICkit™ 2 Getting Started with the PICC-Lite C Compiler

© 2007 Microchip Technology Inc. Page 17 of 20

11. We’ll now “step” through the code one line at a time to see the ADC conversion result stored in the variable adc_conversion.value. Select Debugger > Step Over to execute the line of code pointed to by the green arrow, which will be

adc_conversion.value = ADRESH;

(Instead of the menu, you could press the shortcut key F8 once to Step Over the line of code.)

The arrow advances to the next statement, indicating that the previous statement was executed. In the Watch Window, the value of “ADRESH” has been copied into adc_conversion.value, which is red to indicate the variable value was changed during the “step” execution. This is what we expected from the assignment statement that just executed.

Press F8 again to step over the next line of code,

adc_conversion.new_flag = 1;

The arrow advances again to the next line of code, and the Watch window indicates that adc_conversion.new_flag now contains the value “1” as expected.

Page 18: DBE Getting Started With PICkit 2 App

PICkit™ 2 Getting Started with the PICC-Lite C Compiler

© 2007 Microchip Technology Inc. Page 18 of 20

12. Turn the demo board potentiometer RP1 slightly to change the voltage and select Debugger > Run to continue program execution. The program halts again when it reaches the breakpoint. A new ADC conversion result is available in ADRESH, and adc_conversion.new_flag has been reset by the code in main().

Step over the next 2 instructions to see the new ADC result stored in adc_conversion.value and adc_conversion.new_flag set back to “1”.

Page 19: DBE Getting Started With PICkit 2 App

PICkit™ 2 Getting Started with the PICC-Lite C Compiler

© 2007 Microchip Technology Inc. Page 19 of 20

13. Some closing remarks on debugging with PICkit 2 Debug Express:

Variable values may be edited in the Watch Window by double-clicking on the value while the program is halted. In this way, the effect of specific value, for example a specific ADC conversion result, could be debugged without having to attempt to actually get that value in an ADC result.

When program execution is halted during debugging, most of the microcontroller peripherals “freeze” also, which means they stop operation. However, this is not true of Timer0. Thus if the value of the TMR0 register is Watched while Stepping through the code, the value will not change in proportion to the number of instruction cycles for each Step. It also means that after stepping over the statement to clear T0IF, it will be immediately set again by the timer continuing to run.

A side-effect of “freezing” the peripherals is that it stops the ADC clock, so stepping through the code during an ADC conversion may cause an unpredictable ADC result.

Page 20: DBE Getting Started With PICkit 2 App

PICkit™ 2 Getting Started with the PICC-Lite C Compiler

© 2007 Microchip Technology Inc. Page 20 of 20

Quick Reference

Compiling and programming with PICkit 2 in MPLAB IDEProgrammer > Select Programmer > PICkit 2 (Not needed if already selected)Project > BuildProgrammer > ProgramProgrammer > Release from Reset (To run if /MCLR pin is enabled)

Compiling and debugging with PICkit 2 in MPLAB IDEDebugger > Select Tool > PICkit 2 (Not needed if already selected)Project > BuildDebugger > ProgramDebugger > Run (Begin execution in debug mode)

Next Steps

To find out more about developing code in the MPLAB IDE, see the MPLAB IDE Quick Start Guide included in the “Reference” section of the PICkit Starter Kit CD-ROM. The most recent version of this document is also available on the included MPLAB IDE CD-ROM.

The PIC16F88x Data Sheet is the best resource for learning about the operation of the microcontroller and its peripherals. Stepping through code with the debugger and Watch Window can help greatly in understanding the operation of the device.

Microchip’s Online Discussion Groups at forum.microchip.com are a good place to ask questions and get information on developing with PIC MCUs. The [Development Tools] –Programmers sub-forum is the best place for PICkit 2 related topics.