vuoi
o PayPal
tutte le volte che vuoi
A second, more precise flowchart takes debounce into account, implementing a delay to check
the previous state of the button and determine if an action on the button has occurred.
Then we moved on to the configuration:
• PA5 (led) in output mode;
• PC13 (pushbutton) in input mode.
Using the manual, we identified the following HAL functions to be used:
• HAL_GPIO_ReadPin to read the value of a pin;
• HAL_GPIO_WritePin to set the value of a pin;
• HAL_GPIO_TogglePin to switch the status of the pin;
• HAL_Delay to introduce a delay in milliseconds.
We proceeded to write the C code and added it to the “USER code” sections of the
automatically generated code in the IDE.
#include "main.h"
uint32_t debounce_time = 20; // Debounce time in milliseconds
int main(void){
while (1)
{ // Check if the button is pressed (transition from high to low)
if (HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13) == 0)
{ // Small delay to handle debounce
HAL_Delay(debounce_time);
// Confirm that the button is still pressed after debounce delay
if (HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13) == 0)
{ // Toggle the LED state
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
}
}
}
}
Note: The pushbutton has a logic level of "0" when pressed.
Testing the program on the board empirically demonstrated the correct operation of the
exercise.
2.2 Varying blinking frequency
The goal of this exercise is to create a program that progressively increases the LED's blinking
frequency with each button press. After a reset, the LED should blink at a frequency of 0.25 Hz.
If the frequency exceeds 8 Hz (a limit set by us), the program resets to its initial state.
First, we implemented the flowchart:
The flowchart checks if the pushbutton is pressed. If so, the frequency is doubled. If the
imposed limit is exceeded, the program resets. The variable TOGGLING_PERIOD accounts for
debounce in frequency calculation when the button is pressed, while it remains equal to
LED_PERIOD otherwise.
Using the same method, we continued with the configuration, which was identical to the
previous exercise.
Then, we implemented the code:
#include "main.h"
// Initialize variables
uint8_t reset_state = 1; // Start in reset state (LED off)
uint32_t led_period = 4000; // Initial LED period in milliseconds
uint32_t debounce_time = 20; // Debounce time in milliseconds
uint32_t toggling_period; // Initialize toggling period to initial value
int main(void)
{ while (1)
{ // Check if the button is pressed and was previously released
if (HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13) == 0) {
HAL_Delay(debounce_time); // Debounce delay
// Check the button state again after the delay
if (HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13) == 0) {
if (reset_state == 1) {
// Exit reset state
reset_state = 0;
led_period = 4000; // Restore initial period
}
else {
// Halve the period and check the limit
led_period *= 0.5;
if (led_period < 125) {
reset_state = 1; // Enter reset state
}
}
}
toggling_period = led_period - debounce_time; // Update toggling period
}
// LED toggling logic
if (reset_state == 1) {
// Reset state: keep the LED off
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, 0);
}
else {
// LED toggling logic
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
HAL_Delay(toggling_period);
toggling_period = led_period; // Reset toggling period
}
}
}
Testing the program revealed a limitation of the polling method: the code is unresponsive to the
button during delays introduced by the HAL_Delay() function.
2.3 Generating a Square Wave
The objective of this exercise is to generate a square wave with a frequency of 500 Hz on PINA10
and observe it on an oscilloscope.
We started by generating the flowchart:
Then we moved on to the configuration:
• PA10 in output mode.
And then to the implementation of the code:
#include "main.h"
uint32_t led_period = 2; // LED blink period (ms)
int main(void)
{
while (1)
{ // LED blinking
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_10);
HAL_Delay(led_period);
}
}
The pin's signal was connected to the oscilloscope using the board's Morpho headers.