PV198 - GPIO One-chip Controllers Daniel Dlhopolček, Marek Vrbka, Jan Koniarik, Oldřich Pecák, Tomáš Rohlínek, Ján Labuda, Jan Horáček, Matúš Škvarla Faculty of Informatics, Masaryk University 2/2023 Content Content GPIO Overview Buttons Debouncing Application LED using SDK example Button using config tools Homework ·PV198 - GPIO ·2/2023 2 / 25 GPIO Overview Have you checked the preliminaries in study materials? Do not forget to setup a new branch for this week (Week_02)! ·PV198 - GPIO ·2/2023 3 / 25 GPIO Overview What is GPIO GPIO – General Purpose Input Output Direct control of pins of the MCU Basic interaction with external world Can be programmed as input or output Has only 2 states (logic 0, logic 1) ·PV198 - GPIO ·2/2023 4 / 25 GPIO Overview What is it used for Anything that works with 2 states – on/off LED Buttons Sensors And used by more sophisticated peripherals ·PV198 - GPIO ·2/2023 5 / 25 Buttons How buttons on board work Connects pit to ground (logic 0) or to voltage (logic 1) ·PV198 - GPIO ·2/2023 6 / 25 Buttons Debouncing Button debouncing Bouncing Looks like button is pressed multiple times Cause by mechanical contact of the switch Solution 1. HW debounce (add capacitor) 2. SW debounce (wait few milliseconds) ·PV198 - GPIO ·2/2023 7 / 25 Application LED using SDK example Steps required to create an application Initialize (MCUXpresso Configuration Tools help here) 1. Pin 2. Clocks 3. Peripherals Write application code ·PV198 - GPIO ·2/2023 8 / 25 Application LED using SDK example Peripheral configuration options 1. Write everything from scratch Error prone, time demanding, tedious 2. Use SDK example Works out of box Difficult to modify 3. Use config tools Easy to use and modify ·PV198 - GPIO ·2/2023 9 / 25 Application LED using SDK example LED using the SDK example Select Import SDK example(s)... ·PV198 - GPIO ·2/2023 10 / 25 Application LED using SDK example 1. Open the K6x 2. Select the MK66FN2M0xxx18 3. Click the board image ·PV198 - GPIO ·2/2023 11 / 25 Application LED using SDK example 1. Open driver_examples → gpio 2. Select the gpio_led_output example 3. Click Finish ·PV198 - GPIO ·2/2023 12 / 25 Application LED using SDK example Opened example project Pins and clocks are already configured GPIO_PinInit GPIO_PortToggle ·PV198 - GPIO ·2/2023 13 / 25 Application Button using config tools Button control program We will show you how to check for button presses The end goal is to write program which will print text to console when SW2 is pressed ·PV198 - GPIO ·2/2023 14 / 25 Application Button using config tools ·PV198 - GPIO ·2/2023 15 / 25 Application Button using config tools You should see the Pin tool now ·PV198 - GPIO ·2/2023 16 / 25 Application Button using config tools Initialization How configuration tools can help us: Modify settings easily Visual representation of configuration Great for custom boards (generates defines for custom boards that simplify management) ·PV198 - GPIO ·2/2023 17 / 25 Application Button using config tools Configuration Pins tool contains predefined configurations We should already see the red LED configured Add the configuration for SW2 and SW3 buttons Search for SW2 and SW3 on the “pins” window Click on the checkboxes for SW2 and SW3 and add the GPIO option This will call initialization code for the button pins on program startup ·PV198 - GPIO ·2/2023 18 / 25 Application Button using config tools Code preview was updated If you check the Code Preview tab, you should see that the pin_mux.c file now has extra SW2 and SW3 configuration You should see in the Routed Pins tab (lower-left corner) that button pins are routed to PTD11 (SW2) and PTA10 (SW3) ·PV198 - GPIO ·2/2023 19 / 25 Application Button using config tools Updating code Click the Update Code button It opens the Update Files dialog You can check which changes will be made For now, just click OK ·PV198 - GPIO ·2/2023 20 / 25 Application Button using config tools Writing actual code Task - Reading a button and printing to console Read the current state of the GPIO Button (SW2 and/or SW3) If button is pressed, print text to console Otherwise, do nothing ·PV198 - GPIO ·2/2023 21 / 25 Application Button using config tools Issues When you press the button, text is printed several times Why? What are the ways to resolve it? ·PV198 - GPIO ·2/2023 22 / 25 Application Button using config tools Work Progress Write an application that toggles green LED when SW3 is pressed Fix the issue with the button press being registered more than once Make the LED change color every time it is turned on There are three controllable LEDs on the board ·PV198 - GPIO ·2/2023 23 / 25 Homework Homework Write an application which reacts to both buttons SW2 selects color SW3 toggles the color on and off All colors start turned off Selected color starts on red Colors switch in the following order: Red → Green → Blue → Red... Application must be immune to the effects of bouncing SW3 SW2 SW3 SW3 Red on Green selected Green on Green off ·PV198 - GPIO ·2/2023 24 / 25 Homework Submission Git tag - “Submission_02_x” One project per branch! ·PV198 - GPIO ·2/2023 25 / 25