The project has been developed using RT-Spark Development Board (STM32F407ZGT6). There is a of a random number between one and three seconds before turning on green LED. Once the light turns on, the user should press the button at the earliest convenience. The microcontroller calculates the number of loops executed before the interrupt from the button occurs, which is a relative measure of the reaction speed of the user.
STM32CubeMXStep 1: Configure the Hardware in STM32CubeMX
1. Open STM32CubeMX and click File then New Project.
2. Click Access to MCU Selector Search for and select the STM32F407ZGT6 microcontroller.
3. In the left menu, expand System Core and click SYS Change the Debug interface to Serial Wire This is critical for flashing the board later.
4. Configure the LED: On the chip diagram, left-click pin PF11 and select GPIO_Output. Right-click the same pin, select Enter User Label and name it GREEN_LED.
5. Configure the Button: Left-click pin PC0 (which corresponds to the LEFT button on the RT-Spark board) and select GPIO_EXTI. Right-click it, select Enter User Label, and name it USER_SWITCH.
6. Set the Interrupt Trigger: Go to System Core > GPIO. Click the USER_SWITCH pin (PC0) and change the GPIO mode to External Interrupt Mode with Falling edge trigger detection.
7. Enable the Interrupt: Go to System Core > NVIC and check the Enable box next to the EXTI line for your switch (EXTI line0 interrupt).
8. Go to the Project Manager tab, name your project, change the Toolchain / IDE to STM32CubeIDE, and click Generate Code.
STM32CubeIDE SetupStep 2: Import the Project into STM32CubeIDE
1. In the Project Explorer, locate your Core folder. You will need to create four new files here to handle our custom functions.
2. Right-click the Inc folder, select New > Header File, and create leds.h and delay.h.
3. Right-click the Src folder, select New > Source File, and create leds.c and delay.c.
4. Paste the respective support module code (provided in the Code section) into these four files and save them.
Step 3: Write the Core Firmware Logic1. Open your main.c file located in the Src folder.
2. Near the top of the file, find the /* USER CODE BEGIN Includes */ section and include your custom libraries along with the standard library for random number generation:
#include "leds.h"
#include "delay.h"
#include <stdlib.h>3. Scroll down to /* USER CODE BEGIN PV */ and declare the global variables that will track your interrupt status and your final score:
volatile int isr_triggered = 0;
volatile uint32_t final_reaction_time = 0;4. Scroll to the bottom of the file to /* USER CODE BEGIN 4 */ and write the callback function. This tells the microcontroller to change our flag variable to '1' the exact moment the button is pressed.
5. Finally, find the infinite while (1) loop inside your int main(void) function. Paste the core game logic (provided in the Code section) inside the /* USER CODE BEGIN 3 */ block.
1. Click the Hammer icon in the top toolbar to build the project. Ensure the console shows 0 errors.
2. Plug your RT-Spark board into your computer and click the Bug icon (Debug) to flash the code to the microcontroller.
3. Once the Debug view opens, locate the Live Expressions tab (or open it via Window > Show View > Live Expressions).
4. Click "Add new expression" and type in final_reaction_time.
5. Click the Resume icon (Green Play button) to start the code.
6. Play the game! Keep your eyes on the white square on the board. When it turns green, press the LEFT button as fast as you can. Look at your computer screen to see your reaction score update live!




Comments