PV198 - GPIO One-chip Controllers Daniel Dlhopolček, Marek Vrbka Faculty of Informatics, Masaryk University Content Content GPIO Overview Buttons Debouncing Application LED using SDK example Button using config tools Homework D. Dlhopoček, M. Vrbka ·PV198 - GPIO · 2 / 25 GPIO Overview Have you checked the preliminaries in study materials? Do not forget to setup a new branch for this week! D. Dlhopoček, M. Vrbka ·PV198 - GPIO · 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) D. Dlhopoček, M. Vrbka ·PV198 - GPIO · 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 D. Dlhopoček, M. Vrbka ·PV198 - GPIO · 5 / 25 Buttons How buttons on board work Connects pit to ground (logic 0) or to voltage (logic 1) D. Dlhopoček, M. Vrbka ·PV198 - GPIO · 6 / 25 Buttons Debouncing Button debouncing Bouncing Looks like button is pressed multiple times Cause by mechanical contact of the switch Solution HW debounce(add capacitor) SW debounce(wait few miliseconds) D. Dlhopoček, M. Vrbka ·PV198 - GPIO · 7 / 25 Application LED using SDK example Steps required to create an application Initialize( MCUXpresso Configuration Tools help here) Pin Clocks Peripherals Write application code D. Dlhopoček, M. Vrbka ·PV198 - GPIO · 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 D. Dlhopoček, M. Vrbka ·PV198 - GPIO · 9 / 25 Application LED using SDK example LED using the SDK example Select Import SDK example(s)... D. Dlhopoček, M. Vrbka ·PV198 - GPIO · 10 / 25 Application LED using SDK example Open the K6x Select the MK66FN2M0xxx18 Click the board image D. Dlhopoček, M. Vrbka ·PV198 - GPIO · 11 / 25 Application LED using SDK example Open driver_examples → gpio Select the “gpio_led_output” example Click Finish D. Dlhopoček, M. Vrbka ·PV198 - GPIO · 12 / 25 Application LED using SDK example Opened example project Pins, clocks are already configured GPIO_PinInit GPIO_PortToggle D. Dlhopoček, M. Vrbka ·PV198 - GPIO · 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 D. Dlhopoček, M. Vrbka ·PV198 - GPIO · 14 / 25 Application Button using config tools D. Dlhopoček, M. Vrbka ·PV198 - GPIO · 15 / 25 Application Button using config tools You should see the Pin tool now D. Dlhopoček, M. Vrbka ·PV198 - GPIO · 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) D. Dlhopoček, M. Vrbka ·PV198 - GPIO · 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 D. Dlhopoček, M. Vrbka ·PV198 - GPIO · 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) D. Dlhopoček, M. Vrbka ·PV198 - GPIO · 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 D. Dlhopoček, M. Vrbka ·PV198 - GPIO · 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 D. Dlhopoček, M. Vrbka ·PV198 - GPIO · 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? D. Dlhopoček, M. Vrbka ·PV198 - GPIO · 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 D. Dlhopoček, M. Vrbka ·PV198 - GPIO · 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 D. Dlhopoček, M. Vrbka ·PV198 - GPIO · 24 / 25 Homework Submission Git Branch - “Week_02” (Case sensitive!) Git tag - “Submission_02_x” One project per branch! D. Dlhopoček, M. Vrbka ·PV198 - GPIO · 25 / 25