GPIO Port
Introduction
The MikroSDK.Driver.GPIO.Port
library provides an interface for managing multiple GPIO (General Purpose Input/Output) pins grouped as a port on microcontroller units.
It simplifies the process of configuring and controlling GPIO ports by abstracting the underlying hardware details.
This library is essential for applications requiring efficient control of multiple GPIO pins, such as interfacing with parallel data buses, controlling multiple LEDs, and other similar tasks.
API Name
- MikroSDK.Driver.GPIO.Port
Location of Files and Directories
-
GitHub Repository:
Prerequisites
-
Library Manager:
-
Select Driver.GPIO.Port to add the library to your project using the Library Manager in NECTO Studio.
-
Headers:
- Include the Driver.GPIO.Port header in your source files to access the GPIO port functions.
- This header pops up in the lower right part of NECTO Studio once you perform the previous task (while selecting Driver.GPIO.Port in Library Manager)
- Alternatively, include the drv_port.h header in your source files to access the GPIO port functions.
-
CMakeLists:
- You do not have to perform anything, configuration (CMake) file is already taken care of automatically while you performed the first task in this Prerequisites!
- You can ensure that your CMakeLists.txt includes the necessary configurations to link against the MikroSDK.Driver.GPIO.Port library.
Code Examples
- How to configure PORT as output?
/* Project name:
* Set GPIO PORT as digital output - code example via mikroSDK v2.0 Framework.
* Description:
* This example demonstrates microcontroller PORT digital output configuration.
* Note:
* This example WILL NOT turn on LEDs, this example is just a configuration example.
* Ensure that `Driver.GPIO.Port` and `Board` libraries are
* enabled in NECTO's Library Manager to ensure a successful build.
*/
/**
* Any initialization code needed for MCU to function properly.
* Do not remove this line or clock might not be set correctly.
*/
#ifdef PREINIT_SUPPORTED
#include "preinit.h"
#endif
#include "drv_port.h" // API for GPIO PORT driver.
#define PORT_NAME PORT_B // TODO: Define PORT.
#define PORT_MASK (port_size_t)0xFFFFFFFF // TODO: Define PORT mask.
static port_t test_port; // PORT driver context structure.
int main( void )
{
/* Do not remove this line or clock might not be set correctly. */
#ifdef PREINIT_SUPPORTED
preinit();
#endif
// Proper PORT initialization as a digital output (via mikroSDK v2.0 Framework).
if ( PORT_SUCCESS == port_init( &test_port, PORT_NAME, PORT_MASK, PIN_DIRECTION_DIGITAL_OUTPUT ) ) {
// No error
} else {
// Handle the error
}
return 0;
}
Once you've copied the code snippet into the main.c
file located in NECTO Studio's Project Explorer, ensure that Driver.GPIO.Port
and Board
(to leverage mikroBUS™-based pins) libraries are enabled in NECTO Studio's Library Manager to ensure a successful build.
- How to configure PORT as input?
/* Project name:
* Set GPIO PORT as digital input - code example via mikroSDK v2.0 Framework.
* Description:
* This example demonstrates microcontroller PORT digital input configuration.
* Notes:
* - Ensure that `Driver.GPIO.Port` and `Board` libraries are
* enabled in NECTO's Library Manager to ensure a successful build.
* - Turn on switches for PORT LEDs and BUTTONS in the BOARD SETUP section of your board
* as well as UP-PULL-DOWN switch to enable pull-up/down resistor to be able to modify
* input state of a PORT.
*/
/**
* Any initialization code needed for MCU to function properly.
* Do not remove this line or clock might not be set correctly.
*/
#ifdef PREINIT_SUPPORTED
#include "preinit.h"
#endif
#include "drv_port.h" // API for GPIO PORT driver.
#define PORT_NAME PORT_B // TODO: Define PORT.
#define PORT_MASK (port_size_t)0xFFFFFFFF // TODO: Define PORT mask.
static port_t test_port; // PORT driver context structure.
int main( void )
{
/* Do not remove this line or clock might not be set correctly. */
#ifdef PREINIT_SUPPORTED
preinit();
#endif
// Proper PORT initialization as a digital input (via mikroSDK v2.0 Framework).
if ( PORT_SUCCESS == port_init( &test_port, PORT_NAME, PORT_MASK, PIN_DIRECTION_DIGITAL_INPUT ) ) {
// No error
} else {
// Handle the error
}
return 0;
}
Once you've copied the code snippet into the main.c
file located in NECTO Studio's Project Explorer, ensure that Driver.GPIO.Port
and Board
(to leverage mikroBUS™-based pins) libraries are enabled in NECTO Studio's Library Manager to ensure a successful build.
- How to Write a Value to a PORT?
/* Project name:
* Write a value to GPIO PORT - code example via mikroSDK v2.0 Framework.
* Description:
* This example demonstrates microcontroller PORT digital output configuration
* and writing the output value to the MCU's PORT.
* Note:
* - This example WILL turn on dev board LEDs.
* - Ensure that `Driver.GPIO.Port` and `Board` libraries are
* enabled in NECTO's Library Manager to ensure a successful build.
*/
/**
* Any initialization code needed for MCU to function properly.
* Do not remove this line or clock might not be set correctly.
*/
#ifdef PREINIT_SUPPORTED
#include "preinit.h"
#endif
#include "drv_port.h" // API for GPIO PORT driver.
#define PORT_NAME PORT_B // TODO: Define PORT.
#define PORT_MASK (port_size_t)0xFFFFFFFF // TODO: Define PORT mask.
static port_t test_port; // PORT driver context structure.
int main( void )
{
/* Do not remove this line or clock might not be set correctly. */
#ifdef PREINIT_SUPPORTED
preinit();
#endif
// Proper PORT initialization as a digital output (via mikroSDK v2.0 Framework).
if ( PORT_SUCCESS == port_init( &test_port, PORT_NAME, PORT_MASK, PIN_DIRECTION_DIGITAL_OUTPUT ) ) {
// No error
} else {
// Handle the error
}
while ( 1 ) {
// Writing to the port (All LED's should turn on).
if ( PORT_SUCCESS == port_write( &test_port, PORT_MASK ) ) {
// No error
} else {
// Handle the error
}
Delay_1sec(); // Delay for 1 second
// Writing to the port (All LED's should turn off).
if ( PORT_SUCCESS == port_write( &test_port, ~PORT_MASK ) ) {
// No error
} else {
// Handle the error
}
Delay_1sec(); // Delay for 1 second
}
return 0;
}
Once you've copied the code snippet into the main.c
file located in NECTO Studio's Project Explorer, ensure that Driver.GPIO.Port
and Board
(to leverage mikroBUS™-based pins) libraries are enabled in NECTO Studio's Library Manager to ensure a successful build.
- How to Read a Value (output value) from a PORT?
/* Project name:
* Read an output value of an GPIO PORT - code example via mikroSDK v2.0 Framework.
* Description:
* This example demonstrates microcontroller PORT digital output configuration,
* writing the output value to the MCU's PORT, then subsequently reading its
* output value.
* Note:
* - This example WILL turn on dev board LEDs.
* - Ensure that `Driver.GPIO.Port` and `Board` libraries are
* enabled in NECTO's Library Manager to ensure a successful build.
*/
/**
* Any initialization code needed for MCU to function properly.
* Do not remove this line or clock might not be set correctly.
*/
#ifdef PREINIT_SUPPORTED
#include "preinit.h"
#endif
#include "drv_port.h" // API for GPIO PORT driver.
#define PORT_NAME PORT_B // TODO: Define PORT.
#define PORT_MASK (port_size_t)0xFFFFFFFF // TODO: Define PORT mask.
#define PORT_READ_VALUE 0xAA
static port_t test_port; // PORT driver context structure.
int main( void )
{
/* Do not remove this line or clock might not be set correctly. */
#ifdef PREINIT_SUPPORTED
preinit();
#endif
// Proper PORT initialization as a digital output (via mikroSDK v2.0 Framework).
if ( PORT_SUCCESS == port_init( &test_port, PORT_NAME, PORT_MASK, PIN_DIRECTION_DIGITAL_OUTPUT ) ) {
// No error
} else {
// Handle the error
}
// Writing to the port (All LED's should turn on).
if ( PORT_SUCCESS == port_write( &test_port, PORT_READ_VALUE ) ) {
// No error
} else {
// Handle the error
}
Delay_1sec(); // Delay for 1 second
// Check output value of the PORT that had been read.
if ( PORT_READ_VALUE != port_read( &test_port ) ) {
// Handle the error
}
return 0;
}
Once you've copied the code snippet into the main.c
file located in NECTO Studio's Project Explorer, ensure that Driver.GPIO.Port
and Board
(to leverage mikroBUS™-based pins) libraries are enabled in NECTO Studio's Library Manager to ensure a successful build.
- How to Read an Input Value from a PORT?
/* Project name:
* Read an input value of an GPIO PORT - code example via mikroSDK v2.0 Framework.
* Description:
* This example demonstrates microcontroller PORT digital input configuration,
* setting the input value of the MCU's PORT by pressing and holding a button,
* then subsequently reading PORT's input value.
* Note:
* - How to run this code example on a hardware:
* - Make button actions on your board available by enabling BUTTONS switch;
* - Run debug mode by pressing F9 on your keyboard;
* - Add `read_port_input` variable into Debugger's Watch Window;
* - Press (and hold) some button on your dev board (i.e. button on pin PB6);
* - Observe the value inside `read_port_input` variable: it must reflect
* the current voltage state (HIGH or LOW).
* - Ensure:
* - `Driver.GPIO.Port` and `Board` libraries are
* enabled in NECTO's Library Manager to ensure a successful build.
*/
/**
* Any initialization code needed for MCU to function properly.
* Do not remove this line or clock might not be set correctly.
*/
#ifdef PREINIT_SUPPORTED
#include "preinit.h"
#endif
#include "drv_port.h" // API for GPIO PORT driver.
#define PORT_NAME PORT_B // TODO: Define desired PORT.
#define PORT_MASK (port_size_t)0xFFFFFFFF // TODO: Define desired PORT mask.
#define PORT_READ_VALUE 0xAA // TODO: Define desired PORT read value.
static port_size_t read_port_input; // Retrieve port input value.
static port_t test_port; // PORT driver context structure.
int main( void )
{
/* Do not remove this line or clock might not be set correctly. */
#ifdef PREINIT_SUPPORTED
preinit();
#endif
// Proper PORT initialization as a digital input (via mikroSDK v2.0 Framework).
if ( PORT_SUCCESS == port_init( &test_port, PORT_NAME, PORT_MASK, PIN_DIRECTION_DIGITAL_INPUT ) ) {
// No error
} else {
// Handle the error
}
Delay_ms(500); // Delay for 500 milliseconds
// Check the input value of the PORT.
read_port_input = port_read_input( &test_port );
return 0;
}
Once you've copied the code snippet into the main.c
file located in NECTO Studio's Project Explorer, ensure that Driver.GPIO.Port
and Board
(to leverage mikroBUS™-based pins) libraries are enabled in NECTO Studio's Library Manager to ensure a successful build.