Beginner Lab 6: Switch Input via GPIO
Objective
- Set up a General-Purpose Input/Output (GPIO) pin to detect the state (pressed or released) of a mechanical push button (SW1).
- Configure another GPIO pin to drive an LED, turning it ON or OFF based on the input received from the switch.
- Write and test embedded C code to continuously monitor the switch and update the LED state in real time.
- Understand the importance of pull-up or pull-down resistors in reliable input detection.
- Enable Debouncing to avoid multiple transitions when the mechanical button is pressed or released.
- Observe the practical application of digital logic in embedded systems by integrating hardware and software components.
Overview
This lab exercise introduces the fundamental principles of GPIO in embedded systems. The experiment involves configuring a mechanical switch (SW1) and an LED (LED1) on the PIC32CM LS00 Curiosity Nano+ Touch Evaluation Kit. The state of the switch (ON/OFF) is detected and indicated by controlling the LED, thereby providing visual feedback of the input signal. The exercise also includes the implementation of debouncing techniques to ensure reliable and accurate detection of signals from mechanical buttons.
Lab Activities
- Identify the GPIO pins connected to the switch (SW1) and the LED using the specified board user guide.
- Configure the External Interrupt Controller (EIC) peripheral and assign the appropriate EIC pin for SW1 in MPLAB® X IDE.
- Set up the GPIO pin for the LED using the pin configuration tool.
- Add application code to the generated project, implementing the necessary interrupt handlers and callback functions for EIC interrupt generation and LED toggling.
- Observe the output by verifying the toggling of the LED on the PIC32CM LS00 Curiosity Nano+ Touch Evaluation Kit.
Expected Outcomes
Upon completing this lab, you will be able to:
- Successfully configure the EIC and assign the correct EIC pin for SW1 in MPLAB X IDE.
- Set up and configure GPIO pins for output devices such as LEDs.
- Develop and integrate application code to implement interrupt handlers and callback functions for EIC interrupt generation and LED toggling.
- Verify the correct operation of the system by observing the toggling of the LED on the PIC32CM LS00 Curiosity Nano+ Touch Evaluation Kit.
Lab Source Files and Solutions
This ZIP file contains the completed solution project for this lab. It also contains the source files needed to perform the lab as per the following step-by-step instructions (see the "Lab Index" section on this page).
for this lab.
Lab Index
- Step 1: GPIO Input Handling and Debouncing
- Step 1.1 - GPIO Input Handling
- Step 1.2 - Enable Debouncing to Prevent Multiple Transitions
- Step 1.3 - Debouncing on EIC
- Step 2: Configure Pins for Switch and LED
- Step 2.1 - Configure External Interrupt Controller (EIC) PLIB for Switch Button
- Step 2.2 - Pin Configuration for Switch and LED
- Step 2.3 - Arm® TrustZone® for Armv8-M
- Step 3: Generate Code
- Step 3.1 - Generate Code
- Step 4: Add Application Code to the Project
- Step 4.1 - Application Code for the project
- Step 5: Build, Program and Observe the Output
- Step 5.1 - Build, hardware setup, and program
- Step 5.2 - Observe the Output
Reference Links
- PIC32CM LS00 Curiosity Nano+ Touch Evaluation Kit
- MPLAB X Integrated Development Environment (IDE)
- MPLAB XC32 Compiler
- PIC32CM LS00 Curiosity Nano+ Touch Evaluation Kit: EV41C56A User Guide
- Ultra-Low Power, Secure, and Enhanced Touch MCU: PIC32CM LE00/LS00/LS60 Family Data Sheet