Previous Tutorial | Tutorial 11 | Next Tutorial | |||||
STM32 Serial Port Interfacing With PC | |||||||
STM32 Course Home Page ???? |
In this LAB, we’ll be doing PC interfacing via the serial port using the USB-TTL converter and UART module in the STM32F103C8 microcontroller (Blue Pill Board). We’ll send and receive asynchronous UART data from and to the PC. So, Let’s get started!
[toc]
Required Components For LABs
All the example code/LABs/projects in the course are going to be done using those boards below.
- Nucleo32-L432KC (ARM Cortex-M4 @ 80MHz) or (eBay)
- Blue Pill STM32-F103 (ARM Cortex-M3 @ 72MHz) or (eBay)
- ST-Link v2 Debugger or (eBay)
QTY | Component Name | ???? Amazon.com | ???? eBay.com |
2 | BreadBoard | Amazon | eBay |
1 | LEDs Kit | Amazon Amazon | eBay |
1 | Resistors Kit | Amazon Amazon | eBay |
1 | Capacitors Kit | Amazon Amazon | eBay & eBay |
2 | Jumper Wires Pack | Amazon Amazon | eBay & eBay |
1 | 9v Battery or DC Power Supply | Amazon Amazon Amazon | eBay |
1 | Micro USB Cable | Amazon | eBay |
1 | Push Buttons | Amazon Amazon | eBay |
1 | USB-TTL Converter or FTDI Chip | Amazon Amazon | eBay eBay |
★ Check The Full Course Complete Kit List
Some Extremely Useful Test Equipment For Troubleshooting:
- My Digital Storage Oscilloscope (DSO): Siglent SDS1104 (on Amazon.com) (on eBay)
- FeelTech DDS Function Generator: KKMoon FY6900 (on Amazon.com) (on eBay)
- Logic Analyzer (on Amazon.com) (on eBay)
Affiliate Disclosure: When you click on links in this section and make a purchase, this can result in this site earning a commission. Affiliate programs and affiliations include, but are not limited to, the eBay Partner Network (EPN) and Amazon.com.
Preface & LAB Objectives
LAB Number | 6 |
LAB Title | Serial Port Read/Write With Blue Pill + USB-TTL Converter |
- Configure GPIO input pin (for push-button) & output pins (for LEDs)
- Configure UART in asynchronous mode @ 9600 bps + Enable RX interrupts
- Read the button state and send it to the PC via serial port
- Read the received characters from the PC and decide which led is to be toggled
STM32 Serial Port Interfacing LAB
Preface
The Blue Pill development board lacks an onboard ST-Link programmer/debugger, unlike Nucleo boards. That’s why we use the external USB ST-Link clone. And also it worth mentioning that the USB port on the blue pill board is connected to the STM32F103C8 hardware USB peripheral. Therefore, it can actually be used for debugging but you’ll develop a USB application for it and it’s a topic for a future tutorial.
However, the UART peripherals in the microcontroller can be used to send serial data to the PC serial COM port and display it on a terminal using a USB-TTL converter board. Hence, you’re not restricted to use a specific UART module (UART1, UART2, or UART3).
Note | |||
The STM32F103C8 microcontrollers’ pins are not all 5v tolerant. Hence, you must be careful when receiving input signals from the USB-TTL converter. You can send a 3.3v signal from the MCU TX pin to the USB-TTL RX pin and still get the data identified absolutely fine. However, it won’t work the other way around without shifting the signal’s level. The TX from the USB-TTL can over-drive the MCU’s RX input pin. By checking the diagram below, you’ll notice that the pins for UART1 & UART3 are 5v tolerant while UART2 is not. |
LAB Connection Diagram
LAB Project Code (Step-By-Step)
Step1: Open CubeMX & Create New Project
Step2: Choose The Target MCU & Double-Click Its Name
Step3: Enable USART1 Module (Asynchronous Mode)
Step4: Choose The Desired Settings For UART (Baud Rate, Stop Bits, Parity, etc..)
- Set the baud rate to 9600 bps
- Enable UART global interrupts in NVIC tab
Step5: Configure The Required GPIO Pins For This Project
- PB12, PB13: Output Pins (For LEDs)
- PB14: Input Pin (For The Push Button)
Step6: Goto The RCC Options Tab & Enable External Crystal
Step7: Go To The Clock Configuration & Set The System Clock To 72MHz
Step8: Generate The Initialization Code & Open The Project In CubeIDE
Step9: Write The Application Layer Code For This LAB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
#include "main.h" UART_HandleTypeDef huart1; uint8_t RX1_Char = 0x00; void SystemClock_Config(void); static void MX_GPIO_Init(void); static void MX_USART1_UART_Init(void); //---------[ UART Data Reception Completion CallBackFunc. ]--------- void HAL_USART_RxCpltCallback(UART_HandleTypeDef *huart) { HAL_UART_Receive_IT(&huart1, &RX1_Char, 1); } int main(void) { uint8_t MSG1[] = "Button State: Released\r\n"; uint8_t MSG2[] = "Button State: Pressed\r\n"; HAL_Init(); SystemClock_Config(); MX_GPIO_Init(); MX_USART1_UART_Init(); HAL_UART_Receive_IT(&huart1, &RX1_Char, 1); while (1) { //--------[ Read The Button State & Send It Via UART ]--------- if(HAL_GPIO_ReadPin (GPIOB, GPIO_PIN_14)) { HAL_UART_Transmit(&huart1, MSG2, sizeof(MSG2), 100); } else { HAL_UART_Transmit(&huart1, MSG1, sizeof(MSG1), 100); } //--------[ Read The Received Character & Toggle LEDs Accordingly ]-------- if(RX1_Char == '1') { HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_12); HAL_UART_Receive_IT(&huart1, &RX1_Char, 1); RX1_Char = 0x00; } if(RX1_Char == '2') { HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_13); HAL_UART_Receive_IT(&huart1, &RX1_Char, 1); RX1_Char = 0x00; } HAL_Delay(100); } } |
Step10: Build & Debug To Flash The Code
Step11: Go To The Device Manager & Check The USB-TTL COM Port Num.
Step12: Open The Terminal From CubeIDE or Any Other Terminal
Window > Show View > Console
In Console: click on the NEW icon on its menu bar > Command Shell console > Connection type: Serial port > set Baud Rate & Connection Name > Encoding: UTF-8 > And Click OK!
Alternatively, You Can Use Any Terminal On Your PC (Like Tera Term, Arduino Serial Monitor, etc..)
Don’t Forget To Support The Content By SHARING It On Socials Or Via Patreon!
Previous Tutorial | Tutorial 11 | Next Tutorial |
Hi,
If I type random characters, callback (reading character) eventually doesn’t work anymore. What is the main reason ?
which terminal did you use? i cant do this. pls help