Introduction: STM32F103 GPIO Intrupt (using Keil and STMCubeMX)

In this Tutorial, I will show you how to use GPIO pin as an interrupt.

We will take interrupt 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: Programming 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 interrupt input, is connected to PC13 Pin of the microcontroller. Now Select ‘PC13’on the microcontroller shown in STM32CubeMx Software and Select ‘GPIO_EXTI13’. Using this Button, we will toggle 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 switch to configuration tab by pressing that ‘configuration’ button. Under System Click on “NVIC”. Select the “EXTI line[15:10] interrupts” line Click on the “Enabled” Check Box. Set Preemption Priority to 1. Click “APPLY” and the click “OK”.

{See Image[3]}

Now Under System Click on “GPIO”. 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)

{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 their 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: Programming in Keil

Now, In Keil, in Project Window open ‘ Configuration’ folder. In there, open ‘Application/User’ in there, open file ‘stm32f1xx_it.c’. Scroll down in to “EXTI15_10_IRQHandler” function as shown in figure:

{See Image[6]}

Add following line of code in this function

HAL_GPIO_TogglePin(Ld2_GPIO_Port, Ld2_Pin); //this will toggle the LED

And thus your window be like:

{See Image[7]}

Now, after this, click on Build Button (or press ‘F7’). This will build you Project. Now, Click on “LOAD” button to load this Project onto your STM32 (or press ‘F8’).

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 toggle the LED.

i.e. If its ON it will turn it OFF, and if its OFF then it will turn it ON.