Introduction: STM32F103 USART Transmission (using Keil and STMCubeMX)

In this tutorial, I will demonstrate how to Transmit data using USART2 of STM32F103 Nucleo board.

This tutorial is divided into 3 Steps:

1) Creating Project using STM32CubeMX

2) Programming in Keil

3) Visualizing the Output

Step 1: Creating Project Using STM32CubeMX

Open STM32CubeMX. Click on ‘New Project’. From Series Select ‘STMF1’, from ‘lines’ select STM32F103. From MCUs list, select ‘STM32F103RBTx’. Click “OK”.

{See Image: [1]}

Now, Set ‘Mode’ to ‘Asynchronous’ under the ‘USART2’ under the ‘Peripherals’ as shown in figure below:

{See Image: [2]}

Now, Click on ‘Generate Code’ button. Fill in Project Name, Where to save Project and ‘Toolchain / IDE’ select ‘MDK-ARM V5’. Then Click “OK”. And Then Click “Open Project”.

Step 2: Programming in KEIL

Once Project is Open in Keil, Open ‘main.c’ located under ‘Application/User’ Folder, located under ‘ ’ under ‘Project: ’. Scroll down to find ‘#include "stm32f1xx_hal.h". Just After this add following line of code:

{See Image: [3]}

Now, scroll down to while(1) and just before that add following lines of code:

char buff[50] = "Hello World By ARK";


And thus you Keil window will look like:

{See Image: [4]}

Click on ‘Build’ Button (or Press ‘F7’). The Connect your STM32 Board and Click on ‘LOAD’ button (or Press ‘F8’)

Step 3: Visualizing the Output

Now, Press the ‘Black’ Rest button on you ‘STM32’ Board.

The STM32 Board will now would have transmitted line: “Hello World by ARK”.

Now, as we know that STM32 board will transmit this serially to our PC via the port its connected to. So, in order to view this transmission we need to have some kind of software that will read this serial input from the port. We will also need this software in future. For this purpose we are going to use “Docklight”.

Open the extracted folder (while you should have downloaded in our getting started tutorial. If you have not you can google for “docklight” and download the setup file.) Now, go into folder “docklight” and double click on “setup.exe”. If asked for permission, say “YES”. A window like this will appear.

{See Image: [5]}

Click “Next”.

{See Image: [6]}

Click “Next”.

{See Image:[7]}

Select where to install and click “Next”.

{See Image:[8]}

Click “NEXT”.

{See Image:[9]}

Click “NEXT”.

{See Image:[10]}

Click “INSTALL”. And it will start installing the Docklight:

{See Image:[11]}

After some time:

{See Image:[12]}

Click Finish.

If not opened, open Docklight.

A window like this will appear:

{See Image:[13]}

Click “OK”. Then a window like this will appear:

{See Image:[14]}

Click “Continue”.

Now, you must have this window:

{See Image:[15]}

Double Click on “COM1” in the top right corner. A window like this will appear:

{See Image:[17]}

Now, while pressing and holding the Windows key, press the R key ("Run")

A window like this will appear:

{See Image:[18]}

Type “devmgmt.msc” and press “OK”. “Device Manager will appear” like shown below:

{See Image:[19]}

Now, open “Ports (COM&LPT)” and read the “COMNo. “ in front of “STMicroelectronics STlink Virtual COM Port”. In my case, it’s “COM10”. Now, Switch back to “docklight” and select “COM10 …”

{See Image:[20]}

Select Baud rate: 115200.

{See Image:[21]}

And click “OK”. Now, you must see a window like this:

{See Image:[22]}

And now click on “Start Communication” button. Or Press “F6”.Click the “Black Reset button” on your STM Board. And you will see “Hello World by ARK” which was transmitted by your “STMBoard”.