Lesson 2: Part 1: Controlling GPIO - On-Board Input - SFR Study

Introduction

Push button provide a method for human to give input to the system. It can be requesting a system to start a process, or requesting a system to provide system state and etc. For example, pressing a button in remote control to perform volume up action, pressing start button in washing machine to initiate wash status.

In this lesson, we are be using out board button as system input.

Working Principles

When MCU pin is configure as input pin, the pin state (High or Low) can be read from software by accessing SFR. Below schematic is the circuit design for USER button in STM32F030 Discovery board.
USER Button (B1) <-> PA0
Refer to above circuit, PA0 is the input pin to MCU. When USER button is not press, PA0 signal will be 0V (read by software as Low state). When USER button is press, VDD will be supply at point 3,4, thus enabling PA0 to have VDD voltage, which is 3.3V(read by software as High state)

We need the following configuration to enable an input pin
  1. Enable GPIO section for respective pin
  2. Enable pin to be input function
  3. Read the current input state

Step 1: Enable PA0 GPIO Function

  1. Similar to lesson 1 part 1, we use the same SFR(RCC_AHBENR) to enable GPIO function, but PA0 require us to enable bit (IOPAEN)
Enable Bit IOPAEN In Register RCC_AHBENR

Step 2: Configure Pin To Be Input Pin

  1. Similar to lesson 1 part 1, we use the same SFR(GPIOx_MODER). Since we are looking at Port A, the right register is GPIOA_MODER
  2. PA0 refer to bit 0, thus we should be configure MODER0 to '00'
  3. If we refer to below, the reset state of the pin is 00, that means after power up the pin has been configure to input state. Thus, we can actually skip this setting in our coding.
Set MODER0 To '00' In Register GPIOA_MODER

Step 3: Reading Input State

  1. By reading register GPIOA_IDR,  bit IDR0, we will know the current state of the pin.
Reading IDR0 In Register GPIOA_IDR

No comments:

Post a Comment