You are on page 1of 20

Programming STM32F103C8 using Keil uVision & STM32CubeMX

EMBEDDED

Programming STM32F103C8 using Keil uVision & STM32CubeMX


STM32 Microcontrollers which uses ARM Cortex M architecture is now becoming popular and are
used in many applications because of its feature, cost and performance. We have programmed
STM32F103C8 using the Arduino IDE in our previous tutorials. Programming STM32 with Arduino
IDE is simple, as there are lots of libraries available for various sensors to perform any task, we just
need to add those libraries in the program. This is an easy procedure and you may not get into deep
learning about the ARM processors. So now we are getting into the next level of programming called
ARM programming. By this we can, not only improve our structure of the code but can also save
memory space by not using unnecessary libraries.

STMicroelectronics introduced a tool called STM32Cube MX, which generates basic code


according to the peripherals and the selected STM32 board. So we don’t need to worry about coding
for basic drivers and peripherals. Further this generated code can be used in Keil uVision for editing
according to requirement. And finally the code is burned into STM32 using ST-Link programmer from
STMicroelectronics.

In this tutorial we will learn how to program STM32F103C8 using Keil uVision &
STM32CubeMX by doing a simple project of interfacing a push button and LED with the
STM32F103C8 Blue Pill board. We will generate the code using STM32Cube MX then edit &
upload the code to STM32F103C8 using Keil uVision. Before getting into detail, we will first learn
about ST-LINK programmer and STM32CubeMX software tool.

ST-LINK V2

The ST-LINK/V2 is an in-circuit debugger and programmer for the STM8 and STM32


microcontroller families. We can upload code to STM32F103C8 and other STM8 & STM32
microcontrollers using this this ST-LINK. The single wire interface module (SWIM) and JTAG/serial
wire debugging (SWD) interfaces are used to communicate with any STM8 or STM32 microcontroller
located on an application board. As STM32 applications use the USB full-speed interface to
communicate with Atollic, IAR, Keil or TASKING integrated development environments, so we can
use this hardware to program the STM 8 & STM32 microcontrollers.

Above is the image of ST-LINK V2 dongle from STMicroelectronics that supports the full range of
STM32 SWD debugging interface, a simple 4-wire interface (including power), fast and stable. It is
available in a variety of colours. The body is made out of aluminium alloy. It has a blue LED indication
as it is used to observe the working state of the ST-LINK. The pin names are clearly marked on the
shell as we can see in the above image. It can be interfaced with the Keil software where the program
can be flashed to the STM32 microcontrollers. So let’s see in this tutorial how this ST-LINK
programmer can be used to program STM32 microcontroller. Below image shows the pins of the
ST-LINK V2 module.
Note: When connecting ST-Link with the computer for first time .We need device driver to be
installed. Device drivers can be found in this link according to your operating system.

STM32CubeMX

STM32CubeMX tool is part of STMicroelectronics STMCube .This software tool makes the


development easy by reducing development effort, time and cost. STM32Cube includes
STM32CubeMX which is a graphical software configuration tool that allows the generation of C
initialization code using graphical wizards. That code can be used in various development
environments like keil uVision, GCC, IAR etc. You can download this tool from the following link.

STM32CubeMX has following features  

 Pin out-conflict solver


 A clock-tree setting helper
 A power-consumption calculator
 An utility performing MCU peripheral configuration like GPIO pins, USART etc
 An utility performing MCU peripheral configuration for middleware stacks like USB, TCP/IP
etc

Materials Required

Hardware

 STM32F103C8 Blue Pill board


 ST-LINK V2
 Push Button
 LED
 Breadboard
 Jumper Wires
Software

 STM32CubeMX Code Generation Tool (Link)


 Keil uVision 5(link)
 Drivers for ST-Link V2 (link)

Circuit Diagram and Connections

Below is the circuit diagram to simply connect an LED with STM32 board using a pushbutton.
Connection between ST-LINK V2 & STM32F103C8

