Skip to main content

STM32F103C8T6, UART

UART is acronym for Universal Asynchronous Receiver – Transmitter.

STM32F103C8T6 UART is the TTL serial, the TTL limit signal between 0V - Vcc. If MCU working on 3.3V, TTL limit will be 0V - 3.3V

The COM port (RS232) on PC working on -13V to +13V level, so you can not directly connect your STM32F103C8T6 UART to the COM port. You must use the RS232 to TTL device to connect your MCU to the PC. The following image is a RS232 to TTL device

Or you can use a USB to TTL serial (FT232RL) like the following

In this article we use the blue pill board.

We also need a ST-LINK V2 programmer

There are 3 UART ports available on STM32F103C8T6

Tx is the transmitter, Rx is the receiver. Tx must connect to Rx and vice versa.

Here is the list of materials that we need

Hardware:
FT232RL, Blue pill board, and stlink v2 mini programmer.

Software:
Atollic TrueSTUDIO (https://atollic.com/truestudio).
STM32CubeMX (https://www.st.com/en/development-tools/stm32cubemx.html).
STM32 ST-LINK Utility (https://www.st.com/en/development-tools/stsw-link004.html).
Termite (https://www.compuphase.com/software_termite.htm).

Steps

1. Open STM32CubeMX, New Project, double click on STM32F103C8

2. In STM32CubeMX, Pinout tab, open Configuration > Peripherals, at SYS > Debug select Serial Wire. USART1 select Asynchronous. Click on PC13 pin, choose GPIO_Output on the context menu.

3. On Configuration tab, click at NVIC and then enable USART1 global interrupt

4. On the Project > Settings menu, fill your Project Name.  On Toolchain / IDE select TrueSTUDIO, un-check the Generate Under Root.

5. Select Project > Generate Code to generate Atollic TrueSTUDIO project.

6. Open the generated project with Atollic, go to Project > Build Settings... select convert output to HEX file like the following picture.

7. To send data we use the HAL_UART_Transmit function

uint8_t data[] = "HELLO WORLD\n";
HAL_UART_Transmit(&huart1, data, sizeof(data), 100);

8. To receive data we use HAL_UART_Receive_IT and HAL_UART_RxCpltCallback function.

In this article we will make a program to control the LED on PC13 pin. If the PC13 LOW (0V) the LED will be ON, if the PC13 HIGHT (3.3v) the LED will be OFF.

The command to control LED will be received from the PC through the FT232RL. A command ended with the <enter> (0x13) character. The MCU will send back to serial port everything its received and turn on/off the LED if the commands is "on"/"off".

Full source-code can be downloaded from here https://drive.google.com/open?id=1dMrAu1iAvq_Ebf9YyTqQxcW3g7F_DxJE

9. Compile the project you can get your .hex file, then you need to connect hardware like following

[Blue pill] GND   <===> [STLINK] GND
[Blue pill] DCLK <===> [STLINK] SWCLK
[Blue pill] DIO     <===> [STLINK] SWDIO
[Blue pill] 3.3V    <===> [STLINK] GND

FT232RL set the jumper to 3.3V level and connect like this

[FT232RL] GND   <===> [Blue pill] GND
[FT232RL] RX      <===> [Blue pill] PA9
[FT232RL] TX      <===> [Blue pill] PA10

10. Plug your STLINK and FT232RL to the PC and install driver if needed. Open STM32 ST-LINK Utility, click Connect button to connect with STM32F103C8T6 board, the select the .hex file to download your firmware.

11. Open termite send command and see the results.

Comments