STM32 GPIO INPUT Configuration
In the previous tutorial, we covered how to use the GPIO pin as output using Registers. This tutorial will cover how to configure the pin as input, and then how to read it’s state.
I will use a button to read the input state of the pin.
In order to configure the pin in the input mode, we need to follow the steps mentioned below
1. Enable GPIOA clock
The GPIO clock can be enabled in the RCC_AHB1ENR Register
As you can see above, the 0th bit of RCC_AHB1ENR Register enables the clock for the GPIOA. That’s why we need to write a 1 in the 0th position
2. Set the PIN PA1 as Input
To configure the pin as input, we will modify the GPIOx_MODER Register. This register is responsible for configuring different modes for the GPIO, and in this case we will configure it as the input mode
Since I am using pin PA1, I need to modify the pins 2 and 3. This basically works like, if the PIN is ‘y‘, the we need to configure the bits ‘2y’ and ‘2y+1’
Also, in order to set the pin as input, we need to set the bits (3:2) as 0:0. This means that we need to write a ‘0’ in the 2nd and 3rd positions
3. Configure the Pull UP
I am going to configure the pin as the Pull-Up pin. The reason for the same is that the button is connected as shown below
When the button will be pressed, the current from the PA1 will sink into the ground, and the PA1 will go LOW
To configure the Pull UP / Pull DOWN, we need to modify the GPIOx_PUPDR Register
As we need to configure the Pull-UP for the the pin PA1, we need to modify respective bits (3:2). I want to set the Pull-up here, so i need to configure the bits (3:2) as 0:1
That’s all for the configuration of the GPIO as input pin.
How to Read the Pin State
To read the state of the pin, we will check the Input Data Register (IDR)
IDR is a read only register. The respective bit either set or reset depending on the state of the PIN.
Now since I am using the Pull-UP for pin PA1 here, the 1st bit of IDR will always be HIGH. When the button is pressed, the current from PA1 will sink into the ground, and the pin will be LOW, and so does the bit 1.
So basically we wait for the 1st bit of the IDR to go LOW. And when it is low, indicating the button has been pressed, we can do the processing.
The main CODE
Here we check for the bit 1 of the IDR, if it is low, we increment the count variable.
So whenever the button is pressed, the count variable will increment
As you can see above, the count variable increments to 9, indicating the button has been pressed 9 times
. . . . . DOWNLOAD . . . . .
You can buy me acontrollerstech
coffeeSensor by clicking DONATE OR Just click DOWNLOAD to download the code
- WavePlayer using STM32 Discovery January 16, 2021
- How to use SPI in LPC2148 December 26, 2020
- STM32 ADC MULTI CHANNEL WITHOUT DMA December 24, 2020
- Send and Receive data to PC without UART (STM32 USB COM) December 10, 2020
- STM32 GPIO INPUT Configuration December 4, 2020
- STM32 GPIO OUTPUT Config using REGISTERS November 24, 2020