Microchip® Advanced Software Framework

Quick Start Guide for PORT - Basic

In this use case, the PORT module is configured for:

  • One pin in input mode, with pull-up enabled
  • One pin in output mode

This use case sets up the PORT to read the current state of a GPIO pin set as an input, and mirrors the opposite logical state on a pin configured as an output.

Setup

Prerequisites

There are no special setup requirements for this use-case.

Code

Copy-paste the following setup code to your user application:

{
struct port_config config_port_pin;
port_get_config_defaults(&config_port_pin);
config_port_pin.direction = PORT_PIN_DIR_INPUT;
config_port_pin.input_pull = PORT_PIN_PULL_UP;
port_pin_set_config(BUTTON_0_PIN, &config_port_pin);
config_port_pin.direction = PORT_PIN_DIR_OUTPUT;
port_pin_set_config(LED_0_PIN, &config_port_pin);
}

Add to user application initialization (typically the start of main()):

Workflow

  1. Create a PORT module pin configuration struct, which can be filled out to adjust the configuration of a single port pin.
    struct port_config config_port_pin;
  2. Initialize the pin configuration struct with the module's default values.
    port_get_config_defaults(&config_port_pin);
    Note
    This should always be performed before using the configuration struct to ensure that all values are initialized to known default settings.
  3. Adjust the configuration struct to request an input pin.
    config_port_pin.direction = PORT_PIN_DIR_INPUT;
    config_port_pin.input_pull = PORT_PIN_PULL_UP;
  4. Configure push button pin with the initialized pin configuration struct, to enable the input sampler on the pin.
    port_pin_set_config(BUTTON_0_PIN, &config_port_pin);
  5. Adjust the configuration struct to request an output pin.
    config_port_pin.direction = PORT_PIN_DIR_OUTPUT;
    Note
    The existing configuration struct may be re-used, as long as any values that have been altered from the default settings are taken into account by the user application.
  6. Configure LED pin with the initialized pin configuration struct, to enable the output driver on the pin.
    port_pin_set_config(LED_0_PIN, &config_port_pin);

Use Case

Code

Copy-paste the following code to your user application:

while (true) {
bool pin_state = port_pin_get_input_level(BUTTON_0_PIN);
port_pin_set_output_level(LED_0_PIN, !pin_state);
}

Workflow

  1. Read in the current input sampler state of push button pin, which has been configured as an input in the use-case setup code.
    bool pin_state = port_pin_get_input_level(BUTTON_0_PIN);
  2. Write the inverted pin level state to LED pin, which has been configured as an output in the use-case setup code.
    port_pin_set_output_level(LED_0_PIN, !pin_state);