General Purpose Input/Output (GPIO) pins on an 8-bit PIC® MCU within MPLAB® X are quite easy to setup 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, 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 below:
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.
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.
It should appear as shown below:
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 upon how many peripherals are using it. Therefore, for each of these instances, a row appears for the accompanying function, along with the module that 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 configuration.
Start High
Start High defines if the pin will begin the program as logic HIGH, or VDD – 0.7 V.
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.
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 (5 V). 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:
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.
Using Basic Software Functionality in main.c File
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 Function | What it does |
---|---|
IO_RB0_SetHigh() | This macro sets the pin referred to in red HIGH, which is 3.3 V in physical space or 1 in the digital space. |
IO_RB0_SetLow() | This macro sets the pin referred to in red LOW, which is 0 V 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. |
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 }
Summary
Using GPIO is at the heart of just about any microcontroller project and MPLAB X teamed with the MCC makes setting all this up easy and effective to use. Want help trying this out? Check out the Digital Output example project that is part of a 12 project self-paced training on using MCC.