4.6.2 Pin Manager with Individual Pull-up
I/O Port Management and Configuration with individual pull-up and Inverted I/O
4.6.2.1 Introduction
The Pin Manager controls the I/O pins that represent the instances of the PORT peripheral registers. Each PORT instance has up to eight I/O pins. The PORTs are named PORTA, PORTB, PORTC, etc.
4.6.2.2 Supported Device Families
AVR® Dx | AVR® Ex | ATtiny | ATmegaXXXX |
4.6.2.3 Required header files:
#include"mcc_generated_files/system/pins.h"
4.6.2.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: AVR Pin Manager Use Case Code Snippet Instructions
- AVR Pin Manager Use Case: LED Toggle on 500ms Delay: A LED is toggled every 500 ms, at a period set by a delay.
- AVR 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 the transition associated with a button press.
- AVR 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 the BUTTON pin to detect the transition associated with a button press.
- AVR 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.6.2.5 Module Documentation
4.6.2.5.1 Pin Manager Use Cases
AVR Pin Manager Use Case Code Snippet Instructions
These are the general set of instructions to follow for the Use Case Examples of the General Purpose Input/Output (GPIO) System Driver 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
AVR Pin Manager Use Case: LED Toggle on 500ms Delay
A LED is toggled every 500 ms, at a period set by a delay.
-
Pin Grid View:
-
Select a LED pin, configure as Output.
-
-
Pins:
-
LED pin: Custom name "IO_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" #include <util/delay.h>
int main(void) { SYSTEM_Initialize(); // Initialize the system while (1) { IO_LED_Toggle(); // Toggle the LED state _delay_ms(500); // Delay for 500 milliseconds } }
AVR 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 the transition associated with a button press.
-
Pin Grid View:
-
Select LED pin, configure as Output.
-
Select BUTTON pin, configure as Input.
-
-
Pins:
-
LED pin: Custom name "IO_LED".
-
BUTTON pin: Custom name "IO_BUTTON".
-
BUTTON pin: Enable Weak Pullup if needed (Check board 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) { // Initialize the system SYSTEM_Initialize(); while (1) { // Check if the button is pressed if (!IO_BUTTON_GetValue()) { // Assuming button is active low. Remove "!" if button is active high. // Turn on the LED IO_LED_SetLow(); // Assuming LED is active low. Swap IO_LED_SetLow()/IO_LED_SetHigh() if LED is active high. } else { // Turn off the LED IO_LED_SetHigh(); // Assuming LED is active low. Swap IO_LED_SetLow()/IO_LED_SetHigh() if LED is active high. } } }
AVR 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 the BUTTON pin to detect the transition associated with a button press.
-
Pin Grid View:
-
Select LED pin, configure as Output.
-
Select BUTTON pin, configure as Input.
-
-
Pins:
-
LED pin: Custom name "IO_LED".
-
BUTTON pin: Custom name "IO_BUTTON".
-
BUTTON pin: Enable Weak Pullup if needed (Check board 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" #include <util/delay.h> #define EDGE_DETECTED (true) #define EDGE_NOT_DETECTED (false)
// Function to detect falling edge on button press inline static bool NegEdgeDetectOnButton(void) { static bool lastState = false; bool const currentState = IO_BUTTON_GetValue(); if (currentState != lastState && currentState == false) { lastState = currentState; return EDGE_DETECTED; } else { lastState = currentState; return EDGE_NOT_DETECTED; } } // Function to wait for button press static void WaitButton(void) { while (true) { while (NegEdgeDetectOnButton() == EDGE_NOT_DETECTED) { } _delay_ms(50); // Delay for debounce if (!IO_BUTTON_GetValue()) { return; } } }
void main(void) {
// Initialize the system
SYSTEM_Initialize();
while (true) {
WaitButton(); // Wait for button press
IO_LED_Toggle(); // Toggle the LED state
}
}
AVR 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 debouncing.
-
Pin Grid View:
-
Select LED pin, configure as Output.
-
Select BUTTON pin, configure as Input.
-
-
Pins:
-
LED pin: Custom name "IO_LED".
-
BUTTON pin: Custom name "IO_BUTTON".
-
BUTTON pin: Enable Weak Pullup if needed (Check board schematic).
-
BUTTON pin: Interrupt on Change: Sense negative/positive on BUTTON press (Check board 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" #include <util/delay.h> volatile bool BUTTON_PRESSED = false;
void BUTTON_Press_Callback(void) {
// Toggle the LED
IO_LED_Toggle();
// Set the flag indicating button press
BUTTON_PRESSED = true;
Disable_global_interrupt();
}
int main(void) { // Initialize the system SYSTEM_Initialize(); // Set the interrupt handler for button press IO_BUTTON_SetInterruptHandler(BUTTON_Press_Callback); //Find button pin for your board. Enable_global_interrupt(); while (1) { // Check if button is pressed if (BUTTON_PRESSED) { // Clear the button pressed flag BUTTON_PRESSED = false; // Delay for debouncing _delay_ms(50); //Debounce delay for button Enable_global_interrupt(); } } }