Jan 072014
 

Overview:

Did you know that you can program your chipKIT board or any PIC32 chip preprogrammed with a chipKIT Bootloader right from MPLAB X? It’s actually quite easy to do. This tutorial will demonstrate how to obtain the proper binaries and how to set up MPLAB X to download programs written in C or MIPS Assembly to a PIC32 using the AVRdude bootloader. Note that this tutorial including screen captures was done using a Windows® 7 PC.  

What you will need:

To complete this task, you will require the following:

Procedure:

  1. Unzip the chipKIT MPIDE to a location on your computer
  2. Navigate to the MPIDE folder an locate avrdude.exe and avrdude.conf in the following folders:
    1. avrdude.exe: mpide-0023-windows-xxxxxxxx\hardware\tools\avr\bin\avrdude.exelocateAVRdude
    2. avrdude.conf: mpide-0023-windows-xxxxxxxx\hardware\tools\avr\etc\avrdude.conflocateCONFfile
  3. Copy both of these and paste into your MPLAB X directory: C:\Program Files\Microchip\MPLABX\mplab_ide\binmplabXBin*Note: MPLAB X IDE will need to be installed on your computer for this folder to have been created.
  4. Next, open MPLAB X and set up your project as you normally would and write your code. (If you are not familiar on how to do any of these please refer to the tutorials towards the bottom of our Academia page: http://chipkit.net/academia/)
  5. The bootloader pre-programmed on the PIC32 does take up some room in Flash program memory. Therefore, to make sure we do not overwrite the bootloader, we will need to tell MPLAB X where to put our program code. This will be done using a linker script. There are special linker scripts in our MPIDE download that are already formatted to accommodate the bootloader. In the MPLAB X IDE Project window, right click on the Linker Files folder and select Add Existing Items…. In the dialog, navigate to where you unzipped MPIDE and locate the linker script for your particular device (PIC32MX250F128B used in this tutorial). It will be called something like “chipKIT-application-32MX250F128.ld”. You will also need an additional script called “chipKIT-application-COMMON.ld”. Select both of these files, check the Copy checkbox, and press Select to close the window and add these files to your project folder.addingLDscripts
  6. We will need to make some changes to these linker scripts. Double-click on the part-specific linker script (chipKIT-application-32MX250F128.ld in this tutorial) to open it. Scroll down through the linker script to the last line and uncomment the section:
     INCLUDE "chipKIT-application-COMMON.ld"
  7. Double-click on the “chipKIT-application-COMMON.ld linker” script. Scroll down to approximately line 574 and comment out the line:
     /* ASSERT (SIZEOF(.header_info) == 76, "MPIDE Version not specfied correctly")*/
  8. Next, we will need to let MPLAB X know that we wish to use avrdude to program the PIC32. Therefore, right-click on your project and select Properties to open the Project Properties window.openMPLABXproperties
  9. In the Project Properties Window, select the Building category. On the right side of the window, check the Execute this line after build check box and add the following code to the text field under the check box:
    "C:\Program Files\Microchip\MPLABX\mplab_ide\bin\avrdude.exe" -C "C:\Program\Microchip\MPLABX\mplab_ide\bin\avrdude.conf" -v -p32MX250F128B -cstk500v2 -P COM49 -b 115200 -U flash:w:${ImagePath} selectExecuteAfterBuild
    Some important things to note here:
    1. You will need to make sure you’ve selected your MCU if not using the PIC32MX250F128B as shown in the example.
    2. Similarly, you will also need to change the COM port to the one assigned to your chipKIT Platform. The COM port can be found in your Device Manager (on Windows machines)
  10. Finally, to program your PIC32, make sure that it is in bootloader mode. Depending on the board you have, this may require that you press some buttons or this could be done automatically through the use of an FTDI UART-to-USB chip on the board. Once in bootloader mode, click either the Build or Clean and Build button. Following build, the avrdude bootloader should be called and the PIC32 programmed over the COM port selected with your code. 
  IMPORTANT!! The bootloader will be executed anytime you select Build, Clean and Build or if you attempt to enter Debug mode. The bootloader often resides in sections of memory that are used for debugging. Therefore, if you enter debug mode and your PIC32 is connected to your computer, there is a good chance you will overwrite the bootloader and it will need to be programmed back onto the chip using an external programmer/debugger.  Therefore, always disconnect the USB cable if you plan on doing any code debugging with the simulator. You will not be able to do hardware debugging. A programmer/debugger is needed for that. You will also need to deselect the Execute this line after build checkbox in the Project Properties>Building window shown in Step 9. Otherwise you will receive an error.    
VN:F [1.9.22_1171]
Rating: 9.5/10 (6 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS
Apr 102013
 

Overview:

In this project you will learn to use a basic functionality of chipKIT board in MPLAB X IDE, which is to read an analog value and output the corresponding digital reading for it. A potentiometer is a  three-terminal resistor with a sliding contact that forms an adjustable voltage divider. Using a potentiometer measuring instrument, you could measure the electric potential. In this example we use the potentiometer in the chipKIT Basic IO shield. Find the same project using MPIDE here.

Hardware Used:

To do the analog read project, you require the following hardware devices.

  • chipKIT™ Uno 32
  • chipKIT™ Basic IO shield
  • PICkit® 3 In-Circuit Debugger/Programmer
  • USB cable
  • 6 pin header to connect chipKIT™ board and PICkit® 3

Reference:

The reference guide for each board, schematics and other resources are available on their individual homepages:

Procedure:

  1. Connect the hardware devices. The PICkit® 3 should be connected to the chipKIT™ Uno32 board through a suitable header and use the USB cable to connect the PICkit® 3 to your PC.
  2. Place the IO shield on top of the Uno32 board with a bit of a firm press. A0-potentiometer of the IO shield will be used as peripheral for Uno32 board, in this excercise.
  3. Once the hardware setup is made and the device drivers are updated for PICkit® 3 on your computer, launch MPLAB X (Start>All Programs>Microchip>MPLAB X IDE>MPLAB X IDE vx.xx on Window® Machines ).
  4. Create a new project through File>New Project. In the pop up new project window, select Standalone Project  under Projects  and click Next. Under Family scroll and select 32-bit MCUs (PIC32). Note the processor number from the chipKIT board’s reference manual and enter it under the Device  option. Click Next.
  5. Select PICkit3 in the Hardware Tools, click Next. Select XC32 (v1.20) under Compiler Toolchains, click Next. Give a project name to create the project.
  6. In the Project tab on the left side of the MPLAB X IDE, right click on Source Files > New > C Main File.. and give a file name for main source code. Once the dot c file opens, select all the codes in the file and replace it with the codes below.
    #include <plib.h>
    #include <xc.h>
    
    #pragma config FPLLMUL = MUL_20, FPLLIDIV = DIV_2, FPLLODIV = DIV_1, FWDTEN = OFF
    #pragma config POSCMOD = HS, FNOSC = PRIPLL, FPBDIV = DIV_1
    #define SYS_FREQ (80000000L)
    
    unsigned int channel4;	// conversion result as read from result buffer
    unsigned int offset;	// buffer offset to point to the base of the idle buffer
    
    int main(void)
    {
        // Configure the device for maximum performance but do not change the PBDIV
        // Given the options, this function will change the flash wait states and
        // enable prefetch cache but will not change the PBDIV. The PBDIV value
        // is already set via the pragma FPBDIV option above..
        SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);
    
        // configure and enable the ADC
        CloseADC10();    // ensure the ADC is off before setting the configuration
    
        // define setup parameters for OpenADC10
                    // Turn module on | ouput in integer | trigger mode auto | enable autosample
        #define PARAM1  ADC_MODULE_ON | ADC_FORMAT_INTG | ADC_CLK_AUTO | ADC_AUTO_SAMPLING_ON
    
        // define setup parameters for OpenADC10
                    // ADC ref external    | disable offset test    | disable scan mode | perform 2 samples | use dual buffers | use alternate mode
        #define PARAM2  ADC_VREF_AVDD_AVSS | ADC_OFFSET_CAL_DISABLE | ADC_SCAN_OFF | ADC_SAMPLES_PER_INT_9 | ADC_ALT_BUF_OFF | ADC_ALT_INPUT_OFF
    
        // define setup parameters for OpenADC10
        //                   use ADC internal clock | set sample time
        #define PARAM3  ADC_CONV_CLK_INTERNAL_RC | ADC_SAMPLE_TIME_15
    
        // define setup parameters for OpenADC10
        // do not assign channels to scan
        #define PARAM4    SKIP_SCAN_ALL
    
        // define setup parameters for OpenADC10
                    // set AN2 as analog inputs
        #define PARAM5    ENABLE_AN2_ANA
    
            // use ground as neg ref for A | use AN2 for input A
    
         // configure to sample AN2
        SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN2); // configure to sample AN2
        OpenADC10( PARAM1, PARAM2, PARAM3, PARAM4, PARAM5 ); // configure ADC using parameter define above
    
        EnableADC10(); // Enable the ADC
    
      while ( ! mAD1GetIntFlag() ) { } // wait for the first conversion to complete so there will be vaild data in ADC result registers
    
        // the result of the conversion is available in channel4.
        while (1)
        {
    
            channel4 = ReadADC10(0);          // read the result of channel 4 conversion from the idle buffer
    
        }
    
        return 0;
    }
  7. To read the potentiometer value, access the watch window from Window > Debugging > Watches or you can use the shortcut key Alt+Shift+2. In the watch window panel that opens on the MPLAB X IDE, enter the new watch as channel4, in the blank <Enter new watch> area as shown below in the figure.                                                                                                                                                          analogread mplab1
  8. In the program editor window, put a breakpoint by clicking on the line number after the code line
     channel4 = ReadADC10(0);          // read the result of channel 4 conversion from the idle buffer
    That is on the line with the closing braces of the while loop. This should create a red box in the number line as shown in the figure below,                                                     analogread mplab2
  9. Click on Debug Project icon shown below,                                                 analogread mplab3
  10. Once the program halts its execution at the line with the breakpoint, click on Watches output window, right click on the expand button under Value column and select Display value column as > Decimal. The value that appear in this column for channel4 variable corresponds to the potentiometer reading which is read in the range of 0 to 1023. A value 0 corresponds to 0 volts and a value 1023 corresponds to 3.3 volts.
  11. Now, click on Continue button from the top pane and observe the value of channel4 to change and reflect the potentiometer reading as described before.
  12. If you would like to change the potentiometer knob and see a different value, then, change the potentiometer knob and click on the Reset button on the top pane. This will initialize the variables back to zero. Clicking on the Play button on the top pane, will get your changed  potentiometer value on the Watches window. Below is a screenshot showing the potentiometer value for 3.3 volts being read in the Watch window.  analogread mplab4
VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.22_1171]
Rating: +1 (from 1 vote)

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS
Feb 222013
 

Overview:

The Quick start under Getting started will guide you about creating a new project. For your convenience a quick start on MPLAB X IDE with a blink LED project is demonstrated as follows.

Hardware Used:

To do the blink LED project, you require the following hardware devices.

  • chipKIT™ board
  • PICkit® 3 In-Circuit Debugger/Programmer
  • USB cable
  • 6 pin header to connect chipKIT™ board and PICkit® 3

Procedure:

  1. To get the blink project’s source code, you need to know the processor on your chipKIT™ board. You can get this information by visiting the manufacturer’s website. In this example, Digilent Inc.’s chipKIT™ uC32™ board will be used. The homepage for this board can be accessed through http://www.digilentinc.com/chipKIT™. An example screenshot for chipKIT™ uC32 board is shown on Digilent website highlighting the processor type.                                                                                                                                                                                                           Mplab blink1
  2. You will also need to download the chipKIT™ uC32 Reference Manual as it contains important information about which pins on the Microcontroller connect to components on the chipKIT™ board such as LEDs, pushbuttons and so. To locate the manual, scroll down the product page to the “Supported Documents” section.*NOTE: Reference documentation location will vary by manufacturer.
  3. Microchip Technology has a variety of code examples for a variety of different applications. In this tutorial, we want to blink and LED. This would require that we use a peripheral on the Microcontroller called a PORT. To locate code examples specific to the PORT peripheral on your particular Microcontroller, visit the “Code Examples” website at www.microchip.com/codeexamples. In the “Products” filter window, enter the device number for the Microcontroller you found in the previous section. In this example, we will be using the PIC32MX340F512H. In the “Document Title” filter window, type in the word “ports” and press enter.                                                                                                                                                                                                                                                        Mplab blink2
  4. Locate the “PIC32 PORTS Code Example” and click on the title to download a compressed file containing the source code. Once downloaded, extract the source files into a folder of your choice. It is recommended that MPLAB projects be stored in the MPLAB folder in your My Documents.
  5. Connect the hardware devices. The PICkit® 3 should be connected to the chipKIT™ board through a suitable header and use the USB cable to connect the PICkit® 3 to your PC.
  6. Once, the device drivers are updated for PICkit® 3 on your computer, launch MPLAB X (Start>All Programs>Microchip>MPLAB X IDE>MPLAB X IDE vx.xx on Window® Machines).
  7. MPLAB X IDE vx.xx, File-> Open Project, locate the folder you have saved the code example in and open the blinky_led.X project.
  8. In the file pane, expand the Source Files in the Project tab and double click on blinky_leds.c to open the source code. In the editor pane, a file with blinky_leds.c name will appear with the code for the project.       Mplab blink4
  9. The code you see is for blinking an LED on your chipKIT™ board. To customize the code for your device, you will have to change the PORTA with the port that is actually used to connect to the LEDs in your device.
    mPORTAClearBits(BIT_7 | BIT_6 | BIT_5 | BIT_5 | BIT_4 | \
    
    BIT_3 | BIT_2 | BIT_1 | BIT_0 );
    
    mPORTASetPinsDigitalOut( BIT_7 | BIT_6 | BIT_5 | BIT_5 | BIT_4 | \
    
    BIT_3 | BIT_2 | BIT_1 | BIT_0 );
    
    mPORTAToggleBits( BIT_7 | BIT_6 | BIT_5 | BIT_5 | BIT_4 | \
    
    BIT_3 | BIT_2 | BIT_1 | BIT_0  );
  10. To find out the port accessing the LEDs, open the reference manual you downloaded earlier from http://www.digilentinc.com and find the port ID under Pinout table by logical pin number similar to as shown in the screenshot for uC32 device.                                                                                                                                                                                                                                                                                                                                     blink5
  11. Since the port ID is RF0, it means the LEDs are controlled by changing the status bit 0 of port F. Hence the code is changed as mentioned below,
    mPORTFClearBits(BIT_7 | BIT_6 | BIT_5 | BIT_5 | BIT_4 | \
    							 BIT_3 | BIT_2 | BIT_1 | BIT_0 );
    mPORTFSetPinsDigitalOut( BIT_7 | BIT_6 | BIT_5 | BIT_5 | BIT_4 | \
    							 BIT_3 | BIT_2 | BIT_1 | BIT_0 );
    mPORTFToggleBits( BIT_7 | BIT_6 | BIT_5 | BIT_5 | BIT_4 | \
    							 BIT_3 | BIT_2 | BIT_1 | BIT_0  );
  12. Also, if your processor is not mentioned in the define statements, change SYS_FREQ into actual value as 80000000L similar to shown below
    SYSTEMConfig(80000000, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);
  13. Now, to run the code on your device, click on the icon that says “Clean and build over” when hovered over with the mouse. It is also highlighted in the screenshot below.                                                                  mplab blink6
  14. Once you get the Loading completed line under the Output window, click on the icon next to Clean and Build over, which is Make and Program device. Now, you code will be uploaded to the device and the LED should start blinking.
  15. If you get the error “Target device was not found. You must connect to a target device to use PICkit 3.” in the PICkit 3 output tab, click Run> Set Project Configuration> Customize.  In the Project properties window that opens, select PICkit 3 under Categories in left. In the Option Categories on top, select Power, check the Power target circuit from PICkit3 and set the Voltage Level to 3.0.
  16. In the Categories pane in the left, select Conf:[default]. Under the Device option, select the processor type and click OK. Now build and make the program again.
VN:F [1.9.22_1171]
Rating: 10.0/10 (1 vote cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS