Microchip® Advanced Software Framework

Quick start guide for the common IOPORT service

This is the quick start guide for the Common IOPORT API, with step-by-step instructions on how to configure and use the service in a selection of use cases.

The use cases contain several code fragments. The code fragments in the steps for setup can be copied into a custom initialization function, while the steps for usage can be copied into, e.g., the main application function.

Basic use case

In this use case we will configure one IO pin for button input and one for LED control. Then it will read the button state and output it on the LED.

Setup steps

Example code

#define MY_LED IOPORT_CREATE_PIN(PORTA, 5)
#define MY_BUTTON IOPORT_CREATE_PIN(PORTA, 6)

Workflow

  1. It's useful to give the GPIOs symbolic names and this can be done with the IOPORT_CREATE_PIN macro. We define one for a LED and one for a button.
    • #define MY_LED IOPORT_CREATE_PIN(PORTA, 5)
      #define MY_BUTTON IOPORT_CREATE_PIN(PORTA, 6)
    • Note
      The usefulness of the IOPORT_CREATE_PIN macro and port names differ between architectures:
      • MEGA, MEGA_RF and XMEGA: Use IOPORT_CREATE_PIN macro with port definitions PORTA, PORTB ...
      • UC3: Most convenient to pick up the device header file pin definition and us it directly. E.g.: AVR32_PIN_PB06
      • SAM: Most convenient to pick up the device header file pin definition and us it directly. E.g.: PIO_PA5_IDX
        IOPORT_CREATE_PIN can also be used with port definitions PIOA, PIOB ...
  2. Initialize the ioport service. This typically enables the IO module if needed.
  3. Set the LED GPIO as output:
  4. Set the button GPIO as input:
  5. Enable pull-up for the button GPIO:

Usage steps

Example code

bool value;
value = ioport_get_pin_level(MY_BUTTON);
ioport_set_pin_level(MY_LED, value);

Workflow

  1. Define a boolean variable for state storage:
    • bool value;
  2. Read out the button level into variable value:
  3. Set the LED to read out value from the button:

Advanced use cases