{"id":106428,"date":"2026-09-17T11:25:16","date_gmt":"2026-09-17T03:25:16","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/106428.html"},"modified":"2026-09-17T11:25:16","modified_gmt":"2026-09-17T03:25:16","slug":"%e3%80%90stm32%e3%80%91-i2c_eeprom_test-%e5%b7%a5%e7%a8%8b%ef%bc%88%e5%ae%9e%e6%88%98%e7%af%87%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/106428.html","title":{"rendered":"\u3010STM32\u3011 I2C_EEPROM_test \u5de5\u7a0b\uff08\u5b9e\u6218\u7bc7\uff09"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1600\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260917032506-6aab5d92d8fbd.png\" width=\"2848\" \/><\/p>\n<hr \/>\n<h2><span style=\"color:#a2e043\"><span style=\"background-color:#7b7f82\">\u00a0\u5de5\u7a0b\u5256\u6790&#xff1a;\u4e32\u53e3\u914d\u7f6e \u2192 AT24C02 \u6301\u4e45\u5316 \u2192 LED \u95ea\u706f<\/span><\/span><span style=\"color:#0d0016\"><span style=\"background-color:#7b7f82\">\u00a0\u00a0<\/span><\/span><\/h2>\n<hr \/>\n<h2><span style=\"color:#1a439c\">1. \u5de5\u7a0b\u6982\u89c8<\/span><\/h2>\n<h4><span style=\"color:#511b78\">1.1 \u529f\u80fd\u4e00\u53e5\u8bdd<\/span><\/h4>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u901a\u8fc7\u4e32\u53e3\u4ea4\u4e92\u914d\u7f6e&#034;\u9009\u54ea\u51e0\u9897\u706f\u3001\u95ea\u51e0\u6b21\u3001\u95f4\u9694\u591a\u4e45\u3001\u5ef6\u65f6\u591a\u4e45\u542f\u52a8&#034;&#xff0c;\u628a\u914d\u7f6e\u5199\u5165 AT24C02&#xff08;I2C EEPROM&#xff09;\u56fa\u5316&#xff0c;\u4e0a\u7535\u540e\u6309\u4fdd\u5b58\u7684\u914d\u7f6e\u81ea\u52a8\u95ea\u706f\u2014\u2014\u5178\u578b\u7684&#034;\u53c2\u6570\u6389\u7535\u6301\u4e45\u5316&#034;\u5e94\u7528\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u5b9e\u9645\u8fd0\u884c\u6548\u679c&#xff08;\u4e32\u53e3\u63a7\u5236\u53f0&#xff09;&#xff1a;<\/span><\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"446\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260917032512-6aab5d98a5551.png\" width=\"565\" \/><\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u4e0b\u4e00\u6b21\u4e0a\u7535\u9009&#034;2.\u4f7f\u7528\u73b0\u5728\u7684\u914d\u7f6e&#034;&#xff0c;\u8bfb\u56de EEPROM \u6570\u636e\u3001\u6821\u9a8c\u901a\u8fc7\u540e\u6309\u540c\u4e00\u5957\u53c2\u6570\u95ea\u706f\u3002<\/span><\/p>\n<hr \/>\n<h4><span style=\"color:#511b78\">1.2 \u82af\u7247\u4e0e\u76ee\u5f55\u7ed3\u6784<\/span><\/h4>\n<table>\n<tr><span style=\"color:#0d0016\">\u9879\u76ee<\/span><span style=\"color:#0d0016\">\u5185\u5bb9<\/span><\/tr>\n<tbody>\n<tr>\n<td><span style=\"color:#0d0016\">\u82af\u7247<\/span><\/td>\n<td><span style=\"color:#0d0016\">STM32F103ZET6&#xff08;LQFP144&#xff0c;512KB Flash \/ 64KB SRAM&#xff09;<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">\u4e3b\u9891<\/span><\/td>\n<td><span style=\"color:#0d0016\">8MHz HSE \u00d7 PLL9 &#061; 72MHz<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">\u5916\u8bbe<\/span><\/td>\n<td><span style=\"color:#0d0016\">I2C2&#xff08;400kHz&#xff09;\u3001USART1&#xff08;115200&#xff09;\u3001GPIO PF8~PF11<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">EEPROM<\/span><\/td>\n<td><span style=\"color:#0d0016\">AT24C02&#xff08;256 \u5b57\u8282&#xff0c;8 \u5b57\u8282\/\u9875&#xff0c;\u5199\u5468\u671f\u6700\u957f 5ms&#xff09;<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">\u5de5\u5177\u94fe<\/span><\/td>\n<td><span style=\"color:#0d0016\">CubeMX \u751f\u6210 &#043; Keil MDK-ARM&#xff08;VSCode &#043; Keil Assistant&#xff09;<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>I2C_EEPROM_test\/                  \u2190 \u5de5\u7a0b\u6839\u76ee\u5f55<br \/>\n\u251c\u2500\u2500 APP\/                          # \u5e94\u7528\u5c42&#xff08;\u7528\u6237\u4ee3\u7801&#xff09;<br \/>\n\u2502   \u251c\u2500\u2500 App.c \/ App.h             # \u914d\u7f6e\u4ea4\u4e92 &#043; \u6267\u884c\u903b\u8f91<br \/>\n\u251c\u2500\u2500 Core\/                         # CubeMX \u751f\u6210&#xff1a;HAL \u521d\u59cb\u5316\u9aa8\u67b6<br \/>\n\u2502   \u251c\u2500\u2500 Inc\/                      # main.h \/ stm32f1xx_it.h &#8230;<br \/>\n\u2502   \u2514\u2500\u2500 Src\/                      # main.c \/ stm32f1xx_hal_msp.c \/ stm32f1xx_it.c<br \/>\n\u251c\u2500\u2500 Drivers\/                      # CubeMX \u751f\u6210&#xff1a;HAL \u5e93 &#043; CMSIS<br \/>\n\u251c\u2500\u2500 MDK-ARM\/                      # Keil \u5de5\u7a0b&#xff08;.uvprojx &#043; .vscode\/&#xff09;<br \/>\n\u251c\u2500\u2500 I2C_EEFORM_test.ioc           # CubeMX \u5de5\u7a0b\u6587\u4ef6&#xff08;\u5f15\u811a\/\u65f6\u949f\u914d\u7f6e&#xff09;<br \/>\n\u2514\u2500\u2500 .mxproject<\/p>\n<p>D:\\\\code_project\\\\BSP_drivers\/      \u2190 \u5de5\u7a0b\u5916\u7684\u5171\u4eab BSP \u9a71\u52a8\u5e93<br \/>\n\u251c\u2500\u2500 inc\/   BSP_AT24C02.h  BSP_LED.h  BSP_Usart_Redir.h<br \/>\n\u2514\u2500\u2500 src\/   BSP_AT24C02.c  BSP_LED.c  BSP_Usart_Redir.c<\/p>\n<p><span style=\"color:#511b78\">\u6ce8\u610f&#xff1a;<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0BSP \u9a71\u52a8\u4e0d\u5728\u5de5\u7a0b\u76ee\u5f55\u91cc&#xff0c;Keil \u901a\u8fc7 ..\/..\/BSP_drivers\/inc \u5f15\u7528\u5934\u6587\u4ef6&#xff0c;Usart_Redir_test \u7b49\u5176\u4ed6\u6d4b\u8bd5\u5de5\u7a0b\u5171\u7528\u540c\u4e00\u4efd\u9a71\u52a8\u3002\u8fd9\u5c31\u662f&#034;\u9a71\u52a8\u72ec\u7acb\u6210\u5e93\u3001\u591a\u5de5\u7a0b\u590d\u7528&#034;\u7684\u5de5\u7a0b\u5316\u505a\u6cd5\u3002<\/span><\/p>\n<hr \/>\n<h3><span style=\"color:#1a439c\">2. \u786c\u4ef6\u8bbe\u8ba1<\/span><\/h3>\n<h4><span style=\"color:#511b78\">2.1 \u5f15\u811a\u603b\u8868<\/span><\/h4>\n<table>\n<tr><span style=\"color:#0d0016\">\u5916\u8bbe<\/span><span style=\"color:#0d0016\">\u5f15\u811a<\/span><span style=\"color:#0d0016\">GPIO \u6a21\u5f0f<\/span><span style=\"color:#0d0016\">\u901f\u5ea6\/\u7ec6\u8282<\/span><\/tr>\n<tbody>\n<tr>\n<td><span style=\"color:#0d0016\">I2C2_SCL<\/span><\/td>\n<td><span style=\"color:#0d0016\">PB10<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u590d\u7528\u5f00\u6f0f AF_OD<\/span><\/td>\n<td><span style=\"color:#0d0016\">High speed&#xff0c;400kHz Fast \u6a21\u5f0f<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">I2C2_SDA<\/span><\/td>\n<td><span style=\"color:#0d0016\">PB11<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u590d\u7528\u5f00\u6f0f AF_OD<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u540c\u4e0a&#xff08;\u9700\u5916\u90e8\u4e0a\u62c9\u7535\u963b&#xff09;<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">USART1_TX<\/span><\/td>\n<td><span style=\"color:#0d0016\">PA9<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u590d\u7528\u63a8\u633d AF_PP<\/span><\/td>\n<td><span style=\"color:#0d0016\">115200<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">USART1_RX<\/span><\/td>\n<td><span style=\"color:#0d0016\">PA10<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u590d\u7528\u63a8\u633d AF_PP<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u8f93\u5165<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">LED1~LED4<\/span><\/td>\n<td><span style=\"color:#0d0016\">PF8~PF11<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u901a\u7528\u5f00\u6f0f\u8f93\u51fa OD<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u4f4e\u7535\u5e73\u70b9\u4eae<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<h4><span style=\"color:#511b78\">2.2 LED \u4e3a\u4ec0\u4e48\u662f\u5f00\u6f0f\u8f93\u51fa<\/span><\/h4>\n<p><span style=\"color:#0d0016\">CubeMX \u91cc PF8~PF11 \u914d\u7684\u662f GPIO_MODE_OUTPUT_OD&#xff08;\u5f00\u6f0f&#xff09;&#xff0c;\u4ee3\u7801\u91cc&#034;\u70b9\u4eae &#061; \u5199\u4f4e\u7535\u5e73&#034;&#xff1a;<\/span><\/p>\n<p>GPIO_PinState state &#061; flag ? GPIO_PIN_RESET : GPIO_PIN_SET;  \/\/ ON \u2192 RESET(\u4f4e)<\/p>\n<p><span style=\"color:#511b78\">\u5f00\u6f0f\u8f93\u51fa\u7684\u672c\u8d28&#xff1a;<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u5f15\u811a\u53ea\u80fd\u62c9\u4f4e&#xff08;\u5bfc\u901a\u5230\u5730&#xff09;&#xff0c;\u4e0d\u80fd\u4e3b\u52a8\u8f93\u51fa\u9ad8\u3002\u5bf9\u5e94\u786c\u4ef6\u7535\u8def\u901a\u5e38\u662f&#xff1a;<\/span><\/p>\n<p>VCC \u2500\u2500 LED \u2500\u2500 \u9650\u6d41\u7535\u963b \u2500\u2500 GPIO(PF8)<\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0GPIO \u8f93\u51fa\u4f4e\u7535\u5e73 \u2192 \u7535\u6d41\u704c\u8fdb\u5f15\u811a \u2192 LED \u4eae&#xff1b;<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u8f93\u51fa\u9ad8\u963b\/\u9ad8\u7535\u5e73\u65f6 LED \u706d\u3002<\/span><\/p>\n<p><span style=\"color:#511b78\">\u8fd9\u6837\u505a\u7684\u4e24\u4e2a\u597d\u5904&#xff1a;<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u4e00\u662f\u53ef\u4ee5\u9a71\u52a8\u9ad8\u4e8e MCU \u7535\u5e73\u7684\u8d1f\u8f7d&#xff08;\u6bd4\u5982 5V \u4f9b\u7535\u7684 LED \u706f\u6761&#xff0c;\u5f00\u6f0f &#043; \u5916\u90e8\u4e0a\u62c9\u5c31\u5b9e\u73b0\u4e86\u7535\u5e73\u8f6c\u6362&#xff09;&#xff1b;<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u4e8c\u662f\u591a\u4e2a\u5f00\u6f0f\u8f93\u51fa\u53ef\u4ee5\u76f4\u63a5\u5e76\u8054&#xff08;\u7ebf\u4e0e\u903b\u8f91&#xff09;\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u7f3a\u70b9\u662f\u5fc5\u987b\u9760\u5916\u90e8\u4e0a\u62c9\u7535\u963b&#xff0c;\u6240\u4ee5\u914d\u7f6e\u91cc Pull &#061; GPIO_NOPULL&#xff0c;\u4e0a\u62c9\u5728\u677f\u5b50\u4e0a\u3002<\/span><\/p>\n<hr \/>\n<h4><span style=\"color:#511b78\">2.3 AT24C02 \u5668\u4ef6\u901f\u89c8<\/span><\/h4>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0AT24C02 \u662f Atmel\/Microchip \u7684 2Kbit&#xff08;256 \u5b57\u8282&#xff09;I2C EEPROM&#xff0c;\u672c\u5de5\u7a0b\u7528\u5230\u7684\u5173\u952e\u7279\u6027&#xff1a;<\/span><\/p>\n<table>\n<tr><span style=\"color:#0d0016\">\u7279\u6027<\/span><span style=\"color:#0d0016\">\u503c<\/span><span style=\"color:#0d0016\">\u5bf9\u4ee3\u7801\u7684\u5f71\u54cd<\/span><\/tr>\n<tbody>\n<tr>\n<td><span style=\"color:#0d0016\">\u5bb9\u91cf<\/span><\/td>\n<td><span style=\"color:#0d0016\">256 \u5b57\u8282<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u5730\u5740 0x00~0xFF&#xff0c;8 \u4f4d\u5b58\u50a8\u5730\u5740<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">\u9875\u5927\u5c0f<\/span><\/td>\n<td><span style=\"color:#0d0016\">8 \u5b57\u8282<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u4e00\u6b21\u5199\u5468\u671f\u6700\u591a\u5199 8 \u5b57\u8282\u4e14\u4e0d\u80fd\u8de8\u9875<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">\u5668\u4ef6\u5730\u5740<\/span><\/td>\n<td><span style=\"color:#0d0016\">1010 A2 A1 A0&#xff08;0x50 \u57fa\u5740&#xff09;<\/span><\/td>\n<td><span style=\"color:#0d0016\">A2A1A0&#061;000 \u65f6 7 \u4f4d\u5730\u5740 0x50<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">\u5199\u5468\u671f tWR<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u6700\u957f 5ms<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u5199\u5b8c\u5fc5\u987b\u8f6e\u8be2\u7b49 ACK&#xff0c;\u5426\u5219\u8bfb\u5230\u65e7\u6570\u636e<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">\u64e6\u5199\u5bff\u547d<\/span><\/td>\n<td><span style=\"color:#0d0016\">100 \u4e07\u6b21<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u9891\u7e41\u5199\u8981\u7559\u610f\u78e8\u635f&#xff08;\u672c\u5de5\u7a0b\u53ea\u5728\u914d\u7f6e\u65f6\u5199\u4e00\u6b21&#xff0c;\u65e0\u538b\u529b&#xff09;<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u5f15\u811a\u4e0a\u9664\u4e86 SCL\/SDA&#xff0c;\u8fd8\u6709 A0\/A1\/A2&#xff08;\u5730\u5740\u9009\u62e9&#xff0c;\u63a5\u5730\u4e3a 000 \u2192 0x50&#xff09;\u3001WP&#xff08;\u5199\u4fdd\u62a4&#xff0c;\u63a5\u5730\u5141\u8bb8\u5199&#xff09;\u3002\u540c\u4e00 I2C \u603b\u7ebf\u4e0a\u6700\u591a\u53ef\u4ee5\u6302 8 \u7247 AT24C02&#xff0c;\u9760 A0~A2 \u533a\u5206\u5730\u5740\u3002<\/span><\/p>\n<hr \/>\n<h4><span style=\"color:#511b78\">2.4 \u65f6\u949f\u6811<\/span><\/h4>\n<p>HSE 8MHz \u2500\u2500\u00d79(PLL)\u2500\u2500\u2192 SYSCLK 72MHz<br \/>\n                        \u251c\u2500 AHB \/1 \u2192 HCLK 72MHz&#xff08;SysTick\u3001GPIO \u65f6\u949f\u57df&#xff09;<br \/>\n                        \u251c\u2500 APB1 \/2 \u2192 PCLK1 36MHz&#xff08;I2C2\u3001USART2\/3\u3001TIM \u7b49&#xff09;<br \/>\n                        \u2514\u2500 APB2 \/1 \u2192 PCLK2 72MHz&#xff08;USART1\u3001GPIO\u3001TIM1 \u7b49&#xff09;<\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0SystemClock_Config \u91cc FLASH_LATENCY_2 \u5bf9\u5e94 72MHz \u9700\u8981 2 \u4e2a Flash \u7b49\u5f85\u5468\u671f\u2014\u2014\u8fd9\u662f 72MHz \u8d85\u9891\/\u6b63\u5e38\u6ee1\u9891\u8fd0\u884c\u7684\u5fc5\u8981\u914d\u7f6e\u3002<\/span><\/p>\n<hr \/>\n<h3><span style=\"color:#1a439c\">3. I2C \u534f\u8bae\u4e0e AT24C02 \u57fa\u7840&#xff08;\u628a\u5e95\u5c42\u8bb2\u900f&#xff09;<\/span><\/h3>\n<h4><span style=\"color:#511b78\">3.1 7 \u4f4d\u5730\u5740 vs 8 \u4f4d\u5730\u5740&#xff1a;\u4e3a\u4ec0\u4e48 0x50 &lt;&lt; 1 &#061; 0xA0<\/span><\/h4>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0I2C \u534f\u8bae\u91cc&#xff0c;\u4ece\u673a\u5730\u5740\u5728\u603b\u7ebf\u4e0a\u4f20\u8f93\u65f6\u662f\u4e00\u4e2a\u5b8c\u6574\u7684 8 \u4f4d\u5b57\u8282&#xff1a;<\/span><span style=\"color:#b95514\">\u9ad8 7 \u4f4d\u662f\u5668\u4ef6\u5730\u5740&#xff0c;\u6700\u4f4e\u4f4d\u662f\u8bfb\u5199\u6807\u5fd7\u4f4d&#xff08;0&#061;\u5199&#xff0c;1&#061;\u8bfb&#xff09;<\/span><span style=\"color:#0d0016\">\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0AT24C02 \u7684 7 \u4f4d\u5730\u5740\u7531&#034;\u56fa\u5b9a\u90e8\u5206 1010&#034; &#043; &#034;A2A1A0&#034;\u7ec4\u6210&#xff1a;<\/span><\/p>\n<p>7 \u4f4d\u5730\u5740:  1  0  1  0  A2 A1 A0        \u2190 A2A1A0&#061;000 \u2192 0b1010000 &#061; 0x50<br \/>\n8 \u4f4d\u5199\u5730\u5740: 1010 000 0  &#061; 0xA0          \u2190 \u6700\u4f4e\u4f4d R\/W&#061;0<br \/>\n8 \u4f4d\u8bfb\u5730\u5740: 1010 000 1  &#061; 0xA1          \u2190 \u6700\u4f4e\u4f4d R\/W&#061;1<\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u6240\u4ee5 DEV_ADDRESS 0x50 \u662f 7 \u4f4d\u5730\u5740&#xff0c;at24c02.dev_address &#061; 0x50 &lt;&lt; 1 &#061; 0xA0 \u662f\u5e26\u4e0a\u5199\u4f4d\u7684 8 \u4f4d\u5730\u5740\u2014\u2014HAL \u7684 HAL_I2C_Mem_Read\/Write \u9700\u8981\u7684\u6b63\u662f\u8fd9\u4e2a 8 \u4f4d\u503c\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u8fd9\u662f\u4e2a\u7ecf\u5178\u6613\u6df7\u70b9&#xff1a;\u6570\u636e\u624b\u518c\u4e0a\u5199\u7684&#034;0xA0&#034;\u901a\u5e38\u5df2\u7ecf\u662f 8 \u4f4d\u5199\u5730\u5740&#xff0c;\u800c\u4ee3\u7801\u91cc\u4e60\u60ef\u5b58 7 \u4f4d\u518d\u79fb\u4f4d&#xff0c;\u4e24\u79cd\u5199\u6cd5\u90fd\u5bf9&#xff0c;\u4f46\u8981\u5206\u6e05\u695a\u3002<\/span><\/p>\n<hr \/>\n<h4><span style=\"color:#511b78\">3.2 \u4e00\u6b21\u5199\u4e8b\u52a1\/\u8bfb\u4e8b\u52a1\u5728\u603b\u7ebf\u4e0a\u7684\u6837\u5b50<\/span><\/h4>\n<p><span style=\"color:#0d0016\">\u5199\u4e00\u5b57\u8282\u5230\u5730\u5740 0x00&#xff08;HAL_I2C_Mem_Write \u505a\u7684\u4e8b&#xff09;&#xff1a;<\/span><\/p>\n<p>S | 0xA0(W) | ACK | 0x00(\u5b58\u50a8\u5730\u5740) | ACK | D0 | ACK | P<br \/>\n\u8d77\u59cb  \u4ece\u673a\u5730\u5740  \u5e94\u7b54   \u5185\u5b58\u5730\u5740       \u5e94\u7b54   \u6570\u636e  \u5e94\u7b54  \u505c\u6b62<\/p>\n<p><span style=\"color:#0d0016\">\u4ece\u5730\u5740 0x00 \u8bfb 8 \u5b57\u8282&#xff08;HAL_I2C_Mem_Read \u505a\u7684\u4e8b&#xff0c;\u6ce8\u610f\u4e2d\u95f4\u6709 Repeated START&#xff09;&#xff1a;<\/span><\/p>\n<p>S | 0xA0(W) | ACK | 0x00 | ACK | Sr | 0xA1(R) | ACK | D0..D6 | ACK | D7 | NACK | P<br \/>\n\u8d77\u59cb  \u5148\u5199\u5730\u5740      \u5e94\u7b54          \u91cd\u590d\u8d77\u59cb  \u5207\u5230\u8bfb\u6a21\u5f0f      \u8bfb8\u5b57\u8282     \u672b\u5b57\u8282\u4e0d\u7b54  \u505c\u6b62<\/p>\n<p><span style=\"color:#b95514\">\u4e3a\u4ec0\u4e48\u8981&#034;\u5148\u5199\u5730\u5740\u518d\u5207\u6362\u8bfb\u6a21\u5f0f&#034;&#xff1f;<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u56e0\u4e3a EEPROM \u662f\u5185\u5b58\u6620\u5c04\u5668\u4ef6&#xff0c;\u5fc5\u987b\u5148\u544a\u8bc9\u5b83&#034;\u4ece\u54ea\u4e2a\u5730\u5740\u5f00\u59cb&#034;&#xff0c;\u7136\u540e\u624d\u80fd\u8fde\u7eed\u8bfb\u51fa\u3002<\/span><span style=\"color:#0d0016\">\u00a0\u6700\u540e\u4e00\u4e2a\u5b57\u8282\u4e3b\u673a\u56de NACK \u544a\u8bc9\u4ece\u673a&#034;\u4e0d\u8981\u518d\u53d1\u4e86&#034;&#xff0c;\u7136\u540e\u53d1\u505c\u6b62\u4f4d\u2014\u2014\u8fd9\u662f I2C \u8bfb\u64cd\u4f5c\u7684\u6536\u5c3e\u89c4\u8303\u3002<\/span><\/p>\n<hr \/>\n<h4><span style=\"color:#511b78\">3.3 \u4e3a\u4ec0\u4e48 I2C_MEMADD_SIZE_8BIT<\/span><\/h4>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0AT24C02 \u53ea\u6709 256 \u5b57\u8282&#xff0c;\u5b58\u50a8\u5730\u5740 0255 \u7528\u4e00\u4e2a\u5b57\u8282\u5c31\u80fd\u8868\u793a&#xff0c;\u6240\u4ee5 I2C_MEMADD_SIZE_8BIT<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u6362\u5927\u5bb9\u91cf\u578b\u53f7\u8981\u6539\u8fd9\u91cc<\/span><span style=\"color:#0d0016\">AT24C04\/08\/16 \u5730\u5740\u8d85\u8fc7 8 \u4f4d&#xff08;9~11 \u4f4d&#xff09;&#xff0c;\u4e00\u90e8\u5206\u5730\u5740\u4f4d\u88ab\u5e76\u8fdb\u5668\u4ef6\u5730\u5740\u5b57\u8282\u91cc&#xff0c;I2C_MEMADD_SIZE_8BIT \u5c31\u4e0d\u591f\u7528\u4e86\u2014\u2014\u8fd9\u662f\u79fb\u690d\u65f6\u6700\u5bb9\u6613\u6f0f\u7684\u5730\u65b9\u3002<\/span><\/p>\n<hr \/>\n<h4><span style=\"color:#511b78\">3.4 \u5199\u5468\u671f tWR&#xff1a;\u4e3a\u4ec0\u4e48\u5199\u5b8c\u8fd8\u8981\u8f6e\u8be2<\/span><\/h4>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0EEPROM \u6536\u5230\u6570\u636e\u540e&#xff0c;\u5185\u90e8\u8981\u7528\u7535\u8377\u6cf5\u64e6\u9664&#043;\u7f16\u7a0b&#xff0c;\u8fd9\u4e2a\u8fc7\u7a0b\u9700\u8981\u65f6\u95f4&#xff08;\u6700\u957f 5ms&#xff09;&#xff0c;\u671f\u95f4\u5668\u4ef6\u4e0d\u54cd\u5e94\u4efb\u4f55 ACK\u3002\u6240\u4ee5\u9a71\u52a8\u5199\u5b8c\u5fc5\u987b\u8f6e\u8be2\u63a2\u6d4b&#xff1a;<\/span><\/p>\n<p>static bool BSP_AT24C02_Ready(void)<br \/>\n{<br \/>\n    return HAL_OK &#061;&#061; HAL_I2C_IsDeviceReady(hi2c, dev_address, POLL_RETYIES, POLL_TIME_MS);<br \/>\n    \/\/ \u91cd\u8bd510\u6b21 \u00d7 \u6bcf\u6b21\u95f4\u96942ms&#xff0c;\u5668\u4ef6\u6062\u590dACK\u5373\u8ba4\u4e3a\u5199\u5468\u671f\u7ed3\u675f<br \/>\n}<\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0HAL_I2C_IsDeviceReady \u53d1&#034;<\/span><span style=\"color:#b95514\">\u8d77\u59cb&#043;\u5730\u5740&#043;\u7b49ACK&#043;\u505c\u6b62<\/span><span style=\"color:#0d0016\">&#034;&#xff0c;EEPROM \u6ca1\u5199\u5b8c\u5c31\u4e0d ACK&#xff0c;\u51fd\u6570\u8fd4\u56de\u8d85\u65f6\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0 \u00a0 \u00a0 \u00a0\u6f0f\u6389\u8fd9\u4e00\u6b65\u7684\u5178\u578b\u75c7\u72b6&#xff1a;<\/span><span style=\"color:#b95514\">\u5199\u5b8c\u7acb\u523b\u8bfb&#xff0c;\u8bfb\u5230\u7684\u662f\u65e7\u6570\u636e<\/span><span style=\"color:#0d0016\">\u3002<\/span><\/p>\n<hr \/>\n<h3><span style=\"color:#1a439c\">4. \u5206\u5c42\u67b6\u6784<\/span><\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"442\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260917032512-6aab5d98c03d5.png\" width=\"860\" \/><\/p>\n<p><span style=\"color:#511b78\">\u67b6\u6784\u8981\u70b9&#xff1a;<\/span><\/p>\n<li><span style=\"color:#0d0016\">\u751f\u6210\u5c42\u53ea\u505a\u521d\u59cb\u5316&#xff1a;main.c \u5168\u90e8\u662f CubeMX \u751f\u6210\u7684\u521d\u59cb\u5316\u4ee3\u7801&#xff0c;\u7528\u6237\u4ee3\u7801\u53ea\u51fa\u73b0\u5728 USER CODE \u6ce8\u91ca\u533a\u95f4\u2014\u2014CubeMX \u91cd\u65b0\u751f\u6210\u4e0d\u4f1a\u8986\u76d6\u4e1a\u52a1\u4ee3\u7801\u3002<\/span><\/li>\n<li><span style=\"color:#0d0016\">BSP \u72ec\u7acb\u5171\u4eab&#xff1a;\u9a71\u52a8\u5e93\u5728\u5de5\u7a0b\u5916&#xff0c;\u88ab\u591a\u4e2a\u5de5\u7a0b\u590d\u7528&#xff0c;\u4e1a\u52a1\u5c42\u53ea\u4f9d\u8d56 BSP \u63a5\u53e3&#xff0c;\u4e0d\u76f4\u63a5\u78b0\u5bc4\u5b58\u5668\u3002<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u5355\u5411\u4f9d\u8d56&#xff1a;APP \u2192 BSP \u2192 HAL \u2192 \u786c\u4ef6&#xff0c;\u6bcf\u4e00\u5c42\u53ea\u8c03\u4e0b\u4e00\u5c42\u7684\u63a5\u53e3&#xff0c;\u6ca1\u6709\u53cd\u5411\u4f9d\u8d56\u3002\u8fd9\u79cd\u7ed3\u6784\u8ba9\u6362\u786c\u4ef6&#xff08;\u6bd4\u5982\u6362 I2C1&#xff09;\u3001\u6362\u82af\u7247\u90fd\u53ea\u52a8\u751f\u6210\u5c42\u3002<\/span><\/li>\n<hr \/>\n<h3><span style=\"color:#1a439c\">5. \u4ee3\u7801\u9010\u6a21\u5757\u6df1\u8bfb<\/span><\/h3>\n<h4><span style=\"color:#511b78\">5.1 main.c&#xff1a;\u542f\u52a8\u6d41\u7a0b\u9010\u884c<\/span><\/h4>\n<p>int main(void)<br \/>\n{<br \/>\n    HAL_Init();                \/\/ \u2460 \u590d\u4f4d\u6240\u6709\u5916\u8bbe\u3001\u521d\u59cb\u5316 Flash \u63a5\u53e3\u548c SysTick(1ms \u8282\u62cd)<br \/>\n    SystemClock_Config();      \/\/ \u2461 72MHz \u65f6\u949f\u6811<br \/>\n    MX_GPIO_Init();            \/\/ \u2462 PF8~11 \u5f00\u6f0f\u8f93\u51fa&#xff0c;\u521d\u59cb\u7535\u5e73\u5168\u90e8\u62c9\u4f4e(RESET)<br \/>\n    MX_I2C2_Init();            \/\/ \u2463 I2C2 400kHz&#xff0c;7 \u4f4d\u5730\u5740\u6a21\u5f0f<br \/>\n    MX_USART1_UART_Init();     \/\/ \u2464 USART1 115200<\/p>\n<p>    \/* USER CODE BEGIN 2 *\/    \/\/ \u2190 \u7528\u6237\u4ee3\u7801\u4ece\u8fd9\u91cc\u5f00\u59cb<br \/>\n    BSP_Usart_Init(&amp;huart1);   \/\/ \u2465 \u628a UART \u53e5\u67c4\u4ea4\u7ed9 BSP&#xff08;printf\/scanf \u7acb\u5373\u53ef\u7528&#xff09;<br \/>\n    BSP_AT24C02_Init(&amp;hi2c2);  \/\/ \u2466 \u63a2\u6d4b EEPROM \u662f\u5426\u5728\u7ebf<br \/>\n    \/* USER CODE END 2 *\/<\/p>\n<p>    while (1) {<br \/>\n        App_Init();            \/\/ \u2467 \u4e32\u53e3\u4ea4\u4e92&#xff1a;\u91cd\u65b0\u914d\u7f6e or \u8bfb\u56de\u73b0\u6709\u914d\u7f6e<br \/>\n        App_Run();             \/\/ \u2468 \u6821\u9a8c\u9b54\u6570 \u2192 \u5ef6\u65f6 \u2192 \u6309\u914d\u7f6e\u95ea\u706f<br \/>\n    }<br \/>\n}<\/p>\n<p><span style=\"color:#511b78\">\u51e0\u4e2a\u503c\u5f97\u6ce8\u610f\u7684\u70b9&#xff1a;<\/span><\/p>\n<ul>\n<li><span style=\"color:#b95514\">\u53e5\u67c4\u662f&#034;\u4ea4\u51fa\u53bb&#034;\u4e0d\u662f&#034;\u5305\u8d77\u6765&#034;<\/span><span style=\"color:#0d0016\">&#xff1a;BSP_Usart_Init(&amp;huart1) \u628a huart1 \u6307\u9488\u5b58\u5230 BSP \u5c42\u3002\u4ee5\u540e\u6362\u4e32\u53e3&#xff08;\u6bd4\u5982\u6539\u7528 USART2&#xff09;&#xff0c;\u53ea\u9700\u8981\u6539 main.c \u91cc\u8fd9\u4e00\u884c\u4f20\u53c2&#xff0c;BSP \u4ee3\u7801\u96f6\u6539\u52a8\u2014\u2014\u8fd9\u5c31\u662f&#034;\u4f9d\u8d56\u6ce8\u5165&#034;\u601d\u60f3\u5728\u5d4c\u5165\u5f0f\u91cc\u7684\u6734\u7d20\u5f62\u6001\u3002<\/span><\/li>\n<li><span style=\"color:#0d0016\">I2C2 \u521d\u59cb\u5316\u53c2\u6570&#xff1a;400kHz\u3001I2C_DUTYCYCLE_2&#xff08;Fast \u6a21\u5f0f\u4e0b\u9ad8\u4f4e\u7535\u5e73 2:1 \u5360\u7a7a\u6bd4&#xff09;\u3002HAL \u4f1a\u6839\u636e PCLK1&#061;36MHz \u81ea\u52a8\u8ba1\u7b97 CCR \u5206\u9891\u548c TRISE \u4e0a\u5347\u65f6\u95f4\u5bc4\u5b58\u5668&#xff0c;\u4e0d\u9700\u8981\u624b\u7b97\u3002<\/span><\/li>\n<li><span style=\"color:#0d0016\">USART1 \u7684\u7279\u6b8a\u914d\u7f6e&#xff08;\u91cd\u70b9&#xff09;&#xff1a;<\/span><\/li>\n<\/ul>\n<p>huart1.Init.BaudRate    &#061; 115200;<br \/>\nhuart1.Init.WordLength  &#061; UART_WORDLENGTH_9B;   \/\/ 9 \u4f4d\u5b57\u957f&#xff08;M&#061;1&#xff09;<br \/>\nhuart1.Init.Parity      &#061; UART_PARITY_ODD;      \/\/ \u5947\u6821\u9a8c\u4f7f\u80fd<\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0UART_WORDLENGTH_9B &#043; \u5947\u6821\u9a8c\u7684\u7ec4\u5408&#xff0c;\u5728\u7ebf\u4e0a\u5b9e\u9645\u662f 1 \u8d77\u59cb\u4f4d &#043; 8 \u6570\u636e\u4f4d &#043; 1 \u5947\u6821\u9a8c\u4f4d &#043; 1 \u505c\u6b62\u4f4d&#xff08;\u6821\u9a8c\u4f4d\u5360\u636e 9 \u4f4d\u5e27\u7684\u6700\u9ad8\u4f4d&#xff09;\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u4e5f\u5c31\u662f\u8bf4&#xff1a;\u4e32\u53e3\u52a9\u624b\u8981\u914d\u6210 8 \u4f4d\u6570\u636e &#043; \u5947\u6821\u9a8c&#xff08;8-O-1&#xff09;\u624d\u80fd\u5bf9\u4e0a&#xff0c;\u914d\u6210\u5e38\u89c1\u7684 8N1 \u4f1a\u6574\u4e2a\u5e27\u9519\u4f4d\u3001\u4e71\u7801\u3002<\/span><span style=\"color:#0d0016\">\u5982\u679c\u4f60\u4e0a\u4f4d\u673a\u534f\u8bae\u662f\u6807\u51c6 8N1&#xff0c;\u8fd9\u91cc\u8981\u6539\u56de UART_WORDLENGTH_8B &#043; UART_PARITY_NONE\u3002<\/span><\/p>\n<ul>\n<li><span style=\"color:#0d0016\">Error_Handler&#xff1a;\u4efb\u4f55\u521d\u59cb\u5316\u5931\u8d25\u90fd\u4f1a\u8fdb\u6765&#xff0c;__disable_irq() \u540e\u6b7b\u5faa\u73af\u2014\u2014\u5d4c\u5165\u5f0f\u91cc\u6700\u6734\u7d20\u7684&#034;\u5361\u6b7b\u62a5\u9519&#034;\u65b9\u5f0f&#xff0c;\u65b9\u4fbf\u8c03\u8bd5\u65f6\u4e00\u773c\u770b\u51fa\u521d\u59cb\u5316\u6ca1\u8fc7\u3002<\/span><\/li>\n<\/ul>\n<hr \/>\n<h4><span style=\"color:#511b78\">5.2 BSP_Usart_Redir&#xff1a;printf\/scanf \u91cd\u5b9a\u5411\u7684\u539f\u7406<\/span><\/h4>\n<p><span style=\"color:#0d0016\">\u8fd9\u662f\u6574\u4e2a\u5de5\u7a0b\u6700&#034;\u5999&#034;\u7684\u4e00\u5c42&#xff0c;\u53ea\u6709 40 \u884c&#xff1a;<\/span><\/p>\n<p>UART_HandleTypeDef *ghusart &#061; NULL;   \/\/ \u5168\u5c40\u4e32\u53e3\u53e5\u67c4&#xff08;\u5355\u4f8b&#xff09;<\/p>\n<p>void BSP_Usart_Init(UART_HandleTypeDef *husart) { ghusart &#061; husart; }<\/p>\n<p>int fputc(int ch, FILE *fp)           \/\/ printf(&#034;%d&#034;, x) \u6700\u7ec8\u4f1a\u8c03\u5b83<br \/>\n{<br \/>\n    UNUSED(fp);<br \/>\n    if (ghusart &amp;&amp; HAL_UART_Transmit(ghusart, (uint8_t *)&amp;ch, 1, HAL_MAX_DELAY) &#061;&#061; HAL_OK)<br \/>\n        return ch;<br \/>\n    return EOF;<br \/>\n}<\/p>\n<p>int fgetc(FILE *fp)                   \/\/ scanf(&#034;%d&#034;, &amp;x) \u6700\u7ec8\u4f1a\u8c03\u5b83<br \/>\n{<br \/>\n    UNUSED(fp);<br \/>\n    uint8_t ch;<br \/>\n    while (1) {<br \/>\n        if (HAL_UART_Receive(ghusart, &amp;ch, 1, HAL_MAX_DELAY) !&#061; HAL_OK)<br \/>\n            return EOF;<br \/>\n        if (ch !&#061; &#039;\\\\r&#039;)               \/\/ \u8fc7\u6ee4\u56de\u8f66\u7b26 \\\\r&#xff0c;\u53ea\u7559\u6709\u6548\u5b57\u7b26<br \/>\n            return (int)ch;<br \/>\n    }<br \/>\n}<\/p>\n<p><span style=\"color:#511b78\">\u539f\u7406\u62c6\u89e3&#xff1a;<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0C \u6807\u51c6\u5e93\u7684 printf \u5185\u90e8\u628a\u683c\u5f0f\u5316\u7ed3\u679c\u9010\u5b57\u7b26\u5199\u5230 stdout \u6d41&#xff0c;\u6d41\u7684\u5e95\u5c42\u5199\u51fd\u6570\u5c31\u662f fputc&#xff1b;scanf \u53cd\u8fc7\u6765\u4ece stdin \u6d41\u9010\u5b57\u7b26\u8bfb&#xff0c;\u5e95\u5c42\u662f fgetc\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u53ea\u8981\u5b9e\u73b0\u4e86\u8fd9\u4e24\u4e2a\u51fd\u6570&#xff0c;\u628a\u5b57\u8282\u5bfc\u5411 UART&#xff0c;\u6574\u4e2a\u5e94\u7528\u5c42\u5c31\u80fd\u7528 printf\/scanf \u505a\u4e32\u53e3\u4ea4\u4e92\u2014\u2014\u5e94\u7528\u5c42\u5b8c\u5168\u4e0d\u77e5\u9053 UART \u7684\u5b58\u5728\u3002\u8fd9\u6b63\u662f&#034;\u6807\u51c6\u5e93\u91cd\u5b9a\u5411&#xff08;retarget&#xff09;&#034;\u7684\u7ecf\u5178\u505a\u6cd5&#xff08;Keil \u4e0b\u901a\u5e38\u914d\u5408 MicroLIB \u4f7f\u7528&#xff09;\u3002<\/span><\/p>\n<\/p>\n<p><span style=\"color:#511b78\">\u4e09\u4e2a\u7ec6\u8282&#xff1a;<\/span><\/p>\n<li><span style=\"color:#0d0016\">HAL_MAX_DELAY &#061; \u963b\u585e\u7b49\u5f85\u53d1\u9001\u5b8c\u6210&#xff0c;printf \u662f\u540c\u6b65\u963b\u585e\u7684\u2014\u2014\u672c\u5de5\u7a0b\u4e32\u53e3\u53ea\u662f\u8c03\u8bd5\u4ea4\u4e92&#xff0c;\u6ca1\u95ee\u9898&#xff1b;\u5c06\u6765\u63a5\u5b9e\u65f6\u4efb\u52a1\u8c03\u5ea6&#xff08;FreeRTOS&#xff09;\u5c31\u8981\u6539\u6210\u4e2d\u65ad\/DMA &#043; \u4e92\u65a5\u9501\u3002<\/span><\/li>\n<li><span style=\"color:#0d0016\">fgetc \u91cc while(1) \u963b\u585e\u7b49\u4e00\u4e2a\u5b57\u8282\u2014\u2014scanf \u6ca1\u8f93\u5165\u65f6\u7a0b\u5e8f\u5c31\u505c\u5728\u90a3\u7b49&#xff0c;\u8fd9\u5c31\u662f\u83dc\u5355\u80fd&#034;\u5361\u4f4f\u7b49\u7528\u6237&#034;\u7684\u539f\u56e0\u3002<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u8fc7\u6ee4 &#039;\\\\r&#039;&#xff1a;\u4e32\u53e3\u52a9\u624b\u53d1\u9001\u65f6\u901a\u5e38\u5e26 \\\\r\\\\n&#xff0c;\u5982\u679c\u4e0d\u8fc7\u6ee4&#xff0c;scanf(&#034;%d&#034;) \u4e4b\u540e\u7684\u6b8b\u7559 \\\\r \u4f1a\u88ab\u5f53\u6210\u4e0b\u4e00\u4e2a\u8f93\u5165\u5b57\u7b26\u3002\u8fd9\u91cc\u53ea\u8fc7\u6ee4\u4e86 \\\\r&#xff0c;\\\\n \u4f1a\u7559\u5728\u7f13\u51b2\u533a\u2014\u2014\u4f46 scanf \u7684 %d \u4f1a\u81ea\u52a8\u8df3\u8fc7\u7a7a\u767d\u5b57\u7b26&#xff08;\u7a7a\u683c\u3001\\\\n\u3001\\\\t&#xff09;&#xff0c;\u6240\u4ee5\u4e0d\u5f71\u54cd\u3002<\/span><\/li>\n<p><span style=\"color:#511b78\">\u8bf7\u6c42\u51fd\u6570\u7684\u5c0f\u7ec6\u8282&#xff1a;<\/span><\/p>\n<p>static int Request_Input(const char *message)<br \/>\n{<br \/>\n    int choice &#061; 0;<br \/>\n    printf(&#034;&gt;&gt;%s\\\\n&#034;, message);<br \/>\n    if (scanf(&#034;%d&#034;, &amp;choice) &#061;&#061; 1)   \/\/ scanf \u8fd4\u56de\u503c &#061; \u6210\u529f\u8f6c\u6362\u7684\u53c2\u6570\u4e2a\u6570<br \/>\n        return choice;<br \/>\n    printf(&#034;\u9009\u62e9\u9519\u8bef&#xff01;\u8bf7\u91cd\u65b0\u8f93\u5165&#xff01;&#034;);<br \/>\n    return -1;                       \/\/ \u8f93\u5165\u4e86\u975e\u6570\u5b57 \u2192 \u8fd4\u56de -1<br \/>\n}<\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0scanf \u8fd4\u56de 0 \u8868\u793a&#034;\u4e00\u4e2a\u90fd\u6ca1\u8f6c\u51fa\u6765&#034;&#xff08;\u6bd4\u5982\u8f93\u5165\u4e86\u5b57\u6bcd abc&#xff09;&#xff0c;\u6b64\u65f6\u8fd4\u56de -1\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u800c\u8c03\u7528\u5904 uint8_t choice &#061; Request_Input(&#8230;) \u628a -1 \u585e\u8fdb uint8_t \u53d8\u6210 255&#xff0c;255 !&#061; 1 &amp;&amp; 255 !&#061; 2&#xff0c;\u8d70 else \u5206\u652f\u6253\u5370\u9519\u8bef\u2014\u2014\u9519\u8bef\u8f93\u5165\u88ab&#034;\u65e0\u610f\u4e2d&#034;\u515c\u4f4f\u4e86&#xff0c;\u4f46\u8fd9\u5c5e\u4e8e\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u7684\u5de7\u5408&#xff0c;\u89c1\u7b2c 7 \u8282\u3002<\/span><\/p>\n<hr \/>\n<h4><span style=\"color:#511b78\">5.3 BSP_LED&#xff1a;\u63a7\u5236\u4e0e\u95ea\u706f<\/span><\/h4>\n<p>void BSP_LED_Control(LED_t led, bool flag)<br \/>\n{<br \/>\n    GPIO_PinState state &#061; flag ? GPIO_PIN_RESET : GPIO_PIN_SET;  \/\/ ON &#061; \u4f4e\u7535\u5e73<br \/>\n    switch (led) {<br \/>\n        case LED1: HAL_GPIO_WritePin(GPIOF, GPIO_PIN_8,  state); break;<br \/>\n        case LED2: HAL_GPIO_WritePin(GPIOF, GPIO_PIN_9,  state); break;<br \/>\n        case LED3: HAL_GPIO_WritePin(GPIOF, GPIO_PIN_10, state); break;<br \/>\n        case LED4: HAL_GPIO_WritePin(GPIOF, GPIO_PIN_11, state); break;<br \/>\n        default:   break;<br \/>\n    }<br \/>\n}<\/p>\n<p><span style=\"color:#0d0016\">flag ? GPIO_PIN_RESET : GPIO_PIN_SET \u8fd9\u884c\u662f\u6781\u6027\u53cd\u8f6c&#xff1a;<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u903b\u8f91\u5c42\u8bf4&#034;ON&#034;&#xff0c;\u7269\u7406\u5c42\u5199\u4f4e\u7535\u5e73\u2014\u2014\u628a&#034;\u4f4e\u7535\u5e73\u70b9\u4eae&#034;\u8fd9\u4e2a\u786c\u4ef6\u4e8b\u5b9e\u5c01\u88c5\u5728 BSP \u5185\u90e8&#xff0c;\u4e0a\u5c42\u6c38\u8fdc\u53ea\u8bf4&#034;\u4eae\/\u706d&#034;&#xff0c;\u4e0d\u61c2\u7535\u5b66\u4e5f\u80fd\u7528\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u4ee5\u540e\u7535\u8def\u6539\u6210\u9ad8\u7535\u5e73\u70b9\u4eae&#xff0c;\u53ea\u6539\u8fd9\u4e00\u4e2a\u51fd\u6570\u3002<\/span><\/p>\n<hr \/>\n<p><span style=\"color:#511b78\">\u95ea\u706f\u51fd\u6570 LED_Blink_Version2(led, cnt, delay) \u5206\u4e24\u8def&#xff1a;<\/span><\/p>\n<p>if (led &#061;&#061; 0) {          \/\/ \u5168\u9009&#xff1a;4 \u9897\u706f\u4e00\u8d77\u95ea<br \/>\n    LED_t leds[] &#061; {LED1, LED2, LED3, LED4};<br \/>\n    int leds_num &#061; sizeof(leds) \/ sizeof(leds[0]);   \/\/ &#061; 4&#xff0c;\u6570\u7ec4\u5143\u7d20\u4e2a\u6570<br \/>\n    do {<br \/>\n        \/\/ \u5168\u90e8\u70b9\u4eae \u2192 HAL_Delay(delay) \u2192 \u5168\u90e8\u7184\u706d \u2192 HAL_Delay(delay)<br \/>\n    } while (cnt&#8211;);<br \/>\n} else {                 \/\/ \u5355\u9009&#xff1a;\u95ea cnt \u6b21<br \/>\n    for (int i &#061; 0; i &lt; cnt; i&#043;&#043;) { \/* \u4eae delay \u2192 \u706d delay *\/ }<br \/>\n}<\/p>\n<hr \/>\n<h4><span style=\"color:#511b78\">5.4 BSP_AT24C02&#xff1a;EEPROM \u9a71\u52a8\u5168\u89e3\u6790&#xff08;\u5de5\u7a0b\u6838\u5fc3&#xff09;<\/span><\/h4>\n<h5><span style=\"color:#511b78\">\u6570\u636e\u7ed3\u6784&#xff1a;\u4e00\u4e2a&#034;\u8bbe\u5907\u5bf9\u8c61&#034;<\/span><\/h5>\n<p>typedef struct {<br \/>\n    I2C_HandleTypeDef *hi2c;   \/\/ \u6307\u5411 I2C \u53e5\u67c4&#xff08;\u54ea\u6761 I2C \u603b\u7ebf&#xff09;<br \/>\n    uint16_t dev_address;      \/\/ 8 \u4f4d\u8bbe\u5907\u5730\u5740&#xff08;0xA0&#xff09;<br \/>\n    uint8_t page_size;         \/\/ \u9875\u5927\u5c0f 8<br \/>\n    bool is_init;              \/\/ \u521d\u59cb\u5316\u662f\u5426\u6210\u529f&#xff08;EEPROM \u662f\u5426\u5728\u7ebf&#xff09;<br \/>\n} eeprom_t;<\/p>\n<p>eeprom_t at24c02;              \/\/ \u5168\u5c40\u5355\u4f8b\u5b9e\u4f8b<\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u628a&#034;\u8bbe\u5907\u5c5e\u6027&#034;\u6253\u5305\u6210\u4e00\u4e2a\u7ed3\u6784\u4f53\u3001\u7528\u4e00\u4e2a\u5168\u5c40\u5b9e\u4f8b\u6301\u6709&#xff0c;\u662f\u5d4c\u5165\u5f0f\u9a71\u52a8\u6700\u5e38\u89c1\u7684\u5f62\u5f0f\u3002\u5c40\u9650\u662f\u5355\u4f8b\u4e0d\u652f\u6301\u6302\u591a\u7247 EEPROM&#xff08;\u6539\u8fdb\u89c1\u7b2c 8 \u8282&#xff09;\u3002<\/span><\/p>\n<hr \/>\n<h5><span style=\"color:#511b78\">\u521d\u59cb\u5316&#xff1a;\u63a2\u6d4b &#043; \u8bb0\u5f55\u5c5e\u6027<\/span><\/h5>\n<p>bool BSP_AT24C02_Init(I2C_HandleTypeDef *hi2c)<br \/>\n{<br \/>\n    if (!hi2c) return false;<br \/>\n    at24c02.hi2c &#061; hi2c;<br \/>\n    at24c02.dev_address &#061; DEV_ADDRESS &lt;&lt; 1;   \/\/ 0x50 \u2192 0xA0<br \/>\n    at24c02.page_size &#061; PAGE_SIZE;            \/\/ 8<br \/>\n    at24c02.is_init &#061; false;<br \/>\n    if (HAL_OK &#061;&#061; HAL_I2C_IsDeviceReady(at24c02.hi2c, at24c02.dev_address,<br \/>\n                                        SCAN_RETYIES, SCAN_TIME_MS)) {<br \/>\n        at24c02.is_init &#061; true;               \/\/ \u6536\u5230 ACK &#061; \u5668\u4ef6\u5728\u7ebf<br \/>\n    }<br \/>\n    return at24c02.is_init;<br \/>\n}<\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u6240\u6709\u63a5\u53e3\u5f00\u5934\u90fd\u68c0\u67e5 is_init\u2014\u2014\u6ca1\u521d\u59cb\u5316\u6210\u529f\u5c31\u62d2\u7edd\u8bfb\u5199&#xff0c;\u907f\u514d\u5bf9\u4e0d\u5b58\u5728\u7684\u5668\u4ef6\u7a7a\u8f6c\u8d85\u65f6\u3002<\/span><\/p>\n<hr \/>\n<h5><span style=\"color:#511b78\">\u8bfb&#xff1a;\u8fb9\u754c\u68c0\u67e5 &#043; Mem_Read<\/span><\/h5>\n<p>bool BSP_AT24C02_Read(uint16_t start_address, void *data_out, uint8_t data_len)<br \/>\n{<br \/>\n    if (!at24c02.is_init || !data_out || data_len &#061;&#061; 0) return false;<br \/>\n    if (start_address &#043; data_len &gt; DEV_TOTAL_SIZE) return false;   \/\/ \u8d8a\u754c\u4fdd\u62a4<br \/>\n    HAL_StatusTypeDef state &#061; HAL_I2C_Mem_Read(<br \/>\n        at24c02.hi2c, at24c02.dev_address, start_address,<br \/>\n        I2C_MEMADD_SIZE_8BIT, data_out, data_len, READ_TIMEOUT_MS);<br \/>\n    return state &#061;&#061; HAL_OK;<br \/>\n}<\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u8bfb\u64cd\u4f5c\u6ca1\u6709\u9875\u9650\u5236&#xff08;EEPROM \u53ef\u4ee5\u8de8\u9875\u8fde\u7eed\u8bfb&#xff09;&#xff0c;\u6240\u4ee5\u53ea\u6709\u4e00\u4e2a\u603b\u5bb9\u91cf\u8d8a\u754c\u68c0\u67e5\u3002<\/span><\/p>\n<hr \/>\n<h5><span style=\"color:#511b78\">\u5199&#xff1a;\u5355\u9875\u5199 &#043; \u81ea\u52a8\u7ffb\u9875&#xff08;\u91cd\u70b9\u7b97\u6cd5&#xff09;<\/span><\/h5>\n<p><span style=\"color:#b95514\">\u4e3a\u4ec0\u4e48\u4e0d\u80fd\u4e00\u628a\u68ad\u5168\u5199\u8fdb\u53bb&#xff1a;<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0AT24C02 \u7684\u5199\u5468\u671f\u9650\u5236\u662f&#034;\u4e00\u6b21\u6700\u591a\u5199\u4e00\u9875&#xff08;8 \u5b57\u8282&#xff09;\u4e14\u4e0d\u80fd\u8de8\u9875&#034;\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u4ece\u5730\u5740 5 \u5f00\u59cb\u5199 6 \u5b57\u8282&#xff0c;\u4f1a\u8de8\u8fc7\u5730\u5740 7 \u8fdb\u5165\u4e0b\u4e00\u9875\u2014\u2014\u8fd9\u79cd\u8de8\u9875\u5199\u5728\u5668\u4ef6\u5185\u90e8\u4f1a\u51fa\u9519&#xff08;\u8981\u4e48\u88ab\u5ffd\u7565&#xff0c;\u8981\u4e48\u6570\u636e\u9519\u4e71&#xff09;\u3002\u6240\u4ee5\u9a71\u52a8\u5fc5\u987b\u81ea\u5df1\u628a\u5927\u5757\u6570\u636e\u62c6\u6210&#034;\u6bcf\u6bb5\u4e0d\u8d85\u8fc7\u5f53\u524d\u9875\u5269\u4f59\u7a7a\u95f4&#034;\u7684\u5c0f\u6bb5\u3002<\/span><\/p>\n<hr \/>\n<p><span style=\"color:#b95514\">\u5355\u9875\u5199&#xff1a;<\/span><\/p>\n<p>\/\/ \u5355\u2eda&#xff0c;\u2eda\u5185\u5199\u2f0a<br \/>\nstatic bool BSP_AT24C02_Write_Page(uint16_t start_address, void *data_in,<br \/>\n                                   uint8_t data_len)<br \/>\n{<br \/>\n    if (!at24c02.is_init || !data_in || data_len &#061;&#061; 0)<br \/>\n        return false;<br \/>\n    \/\/ \u68c0\u67e5\u5199\u2f0a\u662f\u5426\u8de8\u2eda<br \/>\n    \/\/ \u5f53\u524d\u5199\u2f0a\u5230\u90a3\u2f00\u2eda<br \/>\n    \/\/ uint16_t page_num &#061; start_address \/ at24c02.page_size; \/\/ \u4e0d\u91cd\u8981<br \/>\n    uint16_t page_offset &#061; start_address % at24c02.page_size; \/\/ \u786e\u5b9a\u7684\u2f00\u2eda&#xff0c;\u5199\u2f0a\u4f4d\u7f6e\u7684\u5f00\u59cb\u4e0b\u6807&#xff01;<br \/>\n    if (page_offset &#043; data_len &gt; at24c02.page_size)<br \/>\n    {<br \/>\n        return false;<br \/>\n    }<br \/>\n    HAL_StatusTypeDef state &#061; HAL_I2C_Mem_Write(<br \/>\n        at24c02.hi2c,<br \/>\n        at24c02.dev_address,<br \/>\n        start_address,<br \/>\n        I2C_MEMADD_SIZE_8BIT,<br \/>\n        data_in,<br \/>\n        data_len,<br \/>\n        WRITE_TIMEOUT_MS);<br \/>\n    if (state !&#061; HAL_OK)<br \/>\n        return false;<br \/>\n    return BSP_AT24C02_Ready();<br \/>\n}<\/p>\n<p><span style=\"color:#b95514\">\u5916\u5c42\u7ffb\u9875\u5faa\u73af&#xff1a;<\/span><\/p>\n<p>\/\/ \u2f40\u6301\u4efb\u610f\u2ed3\u5ea6\u7684\u5199\u2f0a&#xff0c;\u2f83\u52a8\u8de8\u2eda<br \/>\nbool BSP_AT24C02_Write(uint16_t start_address, void *data_in, uint8_t data_len)<br \/>\n{<br \/>\n    if (!at24c02.is_init || !data_in || data_len &#061;&#061; 0)<br \/>\n        return false;<br \/>\n    if (start_address &#043; data_len &gt; DEV_TOTAL_SIZE)\/\/\u8d85\u8fc7\u5168\u90e8\u9875\u7684\u50a8\u91cf<br \/>\n        return false;<br \/>\n    uint16_t bytes_written &#061; 0; \/\/ \u5df2\u7ecf\u5199\u2f0a\u4e86\u591a\u5c11\u4e2a\u5b57\u8282<\/p>\n<p>    while (bytes_written &lt; data_len)\/\/\u672a\u5b8c\u6210\u5199<br \/>\n    {<br \/>\n        \/\/ \u6570\u636e\u8fd8\u6709\u591a\u5c11\u6ca1\u6709\u53d1\u9001<br \/>\n        uint16_t data_left &#061; data_len &#8211; bytes_written;<br \/>\n        \/\/ \u5fc5\u987b\u6309\u7167\u2eda\u6765\u5199&#xff0c;\u5f53\u524d\u2eda\u5269\u4f59\u591a\u5c11\u7a7a\u95f4<br \/>\n        uint16_t new_start_address &#061; start_address &#043; bytes_written;\/\/\u7ffb\u9875\u903b\u8f91<br \/>\n        \/\/ uint16_t current_page_num &#061; new_start_address \/ at24c02.page_size;<br \/>\n        uint16_t page_offset &#061; new_start_address % at24c02.page_size;\/\/\u5df2\u7ecf\u5360\u7528\u7684\u7a7a\u95f4<br \/>\n        uint16_t page_space &#061; at24c02.page_size &#8211; page_offset;\/\/\u5269\u4f59\u7a7a\u95f4<br \/>\n        \/\/ \u6570\u636e\u8fd8\u6709\u591a\u5c11\u6ca1\u6709\u53d1\u9001 -&gt; \u5f53\u524d\u2eda\u5269\u4f59\u591a\u5c11\u7a7a\u95f4<br \/>\n        \/\/ \u2f00\u5b9a\u80fd\u5199\u2f0a\u5f53\u524d\u2eda\u7684\u5269\u4f59\u7a7a\u95f4\u5417&#xff1f;\u4e0d\u2f00\u5b9a&#xff01;&#xff01;<br \/>\n        \/\/ \u8ba1\u7b97\u672c\u6b21\u5199\u2f0a\u91cf<br \/>\n        \/\/ 1. \u6570\u636e\u8fd8\u6709\u591a\u5c11\u6ca1\u6709\u53d1\u9001 &lt;&#061; \u5f53\u524d\u2eda\u5269\u4f59\u591a\u5c11\u7a7a\u95f4 &#061; \u6570\u636e\u8fd8\u6709\u591a\u5c11<br \/>\n        \/\/ 2. \u6570\u636e\u8fd8\u6709\u591a\u5c11\u6ca1\u6709\u53d1\u9001 &gt;  \u5f53\u524d\u2eda\u5269\u4f59\u591a\u5c11\u7a7a\u95f4 &#061; \u5269\u4f59\u591a\u5c11\u7a7a\u95f4<br \/>\n        uint16_t write_bytes &#061; data_left;<br \/>\n        if (write_bytes &gt; page_space)<br \/>\n        {<br \/>\n            write_bytes &#061; page_space;\/\/\u53ef\u4ee5\u5199\u4e14\u4e0d\u8de8\u9875<br \/>\n        }<br \/>\n        if (!BSP_AT24C02_Write_Page(new_start_address,(uint8_t *)data_in &#043; bytes_written, write_bytes))<br \/>\n        {<br \/>\n            return false;\/\/\u5355\u9875\u5199\u6ca1\u6210\u529f<br \/>\n        }<br \/>\n        bytes_written &#043;&#061; write_bytes;\/\/\u628a\u5f53\u9875\u80fd\u5199\u7684\u52a0\u5165&#xff0c;\u518d\u6b21\u5faa\u73af&#xff0c;\u7ffb\u9875\u5199<br \/>\n    }<br \/>\n    return true;<br \/>\n}<\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u7b97\u6cd5\u6838\u5fc3\u4e00\u53e5\u8bdd&#xff1a;\u6bcf\u6b21\u5faa\u73af\u53ea\u5199&#034;\u5f53\u524d\u9875\u5269\u4f59\u7a7a\u95f4&#034;\u8fd9\u4e48\u957f&#xff0c;\u5199\u5b8c\u63a8\u8fdb\u6307\u9488\u8fdb\u5165\u4e0b\u4e00\u9875&#xff0c;\u76f4\u5230\u6570\u636e\u5168\u90e8\u5199\u5b8c\u3002\u8c03\u7528\u65b9\u4e0d\u9700\u8981\u77e5\u9053\u9875\u8fb9\u754c&#xff0c;\u9a71\u52a8\u5185\u90e8\u900f\u660e\u5904\u7406\u2014\u2014\u8fd9\u662f EEPROM \u9a71\u52a8\u8bbe\u8ba1\u7684\u5173\u952e\u5c01\u88c5\u3002<\/span><\/p>\n<hr \/>\n<p><span style=\"color:#511b78\">\u5177\u4f53\u8fc7\u7a0b&#xff08;\u4ee5&#034;\u4ece\u5730\u5740 5 \u5199 10 \u5b57\u8282&#034;\u4e3a\u4f8b&#xff09;\u89c1\u4e0b\u56fe&#xff1a;<\/span><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"304\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260917032513-6aab5d9902fcb.png\" width=\"860\" \/><\/p>\n<p><span style=\"color:#511b78\">\u518d\u7ed9\u4e00\u5f20\u8fed\u4ee3\u8ddf\u8e2a\u8868&#xff08;start&#061;5, len&#061;10&#xff09;&#xff0c;\u628a\u5faa\u73af\u8dd1\u4e00\u904d&#xff1a;<\/span><\/p>\n<table>\n<tr><span style=\"color:#0d0016\">\u8fed\u4ee3<\/span><span style=\"color:#0d0016\">bytes_written<\/span><span style=\"color:#0d0016\">new_addr<\/span><span style=\"color:#0d0016\">page_offset<\/span><span style=\"color:#0d0016\">page_space<\/span><span style=\"color:#0d0016\">data_left<\/span><span style=\"color:#0d0016\">write_bytes<\/span><span style=\"color:#0d0016\">\u5199\u5165\u8303\u56f4<\/span><\/tr>\n<tbody>\n<tr>\n<td><span style=\"color:#0d0016\">1<\/span><\/td>\n<td style=\"width:97px\"><span style=\"color:#0d0016\">0<\/span><\/td>\n<td style=\"width:104px\"><span style=\"color:#0d0016\">5<\/span><\/td>\n<td><span style=\"color:#0d0016\">5<\/span><\/td>\n<td><span style=\"color:#0d0016\">3<\/span><\/td>\n<td><span style=\"color:#0d0016\">10<\/span><\/td>\n<td><span style=\"color:#0d0016\">3<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u5730\u5740 5~7<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">2<\/span><\/td>\n<td style=\"width:97px\"><span style=\"color:#0d0016\">3<\/span><\/td>\n<td style=\"width:104px\"><span style=\"color:#0d0016\">8<\/span><\/td>\n<td><span style=\"color:#0d0016\">0<\/span><\/td>\n<td><span style=\"color:#0d0016\">8<\/span><\/td>\n<td><span style=\"color:#0d0016\">7<\/span><\/td>\n<td><span style=\"color:#0d0016\">7<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u5730\u5740 8~14<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">\u2014<\/span><\/td>\n<td style=\"width:97px\"><span style=\"color:#0d0016\">10 &#061; data_len \u2192 \u5faa\u73af\u7ed3\u675f<\/span><\/td>\n<td style=\"width:104px\"><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u800c\u672c\u5de5\u7a0b\u5b9e\u9645\u8c03\u7528 SAVE_CONFIG \u65f6&#xff0c;CONFIG_ADDRESS&#061;0x00\u3001len&#061;8&#xff0c;\u6070\u597d 8 \u5b57\u8282\u6b63\u597d\u4e00\u9875\u2014\u2014page_offset&#061;0\u3001page_space&#061;8\u3001write_bytes&#061;8&#xff0c;\u5faa\u73af\u4e00\u6b21\u5c31\u5b8c\u6210&#xff0c;\u8fde\u7ffb\u9875\u90fd\u4e0d\u9700\u8981\u3002\u8fd9\u4e5f\u662f\u628a\u914d\u7f6e\u653e\u5728 0x00 \u5730\u5740\u7684\u5de7\u5999\u4e4b\u5904&#xff1a;\u521a\u597d\u5bf9\u9f50\u9875\u8fb9\u754c\u3002<\/span><\/p>\n<hr \/>\n<h4><span style=\"color:#511b78\">5.5 APP \u5c42&#xff1a;\u914d\u7f6e\u5bf9\u8c61\u3001\u4ea4\u4e92\u3001\u9b54\u6570\u6821\u9a8c<\/span><\/h4>\n<h5><span style=\"color:#511b78\">config_t \u7684\u5185\u5b58\u5e03\u5c40<\/span><\/h5>\n<p>typedef struct _config_t {<br \/>\n    uint8_t  magic;       \/\/ \u504f\u79fb 0&#xff1a;\u9b54\u6570 0x35<br \/>\n    uint8_t  version;     \/\/ \u504f\u79fb 1&#xff1a;\u7248\u672c 0x01<br \/>\n    uint8_t  led_id;      \/\/ \u504f\u79fb 2&#xff1a;\u706f\u5e8f\u53f7&#xff08;0&#061;\u5168\u9009&#xff09;<br \/>\n    uint8_t  repeat_cnt;  \/\/ \u504f\u79fb 3&#xff1a;\u95ea\u706f\u6b21\u6570<br \/>\n    uint16_t delay_ms;    \/\/ \u504f\u79fb 4&#xff1a;\u95ea\u70c1\u95f4\u9694<br \/>\n    uint16_t start_time;  \/\/ \u504f\u79fb 6&#xff1a;\u542f\u52a8\u5ef6\u65f6<br \/>\n} config_t;               \/\/ sizeof &#061; 8 \u5b57\u8282&#xff0c;\u65e0\u586b\u5145\u5b57\u8282<\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0uint16_t \u9700\u8981 2 \u5b57\u8282\u5bf9\u9f50&#xff0c;\u7f16\u8bd1\u5668\u628a\u5b83\u6392\u5230\u504f\u79fb 4 \u548c 6&#xff0c;\u524d 4 \u4e2a uint8_t \u5360 0~3\u2014\u2014\u5929\u7136\u7d27\u51d1&#xff0c;\u6b63\u597d 8 \u5b57\u8282&#xff0c;\u6070\u597d\u586b\u6ee1 AT24C02 \u7684\u4e00\u9875\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u8fd9\u4e2a&#034;\u7ed3\u6784\u4f53\u76f4\u63a5\u843d\u76d8&#034;\u7684\u65b9\u5f0f\u4f9d\u8d56\u7f16\u8bd1\u5668\u4e0d\u63d2\u586b\u5145\u5b57\u8282&#xff0c;\u5982\u679c\u5c06\u6765\u5728\u4e2d\u95f4\u52a0\u4e00\u4e2a uint8_t \u5b57\u6bb5&#xff0c;sizeof \u53ef\u80fd\u53d8\u6210 10&#xff0c;\u8de8\u9875\u903b\u8f91\u5c31\u4f1a\u89e6\u53d1\u2014\u2014\u8fd9\u662f\u7ed3\u6784\u4f53\u76f4\u63a5\u6301\u4e45\u5316\u7684\u4e00\u4e2a\u9690\u60a3\u3002<\/span><\/p>\n<hr \/>\n<h5><span style=\"color:#511b78\">\u8bfb\u5199\u5b8f<\/span><\/h5>\n<p>#define SAVE_CONFIG(conf_addr, conf_len) BSP_AT24C02_Write(CONFIG_ADDRESS, conf_addr, conf_len)<br \/>\n#define LOAD_CONFIG(conf_addr, conf_len) BSP_AT24C02_Read(CONFIG_ADDRESS, conf_addr, conf_len)<br \/>\n\/\/   BSP_AT24C02_Write(0x00, &amp;gconfig, 8)<br \/>\n\/\/   BSP_AT24C02_Read(0x00, &amp;gconfig, 8)<\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u5b8f\u628a&#034;\u914d\u7f6e\u5b58\u50a8\u5728\u5730\u5740 0x00&#034;\u8fd9\u4e2a\u7ea6\u5b9a\u7edf\u4e00\u6536\u53e3&#xff0c;\u4e1a\u52a1\u4ee3\u7801\u91cc\u53ea\u5199 SAVE_CONFIG(&amp;gconfig, sizeof(gconfig))&#xff0c;\u6539\u5b58\u50a8\u4f4d\u7f6e\u53ea\u52a8\u4e00\u5904\u3002<\/span><\/p>\n<hr \/>\n<h5><span style=\"color:#511b78\">App_Init&#xff1a;\u4ea4\u4e92\u5f0f\u914d\u7f6e<\/span><\/h5>\n<p>void App_Init(void)<br \/>\n{<br \/>\n    uint8_t choice &#061; Request_Input(&#034;1.\u91cd\u65b0\u914d\u7f6e\u53c2\u6570 2.\u4f7f\u7528\u73b0\u5728\u7684\u914d\u7f6e&#034;);<br \/>\n    if (choice &#061;&#061; 1) {<br \/>\n        gconfig.magic      &#061; APP_MAGIC;    \/\/ 0x35&#xff0c;\u5199\u524d\u5c31\u6253\u4e0a\u6807\u7b7e<br \/>\n        gconfig.version    &#061; APP_VERSION;  \/\/ 0x01<br \/>\n        gconfig.led_id     &#061; (uint8_t)Request_Input(&#034;step1:\u9009\u62e9LED\u30101-4\u3011,0\u8868\u793a\u5168\u9009&gt;&gt; &#034;);<br \/>\n        gconfig.repeat_cnt &#061; (uint8_t)Request_Input(&#034;step2:\u8bbe\u7f6e\u95ea\u706f\u6b21\u6570,\u30101-255\u3011&gt;&gt; &#034;);<br \/>\n        gconfig.delay_ms   &#061; (uint16_t)Request_Input(&#034;step3:\u9009\u62e9\u5ef6\u8fdf\u95f4\u9694,\u5355\u4f4dms&gt;&gt; &#034;);<br \/>\n        gconfig.start_time &#061; (uint16_t)Request_Input(&#034;step4:\u9009\u62e9\u5f00\u59cb\u65f6\u95f4.\u5355\u4f4dms&gt;&gt; &#034;);<br \/>\n        Config_Display();<br \/>\n        SAVE_CONFIG(&amp;gconfig, sizeof(gconfig));   \/\/ \u5199\u5165 EEPROM<br \/>\n        printf(&#034;&gt;&gt; write config done &lt;&lt;\\\\n&#034;);<br \/>\n    } else if (choice &#061;&#061; 2) {<br \/>\n        LOAD_CONFIG(&amp;gconfig, sizeof(gconfig));   \/\/ \u8bfb\u56de EEPROM<br \/>\n        printf(&#034;&gt;&gt; load config done &lt;&lt;\\\\n&#034;);<br \/>\n    } else {<br \/>\n        printf(&#034;\u9009\u62e9\u9519\u8bef&#xff01;\u8bf7\u91cd\u65b0\u8f93\u5165&#xff01;&#034;);<br \/>\n    }<br \/>\n}<\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u6ce8\u610f magic \u548c version \u662f\u5199\u524d\u5148\u586b\u597d\u7684\u2014\u2014\u5b83\u4e0d\u662f\u7528\u6237\u8f93\u5165\u9879&#xff0c;\u662f\u9a71\u52a8\u5c42\u6253\u7684&#034;\u6570\u636e\u6807\u7b7e&#034;\u3002\u7528\u6237\u53ea\u8f93\u5165 4 \u4e2a\u4e1a\u52a1\u53c2\u6570&#xff08;\u706f\u3001\u6b21\u6570\u3001\u95f4\u9694\u3001\u5ef6\u65f6&#xff09;\u3002<\/span><\/p>\n<hr \/>\n<h5><span style=\"color:#511b78\">App_Run&#xff1a;\u9b54\u6570\u6821\u9a8c &#043; \u6267\u884c<\/span><\/h5>\n<p>void App_Run(void)<br \/>\n{<br \/>\n    if (gconfig.magic &#061;&#061; APP_MAGIC &amp;&amp; gconfig.version &#061;&#061; APP_VERSION) {<br \/>\n        printf(&#034;&gt;&gt; %d ms\u4e4b\u540e, \u5f00\u59cb\u95ea\u706f\\\\n&#034;, (int)gconfig.start_time);<br \/>\n        HAL_Delay(gconfig.start_time);                 \/\/ \u5ef6\u65f6\u542f\u52a8&#xff08;\u963b\u585e&#xff09;<br \/>\n        printf(&#034;&gt;&gt; \u95ea\u706f\u4e2d&#8230;\\\\n&#034;);<br \/>\n        LED_Blink_Version2((LED_t)gconfig.led_id, gconfig.repeat_cnt, gconfig.delay_ms);<br \/>\n    }<br \/>\n    \/\/ \u6821\u9a8c\u4e0d\u8fc7 \u2192 \u4ec0\u4e48\u90fd\u4e0d\u505a&#xff08;\u5b89\u5168\u964d\u7ea7&#xff09;<br \/>\n}<\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u9b54\u6570\u6821\u9a8c\u662f APP \u7684\u7cbe\u534e&#xff1a;\u5168\u65b0 EEPROM \u5185\u5bb9\u5168\u662f 0xFF&#xff0c;\u5982\u679c\u76f4\u63a5\u62ff 0xFF \u5f53 repeat_cnt&#xff08;255 \u6b21&#xff09;\u548c delay_ms&#xff08;255ms&#xff09;\u7528&#xff0c;\u4f1a\u51fa\u73b0&#034;\u5e7d\u7075\u95ea\u706f&#034;\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0magic &#061;&#061; 0x35 \u6821\u9a8c\u628a&#034;\u5b58\u8fc7\u6709\u6548\u914d\u7f6e&#034;\u548c&#034;\u7a7a\u7247\/\u6570\u636e\u635f\u574f&#034;\u533a\u5206\u5f00&#xff0c;\u6821\u9a8c\u4e0d\u8fc7\u5c31\u9759\u9ed8\u4e0d\u95ea\u2014\u2014\u5b81\u53ef\u4ec0\u4e48\u90fd\u4e0d\u505a&#xff0c;\u4e5f\u4e0d\u8981\u7528\u9519\u8bef\u6570\u636e\u9a71\u52a8\u786c\u4ef6\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0version \u662f\u7ed9\u5c06\u6765\u683c\u5f0f\u5347\u7ea7\u7559\u7684\u517c\u5bb9\u4f4d\u3002<\/span><\/p>\n<hr \/>\n<h3><span style=\"color:#1a439c\">6. \u5b8c\u6574\u8fd0\u884c\u65f6\u5e8f&#xff1a;\u628a\u4e24\u6761\u6d41\u7a0b\u8d70\u4e00\u904d<\/span><\/h3>\n<p><span style=\"color:#511b78\">\u573a\u666f A&#xff1a;\u9996\u6b21\u4e0a\u7535 &#043; \u914d\u7f6e\u5e76\u4fdd\u5b58<\/span><\/p>\n<p>\u4e0a\u7535 \u2192 HAL\/\u65f6\u949f\/\u5916\u8bbe\u521d\u59cb\u5316 \u2192 EEPROM \u63a2\u6d4b\u6210\u529f<br \/>\n     \u2192 App_Init: \u6253\u5370\u83dc\u5355 \u2192 \u7528\u6237\u9009 1<br \/>\n     \u2192 \u4f9d\u6b21 scanf 4 \u4e2a\u53c2\u6570 \u2192 \u586b gconfig(magic&#061;0x35)<br \/>\n     \u2192 SAVE_CONFIG: I2C2 \u5199 8 \u5b57\u8282\u5230 0x00 \u2192 \u8f6e\u8be2\u7b49 tWR \u7ed3\u675f<br \/>\n     \u2192 App_Run: magic \u6821\u9a8c\u901a\u8fc7 \u2192 \u5ef6\u65f6 start_time ms \u2192 \u95ea\u706f<\/p>\n<p><span style=\"color:#511b78\">\u573a\u666f B&#xff1a;\u65ad\u7535\u91cd\u542f&#xff0c;\u4f7f\u7528\u4fdd\u5b58\u7684\u914d\u7f6e<\/span><\/p>\n<p>\u4e0a\u7535 \u2192 \u521d\u59cb\u5316 \u2192 EEPROM \u63a2\u6d4b\u6210\u529f<br \/>\n     \u2192 App_Init: \u6253\u5370\u83dc\u5355 \u2192 \u7528\u6237\u9009 2<br \/>\n     \u2192 LOAD_CONFIG: I2C2 \u8bfb 8 \u5b57\u8282 \u2192 gconfig<br \/>\n     \u2192 App_Run: magic&#061;&#061;0x35 &amp;&amp; version&#061;&#061;0x01 \u2192 \u5ef6\u65f6 \u2192 \u6309\u53c2\u6570\u95ea\u706f<\/p>\n<p><span style=\"color:#511b78\">\u573a\u666f C&#xff1a;EEPROM \u662f\u7a7a\u7247 \/ \u4ece\u6ca1\u5199\u8fc7<\/span><\/p>\n<p>\u9009 2 \u2192 \u8bfb\u56de\u5168 0xFF \u2192 App_Run \u6821\u9a8c\u5931\u8d25 \u2192 \u4e0d\u95ea\u706f&#xff08;\u5b89\u5168&#xff09;<\/p>\n<h2><span style=\"color:#1a439c\">7. \u5b8c\u6574\u5de5\u7a0b\u4ee3\u7801<\/span><\/h2>\n<h3><span style=\"color:#1a439c\">BSP_AT24C02.h\/c<\/span><\/h3>\n<p>#ifndef __BSP_AT24C02_H<br \/>\n#define __BSP_AT24C02_H<br \/>\n#ifdef __cplusplus<br \/>\nextern &#034;C&#034;<br \/>\n{<br \/>\n#endif<\/p>\n<p>#include &lt;stdbool.h&gt;<br \/>\n#include &#034;stm32f1xx_hal.h&#034;<\/p>\n<p>    bool Read_Page_Data(void *data_out, uint8_t data_len);<br \/>\n    bool Write_Page_Data(void *data_in, uint8_t data_len);<\/p>\n<p>    \/\/ \u8868\u2f70\u4ece\u8bbe\u5907<br \/>\n    typedef struct<br \/>\n    {<br \/>\n        I2C_HandleTypeDef *hi2c; \/\/ I2C2\u53e5\u67c4\u6307\u9488<br \/>\n        uint16_t dev_address;    \/\/ \u4ece\u8bbe\u5907\u5730\u5740<br \/>\n        uint8_t page_size;       \/\/ \u2eda\u2f24\u2f29<br \/>\n        bool is_init;            \/\/ \u662f\u5426\u88ab\u521d\u59cb\u5316<br \/>\n    } eeprom_t;<\/p>\n<p>#define DEV_ADDRESS     0x50    \/\/ I2C\u4ece\u673a\u8bbe\u5907\u5730\u5740<br \/>\n#define PAGE_SIZE       8       \/\/ EEPROM\u5355\u9875\u5199\u5165\u5b57\u8282\u6570<br \/>\n#define DEV_TOTAL_SIZE  256     \/\/ EEPROM\u603b\u5b58\u50a8\u5b57\u8282\u5927\u5c0f<br \/>\n#define SCAN_RETYIES    2       \/\/ I2C\u8bbe\u5907\u626b\u63cf\u91cd\u8bd5\u6b21\u6570<br \/>\n#define SCAN_TIME_MS    5       \/\/ \u626b\u63cf\u91cd\u8bd5\u95f4\u9694(ms)<br \/>\n#define POLL_RETYIES    10      \/\/ \u5199\u7b49\u5f85\u8f6e\u8be2\u91cd\u8bd5\u6b21\u6570<br \/>\n#define POLL_TIME_MS    2       \/\/ \u8f6e\u8be2\u7b49\u5f85\u95f4\u9694(ms)<br \/>\n#define READ_TIMEOUT_MS 50      \/\/ I2C\u8bfb\u53d6\u8d85\u65f6\u65f6\u95f4(ms)<br \/>\n#define WRITE_TIMEOUT_MS 50     \/\/ I2C\u5199\u5165\u8d85\u65f6\u65f6\u95f4(ms)<\/p>\n<p>    bool BSP_AT24C02_Init(I2C_HandleTypeDef *hi2c);<br \/>\n    bool BSP_AT24C02_Read(uint16_t start_address, void *data_out, uint8_t data_len);<br \/>\n    bool BSP_AT24C02_Write(uint16_t start_address, void *data_in, uint8_t data_len);<\/p>\n<p>#ifdef __cplusplus<br \/>\n}<br \/>\n#endif<\/p>\n<p>#endif<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\n#include &#034;BSP_AT24C02.h&#034;<br \/>\neeprom_t at24c02;<\/p>\n<p>\/\/\u521d\u59cb\u5316\u903b\u8f91<br \/>\nbool BSP_AT24C02_Init(I2C_HandleTypeDef *hi2c)<\/p>\n<p>{<br \/>\n    if (!hi2c)<br \/>\n        return false;<br \/>\n    at24c02.hi2c &#061; hi2c;<br \/>\n    at24c02.dev_address &#061; DEV_ADDRESS &lt;&lt; 1; \/\/ \u5de6\u79fb1\u4f4d&#xff08;\u516b\u4f4d\u53d8\u4e03\u4f4d&#xff09;<br \/>\n    at24c02.page_size &#061; PAGE_SIZE;<br \/>\n    at24c02.is_init &#061; false;<br \/>\n    \/\/ 1. \u4ea7\u2f63\u8d77\u59cb<br \/>\n    \/\/ 2. \u53d1\u9001\u4ece\u8bbe\u5907\u5730\u5740&#xff08;\u4e03\u4f4d&#xff09; &#043; w(0) &#061; 8\u5b57\u8282\u6570\u636e<br \/>\n    \/\/ 3. ACK -&gt; \u4ece\u8bbe\u5907\u6d3b\u7740\u5462&#xff01;<br \/>\n    \/\/ 4. STOP<br \/>\n    if (HAL_OK &#061;&#061; HAL_I2C_IsDeviceReady(at24c02.hi2c, at24c02.dev_address,<br \/>\n                                        SCAN_RETYIES, SCAN_TIME_MS))<br \/>\n    {<br \/>\n        at24c02.is_init &#061; true;<br \/>\n    }<br \/>\n    return at24c02.is_init;<br \/>\n}<\/p>\n<p>\/\/ \u6309\u5b57\u8282&#xff0c;\u6309page&#xff0c;\u6309\u4efb\u610f\u8bfb<br \/>\nbool BSP_AT24C02_Read(uint16_t start_address, void *data_out, uint8_t data_len)<br \/>\n{<br \/>\n    if (!at24c02.is_init || !data_out || data_len &#061;&#061; 0)<br \/>\n        return false;<br \/>\n    if (start_address &#043; data_len &gt; DEV_TOTAL_SIZE)\/\/\u8d85\u8fc7\u9875\u53ef\u8bfb\u8303\u56f4<br \/>\n    {<br \/>\n        return false;<br \/>\n    }<br \/>\n    HAL_StatusTypeDef state &#061; HAL_I2C_Mem_Read(<br \/>\n        at24c02.hi2c, at24c02.dev_address,<br \/>\n        start_address,<br \/>\n        I2C_MEMADD_SIZE_8BIT,<br \/>\n        data_out,<br \/>\n        data_len,<br \/>\n        READ_TIMEOUT_MS);<br \/>\n    return state &#061;&#061; HAL_OK;<br \/>\n}<br \/>\nstatic bool BSP_AT24C02_Ready()<br \/>\n{<br \/>\n    if (!at24c02.is_init)<br \/>\n        return false;<br \/>\n    return HAL_OK &#061;&#061; HAL_I2C_IsDeviceReady(at24c02.hi2c, at24c02.dev_address,<br \/>\n                                           POLL_RETYIES, POLL_TIME_MS);<br \/>\n}<\/p>\n<p>\/\/ \u5355\u2eda&#xff0c;\u2eda\u5185\u5199\u2f0a<br \/>\nstatic bool BSP_AT24C02_Write_Page(uint16_t start_address, void *data_in,<br \/>\n                                   uint8_t data_len)<br \/>\n{<br \/>\n    if (!at24c02.is_init || !data_in || data_len &#061;&#061; 0)<br \/>\n        return false;<br \/>\n    \/\/ \u68c0\u67e5\u5199\u2f0a\u662f\u5426\u8de8\u2eda<br \/>\n    \/\/ \u5f53\u524d\u5199\u2f0a\u5230\u90a3\u2f00\u2eda<br \/>\n    \/\/ uint16_t page_num &#061; start_address \/ at24c02.page_size; \/\/ \u4e0d\u91cd\u8981<br \/>\n    uint16_t page_offset &#061; start_address % at24c02.page_size; \/\/ \u786e\u5b9a\u7684\u2f00\u2eda&#xff0c;\u5199\u2f0a\u4f4d\u7f6e\u7684\u5f00\u59cb\u4e0b\u6807&#xff01;<br \/>\n    if (page_offset &#043; data_len &gt; at24c02.page_size)<br \/>\n    {<br \/>\n        return false;<br \/>\n    }<br \/>\n    HAL_StatusTypeDef state &#061; HAL_I2C_Mem_Write(<br \/>\n        at24c02.hi2c,<br \/>\n        at24c02.dev_address,<br \/>\n        start_address,<br \/>\n        I2C_MEMADD_SIZE_8BIT,<br \/>\n        data_in,<br \/>\n        data_len,<br \/>\n        WRITE_TIMEOUT_MS);<br \/>\n    if (state !&#061; HAL_OK)<br \/>\n        return false;<br \/>\n    return BSP_AT24C02_Ready();<br \/>\n}<\/p>\n<p>\/\/ \u2f40\u6301\u4efb\u610f\u2ed3\u5ea6\u7684\u5199\u2f0a&#xff0c;\u2f83\u52a8\u8de8\u2eda<br \/>\nbool BSP_AT24C02_Write(uint16_t start_address, void *data_in, uint8_t data_len)<br \/>\n{<br \/>\n    if (!at24c02.is_init || !data_in || data_len &#061;&#061; 0)<br \/>\n        return false;<br \/>\n    if (start_address &#043; data_len &gt; DEV_TOTAL_SIZE)\/\/\u8d85\u8fc7\u5168\u90e8\u9875\u7684\u50a8\u91cf<br \/>\n        return false;<br \/>\n    uint16_t bytes_written &#061; 0; \/\/ \u5df2\u7ecf\u5199\u2f0a\u4e86\u591a\u5c11\u4e2a\u5b57\u8282<\/p>\n<p>    while (bytes_written &lt; data_len)\/\/\u672a\u5b8c\u6210\u5199<br \/>\n    {<br \/>\n        \/\/ \u6570\u636e\u8fd8\u6709\u591a\u5c11\u6ca1\u6709\u53d1\u9001<br \/>\n        uint16_t data_left &#061; data_len &#8211; bytes_written;<br \/>\n        \/\/ \u5fc5\u987b\u6309\u7167\u2eda\u6765\u5199&#xff0c;\u5f53\u524d\u2eda\u5269\u4f59\u591a\u5c11\u7a7a\u95f4<br \/>\n        uint16_t new_start_address &#061; start_address &#043; bytes_written;\/\/\u7ffb\u9875\u903b\u8f91<br \/>\n        \/\/ uint16_t current_page_num &#061; new_start_address \/ at24c02.page_size;<br \/>\n        uint16_t page_offset &#061; new_start_address % at24c02.page_size;\/\/\u5df2\u7ecf\u5360\u7528\u7684\u7a7a\u95f4<br \/>\n        uint16_t page_space &#061; at24c02.page_size &#8211; page_offset;\/\/\u5269\u4f59\u7a7a\u95f4<br \/>\n        \/\/ \u6570\u636e\u8fd8\u6709\u591a\u5c11\u6ca1\u6709\u53d1\u9001 -&gt; \u5f53\u524d\u2eda\u5269\u4f59\u591a\u5c11\u7a7a\u95f4<br \/>\n        \/\/ \u2f00\u5b9a\u80fd\u5199\u2f0a\u5f53\u524d\u2eda\u7684\u5269\u4f59\u7a7a\u95f4\u5417&#xff1f;\u4e0d\u2f00\u5b9a&#xff01;&#xff01;<br \/>\n        \/\/ \u8ba1\u7b97\u672c\u6b21\u5199\u2f0a\u91cf<br \/>\n        \/\/ 1. \u6570\u636e\u8fd8\u6709\u591a\u5c11\u6ca1\u6709\u53d1\u9001 &lt;&#061; \u5f53\u524d\u2eda\u5269\u4f59\u591a\u5c11\u7a7a\u95f4 &#061; \u6570\u636e\u8fd8\u6709\u591a\u5c11<br \/>\n        \/\/ 2. \u6570\u636e\u8fd8\u6709\u591a\u5c11\u6ca1\u6709\u53d1\u9001 &gt;  \u5f53\u524d\u2eda\u5269\u4f59\u591a\u5c11\u7a7a\u95f4 &#061; \u5269\u4f59\u591a\u5c11\u7a7a\u95f4<br \/>\n        uint16_t write_bytes &#061; data_left;<br \/>\n        if (write_bytes &gt; page_space)<br \/>\n        {<br \/>\n            write_bytes &#061; page_space;\/\/\u53ef\u4ee5\u5199\u4e14\u4e0d\u8de8\u9875<br \/>\n        }<br \/>\n        if (!BSP_AT24C02_Write_Page(new_start_address,(uint8_t *)data_in &#043; bytes_written, write_bytes))<br \/>\n        {<br \/>\n            return false;\/\/\u5355\u9875\u5199\u6ca1\u6210\u529f<br \/>\n        }<br \/>\n        bytes_written &#043;&#061; write_bytes;\/\/\u628a\u5f53\u9875\u80fd\u5199\u7684\u52a0\u5165&#xff0c;\u518d\u6b21\u5faa\u73af&#xff0c;\u7ffb\u9875\u5199<br \/>\n    }<br \/>\n    return true;<br \/>\n}<\/p>\n<hr \/>\n<h3><span style=\"color:#1a439c\">App.h\\\\c<\/span><\/h3>\n<p>#ifndef __APP_H<br \/>\n#define __APP_H<\/p>\n<p>#ifdef __cplusplus<br \/>\nextern &#034;C&#034;<br \/>\n{<br \/>\n#endif<\/p>\n<p>#include &lt;stdint.h&gt;<br \/>\n#include &#034;BSP_AT24C02.h&#034;<br \/>\n#include &#034;BSP_Usart_Redir.h&#034;<br \/>\n#include &#034;BSP_LED.h&#034;<br \/>\n#define APP_MAGIC 0x35<br \/>\n#define APP_VERSION 0x01<\/p>\n<p>    typedef struct _config_t<br \/>\n    {<br \/>\n        uint8_t magic;       \/\/ \u9b54\u6570<br \/>\n        uint8_t version;     \/\/ \u7248\u672c<br \/>\n        uint8_t led_id;      \/\/ \u706f\u5e8f\u53f7<br \/>\n        uint8_t repeat_cnt;  \/\/ \u91cd\u590d\u6b21\u6570<br \/>\n        uint16_t delay_ms;   \/\/ \u5ef6\u8fdf\u95f4\u9694<br \/>\n        uint16_t start_time; \/\/ \u5f00\u59cb\u65f6\u95f4<br \/>\n    } config_t;<\/p>\n<p>#define CONFIG_ADDRESS 0x0  \/\/ \u914d\u7f6e\u6570\u636e\u5728AT24C02\u4e2d\u7684\u5b58\u50a8\u8d77\u59cb\u5730\u5740<br \/>\n#define SAVE_CONFIG(conf_addr, conf_len) BSP_AT24C02_Write(CONFIG_ADDRESS,conf_addr, conf_len)<br \/>\n#define LOAD_CONFIG(conf_addr, conf_len) BSP_AT24C02_Read(CONFIG_ADDRESS,conf_addr, conf_len)<\/p>\n<p>void App_Init(void);<br \/>\nvoid App_Run(void);<\/p>\n<p>#ifdef __cplusplus<br \/>\n}<br \/>\n#endif<\/p>\n<p>#endif<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\n#include &#034;App.h&#034;<\/p>\n<p>config_t gconfig;<br \/>\nstatic void Config_Display(void)<br \/>\n{<br \/>\n    printf(&#034;&#8212;&#8212;&#8212;&#8211;\u914d\u7f6e\u6e05\u5355&#8212;&#8212;&#8212;-\\\\n&#034;);<br \/>\n    printf(&#034;config.magic:0x%x\\\\n&#034;, gconfig.magic);<br \/>\n    printf(&#034;config.version:0x%x\\\\n&#034;, gconfig.version);<br \/>\n    printf(&#034;config.led_id:%d\\\\n&#034;, gconfig.led_id);<br \/>\n    printf(&#034;config.repeat_cnt:%d\\\\n&#034;, gconfig.repeat_cnt);<br \/>\n    printf(&#034;config.delay_ms:%d\\\\n&#034;, gconfig.delay_ms);<br \/>\n    printf(&#034;config.start_time:%d\\\\n&#034;, gconfig.start_time);<br \/>\n}<br \/>\nstatic int Request_Input(const char *message)<br \/>\n{<br \/>\n    int choice &#061; 0;<br \/>\n    printf(&#034;&gt;&gt;%s\\\\n&#034;, message);<br \/>\n    if (scanf(&#034;%d&#034;, &amp;choice) &#061;&#061; 1)<br \/>\n    {<br \/>\n        return choice;<br \/>\n    }<\/p>\n<p>    printf(&#034;\u9009\u62e9\u9519\u8bef&#xff01;\u8bf7\u91cd\u65b0\u8f93\u5165&#xff01;&#034;);<br \/>\n    return -1;<br \/>\n}<br \/>\nvoid App_Init(void)<br \/>\n{<br \/>\n    uint8_t choice &#061; Request_Input(&#034;1.\u91cd\u65b0\u914d\u7f6e\u53c2\u6570 2.\u4f7f\u7528\u73b0\u5728\u7684\u914d\u7f6e&#034;);<br \/>\n    if (choice &#061;&#061; 1)<br \/>\n    {<br \/>\n        gconfig.magic &#061; APP_MAGIC;<br \/>\n        gconfig.version &#061; APP_VERSION;<br \/>\n        gconfig.led_id &#061; (uint8_t)Request_Input(&#034;step1:\u9009\u62e9LED\u30101-4\u3011,0\u8868\u793a\u5168\u9009&gt;&gt; &#034;);<br \/>\n        gconfig.repeat_cnt &#061; (uint8_t)Request_Input(&#034;step2:\u8bbe\u7f6e\u95ea\u706f\u6b21\u6570,\u30101-255\u3011&gt;&gt; &#034;);<br \/>\n        gconfig.delay_ms &#061; (uint16_t)Request_Input(&#034;step3:\u9009\u62e9\u5ef6\u8fdf\u95f4\u9694,\u5355\u4f4dms&gt;&gt; &#034;);<br \/>\n        gconfig.start_time &#061; (uint16_t)Request_Input(&#034;step4:\u9009\u62e9\u5f00\u59cb\u65f6\u95f4.\u5355\u4f4dms&gt;&gt; &#034;);<\/p>\n<p>        Config_Display();<br \/>\n        \/\/ \u5199\u2f0a\u5230EEPROM<br \/>\n        SAVE_CONFIG(&amp;gconfig, sizeof(gconfig)); \/\/ AT24C02\u5199\u2f0a<br \/>\n        printf(&#034;&gt;&gt; write config done &lt;&lt;\\\\n&#034;);<br \/>\n    }<br \/>\n    else if (choice &#061;&#061; 2)<br \/>\n    {<br \/>\n        \/\/ \u8bfb\u53d6EEPROM<br \/>\n        LOAD_CONFIG(&amp;gconfig, sizeof(gconfig));<br \/>\n        printf(&#034;&gt;&gt; load config done &lt;&lt;\\\\n&#034;);<br \/>\n    }<br \/>\n    else<br \/>\n    {<br \/>\n        printf(&#034;\u9009\u62e9\u9519\u8bef&#xff01;\u8bf7\u91cd\u65b0\u8f93\u5165&#xff01;&#034;);<br \/>\n    }<br \/>\n}<br \/>\nvoid App_Run()<br \/>\n{<br \/>\n    if (gconfig.magic &#061;&#061; APP_MAGIC &amp;&amp; gconfig.version &#061;&#061; APP_VERSION)<br \/>\n    {<br \/>\n        printf(&#034;&gt;&gt; %d ms\u4e4b\u540e, \u5f00\u59cb\u95ea\u706f\\\\n&#034;, (int)(gconfig.start_time));<br \/>\n        HAL_Delay(gconfig.start_time);<br \/>\n        printf(&#034;&gt;&gt; \u95ea\u706f\u4e2d&#8230;\\\\n&#034;);<br \/>\n        LED_Blink_Version2((LED_t)(gconfig.led_id), gconfig.repeat_cnt,<br \/>\n                           gconfig.delay_ms);<br \/>\n    }<br \/>\n}<\/p>\n<hr \/>\n<h3><span style=\"color:#1a439c\">main.h\\\\c<\/span><\/h3>\n<p>#ifndef __MAIN_H<br \/>\n#define __MAIN_H<\/p>\n<p>#ifdef __cplusplus<br \/>\nextern &#034;C&#034; {<br \/>\n#endif<\/p>\n<p>\/* Includes &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;*\/<br \/>\n#include &#034;stm32f1xx_hal.h&#034;<\/p>\n<p>\/* Private includes &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-*\/<br \/>\n\/* USER CODE BEGIN Includes *\/<br \/>\n#include &lt;stdbool.h&gt;<br \/>\n#include &#034;BSP_AT24C02.h&#034;<br \/>\n#include &#034;BSP_Usart_Redir.h&#034;<br \/>\n#include &#034;BSP_LED.h&#034;<br \/>\n#include &#034;App.h&#034;<\/p>\n<p>\/* Exported functions prototypes &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;*\/<br \/>\nvoid Error_Handler(void);<\/p>\n<p>#ifdef __cplusplus<br \/>\n}<br \/>\n#endif<\/p>\n<p>#endif \/* __MAIN_H *\/<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<\/p>\n<p>#include &#034;main.h&#034;<\/p>\n<p>\/* Private variables &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;*\/<br \/>\nI2C_HandleTypeDef hi2c2;<\/p>\n<p>UART_HandleTypeDef huart1;<\/p>\n<p>\/* Private function prototypes &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;*\/<br \/>\nvoid SystemClock_Config(void);<br \/>\nstatic void MX_GPIO_Init(void);<br \/>\nstatic void MX_I2C2_Init(void);<br \/>\nstatic void MX_USART1_UART_Init(void);<\/p>\n<p>int main(void)<br \/>\n{<\/p>\n<p>  MX_GPIO_Init();<br \/>\n  MX_I2C2_Init();<br \/>\n  MX_USART1_UART_Init();<\/p>\n<p>  \/* USER CODE BEGIN 2 *\/<br \/>\n  BSP_Usart_Init(&amp;huart1);<br \/>\n  BSP_AT24C02_Init(&amp;hi2c2);<br \/>\n  \/* USER CODE END 2 *\/<\/p>\n<p>  \/* Infinite loop *\/<br \/>\n  \/* USER CODE BEGIN WHILE *\/<br \/>\n  while (1)<br \/>\n  {<br \/>\n    App_Init();<br \/>\n    App_Run();<br \/>\n    \/* USER CODE END WHILE *\/<\/p>\n<p>    \/* USER CODE BEGIN 3 *\/<br \/>\n  }<br \/>\n  \/* USER CODE END 3 *\/<br \/>\n}<\/p>\n<p>\/**<br \/>\n  * &#064;brief System Clock Configuration<br \/>\n  * &#064;retval None<br \/>\n  *\/<br \/>\nvoid SystemClock_Config(void)<br \/>\n{<br \/>\n  RCC_OscInitTypeDef RCC_OscInitStruct &#061; {0};<br \/>\n  RCC_ClkInitTypeDef RCC_ClkInitStruct &#061; {0};<\/p>\n<p>  \/** Initializes the RCC Oscillators according to the specified parameters<br \/>\n  * in the RCC_OscInitTypeDef structure.<br \/>\n  *\/<br \/>\n  RCC_OscInitStruct.OscillatorType &#061; RCC_OSCILLATORTYPE_HSE;<br \/>\n  RCC_OscInitStruct.HSEState &#061; RCC_HSE_ON;<br \/>\n  RCC_OscInitStruct.HSEPredivValue &#061; RCC_HSE_PREDIV_DIV1;<br \/>\n  RCC_OscInitStruct.HSIState &#061; RCC_HSI_ON;<br \/>\n  RCC_OscInitStruct.PLL.PLLState &#061; RCC_PLL_ON;<br \/>\n  RCC_OscInitStruct.PLL.PLLSource &#061; RCC_PLLSOURCE_HSE;<br \/>\n  RCC_OscInitStruct.PLL.PLLMUL &#061; RCC_PLL_MUL9;<br \/>\n  if (HAL_RCC_OscConfig(&amp;RCC_OscInitStruct) !&#061; HAL_OK)<br \/>\n  {<br \/>\n    Error_Handler();<br \/>\n  }<\/p>\n<p>  \/** Initializes the CPU, AHB and APB buses clocks<br \/>\n  *\/<br \/>\n  RCC_ClkInitStruct.ClockType &#061; RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK<br \/>\n                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;<br \/>\n  RCC_ClkInitStruct.SYSCLKSource &#061; RCC_SYSCLKSOURCE_PLLCLK;<br \/>\n  RCC_ClkInitStruct.AHBCLKDivider &#061; RCC_SYSCLK_DIV1;<br \/>\n  RCC_ClkInitStruct.APB1CLKDivider &#061; RCC_HCLK_DIV2;<br \/>\n  RCC_ClkInitStruct.APB2CLKDivider &#061; RCC_HCLK_DIV1;<\/p>\n<p>  if (HAL_RCC_ClockConfig(&amp;RCC_ClkInitStruct, FLASH_LATENCY_2) !&#061; HAL_OK)<br \/>\n  {<br \/>\n    Error_Handler();<br \/>\n  }<br \/>\n}<\/p>\n<p>\/**<br \/>\n  * &#064;brief I2C2 Initialization Function<br \/>\n  * &#064;param None<br \/>\n  * &#064;retval None<br \/>\n  *\/<br \/>\nstatic void MX_I2C2_Init(void)<br \/>\n{<\/p>\n<p>  \/* USER CODE BEGIN I2C2_Init 0 *\/<\/p>\n<p>  \/* USER CODE END I2C2_Init 0 *\/<\/p>\n<p>  \/* USER CODE BEGIN I2C2_Init 1 *\/<\/p>\n<p>  \/* USER CODE END I2C2_Init 1 *\/<br \/>\n  hi2c2.Instance &#061; I2C2;<br \/>\n  hi2c2.Init.ClockSpeed &#061; 400000;<br \/>\n  hi2c2.Init.DutyCycle &#061; I2C_DUTYCYCLE_2;<br \/>\n  hi2c2.Init.OwnAddress1 &#061; 0;<br \/>\n  hi2c2.Init.AddressingMode &#061; I2C_ADDRESSINGMODE_7BIT;<br \/>\n  hi2c2.Init.DualAddressMode &#061; I2C_DUALADDRESS_DISABLE;<br \/>\n  hi2c2.Init.OwnAddress2 &#061; 0;<br \/>\n  hi2c2.Init.GeneralCallMode &#061; I2C_GENERALCALL_DISABLE;<br \/>\n  hi2c2.Init.NoStretchMode &#061; I2C_NOSTRETCH_DISABLE;<br \/>\n  if (HAL_I2C_Init(&amp;hi2c2) !&#061; HAL_OK)<br \/>\n  {<br \/>\n    Error_Handler();<br \/>\n  }<br \/>\n  \/* USER CODE BEGIN I2C2_Init 2 *\/<\/p>\n<p>  \/* USER CODE END I2C2_Init 2 *\/<\/p>\n<p>}<\/p>\n<p>\/**<br \/>\n  * &#064;brief USART1 Initialization Function<br \/>\n  * &#064;param None<br \/>\n  * &#064;retval None<br \/>\n  *\/<br \/>\nstatic void MX_USART1_UART_Init(void)<br \/>\n{<\/p>\n<p>  \/* USER CODE BEGIN USART1_Init 0 *\/<\/p>\n<p>  \/* USER CODE END USART1_Init 0 *\/<\/p>\n<p>  \/* USER CODE BEGIN USART1_Init 1 *\/<\/p>\n<p>  \/* USER CODE END USART1_Init 1 *\/<br \/>\n  huart1.Instance &#061; USART1;<br \/>\n  huart1.Init.BaudRate &#061; 115200;<br \/>\n  huart1.Init.WordLength &#061; UART_WORDLENGTH_9B;<br \/>\n  huart1.Init.StopBits &#061; UART_STOPBITS_1;<br \/>\n  huart1.Init.Parity &#061; UART_PARITY_ODD;<br \/>\n  huart1.Init.Mode &#061; UART_MODE_TX_RX;<br \/>\n  huart1.Init.HwFlowCtl &#061; UART_HWCONTROL_NONE;<br \/>\n  huart1.Init.OverSampling &#061; UART_OVERSAMPLING_16;<br \/>\n  if (HAL_UART_Init(&amp;huart1) !&#061; HAL_OK)<br \/>\n  {<br \/>\n    Error_Handler();<br \/>\n  }<br \/>\n  \/* USER CODE BEGIN USART1_Init 2 *\/<\/p>\n<p>  \/* USER CODE END USART1_Init 2 *\/<\/p>\n<p>}<\/p>\n<p>\/**<br \/>\n  * &#064;brief GPIO Initialization Function<br \/>\n  * &#064;param None<br \/>\n  * &#064;retval None<br \/>\n  *\/<br \/>\nstatic void MX_GPIO_Init(void)<br \/>\n{<br \/>\n  GPIO_InitTypeDef GPIO_InitStruct &#061; {0};<br \/>\n  \/* USER CODE BEGIN MX_GPIO_Init_1 *\/<\/p>\n<p>  \/* USER CODE END MX_GPIO_Init_1 *\/<\/p>\n<p>  \/* GPIO Ports Clock Enable *\/<br \/>\n  __HAL_RCC_GPIOF_CLK_ENABLE();<br \/>\n  __HAL_RCC_GPIOB_CLK_ENABLE();<br \/>\n  __HAL_RCC_GPIOA_CLK_ENABLE();<\/p>\n<p>  \/*Configure GPIO pin Output Level *\/<br \/>\n  HAL_GPIO_WritePin(GPIOF, GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11, GPIO_PIN_RESET);<\/p>\n<p>  \/*Configure GPIO pins : PF8 PF9 PF10 PF11 *\/<br \/>\n  GPIO_InitStruct.Pin &#061; GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11;<br \/>\n  GPIO_InitStruct.Mode &#061; GPIO_MODE_OUTPUT_OD;<br \/>\n  GPIO_InitStruct.Pull &#061; GPIO_NOPULL;<br \/>\n  GPIO_InitStruct.Speed &#061; GPIO_SPEED_FREQ_LOW;<br \/>\n  HAL_GPIO_Init(GPIOF, &amp;GPIO_InitStruct);<\/p>\n<p>  \/* USER CODE BEGIN MX_GPIO_Init_2 *\/<\/p>\n<p>  \/* USER CODE END MX_GPIO_Init_2 *\/<br \/>\n}<\/p>\n<h2><\/h2>\n<hr \/>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1292\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260917032513-6aab5d9925242.png\" width=\"2558\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5de5\u7a0b\u5256\u6790&#xff1a;\u4e32\u53e3\u914d\u7f6e \u2192 AT24C02 \u6301\u4e45\u5316 \u2192 LED \u95ea\u706f\u00a0\u00a01. \u5de5\u7a0b\u6982\u89c81.1 \u529f\u80fd\u4e00\u53e5\u8bdd\u901a\u8fc7\u4e32\u53e3\u4ea4\u4e92\u914d\u7f6e\\&#8221;\u9009\u54ea\u51e0\u9897\u706f\u3001\u95ea\u51e0\u6b21\u3001\u95f4\u9694\u591a\u4e45\u3001\u5ef6\u65f6\u591a\u4e45\u542f\u52a8\\&#8221;&#xff0c;\u628a\u914d\u7f6e\u5199\u5165 AT24C02&#xff08;I2C EEPROM&#xff09;\u56fa\u5316&#xff0c;\u4e0a\u7535\u540e\u6309\u4fdd\u5b58\u7684\u914d\u7f6e\u81ea\u52a8\u95ea\u706f\u2014\u2014\u5178\u578b\u7684\\&#8221;\u53c2\u6570\u6389\u7535\u6301\u4e45\u5316\\&#8221;\u5e94\u7528\u3002\u5b9e\u9645\u8fd0\u884c\u6548\u679c&#xff08;\u4e32\u53e3\u63a7\u5236\u53f0&#xff09;&#xff1a;\u4e0b\u4e00\u6b21\u4e0a\u7535\u9009\\&#8221;2.\u4f7f\u7528\u73b0\u5728\u7684\u914d\u7f6e\\&#8221;&#xff0c;\u8bfb\u56de EE<\/p>\n","protected":false},"author":2,"featured_media":106423,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[11815,722,217,270,269,190],"topic":[],"class_list":["post-106428","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-11815","tag-ecmascript","tag-stm32","tag-270","tag-269","tag-190"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u3010STM32\u3011 I2C_EEPROM_test \u5de5\u7a0b\uff08\u5b9e\u6218\u7bc7\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/106428.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u3010STM32\u3011 I2C_EEPROM_test \u5de5\u7a0b\uff08\u5b9e\u6218\u7bc7\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u5de5\u7a0b\u5256\u6790&#xff1a;\u4e32\u53e3\u914d\u7f6e \u2192 AT24C02 \u6301\u4e45\u5316 \u2192 LED \u95ea\u706f\u00a0\u00a01. \u5de5\u7a0b\u6982\u89c81.1 \u529f\u80fd\u4e00\u53e5\u8bdd\u901a\u8fc7\u4e32\u53e3\u4ea4\u4e92\u914d\u7f6e&quot;\u9009\u54ea\u51e0\u9897\u706f\u3001\u95ea\u51e0\u6b21\u3001\u95f4\u9694\u591a\u4e45\u3001\u5ef6\u65f6\u591a\u4e45\u542f\u52a8&quot;&#xff0c;\u628a\u914d\u7f6e\u5199\u5165 AT24C02&#xff08;I2C EEPROM&#xff09;\u56fa\u5316&#xff0c;\u4e0a\u7535\u540e\u6309\u4fdd\u5b58\u7684\u914d\u7f6e\u81ea\u52a8\u95ea\u706f\u2014\u2014\u5178\u578b\u7684&quot;\u53c2\u6570\u6389\u7535\u6301\u4e45\u5316&quot;\u5e94\u7528\u3002\u5b9e\u9645\u8fd0\u884c\u6548\u679c&#xff08;\u4e32\u53e3\u63a7\u5236\u53f0&#xff09;&#xff1a;\u4e0b\u4e00\u6b21\u4e0a\u7535\u9009&quot;2.\u4f7f\u7528\u73b0\u5728\u7684\u914d\u7f6e&quot;&#xff0c;\u8bfb\u56de EE\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/106428.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-09-17T03:25:16+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260917032506-6aab5d92d8fbd.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"19 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/106428.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/106428.html\",\"name\":\"\u3010STM32\u3011 I2C_EEPROM_test \u5de5\u7a0b\uff08\u5b9e\u6218\u7bc7\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-09-17T03:25:16+00:00\",\"dateModified\":\"2026-09-17T03:25:16+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/106428.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/106428.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/106428.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u3010STM32\u3011 I2C_EEPROM_test \u5de5\u7a0b\uff08\u5b9e\u6218\u7bc7\uff09\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u3010STM32\u3011 I2C_EEPROM_test \u5de5\u7a0b\uff08\u5b9e\u6218\u7bc7\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/106428.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010STM32\u3011 I2C_EEPROM_test \u5de5\u7a0b\uff08\u5b9e\u6218\u7bc7\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u5de5\u7a0b\u5256\u6790&#xff1a;\u4e32\u53e3\u914d\u7f6e \u2192 AT24C02 \u6301\u4e45\u5316 \u2192 LED \u95ea\u706f\u00a0\u00a01. \u5de5\u7a0b\u6982\u89c81.1 \u529f\u80fd\u4e00\u53e5\u8bdd\u901a\u8fc7\u4e32\u53e3\u4ea4\u4e92\u914d\u7f6e\"\u9009\u54ea\u51e0\u9897\u706f\u3001\u95ea\u51e0\u6b21\u3001\u95f4\u9694\u591a\u4e45\u3001\u5ef6\u65f6\u591a\u4e45\u542f\u52a8\"&#xff0c;\u628a\u914d\u7f6e\u5199\u5165 AT24C02&#xff08;I2C EEPROM&#xff09;\u56fa\u5316&#xff0c;\u4e0a\u7535\u540e\u6309\u4fdd\u5b58\u7684\u914d\u7f6e\u81ea\u52a8\u95ea\u706f\u2014\u2014\u5178\u578b\u7684\"\u53c2\u6570\u6389\u7535\u6301\u4e45\u5316\"\u5e94\u7528\u3002\u5b9e\u9645\u8fd0\u884c\u6548\u679c&#xff08;\u4e32\u53e3\u63a7\u5236\u53f0&#xff09;&#xff1a;\u4e0b\u4e00\u6b21\u4e0a\u7535\u9009\"2.\u4f7f\u7528\u73b0\u5728\u7684\u914d\u7f6e\"&#xff0c;\u8bfb\u56de EE","og_url":"https:\/\/www.wsisp.com\/helps\/106428.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-09-17T03:25:16+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260917032506-6aab5d92d8fbd.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"19 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/106428.html","url":"https:\/\/www.wsisp.com\/helps\/106428.html","name":"\u3010STM32\u3011 I2C_EEPROM_test \u5de5\u7a0b\uff08\u5b9e\u6218\u7bc7\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-09-17T03:25:16+00:00","dateModified":"2026-09-17T03:25:16+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/106428.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/106428.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/106428.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u3010STM32\u3011 I2C_EEPROM_test \u5de5\u7a0b\uff08\u5b9e\u6218\u7bc7\uff09"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/106428","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=106428"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/106428\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/106423"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=106428"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=106428"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=106428"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=106428"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}