{"id":1395,"date":"2022-12-11T04:18:07","date_gmt":"2022-12-11T04:18:07","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=1395"},"modified":"2022-12-11T04:18:10","modified_gmt":"2022-12-11T04:18:10","slug":"getting-started-with-stm32f103-uart-receiver-mode","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=1395","title":{"rendered":"Getting Started with STM32F103: UART Receiver mode"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/AdobeStock_316544570-2048x1448.jpeg\" alt=\"\" \/><\/figure>\n\n\n\n<p>In the previous guide (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=1387\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=1387\" target=\"_blank\">here<\/a>), we took a look how to configure the UART to transmit single character. In this guide, we shall use UART to receive a character and echo the received character.<\/p>\n\n\n\n<p>In this guide, we shall cover the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Configure the pin to work in UART mode.<\/li><li>Configure the UART in receiver mode.<\/li><li>Receive a character.<\/li><li>Code.<\/li><li>Demo.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. Configure the pin to work in UART mode:<\/h2>\n\n\n\n<p>From the previous guide, we concluded that PA2 is for TX and PA3 is for RX. Hence, we shall configure PA3.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>For the pin to work in UART RX mode, we need to configure it as input floating as following:<\/p>\n\n\n\n<p><\/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;}\">\/*Set mode to be input*\/\n\tGPIOA-&gt;CRL &amp;=~(GPIO_CRL_MODE3);\n\tGPIOA-&gt;CRL|=GPIO_CRL_CNF3_0;\n\tGPIOA-&gt;CRL&amp;=~GPIO_CRL_CNF3_1;<\/pre><\/div>\n\n\n\n<p>Thats all for the GPIO configuration.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Configure UART in Receiver mode:<\/h2>\n\n\n\n<p>In order to configure the UART to work in receiver mode, we need to enable the receiver bit in CR1 (Control Register 1):<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"278\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-11-at-7.08.53-AM-1024x278.png\" alt=\"\" class=\"wp-image-1396\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-11-at-7.08.53-AM-1024x278.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-11-at-7.08.53-AM-300x81.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-11-at-7.08.53-AM-768x208.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-11-at-7.08.53-AM-1536x417.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-11-at-7.08.53-AM-2048x556.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-11-at-7.08.53-AM-1150x312.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-11-at-7.08.53-AM-750x204.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-11-at-7.08.53-AM-400x109.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-11-at-7.08.53-AM-250x68.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\/\/Enable receiver\n\tUSART2-&gt;CR1 |= USART_CR1_RE;<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Receive a character:<\/h2>\n\n\n\n<p>In order to receive a character, we need to wait for the character to arrive by checking the status register and the bit Read data register not empty and then read the data register:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"278\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-11-at-7.12.38-AM-1024x278.png\" alt=\"\" class=\"wp-image-1397\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-11-at-7.12.38-AM-1024x278.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-11-at-7.12.38-AM-300x81.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-11-at-7.12.38-AM-768x208.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-11-at-7.12.38-AM-1536x417.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-11-at-7.12.38-AM-2048x556.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-11-at-7.12.38-AM-1150x312.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-11-at-7.12.38-AM-750x204.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-11-at-7.12.38-AM-400x109.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/Screenshot-2022-12-11-at-7.12.38-AM-250x68.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;}\">\nchar uart2_read(void)\n{\n\t  \/*Make sure the receive data register is not empty*\/\n\twhile(!(USART2-&gt;SR &amp; USART_SR_RXNE)){}\n\n\t\/*Read data*\/\n\treturn USART2-&gt;DR;\n\n}\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Code:<\/h2>\n\n\n\n<p>The entire code for both Tx and Rx as follows:<\/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;}\">#include &quot;stm32f1xx.h&quot;\n\n\n\n#define Perpher_CLK 8000000\n#define Baudrate\t115200\n\n\n\nstatic uint16_t compute_uart_bd(uint32_t PeriphClk, uint32_t BaudRate)\n{\n\treturn ((PeriphClk + (BaudRate\/2U))\/BaudRate);\n}\n\n\nstatic void uart_set_baudrate(USART_TypeDef *USARTx, uint32_t PeriphClk,  uint32_t BaudRate)\n{\n\tUSARTx-&gt;BRR =  compute_uart_bd(PeriphClk,BaudRate);\n}\n\n\n\n\n\nvoid uart2_write(int ch)\n{\n  \/*Make sure the transmit data register is empty*\/\n\twhile(!(USART2-&gt;SR &amp; USART_SR_TXE)){}\n\n  \/*Write to transmit data register*\/\n\tUSART2-&gt;DR\t=  (ch &amp; 0xFF);\n}\n\n\n\nchar uart2_read(void)\n{\n\t  \/*Make sure the receive data register is not empty*\/\n\twhile(!(USART2-&gt;SR &amp; USART_SR_RXNE)){}\n\n\t\/*Read data*\/\n\treturn USART2-&gt;DR;\n\n}\n\n\n\nint main(void)\n{\n\n\t\/*UART2 Pin configures*\/\n\n\t\/\/enable clock access to GPIOA\n\tRCC-&gt;APB2ENR|=RCC_APB2ENR_IOPAEN;\n\t\/\/Enable clock access to alternate function\n\tRCC-&gt;APB2ENR|=RCC_APB2ENR_AFIOEN;\n\n\t\/*Confgiure PA2 as output maximum speed to 50MHz\n\t * and alternate output push-pull mode*\/\n\tGPIOA-&gt;CRL|=GPIO_CRL_MODE2;\n\n\tGPIOA-&gt;CRL|=GPIO_CRL_CNF2_1;\n\tGPIOA-&gt;CRL&amp;=~GPIO_CRL_CNF2_0;\n\n\n\t\/*Configure PA3 as Input floating*\/\n\n\t\/*Set mode to be input*\/\n\tGPIOA-&gt;CRL &amp;=~(GPIO_CRL_MODE3);\n\tGPIOA-&gt;CRL|=GPIO_CRL_CNF3_0;\n\tGPIOA-&gt;CRL&amp;=~GPIO_CRL_CNF3_1;\n\n\n\n\n\t\/*Don't remap the pins*\/\n\tAFIO-&gt;MAPR&amp;=~AFIO_MAPR_USART2_REMAP;\n\n\n\t\/*USART2 configuration*\/\n\n\t\/\/enable clock access to USART2\n\n\tRCC-&gt;APB1ENR|=RCC_APB1ENR_USART2EN;\n\n\t\/\/Transmit Enable\n\tUSART2-&gt;CR1 |= USART_CR1_TE;\n\n\t\/\/Enable receiver\n\tUSART2-&gt;CR1 |= USART_CR1_RE;\n\n\t\/*Set baudrate *\/\n\tuart_set_baudrate(USART2,Perpher_CLK,Baudrate);\n\t\/\/Enable UART\n\tUSART2-&gt;CR1 |= USART_CR1_UE;\n\n\n\n\twhile(1)\n\t{\n\t\tchar c;\n\t\tc=uart2_read();\n\t\tuart2_write(c);\n\t\tuart2_write('\\r');\n\t\tuart2_write('\\n');\n\n\n\t}\n}\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5. Results:<\/h2>\n\n\n\n<p>Open you favourite serial terminal and set the buadrate to be 115200 and send any character and you should receive the same sent character:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"600\" height=\"434\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/12\/ezgif-5-8176dddb77.gif\" alt=\"\" class=\"wp-image-1398\" \/><\/figure>\n\n\n\n<p>Happy coding \ud83d\ude42<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous guide (here), we took a look how to configure the UART to transmit single character. In this guide, we shall use UART to receive a character and echo the received character. In this guide, we shall cover the following: Configure the pin to work in UART mode. Configure the UART in receiver [&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-1395","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\/1395"}],"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=1395"}],"version-history":[{"count":1,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1395\/revisions"}],"predecessor-version":[{"id":1399,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1395\/revisions\/1399"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1395"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1395"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1395"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}