{"id":3225,"date":"2012-06-18T02:08:25","date_gmt":"2012-06-18T06:08:25","guid":{"rendered":"https:\/\/example.com\/?postname=configure-usart-2-on-stm32f4"},"modified":"2012-06-18T02:08:25","modified_gmt":"2012-06-18T06:08:25","slug":"configure-usart-2-on-stm32f4","status":"publish","type":"post","link":"http:\/\/atomsofttech.com\/blog\/uncategorized\/configure-usart-2-on-stm32f4\/","title":{"rendered":"Configure USART 2 on STM32F4"},"content":{"rendered":"<p>I wrote a nice simple piece of code that will configure USART2 on PD5(tx) and PD6(rx). Works like a charm <img decoding=\"async\" style=\"border-style:none;\" class=\"wlEmoticon wlEmoticon-smile\" alt=\"Smile\" src=\"http:\/\/atomsoft.wordpress.com\/wp-content\/uploads\/2012\/06\/wlemoticon-smile2.png\"> For me so far at least. APBClock is 1\/8 my clock\u2026 if this doesn\u2019t work its usually a incorrect clock setting.<\/p>\n<div style=\"border-bottom:silver 1px solid;text-align:left;border-left:silver 1px solid;line-height:12pt;background-color:#f4f4f4;margin:20px 0 10px;width:97.5%;font-family:'Courier New', courier, monospace;direction:ltr;max-height:200px;font-size:8pt;overflow:auto;border-top:silver 1px solid;cursor:text;border-right:silver 1px solid;padding:4px;\" id=\"codeSnippetWrapper\">\n<pre style=\"text-align:left;line-height:12pt;background-color:#f4f4f4;margin:0;width:100%;font-family:'Courier New', courier, monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0;\"><span style=\"color:#cc6633;\">#define<\/span> myClock 168000000<br><span style=\"color:#cc6633;\">#define<\/span> myBaud 115200<br><br><span style=\"color:#0000ff;\">void<\/span> ConfigUSART2(uint32_t BAUD, uint32_t fosc)<br>{<br>  uint32_t tmpreg = 0x00, apbclock = 0x00;<br>  uint32_t integerdivider = 0x00;<br>  uint32_t fractionaldivider = 0x00;<br><br>  apbclock = fosc\/8;<br><br>  RCC-&gt;APB1ENR = (1 &lt;&lt; 17);    <span style=\"color:#008000;\">\/\/Enable USART2 Clock<\/span><br><br>  GPIOD-&gt;MODER |= (2 &lt;&lt; 10); <span style=\"color:#008000;\">\/\/Setup TX pin for Alternate Function<\/span><br>  GPIOD-&gt;MODER |= (2 &lt;&lt; 12);  <span style=\"color:#008000;\">\/\/Setup RX pin for Alternate Function<\/span><br><br>  GPIOD-&gt;AFR[0] |= (7 &lt;&lt; 20); <span style=\"color:#008000;\">\/\/Setup TX as the Alternate Function<\/span><br>  GPIOD-&gt;AFR[0] |= (7 &lt;&lt; 24); <span style=\"color:#008000;\">\/\/Setup RX as the Alternate Function<\/span><br><br>  USART2-&gt;CR1 |= (1 &lt;&lt; 13);  <span style=\"color:#008000;\">\/\/Enable USART<\/span><br><br>  integerdivider = ((25 * apbclock) \/ (2 * (BAUD)));  <br>  tmpreg = (integerdivider \/ 100) &lt;&lt; 4;<br>  fractionaldivider = integerdivider - (100 * (tmpreg &gt;&gt; 4));<br><br>  tmpreg |= ((((fractionaldivider * 8) + 50) \/ 100)) &amp; ((uint8_t)0x07);<br><br>  USART2-&gt;BRR = (uint16_t)tmpreg;<br>  USART2-&gt;CR1 |= (3 &lt;&lt; 2);  <span style=\"color:#008000;\">\/\/Enable Tx &amp; Rx<\/span><br>}<\/pre>\n<p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>I wrote a nice simple piece of code that will configure USART2 on PD5(tx) and PD6(rx). Works like a charm For me so far at least. APBClock is 1\/8 my clock\u2026 if this doesn\u2019t work its usually a incorrect clock setting. #define myClock 168000000#define myBaud 115200void ConfigUSART2(uint32_t BAUD, uint32_t fosc){ uint32_t tmpreg = 0x00, apbclock [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-3225","post","type-post","status-publish","format-standard","hentry"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/atomsofttech.com\/blog\/wp-json\/wp\/v2\/posts\/3225","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/atomsofttech.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/atomsofttech.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/atomsofttech.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/atomsofttech.com\/blog\/wp-json\/wp\/v2\/comments?post=3225"}],"version-history":[{"count":0,"href":"http:\/\/atomsofttech.com\/blog\/wp-json\/wp\/v2\/posts\/3225\/revisions"}],"wp:attachment":[{"href":"http:\/\/atomsofttech.com\/blog\/wp-json\/wp\/v2\/media?parent=3225"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/atomsofttech.com\/blog\/wp-json\/wp\/v2\/categories?post=3225"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/atomsofttech.com\/blog\/wp-json\/wp\/v2\/tags?post=3225"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}