Skip to content

GPIO Output

Introduction

GPIO Output API - MikroSDK.Driver.GPIO.Out - provides a convenient and efficient interface for managing GPIO (General Purpose Input/Output) output pins on microcontroller units.

It abstracts the complexities involved in directly manipulating the hardware registers, offering a standardized API for configuring and controlling GPIO output pins.

This library is essential for applications requiring digital signal control, such as toggling LEDs, driving relays, and other similar tasks.

API Name

  • MikroSDK.Driver.GPIO.Out

API Files

Prerequisites

  • Library Manager:

    • Select Driver.GPIO.Out to add the API to your project using the Library Manager in NECTO Studio.
  • Headers:

    • Include the Driver.GPIO.Out 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.Out in Library Manager)
    • Alternatively, include the drv_gpio_out.h header in your source files to access the GPIO output 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.Out library.

Code Examples

  • LED Blink example
/**
 * 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 "board.h"           // Board-specific definitions
#include "drv_digital_out.h" // Digital output driver for controlling pins
#include "delays.h"          // Delay functions

#define LED_PIN PA0           // Define the GPIO pin for the LED (adjust based on your board)

int main(void)
{
    /* Do not remove this line or clock might not be set correctly. */
    #ifdef PREINIT_SUPPORTED
    preinit();
    #endif

    // Step 1: Initialize the Digital Out driver for the LED pin
    digital_out_t led;

    // Initialize the pin as a digital output (PA0 or other pin connected to your LED)
    digital_out_init(&led, LED_PIN);

    // Step 2: Main loop for blinking the LED
    while ( 1 ) {
        // Turn the LED ON
        if ( DIGITAL_OUT_SUCCESS == digital_out_high( &led ) ) { // Set the pin to HIGH
            // No error
        } else {
            // Handle the error
        }

        Delay_ms(500); // Delay for 500 milliseconds

        // Turn the LED OFF
        if ( DIGITAL_OUT_SUCCESS == digital_out_low( &led ) ) { // Set the pin to LOW
            // No error
        } else {
            // Handle the error
        }

        Delay_ms(500); // Delay for 500 milliseconds
    }

    return 0;
}

Once you've copied the code snippet into the main.c file located in NECTO Studio's Project Explorer, ensure that both the Board and Driver.GPIO.Out libraries are enabled in NECTO Studio's Library Manager to ensure a successful build.

  • How to initialize a GPIO pin as digital output?
/**
 * @file main.c
 * @brief Code example on how to initialize a GPIO pin as digital output
 */

/**
 * 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 "board.h"           // Board-specific definitions
#include "drv_digital_out.h" // Digital output driver for controlling pins
#include "delays.h"          // Delay functions

// Step 1: Initialize the Digital Out driver for the LED pin
static digital_out_t output_pin;

int main( void )
{
    /* Do not remove this line or clock might not be set correctly. */
    #ifdef PREINIT_SUPPORTED
    preinit();
    #endif

    // Initialize the pin as a digital output (PD12 or other pin connected to your LED)
    if ( DIGITAL_OUT_SUCCESS == digital_out_init( &output_pin, PD12 ) ) {
        // No error
    } else {
        // Handle the error
    }

    // Step 2: Main loop for blinking the LED
    while ( 1 ) {
        // Turn the LED ON
        if ( DIGITAL_OUT_SUCCESS == digital_out_high( &output_pin ) ) { // Set the pin to HIGH
            // No error
        } else {
            // Handle the error
        }

        Delay_ms(500); // Delay for 500 milliseconds

        // Turn the LED OFF
        if ( DIGITAL_OUT_SUCCESS == digital_out_low( &output_pin ) ) { // Set the pin to LOW
            // No error
        } else {
            // Handle the error
        }

        Delay_ms(500); // Delay for 500 milliseconds
    }

    return 0;
}

Once you've copied the code snippet into the main.c file located in NECTO Studio's Project Explorer, ensure that both the Board and Driver.GPIO.Out libraries are enabled in NECTO Studio's Library Manager to ensure a successful build.

  • How can I set a GPIO pin as output in NECTO?
/**
 * @file main.c
 * @brief Code example on how to set a GPIO pin as output in NECTO
 */

/**
 * 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 "board.h"           // Board-specific definitions
#include "drv_digital_out.h" // Digital output driver for controlling pins
#include "delays.h"          // Delay functions

// Step 1: Initialize the Digital Out driver for the LED pin
static digital_out_t output_pin;

int main( void )
{
    /* Do not remove this line or clock might not be set correctly. */
    #ifdef PREINIT_SUPPORTED
    preinit();
    #endif

    // Initialize the pin as a digital output (PD12 or other pin connected to your LED)
    if ( DIGITAL_OUT_SUCCESS == digital_out_init( &output_pin, PD12 ) ) {
        // No error
    } else {
        // Handle the error
    }

    // Step 2: Main loop for blinking the LED
    while ( 1 ) {
        // Turn the LED ON
        if ( DIGITAL_OUT_SUCCESS == digital_out_high( &output_pin ) ) { // Set the pin to HIGH
            // No error
        } else {
            // Handle the error
        }

        Delay_ms(500); // Delay for 500 milliseconds

        // Turn the LED OFF
        if ( DIGITAL_OUT_SUCCESS == digital_out_low( &output_pin ) ) { // Set the pin to LOW
            // No error
        } else {
            // Handle the error
        }

        Delay_ms(500); // Delay for 500 milliseconds
    }

    return 0;
}

