In this guide, we will explore on how to implement external switch interrupts with the Nucleo Board. By configuring the board to detect and respond to external switch events, we can trigger specific actions or functions in our projects.
The objective of this project is to utilize external switch interrupts to detect user inputs and perform corresponding actions on the STM32-F103RB Nucleo Board. The LED should stop glowing for 500ms when a push button is pressed and then resume glowing continuously.
To successfully complete this project, you will need the following components:
STM32-F103RB Nucleo Board
LED
Resistor (220-330 ohms)
Push button
Breadboard
Jumper wires
Follow the circuit diagram below to connect the Switch and LED to the Nucleo board:
Connect the positive (anode) pin of the LED to PA8 pin of the STM32-F103RB Nucleo Board.
Connect the negative (cathode) pin of the LED to a resistor and then to the GND pin of the board.
Connect one terminal of the push button to PA9 pin of the STM32-F103RB Nucleo Board.
Finally, connect the other terminal of the push button to the ground (GND) pin of the STM32-F103RB Nucleo Board.
Follow these steps to configure the .IOC file and set up GPIOs and Clock :
Open the STM32 Cube IDE and start by creating a new STM32 project. Click on the board selector tab in the target selection window.
Enter "NUCLEO-F103RB" as the commercial part number and click on "Next" after selecting the board.
3. Enter the Project Name "External_Interrupt" and click on "Finish". After that, click on "Yes" to initialize all the peripherals in default mode.
4. In the Pinout view of the STM32 Cube IDE, locate the PA8 pin. Select the "GPIO_Output" option for PA8 and name it as "LED". Locate the PA9 pin and select the "GPIO_EXTI9" option for PA9. Name it as "SWITCH".
5. In the Pinout & Configuration menu, navigate to the GPIO section and go to NVIC tab. In the Interrupt table, enable the EXTI[9:5]Pin. And leave the rest of the configuration settings as default.
6. After finishing the Pinout Configuration, go to the Clock Configuration menu. In the HCLK field, enter "72 MHz" as the desired frequency for the HCLK (system clock). The Integrated Development Environment (IDE) will automatically search for the necessary clock sources and prescalers based on this configuration.
7. Next, locate the gear-like icon on the toolbar and click on it. This will add the configuration settings we did in the GUI to the code.
Now, let's proceed to understand and write the C code to control the LED using external interrupts . Follow the steps below:
The Device Configuration Tool will automatically open the main.c file for you. In the project, you will find all the necessary pin and port names defined in the Core ⇾ Inc ⇾ main.h file. These definitions will help us easily reference the pins and ports in our code.
Navigate to the main function in the main.c file and paste and replace the following code before SystemClock_Config function:
Note: You can download the full project code from the provided link.
#include "main.h"
void __delay_ms(int32_t k);
/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef huart2;
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if (GPIO_Pin == SWITCH_Pin) // If The INT Source Is EXTI Line9 (A9 Pin)
{
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
__delay_ms(500);
}
}
void __delay_ms(int32_t k)
{
int32_t i, j;
for (i = 0; i < k; i++)
for (j = 0; j < 3000; j++)
{
}
}
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART2_UART_Init();
while (1)
{
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
}
}
After pasting the above code, ensure that you have connected your STM32-F103RB Nucleo Board to your PC/Laptop using a USB cable.
Next, click on the "Run and Debug" icon in the toolbar menu of the IDE (Refer to image below ). This will initiate the build process for your project. The IDE will compile the code. Once the build process is successful, the IDE will start flashing the compiled project to the STM32-F103RB Nucleo Board.
Upon successfully running the project, the LED will stay on till the push button ispressed, the STM32-F103RB Nucleo Board will detect the push button press as an external interrupt. When the button is pressed, the LED will stop glowing for 500ms and then resume glowing continuously.
In this guide, we have learned to use external interrupts with the STM32-F103RB Nucleo Board. This capability enhances user interaction and allows for the creation of engaging projects. Experiment with different button press actions and LED behaviors to explore the full potential of external interrupts with the Nucleo Board.