Library 04- USART for STM32F4

USARTA lot of times when you work on some project, you want to display data on computer. This can be done with USART peripheral on MCU. With USART you can connect more than just computer, you can connect with GSM modules, GPRS, bluetooth and so much more. Our discovery board supports up to 8 USART channels. In this tutorial we will use USART1 to show principle how to use USART in our project. But first, we have to initialize our pins and peripheral.

Pins pack 1 Pins pack 2 Pins pack 3
U(S)ARTx TX RX TX RX TX RX APB
USART1 PA9 PA10 PB6 PB7 2
USART2 PA2 PA3 PD5 PD6 1
USART3 PB10 PB11 PC10 PC11 PD8 PD9 1
UART4 PA0 PA1 PC10 PC11 1
UART5 PC12 PD2 1
USART6 PC6 PC7 PG14 PG9 2
UART7 PE8 PE7 PF7 PF6 1
UART8 PE1 PE0 1

USART1 uses pins

  • PA9 for transmitting data
  • PA10 for receiving data

so they must have enabled clock, set for alternating function and set them this alternating function.

Then, when you initialized pins, we have to initialize USART peripheral too

Because our USART uses interrupt for incoming data, we have to initialize NVIC (Nested Vector Interrupt Controller). Everytime when data will come, our main program will stop executing and our interrupt routine will jump in.

Library

Features

  • Operate with up to 8 U(S)ARTs in UART mode
  • Selectable pins for USART
  • Receive interrupt handler

Dependencies

  • CMSIS
    • STM32F4xx
    • STM32F4xx RCC
    • STM32F4xx GPIO
    • STM32F4xx USART
  • TM
    • defines.h
    • attributes.h
    • TM GPIO

Functions and enumerations

Hardware

Your computer probably has not RS232 connector, but it has USB for sure. You need USB to serial converter. I prefer FTDI which is very stable and support 3V3 levels. You can buy converter on ebay for about 5$. Connect converter’s TX with board’s RX and converter’s RX with board’s TX. To display data, you need some terminal, I use one from Bray. Open program and set:

  • Select your COM port
  • baudrate to 9600
  • Data bits to 8
  • Parity to none
  • Stop bits to 1

Example

Project available on Github, download library below.

tilz0R

Owner of this site. Application engineer, currently employed by STMicroelectronics. Exploring latest technologies and owner of different libraries posted on Github.

You may also like...