{"id":1407,"date":"2024-11-05T21:10:30","date_gmt":"2024-11-05T13:10:30","guid":{"rendered":"https:\/\/aoofoo.com\/?p=1407"},"modified":"2024-11-06T21:54:29","modified_gmt":"2024-11-06T13:54:29","slug":"issue001","status":"publish","type":"post","link":"https:\/\/aoofoo.com\/index.php\/2024\/11\/05\/issue001\/","title":{"rendered":"[STM32]\u4e32\u53e3\u7591\u96be\u6742\u75c7\u89e3\u51b3[\u65e0\u6cd5\u5b9e\u73b0\u3001printf\u5931\u8d25]"},"content":{"rendered":"<h2>\u524d\u8a00<\/h2>\n<div class=\"sc-star-plane sc-star-plane--1\"><div class=\"sc-star-plane__content\">\u8fd9\u91cc\u662f\u89e3\u51b3\u95ee\u9898\u7684\u7248\u5757\uff0c\u9ed8\u8ba4\u4f60\u5df2\u7ecf\u6709\u76f8\u5173\u77e5\u8bc6\u4e86\u3002<\/div><\/div>\n<p>\u6211\u4eec\u4f7f\u80fd\u4e32\u53e3\u600e\u4e48\u4f7f\u80fd\uff1f<\/p>\n<p>\u65e0\u975e\u662f\u5f00\u542f\u65f6\u949f\u3001\u914d\u7f6eGPIO\u3001\u914d\u7f6eUSART\u3001\u914d\u7f6e\u4e2d\u65ad\u3001\u5199\u4e2d\u65ad\u51fd\u6570\u3002<\/p>\n<p>\u4f46\u5982\u679c\u4f60\u4e66\u5199\u7684\u4ee3\u7801\u90fd\u6ca1\u6709\u95ee\u9898\uff0c\u800c\u4e32\u53e3\u65f6\u800c\u53ef\u4ee5\u53d1\u9001\uff0c\u65f6\u800c\u53c8\u662f\u4e71\u7801\uff0c\u65f6\u800c\u534a\u4e71\u4e0d\u4e71\uff0c\u600e\u4e48\u56de\u4e8b\u5462\uff1f<\/p>\n<p>\u51e0\u4e4e\u53ef\u4ee5\u80af\u5b9a\uff0c\u662f\u56e0\u4e3a<span style=\"color: #ff9900;\"><strong>\u65f6\u949f\u9891\u7387<\/strong><\/span>\u7684\u95ee\u9898\u3002<\/p>\n<p>\u800c\u63a5\u4e0b\u6765\u5c31\u662f\u8981\u8be6\u7ec6\u7684\u8bf4\u660e\u5982\u4f55\u8c03\u8bd5\u6211\u4eec\u7684\u65f6\u949f\u9891\u7387\uff0c\u4ee5\u8fbe\u5230\u6211\u4eec\u671f\u671b\u7684\u6548\u679c\u3002<\/p>\n<p>\u9996\u5148\uff0c\u4f60\u5f97\u4f1aKeil5\u7684\u8c03\u8bd5\u529f\u80fd\uff0c\u56e0\u4e3a\u4e32\u53e3\u7684\u5931\u80fd\u610f\u5473\u7740\u4f60<span style=\"color: #ff9900;\"><strong>\u65e0\u6cd5\u901a\u8fc7<\/strong><strong>\u4e32\u53e3\u52a9\u624b<\/strong><\/span>\u6765\u9501\u5b9a\u4ee3\u7801\u7684\u9519\u8bef\uff0c\u6240\u4ee5\u4f60\u9700\u8981\u8c03\u8bd5\u529f\u80fd\u67e5\u8be2\u53d8\u91cf\u7684\u6570\u503c\u3002<\/p>\n<h2>\u6b63\u7bc7<\/h2>\n<h3>\u4fee\u6539HSE_VALUE<\/h3>\n<p>HSE\u662f\u9ad8\u901f\u5916\u90e8\u65f6\u949f\uff0c\u4f60\u5f97\u786e\u8ba4\u4f60\u624b\u4e0a\u677f\u5b50\u7684\u6676\u632f\uff0cHSE\u6676\u632f\u662f8MHz\u8fd8\u662f25MHz\uff0c\u8fd9\u4e00\u70b9\u53ef\u80fd\u4f1a\u88ab\u521d\u5b66\u8005\u5ffd\u7565\uff0c\u56e0\u4e3a\u5c31\u7b97\u6ca1\u6709\u914d\u7f6e\u597d\uff0c\u7a0b\u5e8f\u4e5f\u662f\u53ef\u4ee5\u8fd0\u884c\u7684\uff0c\u4f46\u662f\u4f1a\u51fa\u73b0\u4e32\u53e3\u4f20\u8f93\u7684\u65f6\u5019\u4f1a\u4e71\u7801\u7684\u95ee\u9898\u3002<\/p>\n<p>stm32f4xx.h\u4e2dSTM32F407\u82af\u7247\u5b9a\u4e49\u7684HSE\u65f6\u949f\u9891\u7387\u662f25M\uff0c\u800c\u6211\u677f\u5b50\u4e0a\u7684HSE\u7684\u9891\u7387\u662f8MHz\uff0c\u6240\u4ee5\u9700\u8981\u9488\u5bf9\u6027\u7684\u66f4\u6539\uff0c\u66f4\u6539\u4e3a8MHz\u3002<\/p>\n<pre class=\"corepress-code-pre\"><code>#define HSE_VALUE    ((uint32_t)8000000)<\/code><\/pre>\n<h3>\u4fee\u6539PLL<\/h3>\n<div class=\"sc-star-plane sc-star-plane--1\"><div class=\"sc-star-plane__content\">\u6ce8\u610f\uff1aUSB OTG FS\u3001SDIO \u548c RNG \u7b49\u5916\u8bbe\u9700\u8981 48 MHz \u7684\u65f6\u949f\uff0c\u76f8\u5dee\u8f83\u5927\u53ef\u80fd\u5bfc\u81f4 USB\u3001SDIO \u7b49\u5916\u8bbe\u5de5\u4f5c\u4e0d\u6b63\u5e38\u3002<\/div><\/div>\n<p>\uff08\u6ce8\u610f\u8fd9\u91cc\u662fCortex-M4\u7684\u89e3\u6790\uff09<\/p>\n<p>HSE\u8fdb\u5165PLL\u9501\u76f8\u73af\u8fdb\u884c\u5206\u9891\uff0cHSE\u5148\u901a\u8fc7<span style=\"color: #ff9900;\"><strong>PLL_M<\/strong><\/span>\u8fdb\u884c8\u5206\u9891\uff0c\u53d8\u62101MHz\u7684\u4fe1\u53f7\u3002<\/p>\n<p>\u8fd91MHz\u7684\u4fe1\u53f7\u5c31\u662fVco\u7684\u8f93\u5165\u9891\u7387\uff0c\u8f93\u5165\u9891\u7387\u518d\u901a\u8fc7<span style=\"color: #ff9900;\"><strong>PLL_N<\/strong><\/span>\uff0c\u653e\u5927336\u500d\uff0c\u53d8\u6210336MHz\uff0c\u53d8\u6210Vco\u7684\u8f93\u51fa\u9891\u7387\u3002<\/p>\n<p>\u5bf9Vco\u8f93\u51fa\u9891\u7387\u8fdb\u884c\u5206\u9891\uff0c\u53ef\u4ee5\u5f97\u5230\u7cfb\u7edf\u65f6\u949f\uff08<span style=\"color: #ff9900;\"><strong>PLL_P<\/strong><\/span>\uff09\uff0c168MHz\u3002<\/p>\n<p>USB OTG FS\u3001SDIO \u3001\u968f\u673a\u6570\u53d1\u751f\u5668\u7684\u65f6\u949f\uff08<span style=\"color: #ff9900;\"><strong>PLL_Q<\/strong><\/span>\uff09\uff0c48<span class=\"mord text\"><span class=\"mord\">MHz\u3002<\/span><\/span><\/p>\n<pre class=\"corepress-code-pre\"><code>#define PLL_M      8\r\n#define PLL_N      336\r\n#define PLL_P      2\r\n#define PLL_Q      7<\/code><\/pre>\n<p>\u5f53\u7136\uff0c\u5982\u679c\u4f60\u60f3\u8981\u8d85\u9891\u5230180\u4e5f\u662f\u6ca1\u95ee\u9898\u7684\uff0c\u53ea\u9700\u8981\u66f4\u6539\u4e00\u4e0b\u53c2\u6570\u5c31\u884c\u4e86\u3002<\/p>\n<p>&nbsp;<\/p>\n<h3>\u4fee\u6539SystemCoreClock<\/h3>\n<p>\u5343\u4e07\u4e0d\u8981\u5fd8\u8bb0\u66f4\u6539\u7cfb\u7edf\u65f6\u949f\uff0c\u6211\u4eec\u8fd9\u91cc\u7684\u7cfb\u7edf\u65f6\u949f\u672c\u8eab\u53ea\u662f\u4e00\u4e2a\u53d8\u91cf\uff0c\u7528\u4e8e\u5b58\u50a8\u548c\u53cd\u6620\u5f53\u524d\u7cfb\u7edf\u65f6\u949f\u9891\u7387\u7684\u53c2\u8003\u503c\u3002\u5b83\u4e0d\u4f1a\u5b9e\u9645\u66f4\u6539\u7cfb\u7edf\u65f6\u949f\u7684\u8bbe\u7f6e\u3002<\/p>\n<p>\u4f46\u662f\u4ed6\u4f9b\u5176\u4ed6\u51fd\u6570\u4f7f\u7528\uff0c\u4ee5\u4fbf\u8ba1\u7b97\u5b9a\u65f6\u5668\u3001\u5ef6\u65f6\u7b49\u9700\u8981\u77e5\u9053\u7cfb\u7edf\u65f6\u949f\u9891\u7387\u7684\u529f\u80fd\u3002<\/p>\n<p>\u5728\u66f4\u6539\u7cfb\u7edf\u65f6\u949f\u914d\u7f6e\u540e\uff0c\u9700\u8981\u624b\u52a8\u8c03\u7528SystemCoreClockUpdate()\u51fd\u6570\u6765\u66f4\u65b0SystemCoreClock\u7684\u503c\uff0c\u4ee5\u786e\u4fdd\u5176\u53cd\u6620\u5f53\u524d\u7684\u5b9e\u9645\u65f6\u949f\u9891\u7387\u3002<\/p>\n<pre class=\"corepress-code-pre\"><code>uint32_t SystemCoreClock = 168000000;<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>\u73b0\u5728\uff0c\u4f60\u4f1a\u53d1\u73b0\u4e32\u53e3\u5df2\u7ecf\u53ef\u4ee5\u6b63\u5e38\u4f7f\u7528\u4e86\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u518d\u8bf4\u8bf4printf\u7684\u95ee\u9898\u5427\u3002<\/p>\n<p>\u5176\u5b9eprintf\u4f60\u53ef\u4ee5\u76f4\u63a5\u53c2\u8003\u4e0b\u9762\u7684\u4ee3\u7801\uff0c\u653e\u5728\u81ea\u5df1\u7684\u4e32\u53e3\u6587\u4ef6\u4e2d\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528printf\u4e86\uff1a<\/p>\n<pre class=\"corepress-code-pre\"><code>#if 1\r\n#if (__ARMCC_VERSION &gt;= 6010050)            \/* \u4f7f\u7528AC6\u7f16\u8bd1\u5668\u65f6 *\/\r\n__asm(\".global __use_no_semihosting\\n\\t\");  \/* \u58f0\u660e\u4e0d\u4f7f\u7528\u534a\u4e3b\u673a\u6a21\u5f0f *\/\r\n__asm(\".global __ARM_use_no_argv \\n\\t\");    \/* AC6\u4e0b\u9700\u8981\u58f0\u660emain\u51fd\u6570\u4e3a\u65e0\u53c2\u6570\u683c\u5f0f\uff0c\u5426\u5219\u90e8\u5206\u4f8b\u7a0b\u53ef\u80fd\u51fa\u73b0\u534a\u4e3b\u673a\u6a21\u5f0f *\/\r\n\r\n#else\r\n\/* \u4f7f\u7528AC5\u7f16\u8bd1\u5668\u65f6, \u8981\u5728\u8fd9\u91cc\u5b9a\u4e49__FILE \u548c \u4e0d\u4f7f\u7528\u534a\u4e3b\u673a\u6a21\u5f0f *\/\r\n#pragma import(__use_no_semihosting)\r\n\r\nstruct __FILE\r\n{\r\n    int handle;\r\n    \/* Whatever you require here. If the only file you are using is *\/\r\n    \/* standard output using printf() for debugging, no file handling *\/\r\n    \/* is required. *\/\r\n};\r\n\r\n#endif\r\n\r\n\/* \u4e0d\u4f7f\u7528\u534a\u4e3b\u673a\u6a21\u5f0f\uff0c\u81f3\u5c11\u9700\u8981\u91cd\u5b9a\u4e49_ttywrch\\_sys_exit\\_sys_command_string\u51fd\u6570,\u4ee5\u540c\u65f6\u517c\u5bb9AC6\u548cAC5\u6a21\u5f0f *\/\r\nint _ttywrch(int ch)\r\n{\r\n    ch = ch;\r\n    return ch;\r\n}\r\n\r\n\/* \u5b9a\u4e49_sys_exit()\u4ee5\u907f\u514d\u4f7f\u7528\u534a\u4e3b\u673a\u6a21\u5f0f *\/\r\nvoid _sys_exit(int x)\r\n{\r\n    x = x;\r\n}\r\n\r\nchar *_sys_command_string(char *cmd, int len)\r\n{\r\n    return NULL;\r\n}\r\n\r\n\/* FILE \u5728 stdio.h\u91cc\u9762\u5b9a\u4e49. *\/\r\nFILE __stdout;\r\n\r\n\/* \u91cd\u5b9a\u4e49fputc\u51fd\u6570, printf\u51fd\u6570\u6700\u7ec8\u4f1a\u901a\u8fc7\u8c03\u7528fputc\u8f93\u51fa\u5b57\u7b26\u4e32\u5230\u4e32\u53e3 *\/\r\nint fputc(int ch, FILE *f)\r\n{\r\n    while ((USART1-&gt;SR &amp; 0X40) == 0);     \/* \u7b49\u5f85\u4e0a\u4e00\u4e2a\u5b57\u7b26\u53d1\u9001\u5b8c\u6210 *\/\r\n\r\n    USART1-&gt;DR = (uint8_t)ch;             \/* \u5c06\u8981\u53d1\u9001\u7684\u5b57\u7b26 ch \u5199\u5165\u5230DR\u5bc4\u5b58\u5668 *\/\r\n    return ch;\r\n}\r\n#endif<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00 \u6211\u4eec\u4f7f\u80fd\u4e32\u53e3\u600e\u4e48\u4f7f\u80fd\uff1f \u65e0\u975e\u662f\u5f00\u542f\u65f6\u949f\u3001\u914d\u7f6eGPIO\u3001\u914d\u7f6eUSART\u3001\u914d\u7f6e\u4e2d\u65ad\u3001\u5199\u4e2d\u65ad\u51fd\u6570\u3002 \u4f46\u5982\u679c\u4f60\u4e66 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[14,15,16],"class_list":["post-1407","post","type-post","status-publish","format-standard","hentry","category-issue","tag-stm32","tag-usart","tag-printf"],"_links":{"self":[{"href":"https:\/\/aoofoo.com\/index.php\/wp-json\/wp\/v2\/posts\/1407","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/aoofoo.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/aoofoo.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/aoofoo.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/aoofoo.com\/index.php\/wp-json\/wp\/v2\/comments?post=1407"}],"version-history":[{"count":1,"href":"https:\/\/aoofoo.com\/index.php\/wp-json\/wp\/v2\/posts\/1407\/revisions"}],"predecessor-version":[{"id":1408,"href":"https:\/\/aoofoo.com\/index.php\/wp-json\/wp\/v2\/posts\/1407\/revisions\/1408"}],"wp:attachment":[{"href":"https:\/\/aoofoo.com\/index.php\/wp-json\/wp\/v2\/media?parent=1407"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/aoofoo.com\/index.php\/wp-json\/wp\/v2\/categories?post=1407"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/aoofoo.com\/index.php\/wp-json\/wp\/v2\/tags?post=1407"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}