Going Through A Starter Project
Last updated
Last updated
0. Create New Project
File -> New -> STM32 Project
Identify the MCU version that you are using. For this entire tutorial, we will be using STM32F446RET6 unless otherwise noticed.
Search for "STM32F446RET6" and select the first result. Then click Next.
Name the project, select a suitable location, and then keep the following settings. Then click Finish.
STM32CubeIDE will open the .ioc graphical configuration utility first.
In the left sidebar, select System Core -> SYS.
Select Debug to use Serial Wire
Select Connectivity -> USART2.
Set Mode to be Asynchronous.
In the Clock Configuration tab, set HCLK to 160 MHz and click Enter, the IDE will automatically derive the required PLL parameters.
Press Ctrl+S. STM32CubeIDE will start to generate the codes.
In main.h
and main.c
, add the following code
main.h
main.c
Click the green "run ..." button
If there's a popup menu, click "OK".
We should be able to see hello world with an upward counter printing from any serial monitor.
Recommend using the VSCode Serial Monitor extension from Microsoft.
Although our code is running now, putting everything into main.c
and main.h
isn't really a good idea ---- these files are autogenerated by STM32CubeIDE, and we would risk breaking it when putting and editing our code between those autogenerated lines.
Thus, a better way of managing the code would be to separate the system-managed code from our user code. Now we would create two new source files, app.h
and app.c
, and move our existing code there.
In the Project Explorer, right click the "Src" folder, select "New -> Source File".
Name the file "app.c" and click "Finish".
Similarly, in the Project Explorer, right click the "Inc" folder, select "New -> Header File".
Name the file "app.h" and click "Finish".
Then, we can move all the previous user codes into these newly created files.
app.h
app.c
Finally, we need to invoke the user application functions in main.c
main.h
main.c