{"id":1673,"date":"2025-05-31T10:30:00","date_gmt":"2025-05-31T02:30:00","guid":{"rendered":"https:\/\/aoofoo.com\/?p=1673"},"modified":"2025-06-01T22:04:15","modified_gmt":"2025-06-01T14:04:15","slug":"6-4-%e5%9f%ba%e7%a1%80%e7%a8%8b%e5%ba%8f%e8%ae%b2%e8%a7%a3%ef%bc%88%e9%9d%9epid%ef%bc%89","status":"publish","type":"post","link":"https:\/\/aoofoo.com\/index.php\/2025\/05\/31\/6-4-%e5%9f%ba%e7%a1%80%e7%a8%8b%e5%ba%8f%e8%ae%b2%e8%a7%a3%ef%bc%88%e9%9d%9epid%ef%bc%89\/","title":{"rendered":"6.4 \u57fa\u7840\u7a0b\u5e8f\u8bb2\u89e3\uff08\u65f6\u57fa\u9a71\u52a8\u7684\u534f\u4f5c\u5f0f\u8c03\u5ea6\uff09"},"content":{"rendered":"<h2>\u65f6\u57fa\u9a71\u52a8\u7684\u534f\u4f5c\u5f0f\u8c03\u5ea6<\/h2>\n<p>\u6211\u4f7f\u7528\u7684\u662f\u88f8\u673a\u64cd\u4f5c\uff0c\u5e76\u6ca1\u6709\u4f7f\u7528RTOS\u64cd\u4f5c\u7cfb\u7edf\u5e2e\u52a9\u534f\u540c\u5de5\u4f5c\uff0c\u4f46\u662f\u4e3a\u4e86\u63d0\u9ad8\u6548\u7387\uff0c\u672c\u6b21\u4ee3\u7801\u5e76\u4e0d\u4ee5\u201c\u524d\u540e\u53f0\u4ee3\u7801\u98ce\u683c\u201d\u6765\u4e66\u5199\u4ee3\u7801\uff0c\u800c\u662f\u4ee5\u65f6\u57fa\u4ee3\u7801\u4f5c\u4e3a\u672c\u6b21\u8bfe\u7a0b\u7684\u8bb2\u89e3\u3002<\/p>\n<p>\u5728RTOS\u4e2d\u6211\u4eec\u77e5\u9053\u5b58\u5728\u4e00\u4e2a\u673a\u5236\uff0c\u5373\u6bcf1ms\u53ef\u4ee5\u5207\u6362\u4e0d\u540c\u7684\u4efb\u52a1\u6267\u884c\uff0c\u8ba9\u7a0b\u5e8f\u770b\u8d77\u6765\u5e76\u6ca1\u6709\u963b\u585e\uff0c\u800c\u5728\u88f8\u673a\u4e2d\uff0c\u60f3\u8981\u5b9e\u73b0\u8fd9\u6837\u7684\u64cd\u4f5c\u5f88\u56f0\u96be\uff0c\u6211\u5c31\u9000\u800c\u6c42\u5176\u6b21\uff0c\u5236\u4f5c\u4e00\u4e2a\u4efb\u52a1\u7ed3\u6784\u4f53\uff0c\u8fd9\u4e2a\u7ed3\u6784\u4f53\u91cc\u9762\u6709\uff1a\u4efb\u52a1\u51fd\u6570\u3001\u4efb\u52a1\u89c4\u5b9a\u8fd0\u884c\u65f6\u95f4\u3001\u4efb\u52a1\u5df2\u8fd0\u884c\u7684\u65f6\u95f4\uff0c\u901a\u8fc7\u7ed3\u6784\u4f53\u4e2d\u7684\u8fd9\u4e09\u4e2a\u5143\u7d20\uff0c\u6765\u8ba9\u4efb\u52a1\u5728\u89c4\u5b9a\u65f6\u95f4\u5185\u8fd0\u884c\uff0c\u867d\u65e0\u6cd5\u8fbe\u5230\u7c7b\u4f3cRTOS\u90a3\u68371ms\u5185\u7684\u591a\u6b21\u4efb\u52a1\u5207\u6362\uff0c\u4f46\u5bf9\u4e8e\u5927\u90e8\u5206\u88f8\u673a\u4efb\u52a1\u800c\u8a00\u5df2\u7ecf\u8db3\u591f\u3002<\/p>\n<p>\u5728\u672c\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4f7f\u7528\u7684\u662f<strong>\u88f8\u673a<\/strong>\u5f00\u53d1\uff0c\u6ca1\u6709\u5f15\u5165\u4efb\u4f55 RTOS\u3002\u4e3a\u4e86\u66f4\u9ad8\u7684\u6267\u884c\u6548\u7387\uff0c\u4e5f\u907f\u514d\u4f20\u7edf\u201c\u524d\u540e\u53f0\uff08Foreground\/Background\uff09\u201d\u98ce\u683c\u5e26\u6765\u7684\u590d\u6742\u6027\uff0c\u6211\u91c7\u7528\u4e86<strong>\u65f6\u57fa\u9a71\u52a8\u7684\u534f\u4f5c\u5f0f\u8c03\u5ea6<\/strong>\u65b9\u5f0f\u6765\u6f14\u793a\u3002<\/p>\n<ul>\n<li>\u6211\u7528\u4e00\u4e2a 1 ms \u4e00\u6b21\u7684\u5b9a\u65f6\u5668\u4e2d\u65ad\u9012\u589e\u5168\u5c40\u65f6\u57fa <code>g_sys_tick<\/code>\u3002<\/li>\n<li>\u5b9a\u4e49\u4e00\u4e2a\u4efb\u52a1\u63a7\u5236\u5757 <code>Task_t<\/code>\uff0c\u5305\u542b\n<ol>\n<li><code>func<\/code>\uff1a\u4efb\u52a1\u5165\u53e3\u51fd\u6570\uff0c<\/li>\n<li><code>period<\/code>\uff1a\u5468\u671f\uff08ms\uff09\uff0c<\/li>\n<li><code>last_run<\/code>\uff1a\u4e0a\u6b21\u6267\u884c\u7684\u65f6\u523b\uff08tick \u503c\uff09\u3002<\/li>\n<\/ol>\n<\/li>\n<li>\u5728\u4e3b\u5faa\u73af\u91cc\u4e0d\u65ad\u8bfb\u53d6\u5f53\u524d\u65f6\u57fa <code>now = g_sys_tick<\/code>\uff0c\u904d\u5386\u6240\u6709\u4efb\u52a1\uff1a<br \/>\nif ((now \u2212 last_run) \u2265 period) \u5c31\u6267\u884c\u8be5\u4efb\u52a1\uff0c\u5e76\u66f4\u65b0 <code>last_run = now<\/code>\u3002<\/li>\n<\/ul>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u662f<strong>\u534f\u4f5c\u5f0f\uff08co-operative\uff09<\/strong>\u3001\u975e\u62a2\u5360\u5f0f\uff08non-preemptive\uff09\u8c03\u5ea6\uff0c\u4e0d\u4f9d\u8d56\u64cd\u4f5c\u7cfb\u7edf\u7684\u4e0a\u4e0b\u6587\u5207\u6362\u3002\u867d\u7136\u5b83\u8fbe\u4e0d\u5230 RTOS \u90a3\u6837\u4e25\u683c\u7684\u6bcf 1 ms \u5207\u6362\u591a\u4efb\u52a1\uff0c\u4f46\u5bf9\u4e8e\u7edd\u5927\u591a\u6570\u88f8\u673a\u573a\u666f\u4e0b\u7684\u5b9a\u65f6\uff0f\u5468\u671f\u6027\u4efb\u52a1\uff0c\u7cbe\u5ea6\u548c\u6548\u7387\u90fd\u5df2\u7ef0\u7ef0\u6709\u4f59\u3002<\/p>\n<h3>\u88f8\u673a\u65f6\u57fa\u4ee3\u7801\u793a\u4f8b<\/h3>\n<pre class=\"corepress-code-pre\"><code>typedef void (*TaskFunc)(void);\r\n\r\ntypedef struct {\r\n    TaskFunc  func;      \/\/ \u4efb\u52a1\u5165\u53e3\r\n    uint32_t  period;    \/\/ \u5468\u671f\uff0c\u5355\u4f4d ms\r\n    uint32_t  last_run;  \/\/ \u4e0a\u6b21\u6267\u884c\u65f6\u523b\uff08tick\uff09\r\n} Task_t;\r\n\r\n#define TASK_NUM  2\r\n\r\nextern volatile uint32_t g_sys_tick;  \/\/ 1ms \u589e\u52a0\u4e00\u6b21\r\n\r\nvoid Task1(void);\r\nvoid Task2(void);\r\n\r\nTask_t tasks[TASK_NUM] = {\r\n    { Task1, 1, 0 },\r\n    { Task2, 1, 0 },\r\n};\r\n\r\nint main(void) {\r\n    Timer1_Init_1ms();   \/\/ \u542f\u52a8 1 ms \u5b9a\u65f6\u4e2d\u65ad\r\n    while (1) {\r\n        uint32_t now = g_sys_tick;\r\n        for (int i = 0; i &lt; TASK_NUM; i++) {\r\n            if ((uint32_t)(now - tasks[i].last_run) &gt;= tasks[i].period) {\r\n                tasks[i].last_run = now;\r\n                tasks[i].func();\r\n            }\r\n        }\r\n    }\r\n}<\/code><\/pre>\n<h2>\u975e\u963b\u585e\u5f0f\u6309\u952e<\/h2>\n<p>\u56e0\u4e3a\u6211\u4eec\u5e76\u4e0d\u662f\u4e25\u683c\u7684\u6309\u7167ms\u6765\u8fdb\u884c\u8ba1\u6570\u7684\uff0c\u6240\u4ee5Count\u7684\u5177\u4f53\u503c\u9700\u8981\u81ea\u5df1\u8fdb\u884c\u8c03\u8bd5\uff0c\u4f46\u662f\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u628aKey_Tick\u653e\u5728\u65f6\u57fa\u5b9a\u65f6\u5668\u7684\u4e2d\u65ad\u4e2d\uff0c\u800c\u4e0d\u662f\u5728\u4efb\u52a1\u4e2d\u8c03\u7528\uff0c\u8fd9\u6837Count\u53ef\u4ee5\u66f4\u52a0\u7cbe\u786e\u7684\u63a5\u8fd1ms\u7684\u5b9a\u4e49\u3002<\/p>\n<h3>\u975e\u963b\u585e\u6309\u952e\u4ee3\u7801\u793a\u4f8b<\/h3>\n<pre class=\"corepress-code-pre\"><code>void Key_Tick(void)\r\n{\r\n\tstatic uint8_t Count;\r\n\tstatic uint8_t CurrState, PrevState;\r\n\tCount ++;\r\n\t\r\n\t\/* \u7d2f\u8ba1\u5230\u4e00\u5b9a\u6b21\u6570\uff08\u7ea6 20 ms\uff09\u624d\u53bb\u505a\u4e00\u6b21\u626b\u63cf\uff0c\u8d77\u5230\u7c97\u7565\u9632\u6296\u548c\u51cf\u5c11 GPIO \u8bfb\u53d6\u9891\u6b21\u7684\u4f5c\u7528 *\/\r\n\tif (Count &gt;= 20)\r\n\t{\r\n\t\tCount = 0;\r\n\t\t\/\/\u6eda\u52a8\u8bb0\u5f55\u72b6\u6001\r\n\t\tPrevState = CurrState; \/\/ \u4e0a\u4e00\u6b21\u626b\u63cf\u65f6\u7684\u72b6\u6001\uff08\u67e5\u770b\u662f\u5426\u4e3a0\uff0c\u4e0d\u4e3a0\uff0c\u5982\u679c\u4e3a1~4\u5219\u4ee3\u8868\u6709\u6309\u952e\u88ab\u6309\u4e0b\uff09\r\n\t\tCurrState = Key_GetState();\/\/\u672c\u6b21\u626b\u63cf\uff1a\u54ea\u4e2a\u952e\u88ab\u6309\u4e0b\uff080~4\uff09\r\n\t\t\r\n\t\t\/*\u68c0\u6d4b \u4e0a\u4e00\u6b21\u6309\u4e0b\uff08PrevState!=0\uff09\u2192 \u672c\u6b21\u5168\u90e8\u677e\u5f00\uff08CurrState==0\uff09\u7684\u201c\u62ac\u8d77\u6cbf\u201d\r\n\t\t\t\u8fd9\u6837\u53ef\u4ee5\u4fdd\u8bc1\u4e00\u6b21\u5b8c\u6574\u7684\u201c\u6309\u4e0b\u2192\u62ac\u8d77\u201d\u540e\u624d\u4ea7\u751f\u4e00\u6b21\u6309\u952e\u4e8b\u4ef6*\/\r\n\t\tif (CurrState == 0 &amp;&amp; PrevState != 0)\r\n\t\t{\r\n\t\t\tKey_Num = PrevState;\r\n\t\t}\r\n\t}\r\n}<\/code><\/pre>\n<h2>\u5367\u5f0f\u7535\u4f4d\u5668<\/h2>\n<p>\u65cb\u94ae\uff08\u5367\u5f0f\u7535\u4f4d\u5668\uff09\uff0c\u6211\u4eec\u751a\u81f3\u4e0d\u9700\u8981\u65b0\u5efa\u4efb\u52a1\uff0c\u4f7f\u7528AD\u6765\u6d4b\u91cf\u7535\u4f4d\uff0c\u6700\u5de6\u7aef\u662f0\uff0c\u6700\u53f3\u7aef\u662f4095\uff0c\u5199\u4e00\u4e2a\u51fd\u6570\u76f4\u63a5\u653e\u5728\u5c4f\u5e55\u663e\u793a\u7684\u4efb\u52a1\u4e2d\u65e2\u53ef\u3002<\/p>\n<h2>\u7535\u673a\u7f16\u7801\u5668<\/h2>\n<p>\u7535\u673a\u7684\u7f16\u7801\u5668\u7528\u6765\u6d4b\u91cf\u5b9e\u9645\u7684\u4f4d\u7f6e\u548c\u901f\u5ea6\u3002<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u65f6\u57fa\u9a71\u52a8\u7684\u534f\u4f5c\u5f0f\u8c03\u5ea6 \u6211\u4f7f\u7528\u7684\u662f\u88f8\u673a\u64cd\u4f5c\uff0c\u5e76\u6ca1\u6709\u4f7f\u7528RTOS\u64cd\u4f5c\u7cfb\u7edf\u5e2e\u52a9\u534f\u540c\u5de5\u4f5c\uff0c\u4f46\u662f\u4e3a\u4e86\u63d0\u9ad8\u6548\u7387\uff0c\u672c\u6b21\u4ee3\u7801\u5e76 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31],"tags":[],"class_list":["post-1673","post","type-post","status-publish","format-standard","hentry","category-pid"],"_links":{"self":[{"href":"https:\/\/aoofoo.com\/index.php\/wp-json\/wp\/v2\/posts\/1673","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/aoofoo.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/aoofoo.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/aoofoo.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/aoofoo.com\/index.php\/wp-json\/wp\/v2\/comments?post=1673"}],"version-history":[{"count":8,"href":"https:\/\/aoofoo.com\/index.php\/wp-json\/wp\/v2\/posts\/1673\/revisions"}],"predecessor-version":[{"id":1682,"href":"https:\/\/aoofoo.com\/index.php\/wp-json\/wp\/v2\/posts\/1673\/revisions\/1682"}],"wp:attachment":[{"href":"https:\/\/aoofoo.com\/index.php\/wp-json\/wp\/v2\/media?parent=1673"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/aoofoo.com\/index.php\/wp-json\/wp\/v2\/categories?post=1673"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/aoofoo.com\/index.php\/wp-json\/wp\/v2\/tags?post=1673"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}