General Purpose Input/Output (GPIO) pins on an 8-bit PIC® MCU within MPLAB® Xpress are quite easy to set up and use, thanks to the simple interface of the MPLAB Code Configurator (MCC) tool for MPLAB Xpress. 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 like 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 MCC within MPLAB Xpress, 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 the custom name section of the Pin Module screen.
Module: This 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: Defines if the pin will begin the program as logic HIGH, or VDD – 0.7V.
Analog: Users seeking to use the GPIO pin as a digital input, make sure this box is unchecked. Checking the box will enable it as a 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 by a variety of environmental factors, it may cause your pin to fluctuate between HIGH and LOW.
For instance, pushbuttons are a good example of this. Depending on 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 of this situation:
IOCP & IOCN: Interrupt on Change (IOC) initiates an interrupt when the pin’s external logic level (voltage) changes. Interrupt-on-Change Positive (IOCP) going transition means it will send an interrupt when the level changes from low to high. Interrupt-on-Change Negative (IOCN) going transition means it will send an interrupt when the level changes from high to low. This functionality is more advanced and basic users do 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 basic users, there are only four necessary functions:
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 Xpress teamed with the MCC makes setting all this up easy and effective to use. Want help trying this out?
Check out the Hello World - Light LED example project.