Microchip® Advanced Software Framework

Advanced use case - Interrupt driven edge detection

Advanced use case - Interrupt driven edge detection

This section will present a more advanced use case for the PIO driver. This use case will configure pin 23 on port A as output and pin 16 as an input with pullup, and then toggle the output pin's value to match that of the input pin using the interrupt controller within the device.

Prerequisites

  • Power Management Controller driver

Initialization code

Add to the application initialization code:

pio_set_output(PIOA, PIO_PA23, LOW, DISABLE, ENABLE);
pio_set_input(PIOA, PIO_PA16, PIO_PULLUP);
pio_handler_set(PIOA, ID_PIOA, PIO_PA16, PIO_IT_EDGE, pin_edge_handler);
pio_enable_interrupt(PIOA, PIO_PA16);
NVIC_EnableIRQ(PIOA_IRQn);

Workflow

  1. Enable the module clock to the PIOA peripheral:
  2. Set pin 23 direction on PIOA as output, default low level:
    pio_set_output(PIOA, PIO_PA23, LOW, DISABLE, ENABLE);
  3. Set pin 16 direction on PIOA as input, with pullup:
    pio_set_input(PIOA, PIO_PA16, PIO_PULLUP);
  4. Configure the input pin 16 interrupt mode and handler:
    pio_handler_set(PIOA, ID_PIOA, PIO_PA16, PIO_IT_EDGE, pin_edge_handler);
  5. Enable the interrupt for the configured input pin:
    pio_enable_interrupt(PIOA, PIO_PA16);
  6. Enable interrupt handling from the PIOA module:
    NVIC_EnableIRQ(PIOA_IRQn);

Example code

Add the following function to your application:

void pin_edge_handler(const uint32_t id, const uint32_t index)
{
if ((id == ID_PIOA) && (index == PIO_PA16)){
if (pio_get(PIOA, PIO_TYPE_PIO_INPUT, PIO_PA16))
pio_clear(PIOA, PIO_PA23);
else
pio_set(PIOA, PIO_PA23);
}
}

Workflow

  1. We check the value of the pin:
    if (pio_get(PIOA, PIO_TYPE_PIO_INPUT, PIO_PA16))
  2. Then we set the new output value based on the read pin value:
    pio_clear(PIOA, PIO_PA23);
    else
    pio_set(PIOA, PIO_PA23);