Difference between revisions of "NU32v2: Starting a New Project and Putting it on the NU32v2"

From Mech
Jump to navigationJump to search
Line 145: Line 145:


[[Image:nu32v2_serial_bootloader_open.png|thumb|300 px|left]]
[[Image:nu32v2_serial_bootloader_open.png|thumb|300 px|left]]
<br clear=all>


Now open NU32v2_Serial_Bootloader.exe. Click "Select .hex File", navigate to your project and select your hex file (projectname.hex).
Now open NU32v2_Serial_Bootloader.exe. Click "Select .hex File", navigate to your project and select your hex file (projectname.hex).


[[Image:nu32v2_serial_bootloader_hex.png|thumb|300 px|left]]
[[Image:nu32v2_serial_bootloader_hex.png|thumb|300 px|left]]
<br clear=all>


Next click on the button corresponding to your COM port. If multiple ports are listed and you don't know which is your NU32v2 board, '''try something'''.
Next click on the button corresponding to your COM port. If multiple ports are listed and you don't know which is your NU32v2 board, '''try something'''.


[[Image:nu32v2_serial_bootloader_com.png|thumb|300 px|left]]
[[Image:nu32v2_serial_bootloader_com.png|thumb|300 px|left]]
<br clear=all>


Next put your NU32v2 board into bootloader mode by pressing and holding the reset button on the NU32v2, pressing and holding the button in the top left breadboard between pin G6 and GND, release reset, then release G6.
Next put your NU32v2 board into bootloader mode by pressing and holding the reset button on the NU32v2, pressing and holding the button in the top left breadboard between pin G6 and GND, release reset, then release G6.


[[Image:nu32v2_serial_bootloader_connect.png|thumb|300 px|left]]
[[Image:nu32v2_serial_bootloader_connect.png|thumb|300 px|left]]
<br clear=all>


Next click the "Program" button. Doing this will clear the memory of your NU32v2, write your new program to the board, reset the board and close NU32v2_Serial_Bootloader.
Next click the "Program" button. Doing this will clear the memory of your NU32v2, write your new program to the board, reset the board and close NU32v2_Serial_Bootloader.

Revision as of 17:06, 15 January 2011

Return to Using the NU32v2


**THIS PAGE IS UNDER CONSTRUCTION AND IS NOT COMPLETE**

**AWL 1/15/2011**


PIC32 Programming in C

Note: Code downloaded from Microchip is constantly evolving, and it is possible that the information below will be outdated for future code releases. This information is accurate for code downloaded from Microchip's website in January 2011. Also, sample code developed by us and others are generally modified from working code for different purposes, and therefore you may find unnecessary legacy code, programming inefficiency, or even incorrect code in some samples.

You should complete the instructions in NU32v2: Software to Install if you have not already. For this page specifically, you need to

  • download and install the MPLAB IDE (Integrated Development Environment) that you use for programming, compiling, debugging, and simulating your PIC code. The MPLAB C Compiler for PIC32 is included in the "complete" installation of the IDE
  • download the Serial Bootloader for NU32v2 PC application
  • install drivers for FTDI

The remainder of this page describes how to create a new Project in MPLAB, compile a general "Hello World" program to blink the LEDs on the NU32v2, and use the Serial Bootloader of NU32v2 PC application to put the program on the NU32v2.

Create a New Project in MPLAB for the NU32v2

For organizational reasons, it is recommended to have a folder on your computer to store your PIC projects. If you do not already have a space to save your projects. Create a new folder and call it PIC_Projects. Each project should have its own folder inside PIC_Projects.

  • Create a folder in PIC_Projects and call it HelloWorld
  • Download this procdefs.ld file and place it in the new HelloWorld folder.

When creating new projects to download to the PIC, you must include a linker file. If you have installed a bootloader (such as with the NU32v2), the linker file tells the bootloader to put the program after the bootloader memory to prevent erasing of the bootloader. The required linker file is called procdefs.ld. You will always need to add this procdefs file to your project folders in order to properly install your programs on the PIC. If you do not use this procdefs file, you potentially could overwrite the bootloader.

  • Open MPLAB
  • Choose Project -> Project Wizard to create a new project.
  • Click Next.
  • Select the device you are using. The NU32v2 is using the PIC32MX795F512L.
  • Choose the Microchip PIC32 C-Compiler Toolsuite from the drop down menu for Active Toolsuite. This sets up the compiler that will be used for creating the hex code that will be placed on the PIC. Click Next
  • Create a new project called HelloWorld in the folder called HelloWorld (click browse to navigate to that folder). Click Next.
  • Highlight the PROCDEFS.ld file under the HelloWorld folder and click Add. This adds the file to your project. For future projects, if you have existing .c and .h code, this is the step to add these files to your project. For this tutorial, we will be creating our own file, so no additional files need to be added.
  • On this last step, it displays a summary of the previous steps. Click finish.

You have now created a project. The project window should appear as below.

PIC32 ProjectWindow.jpg


The procdefs.ld is not the only linker file.

  • In the project window, drag procdefs.ld from the 'Linker Script' folder to the 'Other Files' folder.
PIC32 ProjectWindow2.jpg


Now we are going to create a .c file to add to this project.

  • Choose File -> New.
  • Choose File -> Save As and call the new file HelloWorld.c in the HelloWorld folder. This will create a source file for your code. You need to include '.c' on the file name, otherwise MPLAB does not know that it is a source file.
  • Right click on the 'Source Files' folder and choose 'Add files'. Choose the HelloWorld.c file to add it to the project.
PIC32 AddFiles1.jpg


You have now created a project and added a blank source file to your project. You are now ready to code your first program.

HelloWorld Code

This program will toggle on and off the two LEDS on the NU32v2 board.

You are now ready to edit your new HelloWorld.c file.

  • Cut and paste the code below into your HelloWorld.c file.
/* HelloWorld.c
 *
 * Our first PIC program, which simply flashes LEDs on the NU32v2
 * board.  We have already installed a "bootloader" on our PIC32,
 * which specifies "configuration bits" that set the clock frequency
 * to 80 MHz (assuming the 8 MHz crystal on the NU32v2 board), 
 * 
 *
 * HISTORY
 * 1/15/2011	Created by Nick Marchuk, Andrew Long and Kevin Lynch
 *
 */

// plib.h gives access to useful constant and function definitions, found in 
// Program Files\Microchip\MPLAB C32\pic32-libs\include
#include <plib.h>
					
#define SYS_FREQ  	(80000000L)		// clock frequency set by  bootloader
#define LED_0		LATGbits.LATG12 // mnemonic constants for LEDs on NU32v2
#define LED_1		LATGbits.LATG13
#define WAIT_LOOPS	2000000			// number less than than 2^32 - 1, or about 4 billion

void initLEDs(void);
void delay(unsigned int loops);

int main(void) {
	int i=0;	// loop counter

	// SYSTEMConfig() optimizes performance for our clock frequency.
	// in Microchip\MPLAB C32\pic32-libs\include\peripheral\system.h
	SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);
     
	initLEDs(); 	// set LED pins as outputs

	LED_0 = 1; // low output turns an LED on; see NU32v2 circuit
	LED_1 = 0; // high output turns an LED off
	
	while(1) {		// infinite loop
			
		LED_0 = !LED_0; // toggle LED_0
		LED_1 = !LED_1;	// toggle LED_1

		delay(WAIT_LOOPS);
	}
	return 0;
} // end main

/* initLEDs()
 *
 * initLEDs sets RG12 and RG13 to outputs to control the LEDs on the NU32v2.
 */
void initLEDs(void) {
					// Read about setting digital inputs / outputs on the
					// Digital input-output example	
	LATG |= 0x3000;		// Initializes G12 and G13 to 1 (off for NU32v2 board)
	TRISG &= 0xCFFF;	// set the two pins to inputs
						
}

/* delay(unsigned int loops)
 *
 * This function just wastes time.  Note; it just uses an int number of loops,  
 * which should be less than 2^32 - 1, or about 4 billion.  If you really want
 * to waste a lot of time, you could use a long long.
 */
void delay(unsigned int loops) {
	int i = loops;
	while(i--) {}
}

To load this program to the PIC with the bootloader, we need a hex file. MPLAB creates a hex file in the project folder when you compile the program.

  • Choose Project -> Build all OR F10 on PCs.

The output window should say 'Build Succeeded'. If it doesn't, fix the errors.

Using the Serial Bootloader for NU32v2 PC Application

Under Construction Now that you have compiled your code, you can use the bootloader to put it on your NU32v2.

Plug your NU32v2 into a power supply and turn on the board. Plug in the USB cable to the board and your computer. If this is the first time you have plugged the board into your computer, drivers will be installed assigning your board a communication (COM) port number.

Nu32v2 serial bootloader open.png


Now open NU32v2_Serial_Bootloader.exe. Click "Select .hex File", navigate to your project and select your hex file (projectname.hex).

Nu32v2 serial bootloader hex.png


Next click on the button corresponding to your COM port. If multiple ports are listed and you don't know which is your NU32v2 board, try something.

Nu32v2 serial bootloader com.png


Next put your NU32v2 board into bootloader mode by pressing and holding the reset button on the NU32v2, pressing and holding the button in the top left breadboard between pin G6 and GND, release reset, then release G6.

Nu32v2 serial bootloader connect.png


Next click the "Program" button. Doing this will clear the memory of your NU32v2, write your new program to the board, reset the board and close NU32v2_Serial_Bootloader.

PIC32 Programming in C with the MPLAB X Beta IDE

Open 'MPLAB X IDE beta'. You will see the following screen.

Click Create New Project.

In Categories, select the Microchip Embedded folder. In Projects, select C/ASM Standalone Project. Click Next >.

In Family, select PIC32, and in Device, select PIC32MX795F512L.

In Select Tool, select PICkit3 if you are going to program the code onto your NU32v2, or select Simulator if you are going to practice coding on your computer.

In Select Compiler, select C32.

In Select Project Name and Folder, create a name for your project, select a location to save it to, ab