Once you've copied the code snippet into the main.c file located in NECTO Studio's Project Explorer, ensure that both the Board and Driver.GPIO.Out libraries are enabled in NECTO Studio's Library Manager to ensure a successful build.

  • How to write a high or low value to a GPIO pin?
/**
 * @file main.c
 * @brief Code example on how to write a high or low value to a GPIO pin
 */

/**
 * 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 "board.h"           // Board-specific definitions
#include "drv_digital_out.h" // Digital output driver for controlling pins
#include "delays.h"          // Delay functions

// Step 1: Initialize the Digital Out driver for the LED pin
static digital_out_t output_pin;

int main( void )
{
    /* Do not remove this line or clock might not be set correctly. */
    #ifdef PREINIT_SUPPORTED
    preinit();
    #endif

    // Initialize the pin as a digital output (PD12 or other pin connected to your LED)
    if ( DIGITAL_OUT_SUCCESS == digital_out_init( &output_pin, PD12 ) ) {
        // No error
    } else {
        // Handle the error
    }

    // Step 2: Main loop for blinking the LED
    while ( 1 ) {
        // Turn the LED ON
        if ( DIGITAL_OUT_SUCCESS == digital_out_high( &output_pin ) ) { // Set the pin to HIGH
            // No error
        } else {
            // Handle the error
        }

        Delay_ms(500); // Delay for 500 milliseconds

        // Turn the LED OFF
        if ( DIGITAL_OUT_SUCCESS == digital_out_low( &output_pin ) ) { // Set the pin to LOW
            // No error
        } else {
            // Handle the error
        }

        Delay_ms(500); // Delay for 500 milliseconds
    }

    return 0;
}

Once you've copied the code snippet into the main.c file located in NECTO Studio's Project Explorer, ensure that both the Board and Driver.GPIO.Out libraries are enabled in NECTO Studio's Library Manager to ensure a successful build.

There is another way to write a high or low value to a GPIO pin as well. Here's a simple code example:

/**
 * @file main.c
 * @brief Another code example on how to write a high or low value to a GPIO pin
 */

/**
 * 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 "board.h"           // Board-specific definitions
#include "drv_digital_out.h" // Digital output driver for controlling pins
#include "delays.h"          // Delay functions

// Step 1: Initialize the Digital Out driver for the LED pin
static digital_out_t output_pin;

int main( void )
{
    /* Do not remove this line or clock might not be set correctly. */
    #ifdef PREINIT_SUPPORTED
    preinit();
    #endif

    // Initialize the pin as a digital output (PD12 or other pin connected to your LED)
    if ( DIGITAL_OUT_SUCCESS == digital_out_init( &output_pin, PD12 ) ) {
        // No error
    } else {
        // Handle the error
    }

    // Step 2: Main loop for blinking the LED
    while ( 1 ) {
        // Turn the LED ON
        if ( DIGITAL_OUT_SUCCESS == digital_out_write( &output_pin, 1 ) ) { // Set the pin to HIGH
            // No error
        } else {
            // Handle the error
        }

        Delay_ms(500); // Delay for 500 milliseconds

        // Turn the LED OFF
        if ( DIGITAL_OUT_SUCCESS == digital_out_write( &output_pin, 0 ) ) { // Set the pin to LOW
            // No error
        } else {
            // Handle the error
        }

        Delay_ms(500); // Delay for 500 milliseconds
    }

    return 0;
}

Once you've copied the code snippet into the main.c file located in NECTO Studio's Project Explorer, ensure that both the Board and Driver.GPIO.Out libraries are enabled in NECTO Studio's Library Manager to ensure a successful build.

  • How to toggle pin state of a GPIO pin?
/**
 * @file main.c
 * @brief Code example on how to toggle pin state of a GPIO pin
 * Note:
 *   Ensure that `Driver.GPIO.In`, `Driver.GPIO.Out` 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 "board.h"           // Board-specific definitions
#include "drv_digital_out.h" // Digital output driver for controlling pins
#include "delays.h"          // Delay functions

// Step 1: Initialize the Digital Out driver for the LED pin
static digital_out_t output_pin;

int main( void )
{
    /* Do not remove this line or clock might not be set correctly. */
    #ifdef PREINIT_SUPPORTED
    preinit();
    #endif

    // Initialize the pin as a digital output (PD12 or other pin connected to your LED)
    if ( DIGITAL_OUT_SUCCESS == digital_out_init( &output_pin, PD12 ) ) {
        // No error
    } else {
        // Handle the error
    }

    // Step 2: Main loop for LED pin toggle
    while ( 1 ) {
        // Turn the LED ON and OFF each second.
        if ( DIGITAL_OUT_SUCCESS == digital_out_toggle( &output_pin ) ) {
            // No error
        } else {
            // Handle the error
        }
        Delay_ms(1000);
    }

    return 0;
}

Once you've copied the code snippet into the main.c file located in NECTO Studio's Project Explorer, ensure that both the Board and Driver.GPIO.Out libraries are enabled in NECTO Studio's Library Manager to ensure a successful build.