Using GPIO in MPLAB® X IDE with MCC

Last modified by Microchip on 2024/01/23 16:55

General Purpose Input/Output (GPIO) pins on an 8-bit PIC® MCU within MPLAB® X IDE are quite easy to set up and use thanks to the simple interface of the MPLAB® Code Configurator (MCC) tool. As an input, GPIO may be used for things like carrying sensor information to the chip. As an output, GPIO may be used to send information to different actuators such as LEDs or motors. Many of the I/O pins share a connection with other peripherals on the device but this discussion will focus on using them just as GPIO.

Locking Pins as GPIO

The first step to using a GPIO pin is to select the peripheral connection inside the microcontroller. In the MCC within MPLAB X IDE, pins can be connected to signal lines through the use of the MCC Pin Manager window, which is the farthest right menu of the MCC screen. To connect a particular pin to a particular signal line for use as a GPIO Pin, look for the two rows labeled Pin Module. The input row is used for signals entering the microcontroller, and the output row is used for signals sent out of the microcontroller. The top rows are labeled with ports, underneath which is a series of numbers. These are referred to by the syntax R(PortLetter)#. For example, signal line RA4 refers to port A, pin 4. To connect the pin to that particular signal, click the blue unlocked icon to change it to the green locked icon as seen in the accompanying image:

lock pins

If you do not see the rows labeled Pin Module, you may have to scroll down the list as they are positioned near the bottom of the list.

Back to Top

Configuring the Pin in MCC

Once the GPIO Pin has been locked to the Port, you will see an accompanying row appear in the Pin Module setup screen. This module can be found in the center of the MCC screen after clicking on Pin Module under Project Resources.

pin module from resources

It should appear as shown in the accompanying image:

table of pin assignments

Back to Top

Pin Name

Pin name refers to the Port that was mentioned above. For example, since the first pin locked from left to right was in the Port A section, and corresponded to the 0 column, the pin is labeled RA0. This structure is the same for all pin names, regardless of the capabilities of a particular pin. The designation can be re-labeled under custom name section of the Pin Module screen.

Module

Module refers to the module used for the pin you have chosen. RA0 is being used as a GPIO, which is encompassed under the Pin Module.

Function

Multiple instances of a pin may appear depending on how many peripherals are using it. Therefore, for each of these instances, a row appears for the accompanying function, along with the module that the function relates to. This is a feature of the Peripheral Pin Select capabilities of this device.

Custom Name

The custom name is a user-defined name for the pin, as opposed to the Pin Name which is system defined. This will be the name used while writing any additional code not covered in the configuration.

Start High

Start High defines if the pin will begin the program as logic HIGH, or VDD – 0.7V.

Analog Setting: If you are seeking to use the GPIO pin as a digital input, make sure this box is unchecked. Checking the box will enable it as an Analog or ADC pin.

Output

This box defines whether the pin will be used to receive (input) or send (output) a signal.

Back to Top

WPU

Checking this box controls the use of a weak pull-up resistor on the pin. The pull-up resistor is placed between the input pin signal line and VCC (5V). When you are trying to use certain digital inputs, electrical noise or stray currents caused from a variety of environmental factors may cause your pin to fluctuate between HIGH and LOW.

For example, pushbuttons are a good example of this. Depending upon how you connect the circuit and the pushbutton, you want the pin reading it to read consistently HIGH or LOW when the button is open, and the opposite when it is closed. Connecting the signal line through a pull-up resistor means the state will always be connected to 5 V when the button is open, through the resistor. Conversely, when the push button closes, the current follows the path of least resistance and connects to ground driving the signal LOW. Below is a schematic illustrating this situation:

weak pull up programming

Back to Top

IOCP & IOCN

Interrupt on Change (IOC) initiates an interrupt when the pin’s external logic level (voltage) changes. IOCP (Interrupt-on-Change Positive going transition) means it will send an interrupt when the level changes from low to high. IOCN (Interrupt-on-Change Negative going transition) means it will send an interrupt when the level changes from high to low. This functionality is more advanced, and for most cases you should not need to check either of these boxes.

Back to Top

Using Basic Software Functionality in main.c File

shows the pin_manager.h and pin_manager.c files

In the Project window, under MCC Generated Files, you can find the full list of macros for use with a GPIO pin under the pin_manager.h header file. However, for most cases, there are only four functions that you need to be aware of.

Software FunctionWhat it does
IO_RB0_SetHigh()This macro sets the pin referred to in red HIGH, which is 3.3V in physical space or 1 in the digital space.
IO_RB0_SetLow()This macro sets the pin referred to in red LOW, which is 0V in physical space or 0 in the digital space.
IO_RB0_Toggle()This macro sets the pin referred to in red to the opposite state of whatever it was last read or set to be.
IO_RB0_GetValue()This macro reads the current state of the pin referred to in red.

Back to Top

Example Code

If pin RB0 was given the custom name Pin1, and RB1 the custom name Pin2, then:

while (1)
    {
        Pin1_SetHigh();  // Sets Pin1, or RB0, HIGH
       __delay_ms(1000);
        Pin2_SetLow(); //Sets Pin2, or RB1, LOW
       __delay_ms(1000);
        Pin1_Toggle(); //Sets Pin1 LOW
       __delay_ms(50);
        Pin2_Toggle(); //Sets Pin2 LOW
       __delay_ms(50);
        Pin1_GetValue(); //Returns Low, or 0
       Pin2_GetValue(); //Returns HIGH, or 1
   }

Back to Top

Summary

Using GPIO is at the heart of just about any microcontroller project and MPLAB X IDE teamed with the MCC makes setting all this up easy and effective to use. 

Back to Top