EEE158-Module-1


Project maintained by OverTheCorner Hosted on GitHub Pages — Theme by mattgraham

MPLAB First Steps

Installation

Install MPLAB X IDE

Install MPLAB X IDE through the official Microchip site: MPLAB X IDE

Install XC32 Compiler

Install the XC32 COmpilers from Microchip: MPLAB xC32 Compilers

Your First Bare-Metal Project

alt text

alt text

alt text

alt text

note: We suggest creating a new folder within another folder when you create a project as another folder will be generated in the same directory as the path you choose here (i.e. if you plan on storing all your Projects in a folder called MyProjects, create a folder MyProjects/MyFirstProject and choose MyFirstProject as the place to save)

alt text

note: Unlick the `Open MCC on Finish option as we won’t be using that for now

alt text

Let’s now proceed to creating a main.c file so we can start coding our application.

Guided Exercise: “Blinky!”

Blinky!

Create a main.c file. Right click on the Source Files of the Project Tab then click New and C Main File

alt text

alt text

Proceed to input the following code into main.c

// All register names are defined here.
#include <xc.h>

// Naive software delay; approx. 1us / cycle
void crude_delay_ms(int ms)
{
	while (ms > 0) {
		for (int i = 0; i < 1000; ++i) {
			asm("nop");
		}
		--ms;
	}
}

/*
 * NOTE: Most of my students seem to recall the member-access operator when
 *       using the term 'class' rather than 'struct'.
 *
 * If 'a' is the object itself, or a C++ rvalue:
 * 	a.b()
 *
 * If 'a' is a pointer:
 * 	(*a).b()
 * 	- or -
 * 	a->b()
 */

int main(void)
{
	/*
	 * Configure PA15 (Port Group A, Bit 15)
	 *
	 * 'PORT_SEC_REGS' --> All port I/O registers
	 * 'GROUP'	   --> Array of port groups (0 = 'A', 1 = 'B', etc.)
	 * 'PORT_???'	   --> Register named '???'
	 */

	// NOTE: Some review of C/C++ bitwise operations may be in order.
	PORT_SEC_REGS->GROUP[0].PORT_OUT &= ~(1 << 15);
	PORT_SEC_REGS->GROUP[0].PORT_DIR |=  (1 << 15);
	
	// Nowhere to return to, hence the infinite loop
	for (;;) {
		crude_delay_ms(200);

		// NOTE: A '1' in an XOR mask causes a toggle.
		PORT_SEC_REGS->GROUP[0].PORT_OUT ^= (1 << 15);
	}
	
	// Not expected to be run at all
	return 1;
}

Let us first try Building our Application. Press the Build Icon to compile your project. It’s the one that looks like a hammer alt text

If it builds, we are now ready to try and debug your application.

Simulator Debugging

note: the simulator has limited functionality in simulating the PORT peripheral

alt text

Include a breakpoint in your program by clicking on the line number on the left side of the code editor

alt text

Click on the Debug Main Project button on the ribbon to begin Simulator Debugging.

Application Execution Control

These buttons on the ribbon will allow you to control the debugging of your application.

alt text

Your debugger should stop at the break point we set earlier

alt text

Press the Step Over Button and you should go into the next line of the Application code

alt text

Now press the Step Into Button and you should go into the crude_delay method

alt text

Press the Continue button and after a while, you should hit the breakpoint again

alt text

Target Memory Views

Target memory views allows us to look at the contents of the memory of our device under test (DUT). You can open this by navigating in the ribbon. Window -> Target Memory Views

alt text

Let’s try and open up the Peripherals Memory View.

alt text

This allows us to directly check the contents of the registers and is useful for debugging.

Logic Analyzer

You can open up the logic analyzer by going to ‘Window’ -> ‘Simulator’ -> ‘Logic Analyzer’

alt text

You can watch over a pin’s state by adding it. Press the Settings icon on the logic analyzer window and add PA15 to Selected Pins

alt text

alt text

You can now use the Logic Analyzer to view how PA15 toggles between 0V and 3.3V

alt text

Hardware Debugging

Hardware debugging is similar to Simulator debugging and you can perform any of the Application Execution Controls and Memory View functionality.

To change the Tool from Simulator to you on-board debugger

alt text

alt text

Your First MCC Project

MPLAB Code Configurator (MCC) is a tool from Microchip which allows us to easily configure our Microcontroller by automatically creating macros we can use for our code. Let’s try and use it.

alt text

alt text

alt text

alt text

note: We suggest creating a new folder within another folder when you create a project as another folder will be generated in the same directory as the path you choose here (i.e. if you plan on storing all your Projects in a folder called MyProjects, create a folder MyProjects/MyFirstProject and choose MyFirstProject as the place to save)

alt text

alt text

alt text

MPLAB Code Configurator

alt text

Board Packages

alt text

alt text

note: you can also add these packages later by opening up the Content Manager (CM button beside MCC) if you don’t install them now

MCC Harmony Windows

We now proceed with using MCC to easily create some configurations for our Microcontroller

Open up MCC and navigate to Pin Configuration either through the ribbon or through the Project Graph

alt text

alt text

We can use a GUI to set up the pins of our Microcontroller. Proceed to set up PA15 as Output Pin as shown. Take note of the Security Mode being set as NON-SECURE as well.

alt text

alt text

alt text

You should now see in your Project folder more files, these were auto generated by MCC. The interesting one here is the header file plib_port.h

alt text

You can use these newly declared definition in you code to control the Pin.

alt text

Here is where you can start creating your application. Let’s blink try and blink an LED by switching a pin between HIGH and LOW.

Guided Exercise: “MCC-Blinky!”

Blinky!

int crude_ms_delay(int ms){
    int count = 0;
    unsigned int delay_count = ms * 12000;
    
    while(count < delay_count){
        asm("nop");
        count = count + 1;
    }
    return 0;
}


int main ( void )
{
    /* Initialize all modules */
    SYS_Initialize ( NULL );

    while ( true )
    {
        LED_PIN_Toggle();
        crude_ms_delay(1000);
        /* Maintain state machines of all polled MPLAB Harmony modules. */
        SYS_Tasks ( );
    }

    /* Execution should not come here during normal operation */

    return ( EXIT_FAILURE );
}

Let us first try Building our Application. Press the Build Icon to compile your project. It’s the one that looks like a hammer alt text

If it builds, we are now ready to try and debug your application.