{"id":865,"date":"2022-04-01T15:13:04","date_gmt":"2022-04-01T15:13:04","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=865"},"modified":"2022-04-01T15:15:46","modified_gmt":"2022-04-01T15:15:46","slug":"getting-started-with-stm32l053-gpio-input","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=865","title":{"rendered":"Getting started with STM32L053: GPIO Input"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.st.com\/content\/dam\/arm-cortex-m\/M0-Corenew.jpg\" alt=\"\" \/><\/figure><\/div>\n\n\n\n<p>In the previous guide (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=837\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=837\" target=\"_blank\">here<\/a>), we took a look at GPIO output using STM32L053, now we shall see how to develop GPIO input driver.<\/p>\n\n\n\n<p>In this guide, we shall cover the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Input modes.<\/li><li>Develop the GPIO input driver.<\/li><li>Code to toggle the LED using push-button.<\/li><li>Code.<\/li><li>Demo.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">1. Input modes:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>GPIO input modes include<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>high impedance<\/li><li>pull-up<\/li><li>pull-down<\/li><\/ul>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"floating-high-impedance-tri-stated\">Floating, High Impedance, Tri-Stated<\/h4>\n\n\n\n<p>Floating, high impedance, and tri-stated are three terms that mean the same thing: the pin is just flopping in the breeze. Its state is indeterminate unless it is driven high or low externally. You only want to configure a pin as floating if you know it will be driven externally. Otherwise, configure the input using pulling resistors.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"pull-updown\">Pull Up\/Down<\/h4>\n\n\n\n<p>If an input is configured with an internal pull-up, it will be high unless it is externally driven low. Pull-down inputs do the opposite (\u00a0they\u2019re low unless driven high).<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-01-at-5.52.38-PM-1024x768.png\" alt=\"\" class=\"wp-image-866\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-01-at-5.52.38-PM-1024x768.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-01-at-5.52.38-PM-300x225.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-01-at-5.52.38-PM-768x576.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-01-at-5.52.38-PM-1536x1152.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-01-at-5.52.38-PM-1150x862.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-01-at-5.52.38-PM-750x562.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-01-at-5.52.38-PM-400x300.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-01-at-5.52.38-PM-250x187.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-01-at-5.52.38-PM.png 1566w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2.Develop GPIO input driver:<\/h2>\n\n\n\n<p>Before we start developing the GPIO input driver, we need to figure out which pin is the push-button of Nucleo-64 is connected to.<\/p>\n\n\n\n<p>According to the user manual of the STM32 Nucleo-64, the push-button is connected to pin PC13.<\/p>\n\n\n\n<p>Hence, we start off by enabling clock access to GPIOC as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\t\/*Enable clock access to GPIOC*\/\n\tRCC-&gt;IOPENR |= RCC_IOPENR_GPIOCEN;<\/pre><\/div>\n\n\n\n<p>Then configure the pin as input by reseting bit 26-27 of MODER register as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">GPIOC-&gt;MODER&amp;=~ GPIO_MODER_MODE13;<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>In order to read the GPIO state, we can use GPIOC-&gt;IDR as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">GPIOC-&gt;IDR&amp;GPIO_IDR_ID13 <\/pre><\/div>\n\n\n\n<p>This will return either 1 if the button is not pressed and 0 if the button is pressed.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Code to toggle the LED using push-button:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>To make the guide interesting, we shall toggle the LED with each press of the push button.<\/p>\n\n\n\n<p>We start by enabling clock access to GPIOA and set PA5 as output:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\t\/*Enable clock access to GPIOA*\/\n\tRCC-&gt;IOPENR |= RCC_IOPENR_GPIOAEN;\n\t\/*Set PA5 as output pin*\/\n\tGPIOA-&gt;MODER |= GPIO_MODER_MODE5_0;\n\tGPIOA-&gt;MODER  &amp;=~GPIO_MODER_MODE5_1;<\/pre><\/div>\n\n\n\n<p>Then we need a variable to hold the state:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">uint8_t state;<\/pre><\/div>\n\n\n\n<p>in while (1) loop:<\/p>\n\n\n\n<p>First we check if the button is pressed and the state is 0 as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">if ((GPIOC-&gt;IDR&amp;GPIO_IDR_ID13)==0&amp;&amp;(state==0))\n\t\t\t<\/pre><\/div>\n\n\n\n<p>If the button is pressed and state is 0, perform the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Set state to 1.<\/li><li>Turn on LED.<\/li><li>Wait until the button is released.<\/li><\/ul>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\t\t\t{\n\t\t\tstate=1;\n\t\t\tGPIOA-&gt;BSRR=GPIO_BSRR_BS_5;\n\t\t\twhile((GPIOC-&gt;IDR &amp; GPIO_IDR_ID13)==0);\n\t\t\t}<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Then we check if the button is pressed and the state is 1, if the conditions are met, perform the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Set state to 0.<\/li><li>Turn off the LED.<\/li><li>wait until the button is released.<\/li><\/ul>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\tif ((GPIOC-&gt;IDR &amp; GPIO_IDR_ID13)==0&amp;&amp;(state==1))\n\t\t\t{\n\t\t\tstate=0;\n\t\t\tGPIOA-&gt;BSRR=GPIO_BSRR_BR_5;\n\t\t\twhile((GPIOC-&gt;IDR &amp; GPIO_IDR_ID13)==0);\n\t\t\t}<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Code:<\/h2>\n\n\n\n<p>The entire code as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#include  &quot;stm32l0xx.h&quot;\n\nuint8_t state;\n\n\nint main(void)\n{\n\t\/*Enable clock access to GPIOA*\/\n\tRCC-&gt;IOPENR |= RCC_IOPENR_GPIOAEN;\n\t\/*Enable clock access to GPIOC*\/\n\tRCC-&gt;IOPENR |= RCC_IOPENR_GPIOCEN;\n\n\t\/*Set PA5 as output pin*\/\n\tGPIOA-&gt;MODER |= GPIO_MODER_MODE5_0;\n\tGPIOA-&gt;MODER  &amp;=~GPIO_MODER_MODE5_1;\n\t\/* Set PC13 as input*\/\n\tGPIOC-&gt;MODER&amp;=~ GPIO_MODER_MODE13;\n\n\twhile(1)\n\t{\n\t\tif ((GPIOC-&gt;IDR&amp;GPIO_IDR_ID13)==0&amp;&amp;(state==0))\n\t\t\t{\n\t\t\tstate=1;\n\t\t\tGPIOA-&gt;BSRR=GPIO_BSRR_BS_5;\n\t\t\twhile((GPIOC-&gt;IDR &amp; GPIO_IDR_ID13)==0);\n\t\t\t}\n\n\t\tif ((GPIOC-&gt;IDR &amp; GPIO_IDR_ID13)==0&amp;&amp;(state==1))\n\t\t\t{\n\t\t\tstate=0;\n\t\t\tGPIOA-&gt;BSRR=GPIO_BSRR_BR_5;\n\t\t\twhile((GPIOC-&gt;IDR &amp; GPIO_IDR_ID13)==0);\n\t\t\t}\n\n\t}\n\n}\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5. Demo:<\/h2>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"LED toggle using push-button on STM32L053\" width=\"1170\" height=\"658\" src=\"https:\/\/www.youtube.com\/embed\/ffLREAUvsQQ?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>Happy coding \ud83d\ude42 <\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous guide (here), we took a look at GPIO output using STM32L053, now we shall see how to develop GPIO input driver. In this guide, we shall cover the following: Input modes. Develop the GPIO input driver. Code to toggle the LED using push-button. Code. Demo. 1. Input modes: GPIO input modes include [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,11,12],"tags":[],"class_list":["post-865","post","type-post","status-publish","format-standard","hentry","category-embedded-systems","category-peripheral-drivers","category-stm32"],"_links":{"self":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/865"}],"collection":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=865"}],"version-history":[{"count":2,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/865\/revisions"}],"predecessor-version":[{"id":868,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/865\/revisions\/868"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=865"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=865"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=865"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}