5.8 Pin Manager

5.8.1 Introduction

The MPLAB® Code Configurator (MCC) Melody Pin Manager System Driver generates APIs to support pin related functionality, both for peripherals and for General Purpose Input/Output (GPIO) on PIC16F/PIC18F target MCUs.

In the below Tech Brief, some of the same use cases are build up from scratch, using bare metal coding, as well as MCC.

5.8.2 Supported Device Families

All supported PIC10, PIC12, PIC16, and PIC18 device families.

5.8.3 Required Header Files

#include "mcc_generated_files/system/pins.h"

5.8.4 How to Use the Pin Manager System Driver

The links below provide examples for different use cases of the Pin Manager System driver.

For general instructions common to all examples, refer to this section: 5.8.5.1.1 PIC Pin Manager Use Case Code Snippet Instructions

5.8.5 Module Documentation

5.8.5.1 Pin Manager Use Cases

5.8.5.1.1 PIC Pin Manager Use Case Code Snippet Instructions

The use cases show example uses of the General Purpose Input/Output (GPIO), within a MCC Melody project:

  • Configure pins as inputs/outputs in the Pin Grid View.

  • These pins are then available in the Pins view, where the following can be configured:
    1. Custom Name

    2. Analog: Yes/No (Uncheck for digital only functionality).

    3. Start High: Yes/No (After SYSTEM_Initialize(), pin will be high).

    4. Weak Pullup: Yes/No (E.g. if Button connected to pin needs a pullup).

    5. Open Drain: Yes/No.

    6. Slew Rate: Yes/No (Limit the rate of change of the pin).

    7. Input Level Control: Yes/No.

    8. Interrupt on Change: None/Negative/Positive/Any. (e.g., One may want to sense a Negative edge on pin connected to an active low Button).

  • Generate the code

  • Add the code snippet(s) to the application code

  • Program the board

5.8.5.1.2 PIC Pin Manager Use Case: LED Toggle on 500ms Delay

A LED is toggled every 500 ms, at period set by a delay.  

System Configuration:
  • Pin Grid View:
    1. Select a LED pin (check schematic), configure as Output.

  • Pins:
    1. LED pin: Custom name "LED".

After configuring the components as described above, click 'Generate' to generate the code. Then add the following code snippets to your application:

#include "mcc_generated_files/system/system.h"
int main(void)
{
    SYSTEM_Initialize();

    while(1)
    {
        LED_Toggle();
        __delay_ms(500);
    }
}

5.8.5.1.3 PIC Pin Manager Use Case: LED ON when BUTTON Pressed (Polled)

A LED is turned on while a BUTTON is pressed. The program polls the BUTTON pin to detect that when it is pressed.  

System Configuration:
  • Pin Grid View:
    1. Select a LED pin (check schematic), configure as Output.

    2. Select a BUTTON pin (check schematic), configure as Input.

  • Pins:
    1. LED pin: Custom name "LED".

    2. BUTTON pin: Custom name "BUTTON"

    3. BUTTON pin: Enable Weak Pullup if needed (check schematic)

After configuring the components as described above, click 'Generate' to generate the code. Then add the following code snippets to your application:

#include "mcc_generated_files/system/system.h"
int main(void)
{
    SYSTEM_Initialize();

    while(1)
    {
        if (!BUTTON_GetValue())   //Check for active LOW button.  Remove "!" for active HIGH button.
        {  
            LED_SetLow();         //Assumes LED is active low.  Swap LED_SetLow()/LED_SetHigh() api for active High LED.
        } 
        else  
        {
            LED_SetHigh();
        }
    }
}

5.8.5.1.4 PIC Pin Manager Use Case: LED toggle on Button Press (Polled)

A LED is toggled ON/OFF every time a BUTTON is pressed. The program polls a pin, to detect the transition associated with a button press.  

System Configuration:
  • Pin Grid View:
    1. Select a LED pin (check schematic), configure as Output.

    2. Select a BUTTON pin (check schematic), configure as Input.

  • Pins:
    1. LED pin: Custom name "LED".

    2. BUTTON pin: Custom name "BUTTON"

    3. BUTTON pin: Enable Weak Pullup if needed (check schematic)

After configuring the components as described above, click 'Generate' to generate the code. Then add the following code snippets to your application:

#include "mcc_generated_files/system/system.h"

#define EDGE_DETECTED        (true)
#define EDGE_NOT_DETECTED    (false)
inline static bool NegEdgeDetectOnButton(void)
{
    static bool lastState = false;

    bool const currentState = BUTTON_GetValue() ? true : false;

    if (currentState != lastState && currentState == false)
    {
        lastState = currentState;
        return EDGE_DETECTED;
    }
    else
    {
        lastState = currentState;
        return EDGE_NOT_DETECTED;
    }
}

static void WaitButton(void)
{
    while (true)
    {
        while (NegEdgeDetectOnButton() == EDGE_NOT_DETECTED) { }

        __delay_ms(50);

        if (BUTTON_GetValue() == 0)
        {
            return;
        }
    }
}
void main(void)
{
    SYSTEM_Initialize();

    while (true)
    {
        WaitButton();
        LED_Toggle();
    }
}

5.8.5.1.5 PIC Pin Manager Use Case: LED toggle on Button Press (Pin Interrupt on Change)

A LED is toggled ON/OFF every time a BUTTON is pressed. An Interrupt on Change (IOC) is configured on the edge transition associated with pressing the BUTTON. Interrupts are disabled briefly to handle the BUTTON deboucing.  

System Configuration:
  • Pin Grid View:
    1. Select a LED pin (check schematic), configure as Output.

    2. Select a BUTTON pin (check schematic), configure as Input.

  • Pins:
    1. LED pin: Custom name "LED".

    2. BUTTON pin: Custom name "BUTTON"

    3. BUTTON pin: Enable Weak Pullup if needed (check schematic)

    4. BUTTON pin: Interrupt on Change: Sense negative/positive on BUTTON press (check schematic)

After configuring the components as described above, click 'Generate' to generate the code. Then add the following code snippets to your application:

#include "mcc_generated_files/system/system.h"
volatile bool BUTTON_PRESSED = false;
void BUTTON_Press_Callback(void)
{
    LED_Toggle();
    BUTTON_PRESSED = true;
    INTERRUPT_GlobalInterruptDisable();
}
int main(void)
{
    SYSTEM_Initialize();
    RC0_SetInterruptHandler(BUTTON_Press_Callback);   //Find button pin for your board.
    
    INTERRUPT_GlobalInterruptEnable();

    while(1){
        if(BUTTON_PRESSED)
        {
            BUTTON_PRESSED = false;
            __delay_ms(50);  //Debounce delay for button
            INTERRUPT_GlobalInterruptEnable();
        }
    }
}