Skip to content

Digital Out Documentation

Introduction

The Digital Out library within the mikroSDK ecosystem provides a standardized interface for controlling general-purpose output (GPIO) pins on a microcontroller. This library simplifies the process of configuring and manipulating GPIO pins as digital outputs, allowing developers to easily set, clear, toggle, and read the output state of these pins. The library promotes hardware abstraction, making the code more portable across different microcontrollers supported by mikroSDK. Key features include initialization, setting high/low states, toggling, writing, and reading pin values.

API Overview

  • digital_out_err_t — Enumeration of possible return values for the Digital Out functions, including DIGITAL_OUT_SUCCESS and DIGITAL_OUT_UNSUPPORTED_PIN.
  • digital_out_t — Structure representing the digital output driver context, containing the hal_gpio_pin_t which specifies the pin number.
  • digital_out_init(digital_out_t *out, pin_name_t name) — Initializes the digital output pin specified by name.
  • digital_out_high(digital_out_t *out) — Sets the digital output pin specified by out to a logic high state.
  • digital_out_low(digital_out_t *out) — Sets the digital output pin specified by out to a logic low state.
  • digital_out_toggle(digital_out_t *out) — Toggles the digital output pin specified by out between logic high and low states.
  • digital_out_write(digital_out_t *out, uint8_t value) — Writes a digital value (0 or 1) to the output pin specified by out.
  • digital_out_read(digital_out_t *out) — Reads the current output value of the pin specified by out, returning 0 or 1.

Examples

#include "drv_digital_out.h"

// Digital output driver context structure.
static digital_out_t led;

void main() {
    // Initializes digital output driver context structure and individual GPIO pin as digital output.
    if ( DIGITAL_OUT_SUCCESS == digital_out_init( &led, HAL_PIN_PA0 ) ) {
        // No error
    } else {
        // Handle the error
        while(1); // Infinite loop to indicate error
    }

    while (1) {
        // Set the LED pin to high (on)
        digital_out_high( &led );
        Delay_ms(1000);

        // Set the LED pin to low (off)
        digital_out_low( &led );
        Delay_ms(1000);

        // Toggle the LED pin
        digital_out_toggle( &led );
        Delay_ms(1000);

        // Write a logic high to the pin (on)
        digital_out_write( &led, 1 );
        Delay_ms(1000);

        //Write a logic low to the pin (off)
        digital_out_write( &led, 0 );
        Delay_ms(1000);

        // Read the pin's current output state
        uint8_t current_state = digital_out_read( &led );
        // Use the state, for example, in a conditional statement
        if(current_state == 1){
            //Do something
        }
    }
}

Notes

  • The specific GPIO pin names (e.g., GPIO_PB2, HAL_PIN_PA0) are hardware-dependent and must be adjusted based on the target microcontroller and development board. Refer to the hardware documentation for the correct pin assignments.
  • Ensure that the Delay_ms() function is available in your project, or use an alternative delay implementation suitable for your hardware platform. This function is necessary for the timing intervals shown in the examples.
  • The digital_out_init function configures the pin as a digital output. Be sure that the pin is not also configured for another purpose simultaneously (e.g., as an analog input or part of a communication peripheral).
  • The FLATTEN_ME macro affects how the functions are compiled, potentially inlining them for increased performance. Be mindful of this setting, and adjust accordingly based on project needs.