{"id":894,"date":"2022-04-22T12:39:32","date_gmt":"2022-04-22T12:39:32","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=894"},"modified":"2022-04-22T12:39:35","modified_gmt":"2022-04-22T12:39:35","slug":"getting-started-with-stm32l053-uart-transmit-mode","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=894","title":{"rendered":"Getting started with STM32L053: UART Transmit mode"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.st.com\/content\/dam\/arm-cortex-m\/M0-Corenew.jpg\" alt=\"\" \/><\/figure><\/div>\n\n\n\n<p>In te previous guide (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=865\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=865\" target=\"_blank\">here<\/a>), we took a look how to configure the GPIO as input. In this guide, we shall cover how to use UART in STM32L051 to send a single character.<\/p>\n\n\n\n<p>In this cover, we will cover the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>UART.<\/li><li>Configuring pins to work in UART mode.<\/li><li>Configure the UART.<\/li><li>Send character.<\/li><li>Code.<\/li><li>Results.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. UART:<\/h2>\n\n\n\n<p>To transfer data, computers adopt two method: serial and parallel. In serial communication data is sent a bit at a time (1 line)while in parallel communication 8 or more lines are often used to transfer data. The UART is a form of serial communication. When distance is short, digital signal can be transferred as it is on a single wire and require no modulation. This is how PC keyboard transfer data between the keyboard and motherboard. For long distances like telephone lines, serial data communication requires a modem to modulate.We will not go into the details of this in this lesson, this shall be left for subsequent lessons.&nbsp;<\/p>\n\n\n\n<p><strong>Synchronous vs. Asynchronous<\/strong><\/p>\n\n\n\n<p>Serial communication operate in two ways : synchronous and asynchronous. In the synchronous mode, a block of data is sent at a time while in the asynchronous mode, a single byte of data is sent at a time. It is possible to write code to provide both the synchronous and asynchronous serial communication functions however this could turn out tedious and over complicated therefore, special chips are manufactured to perform these functions. When these chips are added to a microcontroller, they become known as Serial Communication Interface (SCI). The chip that provides the the asynchronous communication (<strong>UART<\/strong>) is the main theme of this lesson. The chip for synchronous communication is known as the&nbsp;<strong>USART<\/strong>&nbsp;and shall left for another lesson.&nbsp;<\/p>\n\n\n\n<p><strong>Asynchronous Communication Data Framing<\/strong><\/p>\n\n\n\n<p>The data is received in zeros and ones format, in order to make sense of the data, the sender and the receiver must agree on a set of rules i.e. a protocol, on how data is packed, how many bits constitute a character and when data begins and ends.<\/p>\n\n\n\n<p><strong>The Protocol<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>Start and Stop bits<\/strong><\/li><\/ol>\n\n\n\n<p>Asynchronous serial communication is widely used in character transfer. Each character is packed between start and stop bits. This is known as the framing.<\/p>\n\n\n\n<p><strong>start bit<\/strong>&nbsp;: &nbsp;Always&nbsp;<strong>1<\/strong>&nbsp;bit, value is always&nbsp;<strong>0<\/strong><\/p>\n\n\n\n<p><strong>stop bit<\/strong>: &nbsp;<strong>&nbsp;1<\/strong>&nbsp;or&nbsp;<strong>2<\/strong>&nbsp;bits, value is always<strong>&nbsp;1<\/strong><\/p>\n\n\n\n<p>Lets take an example. Lets say we are transferring ASCII character \u2018A\u2019&nbsp;<\/p>\n\n\n\n<p>its binary code is 0100 0001 and 0x41 in hex.Furthermore, it is framed between a start bit and 2 stop bits. This is what its frame will look like.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/bohobiom.com\/wp-content\/uploads\/2020\/01\/1frame-750x234-1.png\" alt=\"\" \/><\/figure>\n\n\n\n<p>&nbsp;2.<strong>&nbsp;Parity bit<\/strong><\/p>\n\n\n\n<p>Some systems require parity bit in order to maintain data integrity.The parity bit of a character byte is included in the data frame after the stop bit(s).<\/p>\n\n\n\n<p>&nbsp;3.<strong>Rate of data transfer : Baud rate\/bps<\/strong><\/p>\n\n\n\n<p>We shall explain this concept with an example. Lets say we are asked to calculate the number of bits used &nbsp;and time taken in transferring 50 pages of text, each with 80\u00d725 characters. Assuming 8 bits per character and 1 stop bit<\/p>\n\n\n\n<p><em>solution<\/em><\/p>\n\n\n\n<p>For each character a total of 10 bits is used ( 1 start bit, 8 bits character, 1 stop bit).<\/p>\n\n\n\n<p>Therefore total number of bits &nbsp;=&nbsp;80 x 25 x10 = 20,000 bits per page<\/p>\n\n\n\n<p>50 pages &nbsp;implies,&nbsp;50\u00d720,000 &nbsp;= &nbsp;1,000,000 bits.<\/p>\n\n\n\n<p>Therefore it will take 1 million bits to transfer &nbsp;this information.<\/p>\n\n\n\n<p>The time it takes to transfer the entire data using<\/p>\n\n\n\n<p><strong>a<\/strong>. 9600 baudrate implies, &nbsp;1,000,000 \/ 9600 = 204 seconds<\/p>\n\n\n\n<p><strong>b<\/strong>. 57,600 baudrate implies&nbsp;1,000,000 \/57,600 = 17 seconds<\/p>\n\n\n\n<p>Baudrate simply means the transfer of bits per second. There are various standardized baudrate we can choose from when we program our serial communication devices. \u00a0The key thing is, both communication devices must have the same baudrate. We shall see this later on in our example code.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"> 2. Configuring the pins in UART Mode:<\/h2>\n\n\n\n<p>In order to set the pin in UART, we need to figure out which pins of ST-Link V2 on Nucleo-L053 is connected.<\/p>\n\n\n\n<p>From the user manual of Nucleo-64 UM1724, we can find that PA2 and PA3 are connected to UART2 of STM32L053.<\/p>\n\n\n\n<p>Hence we start off by enabling clock access to GPIOA as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\/*Enable clock access to GPIOC*\/\n\tRCC-&gt;IOPENR |= RCC_IOPENR_GPIOAEN;<\/pre><\/div>\n\n\n\n<p>Since PA2 is the TX pin, we need to set it to alternative mode:<\/p>\n\n\n\n<p>Since the default mode is analog mode, we need to set bit 5 and reset bit4<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"837\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-22-at-3.02.58-PM-1024x837.png\" alt=\"\" class=\"wp-image-895\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-22-at-3.02.58-PM-1024x837.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-22-at-3.02.58-PM-300x245.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-22-at-3.02.58-PM-768x628.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-22-at-3.02.58-PM-1536x1256.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-22-at-3.02.58-PM-2048x1674.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-22-at-3.02.58-PM-1150x940.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-22-at-3.02.58-PM-750x613.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-22-at-3.02.58-PM-400x327.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-22-at-3.02.58-PM-250x204.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\tGPIOA-&gt;MODER|=GPIO_MODER_MODE2_1;\n\tGPIOA-&gt;MODER&amp;=~GPIO_MODER_MODE2_0;<\/pre><\/div>\n\n\n\n<p>Then we need to find which alternate function is related to USART2:<\/p>\n\n\n\n<p>We can find this information from datasheet of STM32L053 table 16.<\/p>\n\n\n\n<p>The Required AF is AF04, hence we can create macro for that:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#define AF04 0x04<\/pre><\/div>\n\n\n\n<p>hence we can configure the PA2 function in AFR[0]<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"668\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-22-at-3.13.52-PM-1024x668.png\" alt=\"\" class=\"wp-image-896\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-22-at-3.13.52-PM-1024x668.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-22-at-3.13.52-PM-300x196.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-22-at-3.13.52-PM-768x501.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-22-at-3.13.52-PM-1536x1001.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-22-at-3.13.52-PM-2048x1335.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-22-at-3.13.52-PM-1150x750.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-22-at-3.13.52-PM-750x489.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-22-at-3.13.52-PM-400x261.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-22-at-3.13.52-PM-250x163.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">GPIOA-&gt;AFR[0]|=(AF04&lt;&lt;8);<\/pre><\/div>\n\n\n\n<p>Thats all for the GPIO.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Configure the UART:<\/h2>\n\n\n\n<p>First we enable clock access to UART2:<\/p>\n\n\n\n<p>UART2 is connected to APB1:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"908\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-22-at-2.56.53-PM-908x1024.png\" alt=\"\" class=\"wp-image-897\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-22-at-2.56.53-PM-908x1024.png 908w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-22-at-2.56.53-PM-266x300.png 266w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-22-at-2.56.53-PM-768x866.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-22-at-2.56.53-PM-1362x1536.png 1362w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-22-at-2.56.53-PM-1150x1297.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-22-at-2.56.53-PM-750x846.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-22-at-2.56.53-PM-400x451.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-22-at-2.56.53-PM-250x282.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-22-at-2.56.53-PM.png 1646w\" sizes=\"(max-width: 908px) 100vw, 908px\" \/><\/figure>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\tRCC-&gt;APB1ENR |= RCC_APB1ENR_USART2EN;<\/pre><\/div>\n\n\n\n<p>Then we need to create a function that allows us to configure the baudrate as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\"> \nstatic uint16_t compute_uart_bd(uint32_t PeriphClk, uint32_t BaudRate)\n{\n\treturn ((PeriphClk  +  (BaudRate\/2U))\/BaudRate);\n}\n<\/pre><\/div>\n\n\n\n<p>Hence we can set the baudrate of UART2 as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\tUSART2-&gt;BRR=compute_uart_bd(APB1_CLK,UART_BAUDRATE);<\/pre><\/div>\n\n\n\n<p>By default, STM32L053 is running at 2.09MHz, hence we can create symbolic name as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#define SYS_FREQ\t\t2097000\n#define APB1_CLK\t\tSYS_FREQ\n\n#define UART_BAUDRATE\t9600<\/pre><\/div>\n\n\n\n<p>Then we enable TX and Direction as following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"335\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-22-at-3.26.02-PM-1024x335.png\" alt=\"\" class=\"wp-image-898\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-22-at-3.26.02-PM-1024x335.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-22-at-3.26.02-PM-300x98.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-22-at-3.26.02-PM-768x251.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-22-at-3.26.02-PM-1536x502.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-22-at-3.26.02-PM-2048x669.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-22-at-3.26.02-PM-1150x376.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-22-at-3.26.02-PM-750x245.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-22-at-3.26.02-PM-400x131.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-22-at-3.26.02-PM-250x82.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\t\/*Configure transfer direction*\/\n    USART2-&gt;CR1 = USART_CR1_TE;\n\n\t\/*Enable uart module*\/\n    USART2-&gt;CR1 |= USART_CR1_UE;<\/pre><\/div>\n\n\n\n<p>That&#8217;s all for configuring the uart.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Transmit single character:<\/h2>\n\n\n\n<p>In order to send a single character, we need to wait until the bure is empty and transmit the data as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">void uart2_write(int ch)\n{\n\t\/*Make sure transmit data register is empty*\/\n\twhile(!(USART2-&gt;ISR &amp; USART_ISR_TXE)){}\n\n\t\/*Write to the transmit data register*\/\n\tUSART2-&gt;TDR =  (ch &amp; 0xFF);\n}<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5. Code:<\/h2>\n\n\n\n<p>You may download the code from here:<\/p>\n\n\n\n<div class=\"wp-block-file\"><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/UART.zip\">UART<\/a><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/UART.zip\" class=\"wp-block-file__button\" download>Download<\/a><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">6. Results:<\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"725\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-22-at-3.37.28-PM-1024x725.png\" alt=\"\" class=\"wp-image-899\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-22-at-3.37.28-PM-1024x725.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-22-at-3.37.28-PM-300x213.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-22-at-3.37.28-PM-768x544.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-22-at-3.37.28-PM-1150x815.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-22-at-3.37.28-PM-750x531.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-22-at-3.37.28-PM-400x283.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-22-at-3.37.28-PM-250x177.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/04\/Screen-Shot-2022-04-22-at-3.37.28-PM.png 1166w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Happy coding \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In te previous guide (here), we took a look how to configure the GPIO as input. In this guide, we shall cover how to use UART in STM32L051 to send a single character. In this cover, we will cover the following: UART. Configuring pins to work in UART mode. Configure the UART. Send character. Code. [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,11,12],"tags":[],"class_list":["post-894","post","type-post","status-publish","format-standard","hentry","category-embedded-systems","category-peripheral-drivers","category-stm32"],"_links":{"self":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/894"}],"collection":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=894"}],"version-history":[{"count":1,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/894\/revisions"}],"predecessor-version":[{"id":901,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/894\/revisions\/901"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=894"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=894"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=894"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}