To Get Start With STM32 Black Pill and STM Cube IDE to Blink External Connected LED Using HAL Programming.
by qwes11_4522 in Circuits > Microcontrollers
105 Views, 1 Favorites, 0 Comments
To Get Start With STM32 Black Pill and STM Cube IDE to Blink External Connected LED Using HAL Programming.
In this we will learn how to use the STM32 Black Pill development board and STM Cube IDE to blink an externally connected LED using HAL programming.
Supplies
- STM32 Black Pill development board
- STM Cube IDE installed on your computer
- Basic knowledge of C programming and microcontrollers
Setting Up the Hardware
First, connect your LED to the STM32 Black Pill board. The positive (longer) leg of the LED should be connected to a digital pin (e.g., PA5), and the negative (shorter) leg should be connected to GND.
Creating a New Project in STM Cube IDE
Open STM Cube IDE and create a new STM32 project. Select the appropriate board (STM32 Black Pill) and give your project a name.
Configuring the Project
In the project configuration, make sure the digital pin you connected the LED to is set as output.
Writing the Code
Now, let's write the code to blink the LED. We will use the HAL library, which provides high-level functions to interact with the hardware.
Building and Uploading the Code
Finally, build the project and upload the code to your STM32 Black Pill board. If everything is set up correctly, you should see the LED start to blink!
Code
#include "main.h"
class Delay {
public:
void operator()(uint16_t time) {
HAL_Delay(time);
}
};
int main() {
HAL_Init();
SystemClock_Config();
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_5;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
Delay delay;
while (1) {
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
delay(1000);
}
}