Pisca Led com microcontrolador Stm32f103c8 keil e Stmcubemx

Led flasher with Stm32f103c8 keil and Stmcubemx microcontroller

After working a lot with 8-bit microcontrollers and learning all the protocols and functions they offer, I now thought of switching to 32-bit microcontrollers. I decided to opt for 32-bit ARM processors due to their popularity in the market. I choose the ARM Cortex-M3 series of processors for my new hobby/learning projects. The reason behind choosing ARM Cortex-M3 series is that Cortex-M3 processors are specially made for connected embedded applications, and microcontrollers built with this series are used in many mid-level projects/applications/embedded products. After so much Googling, I finally made the decision to move forward with stm32 microcontrollers. Stm32 is a family of 32-bit microcontrollers offered by STMicroelectronics. Stm32 microcontrollers are built around the Cortex-M7 , Cortex-M4F , Cortex-M3 , Cortex-M0+ and Cortex-M0 processors.

After deciding to use stm32, I started making initial tutorials on how to get started with stm32. I found a lot of information on how to get started, but none of it is well organized. So I decided to make a series of tutorials on how to start using stm32 microcontrollers. Throughout this series I will discuss all the protocols/functions that stm32 microcontrollers offer. I will present a practical example of easy protocol/interface/function with source code and circuit diagram.

There are many IDEs (integrated development environments) that support stm32 series and you can use any of them to program your stm32 microcontroller. Some Ide are Coocox, keil, mBed, Attolic, microC for Arm. I decide to go with keil and stm32CubeMx. Keil offers in-depth knowledge of the microcontroller and its interface. If you don't want to go in-depth and want a piece of cake, choose MikroElectronica microC for Arm. It is very easy to work with microC ide, it has many examples and libraries you just need to call the functions and everything is done. Stm32CubeMx is a microcontroller peripheral configurator. When using stm32cube you don't need to write configuration code for your stm32 microcontroller. It is a visual platform where you can make microcontroller pins move in, out, enable pull-ups and pull-downs can set the operating frequency of the microcontroller visually and much more. After the visual configuration you can generate the code for the configuration you made. I prefer to work with stmCube because it is officially provided by STMicroelectronics and it is good to work with the material provided by the owner.

Note : Stm32CubeMx is not an Ide, it is a configuration manager. You configure your stm32 microcontroller on it and then generate the configuration code you made to be used with any other IDE. You can directly generate the keil ide project with stmCubeMx by selecting the stmCubeMx option to translate the configuration to the keil ide project.

Installing StmCubeMx and Keil MDK ARM

Installing Keil Ide is quite simple. Download the MDK ARM kit which contains the keil Ide, you don't need the ide to install it separately. Just download the file from the link and install the ide. Arm mdk is available in many editions, the latest is mdk5. I am using mdk5 for my projects and this tutorial is also based on mdk5 and keil uvision-5 ide.
Installing StmCubeMx is also simple. Download StmCube from the link You may be asked to log in. Log in and you will be theirs. Once StmCubeMx is installed, you must install the packages for each stm32 series or series you want to work with. The StmCubeMx and stm32 series packages are two different things. The package for each STM32 series must be installed separately. It is also an option in the StmCubeMx software that installs the series packages. It's under Help > Install new libraries. Since we are starting with stm32f103, we need to install the package for the stm32f1 series. The diagram below explains well about installing the packages.

Installing stm32f103 drivers/packages for StmCubeMx

Installing stm32f1 drivers/packages for StmCubeMx
I bought a cheap stm32f103c8x module on aliexpress. It cost me about $4.5 with free shipping to Pakistan. Shipping took almost 1.5 months and 45 days. The board is cool and offers almost all the features needed to start using stm32 microcontrollers. The board pin is given below.

Cheap Stm32f103 Development Board Pinout and Features Offered

Cheap Stm32f103 Development Board Pinout and Features Offered

LED flasher with stm32f103 keil and stmcubemx

The upper module has an LED connected to pin no. 13 of port c. To start, let's blink. Stm32 microcontroller pins offer multiple features on a single pin. Selecting one and disabling others must be done carefully. The I/O pins of Stm32 microcontrollers can be used in five modes

  • Entry mode
  • Analog mode
  • Output mode
  • Alternative function mode
  • External interrupt/event lines

For our purpose, we will use pin 13 of port c as the output. Almost all stm32 pins have internal pull up and pull down resistors. Since we are not using gpio in input mode, we are not using pull up and down resistors. Stm32 pins can work at different frequencies, let's operate the pin at low frequency. The Stm32 pins can also be initialized low or high after initialization. I placed pin #13 low.

Note: I will use the microcontroller's internal 8Mhz RC oscillator. The top plate has an external 8Mhz crystal but I won't use it. In later tutorials we will use it.

Creating new project in StmCubeMx

Creating a new project is simple. Go to File > New Project. A window will appear to select the microcontroller series, series lines and package. After selecting the package, click on the microcontroller you are using for your project. In our case it is STM32f103C8Tx.

StmCubeMx creating new project

StmCubeMx creating new project
After selecting the stm32f103c8 microcontroller, a window containing the mcu diagram will appear. Click pin #13 and set gpio as output.

StmCubeMx Selecting stm32f103c8 gpio microcontroller as output

StmCubeMx Selecting stm32f103c8 gpio microcontroller as output
To configure GPIO, click on the configuration tab and then on the GPIO button.

Configuring gpio of stm32f103 in stmcubemx

Configuring gpio of stm32f103 in stmcubemx
Click on the pin names so their settings appear. In the configuration window, set the gpio mode, output level, clock speed and give the gpio a name. Press apply to apply the changes.

Stmcube Gpio Configuration

Stmcube Gpio Configuration
The name we gave the pin now appears on it. We can use gpio in the code with this name. Now it's time to generate the code. Click the gear icon to generate code.

stmcubemx gpio name changed and generating code

stmcubemx gpio name changed and generating code
A window appears after clicking the gear icon. Name the project and select the location for the project files. As we are using MDK-ARM V5, in ToolChain/IDE select mdk-arm. Click OK to generate the code.

stmcube project localization and keil code generation

stmcube project localization and keil code generation
Click OK after all settings and codes are generated in the location you specified. Four folders will appear in the location. MDK-ARM contains the keil ide code. The stmcubemx project files are also present in the location. The ico extension file is stmcubemx. Open MDK-ARM folder which contains some file. The file with .uvprojx extension is a keil ide file. Now you can open the file to make some more changes to the code.

stmcube and keil ide files

stmcube and keil ide files
StmCubeMx translation code that contains the HAL library functions. HAL are libraries released by stm to work with their microcontrollers. The functions of the HAL libraries and their definition datasheet are available in the user manual, given that the 19 MB file is actually the function definition explanation document of the HAL libraries . The code generated by stmcubemx has instructions listed below. I write the meaning of each statement with it.
To blink the LED, add the lines to the empty while function. Remember that stmcube only generates the configuration code. We have to write our logic ourselves. Therefore, in the while(1) function add the toggle function.
​HAL_GPIO_TogglePin(Led_GPIO_Port,Led_Pin);
HAL_Delay(1000);

Flashing LED Code Stm32

Flashing LED Code Stm32

Upolad code for stm32 microcontroller

As the development board does not have an on build programmer, I used an external St Link programmer to load the code on the board. To send the code you must make some settings in keil. Follow the steps below to upload the code.

Keil uploads code to the stm32 development board using the stlink debugger

Keil uploads code to the stm32 development board using the stlink debugger
Note: After sending the code, press the reset button on the board. The code will not work unless you reset the board by pressing the reset button.
Project files/code folder

Related Content

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.