{"id":329,"date":"2021-08-16T04:49:09","date_gmt":"2021-08-16T04:49:09","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=329"},"modified":"2021-08-16T04:49:12","modified_gmt":"2021-08-16T04:49:12","slug":"working-with-stm32-and-uart-part-1-sending-a-single-character","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=329","title":{"rendered":"Working with STM32 and UART part 1: Sending a single Character"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"441\" height=\"259\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/UART-BUS-between-two-devices.jpg\" alt=\"\" class=\"wp-image-330\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/UART-BUS-between-two-devices.jpg 441w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/UART-BUS-between-two-devices-300x176.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/UART-BUS-between-two-devices-400x235.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/UART-BUS-between-two-devices-250x147.jpg 250w\" sizes=\"(max-width: 441px) 100vw, 441px\" \/><figcaption>UART<\/figcaption><\/figure><\/div>\n\n\n\n<p>In this guide, we shall discuss how to use the UART module on STM32F4xx specifically, STM32F411RE Nucleo.<\/p>\n\n\n\n<p>In this guide we will cover the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>What is UART<\/li><li>How to configure the UART on STM32F4xx<\/li><li>Code<\/li><li>Demo<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">1. What is 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<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/bohobiom.com\/wp-content\/uploads\/2020\/01\/1frame-750x234-1.png\" alt=\"\" \/><\/figure><\/div>\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. &nbsp;The 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<h2 class=\"wp-block-heading\">2. How to Configure UART on STM32F4xx<\/h2>\n\n\n\n<ol class=\"wp-block-list\"><li>Enable Clock access to UART<\/li><\/ol>\n\n\n\n<p>First, we need to check which bus is the UART2 connected to<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"775\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-6.56.10-AM-775x1024.png\" alt=\"\" class=\"wp-image-332\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-6.56.10-AM-775x1024.png 775w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-6.56.10-AM-227x300.png 227w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-6.56.10-AM-768x1015.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-6.56.10-AM-1162x1536.png 1162w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-6.56.10-AM-1150x1520.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-6.56.10-AM-750x991.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-6.56.10-AM-400x529.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-6.56.10-AM-250x330.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-6.56.10-AM.png 1528w\" sizes=\"(max-width: 775px) 100vw, 775px\" \/><figcaption>Block diagram<\/figcaption><\/figure><\/div>\n\n\n\n<p>As shown in block diagram, UART2 is connected to APB1, hence we can enable clock access 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;}\">RCC-&gt;APB1ENR|=RCC_APB1ENR_USART2EN;<\/pre><\/div>\n\n\n\n<p>2. Locating which pins are for USART and configure them<\/p>\n\n\n\n<p>According to pinout, USART2 pins are connected to PA2 and PA3<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"811\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-7.03.00-AM-1024x811.png\" alt=\"\" class=\"wp-image-333\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-7.03.00-AM-1024x811.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-7.03.00-AM-300x238.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-7.03.00-AM-768x608.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-7.03.00-AM-1536x1216.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-7.03.00-AM-2048x1622.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-7.03.00-AM-1150x911.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-7.03.00-AM-750x594.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-7.03.00-AM-400x317.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-7.03.00-AM-250x198.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption>USART Pinout<\/figcaption><\/figure><\/div>\n\n\n\n<p>Hence, we need to enable 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;}\">RCC-&gt;AHB1ENR|=RCC_AHB1ENR_GPIOAEN;<\/pre><\/div>\n\n\n\n<p>Since  the UART is a special function, we need to set PA2 as Alternate Function as following<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"642\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-7.08.30-AM-1024x642.png\" alt=\"\" class=\"wp-image-334\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-7.08.30-AM-1024x642.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-7.08.30-AM-300x188.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-7.08.30-AM-768x481.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-7.08.30-AM-1536x962.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-7.08.30-AM-2048x1283.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-7.08.30-AM-1150x721.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-7.08.30-AM-750x470.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-7.08.30-AM-400x251.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-7.08.30-AM-250x157.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption>Mode register<\/figcaption><\/figure><\/div>\n\n\n\n<p>Hence we need to set bit5 to 1 and reset bit4 to enable alternate function for PA2<\/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;}\">GPIOA-&gt;MODER|=(1&lt;&lt;5);\/\/set bit5\nGPIOA-&gt;MODER&amp;=~(1&lt;&lt;4);\/\/reset bit4<\/pre><\/div>\n\n\n\n<p>Since we set the pin as alternate function, we need to inform the cpu which type of function to be, in this case we need to know which alternate function is connected to USART2<\/p>\n\n\n\n<p>This information can be found I\/O pin multiplexer and mapping section of GPIO in reference manual<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"575\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-7.14.48-AM-1024x575.png\" alt=\"\" class=\"wp-image-335\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-7.14.48-AM-1024x575.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-7.14.48-AM-300x168.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-7.14.48-AM-768x431.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-7.14.48-AM-1536x863.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-7.14.48-AM-2048x1150.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-7.14.48-AM-1150x647.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-7.14.48-AM-750x422.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-7.14.48-AM-400x225.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-7.14.48-AM-250x141.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption>Alternate function map<\/figcaption><\/figure><\/div>\n\n\n\n<p>hence, we need to set PA2 to be ALT7 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;}\">GPIOA-&gt;AFR[0]|=0x700;<\/pre><\/div>\n\n\n\n<p>Thats all for GPIO configuration, now we need to configure the USART module<\/p>\n\n\n\n<p>3. Configure UART<\/p>\n\n\n\n<p>First we need to configure the baudrate which can be calculated as following<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"403\" height=\"349\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/uartBRRR.jpg\" alt=\"\" class=\"wp-image-337\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/uartBRRR.jpg 403w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/uartBRRR-300x260.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/uartBRRR-400x346.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/uartBRRR-250x217.jpg 250w\" sizes=\"(max-width: 403px) 100vw, 403px\" \/><figcaption>baud rate calculation example <\/figcaption><\/figure><\/div>\n\n\n\n<p>Since out MCU is running at 16MHz and we need baud rate of 9600, we need to set the BRR register 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;}\">USART2-&gt;BRR=0x0681;<\/pre><\/div>\n\n\n\n<p>From CR1 we need to set the UART to transmit and enable it<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"276\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-7.30.48-AM-1024x276.png\" alt=\"\" class=\"wp-image-338\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-7.30.48-AM-1024x276.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-7.30.48-AM-300x81.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-7.30.48-AM-768x207.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-7.30.48-AM-1536x414.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-7.30.48-AM-2048x552.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-7.30.48-AM-1150x310.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-7.30.48-AM-750x202.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-7.30.48-AM-400x108.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-7.30.48-AM-250x67.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption>Control register 1 of UART<\/figcaption><\/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;}\">USART2-&gt;CR1  |=0x2008; \/\/ enable tx and uart<\/pre><\/div>\n\n\n\n<p>4. sending a single character<\/p>\n\n\n\n<p>In order to send a single character, we need the following steps:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>wait to TX buffer to be empty<\/li><li>Send the data<\/li><\/ul>\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;}\">while(!(USART2-&gt;SR&amp;0x0080)){\n}\nUSART2-&gt;DR=(ch);<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">3. Code:<\/h2>\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;}\">#include &quot;stm32f4xx.h&quot;                  \/\/ Device header\n\nvoid USART_write(int ch);\nvoid USART2_Init(void);\nvoid delay(int delayms);\nint main(){\n\nUSART2_Init();\nwhile(1){\nUSART_write('!');\/\/print !\t\nUSART_write('\\r');USART_write('\\n'); \/\/new line and return cursor to start of the line\ndelay(10);\n}\n}\n\nvoid USART2_Init(void){\nRCC-&gt;APB1ENR|=RCC_APB1ENR_USART2EN;\nRCC-&gt;AHB1ENR|=RCC_AHB1ENR_GPIOAEN;\nGPIOA-&gt;AFR[0]=0x0700;\nGPIOA-&gt;MODER|=(1&lt;&lt;5);\/\/set bit5\nGPIOA-&gt;MODER&amp;=~(1&lt;&lt;4);\/\/reset bit4\nUSART2-&gt;BRR  = 0x0681;    \/\/9600 @16MHz\nUSART2-&gt;CR1  |=0x2008; \/\/ enable tx\n\n}\n\nvoid USART_write(int ch){\nwhile(!(USART2-&gt;SR&amp;0x0080)){\n}\nUSART2-&gt;DR=(ch);\n\t\n}\n\nvoid delay(int delayms){\nint i;\n\tfor(; delayms&gt;0;delayms--){\n\tfor(i=0;i&lt;3192;i++);\n\t}\n}<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Result<\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"656\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-7.47.03-AM-1024x656.png\" alt=\"\" class=\"wp-image-339\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-7.47.03-AM-1024x656.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-7.47.03-AM-300x192.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-7.47.03-AM-768x492.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-7.47.03-AM-1150x737.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-7.47.03-AM-750x481.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-7.47.03-AM-400x256.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-7.47.03-AM-250x160.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-16-at-7.47.03-AM.png 1370w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, we shall discuss how to use the UART module on STM32F4xx specifically, STM32F411RE Nucleo. In this guide we will cover the following: What is UART How to configure the UART on STM32F4xx Code Demo 1. What is UART To transfer data, computers adopt two method: serial and parallel. In serial communication data [&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-329","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\/329"}],"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=329"}],"version-history":[{"count":3,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/329\/revisions"}],"predecessor-version":[{"id":340,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/329\/revisions\/340"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=329"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=329"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=329"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}