{"id":1317,"date":"2024-04-22T10:26:00","date_gmt":"2024-04-22T02:26:00","guid":{"rendered":"https:\/\/xujiwei.com\/blog\/?p=1317"},"modified":"2024-04-28T23:03:53","modified_gmt":"2024-04-28T15:03:53","slug":"diy-usb-meter-power-history","status":"publish","type":"post","link":"https:\/\/xujiwei.com\/blog\/2024\/04\/diy-usb-meter-power-history\/","title":{"rendered":"DIY USB \u7535\u6d41\u8868\uff088\uff09\uff1a\u68c0\u6d4b\u6309\u952e\u548c\u7ed8\u5236\u529f\u7387\u66f2\u7ebf"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_65 ez-toc-wrap-right counter-hierarchy ez-toc-counter ez-toc-light-blue ez-toc-container-direction\">\n<p class=\"ez-toc-title\">\u6587\u7ae0\u76ee\u5f55<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-69e4b8a944000\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-69e4b8a944000\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/xujiwei.com\/blog\/2024\/04\/diy-usb-meter-power-history\/#%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C\" title=\"\u51c6\u5907\u5de5\u4f5c\">\u51c6\u5907\u5de5\u4f5c<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/xujiwei.com\/blog\/2024\/04\/diy-usb-meter-power-history\/#%E6%8C%89%E9%94%AE%E6%A3%80%E6%B5%8B\" title=\"\u6309\u952e\u68c0\u6d4b\">\u6309\u952e\u68c0\u6d4b<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/xujiwei.com\/blog\/2024\/04\/diy-usb-meter-power-history\/#%E5%AE%9E%E7%8E%B0%E6%98%BE%E7%A4%BA%E9%A1%B5%E9%9D%A2%E5%88%87%E6%8D%A2\" title=\"\u5b9e\u73b0\u663e\u793a\u9875\u9762\u5207\u6362\">\u5b9e\u73b0\u663e\u793a\u9875\u9762\u5207\u6362<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/xujiwei.com\/blog\/2024\/04\/diy-usb-meter-power-history\/#%E5%B1%8F%E5%B9%95%E7%94%BB%E7%82%B9%E9%A9%B1%E5%8A%A8\" title=\"\u5c4f\u5e55\u753b\u70b9\u9a71\u52a8\">\u5c4f\u5e55\u753b\u70b9\u9a71\u52a8<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/xujiwei.com\/blog\/2024\/04\/diy-usb-meter-power-history\/#%E8%AE%B0%E5%BD%95%E5%8A%9F%E7%8E%87%E6%9B%B2%E7%BA%BF\" title=\"\u8bb0\u5f55\u529f\u7387\u66f2\u7ebf\">\u8bb0\u5f55\u529f\u7387\u66f2\u7ebf<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/xujiwei.com\/blog\/2024\/04\/diy-usb-meter-power-history\/#%E7%BB%98%E5%88%B6%E5%8A%9F%E7%8E%87%E6%9B%B2%E7%BA%BF\" title=\"\u7ed8\u5236\u529f\u7387\u66f2\u7ebf\">\u7ed8\u5236\u529f\u7387\u66f2\u7ebf<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/xujiwei.com\/blog\/2024\/04\/diy-usb-meter-power-history\/#%E5%AE%9E%E9%99%85%E8%BF%90%E8%A1%8C%E6%95%88%E6%9E%9C\" title=\"\u5b9e\u9645\u8fd0\u884c\u6548\u679c\">\u5b9e\u9645\u8fd0\u884c\u6548\u679c<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/xujiwei.com\/blog\/2024\/04\/diy-usb-meter-power-history\/#%E5%B0%8F%E7%BB%93\" title=\"\u5c0f\u7ed3\">\u5c0f\u7ed3<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/xujiwei.com\/blog\/2024\/04\/diy-usb-meter-power-history\/#USB_%E7%94%B5%E6%B5%81%E8%A1%A8%E5%BC%80%E6%BA%90%E5%9C%B0%E5%9D%80\" title=\"USB \u7535\u6d41\u8868\u5f00\u6e90\u5730\u5740\">USB \u7535\u6d41\u8868\u5f00\u6e90\u5730\u5740<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/xujiwei.com\/blog\/2024\/04\/diy-usb-meter-power-history\/#DIY_USB_%E7%94%B5%E6%B5%81%E8%A1%A8%E7%B3%BB%E5%88%97\" title=\"DIY USB \u7535\u6d41\u8868\u7cfb\u5217\">DIY USB \u7535\u6d41\u8868\u7cfb\u5217<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/xujiwei.com\/blog\/2024\/04\/diy-usb-meter-power-history\/#%E5%85%B6%E4%BB%96_DIY_%E9%A1%B9%E7%9B%AE\" title=\"\u5176\u4ed6 DIY \u9879\u76ee\">\u5176\u4ed6 DIY \u9879\u76ee<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/xujiwei.com\/blog\/2024\/04\/diy-usb-meter-power-history\/#30_%E5%85%83_DIY_%E4%B8%80%E4%B8%AA%E6%9F%94%E6%80%A7%E7%81%AF%E4%B8%9D%E6%B0%9B%E5%9B%B4%E7%81%AF\" title=\"30 \u5143 DIY \u4e00\u4e2a\u67d4\u6027\u706f\u4e1d\u6c1b\u56f4\u706f\">30 \u5143 DIY \u4e00\u4e2a\u67d4\u6027\u706f\u4e1d\u6c1b\u56f4\u706f<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/xujiwei.com\/blog\/2024\/04\/diy-usb-meter-power-history\/#%E5%8F%82%E8%80%83%E8%B5%84%E6%96%99\" title=\"\u53c2\u8003\u8d44\u6599\">\u53c2\u8003\u8d44\u6599<\/a><\/li><\/ul><\/nav><\/div>\n<p>\u5728\u524d\u4e00\u7bc7 <a href=\"https:\/\/xujiwei.com\/blog\/2024\/04\/diy-usb-meter-read-ina219\/\">\u300aDIY USB \u7535\u6d41\u8868\uff087\uff09\uff1a\u8bfb\u53d6\u548c\u663e\u793a INA219 \u7535\u6d41\u7535\u538b\u6570\u636e\u300b<\/a> \u4e2d\uff0c\u6211\u4eec\u5df2\u7ecf\u57fa\u672c\u5b8c\u6210\u4e86\u8fd9\u4e2a DIY USB \u7535\u6d41\u8868\u7684\u6838\u5fc3\u529f\u80fd\u5f00\u53d1\uff0c\u5df2\u7ecf\u53ef\u4ee5\u5728\u5c4f\u5e55\u4e0a\u663e\u793a\u5f53\u524d\u7535\u6e90\u8f93\u5165\u7535\u538b\u3001\u8d1f\u8f7d\u7684\u7535\u6d41\u3001\u529f\u7387\u4ee5\u53ca\u7d2f\u8ba1\u7535\u91cf\u7b49\u6570\u636e\uff0c\u5982\u679c\u4e0d\u9700\u8981\u66f4\u591a\u529f\u80fd\uff0c\u5df2\u7ecf\u53ef\u4ee5\u7ed3\u9879\u4e86 \ud83d\ude03\u3002<\/p>\n<p>\u4f46\u662f\u5982\u679c\u60f3\u62ff\u8fd9\u4e2a USB \u7535\u6d41\u8868\u5728\u4e00\u4e9b\u5206\u6790\u573a\u666f\u4f7f\u7528\uff0c\u90a3\u4e48\u8fd8\u9700\u8981\u518d\u589e\u52a0\u4e00\u4e9b\u529f\u80fd\uff0c\u4f8b\u5982\uff0c\u662f\u5426\u53ef\u4ee5\u901a\u8fc7\u8fd9\u4e2a USB \u7535\u6d41\u8868\u6765\u8bb0\u5f55\u7ed9\u624b\u673a\u5145\u7535\u65f6\u7684\u529f\u7387\u66f2\u7ebf\uff0c\u8fd9\u6837\u53ef\u4ee5\u77e5\u9053\u624b\u673a\u7684\u5feb\u5145\u662f\u4ec0\u4e48\u6837\u7684\u8282\u594f\u6765\u5b8c\u6210\u5145\u7535\u7684\u3002<\/p>\n<p>\u8fd9\u6b21\u6211\u4eec\u5c31\u6765\u5b8c\u6210\u8fd9\u4e2a\u529f\u80fd\uff0c\u8bb0\u5f55\u8d1f\u8f7d\u7684\u529f\u7387\u5386\u53f2\uff0c\u5e76\u7ed8\u5236\u6210\u66f2\u7ebf\u6765\u663e\u793a\u529f\u7387\u8d8b\u52bf\u3002<\/p>\n<p>PS. \u6211\u4e5f\u8fd8\u662f\u4e00\u4e2a\u521d\u5b66\u8005\uff0c\u5982\u679c\u6587\u7ae0\u4e2d\u6709\u4e00\u4e9b\u9519\u8bef\u6216\u4e0d\u8db3\uff0c\u8fd8\u8bf7\u591a\u591a\u6307\u6559\u3002<\/p>\n<h2><span class=\"ez-toc-section\" id=\"%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C\"><\/span>\u51c6\u5907\u5de5\u4f5c<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u5728\u5f00\u59cb\u7ed8\u5236\u529f\u7387\u66f2\u7ebf\u4e4b\u524d\uff0c\u540c\u6837\u6709\u4e00\u4e9b\u51c6\u5907\u5de5\u4f5c\u9700\u8981\u8fdb\u884c\uff0c\u4f8b\u5982\u5728\u591a\u4e86\u4e00\u4e2a\u529f\u7387\u66f2\u7ebf\u9875\u9762\u4e4b\u540e\uff0c\u539f\u6765\u7684\u7535\u538b\u6570\u636e\u9875\u9762\u600e\u4e48\u529e\uff0c\u600e\u4e48\u5207\u6362\u591a\u4e2a\u9875\u9762\uff1f<\/p>\n<p>\u5728\u6700\u65e9\u7684\u529f\u80fd\u8bbe\u8ba1\u4e2d\uff0c\u6211\u4eec\u5df2\u7ecf\u7ed9\u8fd9\u4e2a DIY USB \u7535\u6d41\u8868\u6dfb\u52a0\u4e86\u4e24\u4e2a\u6309\u952e\uff0c\u73b0\u5728\u5c31\u53ef\u4ee5\u901a\u8fc7\u8fd9\u4e24\u4e2a\u6309\u952e\u6765\u5b8c\u6210\u9875\u9762\u5207\u6362\u64cd\u4f5c\u4e86\u3002<\/p>\n<h3><span class=\"ez-toc-section\" id=\"%E6%8C%89%E9%94%AE%E6%A3%80%E6%B5%8B\"><\/span>\u6309\u952e\u68c0\u6d4b<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><strong>\u6309\u952e\u539f\u7406\u56fe<\/strong><\/p>\n<p>\u5728\u539f\u7406\u56fe\u4e2d\u53ef\u4ee5\u770b\u5230\uff0c\u4e24\u4e2a\u6309\u952e\u5df2\u7ecf\u63a5\u5230 CH32V003 \u7684 PD2\u3001PD3 \u5f15\u811a\uff0c\u5728\u521d\u59cb\u5316\u4ee3\u7801\u4e2d\uff0c\u52a0\u4e0a\u521d\u59cb\u5316\u4e24\u4e2a\u5f15\u811a\u4e3a\u8f93\u5165\u6a21\u5f0f\u3002<\/p>\n<p><img src=\"https:\/\/xujiwei.com\/blog\/wp-content\/uploads\/2024\/04\/diy-usb-meter-8-1.png\" alt=\"diy-usb-meter-8-1\" style=\"zoom:50%;\" \/><br \/>\n<img src=\"https:\/\/xujiwei.com\/blog\/wp-content\/uploads\/2024\/04\/diy-usb-meter-8-2.png\" alt=\"diy-usb-meter-8-2\" style=\"zoom:50%;\" \/><\/p>\n<p><strong>\u6309\u952e\u72b6\u6001\u7ed3\u6784\u4f53<\/strong><\/p>\n<p>\u4e3a\u4e86\u7ef4\u62a4\u6309\u952e\u7684\u72b6\u6001\uff0c\u6211\u4eec\u5148\u5b9a\u4e49\u4e00\u4e2a\u7ed3\u6784\u4f53\u6765\u7ef4\u62a4\u72b6\u6001\u3002\u5728 <code>ButtonState<\/code> \u7ed3\u6784\u4f53\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a ts \u5b57\u6bb5\uff0c\u8fd9\u4e2a\u7528\u6765\u8bb0\u5f55\u6309\u952e\u72b6\u6001\u53d8\u5316\u65f6\u7684\u65f6\u95f4\u6233\uff0c\u8fd9\u6837\u53ef\u4ee5\u5728\u72b6\u6001\u53d8\u5316\u65f6\uff0c\u4f8b\u5982\u4ece\u4f4e\u7535\u5e73\u53d8\u5316\u4e3a\u9ad8\u7535\u5e73\u65f6\uff0c\u8ba1\u7b97\u51fa\u6765\u6309\u952e\u6309\u4e0b\u53bb\u7684\u6301\u7eed\u65f6\u95f4\uff0c\u5982\u679c\u8d85\u8fc7 3000 \u6beb\u79d2\uff0c\u5c31\u8ba4\u4e3a\u662f\u957f\u6309\u4e8b\u4ef6\u3002<\/p>\n<pre><code class=\"language-c\">#pragma once\n\n#include &quot;drivers\/clock.h&quot;\n#include &quot;drivers\/gpio.h&quot;\n\n#define BTN_LEFT    0\n#define BTN_RIGHT   1\n#define LONG_PRESS_PERIOD   3000\n\nstruct ButtonState {\n    uint8_t pin;\n    uint8_t state;\n    uint32_t ts;\n};\n\nButtonState buttons[] = {\n    {\n        .pin = PD2,\n        .state = 1,\n        .ts = 0,\n    },\n    {\n        .pin = PD3,\n        .state = 1,\n        .ts = 0,\n    },\n};<\/code><\/pre>\n<p>\u8fd9\u91cc\u7684\u9ed8\u8ba4 <code>state<\/code> \u4e3a 1 \u662f\u56e0\u4e3a\u5728\u539f\u7406\u56fe\u4e2d\u6dfb\u52a0\u4e86\u4e0a\u62c9\u7535\u963b\uff0c\u9ed8\u8ba4 GPIO \u72b6\u6001\u4e3a\u9ad8\u7535\u5e73\u3002<\/p>\n<p><strong>\u521d\u59cb\u5316\u6309\u952e\u68c0\u6d4b<\/strong><\/p>\n<p>\u521d\u59cb\u5316\u6309\u952e\u68c0\u6d4b\u6bd4\u8f83\u7b80\u5355\uff0c\u53ea\u9700\u8981\u5c06 PD2\u3001PD3 \u4e24\u4e2a GPIO \u8bbe\u7f6e\u4e3a\u8f93\u5165\u6a21\u5f0f\u5373\u53ef\u3002<\/p>\n<pre><code class=\"language-c\">void button_setup() {\n    PIN_input(buttons[BTN_LEFT].pin);\n    PIN_input(buttons[BTN_RIGHT].pin);\n}<\/code><\/pre>\n<p><strong>\u6309\u952e\u4e8b\u4ef6\u68c0\u6d4b<\/strong><\/p>\n<pre><code class=\"language-c\">int check_button(int index) {\n    int new_state = PIN_read(buttons[index].pin);\n    int old_state = buttons[index].state;\n    int action = BUTTON_NO_ACTION;\n    if (new_state != old_state) {\n        if (new_state == 1) {\n            if (millis() - buttons[index].ts &gt; LONG_PRESS_PERIOD) {\n                action = BUTTON_LONG_CLICK;\n            } else {\n                action = BUTTON_CLICK;\n            }\n        }\n        buttons[index].state = new_state;\n        buttons[index].ts = millis();\n    }\n\n    return action;\n}<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"%E5%AE%9E%E7%8E%B0%E6%98%BE%E7%A4%BA%E9%A1%B5%E9%9D%A2%E5%88%87%E6%8D%A2\"><\/span>\u5b9e\u73b0\u663e\u793a\u9875\u9762\u5207\u6362<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>\u4e3a\u4e86\u5b9e\u73b0\u9875\u9762\u5207\u6362\uff0c\u5148\u5b9a\u4e49\u4e24\u4e2a\u9875\u9762 ID\uff0c\u5e76\u4e14\u5c06\u5f53\u524d\u9875\u9762 ID \u4fdd\u5b58\u5728 <code>current_page<\/code> \u53d8\u91cf\u4e2d\uff0c\u9ed8\u8ba4\u4e3a\u6570\u636e\u5b57\u6bb5\u9875\u9762\u3002<\/p>\n<pre><code class=\"language-c\">int current_page = 0;\n#define PAGE_METRICS    0\n#define PAGE_HISTORY    1\n#define PAGE_COUNT      2<\/code><\/pre>\n<p>\u5728 <code>main<\/code> \u51fd\u6570\u4e2d\u5148\u521d\u59cb\u5316\u4e00\u4e0b\u6309\u94ae\u8bbe\u7f6e\uff0c\u518d\u5728 while \u5faa\u73af\u4e2d\u7684\u68c0\u6d4b\u6309\u952e\u4e8b\u4ef6\uff0c\u6839\u636e <code>current_page<\/code> \u7684\u503c\u6765\u786e\u5b9a\u663e\u793a\u54ea\u4e2a\u9875\u9762\uff1a<\/p>\n<ul>\n<li>current_page = 0 \u65f6\uff0c\u8c03\u7528 <code>show_power_metrics()<\/code>  \u663e\u793a\u6570\u636e\u5b57\u6bb5\u9875\u9762<\/li>\n<li>current_page = 1 \u65f6\uff0c\u8c03\u7528 <code>show_power_history()<\/code> \u663e\u793a\u529f\u7387\u5386\u53f2\u66f2\u7ebf\u9875\u9762<\/li>\n<\/ul>\n<p><img src=\"https:\/\/xujiwei.com\/blog\/wp-content\/uploads\/2024\/04\/diy-usb-meter-8-3.png\" alt=\"diy-usb-meter-8-3\" style=\"zoom:50%;\" \/><\/p>\n<h3><span class=\"ez-toc-section\" id=\"%E5%B1%8F%E5%B9%95%E7%94%BB%E7%82%B9%E9%A9%B1%E5%8A%A8\"><\/span>\u5c4f\u5e55\u753b\u70b9\u9a71\u52a8<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>\u53e6\u5916\uff0c\u4e4b\u524d\u7684\u663e\u793a\u9a71\u52a8\u53ea\u5b9e\u73b0\u4e86\u663e\u793a\u82f1\u6587\u5b57\u7b26\uff0c\u8fd9\u91cc\u4e3a\u4e86\u7ed8\u5236\u529f\u7387\u66f2\u7ebf\uff0c\u8fd8\u9700\u8981\u5b9e\u73b0\u4e00\u4e2a\u753b\u70b9\u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u6bcf\u4e2a\u65f6\u95f4\u70b9\u7684\u529f\u7387\u6839\u636e\u6bd4\u4f8b\u7ed8\u5236\u5728\u5c4f\u5e55\u6307\u5b9a\u4f4d\u7f6e\u4e0a\u3002<\/p>\n<pre><code class=\"language-c\">void display_draw_dot(int x, int y) {\n    int row = y \/ 8;\n    int offset = row * 128 + x;\n    int pixel_offset = y % 8;\n    u8 b = display_buffer[offset];\n    b = b | (1 &lt;&lt; pixel_offset);\n    display_buffer[offset] = b;\n}<\/code><\/pre>\n<p>\u5b8c\u6210\u4ee5\u4e0a\u51c6\u5907\u529f\u80fd\uff0c\u5c31\u53ef\u4ee5\u5f00\u59cb\u5b9e\u73b0\u771f\u6b63\u7684\u4e1a\u52a1\u529f\u80fd\u4e86\u3002<\/p>\n<h2><span class=\"ez-toc-section\" id=\"%E8%AE%B0%E5%BD%95%E5%8A%9F%E7%8E%87%E6%9B%B2%E7%BA%BF\"><\/span>\u8bb0\u5f55\u529f\u7387\u66f2\u7ebf<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u4e3a\u4e86\u8bb0\u5f55\u529f\u7387\u7684\u5386\u53f2\u6570\u636e\uff0c\u5148\u5728 <code>data_manager.hpp<\/code> \u4e2d\u6dfb\u52a0\u4e00\u5757\u7f13\u5b58\u533a\u57df\uff0c\u7528\u4e8e\u5b58\u653e\u5386\u53f2\u7684\u529f\u7387\u6570\u636e\u3002<\/p>\n<p>\u56e0\u4e3a\u6211\u4eec\u4f7f\u7528\u7684\u5c4f\u5e55\u5206\u8fa8\u7387\u662f 128 * 64\uff0c\u53ef\u4ee5\u5c06\u5386\u53f2\u6570\u636e\u70b9\u6570\u91cf\u5b9a\u4e49\u4e3a 128 \u4e2a\uff0c\u6570\u636e\u70b9\u518d\u591a\uff0c\u5c4f\u5e55\u4e5f\u663e\u793a\u4e0d\u4e0b\uff0c\u610f\u4e49\u4e0d\u5927\u4e86\u3002<\/p>\n<pre><code class=\"language-c\">#define POWER_HISTORY_PERIOD 60000\n#define POWER_HISTORY_MAX_COUNT  128\n\nu16 power_history[POWER_HISTORY_MAX_COUNT] = { 0 };\nint power_history_index = 0;\nint power_history_count = 0;\nuint32_t power_history_last_ts = 0;<\/code><\/pre>\n<p>\u5728 <code>adc_read_values<\/code> \u65b9\u6cd5\u4e2d\uff0c\u8bfb\u53d6\u5b8c INA219 \u7684\u6570\u636e\uff0c\u5c31\u53ef\u4ee5\u5c06\u5b83\u8bb0\u5f55\u4e0b\u6765\u5b58\u653e\u5230 <code>power_history<\/code> \u4e2d\u4e86\u3002<\/p>\n<p>\u4e0d\u8fc7\u8fd9\u91cc\u5b58\u5728\u4e00\u4e2a\u95ee\u9898\uff0c<code>adc_read_values<\/code> \u662f\u5728\u4e3b\u5faa\u73af\u4e2d\u4e00\u76f4\u88ab\u8c03\u7528\u7684\uff0c\u4e3a\u4e86\u907f\u514d\u6570\u636e\u88ab\u4e00\u76f4\u5237\u65b0\uff0c\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u65f6\u95f4\u95f4\u9694\uff0c\u5728\u8d85\u8fc7\u786e\u5b9a\u7684\u65f6\u95f4\u95f4\u9694\u540e\u624d\u5c06\u6570\u636e\u8bb0\u5f55\u4e0b\u6765\u3002<\/p>\n<p>\u8fd9\u91cc\u901a\u8fc7 <code>POWER_HISTORY_PERIOD<\/code> \u5b9a\u4e49\u4e86\u6bcf 60000 \u6beb\u79d2\u8bb0\u5f55\u4e00\u6b21\u6570\u636e\uff0c\u5373\u6bcf\u5206\u949f\u4f1a\u6709\u4e00\u4e2a\u6570\u636e\u70b9\u4ea7\u751f\u3002<\/p>\n<p>\u7136\u540e\u5728 <code>adc_read_values<\/code> \u7684\u6700\u540e\uff0c\u52a0\u4e0a\u6570\u636e\u4fdd\u5b58\u903b\u8f91\u3002<\/p>\n<pre><code class=\"language-c\">void adc_read_values() {\n    \/\/ ..... \u6570\u636e\u8bfb\u53d6\u4ee3\u7801\n        measure_cap_mwh += period_cap;\n\n    \/\/ \u8bb0\u5f55\u529f\u7387\u6570\u636e\n    if (now - power_history_last_ts &gt;= POWER_HISTORY_PERIOD) {\n\n        int save_index = (power_history_index + power_history_count) % POWER_HISTORY_MAX_COUNT;\n        power_history[save_index] = measure_power_mw;\n\n        power_history_count = power_history_count + 1;\n        if (power_history_count &gt;= POWER_HISTORY_MAX_COUNT) {\n            power_history_count = POWER_HISTORY_MAX_COUNT;\n            power_history_index = (power_history_index + 1) % POWER_HISTORY_MAX_COUNT;\n        }\n        power_history_last_ts = now;\n    }\n}<\/code><\/pre>\n<p>\u8fd9\u91cc\u4f7f\u7528\u4e86\u4e00\u4e2a\u7b80\u5355\u7684 Ring Buffer \u5b9e\u73b0\u6570\u636e\u7684\u5faa\u73af\u8986\u76d6\u8bb0\u5f55\uff0c\u603b\u662f\u4fdd\u7559\u6700\u65b0\u6570\u636e\u3002\u5173\u4e8e Ring Buffer \u7684\u4ecb\u7ecd\u53ef\u4ee5\u53c2\u8003\u8fd9\u7bc7\u6587\u7ae0\uff1a <a href=\"https:\/\/zhuanlan.zhihu.com\/p\/534098236\">https:\/\/zhuanlan.zhihu.com\/p\/534098236<\/a> \u3002<\/p>\n<h2><span class=\"ez-toc-section\" id=\"%E7%BB%98%E5%88%B6%E5%8A%9F%E7%8E%87%E6%9B%B2%E7%BA%BF\"><\/span>\u7ed8\u5236\u529f\u7387\u66f2\u7ebf<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u6700\u540e\u5728 <code>main.cpp<\/code> \u4e2d\u52a0\u4e0a <code>show_power_history<\/code> \u65b9\u6cd5\uff0c\u5728\u5207\u6362\u5230\u529f\u7387\u5386\u53f2\u9875\u9762\u7684\u65f6\u5019\uff0c\u7ed8\u5236\u66f2\u7ebf\u51fa\u6765\u3002<\/p>\n<pre><code class=\"language-c\">void show_power_history() {\n    display_clear();\n\n    display_write_line(0, &quot;Power History&quot;);\n\n    for (int i = 0; i &lt; power_history_count; ++i) {\n        u16 p = power_history[(power_history_index + i) % POWER_HISTORY_MAX_COUNT];\n        int32_t y = 63 - (p * 63 \/ 100000);\n        display_draw_dot(i, y);\n    }\n\n    display_flush();\n}<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4e2d <code>int32_t y = 63 - (p * 63 \/ 100000);<\/code> \u7528\u6765\u8ba1\u7b97\u529f\u7387\u503c\u5bf9\u5e94\u5230\u5c4f\u5e55\u4e0a\u70b9\u7684\u4f4d\u7f6e\uff0c\u56e0\u4e3a\u5c4f\u5e55\u7684 Y \u8f74\u5750\u6807\u662f\u5411\u4e0b\u589e\u957f\u7684\uff0c\u800c\u7ed8\u5236\u66f2\u7ebf\u65f6\uff0cY \u8f74\u662f\u5411\u4e0a\u589e\u957f\u7684\uff0c\u56e0\u6b64\u9700\u8981\u4f7f\u7528\u5c4f\u5e55\u9ad8\u5ea6\u51cf\u53bb\u529f\u7387\u5360\u6700\u5927\u529f\u7387\u7684\u6bd4\u4f8b\u3002<\/p>\n<p>\u53e6\u5916\u8fd9\u91cc\u7684 Y \u8f74\u4e0a\u9650\u4f7f\u7528\u4e86 100000mW \u4f5c\u4e3a\u53c2\u8003\uff0c\u5373\u6211\u4eec\u7684 USB \u7535\u6d41\u6700\u5927\u53ef\u4ee5\u6d4b\u91cf 100W \u7684\u529f\u7387\uff0c\u5f53\u7136\u8fd9\u4f1a\u5bfc\u81f4 Y \u8f74\u7684\u5206\u8fa8\u7387\u53d8\u4f4e\uff0c\u6bcf\u4e2a\u50cf\u7d20\u70b9\u53ef\u4ee5\u8868\u793a\u7ea6 1.5W \u7684\u6570\u636e\u8303\u56f4\u3002\u5982\u679c\u5b9e\u9645\u4f7f\u7528\u8fc7\u7a0b\u4e2d\u4e0d\u4f1a\u6709\u8fd9\u4e48\u5927\u7684\u529f\u7387\u6d4b\u91cf\u9700\u6c42\uff0c\u4e5f\u53ef\u4ee5\u964d\u4f4e\u4e0a\u9650\u3002<\/p>\n<h2><span class=\"ez-toc-section\" id=\"%E5%AE%9E%E9%99%85%E8%BF%90%E8%A1%8C%E6%95%88%E6%9E%9C\"><\/span>\u5b9e\u9645\u8fd0\u884c\u6548\u679c<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u8fd9\u91cc\u4e3a\u4e86\u66f4\u597d\u5730\u5c55\u793a\u66f2\u7ebf\u6548\u679c\uff0c\u4f7f\u7528\u4e86 30W \u7684\u529f\u7387\u4e0a\u9650\uff0c\u5c06\u5e76\u4f7f\u7528\u7535\u5b50\u8d1f\u8f7d\u4eea\u6a21\u62df\u4e86\u529f\u7387\u53d8\u5316\uff0c\u53ef\u4ee5\u770b\u5230\u529f\u7387\u7684\u5386\u53f2\u66f2\u7ebf\u5df2\u7ecf\u6b63\u786e\u7684\u7ed8\u5236\u51fa\u6765\u4e86\u3002<\/p>\n<p><img src=\"https:\/\/xujiwei.com\/blog\/wp-content\/uploads\/2024\/04\/diy-usb-meter-8-4.jpg\" alt=\"diy-usb-meter-8-4\" style=\"zoom:50%;\" \/><\/p>\n<p>\u5728\u5b9e\u9645\u4f7f\u7528\u65f6\uff0c\u6839\u636e 128 \u4e2a\u6570\u636e\u70b9\uff0c\u4ee5\u53ca 1 \u5206\u949f\u7684\u8bb0\u5f55\u95f4\u9694\u6765\u8ba1\u7b97\uff0c\u4e00\u5c4f\u53ef\u4ee5\u663e\u793a\u5386\u53f2 2 \u5c0f\u65f6\u7684\u529f\u7387\u53d8\u5316\u8bb0\u5f55\u3002<\/p>\n<h2><span class=\"ez-toc-section\" id=\"%E5%B0%8F%E7%BB%93\"><\/span>\u5c0f\u7ed3<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u5728\u5b8c\u6210\u529f\u7387\u66f2\u7ebf\u7ed8\u5236\u540e\uff0c\u6211\u4eec\u5df2\u7ecf\u5b8c\u6210\u4e86 DIY USB \u7535\u6d41\u8868\u56fa\u4ef6\u7684\u5927\u90e8\u5206\u529f\u80fd\u5f00\u53d1\u5de5\u4f5c\uff0c\u5e76\u4e14\u5df2\u7ecf\u4f7f\u7528\u5230\u4e86 PCB \u4e0a\u7684\u6240\u6709\u786c\u4ef6\uff0c\u5305\u62ec\u5c4f\u5e55\u3001INA219\u3001\u6309\u952e\u3002<\/p>\n<p>\u56fa\u4ef6\u4e0a\u8fd8\u6709\u4ec0\u4e48\u529f\u80fd\u53ef\u4ee5\u5b8c\u5584\u4e00\u4e0b\u5462\uff1f\u76ee\u524d\u8fd9\u4e2a USB \u7535\u6d41\u8868\u7684\u6570\u636e\u90fd\u53ea\u4fdd\u5b58\u5728\u5185\u5b58\u4e2d\uff0c\u4e00\u65e6\u7535\u6e90\u8f93\u5165\u65ad\u7535\uff0c\u6216\u8005\u662f\u8d1f\u8f7d\u505c\u6b62\u8017\u7535\uff0c\u6574\u4e2a USB \u7535\u6d41\u8868\u4e2d\u8bb0\u5f55\u7684\u6570\u636e\u90fd\u4f1a\u4e22\u5931\uff0c\u5982\u679c\u6211\u4eec\u60f3\u8981\u901a\u8fc7\u8fd9\u4e2a USB \u7535\u6d41\u8868\u6765\u7edf\u8ba1\u4e00\u4e2a\u5145\u7535\u5b9d\u5b9e\u9645\u8f93\u51fa\u7684\u7535\u91cf\uff0c\u90a3\u4e48\u5c31\u4e0d\u80fd\u8fbe\u5230\u76ee\u6807\u4e86\u3002<\/p>\n<p>\u4e0b\u4e00\u7bc7\u6211\u4eec\u6765\u7ed9 USB \u7535\u6d41\u8868\u52a0\u4e0a\u6570\u636e\u4fdd\u5b58\u529f\u80fd\uff0c\u4f7f\u5b83\u53ef\u4ee5\u6210\u4e3a\u4e00\u4e2a\u7535\u6c60\u5bb9\u91cf\u6d4b\u91cf\u5de5\u5177\u3002<\/p>\n<h2><span class=\"ez-toc-section\" id=\"USB_%E7%94%B5%E6%B5%81%E8%A1%A8%E5%BC%80%E6%BA%90%E5%9C%B0%E5%9D%80\"><\/span>USB \u7535\u6d41\u8868\u5f00\u6e90\u5730\u5740<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u8fd9\u4e2a USB \u7535\u6d41\u8868\u6240\u6709\u8d44\u6599\u5df2\u7ecf\u5f00\u6e90\uff0c\u53ef\u4ee5\u5728\u4ee5\u4e0b\u4ed3\u5e93\u4e2d\u83b7\u53d6\uff0c\u5305\u542b\u56fa\u4ef6\u4ee3\u7801\u3001PCB \u751f\u4ea7 Gerber \u6587\u4ef6\u3001\u539f\u7406\u56fe\u548c\u5916\u58f3 STL \u6587\u4ef6\u3002<\/p>\n<p><a href=\"https:\/\/github.com\/ohdarling\/CH32V003-USBMeter\">https:\/\/github.com\/ohdarling\/CH32V003-USBMeter<\/a><\/p>\n<p>\u786c\u4ef6\u76f8\u5173\u7684\u6e90\u6587\u4ef6\u5df2\u7ecf\u5728\u7acb\u521b\u5f00\u6e90\u5e73\u53f0\u5f00\u6e90\uff0c\u8bbf\u95ee\u4ee5\u4e0b\u5730\u5740\u53ef\u4ee5\u8fdb\u884c\u4e00\u952e PCB \u4e0b\u5355\u548c\u4e00\u952e BOM \u914d\u5355\u64cd\u4f5c\uff1a<\/p>\n<p><a href=\"https:\/\/oshwhub.com\/wandaeda\/ji-yu-ch32v003-de-usb-dian-liu-biao\">https:\/\/oshwhub.com\/wandaeda\/ji-yu-ch32v003-de-usb-dian-liu-biao<\/a><\/p>\n<h2><span class=\"ez-toc-section\" id=\"DIY_USB_%E7%94%B5%E6%B5%81%E8%A1%A8%E7%B3%BB%E5%88%97\"><\/span>DIY USB \u7535\u6d41\u8868\u7cfb\u5217<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<ul>\n<li><a href=\"https:\/\/xujiwei.com\/blog\/2024\/04\/diy-usb-meter-read-ina219\/\">DIY USB \u7535\u6d41\u8868\uff087\uff09\uff1a\u8bfb\u53d6\u548c\u663e\u793a INA219 \u7535\u6d41\u7535\u538b\u6570\u636e<\/a> <\/li>\n<li><a href=\"https:\/\/xujiwei.com\/blog\/2024\/04\/diy-usb-meter-oled-display\/\">DIY USB \u7535\u6d41\u8868\uff086\uff09\uff1a\u70b9\u4e2a\u5c4f\uff0c\u4f7f\u7528 I2C \u9a71\u52a8 0.96 \u5bf8 OLED<\/a> <\/li>\n<li><a href=\"https:\/\/xujiwei.com\/blog\/2024\/04\/diy-usb-meter-setup-fw-dev\/\">DIY USB \u7535\u6d41\u8868\uff085\uff09\uff1a\u4f7f\u7528 VSCode + PlatformIO \u642d\u5efa\u56fa\u4ef6\u5f00\u53d1\u73af\u5883<\/a><\/li>\n<li><a href=\"https:\/\/xujiwei.com\/blog\/2024\/03\/diy-usb-meter-pcb-soldering\/\">DIY USB \u7535\u6d41\u8868\uff084\uff09\uff1aPCB \u710a\u63a5\u4e0e\u8c03\u8bd5<\/a><\/li>\n<li><a href=\"https:\/\/xujiwei.com\/blog\/2024\/03\/diy-usermeter-order-pcb\/\">DIY USB \u7535\u6d41\u8868\uff083\uff09\uff1aPCB \u514d\u8d39\u6253\u6837\u8be6\u89e3<\/a><\/li>\n<li><a href=\"https:\/\/xujiwei.com\/blog\/2024\/02\/diy-usb-meter-pcb-design\/\">DIY USB \u7535\u6d41\u8868\uff082\uff09\uff1aPCB \u5e03\u5c40\u5e03\u7ebf<\/a><\/li>\n<li><a href=\"https:\/\/xujiwei.com\/blog\/2024\/02\/diy-usb-meter-schematic\/\">DIY USB \u7535\u6d41\u8868\uff081\uff09\uff1a\u5143\u4ef6\u9009\u578b\u548c\u539f\u7406\u56fe\u7ed8\u5236<\/a><\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"%E5%85%B6%E4%BB%96_DIY_%E9%A1%B9%E7%9B%AE\"><\/span>\u5176\u4ed6 DIY \u9879\u76ee<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"30_%E5%85%83_DIY_%E4%B8%80%E4%B8%AA%E6%9F%94%E6%80%A7%E7%81%AF%E4%B8%9D%E6%B0%9B%E5%9B%B4%E7%81%AF\"><\/span><a href=\"https:\/\/xujiwei.com\/blog\/2024\/04\/diy-ambient-light\/\">30 \u5143 DIY \u4e00\u4e2a\u67d4\u6027\u706f\u4e1d\u6c1b\u56f4\u706f<\/a><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><img src=\"https:\/\/xujiwei.com\/blog\/wp-content\/uploads\/2024\/03\/diy-ambient-light-1.jpg\" alt=\"diy-ambient-light-1\" style=\"zoom:50%;\" \/><\/p>\n<p>\u6559\u7a0b\u5730\u5740\uff1a <a href=\"https:\/\/xujiwei.com\/blog\/2024\/04\/diy-ambient-light\/\">https:\/\/xujiwei.com\/blog\/2024\/04\/diy-ambient-light\/<\/a><\/p>\n<h2><span class=\"ez-toc-section\" id=\"%E5%8F%82%E8%80%83%E8%B5%84%E6%96%99\"><\/span>\u53c2\u8003\u8d44\u6599<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<ul>\n<li><a href=\"https:\/\/github.com\/wagiminator\/CH32V003-GameConsole\">https:\/\/github.com\/wagiminator\/CH32V003-GameConsole<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/ohdarling\/CH32V003-USBMeter\">https:\/\/github.com\/ohdarling\/CH32V003-USBMeter<\/a><\/li>\n<li><a href=\"https:\/\/oshwhub.com\/wandaeda\/ji-yu-ch32v003-de-usb-dian-liu-biao\">https:\/\/oshwhub.com\/wandaeda\/ji-yu-ch32v003-de-usb-dian-liu-biao<\/a><\/li>\n<li><a href=\"https:\/\/zhuanlan.zhihu.com\/p\/534098236\">https:\/\/zhuanlan.zhihu.com\/p\/534098236<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u524d\u4e00\u7bc7 \u300aDIY USB \u7535\u6d41\u8868\uff087\uff09\uff1a\u8bfb\u53d6\u548c\u663e\u793a INA219 \u7535\u6d41\u7535\u538b\u6570\u636e &hellip;<\/p>\n<p class=\"read-more\"><a href=\"https:\/\/xujiwei.com\/blog\/2024\/04\/diy-usb-meter-power-history\/\">\u7ee7\u7eed\u9605\u8bfb &raquo;<\/a><\/p>\n","protected":false},"author":2,"featured_media":1313,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[167,402],"tags":[359,169,329,372,371,387,386],"_links":{"self":[{"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/posts\/1317"}],"collection":[{"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/comments?post=1317"}],"version-history":[{"count":0,"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/posts\/1317\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/media\/1313"}],"wp:attachment":[{"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/media?parent=1317"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/categories?post=1317"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/tags?post=1317"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}