{"id":345,"date":"2025-10-26T22:33:12","date_gmt":"2025-10-26T14:33:12","guid":{"rendered":"https:\/\/dioho.me\/?p=345"},"modified":"2025-10-27T10:14:41","modified_gmt":"2025-10-27T02:14:41","slug":"uart%e5%88%9d%e5%a7%8b%e5%8c%96%e9%85%8d%e7%bd%ae","status":"publish","type":"post","link":"https:\/\/dioho.me\/index.php\/2025\/10\/26\/uart%e5%88%9d%e5%a7%8b%e5%8c%96%e9%85%8d%e7%bd%ae\/","title":{"rendered":"Uart\u521d\u59cb\u5316\u914d\u7f6e"},"content":{"rendered":"\n<p>\u5728\u9879\u76ee\u6587\u4ef6\u5939\/usr\u76ee\u5f55\u4e0b\u65b0\u5efauart\u6587\u4ef6\u5939\uff0c\u65b0\u5efaduart.h\uff0cduart.c\u6587\u4ef6\uff0c\u5728\u9879\u76ee\u4e2d\u65b0\u5efauart\u5e76\u5c06.c\u548c.h\u52a0\u5165\u5230uart\u4e0b\u3002<\/p>\n\n\n\n<p>\u5728stm32f10x_conf.h\u4e2d\u5c06include &#8220;stm32f10x_usart.h&#8221;\u53d6\u6d88\u6ce8\u91ca<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/dioho.me\/wp-content\/uploads\/2025\/10\/image-1.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"806\" height=\"152\" data-original=\"https:\/\/dioho.me\/wp-content\/uploads\/2025\/10\/image-1.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-351\"  sizes=\"auto, (max-width: 806px) 100vw, 806px\" \/><\/div><\/figure>\n\n\n\n<p>\u4e0b\u9762\u5206\u522b\u662fduart.h\u548cduart.c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/duart.c\n\n#include \"duart.h\"\n\nvoid DUartInit(void)\n{\n    USART_InitTypeDef USART_InitStructure;\n    GPIO_InitTypeDef  GPIO_InitStruct;\n\n    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);\n\n    GPIO_InitStruct.GPIO_Pin   = GPIO_Pin_9;\n    GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_AF_PP;\n    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;\n    GPIO_Init(GPIOA, &amp;GPIO_InitStruct);\n\n    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);\n\n    USART_InitStructure.USART_BaudRate            = 115200;\n    USART_InitStructure.USART_WordLength          = USART_WordLength_8b;\n    USART_InitStructure.USART_StopBits            = USART_StopBits_1;\n    USART_InitStructure.USART_Parity              = USART_Parity_No;\n    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;\n    USART_InitStructure.USART_Mode                = USART_Mode_Tx;\n    USART_Init(USART1, &amp;USART_InitStructure);\n    USART_Cmd(USART1, ENABLE);\n}\n\nvoid Uart_Putchar(char ch)\n{\n    USART1->DR = ch;\n}\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/duart.h\n#ifndef DIO_DUART_H\n#define DIO_DUART_H\n\n#include &lt;stm32f10x.h&gt;\n\nvoid DUartInit(void);\nvoid Uart_Putchar(char ch);\n\n#endif\n<\/code><\/pre>\n\n\n\n<p>\u5728main.c\u4e2d\u9700\u8981\u5f15\u5165duart.h\u5934\u6587\u4ef6\u5e76\u521d\u59cb\u5316<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/* Includes ------------------------------------------------------------------*\/\n#include \"stm32f10x.h\"\n#include \"led.h\"\n#include \"qsystick.h\"\n#include \"duart.h\"\n\n\/\/#define S_GPIOB_BASE 0X40010C00\n\/\/\/\/#define S_CRL S_GPIO_BASE + 0x00\n\/\/\/\/#define S_ODR S_GPIO_BASE + 0x0C\n\/\/#define S_GPIOB ((GPIOA_PORTTypeDef*)S_GPIOB_BASE)\n\n\/\/typedef struct{\n\/\/\tuint32_t CRL;\n\/\/\tuint32_t IDR;\n\/\/\tuint32_t ODR;\n\/\/\tuint32_t BSRR;\n\/\/\tuint32_t BRR;\n\/\/\tuint32_t CKR;\n\/\/}GPIOA_PORTTypeDef;\n\/\/\t\n\/\/void delay_ms(uint32_t ms){\n\/\/\tuint32_t count = 10000;\n\/\/\t\n\/\/\twhile(ms --){\n\/\/\t\tcount = 10000;\n\/\/\t\twhile(count --);\n\/\/\t}\t\n\/\/}\n\/\/int main(void)\n\/\/{\n\/\/    uint32_t temreg = 0;\n\n\/\/    \/* GPIOB Periph clock enable *\/\n\/\/    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);\n\n\/\/    temreg = S_GPIOB->CRL;\n\/\/\t  temreg &amp;= 0xFF0FFFFF;\n\/\/\t\ttemreg += 0x00200000;\n\/\/\t\tS_GPIOB->CRL = temreg;\n\/\/\t\n\/\/\t\ttemreg = S_GPIOB->ODR;\n\/\/\t\ttemreg &amp;= 0x0EF;\n\/\/\t\tS_GPIOB->ODR = temreg;\n\n\/\/    while(1);\n\/\/}\n\nint main(void)\n{\n\tSysticInit();\n\tLedInit();\n\tDUartInit(); \n\t\n\twhile(1){\n\t\tUart_Putchar('.');\n\t\tLed2_blink();\n\t\tDelayMs(500);\n\t}\n}\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/dioho.me\/wp-content\/uploads\/2025\/10\/image.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"566\" height=\"179\" data-original=\"https:\/\/dioho.me\/wp-content\/uploads\/2025\/10\/image.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-346\"  sizes=\"auto, (max-width: 566px) 100vw, 566px\" \/><\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u9879\u76ee\u6587\u4ef6\u5939\/usr\u76ee\u5f55\u4e0b\u65b0\u5efauart\u6587\u4ef6\u5939\uff0c\u65b0\u5efaduart.h\uff0cduart.c\u6587\u4ef6\uff0c\u5728\u9879\u76ee\u4e2d\u65b0\u5efauart\u5e76\u5c06 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-345","post","type-post","status-publish","format-standard","hentry","category-5"],"_links":{"self":[{"href":"https:\/\/dioho.me\/index.php\/wp-json\/wp\/v2\/posts\/345","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dioho.me\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dioho.me\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dioho.me\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dioho.me\/index.php\/wp-json\/wp\/v2\/comments?post=345"}],"version-history":[{"count":3,"href":"https:\/\/dioho.me\/index.php\/wp-json\/wp\/v2\/posts\/345\/revisions"}],"predecessor-version":[{"id":352,"href":"https:\/\/dioho.me\/index.php\/wp-json\/wp\/v2\/posts\/345\/revisions\/352"}],"wp:attachment":[{"href":"https:\/\/dioho.me\/index.php\/wp-json\/wp\/v2\/media?parent=345"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dioho.me\/index.php\/wp-json\/wp\/v2\/categories?post=345"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dioho.me\/index.php\/wp-json\/wp\/v2\/tags?post=345"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}