{"id":311,"date":"2025-09-29T22:31:54","date_gmt":"2025-09-29T14:31:54","guid":{"rendered":"https:\/\/dioho.me\/?p=311"},"modified":"2025-10-13T10:33:15","modified_gmt":"2025-10-13T02:33:15","slug":"stm32%e7%82%b9%e4%ba%aeled0%e5%92%8cled1","status":"publish","type":"post","link":"https:\/\/dioho.me\/index.php\/2025\/09\/29\/stm32%e7%82%b9%e4%ba%aeled0%e5%92%8cled1\/","title":{"rendered":"STM32\u57fa\u7840-LED"},"content":{"rendered":"\n<p>1\u3001\u70b9\u4eae\u4e24\u9897LED<\/p>\n\n\n\n<p>\u5728main.c\u4e2d\uff0c\u7f16\u5199\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/* Includes *\/\n#include \"stm32f10x.h\"\n\nint main(void)\n{\n    \/\/ \u914d\u7f6e\n    \/\/\u5b9a\u4e49\u4e00\u4e2a GPIO \u521d\u59cb\u5316\u7ed3\u6784\u4f53\uff0c\u7528\u6765\u4e00\u6b21\u6027\u63cf\u8ff0\u201c\u8981\u914d\u7f6e\u54ea\u4e9b\u5f15\u811a\u3001\u4ec0\u4e48\u6a21\u5f0f\u3001\u4ec0\u4e48\u901f\u7387\u201d\n    GPIO_InitTypeDef GPIO_InitStruct;\n\n    \/\/\u7ed9 GPIOB \u5916\u8bbe\u5f00\u65f6\u949f\uff08APB2 \u603b\u7ebf\uff09\u3002\u6ca1\u5f00\u65f6\u949f\u5199 GPIO \u5bc4\u5b58\u5668\u4e0d\u4f1a\u751f\u6548\uff0c\u751a\u81f3\u53ef\u80fd HardFault\n    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);\n    \n    \/\/\u9009\u62e9 PB5\uff1b\n    \/\/\u6a21\u5f0f\u4e3a \u63a8\u633d\u8f93\u51fa\uff08Out_PP\uff09\uff0c\u9002\u5408\u9a71\u52a8 LED\uff1b\n    \/\/\u901f\u7387 2 MHz\uff1aF1 \u7684\u201c\u901f\u7387\u201d\u672c\u8d28\u662f\u8f93\u51fa\u9a71\u52a8\u80fd\u529b\/\u7ffb\u8f6c\u4e0a\u9650\uff082\/10\/50 MHz \u4e09\u6863\uff09\uff0c\u5bf9\u70b9\u706f\u9009 2 MHz \u5373\u53ef\u3001\u529f\u8017\u66f4\u4f4e\u3002\n    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5;\n    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;\n    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;\n\n    \/\/\u8c03\u5e93\u51fd\u6570\u628a\u4ee5\u4e0a\u8bbe\u7f6e\u5199\u5165\u5bc4\u5b58\u5668\uff1a\u5bf9\u4e8e F1\uff0cPB0\u2013PB7 \u5728 CRL\uff0cPB8\u2013PB15 \u5728 CRH\uff1b\u6b64\u5904\u628a MODE5=10\uff082 MHz\uff09\uff0cCNF5=00\uff08\u63a8\u633d\uff09\u3002\n    GPIO_Init(GPIOB, &amp;GPIO_InitStruct);\n\n    return 0;\n}\n<\/code><\/pre>\n\n\n\n<p>\u6b64\u4ee3\u7801\u4f5c\u7528\u662f\u70b9\u4eaeLED0\uff0c\u56e0\u4e3aLED0\u662f\u63a5\u5728B\u7ec4GPIO\u7684pin5\uff0c\u56e0\u6b64\u9700\u8981\u521d\u59cb\u5316B\u7ec4GPIO\uff0c\u5982\u679c\u9700\u8981\u70b9\u4eaeLED1\uff0c\u67e5\u770b\u539f\u7406\u56fe\u53ef\u77e5\uff0cLED0\u63a5\u5728E\u7ec4GPIO\u7684pin5\uff0c\u56e0\u6b64\u9700\u8981\u521d\u59cb\u5316E\u7ec4GPIO<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n#include \"stm32f10x.h\"\n\nint main(void)\n{\n    \/\/ \u914d\u7f6e\n    GPIO_InitTypeDef GPIO_InitStruct;\n\n    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);\n\n    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5;\n    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;\n    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;\n    GPIO_Init(GPIOB, &amp;GPIO_InitStruct);\n\n    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);\n\n    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5;\n    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;\n    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;\n    GPIO_Init(GPIOE, &amp;GPIO_InitStruct);\n\n    \/\/\u53ea\u4f4e\u7535\u5e73\u70b9\u4eae\u6ca1\u7528\uff0c\u95ea\u70c1\u903b\u8f91\u6709\u7528\n    GPIO_WriteBit(GPIOB, GPIO_Pin_5, Bit_RESET)\n    GPIO_WriteBit(GPIOE, GPIO_Pin_5, Bit_RESET)\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>\u6dfb\u52a0\u95ea\u70c1\u903b\u8f91<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/* Includes ------------------------------------------------------------------*\/\n#include \"stm32f10x.h\"\n\nvoid 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\nint main(void)\n{\n\tGPIO_InitTypeDef GPIO_InitStruct;\n\t\n\tRCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);\n\t\n\tGPIO_InitStruct.GPIO_Pin = GPIO_Pin_5;\n\tGPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;\n\tGPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;\n\tGPIO_Init(GPIOB, &amp;GPIO_InitStruct);\n\t\n\tRCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);\n\t\n\tGPIO_InitStruct.GPIO_Pin = GPIO_Pin_5;\n\tGPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;\n\tGPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;\n\tGPIO_Init(GPIOE, &amp;GPIO_InitStruct);\n\t\n\twhile(1){\n\t\tGPIO_WriteBit(GPIOB, GPIO_Pin_5, Bit_RESET);\n\t\tdelay_ms(500);\n\t\tGPIO_WriteBit(GPIOE, GPIO_Pin_5, Bit_RESET);\n\t\tdelay_ms(500);\n\t\tGPIO_WriteBit(GPIOB, GPIO_Pin_5, Bit_SET);\n\t\tdelay_ms(500);\n\t\tGPIO_WriteBit(GPIOE, GPIO_Pin_5, Bit_SET);\n\t\tdelay_ms(500);\n\t}\n\treturn 0;\n}<\/code><\/pre>\n\n\n\n<p>10-13\u63a7\u5236\u5bc4\u5b58\u5668<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/* Includes ------------------------------------------------------------------*\/\n#include \"stm32f10x.h\"\n\n#define S_GPIO_BASE 0X40010C00\n#define S_CRL S_GPIO_BASE + 0x00\n#define S_ODR S_GPIO_BASE +0x0C\n\nvoid 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}\nint main(void)\n{\n    uint32_t temreg = 0;\n\n    \/* GPIOB Periph clock enable *\/\n    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);\n\n    temreg = *((uint32_t*)(S_CRL));\n    temreg &amp;= 0xFF0FFFFF;\n    temreg += 0x00200000;\n    *((uint32_t*)(S_CRL)) = temreg;\n\n    temreg = *((uint32_t*)(S_ODR));\n    temreg &amp;= 0x0EF;\n    *((uint32_t*)(S_ODR)) = temreg;\n\n    while(1);\n}\n\n\/\/int main(void)\n\/\/{\n\/\/\t\/\/\u00c5\u00e4\u00d6\u00c3\n\/\/\tGPIO_InitTypeDef GPIO_InitStruct;\n\/\/\t\n\/\/\tRCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);\n\/\/\t\n\/\/\tGPIO_InitStruct.GPIO_Pin = GPIO_Pin_5;\n\/\/\tGPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;\n\/\/\tGPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;\n\/\/\tGPIO_Init(GPIOB, &amp;GPIO_InitStruct);\n\/\/\t\n\/\/\tRCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);\n\/\/\t\n\/\/\tGPIO_InitStruct.GPIO_Pin = GPIO_Pin_5;\n\/\/\tGPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;\n\/\/\tGPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;\n\/\/\tGPIO_Init(GPIOE, &amp;GPIO_InitStruct);\n\/\/\t\n\/\/\twhile(1){\n\/\/\t\tGPIO_WriteBit(GPIOB, GPIO_Pin_5, Bit_RESET);\n\/\/\t\tdelay_ms(500);\n\/\/\t\tGPIO_WriteBit(GPIOE, GPIO_Pin_5, Bit_RESET);\n\/\/\t\tdelay_ms(500);\n\/\/\t\tGPIO_WriteBit(GPIOB, GPIO_Pin_5, Bit_SET);\n\/\/\t\tdelay_ms(500);\n\/\/\t\tGPIO_WriteBit(GPIOE, GPIO_Pin_5, Bit_SET);\n\/\/\t\tdelay_ms(500);\n\/\/\t}\n\/\/\treturn 0;\n\/\/}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u70b9\u4eae\u4e24\u9897LED \u5728main.c\u4e2d\uff0c\u7f16\u5199\u4ee5\u4e0b\u4ee3\u7801\uff1a \u6b64\u4ee3\u7801\u4f5c\u7528\u662f\u70b9\u4eaeLED0\uff0c\u56e0\u4e3aLED0\u662f\u63a5\u5728B\u7ec4GPIO [&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-311","post","type-post","status-publish","format-standard","hentry","category-5"],"_links":{"self":[{"href":"https:\/\/dioho.me\/index.php\/wp-json\/wp\/v2\/posts\/311","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=311"}],"version-history":[{"count":6,"href":"https:\/\/dioho.me\/index.php\/wp-json\/wp\/v2\/posts\/311\/revisions"}],"predecessor-version":[{"id":335,"href":"https:\/\/dioho.me\/index.php\/wp-json\/wp\/v2\/posts\/311\/revisions\/335"}],"wp:attachment":[{"href":"https:\/\/dioho.me\/index.php\/wp-json\/wp\/v2\/media?parent=311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dioho.me\/index.php\/wp-json\/wp\/v2\/categories?post=311"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dioho.me\/index.php\/wp-json\/wp\/v2\/tags?post=311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}