PV198 – One-chip Controllers, Introduction / Daniel Dlhopolček 1 PV198 – One-chip Controllers GPIO – LED & Button PV198 – One-chip Controllers, Introduction / Daniel Dlhopolček 2 Content 1.What is GPIO 2.What is it used for 3.How does it work 1.Switch debouncing 4.Application 1.Output – LED using SDK example 2.Input – Button using Config Tools § § PV198 – One-chip Controllers, Introduction / Daniel Dlhopolček 3 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 – One-chip Controllers, Introduction / Daniel Dlhopolček 4 Content 1.What is GPIO 2.What is it used for 3.How does it work 1.Switch debouncing 4.Application 1.Output – LED using SDK example 2.Input – Button using Config Tools § § What is it used for §Anything that works with 2 states – on/off § §LED §Buttons §Sensors §And used by more sophisticated peripherals § 5 PV198 – One-chip Controllers, Introduction / Daniel Dlhopolček PV198 – One-chip Controllers, Introduction / Daniel Dlhopolček 6 Content 1.What is GPIO 2.What is it used for 3.How does it work 1.Switch debouncing 4.Application 1.Output – LED using SDK example 2.Input – Button using Config Tools § § How buttons on the board work §Connects pit to ground (logic 0) or to voltage (logic 1) 1. 7 PV198 – One-chip Controllers, Introduction / Daniel Dlhopolček Diagram Description automatically generated How does it work – Switch debouncing §Bouncing §Looks like button is pressed multiple times §Cause by mechanical contact of the switch 1. 8 PV198 – One-chip Controllers, Introduction / Daniel Dlhopolček How doeas it work – Switch 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) 1. 9 PV198 – One-chip Controllers, Introduction / Daniel Dlhopolček PV198 – One-chip Controllers, Introduction / Daniel Dlhopolček 10 Content 1.What is GPIO 2.What is it used for 3.How does it work 1.Switch debouncing 4.Application 1.Output – LED using SDK example 2.Input – Button using Config Tools § § Application §Steps required to create an application: §Initialize( MCUXpresso Configuration Tools help here) §Pin §Clocks §Peripherals §Write application code 11 PV198 – One-chip Controllers, Introduction / Daniel Dlhopolček Application 1.Write everything from scratch § ❌Error-prone , time demanding, tedious 2.USE SDK example § ✔️Works out-of-box § ❌More difficult to modify 3.USE Config Tools § ✔️Easy to use and modify 12 PV198 – One-chip Controllers, Introduction / Daniel Dlhopolček PV198 – One-chip Controllers, Introduction / Daniel Dlhopolček 13 Content 1.What is GPIO 2.What is it used for 3.How does it work 1.Switch debouncing 4.Application 1.Output – LED using SDK example 2.Input – Button using Config Tools § § Application – LED using SDK example 14 PV198 – One-chip Controllers, Introduction / Daniel Dlhopolček §Import SDK example “gpio_led_output” § §Select import SDK examples(s)… Graphical user interface, text, application Description automatically generated Application – LED using SDK example 15 PV198 – One-chip Controllers, Introduction / Daniel Dlhopolček §In the SDK wizard unfold the K6x, select the MK66FN2… and click on the board image Graphical user interface, text, application Description automatically generated Application – LED using SDK example 16 PV198 – One-chip Controllers, Introduction / Daniel Dlhopolček §Unfold driver_examples -> gpio §Select “gpio_led_output” example §Click Finish Graphical user interface, application Description automatically generated Application – LED using SDK example 17 PV198 – One-chip Controllers, Introduction / Daniel Dlhopolček §Go through code together in detail §Pins, clocks are already configured §GPIO_PinInit §GPIO_PortToggle PV198 – One-chip Controllers, Introduction / Daniel Dlhopolček 18 Content 1.What is GPIO 2.What is it used for 3.How does it work 1.Switch debouncing 4.Application 1.Output – LED using SDK example 2.Input – Button using Config Tools § § Application – Button using Config Tools §Goal: §Press SW2 button to print text into console 19 PV198 – One-chip Controllers, Introduction / Daniel Dlhopolček Application – Button using Config Tools §Create new project §Open Config Tools 20 PV198 – One-chip Controllers, Introduction / Daniel Dlhopolček Graphical user interface Description automatically generated Application – Button using Config Tools §You should see Pin tool now: 21 PV198 – One-chip Controllers, Introduction / Daniel Dlhopolček Graphical user interface, application, table Description automatically generated Application – Initialization §How configuration tools can help us: §Modify settings easily §Visual representation of a configuration §Great for custom boards (our board already has a lot of useful DEFINEs, which is not a case when new board is created) 22 PV198 – One-chip Controllers, Introduction / Daniel Dlhopolček Application – Button using Config Tools §Pins tool contains predefined configurations §Open dialog with functional groups by clicking on the icon 23 PV198 – One-chip Controllers, Introduction / Daniel Dlhopolček Graphical user interface, application, table Description automatically generated 24 PV198 – One-chip Controllers, Introduction / Daniel Dlhopolček Application – Button using Config Tools §Dialog with predefined functional group appears §Set checkbox at the bottom of the dialog for functional group: “BOARD_InitButtonsPins” to call initialization code for the group in default initialization function 25 PV198 – One-chip Controllers, Introduction / Daniel Dlhopolček 26 PV198 – One-chip Controllers, Introduction / Daniel Dlhopolček Graphical user interface, text, application, email Description automatically generated Prefix used in generated code Application – Button using Config Tools §Code preview is updated §Initialization function now calls 1 more function 27 PV198 – One-chip Controllers, Introduction / Daniel Dlhopolček 28 PV198 – One-chip Controllers, Introduction / Daniel Dlhopolček Graphical user interface, text, application, email Description automatically generated Graphical user interface, text, application, email Description automatically generated Application – Button using Config Tools §Select “BOARD_InitBUTTONsPins” functional group from a combo-box 29 PV198 – One-chip Controllers, Introduction / Daniel Dlhopolček Graphical user interface, application Description automatically generated 30 PV198 – One-chip Controllers, Introduction / Daniel Dlhopolček Application, table Description automatically generated Graphical user interface, application Description automatically generated GPIO signal is routed to “PTD11” 31 PV198 – One-chip Controllers, Introduction / Daniel Dlhopolček Application, table Description automatically generated Text Description automatically generated User-friendly Label – specific for our board Identifier used in generated code Identifier used in generated code Prefix used in generated code User friendly Label – specific for our board Application – Button using Config Tools §Your project needs to be updated by newly generated code now – this action is done automatically §Whenever generated code does not match the code is your project, it is displayed by changed color of the icon in the toolbar 32 PV198 – One-chip Controllers, Introduction / Daniel Dlhopolček Diagram Description automatically generated Application – Button using Config Tools §Update Project Files dialog should appear §You can view changes to each file by clicking on the “change” text in the “Status” column §Press “OK” to update your project 33 PV198 – One-chip Controllers, Introduction / Daniel Dlhopolček Graphical user interface, text, application Description automatically generated Application – Button using Config Tools §Clocks tool – preconfigured, you can view clock settings §Peripherals tool – no need for now 34 PV198 – One-chip Controllers, Introduction / Daniel Dlhopolček Application – Button using Config Tools §Write application code §Read current state of the GPIO(button) §Detect button press •Print text into console 35 PV198 – One-chip Controllers, Introduction / Daniel Dlhopolček Application – Button using Config Tools §When you press the button, text is printed more than once § §Why? §How to resolve this issue? 36 PV198 – One-chip Controllers, Introduction / Daniel Dlhopolček Work Progress §Write an applications that toggles GREEN LED when SW3 button is pressed §Fix the issue with button press being registered more than once § §Make LED change color every time it is turned on (there are 3 LEDs on the board: Red, Green, Blue) 37 PV198 – One-chip Controllers, Introduction / Daniel Dlhopolček Homework §Write an applications that reacts to both buttons §SW3 turns on/off selected color §SW2 changes selected color (R -> G -> B -> R …) §At start all colors are turned off, and RED is selected § 38 PV198 – One-chip Controllers, Introduction / Daniel Dlhopolček All colors are off Red turned on Red stays on Green Turned on Green Turned off SW3 SW3 SW3 SW2 Homework - git §Git Branch- “Week_02” §Git tag – “Submission_02_x” 39 PV198 – One-chip Controllers, Introduction / Daniel Dlhopolček