No ACL setup yet! Denying access to everyone.
en:edu:zmitac:macro:embedeed:lab:stm32-gpio
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:edu:zmitac:macro:embedeed:lab:stm32-gpio [2024/03/17 17:04] – [Tasks to be implemented] ktokarz | en:edu:zmitac:macro:embedeed:lab:stm32-gpio [2025/04/02 10:05] (current) – ktokarz | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== STM32 Control of build-in LEDs with buttons===== | + | ====== STM32 Control of built-in LEDs with buttons |
| This exercise presents how to control three LEDs with buttons mounted on the Nucleo STM32WB55 board. | This exercise presents how to control three LEDs with buttons mounted on the Nucleo STM32WB55 board. | ||
| ===== Prerequisites ===== | ===== Prerequisites ===== | ||
| A good understanding of the digital GPIO pins operation and principles of interrupts is necessary. We use the Arduino Core STM32 framework (stm32duino)((" | A good understanding of the digital GPIO pins operation and principles of interrupts is necessary. We use the Arduino Core STM32 framework (stm32duino)((" | ||
| ===== Background ===== | ===== Background ===== | ||
| - | LEDs and buttons are electrically connected to microcontroller' | + | LEDs and buttons are electrically connected to the microcontroller' |
| ==== LED connection ==== | ==== LED connection ==== | ||
| - | LEDs are electrically connected to microcontroller' | + | LEDs are electrically connected to the microcontroller' |
| {{ : | {{ : | ||
| ==== Buttons connection ==== | ==== Buttons connection ==== | ||
| - | The buttons on board are named SW1, SW2, and SW3, and are connected to pins PC4, PD0, and PD1 respectively. In the software, the names of the pushbuttons are SW1_BTN, SW2_BTN, and SW3_BTN. Pushbuttons are easily accessible in the software by their names using the Arduino function DigitalRead(). Buttons are connected to the ground, so the reading gives the " | + | The buttons on board are named SW1, SW2, and SW3, and are connected to pins PC4, PD0, and PD1, respectively. In the software, the names of the pushbuttons are SW1_BTN, SW2_BTN, and SW3_BTN. Pushbuttons are easily accessible in the software by their names using the Arduino function DigitalRead(). Buttons are connected to the ground, so the reading gives the " |
| {{ : | {{ : | ||
| ==== Interrupts ==== | ==== Interrupts ==== | ||
| - | The button can control LED in a simple way with a pair of functions digitalRead() and digitalWrite(). The drawback of this approach is the dependency of the reading and writing time on the current flow of the program. If the program calls the Delay() function it's impossible to perform other actions, including controlling the LED state. The asynchronous approach would allow us to use the Delay() or other blocking functions together with the instant reaction on the button press. This can be achieved with interrupts. It is possible to call the interrupt handler function as the callback attached to the button press event. To define the attachment you can use the attachInterrupt() function. It requires three parameters: | + | The button can control |
| - | pin number, address of the callback function, and event type. | + | |
| - | Attaching switch 1 with the SW1_callback function which is called every time the button is pressed | + | |
| - | <code c> | + | |
| - | attachInterrupt (SW1_BTN, SW1_callback_f, | + | |
| - | </ | + | |
| - | SW1_BTN is the pin number of the chosen button. SW1_callback is the name of the function | + | |
| < | < | ||
| - | Except for CHANGE, RISING, and FALLING there are also HIGH and LOW events. HIGH works the same as RISING, LOW the same as FALLING. | + | Except for CHANGE, RISING, and FALLING, there are also HIGH and LOW events. HIGH works the same as RISING, LOW the same as FALLING. |
| </ | </ | ||
| - | Inside the callback function, you can write any code until you keep the function short, and avoid using loops and delays. The function called at the interrupt level has higher priority over the main program, so longer execution can disrupt the normal behaviour of the program. | + | Inside the callback function, you can write any code until you keep the function short, and avoid using loops and delays. The function called at the interrupt level has a higher priority over the main program, so longer execution can disrupt the normal behaviour of the program. |
| ==== Tasks to be implemented ==== | ==== Tasks to be implemented ==== | ||
| 1. Write the program that will switch the red LED for one second, then the green LED for one second, and the blue LED for one second repeatedly. Use the Delay() function. This part is to be familiar with the environment, | 1. Write the program that will switch the red LED for one second, then the green LED for one second, and the blue LED for one second repeatedly. Use the Delay() function. This part is to be familiar with the environment, | ||
| \\ | \\ | ||
| - | 2. Write the program that lights the blue LED when the SW1 button is pressed, green RED when SW2 is pressed and red LED when SW3 is pressed. | + | 2. Write the program that lights the blue LED when the SW1 button is pressed, green LED when SW2 is pressed, and red LED when SW3 is pressed. |
| \\ | \\ | ||
| - | 3. Write the program that controls the LEDs with interrupts. One LED should blink periodically using the Delay() function, and the second LED should be controlled asynchronously with two buttons. One button press should turn the LED on, second button should turn the LED off. | + | 3. Write the program that controls the LEDs with interrupts. One LED should blink periodically using the Delay() function, and the second LED should be controlled asynchronously with two buttons. One button press should turn the LED on, and the second button should turn the LED off. |
| \\ | \\ | ||
| - | 4. You can experiment | + | 4. Experiment |
| + | |||
| + | ==== Task 1 ==== | ||
| + | In the Arduino framework for STM32 handling the GPIO pins is done the same way as for the classic Arduino board. First, the pin should be configured as the output. | ||
| + | <code c> | ||
| + | pinMode(LED_RED, | ||
| + | </ | ||
| + | |||
| + | To turn the LED on, we have to set the output pin to " | ||
| + | <code c> | ||
| + | DigitalWrite(LED_RED, | ||
| + | </ | ||
| + | |||
| + | To turn the LED off, we have to clear the output pin. | ||
| + | <code c> | ||
| + | DigitalWrite(LED_RED, | ||
| + | </ | ||
| + | Use the delay function to be able to observe the behaviour of LEDs. | ||
| + | <code c> | ||
| + | Delay(500); //Parameter is the delay time in microseconds | ||
| + | </ | ||
| + | |||
| + | ==== Task 2 ==== | ||
| + | From task 1, you are already familiar with turning the LED on and off. To read the button state, you can use the digitalRead() function, which requires the pin number as the parameter, and returns the state of the input pin at the time of the function call. | ||
| + | <code c> | ||
| + | digitalRead(SW1_BTN); | ||
| + | </ | ||
| + | |||
| + | ==== Task 3 ==== | ||
| + | This task requires writing the callback functions triggered by the events of button pressing. One function should be attached to the chosen push button, and the second function should be attached to another push button. Attaching switch 1 with the SW1_callback_f function which is called every time the button is pressed, can look like the following line. | ||
| + | <code c> | ||
| + | attachInterrupt (SW1_BTN, SW1_callback_f, | ||
| + | </ | ||
| + | SW1_BTN is the pin number of the chosen button. SW1_callback_f is the name of the function which works as the interrupt handler, FALLING means that our function will be started whenever the signal at the pin changes from " | ||
| + | <code c> | ||
| + | void SW1_callback_f(){ | ||
| + | digitalWrite(LED_RED, | ||
| + | } | ||
| + | </ | ||
| + | ==== Task 4 ==== | ||
| + | In this task, add the delay() function inside the interrupt handler. Experiment with the number of microseconds. Observe the results. | ||
| + | |||
| + | ==== Result validation ==== | ||
| + | You should be able to observe LED blinking in task 1 and control LEDs with push buttons for tasks 2 and 3. For task 4 you should observe the increase in LED timing after the button press. | ||
| + | |||
| + | ===== FAQ ===== | ||
| + | **What if the time of interrupt handler execution is too long?**: You need to avoid long interrupt handlers. They should be as short as possible. In some cases, if a handler is too long, it can block proper execution of the operating system and cause the system to crash. A good practice is to write in the handler code for setting the flag, which passes the information to the main code about the need to perform some task, and code for the whole task in the main program. | ||
en/edu/zmitac/macro/embedeed/lab/stm32-gpio.1710691456.txt.gz · Last modified: by ktokarz
