Friday 2 October 2020

How to program STM32 F1 using STM32CubeMx and Keil uVision - LED Blinking using STM32F103C8

 

STM32 is a family of 32-bit microcontroller integrated circuits by STMicroelectronics. The STM32 chips are grouped into related series that are based around the same 32-bit ARM processor core, such as the Cortex-M33F, Cortex-M7F, Cortex-M4F, Cortex-M3, Cortex-M0+, or Cortex-M0. Internally, each microcontroller consists of the processor core, static RAM, flash memory, debugging interface, and various peripherals.

STM32 F1

The STM32 F1-series was the first group of STM32 microcontrollers based on the ARM Cortex-M3 core and considered their mainstream ARM microcontrollers. The F1-series has evolved over time by increasing CPU speed, size of internal memory, variety of peripherals. There are five F1 lines: Connectivity (STM32F105/107), Performance (STM32F103), USB Access (STM32F102), Access (STM32F101), Value (STM32F100). 

STMicroelectronics introduced a tool called STM32Cube MX, which generates basic code according to the peripherals and 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 can burned into STM32 using ST-Link V2 programmer as Well as FTDI TTL Convertor.

In this tutorial we will learn How to Program STM32F103C8 using STM32CubeMX and Keil uVision by doing a simple project of Inbuilt LED Blinking with STM32F103C8 Blue Pill board. We will generate the code using STM32CubeMX then edit & upload the code to STM32F103C8 using Keil uVision. Before getting into detail, we will first learn about how to install the Software (both Keil uVision and STM32CubeMX), configure the software as per our requirements.

Let's Get Started,

Software:

  • STM32CubeMX Code Generation Tool (link), "You have the option here to select the version of software to download, i recommend you to download "STM32CubeMX 4.27.0" as i am using the same for this tutorial."

  • Keil uVision V5 (link)

  • Drivers for ST-Link V2 (link) 

Note: Make sure to install all given software before start this tutorial

 

Circuit Diagram and Connections

Connection between ST-LINK V2 & STM32F103C8

OR

Connection between FTDI TTL Convertor & STM32F103C8

Pin diagram of stm32f103c8

Configure and Generate code using STM32CubeMX

 

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

Step 2 - Open >> STM32CubeMX

Step 3 - Then Click on New Project

 
Step 4 -
 

  1. Click to MCU Selector.
  2. Search part number of STM32 microcontroller in this tutorial we are using stm32f103c8.
  3. From drop down of MCUs list >> select the searched microcontroller from drop down list.
  4. Click on Start Project 
Step 5 -
 
  1. Click on Pinout to configure the stm32
  2. Expand SYS using "+" >> select Serial Wire in Debug drop down
Step 6 -

As we want to do LED Blinking with STM32F103C8, we need to set the GPIO pin as OUTPUT for LED. In this tutorial, we are working on Inbuilt LED blinking. so as per the datasheet, the inbuilt led of stm32F103C8 is internally connected with PC-13 >> Click on PC-13 pin of STM32 and set it as "GPIO_OUTPUT"

Step 7 -
 
  1. Now generate code by click on generate code icon >> after click the pop-up window will open
  2. In pop-up window click on Project
  3. Give the project name
  4. Provide project folder location
  5. Select the "MDK-ARM V5" IDE from drop down list. It is nothing but the Keil uVision V5 IDE
Step 8 -
 

  1. Now click on Code Generator
  2. Select "Copy only the necessary library files". This will generate the code with only necessary library
  3. Click on OK, finally code is generated.
Step 9 -
 
When you click on Open project it will automatically open the project in Keil uVision V5. 
 
 
Step 10 -
 
As we have generated code using STM32CubeMX software, Now the Keil uVision tool opens our generated code with the same project name with necessary library and codes that are configured for the pins we selected. Now we just need to include the logic to perform some action at the output LED (pin PC-13). So select our main.c program to include some codes.
 

Step 11 -
 
Now add the code in the while(1) loop, the code for LED blinking is given as below:
 

Step 12 -

  1. After finish editing the code, click the Options for Target icon
  2. Options for Target pop-up window will open, in that window click on Debug
  3. Under the Debug tab select ST-LINK Debugger
  4. Then click on settings
Step 13 -
 

  1. Click on Flash Download
  2. Tick the Reset and Run check box
  3. Click Ok
Step 14 -
 
Now click on Rebuild icon to rebuild all target files.
 

Step 15 -
 
Now you can plug in the ST-LINK V2 or FTDI TTL Convertor to computer with the given circuit connections and click on the DOWNLOAD icon or press F8 to flash the STM32F103C8 with the code you have generated and edited.
 
 

Output


 

6 comments: