GPIO Input
Introduction
The MikroSDK.Driver.GPIO.In
library provides an easy-to-use interface for managing GPIO (General Purpose Input/Output) input pins on microcontroller units.
It simplifies the process of reading digital signals from GPIO pins by abstracting the underlying hardware details.
This library is essential for applications requiring digital input signals, such as reading button states, sensor outputs, and other similar tasks.
API Name
- MikroSDK.Driver.GPIO.In
Location of Files and Directories
-
GitHub Repository:
Prerequisites
-
Library Manager:
- Select Driver.GPIO.In to add the library to your project using the Library Manager in NECTO Studio.
-
Headers:
- Include the Driver.GPIO.In header in your source files to access the GPIO output functions.
- This header pops up in the lower right part of NECTO Studio once you perform the previous task (while selecting Driver.GPIO.In in Library Manager)
- Alternatively, include the drv_gpio_in.h header in your source files to access the GPIO input 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.In library.
Code Examples
- How to initialize a GPIO pin as digital input?
/* Project name:
* Digital Input (with Digital Output)
* Description:
* This example demonstrates digital input (and digital output) functionalities.
* Turn on switches for PORT LED and BUTTON in the BOARD SETUP section of your board
* and see how the LED responds to pushing adequate button.
* Note:
* Ensure that `Driver.GPIO.In`, `Driver.GPIO.Out` and `Board` libraries are
* enabled in NECTO's Library Manager to ensure a successful build.
*/
// ------------------------------------------------------------------ INCLUDES
/**
* 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_digital_in.h"
#include "drv_digital_out.h"
#include "board.h"
#define LED_0 PA5 // TODO: Replace with a pin name which has LED connected to it.
#define BUTTON_0 PA0 // TODO: Replace with a pin name which has button connected to it.
// ------------------------------------------------------------------ VARIABLES
static digital_in_t input_pin_0; // Digital input driver context structure.
static digital_out_t output_pin_0; // Digital output driver context structure.
/**
* @brief This function initializes pins.
*/
void pin_init( void ) {
// Initializes digital output driver context structure and GPIO pin as digital output.
digital_out_init( &output_pin_0, LED_0 );
// Initializes digital input driver context structure and GPIO pin as digital input.
digital_in_init( &input_pin_0, BUTTON_0 );
}
// ------------------------------------------------------------------ USER CODE
int main( void ) {
/* Do not remove this line or clock might not be set correctly. */
#ifdef PREINIT_SUPPORTED
preinit( );
#endif
pin_init( );
while( 1 ) {
if( digital_in_read( &input_pin_0 ) ) {
digital_out_write( &output_pin_0, 1 );
} else {
digital_out_write( &output_pin_0, 0 );
}
}
return 0;
}
// ------------------------------------------------------------------------ END
Once you've copied the code snippet into the main.c
file located in NECTO Studio's Project Explorer, ensure that Driver.GPIO.In
, Driver.GPIO.Out
and Board
libraries are enabled in NECTO Studio's Library Manager to ensure a successful build.
- Button code example
/* Project name:
* Button code example
* Description:
* This example demonstrates "Button effect" via digital input (and digital output) functionalities.
* Turn on switches for PORT LED and BUTTON in the BOARD SETUP section of your board
* and see how the LED responds to pushing adequate button.
* Note:
* Ensure that `Driver.GPIO.In`, `Driver.GPIO.Out` and `Board` libraries are
* enabled in NECTO's Library Manager to ensure a successful build.
*/
// ------------------------------------------------------------------ INCLUDES
/**
* 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_digital_in.h"
#include "drv_digital_out.h"
#include "board.h"
#define LED_0 PA5 // TODO: Replace with a pin name which has LED connected to it.
#define BUTTON_0 PA0 // TODO: Replace with a pin name which has button connected to it.
// ------------------------------------------------------------------ VARIABLES
static digital_in_t input_pin_0; // Digital input driver context structure.
static digital_out_t output_pin_0; // Digital output driver context structure.
/**
* @brief This function initializes pins.
*/
void pin_init( void ) {
// Initializes digital output driver context structure and GPIO pin as digital output.
digital_out_init( &output_pin_0, LED_0 );
// Initializes digital input driver context structure and GPIO pin as digital input.
digital_in_init( &input_pin_0, BUTTON_0 );
}
// ------------------------------------------------------------------ USER CODE
int main( void ) {
/* Do not remove this line or clock might not be set correctly. */
#ifdef PREINIT_SUPPORTED
preinit( );
#endif
pin_init( );
while( 1 ) {
if( digital_in_read( &input_pin_0 ) ) {
digital_out_write( &output_pin_0, 1 );
} else {
digital_out_write( &output_pin_0, 0 );
}
}
return 0;
}
// ------------------------------------------------------------------------ END
Once you've copied the code snippet into the main.c
file located in NECTO Studio's Project Explorer, ensure that Driver.GPIO.In
, Driver.GPIO.Out
and Board
libraries are enabled in NECTO Studio's Library Manager to ensure a successful build.