Here the STM32 Blue Pill board is powered from the ST-LINK which is connected to the computer’s
USB port. So we need not to power the STM32 separately. The table below shows the connection
between ST-Link and Blue pill board.

STM32F103C
ST-Link V2
8

GND GND

SWCLK SWCLK

SWDIO SWDIO

3V3 3.3V

LED & Push Button

The LED is used to indicate the output from Blue Pill board when a push button is pressed. LED’s
anode is connected to the pin PC13 of the Blue Pill board and cathode is grounded.

A push button is connected to provide input to the pin PA1 of Blue Pill board. We must also use a pull
up resistor of value 10k because the pin might float without any input when the button is released. One
end of the push button is connected to ground and other end to pin PA1 & a pull up resistor of 10k is
also connected to 3.3V of Blue Pill board.
Creating and burning a program into STM32 using Keil uVision and ST-Link

Step 1:- First install all the device drivers for ST-LINK V2, software tools STM32Cube MX & Keil
uVision and install necessary packages for STM32F103C8.

Step 2:- Second step is Open >> STM32Cube MX

Step 3:- Then Click on New Project


Step 4:- After that search & select our microcontroller STM32F103C8
Step 5:- Now the pin-out sketch of STM32F103C8 appears, here we can set the pin configurations.
We can also select our pins in the peripherals section according to our project.

Step 6:- You can also click on the pin directly and a list appears, now select the required pin
configuration.
Step 7:- For our project we have selected PA1 as GPIO INPUT, PC13 as GPIO OUTPUT & SYS
debug as SERIAL WIRE, here only we connect the ST-LINK SWCLK & SWDIO pins. The selected
& configured pins appear in GREEN colour. You can note that in below image.
Step 8:- Next under the Configuration tab, select GPIO to set GPIO pin configurations for the pins we
have selected.

Step 9:- Next in this pin configuration box we can configure User Label for pins we are using, that is
user defined pin names.
Step 10:- After that click on Project >> Generate Code.
Step 11:- Now the project settings dialog box appears. In this box choose your project name and
location and select the development environment .We are using Keil so select MDK-ARMv5 as IDE.

Step 12:- Next under Code Generator tab, select Copy only the necessary library files and then click
OK.
Step 13:- Now the code generation dialog box appears. Select Open Project to open project
automatically the generated code in Keil uvsion.
Step 14:- Now the Keil uVision tool opens with our generated code in STM32CubeMx with the same
project name with necessary library and codes that are configured for the pins we selected.

Step 15:- Now we just need to include the logic to perform some action at the output LED (pin PC13 )
when the button is pressed and released at the GPIO input (pin PA1). So select our main.c program to
include some codes.
Step 16:- Now add the code in the while(1) loop, see the below image where I highlighted that section
to run the code continuously.

while (1)

if(HAL_GPIO_ReadPin(BUTN_GPIO_Port,BUTN_Pin)==0) //=> DETECTS Button is Pressed

HAL_GPIO_WritePin(LEDOUT_GPIO_Port,LEDOUT_Pin,1); //To make output high when


button pressesd

else

{
HAL_GPIO_WritePin(LEDOUT_GPIO_Port,LEDOUT_Pin,0); //To make output Low when
button de pressed

Step 17:- After finish editing the code, click the Options for Target icon under the debug tab select
ST-LINK Debugger
Also, click on Settings button and then under Flash Download tab tick the Reset and Run check box
and click ‘ok’.
Step 18:- Now click on Rebuild icon to rebuild all target files.

Step 19:- Now you can plug in the ST-LINK to computer with the circuit connections ready and click
on the DOWNLOAD icon or press F8 to flash the STM32F103C8 with the code you have generated
and edited.
Step 20:- You can notice the flashing indication at the bottom of the keil uVision window.
Output of Keil Programmed STM32 Board

Now when we press the push button, LED turn On and when we release it, the LED turns Off.
Program

The main part which we have added in the generated program is shown below. This below code needs
to be included in while(1) of main.c program generated by the STM32CubeMX. You can go back to
Step 15 to step 17 to learn how it should be added in main.c program.

You might also like