{"id":341,"date":"2021-08-19T05:04:41","date_gmt":"2021-08-19T05:04:41","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=341"},"modified":"2021-09-13T04:12:42","modified_gmt":"2021-09-13T04:12:42","slug":"working-with-stm32-and-uart-part-2-sending-strings-and-variable-polling-mode","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=341","title":{"rendered":"Working with STM32 and UART part 2: Sending strings and variable polling mode"},"content":{"rendered":"\n<p>In previous guide(<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>), we looked for how to send a single character. Now we will extend the usage of uart module to send a string contains &#8220;Hello from stm32f4&#8221; and a variable as &#8220;my variable is variable&#8221;<\/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-1.jpg\" alt=\"\" class=\"wp-image-342\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/UART-BUS-between-two-devices-1.jpg 441w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/UART-BUS-between-two-devices-1-300x176.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/UART-BUS-between-two-devices-1-400x235.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/UART-BUS-between-two-devices-1-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>Extra steps needed to send a string<\/li><li>Code<\/li><li>Results<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">1. Extra steps needed to send a string <\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>Starting from part 1, we need to add the following two header file<\/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;stdio.h&quot; \/\/stdio which allows us to use sprinttf \n#include &quot;stdint.h&quot; \/\/standardd int defention <\/pre><\/div>\n\n\n\n<p>then the prototype of the function to send a string <\/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 UART_Write_String(char *p);<\/pre><\/div>\n\n\n\n<p>then the buffer to hold the data and variable <\/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;}\">char buffer[40]={'0'};\nunsigned int variable;<\/pre><\/div>\n\n\n\n<p>Now for the function that allows us to print the string<\/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 UART_Write_String(char *p)\n\t{\n\twhile(*p!='\\0')\n{\nUSART_write(*p);\np++;\n}\n}<\/pre><\/div>\n\n\n\n<p>The function works as following<\/p>\n\n\n\n<p>First, we pass the string as argument, then it sends the string character by character until reaches NULL (&#8216;\\0&#8217;) then it stops<\/p>\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\n#include &quot;stdio.h&quot;\n#include &quot;stdint.h&quot;\nvoid USART_write(int ch);\nvoid USART2_Init(void);\nvoid delay(int delayms);\nvoid UART_Write_String(char *p);\n\nchar buffer[40]={'0'};\nunsigned int variable;\n\nint main(){\n\nUSART2_Init();\nwhile(1){\nUART_Write_String(&quot;---------------------------\\r\\n&quot;);\nUART_Write_String(&quot;hello from stm32f411RE\\r\\n&quot;);\nsprintf(buffer,&quot;my variable is %u \\r\\n&quot;,++variable);\nUART_Write_String(buffer);\nUART_Write_String(&quot;---------------------------\\r\\n&quot;);\n\/\/variable=variable+1;\ndelay(1000);\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 UART_Write_String(char *p)\n\t{\n\twhile(*p!='\\0')\n{\nUSART_write(*p);\np++;\n}\n}\n\n\nvoid delay(int delayms){\nvolatile int i;\n\tfor(; delayms&gt;0;delayms--){\n\tfor(i=0;i&lt;3192;i++);\n\t}\n}\n<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">3. Results<\/h2>\n\n\n\n<p>After compiling and uploading the code, open your serial terminal and set the baud to 9600 and you will get this<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"994\" height=\"514\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-19-at-8.03.22-AM.png\" alt=\"\" class=\"wp-image-343\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-19-at-8.03.22-AM.png 994w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-19-at-8.03.22-AM-300x155.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-19-at-8.03.22-AM-768x397.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-19-at-8.03.22-AM-750x388.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-19-at-8.03.22-AM-400x207.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/Screen-Shot-2021-08-19-at-8.03.22-AM-250x129.png 250w\" sizes=\"(max-width: 994px) 100vw, 994px\" \/><\/figure><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In previous guide(part 1), we looked for how to send a single character. Now we will extend the usage of uart module to send a string contains &#8220;Hello from stm32f4&#8221; and a variable as &#8220;my variable is variable&#8221; In this guide we will cover the following: Extra steps needed to send a string Code Results [&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-341","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\/341"}],"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=341"}],"version-history":[{"count":2,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/341\/revisions"}],"predecessor-version":[{"id":408,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/341\/revisions\/408"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=341"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=341"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=341"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}