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/18 08:53] – [FAQ] 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 pin number, address of the callback function, and event type. You can the event type as FALLING which detects the pin state change from " | + | The button can control |
| < | < | ||
| - | 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, and the 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. | ||
| Line 32: | Line 32: | ||
| </ | </ | ||
| - | To turn the LED on we have to set the output pin to " | + | To turn the LED on, we have to set the output pin to " |
| <code c> | <code c> | ||
| DigitalWrite(LED_RED, | DigitalWrite(LED_RED, | ||
| </ | </ | ||
| - | To turn the LED off we have to clear the output pin. | + | To turn the LED off, we have to clear the output pin. |
| <code c> | <code c> | ||
| DigitalWrite(LED_RED, | DigitalWrite(LED_RED, | ||
| Line 47: | Line 47: | ||
| ==== Task 2 ==== | ==== 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. | + | 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> | <code c> | ||
| digitalRead(SW1_BTN); | digitalRead(SW1_BTN); | ||
| Line 53: | Line 53: | ||
| ==== Task 3 ==== | ==== Task 3 ==== | ||
| - | This task requires writing the callback functions triggered | + | This task requires writing the callback functions triggered |
| <code c> | <code c> | ||
| attachInterrupt (SW1_BTN, SW1_callback_f, | attachInterrupt (SW1_BTN, SW1_callback_f, | ||
| Line 64: | Line 64: | ||
| </ | </ | ||
| ==== Task 4 ==== | ==== Task 4 ==== | ||
| - | In this task add the delay() function inside the interrupt handler. Experiment with the number of microseconds. Observe the results. | + | In this task, add the delay() function inside the interrupt handler. Experiment with the number of microseconds. Observe the results. |
| ==== Result validation ==== | ==== Result validation ==== | ||
| - | You should be able to observe LED blinking in task 1 and to 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. | + | 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 ===== | ===== 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. | **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.1710748382.txt.gz · Last modified: by ktokarz
