4.8 Pin Manager
4.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.
- Related Technical Brief: TB3284: Getting Started with GPIO on PIC18 (pdf)
4.8.2 Supported Device Families
All supported PIC10, PIC12, PIC16, and PIC18 device families.
4.8.3 Required Header Files
#include "mcc_generated_files/system/pins.h"
4.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: PIC Pin Manager Use Case Code Snippet Instructions
- PIC Pin Manager Use Case: LED Toggle on 500ms Delay: A LED is toggled every 500 ms, at period set by a delay.
- 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.
- 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.
- PIC Pin Manager Use Case: LED toggle on Button Press (Pin Interrupt on Change): An Interrupt on Change (IOC) is configured on an active low button, turning on an LED.
4.8.5 Module Documentation
4.8.5.1 Pin Manager Use Cases
4.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:
-
Custom Name
-
Analog: Yes/No (Uncheck for digital only functionality).
-
Start High: Yes/No (After SYSTEM_Initialize(), pin will be high).
-
Weak Pullup: Yes/No (E.g. if Button connected to pin needs a pullup).
-
Open Drain: Yes/No.
-
Slew Rate: Yes/No (Limit the rate of change of the pin).
-
Input Level Control: Yes/No.
-
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
4.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.
-
Pin Grid View:
-
Select a LED pin (check schematic), configure as Output.
-
-
Pins:
-
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); } }
4.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.
-
Pin Grid View:
-
Select a LED pin (check schematic), configure as Output.
-
Select a BUTTON pin (check schematic), configure as Input.
-
-
Pins:
-
LED pin: Custom name "LED".
-
BUTTON pin: Custom name "BUTTON"
-
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(); } } }
4.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.
-
Pin Grid View:
-
Select a LED pin (check schematic), configure as Output.
-
Select a BUTTON pin (check schematic), configure as Input.
-
-
Pins:
-
LED pin: Custom name "LED".
-
BUTTON pin: Custom name "BUTTON"
-
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();
}
}
4.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.
-
Pin Grid View:
-
Select a LED pin (check schematic), configure as Output.
-
Select a BUTTON pin (check schematic), configure as Input.
-
-
Pins:
-
LED pin: Custom name "LED".
-
BUTTON pin: Custom name "BUTTON"
-
BUTTON pin: Enable Weak Pullup if needed (check schematic)
-
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(); } } }