{"id":195,"date":"2025-01-10T13:30:07","date_gmt":"2025-01-10T05:30:07","guid":{"rendered":"https:\/\/cthzs.cn\/?p=195"},"modified":"2025-01-10T13:30:07","modified_gmt":"2025-01-10T05:30:07","slug":"stm32%e6%8c%89%e9%94%ae%e6%8e%a7%e5%88%b6led%e7%81%af%e6%a8%a1%e5%9d%97%e5%8c%96%e7%bc%96%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/loveyou.cthzs.cn\/?p=195","title":{"rendered":"STM32\u6309\u952e\u63a7\u5236LED\u706f(\u6a21\u5757\u5316\u7f16\u7a0b)"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u9996\u5148\u6253\u5f00\u5de5\u7a0b\u6587\u4ef6\u5939\u65b0\u5efa\u4e00\u4e2a<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">Hardware<\/mark>\u6587\u4ef6\u5939<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"597\" height=\"309\" src=\"https:\/\/cthzs.cn\/wp-content\/uploads\/2025\/01\/1736414111-image.png\" alt=\"\" class=\"wp-image-197\" srcset=\"https:\/\/loveyou.cthzs.cn\/wp-content\/uploads\/2025\/01\/1736414111-image.png 597w, https:\/\/loveyou.cthzs.cn\/wp-content\/uploads\/2025\/01\/1736414111-image-300x155.png 300w\" sizes=\"auto, (max-width: 597px) 100vw, 597px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u7136\u540e\u70b9\u51fb\u7ea2\u7eff\u767d\u4e09\u4e2a\u65b9\u5757\uff0c\u63a5\u7740\u9009\u62e9\u5de5\u7a0b\u7ba1\u7406\u5efa\u4e00\u4e2a<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">Hardware<\/mark>\u7ec4<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"861\" height=\"765\" src=\"https:\/\/cthzs.cn\/wp-content\/uploads\/2025\/01\/1736416224-image.png\" alt=\"\" class=\"wp-image-201\" style=\"width:600px;height:auto\" srcset=\"https:\/\/loveyou.cthzs.cn\/wp-content\/uploads\/2025\/01\/1736416224-image.png 861w, https:\/\/loveyou.cthzs.cn\/wp-content\/uploads\/2025\/01\/1736416224-image-300x267.png 300w, https:\/\/loveyou.cthzs.cn\/wp-content\/uploads\/2025\/01\/1736416224-image-768x682.png 768w\" sizes=\"auto, (max-width: 861px) 100vw, 861px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u70b9\u51fb\u9b54\u672f\u68d2\uff0c\u9009\u62e9C\/C++\u70b9\u51fb \u5305\u542b\u8def\u5f84 \u628a\u65b0\u5efa\u7684<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">Hardware<\/mark>\u6587\u4ef6\u5939\u6dfb\u52a0\u5230\u5934\u6587\u4ef6\u91cc\u9762<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"961\" height=\"436\" src=\"https:\/\/cthzs.cn\/wp-content\/uploads\/2025\/01\/1736416716-image.png\" alt=\"\" class=\"wp-image-202\" srcset=\"https:\/\/loveyou.cthzs.cn\/wp-content\/uploads\/2025\/01\/1736416716-image.png 961w, https:\/\/loveyou.cthzs.cn\/wp-content\/uploads\/2025\/01\/1736416716-image-300x136.png 300w, https:\/\/loveyou.cthzs.cn\/wp-content\/uploads\/2025\/01\/1736416716-image-768x348.png 768w\" sizes=\"auto, (max-width: 961px) 100vw, 961px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u53f3\u51fb<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">Hardware<\/mark> \u9009\u62e9\u65b0\u5efaLED.c\u548cLED.h\u6587\u4ef6\u8ddfkey.c\u548ckey.h\uff0c\u6587\u4ef6\u8def\u5f84\u6307\u5411<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">Hardware<\/mark>\u6587\u4ef6\u5939<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"961\" height=\"854\" src=\"https:\/\/cthzs.cn\/wp-content\/uploads\/2025\/01\/1736417516-image.png\" alt=\"\" class=\"wp-image-203\" style=\"width:709px;height:auto\" srcset=\"https:\/\/loveyou.cthzs.cn\/wp-content\/uploads\/2025\/01\/1736417516-image.png 961w, https:\/\/loveyou.cthzs.cn\/wp-content\/uploads\/2025\/01\/1736417516-image-300x267.png 300w, https:\/\/loveyou.cthzs.cn\/wp-content\/uploads\/2025\/01\/1736417516-image-768x682.png 768w\" sizes=\"auto, (max-width: 961px) 100vw, 961px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">LED.c\u548cLED.h\u7528\u6765\u5c01\u88c5LED\u9a71\u52a8\uff0cLED.c\u662f\u7528\u6765\u5b58\u653e\u9a71\u52a8\u7a0b\u5e8f\u4e3b\u4f53\u4ee3\u7801\u800cLED.h\u662f\u5b58\u653eLED.c\u9a71\u52a8\u7a0b\u5e8f\u7684\u53ef\u4ee5\u5bf9\u5916\u63d0\u4f9b\u7684\u51fd\u6570\u6216\u53d8\u91cf\u58f0\u660e\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u9996\u5148\u5728LED.c\u548ckey.c\u91cc\u9762\u65b0\u5efa\u4e00\u4e2a<code>include \"stm32f10x.h\" \/\/ Device header<\/code>\u5934\u6587\u4ef6\uff0c\u7136\u540e\u5728LED.h\u548ckey.h\u91cc\u5efa\u4e00\u4e2a\u9632\u6b62\u5934\u6587\u4ef6\u91cd\u590d\u5305\u542b\u7684\u4ee3\u7801<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#ifndef __LED_H\n#define __LED_H\n\n\n\n#endif\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u63a5\u7740\u7ee7\u7eed\u5c01\u88c5LED.c\u9a71\u52a8<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include \"stm32f10x.h\"                  \/\/ Device header\n\nvoid LED_Init (void)  \/\/\u521d\u59cb\u5316LED\u51fd\u6570\n{\n\/\/RCC_APB2PeriphClockCmd\u521d\u59cb\u5316\u65f6\u949f\uff0cRCC_APB2Periph_GPIOA\u6253\u5f00GPIOA\u5916\u8bbe\uff0cENABLE\u8868\u793a\u8981\u542f\u7528\u76f8\u5e94\u7684\u65f6\u949f\n\tRCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOA, ENABLE);\n\t\n\/\/GPIO_InitTypeDef\u6570\u636e\u7c7b\u578b\u7528\u4e8e\u5b58\u50a8GPIO\u7684\u521d\u59cb\u5316\u4fe1\u606f\uff0cGPIO_Initstucture\u4e3a\u53d8\u91cf\u540d\n\tGPIO_InitTypeDef GPIO_Initstucture;\n\/\/.GPIO_Mode \u662f\u7528\u6765\u8bbe\u7f6eGPIO\u5f15\u811a\u7684\u5de5\u4f5c\u6a21\u5f0f\uff0cGPIO_Mode_Out_PP\u8868\u793a\u5c06GPIO\u5f15\u811a\u8bbe\u7f6e\u4e3a\u63a8\u633d\u8f93\u51fa\u6a21\u5f0f\n\tGPIO_Initstucture.GPIO_Mode = GPIO_Mode_Out_PP; \n\/\/.GPIO_Pin\u7528\u4e8e\u6307\u5b9a\u8981\u914d\u7f6e\u7684\u5177\u4f53GPIO\u5f15\u811a\uff0cGPIO_Pin_1 | GPIO_Pin_2\u4ee3\u8868GPIO\u7aef\u53e3\u4e2d\u7684\u7b2c1\u53f7\u5f15\u811a\u548c\u7b2c2\u53f7\u5f15\u811a\n\tGPIO_Initstucture.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2;\n\/\/.GPIO_Speed\u7528\u4e8e\u6307\u5b9a GPIO \u5f15\u811a\u7684\u8f93\u51fa\u901f\u5ea6\uff0cGPIO_Speed_50MHz\u9891\u7387\u4e3a50\u8d6b\u5179\n\tGPIO_Initstucture.GPIO_Speed = GPIO_Speed_50MHz;\n\/\/GPIO_Init\u521d\u59cb\u5316GPIO\u7aef\u53e3\u7684\u51fd\u6570\uff0cGPIOA\u8868\u793a\u8981\u521d\u59cb\u5316\u7684GPIOA\u7aef\u53e3\uff0c&amp;GPIO_Initstucture\u5c06\u5176\u5730\u5740\u4f20\u9012\u7ed9GPIO_Init\u51fd\u6570\uff0c\u4ee5\u4fbf\u51fd\u6570\u80fd\u591f\u8bfb\u53d6\u8fd9\u4e9b\u914d\u7f6e\u4fe1\u606f\u5e76\u5c06\u5176\u5e94\u7528\u5230GPIOA\u7aef\u53e3\u4e0a\n\tGPIO_Init (GPIOA, &amp;GPIO_Initstucture);\n\t\n\/\/\u5173\u95edGPIOA-1\u548cGPIOA-2\u4e32\u53e3\n\tGPIO_SetBits(GPIOA, GPIO_Pin_1 | GPIO_Pin_2);\n}\n\nvoid LED1_ON(void)  \/\/\u521d\u59cb\u5316LED1_ON\u51fd\u6570\u7528\u4e8e\u6253\u5f00\n{\n\tGPIO_ResetBits (GPIOA, GPIO_Pin_1);\n}\n\nvoid LED1_OFF(void)  \/\/\u521d\u59cb\u5316LED1_OFF\u51fd\u6570\u7528\u4e8e\u5173\u95ed\n{\n\tGPIO_SetBits (GPIOA, GPIO_Pin_1);\n}\n\nvoid LED1_Turn (void)  \/\/\u521d\u59cb\u5316LED1_Turn\u7528\u4e8e\u53d6\u53cd\n{\n\/\/GPIO_ReadOutputDataBit\u8bfb\u53d6\u5f53\u524d\u7aef\u53e3\u8f93\u51fa\u72b6\u6001\uff0c\u8bfb\u53d6GPIOA-1\u5982\u679c\u4e3a0\u5c31\u7f6e1\uff0c\u53cd\u4e4b\u4e3a0\n\tif(GPIO_ReadOutputDataBit (GPIOA, GPIO_Pin_1) == 0) GPIO_SetBits (GPIOA, GPIO_Pin_1); else GPIO_ResetBits (GPIOA, GPIO_Pin_1);\n}\n\nvoid LED2_ON(void)\n{\n\tGPIO_ResetBits (GPIOA, GPIO_Pin_2);\n}\n\nvoid LED2_OFF(void)\n{\n\tGPIO_SetBits (GPIOA, GPIO_Pin_2);\n}\n\nvoid LED2_Turn (void)\n{\n\tif(GPIO_ReadOutputDataBit (GPIOA, GPIO_Pin_2) == 0) GPIO_SetBits (GPIOA, GPIO_Pin_2); else GPIO_ResetBits (GPIOA, GPIO_Pin_2);\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u63a5\u7740\u628a\u8fd9\u4e9b\u521d\u59cb\u5316\u51fd\u6570\u653e\u5230LED.h\u91cc\u58f0\u660e<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#ifndef __LED_H\n#define __LED_H\n\nvoid LED_Init (void);\nvoid LED1_ON(void);\nvoid LED1_OFF(void);\nvoid LED1_Turn (void);\nvoid LED2_ON(void);\nvoid LED2_OFF(void);\nvoid LED2_Turn (void);\n\n#endif\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u7136\u540e\u662fkey.c\u6587\u4ef6<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include \"stm32f10x.h\"                  \/\/ Device header\n#include \"Delay.h\"\n\nvoid key_Init (void)\n{\n\tRCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOB, ENABLE);\n\t\n\tGPIO_InitTypeDef GPIO_Initstructure;\n\tGPIO_Initstructure.GPIO_Mode = GPIO_Mode_IPU;  \/\/\u4ee3\u8868\u4e0a\u62c9\u8f93\u5165\u6a21\u5f0f\n\tGPIO_Initstructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_11;\n\tGPIO_Initstructure.GPIO_Speed = GPIO_Speed_50MHz;\n\tGPIO_Init (GPIOB, &amp;GPIO_Initstructure);\n}\n\nuint8_t key_GetNum(void)  \/\/\u53ef\u4ee5\u8fd4\u56de\u6309\u4e0b\u6309\u952e\u7684\u4ee3\u7801\n{\n\tuint8_t keyNum = 0;  \/\/\u6309\u952e\u9ed8\u8ba4\u7ed90\uff0c\u6ca1\u6709\u6309\u4e0b\u8fd4\u56de0\n\tif (GPIO_ReadInputDataBit (GPIOB, GPIO_Pin_1) == 0) \/\/\u5224\u65ad\u8bfb\u53d6GPIOB-1\u7684\u503c\u662f\u5426\u4e3a0\uff0c0\u4e3a\u4f4e\u7535\u5e73\uff0c1\u4e3a\u9ad8\u7535\u5e73\uff0c\u6309\u4e0b\u4e3a0\n\t{\n\t\t\tDelay_ms(20);  \/\/\u6d88\u9664\u6296\u52a8\n\/\/ for \u5faa\u73af\u6301\u7eed\u68c0\u67e5 GPIOB \u7684 1 \u53f7\u5f15\u811a\u662f\u5426\u4ecd\u4e3a\u4f4e\u7535\u5e73\u3002\u53ea\u8981\u8be5\u5f15\u811a\u4fdd\u6301\u4f4e\u7535\u5e73\uff0c\u5faa\u73af\u5c06\u6301\u7eed\u6267\u884c\uff0c\u5e76\u5c06 keyNum \u8d4b\u503c\u4e3a 1\n\/\/ \u540c\u65f6\uff0c\u6bcf\u6b21\u5faa\u73af\u4f1a\u8fdb\u884c 20 \u6beb\u79d2\u7684\u5ef6\u8fdf\uff0c\u76ee\u7684\u662f\u786e\u4fdd\u6309\u952e\u5df2\u7ecf\u7a33\u5b9a\u6309\u4e0b\n\t\tfor ( ;GPIO_ReadInputDataBit (GPIOB, GPIO_Pin_1) == 0;keyNum = 1)\n\t\t{\n\t\t\tDelay_ms(20);\n\t\t}\n\t}\n\t\t\t\n\tif (GPIO_ReadInputDataBit (GPIOB, GPIO_Pin_11) == 0)\n\t{\n\t\t\tDelay_ms(20);\n\t\tfor ( ;GPIO_ReadInputDataBit (GPIOB, GPIO_Pin_11) == 0;keyNum = 2)\n\t\t{\n\t\t\tDelay_ms(20);\n\t\t}\n\t}\n\t\n\treturn keyNum;  \/\/\u8fd4\u56de\u503c\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u58f0\u660ekey.h<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#ifndef __KEY_H\n#define __KEY_H\n\nvoid key_Init (void);\nuint8_t key_GetNum(void);\n\n#endif\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">main.c\u4e3b\u4ee3\u7801<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include \"stm32f10x.h\"                  \/\/ Device header\n#include \"Delay.h\"\n#include \"LED.h\"\n#include \"key.h\"\n\nuint8_t keyNum;  \/\/\u5168\u5c40\u53d8\u91cf\uff0c\u5728\u51fd\u6570\u5916\u9762\u7684\u662f\u5168\u5c40\u53d8\u91cf\uff0c\u5728\u51fd\u6570\u91cc\u9762\u7684\u662f\u5c40\u90e8\u53d8\u91cf\n\nint main (void)\n{\nLED_Init ();\nkey_Init ();\n\n\n\tfor( ; ; )\n\t{\n\t\tkeyNum = key_GetNum();  \/\/\u8bfb\u53d6key_GetNum\u7684\u503c\u8d4b\u7ed9keyNum\n\t\tif (keyNum == 1) \/\/\u5224\u65adkeyNum\u662f\u5426\u4e3a1\uff0c\u662f\u6267\u884cLED1_Turn();\n\t\t{\n\t\t\tLED1_Turn();\n\t\t}\n\t\tif (keyNum == 2)\n\t\t{\n\t\t\tLED2_Turn();\n\t\t}\n\t}\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9996\u5148\u6253\u5f00\u5de5\u7a0b\u6587\u4ef6\u5939\u65b0\u5efa\u4e00\u4e2aHardware\u6587\u4ef6\u5939 \u7136\u540e\u70b9\u51fb\u7ea2\u7eff\u767d\u4e09\u4e2a\u65b9\u5757\uff0c\u63a5\u7740\u9009\u62e9\u5de5\u7a0b\u7ba1\u7406\u5efa\u4e00\u4e2aHardware\u7ec4 \u70b9\u51fb\u9b54\u672f\u68d2\uff0c\u9009 &#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[5],"tags":[],"class_list":["post-195","post","type-post","status-publish","format-standard","hentry","category-5"],"_links":{"self":[{"href":"https:\/\/loveyou.cthzs.cn\/index.php?rest_route=\/wp\/v2\/posts\/195","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/loveyou.cthzs.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/loveyou.cthzs.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/loveyou.cthzs.cn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/loveyou.cthzs.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=195"}],"version-history":[{"count":11,"href":"https:\/\/loveyou.cthzs.cn\/index.php?rest_route=\/wp\/v2\/posts\/195\/revisions"}],"predecessor-version":[{"id":213,"href":"https:\/\/loveyou.cthzs.cn\/index.php?rest_route=\/wp\/v2\/posts\/195\/revisions\/213"}],"wp:attachment":[{"href":"https:\/\/loveyou.cthzs.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/loveyou.cthzs.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/loveyou.cthzs.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}