Introduction: STM32F103 GPIO Input (using Keil and STMCubeMX)
In this Tutorial, I will show you how to get input from GPIO pin.
We will take input from the Blue Button on our STM32 Board to switch LED on same Board.
This tutorial is divided into three steps:
Step 1: Creating Project In STM32CubeMX
Step 2: Writing Code in Keil
Step 3: Visualizing Output
Step 1: Creating Project in STM32CubeMX
Open STM32CubeMX. Click on “New Project”. From ‘Series’ select ‘STM32F1’. From ‘lines’ select ‘STM32F103’. Now, From MCU list select “STM32F103RBTx” and Click “OK”.
{See Image[1]}
Now, That “Blue Push Button” on the Nucleo Board which we will be using as input, is connected to PC13 Pin of the microcontroller. Now Select ‘PC13’on the microcontroller shown in STM32CubeMx Software and Select ‘GPIO_Input’. Using this Button we will toogle the LED on our nucleo board which is connected to ‘PA5’ pin of the microcontroller. So, Now Select ‘PA5’on the microcontroller shown in STM32CubeMx Software and Select ‘GPIO_Output’. After this your microcontroller must look like this:
{See Image[2]}
Now Click on “Configuration Button” as shown in previous figure. Under ‘System’ Click on ‘GPIO’, So, that you see a window like this:
{See Image[3]}
Now, Select ‘PA5’ Pin and in User Label write “Ld2”. After this select, ‘PC13’ pin and in User Label write ‘B1’. So that this window now will look like this. Click “Apply” and Click “OK”.
{See Image[4]}
And so your microcontroller in STM32CubeMX now looks like this: (to switch to this tab click on ‘PIN OUT’ button as show in Figure 2)
{See Image[5]}
Now Click on ‘Generate Code’ Button or Go to ‘Project>Generate Code’ or press ‘Ctrl+Shift+G’. A Project Setting Window will appear. In there write Project name, Select where to save Project select ‘MDK-ARM V5’ under ‘toolchain/IDE’ and then click ‘OK’. Doing this will generate code. If asked either to open Project in Keil, click open Project. To manually open Project Where you just have saved this Project. There you will find a folder named ‘MDK-ARM’. In that folder open file ‘.uvprojx’ with Keil.
Step 2: Writing Code in KEIL
Now, In Keil, in Project Window open ‘Configuration’ folder. In there, open ‘Application/User’ in there, open file ‘main.c’. Scroll down in to while loop as shown in figure:
{See Image[6]}
Now, here in this while loop we will write code to switch led on if Button is being pressed and turn it off when Button is in release State. So, Write following lines of code in while loop:
//we configured our button as B1 so we will use 'B1_GPIO_Port' and 'B1_Pin'
//this function 'HAL_GPIO_ReadPin()' will digitally read the status of givn pin
//now if button is pressed it will return 0
//and if button is released it will return 1
if(HAL_GPIO_ReadPin(B1_GPIO_Port,B1_Pin)==0) //=> Button is Pressed
{
//Switch LED ON.
//remember that we have configured our LED as Ld2, so
//this function 'HAL_GPIO_WritePin()' will digitally write on given pin
//writting 1, will give HIGH output
//writting 0, will give LOW ouptut
HAL_GPIO_WritePin(Ld2_GPIO_Port,Ld2_Pin,1);
}
else //=>Button is released
{
HAL_GPIO_WritePin(Ld2_GPIO_Port,Ld2_Pin,0);
}
Now, you Keil window should look like this:
{See Image[7]}
Now, after this, click on Build Button as shown in prev. image. This will build you Project. Now, Click on “LOAD” button too load this Project onto your STM32 Board as shown in prev. image.
Step 3: VISUALIZING OUTPUT
After Building and Loading the Project onto STM32 Board. Click the “Black” reset button on the Board.
Now, you will see that when you press that blue button it will turn LED ON. And when you will release that button it will turn LED OFF.