{"id":347,"date":"2021-08-23T05:53:29","date_gmt":"2021-08-23T05:53:29","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=347"},"modified":"2021-08-23T05:53:33","modified_gmt":"2021-08-23T05:53:33","slug":"working-with-stm32-and-uart-part-3-receive-a-character-in-interrupt-mode","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=347","title":{"rendered":"Working with STM32 and UART part 3: Receive a character in interrupt mode"},"content":{"rendered":"\n<p>In the pervious two guides (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=329\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=329\" target=\"_blank\">part 1<\/a> &amp; <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=341\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=341\" target=\"_blank\">part 2<\/a> ) how to send a single character and string using UART. In this guide, we shall discuss how to receive a character from UART using interrupt handler and echo back the sent character and blink the internal led number of times according to the sent number.<\/p>\n\n\n\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-2.jpg\" alt=\"\" class=\"wp-image-348\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/UART-BUS-between-two-devices-2.jpg 441w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/UART-BUS-between-two-devices-2-300x176.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/UART-BUS-between-two-devices-2-400x235.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/UART-BUS-between-two-devices-2-250x147.jpg 250w\" sizes=\"(max-width: 441px) 100vw, 441px\" \/><figcaption>UART<\/figcaption><\/figure><\/div>\n\n\n\n<p>In this guide, we will cover the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Configure UART for full duplex (TX and RX).<\/li><li>Code <\/li><li>Demo<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">1. Configure UART for full duplex <\/h2>\n\n\n\n<p>In part 1 we discussed how to set the uart to send a single character, we shall use the same initialization sequence.<\/p>\n\n\n\n<p>We started by enabling clock access to USART2 and GPIOA port<\/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;\nRCC-&gt;AHB1ENR|=RCC_AHB1ENR_GPIOAEN;<\/pre><\/div>\n\n\n\n<p>From part one, we concluded that PA2 is the TX pin and PA3 is the RX pin. Hence, we configure them as alternate function and which alternate function 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;MODER|=(1&lt;&lt;5);\/\/set bit5\nGPIOA-&gt;MODER&amp;=~(1&lt;&lt;4);\/\/reset bit4\n\nGPIOA-&gt;MODER|=(1&lt;&lt;7);\/\/set bit7\nGPIOA-&gt;MODER&amp;=~(1&lt;&lt;6);\/\/reset bit6\n\nGPIOA-&gt;AFR[0]=0x07700; \/\/ALT7 for UART2 (PA2 and PA3)<\/pre><\/div>\n\n\n\n<p>Now we need to configure the UART<\/p>\n\n\n\n<p>We starting by setting the baud rate to 9600 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;    \/\/9600 @16MHz<\/pre><\/div>\n\n\n\n<p>then selecting the UART to enable TX, RX and RXNEIE (Receiver interrupt) in Control Register 1 (CR1) and enabling UART module 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=\"267\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-23-at-8.30.00-AM-1024x267.png\" alt=\"\" class=\"wp-image-350\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-23-at-8.30.00-AM-1024x267.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-23-at-8.30.00-AM-300x78.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-23-at-8.30.00-AM-768x200.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-23-at-8.30.00-AM-1536x401.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-23-at-8.30.00-AM-2048x534.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-23-at-8.30.00-AM-1150x300.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-23-at-8.30.00-AM-750x196.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-23-at-8.30.00-AM-400x104.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-23-at-8.30.00-AM-250x65.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption>CR1<\/figcaption><\/figure><\/div>\n\n\n\n<p>Hence, the configuration shall be 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;CR1 |= (1&lt;&lt;2)|(1&lt;&lt;3)|(1&lt;&lt;5)|(1&lt;&lt;13);<\/pre><\/div>\n\n\n\n<p>After than we shall enable the interrupt of USART in NVIC 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;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">NVIC_EnableIRQ(USART2_IRQn);<\/pre><\/div>\n\n\n\n<p>Now for the interrupt handler <\/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 USART2_IRQHandler(void){\n\nif(USART2-&gt;SR&amp;0x0020) \/\/check if the read data register is not empty\n{\nc=USART2-&gt;DR; \/\/store current data in variable c\n}\n}<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">2. 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\nvoid delay(int delayms);\nvoid led_play(int value);\nvoid UART2_Init();\nvoid USART2_IRQHandler(void);\nvoid USART2_write(int ch);\nchar c;\nint main ()\n{\n__disable_irq();\nUART2_Init();\nGPIOA-&gt;MODER|= GPIO_MODER_MODER5_0; \/\/PA5 as output\nUSART2-&gt;CR1|=0x0020;\n\n__enable_irq();\n\nwhile(1)\n{\nif(c!=' '){\nled_play(c);\nUSART2_write(c);\nUSART2_write('\\r');\nUSART2_write('\\n');\t\nc=' ';\n}\n}\n\n}\n\nvoid led_play(int value)\n{\nvalue %=16; \n\tfor (;value&gt;0;value--){\n\tGPIOA-&gt;BSRR|=GPIO_BSRR_BS5; \/\/turn on ledd\n\t\tdelay(100);\n\t\tGPIOA-&gt;BSRR|=GPIO_BSRR_BR5; \/\/turn off the led\t\t\n\t\tdelay(100);\n\t}\n}\n\nvoid delay(int delayms){\nSysTick-&gt;LOAD=16000-1;\nSysTick-&gt;VAL=0;\nSysTick-&gt;CTRL=0x5;\nfor (int i=0;i&lt;delayms;i++)\n{\nwhile(!(SysTick-&gt;CTRL &amp;0x10000)){}\n\t\t}\n\tSysTick-&gt;CTRL=0;\n}\n\nvoid UART2_Init(){\n\tRCC-&gt;AHB1ENR |=1; \/\/Enable GPIOA clock\n\tRCC-&gt;APB1ENR|=0x20000; \/\/Enable USART clock\n\t\/\/configure PA3 as alternative function \n\tGPIOA-&gt;AFR[0]=0x07700; \/\/ALT7 for UART2\n\tGPIOA-&gt;AFR[1]=0x07000;\n\tGPIOA-&gt;MODER|=0x0080;  \/\/enable PA3 as alternate function\n\tGPIOA-&gt;MODER|=0x0020; \/\/enable PA2 as alterate fuction\n\tUSART2-&gt;BRR=0x008B; \/\/ set baud rate of 115200 for 16MHz\n\tUSART2-&gt;CR1|=0x0020;\n\tUSART2-&gt;CR1|=0x200C; \/\/enable RX\n\tNVIC_EnableIRQ(USART2_IRQn);\n}\n\nvoid USART2_IRQHandler(void){\n\nif(USART2-&gt;SR&amp;0x0020){\nc=USART2-&gt;DR;\n}\n}\nvoid USART2_write(int ch){\n\t\nwhile(!(USART2-&gt;SR&amp;0x0080)){\n}\nUSART2-&gt;DR=(ch&amp;0xff);\t\n}<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">3. Demo<\/h2>\n\n\n\n<figure class=\"wp-block-video aligncenter\"><video controls src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/IMG_6215.mov\"><\/video><figcaption>Demo<\/figcaption><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>In the pervious two guides (part 1 &amp; part 2 ) how to send a single character and string using UART. In this guide, we shall discuss how to receive a character from UART using interrupt handler and echo back the sent character and blink the internal led number of times according to the sent [&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-347","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\/347"}],"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=347"}],"version-history":[{"count":4,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/347\/revisions"}],"predecessor-version":[{"id":354,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/347\/revisions\/354"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=347"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=347"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=347"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}